From 4610db9f1b77f8c263b348fda1974be165a7f841 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 31 Jul 2009 22:31:05 +0100 Subject: [PATCH] Add interface to Linux 'inotify' API. --- .gitignore | 1 + daemon/Makefile.am | 1 + daemon/inotify.c | 320 +++++++++++++++++++++++++++++++++++++++++++++++++++++ java/Makefile.inc | 2 +- po/POTFILES.in | 1 + src/MAX_PROC_NR | 2 +- src/generator.ml | 109 +++++++++++++++++- 7 files changed, 433 insertions(+), 3 deletions(-) create mode 100644 daemon/inotify.c diff --git a/.gitignore b/.gitignore index ecc39a5..a3b365f 100644 --- a/.gitignore +++ b/.gitignore @@ -96,6 +96,7 @@ java/com/redhat/et/libguestfs/Dirent.java java/com_redhat_et_libguestfs_GuestFS.c java/com_redhat_et_libguestfs_GuestFS.h java/com/redhat/et/libguestfs/GuestFS.java +java/com/redhat/et/libguestfs/INotifyEvent.java java/com/redhat/et/libguestfs/IntBool.java java/com/redhat/et/libguestfs/LV.java java/com/redhat/et/libguestfs/PV.java diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 6da485b..024c097 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -46,6 +46,7 @@ guestfsd_SOURCES = \ headtail.c \ hexdump.c \ initrd.c \ + inotify.c \ link.c \ ls.c \ lvm.c \ diff --git a/daemon/inotify.c b/daemon/inotify.c new file mode 100644 index 0000000..43f282d --- /dev/null +++ b/daemon/inotify.c @@ -0,0 +1,320 @@ +/* libguestfs - the guestfsd daemon + * Copyright (C) 2009 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * 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. + */ + +#include + +#include +#include +#include +#include + +#include "../src/guestfs_protocol.h" +#include "daemon.h" +#include "actions.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; + +/* Because inotify_init does NEED_ROOT, NEED_INOTIFY implies NEED_ROOT. */ +#define NEED_INOTIFY(errcode) \ + do { \ + if (inotify_fd == -1) { \ + reply_with_error ("%s: you must call 'inotify_init' first to initialize inotify", __func__); \ + return (errcode); \ + } \ + } while (0) + +#define MQE_PATH "/proc/sys/fs/inotify/max_queued_events" + +int +do_inotify_init (int max_events) +{ + FILE *fp; + + NEED_ROOT (-1); + + if (max_events < 0) { + reply_with_error ("inotify_init: max_events < 0"); + return -1; + } + + if (max_events > 0) { + fp = fopen (MQE_PATH, "w"); + if (fp == NULL) { + reply_with_perror (MQE_PATH); + return -1; + } + fprintf (fp, "%d\n", max_events); + fclose (fp); + } + + if (inotify_fd >= 0) + if (do_inotify_close () == -1) + return -1; + + inotify_fd = inotify_init1 (IN_NONBLOCK | IN_CLOEXEC); + if (inotify_fd == -1) { + reply_with_perror ("inotify_init"); + return -1; + } + + return 0; +} + +int +do_inotify_close (void) +{ + NEED_INOTIFY (-1); + + if (inotify_fd == -1) { + reply_with_error ("inotify_close: handle is not open"); + return -1; + } + + if (close (inotify_fd) == -1) { + reply_with_perror ("close"); + return -1; + } + + inotify_fd = -1; + inotify_posn = 0; + + return 0; +} + +int64_t +do_inotify_add_watch (char *path, int mask) +{ + int64_t r; + char *buf; + + NEED_INOTIFY (-1); + ABS_PATH (path, -1); + + buf = sysroot_path (path); + if (!buf) { + reply_with_perror ("malloc"); + return -1; + } + + r = inotify_add_watch (inotify_fd, buf, mask); + free (buf); + if (r == -1) { + reply_with_perror ("inotify_add_watch: %s", path); + return -1; + } + + return r; +} + +int +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); + return -1; + } + + return 0; +} + +guestfs_int_inotify_event_list * +do_inotify_read (void) +{ + int space; + guestfs_int_inotify_event_list *ret; + + NEED_INOTIFY (NULL); + + ret = malloc (sizeof *ret); + if (ret == NULL) { + reply_with_perror ("malloc"); + return NULL; + } + ret->guestfs_int_inotify_event_list_len = 0; + ret->guestfs_int_inotify_event_list_val = NULL; + + /* Read events that are available, but make sure we won't exceed + * maximum message size. In order to achieve this we have to + * guesstimate the remaining space available. + */ + space = GUESTFS_MESSAGE_MAX / 2; + + while (space > 0) { + struct inotify_event *event; + int n, r; + + r = read (inotify_fd, inotify_buf + inotify_posn, + sizeof (inotify_buf) - inotify_posn); + if (r == -1) { + if (errno == EWOULDBLOCK || errno == EAGAIN) /* End of list. */ + break; + reply_with_perror ("read"); + goto error; + } + if (r == 0) { /* End of file - we're not expecting it. */ + reply_with_error ("inotify_read: unexpected end of file"); + goto error; + } + + inotify_posn += r; + + /* Read complete events from the buffer and add them to the result. */ + n = 0; + while (n < inotify_posn) { + guestfs_int_inotify_event *np; + guestfs_int_inotify_event *in; + + event = (struct inotify_event *) &inotify_buf[n]; + + /* 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; +#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)); + if (np == NULL) { + 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]; + ret->guestfs_int_inotify_event_list_len++; + + in->in_wd = event->wd; + in->in_mask = event->mask; + in->in_cookie = event->cookie; + + if (event->len > 0) + in->in_name = strdup (event->name); + else + in->in_name = strdup (""); /* Should have optional string fields XXX. */ + if (in->in_name == NULL) { + reply_with_perror ("strdup"); + goto error; + } + + /* Estimate space used by this event in the message. */ + space -= 16 + 4 + strlen (in->in_name) + 4; + + /* Move pointer to next event. */ +#ifdef __GNUC__ + n += sizeof (struct inotify_event) + event->len; +#else +#error "this code needs fixing so it works on non-GCC compilers" +#endif + } + + /* 'n' now points to the first unprocessed/incomplete + * message in the buffer. Copy that to offset 0 in the buffer. + */ + memmove (inotify_buf, &inotify_buf[n], inotify_posn - n); + inotify_posn -= n; + } + + /* Return the messages. */ + return ret; + + error: + xdr_free ((xdrproc_t) xdr_guestfs_int_inotify_event_list, (char *) ret); + free (ret); + return NULL; +} + +char ** +do_inotify_files (void) +{ + char **ret = NULL; + int size = 0, alloc = 0; + int i; + FILE *fp; + guestfs_int_inotify_event_list *events; + char buf[PATH_MAX]; + + NEED_INOTIFY (NULL); + + fp = popen ("sort -u > /tmp/inotify", "w"); + if (fp == NULL) { + reply_with_perror ("sort"); + return NULL; + } + + while (1) { + events = do_inotify_read (); + if (events == NULL) + goto error; + + if (events->guestfs_int_inotify_event_list_len == 0) { + free (events); + break; /* End of list of events. */ + } + + for (i = 0; i < events->guestfs_int_inotify_event_list_len; ++i) { + const char *name = events->guestfs_int_inotify_event_list_val[i].in_name; + + if (name[0] != '\0') + fprintf (fp, "%s\n", name); + } + + xdr_free ((xdrproc_t) xdr_guestfs_int_inotify_event_list, (char *) events); + free (events); + } + + pclose (fp); + + fp = fopen ("/tmp/inotify", "r"); + if (fp == NULL) { + reply_with_perror ("/tmp/inotify"); + return NULL; + } + + while (fgets (buf, sizeof buf, fp) != NULL) { + int len = strlen (buf); + + if (len > 0 && buf[len-1] == '\n') + buf[len-1] = '\0'; + + if (add_string (&ret, &size, &alloc, buf) == -1) { + fclose (fp); + goto error; + } + } + + fclose (fp); + + if (add_string (&ret, &size, &alloc, NULL) == -1) + goto error; + + unlink ("/tmp/inotify"); + return ret; + + error: + unlink ("/tmp/inotify"); + return NULL; +} diff --git a/java/Makefile.inc b/java/Makefile.inc index 063cd43..e9d30f1 100644 --- a/java/Makefile.inc +++ b/java/Makefile.inc @@ -1 +1 @@ -java_built_sources = com/redhat/et/libguestfs/IntBool.java com/redhat/et/libguestfs/PV.java com/redhat/et/libguestfs/VG.java com/redhat/et/libguestfs/LV.java com/redhat/et/libguestfs/Stat.java com/redhat/et/libguestfs/StatVFS.java com/redhat/et/libguestfs/Dirent.java com/redhat/et/libguestfs/Version.java com/redhat/et/libguestfs/XAttr.java com/redhat/et/libguestfs/GuestFS.java +java_built_sources = com/redhat/et/libguestfs/IntBool.java com/redhat/et/libguestfs/PV.java com/redhat/et/libguestfs/VG.java com/redhat/et/libguestfs/LV.java com/redhat/et/libguestfs/Stat.java com/redhat/et/libguestfs/StatVFS.java com/redhat/et/libguestfs/Dirent.java com/redhat/et/libguestfs/Version.java com/redhat/et/libguestfs/XAttr.java com/redhat/et/libguestfs/INotifyEvent.java com/redhat/et/libguestfs/GuestFS.java diff --git a/po/POTFILES.in b/po/POTFILES.in index c7be238..2a57823 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -24,6 +24,7 @@ daemon/guestfsd.c daemon/headtail.c daemon/hexdump.c daemon/initrd.c +daemon/inotify.c daemon/link.c daemon/ls.c daemon/lvm.c diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index f84d24e..dc37bbd 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -178 +184 diff --git a/src/generator.ml b/src/generator.ml index 7437926..07902f2 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -3295,6 +3295,104 @@ Create a swap file. This command just writes a swap file signature to an existing file. To create the file itself, use something like C."); + ("inotify_init", (RErr, [Int "maxevents"]), 179, [], + [InitSquashFS, Always, TestRun ( + [["inotify_init"; "0"]])], + "create an inotify handle", + "\ +This command creates a new inotify handle. +The inotify subsystem can be used to notify events which happen to +objects in the guest filesystem. + +C is the maximum number of events which will be +queued up between calls to C or +C. +If this is passed as C<0>, then the kernel (or previously set) +default is used. For Linux 2.6.29 the default was 16384 events. +Beyond this limit, the kernel throws away events, but records +the fact that it threw them away by setting a flag +C in the returned structure list (see +C). + +Before any events are generated, you have to add some +watches to the internal watch list. See: +C, +C and +C. + +Queued up events should be read periodically by calling +C +(or C which is just a helpful +wrapper around C). If you don't +read the events out often enough then you risk the internal +queue overflowing. + +The handle should be closed after use by calling +C. This also removes any +watches automatically. + +See also L for an overview of the inotify interface +as exposed by the Linux kernel, which is roughly what we expose +via libguestfs. Note that there is one global inotify handle +per libguestfs instance."); + + ("inotify_add_watch", (RInt64 "wd", [String "path"; Int "mask"]), 180, [], + [InitBasicFS, Always, TestOutputList ( + [["inotify_init"; "0"]; + ["inotify_add_watch"; "/"; "1073741823"]; + ["touch"; "/a"]; + ["touch"; "/b"]; + ["inotify_files"]], ["a"; "b"])], + "add an inotify watch", + "\ +Watch C for the events listed in C. + +Note that if C is a directory then events within that +directory are watched, but this does I happen recursively +(in subdirectories). + +Note for non-C or non-Linux callers: the inotify events are +defined by the Linux kernel ABI and are listed in +C."); + + ("inotify_rm_watch", (RErr, [Int(*XXX64*) "wd"]), 181, [], + [], + "remove an inotify watch", + "\ +Remove a previously defined inotify watch. +See C."); + + ("inotify_read", (RStructList ("events", "inotify_event"), []), 182, [], + [], + "return list of inotify events", + "\ +Return the complete queue of events that have happened +since the previous read call. + +If no events have happened, this returns an empty list. + +I: In order to make sure that all events have been +read, you must call this function repeatedly until it +returns an empty list. The reason is that the call will +read events up to the maximum appliance-to-host message +size and leave remaining events in the queue."); + + ("inotify_files", (RStringList "paths", []), 183, [], + [], + "return list of watched files that had events", + "\ +This function is a helpful wrapper around C +which just returns a list of pathnames of objects that were +touched. The returned pathnames are sorted and deduplicated."); + + ("inotify_close", (RErr, []), 184, [], + [], + "close the inotify handle", + "\ +This closes the inotify handle which was previously +opened by inotify_init. It removes all watches, throws +away any pending events, and deallocates all resources."); + ] let all_functions = non_daemon_functions @ daemon_functions @@ -3309,7 +3407,7 @@ let all_functions_sorted = (* Field types for structures. *) type field = | FChar (* C 'char' (really, a 7 bit byte). *) - | FString (* nul-terminated ASCII string. *) + | FString (* nul-terminated ASCII string, NOT NULL. *) | FBuffer (* opaque buffer of bytes, (char *, int) pair *) | FUInt32 | FInt32 @@ -3455,6 +3553,14 @@ let structs = [ "attrname", FString; "attrval", FBuffer; ]; + + (* Inotify events. *) + "inotify_event", [ + "in_wd", FInt64; + "in_mask", FUInt32; + "in_cookie", FUInt32; + "in_name", FString; + ]; ] (* end of structs *) (* Ugh, Java has to be different .. @@ -3470,6 +3576,7 @@ let java_structs = [ "dirent", "Dirent"; "version", "Version"; "xattr", "XAttr"; + "inotify_event", "INotifyEvent"; ] (* Used for testing language bindings. *) -- 1.8.3.1