X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=daemon%2Finotify.c;h=3fc7b92b437bda6ce43429b77a1d7950dc138438;hb=425374ddc84c4f9f74a5218e8d35452bb511d9f3;hp=465d0b6ec6f9b991bba4058b42a751bf8dd509b9;hpb=edb9b3abc03c0a0f84b1cbd9cf5920e3c84e5c18;p=libguestfs.git diff --git a/daemon/inotify.c b/daemon/inotify.c index 465d0b6..3fc7b92 100644 --- a/daemon/inotify.c +++ b/daemon/inotify.c @@ -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 @@ -28,9 +28,10 @@ #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. */ @@ -38,6 +39,18 @@ 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. */ @@ -57,10 +70,10 @@ do_inotify_init (int max_events) #ifdef HAVE_SYS_INOTIFY_H FILE *fp; - NEED_ROOT (return -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; } @@ -81,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 @@ -106,8 +119,7 @@ do_inotify_init (int max_events) return 0; #else - reply_with_error ("%s is not available", __func__); - return -1; + NOT_AVAILABLE (-1); #endif } @@ -118,7 +130,7 @@ do_inotify_close (void) 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; } @@ -132,8 +144,7 @@ do_inotify_close (void) return 0; #else - reply_with_error ("%s is not available", __func__); - return -1; + NOT_AVAILABLE (-1); #endif } @@ -155,14 +166,13 @@ do_inotify_add_watch (const 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 - reply_with_error ("%s is not available", __func__); - return -1; + NOT_AVAILABLE (-1); #endif } @@ -173,14 +183,13 @@ do_inotify_rm_watch (int wd) 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 - reply_with_error ("%s is not available", __func__); - return -1; + NOT_AVAILABLE (-1); #endif } @@ -221,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; } @@ -294,8 +303,7 @@ do_inotify_read (void) free (ret); return NULL; #else - reply_with_error ("%s is not available", __func__); - return NULL; + NOT_AVAILABLE (NULL); #endif } @@ -306,13 +314,24 @@ do_inotify_files (void) 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; @@ -341,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; } @@ -353,25 +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 - reply_with_error ("%s is not available", __func__); - return NULL; + NOT_AVAILABLE (NULL); #endif }