Propagate the exit code of elevated process, when --wait is used.

This addresses part of the problem about exit code noted in [1], although we
also need to audit that a non-zero exit code is reported in all error
situations.

[1] https://cygwin.com/ml/cygwin/2018-07/msg00020.html

Also report if starting the elevated child process failed.

Signed-off-by: Jon Turney <[email protected]>
---
 main.cc | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/main.cc b/main.cc
index 9ca3f25..1374fb6 100644
--- a/main.cc
+++ b/main.cc
@@ -343,12 +343,16 @@ WinMain (HINSTANCE h,
 
        if (ShellExecuteEx(&sei))
          {
+           DWORD exitcode = 0;
            /* Wait until child process is finished. */
            if (WaitOption && sei.hProcess != NULL)
-             WaitForSingleObject (sei.hProcess, INFINITE);
+             if (!WaitForSingleObject (sei.hProcess, INFINITE))
+               GetExitCodeProcess (sei.hProcess, &exitcode);
+           Logger ().setExitMsg (IDS_ELEVATED);
+           Logger ().exit (exitcode, false);
          }
-       Logger ().setExitMsg (IDS_ELEVATED);
-       Logger ().exit (0, false);
+       Log (LOG_PLAIN) << "Starting elevated child process failed" << endLog;
+       Logger ().exit (1, false);
       }
     else
       {
-- 
2.17.0

Reply via email to