From f3b06b62ab757f34ecc239fcf9a87f696082e065 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Thu, 21 Aug 2014 17:20:55 +0200 Subject: [PATCH] use CreateProcess instead of system to run scripts. That prevents the pop up of terminal windows. [dwmw2: Use -ETIMEDOUT instead of -ETIME which doesn't seem to be present in my Fedora 20 MinGW setup. Do not prepend 'cscript' to vpnc_script string now that we invoke it that way unconditionally.] Signed-off-by: Nikos Mavrogiannopoulos Signed-off-by: David Woodhouse --- main.c | 2 +- script.c | 64 ++++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/main.c b/main.c index 8c5a18a3..7c1fdb4e 100644 --- a/main.c +++ b/main.c @@ -647,7 +647,7 @@ static void set_default_vpncscript(void) _pgmptr); exit(1); } - if (asprintf((char **)&default_vpncscript, "cscript %.*s%s", + if (asprintf((char **)&default_vpncscript, "%.*s%s", (c - _pgmptr + 1), _pgmptr, DEFAULT_VPNCSCRIPT) < 0) { fprintf(stderr, _("Allocation for vpnc-script path failed\n")); exit(1); diff --git a/script.c b/script.c index 66b0a0f4..d0eddd39 100644 --- a/script.c +++ b/script.c @@ -318,42 +318,60 @@ int script_config_tun(struct openconnect_info *vpninfo, const char *reason) wchar_t *script_w; int nr_chars; int ret; + char *cmd; + PROCESS_INFORMATION pi; + STARTUPINFOW si; if (!vpninfo->vpnc_script || vpninfo->script_tun) return 0; + memset(&si, 0, sizeof(si)); + si.cb = sizeof(si); + /* probably superfluous */ + si.dwFlags = STARTF_USESHOWWINDOW; + si.wShowWindow = SW_HIDE; + setenv("reason", reason, 1); - nr_chars = MultiByteToWideChar(CP_UTF8, 0, vpninfo->vpnc_script, -1, NULL, 0); + if (asprintf(&cmd, "cscript.exe \"%s\"", vpninfo->vpnc_script) == -1) + return 0; + + nr_chars = MultiByteToWideChar(CP_UTF8, 0, cmd, -1, NULL, 0); script_w = malloc(nr_chars * sizeof(wchar_t)); - if (!script_w) + + if (!script_w) { + free(cmd); return -ENOMEM; + } - MultiByteToWideChar(CP_UTF8, 0, vpninfo->vpnc_script, -1, script_w, nr_chars); - ret = _wsystem(script_w); - free(script_w); + MultiByteToWideChar(CP_UTF8, 0, cmd, -1, script_w, nr_chars); - if (ret == -1) { - int e = errno; - vpn_progress(vpninfo, PRG_ERR, - _("Failed to spawn script '%s' for %s: %s\n"), - vpninfo->vpnc_script, reason, strerror(e)); - return -e; - } - if (ret == 0x2331) { - /* This is what cmd.exe returns for unrecognised commands */ - vpn_progress(vpninfo, PRG_ERR, - _("Failed to spawn script '%s' for %s: %s\n"), - vpninfo->vpnc_script, reason, strerror(ENOENT)); - return -ENOENT; + free(cmd); + + if(CreateProcessW(NULL, script_w, + NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, + NULL, &si, &pi)) { + ret = WaitForSingleObject(pi.hProcess,10000); + CloseHandle(pi.hThread); + CloseHandle(pi.hProcess); + if (ret == WAIT_TIMEOUT) + ret = -ETIMEDOUT; + else + ret = 0; + } else { + ret = -EIO; } - if (ret) { + + if (ret < 0) { vpn_progress(vpninfo, PRG_ERR, - _("Script '%s' returned error %d\n"), - vpninfo->vpnc_script, ret); - return -EIO; + _("Failed to spawn script '%s' for %s: %d\n"), + vpninfo->vpnc_script, reason, (int)GetLastError()); + goto cleanup; } - return 0; + + cleanup: + free(script_w); + return ret; } #else int script_config_tun(struct openconnect_info *vpninfo, const char *reason) -- 2.50.1