From: Martin Lucina Date: Mon, 26 Jan 2015 11:40:11 +0000 (+0100) Subject: rumprun, rumpconfig: Add support for setting guest environment. X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=1a5460b8ac3941c3b67535d3ce44d8b9108582f2;p=rumpuser-xen.git rumprun, rumpconfig: Add support for setting guest environment. Signed-off-by: Martin Lucina --- diff --git a/app-tools/rumprun b/app-tools/rumprun index 2bdf6f3..889bd57 100755 --- a/app-tools/rumprun +++ b/app-tools/rumprun @@ -35,7 +35,7 @@ err() { usage() { cat <>${xenstore} bindex=$(expr $bindex + 1) ;; + # -e: Set guest environment variable. + e) + case "${OPTARG}" in + *=*) + key=${OPTARG%%=*} + value=${OPTARG#*=} + ;; + *) + key=${OPTARG} + value= + ;; + esac + [ -n "$key" ] || usage + echo environ/${key} "${value}" >>${xenstore} + ;; # -p: Leave the domain paused after creation. p) opt_pause=1 @@ -237,8 +253,8 @@ EOM domid=$(${sudo} xl domid ${name}) # Write provisioning information for domain to xenstore. prefix=/local/domain/${domid}/rumprun - cat ${xenstore} | while read line; do - xenstore-write ${prefix}/${line} + cat ${xenstore} | while read key value; do + xenstore-write "${prefix}/${key}" "${value}" done rm ${xenstore} # Attach debugger if requested. diff --git a/rumpconfig.c b/rumpconfig.c index cec6a5d..3c55fc4 100644 --- a/rumpconfig.c +++ b/rumpconfig.c @@ -410,13 +410,66 @@ out: free(blk_fstype); } +static int +xs_read_environ(const char *name, char **value_out) +{ + char *value = NULL; + char buf[128]; + char *xberr = NULL; + xenbus_transaction_t txn; + int xbretry = 0; + + xberr = xenbus_transaction_start(&txn); + if (xberr) { + warnx("rumprun_config: xenbus_transaction_start() failed: %s", + xberr); + return 1; + } + snprintf(buf, sizeof buf, "rumprun/environ/%s", name); + xberr = xenbus_read(txn, buf, &value); + if (xberr) { + warnx("rumprun_config: environ: read %s failed: %s", + buf, xberr); + xenbus_transaction_end(txn, 0, &xbretry); + return 1; + } + xberr = xenbus_transaction_end(txn, 0, &xbretry); + if (xberr) { + warnx("rumprun_config: xenbus_transaction_end() failed: %s", + xberr); + free(value); + return 1; + } + *value_out = value; + return 0; +} + +static void +rumprun_config_environ(const char *name) +{ + char *value = NULL; + int rv; + + rv = xs_read_environ(name, &value); + if (rv != 0) + return; + if (setenv(name, value, 1) != 0) { + warnx("rumprun_config: setenv(%s) failed: %d", errno); + goto out; + } + +out: + free(value); +} + void _rumprun_config(void) { char *err = NULL; xenbus_transaction_t txn; char **netdevices = NULL, - **blkdevices = NULL; + **blkdevices = NULL, + **environ = NULL; int retry = 0, i; @@ -438,6 +491,13 @@ _rumprun_config(void) xenbus_transaction_end(txn, 0, &retry); goto out_err; } + err = xenbus_ls(txn, "rumprun/environ", &environ); + if (err && strcmp(err, "ENOENT") != 0) { + warnx("rumprun_config: xenbus_ls(rumprun/environ) failed: %s", + err); + xenbus_transaction_end(txn, 0, &retry); + goto out_err; + } err = xenbus_transaction_end(txn, 0, &retry); if (err) { warnx("rumprun_config: xenbus_transaction_end() failed: %s", @@ -458,6 +518,13 @@ _rumprun_config(void) } free(blkdevices); } + if (environ) { + for(i = 0; environ[i]; i++) { + rumprun_config_environ(environ[i]); + free(environ[i]); + } + free(environ); + } return; out_err: @@ -471,6 +538,11 @@ out_err: free(blkdevices[i]); free(blkdevices); } + if (environ) { + for(i = 0; environ[i]; i++) + free(environ[i]); + free(environ); + } } void diff --git a/tests/hello/hello.c b/tests/hello/hello.c index 4859dbb..3d97396 100644 --- a/tests/hello/hello.c +++ b/tests/hello/hello.c @@ -1,8 +1,13 @@ #include +#include int main (int argc, char *argv[]) { - printf ("Hello, world!\n"); + char *world = getenv ("WORLD"); + if (world) + printf ("Hello, %s!\n", world); + else + printf ("Hello, world!\n"); printf ("Sleeping 5s...\n"); sleep (5); printf ("Goodbye, world!\n");