From: Ian Campbell Subject: Allow xenstore client utilities to be built statically. Date: Tue, 08 Apr 2008 17:38:34 +0100 Message-ID: <1207672714.3972.77.camel@zakaz.uk.xensource.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-TbcWJ7/5CXbq2UxjRl0L" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel List-Id: xen-devel@lists.xenproject.org --=-TbcWJ7/5CXbq2UxjRl0L Content-Type: text/plain Content-Transfer-Encoding: 7bit The attached patches enable the xenstore client utilities (xenstore-read/write and friends) to be built statically and against uclibc. This is useful for utility domains where a small userspace is desired. Both static building with glibc and linking with uclibc seem to have problems with threading and so the static version of the library is built without thread support. This is OK since none of the clients actually require it. The second patch (static-xenstore-watches) is rather optional since nothing currently requires it and it is therefore untested. The third and fourth patches (multicall-xenstore-*) turn the clients from separate binaries (created via multiple compilation of xenstore_client.c) into a single multicall binary in order to claw back some of the space lost to static linking. This also enables the tool be used as "xenstore " in addition to the existing "xenstore-" way. Ian. --=-TbcWJ7/5CXbq2UxjRl0L Content-Disposition: attachment; filename=static-xenstore-clients.patch Content-Type: text/x-patch; name=static-xenstore-clients.patch; charset=UTF-8 Content-Transfer-Encoding: 7bit # HG changeset patch # User Ian Campbell # Date 1207658376 -3600 # Node ID 40310b53f5d0a456f12302aa07b592fd27736cb5 # Parent af1d20b86b02bf463690e8af61e77ddf169c4c6c xenstore: support building the xenstore clients statically. This removes threading from libxenstore.a (but not libxenstore.so) since pthreads is incompatible with static linking and none of the command line clients require threads anyway. This also disables watch support in the static library. It is now possible to build these utilities statically with a uclibc toolchain which is useful for small userspace utility domains. Signed-off-by: Ian Campbell diff -r af1d20b86b02 -r 40310b53f5d0 tools/xenstore/Makefile --- a/tools/xenstore/Makefile Tue Apr 08 09:57:37 2008 +0100 +++ b/tools/xenstore/Makefile Tue Apr 08 13:39:36 2008 +0100 @@ -24,8 +24,18 @@ XENSTORED_OBJS_$(CONFIG_NetBSD) = xensto XENSTORED_OBJS += $(XENSTORED_OBJS_y) +ifneq ($(XENSTORE_STATIC_CLIENTS),y) +LIBXENSTORE := libxenstore.so +else +LIBXENSTORE := libxenstore.a +$(CLIENTS) xenstore-control xenstore-ls: CFLAGS += -static +endif + .PHONY: all -all: libxenstore.so libxenstore.a xenstored $(CLIENTS) xs_tdb_dump xenstore-control xenstore-ls +all: libxenstore.so libxenstore.a xenstored clients xs_tdb_dump + +.PHONY: clients +clients: $(CLIENTS) xenstore-control xenstore-ls ifeq ($(CONFIG_SunOS),y) xenstored_probes.h: xenstored_probes.d @@ -42,16 +52,16 @@ xenstored: $(XENSTORED_OBJS) xenstored: $(XENSTORED_OBJS) $(CC) $(CFLAGS) $(LDFLAGS) $^ $(LDFLAGS_libxenctrl) $(SOCKET_LIBS) -o $@ -$(CLIENTS): xenstore-%: xenstore_%.o libxenstore.so +$(CLIENTS): xenstore-%: xenstore_%.o $(LIBXENSTORE) $(CC) $(CFLAGS) $(LDFLAGS) $< -L. -lxenstore $(SOCKET_LIBS) -o $@ $(CLIENTS_OBJS): xenstore_%.o: xenstore_client.c $(COMPILE.c) -DCLIENT_$(*F) -o $@ $< -xenstore-control: xenstore_control.o libxenstore.so +xenstore-control: xenstore_control.o $(LIBXENSTORE) $(CC) $(CFLAGS) $(LDFLAGS) $< -L. -lxenstore $(SOCKET_LIBS) -o $@ -xenstore-ls: xsls.o libxenstore.so +xenstore-ls: xsls.o $(LIBXENSTORE) $(CC) $(CFLAGS) $(LDFLAGS) $< -L. -lxenstore $(SOCKET_LIBS) -o $@ xs_tdb_dump: xs_tdb_dump.o utils.o tdb.o talloc.o @@ -61,6 +71,8 @@ libxenstore.so: libxenstore.so.$(MAJOR) ln -sf $< $@ libxenstore.so.$(MAJOR): libxenstore.so.$(MAJOR).$(MINOR) ln -sf $< $@ + +xs.opic: CFLAGS += -DUSE_PTHREAD libxenstore.so.$(MAJOR).$(MINOR): xs.opic xs_lib.opic $(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenstore.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $^ $(SOCKET_LIBS) -lpthread diff -r af1d20b86b02 -r 40310b53f5d0 tools/xenstore/xs.c --- a/tools/xenstore/xs.c Tue Apr 08 09:57:37 2008 +0100 +++ b/tools/xenstore/xs.c Tue Apr 08 13:39:36 2008 +0100 @@ -32,7 +32,9 @@ #include #include #include +#ifdef USE_PTHREAD #include +#endif #include "xs.h" #include "list.h" #include "utils.h" @@ -43,6 +45,7 @@ struct xs_stored_msg { char *body; }; +#ifdef USE_PTHREAD struct xs_handle { /* Communications channel to xenstore daemon. */ int fd; @@ -78,14 +81,61 @@ struct xs_handle { pthread_mutex_t request_mutex; }; +static void watch_mutex_lock(struct xs_handle *h) +{ + pthread_mutex_lock(&h->watch_mutex); +} + +static void watch_mutex_unlock(struct xs_handle *h) +{ + pthread_mutex_unlock(&h->watch_mutex); +} + +static void request_mutex_lock(struct xs_handle *h) +{ + pthread_mutex_lock(&h->request_mutex); +} + +static void request_mutex_unlock(struct xs_handle *h) +{ + pthread_mutex_unlock(&h->request_mutex); +} + +static void reply_mutex_lock(struct xs_handle *h) +{ + pthread_mutex_lock(&h->reply_mutex); +} + +static void reply_mutex_unlock(struct xs_handle *h) +{ + pthread_mutex_unlock(&h->reply_mutex); +} + +static void *read_thread(void *arg); + +#else /*!USE_PTHREAD*/ + +struct xs_handle { + int fd; + struct list_head reply_list; +}; + +static void request_mutex_lock(struct xs_handle *h) {} +static void request_mutex_unlock(struct xs_handle *h) {} + +static void reply_mutex_lock(struct xs_handle *h) {} +static void reply_mutex_unlock(struct xs_handle *h) {} + +#endif + static int read_message(struct xs_handle *h); -static void *read_thread(void *arg); - + +#ifdef USE_PTHREAD int xs_fileno(struct xs_handle *h) { char c = 0; - pthread_mutex_lock(&h->watch_mutex); + watch_mutex_lock(h); if ((h->watch_pipe[0] == -1) && (pipe(h->watch_pipe) != -1)) { /* Kick things off if the watch list is already non-empty. */ @@ -94,10 +144,11 @@ int xs_fileno(struct xs_handle *h) continue; } - pthread_mutex_unlock(&h->watch_mutex); + watch_mutex_unlock(h); return h->watch_pipe[0]; } +#endif static int get_socket(const char *connect_to) { @@ -163,6 +214,9 @@ static struct xs_handle *get_handle(cons h->fd = fd; + INIT_LIST_HEAD(&h->reply_list); + +#ifdef USE_PTHREAD /* Watch pipe is allocated on demand in xs_fileno(). */ h->watch_pipe[0] = h->watch_pipe[1] = -1; @@ -170,11 +224,11 @@ static struct xs_handle *get_handle(cons pthread_mutex_init(&h->watch_mutex, NULL); pthread_cond_init(&h->watch_condvar, NULL); - INIT_LIST_HEAD(&h->reply_list); pthread_mutex_init(&h->reply_mutex, NULL); pthread_cond_init(&h->reply_condvar, NULL); pthread_mutex_init(&h->request_mutex, NULL); +#endif return h; } @@ -198,34 +252,38 @@ void xs_daemon_close(struct xs_handle *h { struct xs_stored_msg *msg, *tmsg; - pthread_mutex_lock(&h->request_mutex); - pthread_mutex_lock(&h->reply_mutex); - pthread_mutex_lock(&h->watch_mutex); +#ifdef USE_PTHREAD + request_mutex_lock(h); + reply_mutex_lock(h); + watch_mutex_lock(h); if (h->read_thr_exists) { /* XXX FIXME: May leak an unpublished message buffer. */ pthread_cancel(h->read_thr); pthread_join(h->read_thr, NULL); } +#endif list_for_each_entry_safe(msg, tmsg, &h->reply_list, list) { free(msg->body); free(msg); } +#ifdef USE_PTHREAD list_for_each_entry_safe(msg, tmsg, &h->watch_list, list) { free(msg->body); free(msg); } - pthread_mutex_unlock(&h->request_mutex); - pthread_mutex_unlock(&h->reply_mutex); - pthread_mutex_unlock(&h->watch_mutex); + request_mutex_unlock(h); + reply_mutex_unlock(h); + watch_mutex_unlock(h); if (h->watch_pipe[0] != -1) { close(h->watch_pipe[0]); close(h->watch_pipe[1]); } +#endif close(h->fd); @@ -277,17 +335,24 @@ static void *read_reply( struct xs_stored_msg *msg; char *body; +#ifdef USE_PTHREAD /* Read from comms channel ourselves if there is no reader thread. */ if (!h->read_thr_exists && (read_message(h) == -1)) return NULL; - - pthread_mutex_lock(&h->reply_mutex); +#endif + + reply_mutex_lock(h); +#ifdef USE_PTHREAD while (list_empty(&h->reply_list)) pthread_cond_wait(&h->reply_condvar, &h->reply_mutex); +#else + while (list_empty(&h->reply_list)) + read_message(h); +#endif msg = list_top(&h->reply_list, struct xs_stored_msg, list); list_del(&msg->list); assert(list_empty(&h->reply_list)); - pthread_mutex_unlock(&h->reply_mutex); + reply_mutex_unlock(h); *type = msg->hdr.type; if (len) @@ -329,7 +394,7 @@ static void *xs_talkv(struct xs_handle * ignorepipe.sa_flags = 0; sigaction(SIGPIPE, &ignorepipe, &oldact); - pthread_mutex_lock(&h->request_mutex); + request_mutex_lock(h); if (!xs_write_all(h->fd, &msg, sizeof(msg))) goto fail; @@ -342,7 +407,7 @@ static void *xs_talkv(struct xs_handle * if (!ret) goto fail; - pthread_mutex_unlock(&h->request_mutex); + request_mutex_unlock(h); sigaction(SIGPIPE, &oldact, NULL); if (msg.type == XS_ERROR) { @@ -362,7 +427,7 @@ fail: fail: /* We're in a bad state, so close fd. */ saved_errno = errno; - pthread_mutex_unlock(&h->request_mutex); + request_mutex_unlock(h); sigaction(SIGPIPE, &oldact, NULL); close_fd: close(h->fd); @@ -547,6 +612,7 @@ unwind: return false; } +#ifdef USE_PTHREAD /* Watch a node for changes (poll on fd to detect, or call read_watch()). * When the node (or any child) changes, fd will become readable. * Token is returned when watch is read, to allow matching. @@ -557,15 +623,15 @@ bool xs_watch(struct xs_handle *h, const struct iovec iov[2]; /* We dynamically create a reader thread on demand. */ - pthread_mutex_lock(&h->request_mutex); + request_mutex_lock(h); if (!h->read_thr_exists) { if (pthread_create(&h->read_thr, NULL, read_thread, h) != 0) { - pthread_mutex_unlock(&h->request_mutex); + request_mutex_unlock(h); return false; } h->read_thr_exists = 1; } - pthread_mutex_unlock(&h->request_mutex); + request_mutex_unlock(h); iov[0].iov_base = (void *)path; iov[0].iov_len = strlen(path) + 1; @@ -586,7 +652,7 @@ char **xs_read_watch(struct xs_handle *h char **ret, *strings, c = 0; unsigned int num_strings, i; - pthread_mutex_lock(&h->watch_mutex); + watch_mutex_lock(h); /* Wait on the condition variable for a watch to fire. */ while (list_empty(&h->watch_list)) @@ -599,7 +665,7 @@ char **xs_read_watch(struct xs_handle *h while (read(h->watch_pipe[0], &c, 1) != 1) continue; - pthread_mutex_unlock(&h->watch_mutex); + watch_mutex_unlock(h); assert(msg->hdr.type == XS_WATCH_EVENT); @@ -642,6 +708,7 @@ bool xs_unwatch(struct xs_handle *h, con return xs_bool(xs_talkv(h, XBT_NULL, XS_UNWATCH, iov, ARRAY_SIZE(iov), NULL)); } +#endif /* Start a transaction: changes by others will not be seen during this * transaction, and changes will not be visible to others until end. @@ -801,7 +868,8 @@ static int read_message(struct xs_handle body[msg->hdr.len] = '\0'; if (msg->hdr.type == XS_WATCH_EVENT) { - pthread_mutex_lock(&h->watch_mutex); +#ifdef USE_PTHREAD + watch_mutex_lock(h); /* Kick users out of their select() loop. */ if (list_empty(&h->watch_list) && @@ -812,20 +880,27 @@ static int read_message(struct xs_handle list_add_tail(&msg->list, &h->watch_list); pthread_cond_signal(&h->watch_condvar); - pthread_mutex_unlock(&h->watch_mutex); + watch_mutex_unlock(h); +#else + /* Cannot handle this event, but should never have been registered. */ + errno = -EIO; + goto error; +#endif } else { - pthread_mutex_lock(&h->reply_mutex); + reply_mutex_lock(h); /* There should only ever be one response pending! */ if (!list_empty(&h->reply_list)) { - pthread_mutex_unlock(&h->reply_mutex); + reply_mutex_unlock(h); goto error; } list_add_tail(&msg->list, &h->reply_list); +#ifdef USE_PTHREAD pthread_cond_signal(&h->reply_condvar); - - pthread_mutex_unlock(&h->reply_mutex); +#endif + + reply_mutex_unlock(h); } return 0; @@ -838,6 +913,7 @@ static int read_message(struct xs_handle return -1; } +#ifdef USE_PTHREAD static void *read_thread(void *arg) { struct xs_handle *h = arg; @@ -847,6 +923,7 @@ static void *read_thread(void *arg) return NULL; } +#endif /* * Local variables: --=-TbcWJ7/5CXbq2UxjRl0L Content-Disposition: attachment; filename=static-xenstore-watches.patch Content-Type: text/x-patch; name=static-xenstore-watches.patch; charset=UTF-8 Content-Transfer-Encoding: 7bit # HG changeset patch # User Ian Campbell # Date 1207658379 -3600 # Node ID 46073fa39b7bc6cb914a1bc4b4ec2397f9018374 # Parent 40310b53f5d0a456f12302aa07b592fd27736cb5 xenstore: re-enable watch support when threading is disabled. The caller can select on the watch fd in the normal way. This patch is only build tested. diff -r 40310b53f5d0 -r 46073fa39b7b tools/xenstore/xs.c --- a/tools/xenstore/xs.c Tue Apr 08 13:39:36 2008 +0100 +++ b/tools/xenstore/xs.c Tue Apr 08 13:39:39 2008 +0100 @@ -118,7 +118,13 @@ struct xs_handle { struct xs_handle { int fd; struct list_head reply_list; + struct list_head watch_list; + /* Clients can select() on this pipe to wait for a watch to fire. */ + int watch_pipe[2]; }; + +static void watch_mutex_lock(struct xs_handle *h) {} +static void watch_mutex_unlock(struct xs_handle *h) {} static void request_mutex_lock(struct xs_handle *h) {} static void request_mutex_unlock(struct xs_handle *h) {} @@ -130,7 +136,6 @@ static void reply_mutex_unlock(struct xs static int read_message(struct xs_handle *h); -#ifdef USE_PTHREAD int xs_fileno(struct xs_handle *h) { char c = 0; @@ -148,7 +153,6 @@ int xs_fileno(struct xs_handle *h) return h->watch_pipe[0]; } -#endif static int get_socket(const char *connect_to) { @@ -215,12 +219,12 @@ static struct xs_handle *get_handle(cons h->fd = fd; INIT_LIST_HEAD(&h->reply_list); - -#ifdef USE_PTHREAD + INIT_LIST_HEAD(&h->watch_list); + /* Watch pipe is allocated on demand in xs_fileno(). */ h->watch_pipe[0] = h->watch_pipe[1] = -1; - INIT_LIST_HEAD(&h->watch_list); +#ifdef USE_PTHREAD pthread_mutex_init(&h->watch_mutex, NULL); pthread_cond_init(&h->watch_condvar, NULL); @@ -269,21 +273,21 @@ void xs_daemon_close(struct xs_handle *h free(msg); } -#ifdef USE_PTHREAD list_for_each_entry_safe(msg, tmsg, &h->watch_list, list) { free(msg->body); free(msg); } +#ifdef USE_PTHREAD request_mutex_unlock(h); reply_mutex_unlock(h); watch_mutex_unlock(h); +#endif if (h->watch_pipe[0] != -1) { close(h->watch_pipe[0]); close(h->watch_pipe[1]); } -#endif close(h->fd); @@ -612,7 +616,6 @@ unwind: return false; } -#ifdef USE_PTHREAD /* Watch a node for changes (poll on fd to detect, or call read_watch()). * When the node (or any child) changes, fd will become readable. * Token is returned when watch is read, to allow matching. @@ -622,6 +625,7 @@ bool xs_watch(struct xs_handle *h, const { struct iovec iov[2]; +#ifdef USE_PTHREAD /* We dynamically create a reader thread on demand. */ request_mutex_lock(h); if (!h->read_thr_exists) { @@ -632,6 +636,7 @@ bool xs_watch(struct xs_handle *h, const h->read_thr_exists = 1; } request_mutex_unlock(h); +#endif iov[0].iov_base = (void *)path; iov[0].iov_len = strlen(path) + 1; @@ -656,7 +661,11 @@ char **xs_read_watch(struct xs_handle *h /* Wait on the condition variable for a watch to fire. */ while (list_empty(&h->watch_list)) +#ifdef USE_PTHREAD pthread_cond_wait(&h->watch_condvar, &h->watch_mutex); +#else + read_message(h); +#endif msg = list_top(&h->watch_list, struct xs_stored_msg, list); list_del(&msg->list); @@ -708,7 +717,6 @@ bool xs_unwatch(struct xs_handle *h, con return xs_bool(xs_talkv(h, XBT_NULL, XS_UNWATCH, iov, ARRAY_SIZE(iov), NULL)); } -#endif /* Start a transaction: changes by others will not be seen during this * transaction, and changes will not be visible to others until end. @@ -868,7 +876,6 @@ static int read_message(struct xs_handle body[msg->hdr.len] = '\0'; if (msg->hdr.type == XS_WATCH_EVENT) { -#ifdef USE_PTHREAD watch_mutex_lock(h); /* Kick users out of their select() loop. */ @@ -878,14 +885,12 @@ static int read_message(struct xs_handle continue; list_add_tail(&msg->list, &h->watch_list); + +#ifdef USE_PTHREAD pthread_cond_signal(&h->watch_condvar); +#endif watch_mutex_unlock(h); -#else - /* Cannot handle this event, but should never have been registered. */ - errno = -EIO; - goto error; -#endif } else { reply_mutex_lock(h); --=-TbcWJ7/5CXbq2UxjRl0L Content-Disposition: attachment; filename=multicall-xenstore-clients Content-Type: text/plain; name=multicall-xenstore-clients; charset=UTF-8 Content-Transfer-Encoding: 7bit # HG changeset patch # User Ian Campbell # Date 1207658430 -3600 # Node ID f9122c15f2148abf8d8a7738738298102877060f # Parent 46073fa39b7bc6cb914a1bc4b4ec2397f9018374 xenstore: make the xenstore clients a single multicall binary rather than multiply compiled source. This saves a bunch of space when statically compiling. Signed-off-by: Ian Campbell diff -r 46073fa39b7b -r f9122c15f214 .hgignore --- a/.hgignore Tue Apr 08 13:39:39 2008 +0100 +++ b/.hgignore Tue Apr 08 13:40:30 2008 +0100 @@ -208,6 +208,7 @@ ^tools/xenstat/xentop/xentop$ ^tools/xenstore/testsuite/tmp/.*$ ^tools/xenstore/xen$ +^tools/xenstore/xenstore$ ^tools/xenstore/xenstore-chmod$ ^tools/xenstore/xenstore-exists$ ^tools/xenstore/xenstore-list$ diff -r 46073fa39b7b -r f9122c15f214 tools/xenstore/Makefile --- a/tools/xenstore/Makefile Tue Apr 08 13:39:39 2008 +0100 +++ b/tools/xenstore/Makefile Tue Apr 08 13:40:30 2008 +0100 @@ -14,7 +14,6 @@ DEP = .*.d CLIENTS := xenstore-exists xenstore-list xenstore-read xenstore-rm xenstore-chmod CLIENTS += xenstore-write -CLIENTS_OBJS := $(patsubst xenstore-%,xenstore_%.o,$(CLIENTS)) 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 @@ -28,14 +27,14 @@ LIBXENSTORE := libxenstore.so LIBXENSTORE := libxenstore.so else LIBXENSTORE := libxenstore.a -$(CLIENTS) xenstore-control xenstore-ls: CFLAGS += -static +xenstore xenstore-control xenstore-ls: CFLAGS += -static endif .PHONY: all all: libxenstore.so libxenstore.a xenstored clients xs_tdb_dump .PHONY: clients -clients: $(CLIENTS) xenstore-control xenstore-ls +clients: xenstore $(CLIENTS) xenstore-control xenstore-ls ifeq ($(CONFIG_SunOS),y) xenstored_probes.h: xenstored_probes.d @@ -52,11 +51,11 @@ xenstored: $(XENSTORED_OBJS) xenstored: $(XENSTORED_OBJS) $(CC) $(CFLAGS) $(LDFLAGS) $^ $(LDFLAGS_libxenctrl) $(SOCKET_LIBS) -o $@ -$(CLIENTS): xenstore-%: xenstore_%.o $(LIBXENSTORE) +$(CLIENTS): xenstore + ln -f xenstore $@ + +xenstore: xenstore_client.o $(LIBXENSTORE) $(CC) $(CFLAGS) $(LDFLAGS) $< -L. -lxenstore $(SOCKET_LIBS) -o $@ - -$(CLIENTS_OBJS): xenstore_%.o: xenstore_client.c - $(COMPILE.c) -DCLIENT_$(*F) -o $@ $< xenstore-control: xenstore_control.o $(LIBXENSTORE) $(CC) $(CFLAGS) $(LDFLAGS) $< -L. -lxenstore $(SOCKET_LIBS) -o $@ @@ -85,7 +84,7 @@ clean: rm -f *.a *.o *.opic *.so* xenstored_probes.h rm -f xenstored xs_random xs_stress xs_crashme rm -f xs_tdb_dump xenstore-control xenstore-ls - rm -f $(CLIENTS) + rm -f xenstore $(CLIENTS) $(RM) $(DEP) .PHONY: TAGS @@ -104,8 +103,11 @@ install: all $(INSTALL_DIR) $(DESTDIR)$(SBINDIR) $(INSTALL_DIR) $(DESTDIR)$(INCLUDEDIR) $(INSTALL_PROG) xenstored $(DESTDIR)$(SBINDIR) - $(INSTALL_PROG) $(CLIENTS) $(DESTDIR)$(BINDIR) $(INSTALL_PROG) xenstore-control $(DESTDIR)$(BINDIR) + $(INSTALL_PROG) xenstore $(DESTDIR)/usr/bin + set -e ; for c in $(CLIENTS) ; do \ + ln -f $(DESTDIR)/usr/bin/xenstore $(DESTDIR)/usr/bin/$${c} ; \ + done $(INSTALL_PROG) xenstore-ls $(DESTDIR)$(BINDIR) $(INSTALL_DIR) $(DESTDIR)$(LIBDIR) $(INSTALL_PROG) libxenstore.so.$(MAJOR).$(MINOR) $(DESTDIR)$(LIBDIR) diff -r 46073fa39b7b -r f9122c15f214 tools/xenstore/xenstore_client.c --- a/tools/xenstore/xenstore_client.c Tue Apr 08 13:39:39 2008 +0100 +++ b/tools/xenstore/xenstore_client.c Tue Apr 08 13:40:30 2008 +0100 @@ -18,10 +18,24 @@ #include #include +#define PATH_SEP '/' +#define MAX_PATH_LEN 256 + +#define MAX_PERMS 16 + +enum mode { + MODE_unknown, + MODE_chmod, + MODE_exists, + MODE_list, + MODE_read, + MODE_rm, + MODE_write, +}; + static char *output_buf = NULL; static int output_pos = 0; -#if defined(CLIENT_read) || defined(CLIENT_list) static int output_size = 0; static void @@ -47,26 +61,36 @@ output(const char *fmt, ...) { va_end(ap); output_pos += len; } -#endif static void -usage(const char *progname) -{ -#if defined(CLIENT_read) - errx(1, "Usage: %s [-h] [-p] [-s] key [...]", progname); -#elif defined(CLIENT_write) - errx(1, "Usage: %s [-h] [-s] key value [...]", progname); -#elif defined(CLIENT_rm) - errx(1, "Usage: %s [-h] [-s] [-t] key [...]", progname); -#elif defined(CLIENT_exists) || defined(CLIENT_list) - errx(1, "Usage: %s [-h] [-s] key [...]", progname); -#elif defined(CLIENT_chmod) - errx(1, "Usage: %s [-h] [-s] key ", progname); -#endif -} - - -#if defined(CLIENT_rm) +usage(enum mode mode, int incl_mode, const char *progname) +{ + const char *mstr = NULL; + + switch (mode) { + case MODE_unknown: + errx(1, "Usage: %s [-h] [...]", progname); + case MODE_read: + mstr = incl_mode ? "read " : ""; + errx(1, "Usage: %s %s[-h] [-p] [-s] key [...]", progname, mstr); + case MODE_write: + mstr = incl_mode ? "write " : ""; + errx(1, "Usage: %s %s[-h] [-s] key value [...]", progname, mstr); + case MODE_rm: + mstr = incl_mode ? "rm " : ""; + errx(1, "Usage: %s %s[-h] [-s] [-t] key [...]", progname, mstr); + case MODE_exists: + mstr = incl_mode ? "exists " : ""; + case MODE_list: + mstr = mstr ? : incl_mode ? "list " : ""; + errx(1, "Usage: %s %s[-h] [-s] key [...]", progname, mstr); + case MODE_chmod: + mstr = incl_mode ? "chmod " : ""; + errx(1, "Usage: %s %s[-h] [-s] key ", progname, mstr); + } +} + + static int do_rm(char *path, struct xs_handle *xsh, xs_transaction_t xth) { @@ -78,11 +102,6 @@ do_rm(char *path, struct xs_handle *xsh, return 1; } } -#endif - -#if defined(CLIENT_chmod) -#define PATH_SEP '/' -#define MAX_PATH_LEN 256 static void do_chmod(char *path, struct xs_permissions *perms, int nperms, int upto, @@ -130,145 +149,177 @@ do_chmod(char *path, struct xs_permissio } } } -#endif static int -perform(int optind, int argc, char **argv, struct xs_handle *xsh, +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) { while (optind < argc) { -#if defined(CLIENT_read) - static struct expanding_buffer ebuf; - unsigned len; - char *val = xs_read(xsh, xth, argv[optind], &len); - if (val == NULL) { - warnx("couldn't read path %s", argv[optind]); - return 1; - } - if (prefix) - output("%s: ", argv[optind]); - output("%s\n", sanitise_value(&ebuf, val, len)); - free(val); - optind++; -#elif defined(CLIENT_write) - static struct expanding_buffer ebuf; - char *val_spec = argv[optind + 1]; - unsigned len; - expanding_buffer_ensure(&ebuf, strlen(val_spec)+1); - unsanitise_value(ebuf.buf, &len, val_spec); - if (!xs_write(xsh, xth, argv[optind], ebuf.buf, len)) { - warnx("could not write path %s", argv[optind]); - return 1; - } - optind += 2; -#elif defined(CLIENT_rm) - /* Remove the specified path. If the tidy flag is set, then also - remove any containing directories that are both empty and have no - value attached, and repeat, recursing all the way up to the root if - necessary. - */ - - char *slash, *path = argv[optind]; - - if (tidy) { - /* Copy path, because we can't modify argv because we will need it - again if xs_transaction_end gives us EAGAIN. */ - char *p = malloc(strlen(path) + 1); - strcpy(p, path); - path = p; - - again: - if (do_rm(path, xsh, xth)) { + switch (mode) { + case MODE_unknown: + /* CANNOT BE REACHED */ + errx(1, "invalid mode %d", mode); + case MODE_read: { + static struct expanding_buffer ebuf; + unsigned len; + char *val = xs_read(xsh, xth, argv[optind], &len); + if (val == NULL) { + warnx("couldn't read path %s", argv[optind]); return 1; } - - slash = strrchr(p, '/'); - if (slash) { - char *val; - unsigned len; - *slash = '\0'; - val = xs_read(xsh, xth, p, &len); - if (val && len == 0) { - unsigned int num; - char ** list = xs_directory(xsh, xth, p, &num); - - if (list && num == 0) { - goto again; + if (prefix) + output("%s: ", argv[optind]); + output("%s\n", sanitise_value(&ebuf, val, len)); + free(val); + optind++; + break; + } + case MODE_write: { + static struct expanding_buffer ebuf; + char *val_spec = argv[optind + 1]; + unsigned len; + expanding_buffer_ensure(&ebuf, strlen(val_spec)+1); + unsanitise_value(ebuf.buf, &len, val_spec); + if (!xs_write(xsh, xth, argv[optind], ebuf.buf, len)) { + warnx("could not write path %s", argv[optind]); + return 1; + } + optind += 2; + } break; + case MODE_rm: { + /* Remove the specified path. If the tidy flag is set, then also + remove any containing directories that are both empty and have no + value attached, and repeat, recursing all the way up to the root if + necessary. + */ + + char *slash, *path = argv[optind]; + + if (tidy) { + /* Copy path, because we can't modify argv because we will need it + again if xs_transaction_end gives us EAGAIN. */ + char *p = malloc(strlen(path) + 1); + strcpy(p, path); + path = p; + + again: + if (do_rm(path, xsh, xth)) { + return 1; + } + + slash = strrchr(p, '/'); + if (slash) { + char *val; + unsigned len; + *slash = '\0'; + val = xs_read(xsh, xth, p, &len); + if (val && len == 0) { + unsigned int num; + char ** list = xs_directory(xsh, xth, p, &num); + + if (list && num == 0) { + goto again; + } } } - } - - free(path); - } - else { - if (do_rm(path, xsh, xth)) { + + free(path); + } + else { + if (do_rm(path, xsh, xth)) { + return 1; + } + } + + optind++; + break; + } + case MODE_exists: { + char *val = xs_read(xsh, xth, argv[optind], NULL); + if (val == NULL) { return 1; } - } - - optind++; -#elif defined(CLIENT_exists) - char *val = xs_read(xsh, xth, argv[optind], NULL); - if (val == NULL) { - return 1; - } - free(val); - optind++; -#elif defined(CLIENT_list) - unsigned int i, num; - char **list = xs_directory(xsh, xth, argv[optind], &num); - if (list == NULL) { - warnx("could not list path %s", argv[optind]); - return 1; - } - for (i = 0; i < num; i++) { - if (prefix) - output("%s/", argv[optind]); - output("%s\n", list[i]); - } - free(list); - optind++; -#elif defined(CLIENT_chmod) -#define MAX_PERMS 16 - struct xs_permissions perms[MAX_PERMS]; - int nperms = 0; - /* save path pointer: */ - char *path = argv[optind++]; - for (; argv[optind]; optind++, nperms++) - { - if (MAX_PERMS <= nperms) - errx(1, "Too many permissions specified. " - "Maximum per invocation is %d.", MAX_PERMS); - - perms[nperms].id = atoi(argv[optind]+1); - - switch (argv[optind][0]) - { - case 'n': - perms[nperms].perms = XS_PERM_NONE; - break; - case 'r': - perms[nperms].perms = XS_PERM_READ; - break; - case 'w': - perms[nperms].perms = XS_PERM_WRITE; - break; - case 'b': - perms[nperms].perms = XS_PERM_READ | XS_PERM_WRITE; - break; - default: - errx(1, "Invalid permission specification: '%c'", - argv[optind][0]); - } - } - - do_chmod(path, perms, nperms, upto, recurse, xsh, xth); -#endif - } - - return 0; -} - + free(val); + optind++; + break; + } + case MODE_list: { + unsigned int i, num; + char **list = xs_directory(xsh, xth, argv[optind], &num); + if (list == NULL) { + warnx("could not list path %s", argv[optind]); + return 1; + } + for (i = 0; i < num; i++) { + if (prefix) + output("%s/", argv[optind]); + output("%s\n", list[i]); + } + free(list); + optind++; + break; + } + case MODE_chmod: { + struct xs_permissions perms[MAX_PERMS]; + int nperms = 0; + /* save path pointer: */ + char *path = argv[optind++]; + for (; argv[optind]; optind++, nperms++) + { + if (MAX_PERMS <= nperms) + errx(1, "Too many permissions specified. " + "Maximum per invocation is %d.", MAX_PERMS); + + perms[nperms].id = atoi(argv[optind]+1); + + switch (argv[optind][0]) + { + case 'n': + perms[nperms].perms = XS_PERM_NONE; + break; + case 'r': + perms[nperms].perms = XS_PERM_READ; + break; + case 'w': + perms[nperms].perms = XS_PERM_WRITE; + break; + case 'b': + perms[nperms].perms = XS_PERM_READ | XS_PERM_WRITE; + break; + default: + errx(1, "Invalid permission specification: '%c'", + argv[optind][0]); + } + } + + do_chmod(path, perms, nperms, upto, recurse, xsh, xth); + break; + } + } + + return 0; + } +} + +static enum mode lookup_mode(const char *m) +{ + if (strcmp(m, "read") == 0) + return MODE_read; + else if (strcmp(m, "chmod") == 0) + return MODE_chmod; + else if (strcmp(m, "exists") == 0) + return MODE_exists; + else if (strcmp(m, "list") == 0) + return MODE_list; + else if (strcmp(m, "rm") == 0) + return MODE_rm; + else if (strcmp(m, "write") == 0) + return MODE_write; + else if (strcmp(m, "read") == 0) + return MODE_read; + else + errx(1, "unknown mode %s\n", m); +} int main(int argc, char **argv) @@ -281,92 +332,111 @@ main(int argc, char **argv) int upto = 0; int recurse = 0; int transaction; + enum mode mode; + + const char *_command = strrchr(argv[0], '/'); + const char *command = _command ? &_command[1] : argv[0]; + int switch_argv = -1; /* which element of argv did we switch on */ + + if (strncmp(command, "xenstore-", strlen("xenstore-")) == 0) + { + switch_argv = 0; + command = command + strlen("xenstore-"); + } + else if (argc < 2) + usage(MODE_unknown, 0, argv[0]); + else + { + command = argv[1]; + switch_argv = 1; + } + + mode = lookup_mode(command); while (1) { int c, index = 0; static struct option long_options[] = { - {"help", 0, 0, 'h'}, - {"socket", 0, 0, 's'}, -#if defined(CLIENT_read) || defined(CLIENT_list) - {"prefix", 0, 0, 'p'}, -#elif defined(CLIENT_rm) - {"tidy", 0, 0, 't'}, -#elif defined(CLIENT_chmod) - {"upto", 0, 0, 'u'}, - {"recurse", 0, 0, 'r'}, -#endif + {"help", 0, 0, 'h'}, + {"socket", 0, 0, 's'}, + {"prefix", 0, 0, 'p'}, /* MODE_read || MODE_list */ + {"tidy", 0, 0, 't'}, /* MODE_rm */ + {"upto", 0, 0, 'u'}, /* MODE_chmod */ + {"recurse", 0, 0, 'r'}, /* MODE_chmod */ {0, 0, 0, 0} }; - c = getopt_long(argc, argv, "hs" -#if defined(CLIENT_read) || defined(CLIENT_list) - "p" -#elif defined(CLIENT_rm) - "t" -#elif defined(CLIENT_chmod) - "ur" -#endif - , long_options, &index); + c = getopt_long(argc - switch_argv, argv + switch_argv, "hsptur", + long_options, &index); if (c == -1) break; switch (c) { case 'h': - usage(argv[0]); + usage(mode, switch_argv, argv[0]); /* NOTREACHED */ case 's': socket = 1; break; -#if defined(CLIENT_read) || defined(CLIENT_list) case 'p': - prefix = 1; - break; -#elif defined(CLIENT_rm) + if ( mode == MODE_read || mode == MODE_list ) + prefix = 1; + else + usage(mode, switch_argv, argv[0]); + break; case 't': - tidy = 1; - break; -#elif defined(CLIENT_chmod) + if ( mode == MODE_rm ) + tidy = 1; + else + usage(mode, switch_argv, argv[0]); + break; case 'u': - upto = 1; + if ( mode == MODE_chmod ) + upto = 1; + else + usage(mode, switch_argv, argv[0]); break; case 'r': - recurse = 1; - break; -#endif + if ( mode == MODE_chmod ) + recurse = 1; + else + usage(mode, switch_argv, argv[0]); + break; } } if (optind == argc) { - usage(argv[0]); + usage(mode, switch_argv, argv[0]); /* NOTREACHED */ } -#if defined(CLIENT_write) - if ((argc - optind) % 2 == 1) { - usage(argv[0]); + if (mode == MODE_write && (argc - switch_argv - optind) % 2 == 1) { + usage(mode, switch_argv, argv[0]); /* NOTREACHED */ } -#endif - -#if defined(CLIENT_read) - transaction = (argc - optind) > 1; -#elif defined(CLIENT_write) - transaction = (argc - optind) > 2; -#else - transaction = 1; -#endif + + switch (mode) { + case MODE_read: + transaction = (argc - switch_argv - optind) > 1; + break; + case MODE_write: + transaction = (argc - switch_argv - optind) > 2; + break; + default: + transaction = 1; + break; + } xsh = socket ? xs_daemon_open() : xs_domain_open(); if (xsh == NULL) err(1, socket ? "xs_daemon_open" : "xs_domain_open"); - again: +again: if (transaction) { xth = xs_transaction_start(xsh); if (xth == XBT_NULL) errx(1, "couldn't start transaction"); } - ret = perform(optind, argc, argv, xsh, xth, prefix, tidy, upto, recurse); + ret = perform(mode, optind, argc - switch_argv, argv + switch_argv, xsh, xth, prefix, tidy, upto, recurse); if (transaction && !xs_transaction_end(xsh, xth, ret)) { if (ret == 0 && errno == EAGAIN) { --=-TbcWJ7/5CXbq2UxjRl0L Content-Disposition: attachment; filename=multicall-xenstore-ls Content-Type: text/plain; name=multicall-xenstore-ls; charset=UTF-8 Content-Transfer-Encoding: 7bit # HG changeset patch # User Ian Campbell # Date 1207658430 -3600 # Node ID af555f1dfa4763a9731b05ccb73c61976234c96b # Parent f9122c15f2148abf8d8a7738738298102877060f xenstore: merge xenstore-ls into the multicall binary. This is done separately from moving the other utilities into the mutlicall since ls is slightly different and was already separate. Signed-off-by: Ian Campbell diff -r f9122c15f214 -r af555f1dfa47 tools/xenstore/Makefile --- a/tools/xenstore/Makefile Tue Apr 08 13:40:30 2008 +0100 +++ b/tools/xenstore/Makefile Tue Apr 08 13:40:30 2008 +0100 @@ -13,7 +13,7 @@ DEP = .*.d DEP = .*.d CLIENTS := xenstore-exists xenstore-list xenstore-read xenstore-rm xenstore-chmod -CLIENTS += xenstore-write +CLIENTS += xenstore-write xenstore-ls 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 @@ -27,14 +27,14 @@ LIBXENSTORE := libxenstore.so LIBXENSTORE := libxenstore.so else LIBXENSTORE := libxenstore.a -xenstore xenstore-control xenstore-ls: CFLAGS += -static +xenstore xenstore-control: CFLAGS += -static endif .PHONY: all all: libxenstore.so libxenstore.a xenstored clients xs_tdb_dump .PHONY: clients -clients: xenstore $(CLIENTS) xenstore-control xenstore-ls +clients: xenstore $(CLIENTS) xenstore-control ifeq ($(CONFIG_SunOS),y) xenstored_probes.h: xenstored_probes.d @@ -60,9 +60,6 @@ xenstore-control: xenstore_control.o $(L xenstore-control: xenstore_control.o $(LIBXENSTORE) $(CC) $(CFLAGS) $(LDFLAGS) $< -L. -lxenstore $(SOCKET_LIBS) -o $@ -xenstore-ls: xsls.o $(LIBXENSTORE) - $(CC) $(CFLAGS) $(LDFLAGS) $< -L. -lxenstore $(SOCKET_LIBS) -o $@ - xs_tdb_dump: xs_tdb_dump.o utils.o tdb.o talloc.o $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ @@ -83,7 +80,7 @@ clean: clean: rm -f *.a *.o *.opic *.so* xenstored_probes.h rm -f xenstored xs_random xs_stress xs_crashme - rm -f xs_tdb_dump xenstore-control xenstore-ls + rm -f xs_tdb_dump xenstore-control rm -f xenstore $(CLIENTS) $(RM) $(DEP) @@ -108,7 +105,6 @@ install: all set -e ; for c in $(CLIENTS) ; do \ ln -f $(DESTDIR)/usr/bin/xenstore $(DESTDIR)/usr/bin/$${c} ; \ done - $(INSTALL_PROG) xenstore-ls $(DESTDIR)$(BINDIR) $(INSTALL_DIR) $(DESTDIR)$(LIBDIR) $(INSTALL_PROG) libxenstore.so.$(MAJOR).$(MINOR) $(DESTDIR)$(LIBDIR) ln -sf libxenstore.so.$(MAJOR).$(MINOR) $(DESTDIR)$(LIBDIR)/libxenstore.so.$(MAJOR) diff -r f9122c15f214 -r af555f1dfa47 tools/xenstore/xenstore_client.c --- a/tools/xenstore/xenstore_client.c Tue Apr 08 13:40:30 2008 +0100 +++ b/tools/xenstore/xenstore_client.c Tue Apr 08 13:40:30 2008 +0100 @@ -16,7 +16,11 @@ #include #include #include +#include +#include #include + +#include #define PATH_SEP '/' #define MAX_PATH_LEN 256 @@ -28,6 +32,7 @@ enum mode { MODE_chmod, MODE_exists, MODE_list, + MODE_ls, MODE_read, MODE_rm, MODE_write, @@ -84,6 +89,9 @@ usage(enum mode mode, int incl_mode, con case MODE_list: mstr = mstr ? : incl_mode ? "list " : ""; errx(1, "Usage: %s %s[-h] [-s] key [...]", progname, mstr); + case MODE_ls: + mstr = mstr ? : incl_mode ? "ls " : ""; + errx(1, "Usage: %s %s[-h] [-s] [path]", progname, mstr); case MODE_chmod: mstr = incl_mode ? "chmod " : ""; errx(1, "Usage: %s %s[-h] [-s] key ", progname, mstr); @@ -101,6 +109,111 @@ do_rm(char *path, struct xs_handle *xsh, warnx("could not remove path %s", path); return 1; } +} + +#define STRING_MAX XENSTORE_ABS_PATH_MAX+1024 +static int max_width = 80; +static int desired_width = 60; +static int show_whole_path = 0; + +#define TAG " = \"...\"" +#define TAG_LEN strlen(TAG) + +#define MIN(a, b) (((a) < (b))? (a) : (b)) + +void do_ls(struct xs_handle *h, char *path, int cur_depth, int show_perms) +{ + static struct expanding_buffer ebuf; + char **e; + char newpath[STRING_MAX], *val; + int newpath_len; + int i; + unsigned int num, len; + + e = xs_directory(h, XBT_NULL, path, &num); + if (e == NULL) + err(1, "xs_directory (%s)", path); + + for (i = 0; i 2; break; + case MODE_ls: + transaction = 0; + break; default: transaction = 1; break; + } + + if ( mode == MODE_ls ) + { + memset(&ws, 0, sizeof(ws)); + ret = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); + if (!ret) + max_width = ws.ws_col - 2; } xsh = socket ? xs_daemon_open() : xs_domain_open(); diff -r f9122c15f214 -r af555f1dfa47 tools/xenstore/xsls.c --- a/tools/xenstore/xsls.c Tue Apr 08 13:40:30 2008 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,171 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define STRING_MAX XENSTORE_ABS_PATH_MAX+1024 -static int max_width = 80; -static int desired_width = 60; -static int show_whole_path = 0; - -#define TAG " = \"...\"" -#define TAG_LEN strlen(TAG) - -#define MIN(a, b) (((a) < (b))? (a) : (b)) - -void print_dir(struct xs_handle *h, char *path, int cur_depth, int show_perms) -{ - static struct expanding_buffer ebuf; - char **e; - char newpath[STRING_MAX], *val; - int newpath_len; - int i; - unsigned int num, len; - - e = xs_directory(h, XBT_NULL, path, &num); - if (e == NULL) - err(1, "xs_directory (%s)", path); - - for (i = 0; i