X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=daemon%2Finotify.c;h=807fb2f59aee2310d07f64d4ada2c39e7f6f7ddb;hp=4bc6f17918b7767195727b88786a14f51e2db742;hb=61cfe13ec49f1d1e9d6124f0ee98520aab720087;hpb=2f1a50d81671810256dce0852e6b1e0810ac44af diff --git a/daemon/inotify.c b/daemon/inotify.c index 4bc6f17..807fb2f 100644 --- a/daemon/inotify.c +++ b/daemon/inotify.c @@ -21,17 +21,37 @@ #include #include #include +#include +#include + +#ifdef HAVE_SYS_INOTIFY_H #include +#endif -#include "../src/guestfs_protocol.h" +#include "guestfs_protocol.h" #include "daemon.h" #include "actions.h" +#include "optgroups.h" +#ifdef HAVE_SYS_INOTIFY_H /* Currently open inotify handle, or -1 if not opened. */ static int inotify_fd = -1; static char inotify_buf[64*1024*1024]; /* Event buffer, [0..posn-1] is valid */ -static int inotify_posn = 0; +static size_t inotify_posn = 0; + +int +optgroup_inotify_available (void) +{ + return 1; +} +#else /* !HAVE_SYS_INOTIFY_H */ +int +optgroup_inotify_available (void) +{ + return 0; +} +#endif /* Because inotify_init does NEED_ROOT, NEED_INOTIFY implies NEED_ROOT. */ #define NEED_INOTIFY(errcode) \ @@ -47,12 +67,13 @@ static int inotify_posn = 0; int do_inotify_init (int max_events) { +#ifdef HAVE_SYS_INOTIFY_H FILE *fp; - NEED_ROOT (-1); + NEED_ROOT (0, return -1); if (max_events < 0) { - reply_with_error ("inotify_init: max_events < 0"); + reply_with_error ("max_events < 0"); return -1; } @@ -70,22 +91,46 @@ do_inotify_init (int max_events) if (do_inotify_close () == -1) return -1; +#ifdef HAVE_INOTIFY_INIT1 inotify_fd = inotify_init1 (IN_NONBLOCK | IN_CLOEXEC); if (inotify_fd == -1) { + reply_with_perror ("inotify_init1"); + return -1; + } +#else + inotify_fd = inotify_init (); + if (inotify_fd == -1) { reply_with_perror ("inotify_init"); return -1; } + if (fcntl (inotify_fd, F_SETFL, O_NONBLOCK) == -1) { + reply_with_perror ("fcntl: O_NONBLOCK"); + close (inotify_fd); + inotify_fd = -1; + return -1; + } + if (fcntl (inotify_fd, F_SETFD, FD_CLOEXEC) == -1) { + reply_with_perror ("fcntl: FD_CLOEXEC"); + close (inotify_fd); + inotify_fd = -1; + return -1; + } +#endif return 0; +#else + NOT_AVAILABLE (-1); +#endif } int do_inotify_close (void) { +#ifdef HAVE_SYS_INOTIFY_H NEED_INOTIFY (-1); if (inotify_fd == -1) { - reply_with_error ("inotify_close: handle is not open"); + reply_with_error ("handle is not open"); return -1; } @@ -98,16 +143,19 @@ do_inotify_close (void) inotify_posn = 0; return 0; +#else + NOT_AVAILABLE (-1); +#endif } int64_t -do_inotify_add_watch (char *path, int mask) +do_inotify_add_watch (const char *path, int mask) { +#ifdef HAVE_SYS_INOTIFY_H int64_t r; char *buf; NEED_INOTIFY (-1); - ABS_PATH (path, -1); buf = sysroot_path (path); if (!buf) { @@ -118,29 +166,37 @@ do_inotify_add_watch (char *path, int mask) r = inotify_add_watch (inotify_fd, buf, mask); free (buf); if (r == -1) { - reply_with_perror ("inotify_add_watch: %s", path); + reply_with_perror ("%s", path); return -1; } return r; +#else + NOT_AVAILABLE (-1); +#endif } int do_inotify_rm_watch (int wd) { +#ifdef HAVE_SYS_INOTIFY_H NEED_INOTIFY (-1); if (inotify_rm_watch (inotify_fd, wd) == -1) { - reply_with_perror ("inotify_rm_watch: %d", wd); + reply_with_perror ("%d", wd); return -1; } return 0; +#else + NOT_AVAILABLE (-1); +#endif } guestfs_int_inotify_event_list * do_inotify_read (void) { +#ifdef HAVE_SYS_INOTIFY_H int space; guestfs_int_inotify_event_list *ret; @@ -162,7 +218,8 @@ do_inotify_read (void) while (space > 0) { struct inotify_event *event; - int n, r; + int r; + size_t n; r = read (inotify_fd, inotify_buf + inotify_posn, sizeof (inotify_buf) - inotify_posn); @@ -173,7 +230,7 @@ do_inotify_read (void) goto error; } if (r == 0) { /* End of file - we're not expecting it. */ - reply_with_error ("inotify_read: unexpected end of file"); + reply_with_error ("unexpected end of file"); goto error; } @@ -245,14 +302,18 @@ do_inotify_read (void) xdr_free ((xdrproc_t) xdr_guestfs_int_inotify_event_list, (char *) ret); free (ret); return NULL; +#else + NOT_AVAILABLE (NULL); +#endif } char ** do_inotify_files (void) { +#ifdef HAVE_SYS_INOTIFY_H char **ret = NULL; int size = 0, alloc = 0; - int i; + unsigned int i; FILE *fp; guestfs_int_inotify_event_list *events; char buf[PATH_MAX]; @@ -317,4 +378,7 @@ do_inotify_files (void) error: unlink ("/tmp/inotify"); return NULL; +#else + NOT_AVAILABLE (NULL); +#endif }