diff options
Diffstat (limited to 'tools/xenstore/xs_lib.c')
-rw-r--r-- | tools/xenstore/xs_lib.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/tools/xenstore/xs_lib.c b/tools/xenstore/xs_lib.c new file mode 100644 index 0000000000..8630eaffce --- /dev/null +++ b/tools/xenstore/xs_lib.c @@ -0,0 +1,141 @@ +#include "xs_lib.h" +#include <unistd.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> + +/* Common routines for the Xen store daemon and client library. */ + +static const char *xs_daemon_rootdir(void) +{ + char *s = getenv("XENSTORED_ROOTDIR"); + return (s ? s : "/var/lib/xenstored"); +} + +static const char *xs_daemon_rundir(void) +{ + char *s = getenv("XENSTORED_RUNDIR"); + return (s ? s : "/var/run/xenstored"); +} + +const char *xs_daemon_socket(void) +{ + static char buf[PATH_MAX]; + sprintf(buf, "%s/socket", xs_daemon_rundir()); + return buf; +} + +const char *xs_daemon_socket_ro(void) +{ + static char buf[PATH_MAX]; + sprintf(buf, "%s/socket_ro", xs_daemon_rundir()); + return buf; +} + +const char *xs_daemon_store(void) +{ + static char buf[PATH_MAX]; + sprintf(buf, "%s/store", xs_daemon_rootdir()); + return buf; +} + +const char *xs_daemon_transactions(void) +{ + static char buf[PATH_MAX]; + sprintf(buf, "%s/transactions", xs_daemon_rootdir()); + return buf; +} + +/* Simple routines for writing to sockets, etc. */ +bool write_all(int fd, const void *data, unsigned int len) +{ + while (len) { + int done; + + done = write(fd, data, len); + if (done < 0 && errno == EINTR) + continue; + if (done <= 0) + return false; + data += done; + len -= done; + } + + return true; +} + +/* Convert strings to permissions. False if a problem. */ +bool strings_to_perms(struct xs_permissions *perms, unsigned int num, + const char *strings) +{ + const char *p; + char *end; + unsigned int i; + + for (p = strings, i = 0; i < num; i++) { + /* "r", "w", or "b" for both. */ + switch (*p) { + case 'r': + perms[i].perms = XS_PERM_READ; + break; + case 'w': + perms[i].perms = XS_PERM_WRITE; + break; + case 'b': + perms[i].perms = XS_PERM_READ|XS_PERM_WRITE; + break; + case 'n': + perms[i].perms = XS_PERM_NONE; + break; + default: + errno = EINVAL; + return false; + } + p++; + perms[i].id = strtol(p, &end, 0); + if (*end || !*p) { + errno = EINVAL; + return false; + } + p = end + 1; + } + return true; +} + +/* Convert permissions to a string (up to len MAX_STRLEN(domid_t)+1). */ +bool perm_to_string(const struct xs_permissions *perm, char *buffer) +{ + switch (perm->perms) { + case XS_PERM_WRITE: + *buffer = 'w'; + break; + case XS_PERM_READ: + *buffer = 'r'; + break; + case XS_PERM_READ|XS_PERM_WRITE: + *buffer = 'b'; + break; + case XS_PERM_NONE: + *buffer = 'n'; + break; + default: + errno = EINVAL; + return false; + } + sprintf(buffer+1, "%i", (int)perm->id); + return true; +} + +/* Given a string and a length, count how many strings (nul terms). */ +unsigned int count_strings(const char *strings, unsigned int len) +{ + unsigned int num; + const char *p; + + for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1) + num++; + + return num; +} + |