Simplify handling scripts: parse all these "no" and '\0' once, and then keep simpler logic for net_tap_open() and net_init_tap_one(): NULL means no script to run, otherwise run script.
Signed-off-by: Vladimir Sementsov-Ogievskiy <[email protected]> Reviewed-by: Maksim Davydov <[email protected]> --- net/tap.c | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/net/tap.c b/net/tap.c index 5c6b9399e0..017c184933 100644 --- a/net/tap.c +++ b/net/tap.c @@ -91,6 +91,21 @@ static void launch_script(const char *setup_script, const char *ifname, static void tap_send(void *opaque); static void tap_writable(void *opaque); +static char *tap_parse_script(const char *script_arg, const char *default_path) +{ + g_autofree char *res = g_strdup(script_arg); + + if (!res) { + res = get_relocated_path(default_path); + } + + if (res[0] == '\0' || strcmp(res, "no") == 0) { + return NULL; + } + + return g_steal_pointer(&res); +} + static void tap_update_fd_handler(TAPState *s) { qemu_set_fd_handler(s->fd, @@ -676,9 +691,7 @@ static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr, return -1; } - if (setup_script && - setup_script[0] != '\0' && - strcmp(setup_script, "no") != 0) { + if (setup_script) { launch_script(setup_script, ifname, fd, &err); if (err) { error_propagate(errp, err); @@ -714,9 +727,9 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, qemu_set_info_str(&s->nc, "helper=%s", tap->helper); } else { qemu_set_info_str(&s->nc, "ifname=%s,script=%s,downscript=%s", ifname, - script, downscript); + script ?: "no", downscript ?: "no"); - if (strcmp(downscript, "no") != 0) { + if (downscript) { snprintf(s->down_script, sizeof(s->down_script), "%s", downscript); snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname); @@ -971,23 +984,16 @@ free_fail: return -1; } } else { - const char *script = tap->script; - const char *downscript = tap->downscript; - g_autofree char *default_script = NULL; - g_autofree char *default_downscript = NULL; + g_autofree char *script = + tap_parse_script(tap->script, DEFAULT_NETWORK_SCRIPT); + g_autofree char *downscript = + tap_parse_script(tap->downscript, DEFAULT_NETWORK_DOWN_SCRIPT); + if (tap->vhostfds) { error_setg(errp, "vhostfds= is invalid if fds= wasn't specified"); return -1; } - if (!script) { - script = default_script = get_relocated_path(DEFAULT_NETWORK_SCRIPT); - } - if (!downscript) { - downscript = default_downscript = - get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT); - } - if (tap->ifname) { pstrcpy(ifname, sizeof ifname, tap->ifname); } else { @@ -995,7 +1001,7 @@ free_fail: } for (i = 0; i < queues; i++) { - fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script, + fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? NULL : script, ifname, sizeof ifname, queues > 1, errp); if (fd == -1) { return -1; @@ -1010,8 +1016,8 @@ free_fail: } net_init_tap_one(tap, peer, "tap", name, ifname, - i >= 1 ? "no" : script, - i >= 1 ? "no" : downscript, + i >= 1 ? NULL : script, + i >= 1 ? NULL : downscript, vhostfdname, vnet_hdr, fd, &err); if (err) { error_propagate(errp, err); -- 2.48.1
