From b2cb1bd5310c18a9843101df0583dc4a7cc91436 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 4 Jun 2016 14:54:41 -0400 Subject: [PATCH] Wait for all children processes to stop This fixes installation failing when we check for the wrong child and don't let the installer finish. However, this code now ignores the status from the child. I might see if there's a better way around fixing that.. --- Sparkle/SUFileManager.m | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/Sparkle/SUFileManager.m b/Sparkle/SUFileManager.m index 3bf8d24f..113a8017 100644 --- a/Sparkle/SUFileManager.m +++ b/Sparkle/SUFileManager.m @@ -32,21 +32,33 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - if (AuthorizationExecuteWithPrivileges(authorization, executablePath, options, arguments, NULL) == errAuthorizationSuccess) + if (AuthorizationExecuteWithPrivileges(authorization, executablePath, options, arguments, NULL) == errAuthorizationSuccess) { #pragma clang diagnostic pop - { - int status = 0; - pid_t pid = 0; - - do { - pid = wait(&status); - } while (pid == -1 && errno == EINTR); - - if (pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) - returnValue = NO; - } - else + // Wait for every child to finish termination because we won't know which particular child is ours. + // Perhaps in the future writing a custom wrapper tool that communicates its pid in stdout may be a better route + // However that has the security issue of anyone being able to swap in our tool with their own + // Another possible idea is using sh -c and echo'ing the pid which we read from here, then executing our command, + // but we would have to be careful about sanitizing paths, \ escaping quotation marks, ... + while (true) { + pid_t waitPid = 0; + int waitStatus = 0; + + waitPid = wait(&waitStatus); + if (waitPid != -1 || errno == EINTR) { + // Continue and wait for all children to terminate + } else { + if (errno != ECHILD) { + // Ran into an abnormal error + returnValue = NO; + } + break; + } + } + // We can't check the status from wait() here because the child whose status we check may not be the child we are responsible for + // So we will assume that the process terminated successfully; I know, not ideal. + } else { returnValue = NO; + } return returnValue; }