Skip to content

Commit

Permalink
use CreateProcess instead of system to run scripts.
Browse files Browse the repository at this point in the history
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 <nmav@redhat.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
Nikos Mavrogiannopoulos authored and David Woodhouse committed Sep 29, 2014
1 parent caf8deb commit f3b06b6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
2 changes: 1 addition & 1 deletion main.c
Expand Up @@ -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);
Expand Down
64 changes: 41 additions & 23 deletions script.c
Expand Up @@ -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)
Expand Down

0 comments on commit f3b06b6

Please sign in to comment.