qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH] Pass all parameters to -net tap, ... as env vars to


From: Ian Jackson
Subject: [Qemu-devel] [PATCH] Pass all parameters to -net tap, ... as env vars to if script
Date: Fri, 13 Jun 2008 12:10:46 +0100

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 <address@hidden>


qemu_ifup and qemu_ifdown now get in environment variables the values
of all of the settings specified on the command line.  Each parameter
<foo> actually specified appears in an environment variable
QEMU_IF_<foo>.  (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





reply via email to

[Prev in Thread] Current Thread [Next in Thread]