>From 598cf6b61b3dffb203b419f9b06beea493104f66 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Thu, 21 Aug 2014 17:20:55 +0200 Subject: [PATCH 3/4] use CreateProcess instead of system to run scripts. That prevents the pop up of terminal windows. Signed-off-by: Nikos Mavrogiannopoulos --- script.c | 64 +++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/script.c b/script.c index 66b0a0f..005eaf5 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 = -ETIME; + 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.0.0