diff -r 89116f28083f tools/xenstore/xs.c --- a/tools/xenstore/xs.c Wed Dec 08 10:46:31 2010 +0000 +++ b/tools/xenstore/xs.c Fri Dec 10 01:54:53 2010 -0800 @@ -182,12 +182,17 @@ return -1; } -static int get_dev(const char *connect_to) +static int get_dev(const char *connect_to, int mode) { - return open(connect_to, O_RDWR); + if (mode & XS_OPEN_READWRITE) + return open(connect_to, O_RDWR); + else if(mode & XS_OPEN_READONLY) + return open(connect_to, O_RDONLY); + else + return -1; } -static struct xs_handle *get_handle(const char *connect_to) +static struct xs_handle *get_handle(const char *connect_to, int mode) { struct stat buf; struct xs_handle *h = NULL; @@ -199,7 +204,7 @@ if (S_ISSOCK(buf.st_mode)) fd = get_socket(connect_to); else - fd = get_dev(connect_to); + fd = get_dev(connect_to, mode); if (fd == -1) return NULL; @@ -235,19 +240,41 @@ return h; } +/* + * The following three functions have been deprecated and are + * maintained as aliases to xs_open for legacy code. Newer code + * should use xs_open directly + */ struct xs_handle *xs_daemon_open(void) { - return get_handle(xs_daemon_socket()); + return xs_open(XS_OPEN_READWRITE); } struct xs_handle *xs_daemon_open_readonly(void) { - return get_handle(xs_daemon_socket_ro()); + return xs_open(XS_OPEN_READONLY); } struct xs_handle *xs_domain_open(void) { - return get_handle(xs_domain_dev()); + return xs_open(XS_OPEN_READWRITE); +} + +struct xs_handle *xs_open(int mode) +{ + struct xs_handle *xsh = NULL; + + if(mode & XS_OPEN_READWRITE) + xsh = get_handle(xs_daemon_socket(), mode); + else if(mode & XS_OPEN_READONLY) + xsh = get_handle(xs_daemon_socket_ro(), mode); + else + return NULL; + + if(!xsh) + xsh = get_handle(xs_domain_dev(), mode); + + return xsh; } static void close_free_msgs(struct xs_handle *h) { @@ -303,6 +330,12 @@ close_fds_free(h); } +void xs_close(struct xs_handle* xsh) +{ + if(xsh) + xs_daemon_close(xsh); +} + static bool read_all(int fd, void *data, unsigned int len) { while (len) { diff -r 89116f28083f tools/xenstore/xs.h --- a/tools/xenstore/xs.h Wed Dec 08 10:46:31 2010 +0000 +++ b/tools/xenstore/xs.h Fri Dec 10 01:54:53 2010 -0800 @@ -24,6 +24,9 @@ #define XBT_NULL 0 +#define XS_OPEN_READWRITE 1<<0 +#define XS_OPEN_READONLY 1<<1 + struct xs_handle; typedef uint32_t xs_transaction_t; @@ -39,6 +42,8 @@ */ struct xs_handle *xs_daemon_open(void); struct xs_handle *xs_domain_open(void); +struct xs_handle *xs_open(int mode); +void xs_close(struct xs_handle *xsh); /* Connect to the xs daemon (readonly for non-root clients). * Returns a handle or NULL.