Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1K77BK-0000au-1N for qemu-devel@nongnu.org; Fri, 13 Jun 2008 07:10:58 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1K77BH-0000Xb-SF for qemu-devel@nongnu.org; Fri, 13 Jun 2008 07:10:56 -0400 Received: from [199.232.76.173] (port=46718 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1K77BH-0000XF-NO for qemu-devel@nongnu.org; Fri, 13 Jun 2008 07:10:55 -0400 Received: from smtp02.citrix.com ([66.165.176.63]:64504) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1K77BH-0004gC-22 for qemu-devel@nongnu.org; Fri, 13 Jun 2008 07:10:55 -0400 Received: from mariner.uk.xensource.com ([10.80.2.22]) by smtp01.ad.xensource.com (8.13.1/8.13.1) with ESMTP id m5DBAqIN010680 for ; Fri, 13 Jun 2008 04:10:52 -0700 Received: from iwj by mariner.uk.xensource.com with local (Exim 4.63) (envelope-from ) id 1K77B8-0008Ji-CY for qemu-devel@nongnu.org; Fri, 13 Jun 2008 12:10:46 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <18514.21942.382032.95776@mariner.uk.xensource.com> Date: Fri, 13 Jun 2008 12:10:46 +0100 In-Reply-To: <18514.15345.497693.332066@mariner.uk.xensource.com> References: <18513.23567.23400.891876@mariner.uk.xensource.com> <48516D47.6080005@codemonkey.ws> <4851948E.4070300@qumranet.com> <48519A19.8080803@codemonkey.ws> <18514.15345.497693.332066@mariner.uk.xensource.com> From: Ian Jackson Subject: [Qemu-devel] [PATCH] Pass all parameters to -net tap, ... as env vars to if script Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org iwj writes ("Re: [Qemu-devel] [PATCH] New scriptarg=... -net parameter allows passing of an argument"): > I agree that all of the parameters to -net ought to be passed as > environment variables. This is what I've done in the attached patch. Signed-off-by: Ian Jackson qemu_ifup and qemu_ifdown now get in environment variables the values of all of the settings specified on the command line. Each parameter actually specified appears in an environment variable QEMU_IF_. (NB this variable typically has some lowercase letters.) Currently only parameters actually specified appear in the environment; settings which take their default values are not provided to the scripts. --- vl.c | 44 ++++++++++++++++++++++++++++++++++++++------ 1 files changed, 38 insertions(+), 6 deletions(-) diff --git a/vl.c b/vl.c index 229b536..7fe0e40 100644 --- a/vl.c +++ b/vl.c @@ -3963,6 +3963,7 @@ typedef struct TAPState { VLANClientState *vc; int fd; char down_script[1024]; + char net_args[2048]; } TAPState; static void tap_receive(void *opaque, const uint8_t *buf, int size) @@ -4198,11 +4199,19 @@ static int tap_open(char *ifname, int ifname_size) } #endif -static int launch_script(const char *setup_script, const char *ifname, int fd) +static const char *get_opt_name(char *buf, int buf_size, const char *p); +static const char *get_opt_value(char *buf, int buf_size, const char *p); + +static int launch_script(const char *setup_script, const char *ifname, + const char *net_args, int fd) { - int pid, status; + static const char option_env_prefix[] = "QEMU_IF_"; +#define OPTION_ENV_PREFIX_LEN (sizeof(option_env_prefix)-1) + int pid, status, ret; char *args[3]; char **parg; + const char *p; + char envvar[128 + OPTION_ENV_PREFIX_LEN], value[1024]; /* try to launch network script */ pid = fork(); @@ -4216,6 +4225,26 @@ static int launch_script(const char *setup_script, const char *ifname, int fd) i != fd) close(i); + p = net_args; + memcpy(envvar, option_env_prefix, OPTION_ENV_PREFIX_LEN); + for (;;) { + p = get_opt_name(envvar + OPTION_ENV_PREFIX_LEN, + sizeof(envvar) - OPTION_ENV_PREFIX_LEN, + p); + if (*p != '=') + break; + p++; + p = get_opt_value(value, sizeof(value), p); + ret = setenv(envvar, value, 1); + if (ret != 0) { + perror("cannot set option env var"); + _exit(-1); + } + if (*p != ',') + break; + p++; + } + parg = args; *parg++ = (char *)setup_script; *parg++ = (char *)ifname; @@ -4235,7 +4264,8 @@ static int launch_script(const char *setup_script, const char *ifname, int fd) } static int net_tap_init(VLANState *vlan, const char *ifname1, - const char *setup_script, const char *down_script) + const char *setup_script, const char *down_script, + const char *net_args) { TAPState *s; int fd; @@ -4252,7 +4282,7 @@ static int net_tap_init(VLANState *vlan, const char *ifname1, if (!setup_script || !strcmp(setup_script, "no")) setup_script = ""; if (setup_script[0] != '\0') { - if (launch_script(setup_script, ifname, fd)) + if (launch_script(setup_script, ifname, net_args, fd)) return -1; } s = net_tap_fd_init(vlan, fd); @@ -4262,6 +4292,7 @@ static int net_tap_init(VLANState *vlan, const char *ifname1, "tap: ifname=%s setup_script=%s", ifname, setup_script); if (down_script && strcmp(down_script, "no")) snprintf(s->down_script, sizeof(s->down_script), "%s", down_script); + snprintf(s->net_args, sizeof(s->net_args), "%s", net_args); return 0; } @@ -4873,7 +4904,7 @@ static int net_client_init(const char *str) if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) { pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT); } - ret = net_tap_init(vlan, ifname, setup_script, down_script); + ret = net_tap_init(vlan, ifname, setup_script, down_script, p); } } else #endif @@ -7196,6 +7227,7 @@ static void help(int exitcode) " network scripts 'file' (default=%s)\n" " and 'dfile' (default=%s);\n" " use '[down]script=no' to disable script execution;\n" + " script will have QEMU_IF_vlan=... and so on in its environment;\n" " use 'fd=h' to connect to an already opened TAP interface\n" #endif "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n" @@ -8638,7 +8670,7 @@ int main(int argc, char **argv) if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 && s->down_script[0]) - launch_script(s->down_script, ifname, s->fd); + launch_script(s->down_script, ifname, s->net_args, s->fd); } } } -- 1.4.4.4