build: enable automake's silent rules option
[libguestfs.git] / daemon / inotify.c
index 43f282d..8bb8ed2 100644 (file)
@@ -21,6 +21,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
 #include <sys/inotify.h>
 
 #include "../src/guestfs_protocol.h"
@@ -31,7 +33,7 @@
 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;
 
 /* Because inotify_init does NEED_ROOT, NEED_INOTIFY implies NEED_ROOT. */
 #define NEED_INOTIFY(errcode)                                          \
@@ -70,11 +72,31 @@ 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_init");
     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;
 }
@@ -162,13 +184,14 @@ 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);
+              sizeof (inotify_buf) - inotify_posn);
     if (r == -1) {
       if (errno == EWOULDBLOCK || errno == EAGAIN) /* End of list. */
-       break;
+        break;
       reply_with_perror ("read");
       goto error;
     }
@@ -190,18 +213,18 @@ do_inotify_read (void)
       /* Have we got a complete event in the buffer? */
 #ifdef __GNUC__
       if (n + sizeof (struct inotify_event) > inotify_posn ||
-         n + sizeof (struct inotify_event) + event->len > inotify_posn)
-       break;
+          n + sizeof (struct inotify_event) + event->len > inotify_posn)
+        break;
 #else
 #error "this code needs fixing so it works on non-GCC compilers"
 #endif
 
       np = realloc (ret->guestfs_int_inotify_event_list_val,
-                   (ret->guestfs_int_inotify_event_list_len + 1) *
-                   sizeof (guestfs_int_inotify_event));
+                    (ret->guestfs_int_inotify_event_list_len + 1) *
+                    sizeof (guestfs_int_inotify_event));
       if (np == NULL) {
-       reply_with_perror ("realloc");
-       goto error;
+        reply_with_perror ("realloc");
+        goto error;
       }
       ret->guestfs_int_inotify_event_list_val = np;
       in = &ret->guestfs_int_inotify_event_list_val[ret->guestfs_int_inotify_event_list_len];
@@ -212,12 +235,12 @@ do_inotify_read (void)
       in->in_cookie = event->cookie;
 
       if (event->len > 0)
-       in->in_name = strdup (event->name);
+        in->in_name = strdup (event->name);
       else
-       in->in_name = strdup (""); /* Should have optional string fields XXX. */
+        in->in_name = strdup (""); /* Should have optional string fields XXX. */
       if (in->in_name == NULL) {
-       reply_with_perror ("strdup");
-       goto error;
+        reply_with_perror ("strdup");
+        goto error;
       }
 
       /* Estimate space used by this event in the message. */
@@ -252,7 +275,7 @@ do_inotify_files (void)
 {
   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];
@@ -279,7 +302,7 @@ do_inotify_files (void)
       const char *name = events->guestfs_int_inotify_event_list_val[i].in_name;
 
       if (name[0] != '\0')
-       fprintf (fp, "%s\n", name);
+        fprintf (fp, "%s\n", name);
     }
 
     xdr_free ((xdrproc_t) xdr_guestfs_int_inotify_event_list, (char *) events);