tune2fs-l: Add a test.
[libguestfs.git] / daemon / inotify.c
index 2f22f52..3fc7b92 100644 (file)
@@ -1,5 +1,5 @@
 /* libguestfs - the guestfsd daemon
- * Copyright (C) 2009 Red Hat Inc.
+ * Copyright (C) 2009-2011 Red Hat Inc.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -13,7 +13,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <config.h>
 #include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
+
+#ifdef HAVE_SYS_INOTIFY_H
 #include <sys/inotify.h>
+#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 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)                                          \
   do {                                                                 \
@@ -49,12 +67,13 @@ static size_t inotify_posn = 0;
 int
 do_inotify_init (int max_events)
 {
+#ifdef HAVE_SYS_INOTIFY_H
   FILE *fp;
 
-  NEED_ROOT (-1);
+  NEED_ROOT (, return -1);
 
   if (max_events < 0) {
-    reply_with_error ("inotify_init: max_events < 0");
+    reply_with_error ("max_events < 0");
     return -1;
   }
 
@@ -75,7 +94,7 @@ do_inotify_init (int max_events)
 #ifdef HAVE_INOTIFY_INIT1
   inotify_fd = inotify_init1 (IN_NONBLOCK | IN_CLOEXEC);
   if (inotify_fd == -1) {
-    reply_with_perror ("inotify_init");
+    reply_with_perror ("inotify_init1");
     return -1;
   }
 #else
@@ -99,15 +118,19 @@ do_inotify_init (int max_events)
 #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;
   }
 
@@ -120,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, return -1);
 
   buf = sysroot_path (path);
   if (!buf) {
@@ -140,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;
 
@@ -196,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;
     }
 
@@ -268,21 +302,36 @@ 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;
   unsigned int i;
-  FILE *fp;
+  FILE *fp = NULL;
   guestfs_int_inotify_event_list *events;
   char buf[PATH_MAX];
+  char tempfile[] = "/tmp/inotifyXXXXXX";
+  int fd;
+  char cmd[64];
 
   NEED_INOTIFY (NULL);
 
-  fp = popen ("sort -u > /tmp/inotify", "w");
+  fd = mkstemp (tempfile);
+  if (fd == -1) {
+    reply_with_perror ("mkstemp");
+    return NULL;
+  }
+
+  snprintf (cmd, sizeof cmd, "sort -u > %s", tempfile);
+
+  fp = popen (cmd, "w");
   if (fp == NULL) {
     reply_with_perror ("sort");
     return NULL;
@@ -311,9 +360,11 @@ do_inotify_files (void)
 
   pclose (fp);
 
-  fp = fopen ("/tmp/inotify", "r");
+  fp = fdopen (fd, "r");
   if (fp == NULL) {
-    reply_with_perror ("/tmp/inotify");
+    reply_with_perror ("%s", tempfile);
+    unlink (tempfile);
+    close (fd);
     return NULL;
   }
 
@@ -323,21 +374,26 @@ do_inotify_files (void)
     if (len > 0 && buf[len-1] == '\n')
       buf[len-1] = '\0';
 
-    if (add_string (&ret, &size, &alloc, buf) == -1) {
-      fclose (fp);
+    if (add_string (&ret, &size, &alloc, buf) == -1)
       goto error;
-    }
   }
 
-  fclose (fp);
+  fclose (fp); /* implicitly closes fd */
+  fp = NULL;
 
   if (add_string (&ret, &size, &alloc, NULL) == -1)
     goto error;
 
-  unlink ("/tmp/inotify");
+  unlink (tempfile);
   return ret;
 
  error:
-  unlink ("/tmp/inotify");
+  if (fp != NULL)
+    fclose (fp);
+
+  unlink (tempfile);
   return NULL;
+#else
+  NOT_AVAILABLE (NULL);
+#endif
 }