ia64/xen-unstable

changeset 10911:8f452e26224a

[TOOLS] Modify xenstore_client.c to include a new utility, xenstore-chmod.
This utility permits developers and administrators to
manually change the permissions on arbitrary locations in XenStore
from the command line. This is often helpful if you're trying to debug an
application that relies on XenStore and is encountering difficulties
with permissions.

Signed-off-by: Michael LeMay <mdlemay@epoch.ncsc.mil>
author kfraser@localhost.localdomain
date Wed Aug 02 14:58:37 2006 +0100 (2006-08-02)
parents 9632ececc8f4
children ee6014279103
files .hgignore tools/xenstore/Makefile tools/xenstore/xenstore_client.c
line diff
     1.1 --- a/.hgignore	Wed Aug 02 10:13:30 2006 +0100
     1.2 +++ b/.hgignore	Wed Aug 02 14:58:37 2006 +0100
     1.3 @@ -156,6 +156,7 @@
     1.4  ^tools/xenstat/xentop/xentop$
     1.5  ^tools/xenstore/testsuite/tmp/.*$
     1.6  ^tools/xenstore/xen$
     1.7 +^tools/xenstore/xenstore-chmod$
     1.8  ^tools/xenstore/xenstore-exists$
     1.9  ^tools/xenstore/xenstore-list$
    1.10  ^tools/xenstore/xenstore-read$
     2.1 --- a/tools/xenstore/Makefile	Wed Aug 02 10:13:30 2006 +0100
     2.2 +++ b/tools/xenstore/Makefile	Wed Aug 02 14:58:37 2006 +0100
     2.3 @@ -26,7 +26,7 @@ TESTDIR  = testsuite/tmp
     2.4  TESTFLAGS= -DTESTING
     2.5  TESTENV  = XENSTORED_ROOTDIR=$(TESTDIR) XENSTORED_RUNDIR=$(TESTDIR)
     2.6  
     2.7 -CLIENTS := xenstore-exists xenstore-list xenstore-read xenstore-rm
     2.8 +CLIENTS := xenstore-exists xenstore-list xenstore-read xenstore-rm xenstore-chmod
     2.9  CLIENTS += xenstore-write
    2.10  CLIENTS_OBJS := $(patsubst xenstore-%,xenstore_%.o,$(CLIENTS))
    2.11  
     3.1 --- a/tools/xenstore/xenstore_client.c	Wed Aug 02 10:13:30 2006 +0100
     3.2 +++ b/tools/xenstore/xenstore_client.c	Wed Aug 02 14:58:37 2006 +0100
     3.3 @@ -60,6 +60,8 @@ usage(const char *progname)
     3.4      errx(1, "Usage: %s [-h] [-s] [-t] key [...]", progname);
     3.5  #elif defined(CLIENT_exists) || defined(CLIENT_list)
     3.6      errx(1, "Usage: %s [-h] [-s] key [...]", progname);
     3.7 +#elif defined(CLIENT_chmod)
     3.8 +    errx(1, "Usage: %s [-h] [-s] key <mode [modes...]>", progname);
     3.9  #endif
    3.10  }
    3.11  
    3.12 @@ -78,10 +80,61 @@ do_rm(char *path, struct xs_handle *xsh,
    3.13  }
    3.14  #endif
    3.15  
    3.16 +#if defined(CLIENT_chmod)
    3.17 +#define PATH_SEP '/'
    3.18 +#define MAX_PATH_LEN 256
    3.19 +
    3.20 +static void
    3.21 +do_chmod(char *path, struct xs_permissions *perms, int nperms, int upto,
    3.22 +	 int recurse, struct xs_handle *xsh, xs_transaction_t xth)
    3.23 +{
    3.24 +    int ret;
    3.25 +
    3.26 +    if (!path[0])
    3.27 +	return;
    3.28 +
    3.29 +    ret = xs_set_permissions(xsh, xth, path, perms, nperms);
    3.30 +    if (!ret)
    3.31 +	err(1, "Error occurred setting permissions on '%s'", path);
    3.32 +
    3.33 +    if (upto) {
    3.34 +	/* apply same permissions to all parent entries: */
    3.35 +	char *path_sep_ptr = strrchr(path, PATH_SEP);
    3.36 +	if (!path_sep_ptr)
    3.37 +	    errx(1, "Unable to locate path separator '%c' in '%s'",
    3.38 +		 PATH_SEP, path);
    3.39 +	
    3.40 +	*path_sep_ptr = '\0'; /* truncate path */
    3.41 +	
    3.42 +	do_chmod(path, perms, nperms, 1, 0, xsh, xth);
    3.43 +
    3.44 +	*path_sep_ptr = PATH_SEP;
    3.45 +    }
    3.46 +
    3.47 +    if (recurse) {
    3.48 +	char buf[MAX_PATH_LEN];
    3.49 +
    3.50 +	/* apply same permissions to all child entries: */
    3.51 +	unsigned int xsval_n;
    3.52 +	char **xsval = xs_directory(xsh, xth, path, &xsval_n);
    3.53 +
    3.54 +	if (xsval) {
    3.55 +	    int i;
    3.56 +	    for (i = 0; i < xsval_n; i++) {
    3.57 +		snprintf(buf, MAX_PATH_LEN, "%s/%s", path, xsval[i]);
    3.58 +
    3.59 +		do_chmod(buf, perms, nperms, 0, 1, xsh, xth);
    3.60 +	    }
    3.61 +
    3.62 +	    free(xsval);
    3.63 +	}
    3.64 +    }
    3.65 +}
    3.66 +#endif
    3.67  
    3.68  static int
    3.69  perform(int optind, int argc, char **argv, struct xs_handle *xsh,
    3.70 -        xs_transaction_t xth, int prefix, int tidy)
    3.71 +        xs_transaction_t xth, int prefix, int tidy, int upto, int recurse)
    3.72  {
    3.73      while (optind < argc) {
    3.74  #if defined(CLIENT_read)
    3.75 @@ -168,6 +221,41 @@ perform(int optind, int argc, char **arg
    3.76  	}
    3.77  	free(list);
    3.78  	optind++;
    3.79 +#elif defined(CLIENT_chmod)
    3.80 +#define MAX_PERMS 16
    3.81 +	struct xs_permissions perms[MAX_PERMS];
    3.82 +	int nperms = 0;
    3.83 +	/* save path pointer: */
    3.84 +	char *path = argv[optind++];
    3.85 +	for (; argv[optind]; optind++, nperms++)
    3.86 +	{
    3.87 +	    if (MAX_PERMS <= nperms)
    3.88 +		errx(1, "Too many permissions specified.  "
    3.89 +		     "Maximum per invocation is %d.", MAX_PERMS);
    3.90 +
    3.91 +	    perms[nperms].id = atoi(argv[optind]+1);
    3.92 +
    3.93 +	    switch (argv[optind][0])
    3.94 +	    {
    3.95 +	    case 'n':
    3.96 +		perms[nperms].perms = XS_PERM_NONE;
    3.97 +		break;
    3.98 +	    case 'r':
    3.99 +		perms[nperms].perms = XS_PERM_READ;
   3.100 +		break;
   3.101 +	    case 'w':
   3.102 +		perms[nperms].perms = XS_PERM_WRITE;
   3.103 +		break;
   3.104 +	    case 'b':
   3.105 +		perms[nperms].perms = XS_PERM_READ | XS_PERM_WRITE;
   3.106 +		break;
   3.107 +	    default:
   3.108 +		errx(1, "Invalid permission specification: '%c'",
   3.109 +		     argv[optind][0]);
   3.110 +	    }
   3.111 +	}
   3.112 +
   3.113 +	do_chmod(path, perms, nperms, upto, recurse, xsh, xth);
   3.114  #endif
   3.115      }
   3.116  
   3.117 @@ -183,6 +271,8 @@ main(int argc, char **argv)
   3.118      int ret = 0, socket = 0;
   3.119      int prefix = 0;
   3.120      int tidy = 0;
   3.121 +    int upto = 0;
   3.122 +    int recurse = 0;
   3.123  
   3.124      while (1) {
   3.125  	int c, index = 0;
   3.126 @@ -193,6 +283,9 @@ main(int argc, char **argv)
   3.127  	    {"prefix", 0, 0, 'p'},
   3.128  #elif defined(CLIENT_rm)
   3.129              {"tidy",   0, 0, 't'},
   3.130 +#elif defined(CLIENT_chmod)
   3.131 +	    {"upto",    0, 0, 'u'},
   3.132 +	    {"recurse", 0, 0, 'r'},
   3.133  #endif
   3.134  	    {0, 0, 0, 0}
   3.135  	};
   3.136 @@ -202,6 +295,8 @@ main(int argc, char **argv)
   3.137  			"p"
   3.138  #elif defined(CLIENT_rm)
   3.139                          "t"
   3.140 +#elif defined(CLIENT_chmod)
   3.141 +			"ur"
   3.142  #endif
   3.143  			, long_options, &index);
   3.144  	if (c == -1)
   3.145 @@ -222,6 +317,13 @@ main(int argc, char **argv)
   3.146  	case 't':
   3.147  	    tidy = 1;
   3.148  	    break;
   3.149 +#elif defined(CLIENT_chmod)
   3.150 +	case 'u':
   3.151 +	    upto = 1;
   3.152 +	    break;
   3.153 +	case 'r':
   3.154 +	    recurse = 1;
   3.155 +	    break;
   3.156  #endif
   3.157  	}
   3.158      }
   3.159 @@ -246,7 +348,7 @@ main(int argc, char **argv)
   3.160      if (xth == XBT_NULL)
   3.161  	errx(1, "couldn't start transaction");
   3.162  
   3.163 -    ret = perform(optind, argc, argv, xsh, xth, prefix, tidy);
   3.164 +    ret = perform(optind, argc, argv, xsh, xth, prefix, tidy, upto, recurse);
   3.165  
   3.166      if (!xs_transaction_end(xsh, xth, ret)) {
   3.167  	if (ret == 0 && errno == EAGAIN) {