daemon: enable -Werror and many gcc warnings when --enable-gcc-warnings
[libguestfs.git] / daemon / inotify.c
index 4bc6f17..3e314f0 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)                                          \
@@ -49,7 +51,7 @@ do_inotify_init (int max_events)
 {
   FILE *fp;
 
-  NEED_ROOT (-1);
+  NEED_ROOT (return -1);
 
   if (max_events < 0) {
     reply_with_error ("inotify_init: max_events < 0");
@@ -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;
 }
@@ -101,13 +123,12 @@ do_inotify_close (void)
 }
 
 int64_t
-do_inotify_add_watch (char *path, int mask)
+do_inotify_add_watch (const char *path, int mask)
 {
   int64_t r;
   char *buf;
 
   NEED_INOTIFY (-1);
-  ABS_PATH (path, -1);
 
   buf = sysroot_path (path);
   if (!buf) {
@@ -162,7 +183,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);
@@ -252,7 +274,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];