]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
use CreateProcess instead of system to run scripts.
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Thu, 21 Aug 2014 15:20:55 +0000 (17:20 +0200)
committerDavid Woodhouse <David.Woodhouse@intel.com>
Mon, 29 Sep 2014 14:27:31 +0000 (15:27 +0100)
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>
main.c
script.c

diff --git a/main.c b/main.c
index 8c5a18a321c8386a6e606349c411b27ec16ff715..7c1fdb4e713072a5d26e1e973479acd458ee7b0d 100644 (file)
--- 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);
index 66b0a0f47245b61078270fcf6d5cea3b0ed4125d..d0eddd39938e7e59be0a310fb04d6c9021859196 100644 (file)
--- 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)