From ba8e9e8bbf851e38ff230551dacbf149fe551e27 Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Wed, 31 Mar 2021 14:48:09 -0700 Subject: [PATCH] Check vpnc-script exit status on all platforms including Windows See https://gitlab.com/openconnect/vpnc-scripts/-/merge_requests/26, in which I modified vpnc-script-win.js to return a usable exit status. Signed-off-by: Daniel Lenski --- script.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/script.c b/script.c index 7d1864d4..15736c69 100644 --- a/script.c +++ b/script.c @@ -547,7 +547,7 @@ int script_config_tun(struct openconnect_info *vpninfo, const char *reason) char *cmd; PROCESS_INFORMATION pi; STARTUPINFOW si; - DWORD cpflags; + DWORD cpflags, exit_status; if (!vpninfo->vpnc_script || vpninfo->script_tun) return 0; @@ -585,11 +585,28 @@ int script_config_tun(struct openconnect_info *vpninfo, const char *reason) if (CreateProcessW(NULL, script_w, NULL, NULL, FALSE, cpflags, script_env, NULL, &si, &pi)) { ret = WaitForSingleObject(pi.hProcess,10000); + if (!GetExitCodeProcess(pi.hProcess, &exit_status)) { + vpn_progress(vpninfo, PRG_ERR, + _("Failed to get script exit status: %s"), + openconnect__win32_strerror(GetLastError())); + ret = -EIO; + } else if (exit_status > 0 && exit_status != STILL_ACTIVE) { + /* STILL_ACTIVE == 259. That means that a perfectly normal positive integer return value overlaps with + * an exceptional condition. Don't blame me. I didn't design this. + * https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodeprocess#remarks + */ + vpn_progress(vpninfo, PRG_ERR, + _("Script '%s' returned error %ld\n"), + vpninfo->vpnc_script, exit_status); + ret = -EIO; + } CloseHandle(pi.hThread); CloseHandle(pi.hProcess); - if (ret == WAIT_TIMEOUT) + if (ret == WAIT_TIMEOUT || exit_status == STILL_ACTIVE) { + vpn_progress(vpninfo, PRG_ERR, + _("Script did not complete within 10 seconds.")); ret = -ETIMEDOUT; - else + } else if (ret != -EIO) ret = 0; } else { ret = -EIO; -- 2.50.1