/* * This file is subject to the terms and conditions of the GNU General * Public License. See the file "COPYING" in the main directory of * this archive for more details. * * Copyright (C) 2005 by Christian Limpach * Copyright (C) 2005 XenSource Ltd. * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define PATH_SEP '/' #define MAX_PATH_LEN 256 enum mode { MODE_unknown, MODE_chmod, MODE_exists, MODE_list, MODE_ls, MODE_read, MODE_rm, MODE_write, MODE_watch, }; /* Sanitising (quoting) possibly-binary strings. */ struct expanding_buffer { char *buf; int avail; }; static char *output_buf = NULL; static int output_pos = 0; static struct expanding_buffer ebuf; static int output_size = 0; /* Ensure that given expanding buffer has at least min_avail characters. */ static char *expanding_buffer_ensure(struct expanding_buffer *ebuf, int min_avail) { int want; char *got; if ( ebuf->avail >= min_avail ) return ebuf->buf; if ( min_avail >= INT_MAX/3 ) return 0; want = ebuf->avail + min_avail + 10; got = realloc(ebuf->buf, want); if ( !got ) return 0; ebuf->buf = got; ebuf->avail = want; return ebuf->buf; } /* sanitise_value() may return NULL if malloc fails. */ static char *sanitise_value(struct expanding_buffer *ebuf, const char *val, unsigned len) { int used, remain, c; unsigned char *ip; #define ADD(c) (ebuf->buf[used++] = (c)) #define ADDF(f,c) (used += sprintf(ebuf->buf+used, (f), (c))) assert(len < INT_MAX/5); ip = (unsigned char *)val; used = 0; remain = len; if ( !expanding_buffer_ensure(ebuf, remain + 1) ) return NULL; while ( remain-- > 0 ) { c= *ip++; if ( c >= ' ' && c <= '~' && c != '\\' ) { ADD(c); continue; } if ( !expanding_buffer_ensure(ebuf, used + remain + 5) ) /* for "\\nnn\0" */ return 0; ADD('\\'); switch (c) { case '\t': ADD('t'); break; case '\n': ADD('n'); break; case '\r': ADD('r'); break; case '\\': ADD('\\'); break; default: if ( c < 010 ) ADDF("%03o", c); else ADDF("x%02x", c); } } ADD(0); assert(used <= ebuf->avail); return ebuf->buf; #undef ADD #undef ADDF } /* *out_len_r on entry is ignored; out must be at least strlen(in)+1 bytes. */ static void unsanitise_value(char *out, unsigned *out_len_r, const char *in) { const char *ip; char *op; unsigned c; int n; for ( ip = in, op = out; (c = *ip++); *op++ = c ) { if ( c == '\\' ) { c = *ip++; #define GETF(f) do \ { \ n = 0; \ sscanf(ip, f "%n", &c, &n); \ ip += n; \ } while ( 0 ) switch ( c ) { case 't': c= '\t'; break; case 'n': c= '\n'; break; case 'r': c= '\r'; break; case '\\': c= '\\'; break; case 'x': GETF("%2x"); break; case '0': case '4': case '1': case '5': case '2': case '6': case '3': case '7': --ip; GETF("%3o"); break; case 0: --ip; break; default:; } #undef GETF } } *op = 0; if ( out_len_r ) *out_len_r = op - out; } /* make sure there is at least 'len' more space in output_buf */ static void expand_buffer(size_t len) { if (output_pos + len > output_size) { output_size += len + 1024; output_buf = realloc(output_buf, output_size); if (output_buf == NULL) err(1, "malloc"); } } static void output(const char *fmt, ...) { va_list ap; int len; char buf[1]; va_start(ap, fmt); len = vsnprintf(buf, 1, fmt, ap); if (len < 0) err(1, "output"); va_end(ap); expand_buffer(len + 1); va_start(ap, fmt); if (vsnprintf(&output_buf[output_pos], len + 1, fmt, ap) != len) err(1, "output"); va_end(ap); output_pos += len; } static void output_raw(const char *data, int len) { expand_buffer(len); memcpy(&output_buf[output_pos], data, len); output_pos += len; } static void 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] [-R] key [...]", progname, mstr); case MODE_write: mstr = incl_mode ? "write " : ""; errx(1, "Usage: %s %s[-h] [-R] key value [...]", progname, mstr); case MODE_rm: mstr = incl_mode ? "rm " : ""; errx(1, "Usage: %s %s[-h] [-t] key [...]", progname, mstr); case MODE_exists: mstr = incl_mode ? "exists " : ""; /* fallthrough */ case MODE_list: mstr = mstr ? : incl_mode ? "list " : ""; errx(1, "Usage: %s %s[-h] [-p] key [...]", progname, mstr); case MODE_ls: mstr = mstr ? : incl_mode ? "ls " : ""; errx(1, "Usage: %s %s[-h] [-f] [-p] [path]", progname, mstr); case MODE_chmod: mstr = incl_mode ? "chmod " : ""; errx(1, "Usage: %s %s[-h] [-u] [-r] key ", progname, mstr); case MODE_watch: mstr = incl_mode ? "watch " : ""; errx(1, "Usage: %s %s[-h] [-n NR] key", progname, mstr); } } static int do_rm(char *path, struct xs_handle *xsh, xs_transaction_t xth) { if (xs_rm(xsh, xth, path)) { return 0; } else { 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)) static void do_ls(struct xs_handle *h, char *path, int cur_depth, int show_perms) { char **e; char *newpath, *val; int newpath_len; int i; unsigned int num, len; e = xs_directory(h, XBT_NULL, path, &num); if (e == NULL) { if (cur_depth && errno == ENOENT) { /* If a node disappears while recursing, silently move on. */ return; } err(1, "xs_directory (%s)", path); } newpath = malloc(STRING_MAX); if (!newpath) err(1, "malloc in do_ls"); for (i = 0; i 1; break; case MODE_write: transaction = (argc - switch_argv - optind) > 2; break; case MODE_ls: case MODE_watch: 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 = xs_open(0); if (xsh == NULL) err(1, "xs_open"); again: if (transaction) { xth = xs_transaction_start(xsh); if (xth == XBT_NULL) errx(1, "couldn't start transaction"); } ret = perform(mode, optind, argc - switch_argv, argv + switch_argv, xsh, xth, prefix, tidy, upto, recurse, nr_watches, raw); if (transaction && !xs_transaction_end(xsh, xth, ret)) { if (ret == 0 && errno == EAGAIN) { output_pos = 0; goto again; } errx(1, "couldn't end transaction"); } if (output_pos) fwrite(output_buf, 1, output_pos, stdout); free(output_buf); free(ebuf.buf); if (xsh) xs_close(xsh); return ret; }