From cc0b2bf9de6871cb951fccd4c4e860f08c15e2ec Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Thu, 21 Oct 2010 18:14:50 +0100 Subject: xenstore: add xenstore-watch command line client # xenstore-watch x | while read w ; do > echo "watch fired on $w" > echo "value" $(xenstore-read $w) > echo > done # xenstore-write x/y/z 42 output from while loop: watch fired on x/t/z value 42 [ also add line to .hgignore - iwj ] Signed-off-by: Ian Campbell Signed-off-by: Ian Jackson --- tools/xenstore/Makefile | 2 +- tools/xenstore/xenstore_client.c | 49 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 4 deletions(-) (limited to 'tools/xenstore') diff --git a/tools/xenstore/Makefile b/tools/xenstore/Makefile index c4b33cb8f8..35e68d0533 100644 --- a/tools/xenstore/Makefile +++ b/tools/xenstore/Makefile @@ -9,7 +9,7 @@ CFLAGS += -I. CFLAGS += $(CFLAGS_libxenctrl) CLIENTS := xenstore-exists xenstore-list xenstore-read xenstore-rm xenstore-chmod -CLIENTS += xenstore-write xenstore-ls +CLIENTS += xenstore-write xenstore-ls xenstore-watch XENSTORED_OBJS = xenstored_core.o xenstored_watch.o xenstored_domain.o xenstored_transaction.o xs_lib.o talloc.o utils.o tdb.o hashtable.o diff --git a/tools/xenstore/xenstore_client.c b/tools/xenstore/xenstore_client.c index 50fc3d7646..fe63b6b0c8 100644 --- a/tools/xenstore/xenstore_client.c +++ b/tools/xenstore/xenstore_client.c @@ -36,6 +36,7 @@ enum mode { MODE_read, MODE_rm, MODE_write, + MODE_watch, }; static char *output_buf = NULL; @@ -95,6 +96,9 @@ usage(enum mode mode, int incl_mode, const char *progname) case MODE_chmod: mstr = incl_mode ? "chmod " : ""; errx(1, "Usage: %s %s[-h] [-u] [-r] [-s] key ", progname, mstr); + case MODE_watch: + mstr = incl_mode ? "watch " : ""; + errx(1, "Usage: %s %s[-h] [-n NR] key", progname, mstr); } } @@ -263,9 +267,28 @@ do_chmod(char *path, struct xs_permissions *perms, int nperms, int upto, } } +static void +do_watch(struct xs_handle *xsh, int max_events) +{ + int count = 0; + char **vec = NULL; + + for ( count = 0; max_events == -1 || count < max_events; count++ ) { + unsigned int num; + + vec = xs_read_watch(xsh, &num); + if (vec == NULL) + continue; + + printf("%s\n", vec[XS_WATCH_PATH]); + fflush(stdout); + free(vec); + } +} + static int perform(enum mode mode, int optind, int argc, char **argv, struct xs_handle *xsh, - xs_transaction_t xth, int prefix, int tidy, int upto, int recurse) + xs_transaction_t xth, int prefix, int tidy, int upto, int recurse, int nr_watches) { switch (mode) { case MODE_ls: @@ -428,6 +451,15 @@ perform(enum mode mode, int optind, int argc, char **argv, struct xs_handle *xsh do_chmod(path, perms, nperms, upto, recurse, xsh, xth); break; } + case MODE_watch: { + for (; argv[optind]; optind++) { + const char *w = argv[optind]; + + if (!xs_watch(xsh, w, w)) + errx(1, "Unable to add watch on %s\n", w); + } + do_watch(xsh, nr_watches); + } } } @@ -452,6 +484,8 @@ static enum mode lookup_mode(const char *m) return MODE_write; else if (strcmp(m, "read") == 0) return MODE_read; + else if (strcmp(m, "watch") == 0) + return MODE_watch; errx(1, "unknown mode %s\n", m); return 0; @@ -467,6 +501,7 @@ main(int argc, char **argv) int tidy = 0; int upto = 0; int recurse = 0; + int nr_watches = -1; int transaction; struct winsize ws; enum mode mode; @@ -500,10 +535,11 @@ main(int argc, char **argv) {"tidy", 0, 0, 't'}, /* MODE_rm */ {"upto", 0, 0, 'u'}, /* MODE_chmod */ {"recurse", 0, 0, 'r'}, /* MODE_chmod */ + {"number", 1, 0, 'n'}, /* MODE_watch */ {0, 0, 0, 0} }; - c = getopt_long(argc - switch_argv, argv + switch_argv, "hfsptur", + c = getopt_long(argc - switch_argv, argv + switch_argv, "hfspturn:", long_options, &index); if (c == -1) break; @@ -548,6 +584,12 @@ main(int argc, char **argv) else usage(mode, switch_argv, argv[0]); break; + case 'n': + if ( mode == MODE_watch ) + nr_watches = atoi(optarg); + else + usage(mode, switch_argv, argv[0]); + break; } } @@ -575,6 +617,7 @@ main(int argc, char **argv) transaction = (argc - switch_argv - optind) > 2; break; case MODE_ls: + case MODE_watch: transaction = 0; break; default: @@ -601,7 +644,7 @@ again: errx(1, "couldn't start transaction"); } - ret = perform(mode, optind, argc - switch_argv, argv + switch_argv, xsh, xth, prefix, tidy, upto, recurse); + ret = perform(mode, optind, argc - switch_argv, argv + switch_argv, xsh, xth, prefix, tidy, upto, recurse, nr_watches); if (transaction && !xs_transaction_end(xsh, xth, ret)) { if (ret == 0 && errno == EAGAIN) { -- cgit v1.2.3