generally update exitCode only after successful completion of waitpid()

This commit is contained in:
Johannes Hofmann 2016-09-22 10:55:21 +02:00
commit 5986b78bbe

View file

@ -957,13 +957,10 @@ elif not defined(useNimRtl):
proc running(p: Process): bool = proc running(p: Process): bool =
var ret : int var ret : int
when not defined(freebsd): var status : cint = 1
ret = waitpid(p.id, p.exitCode, WNOHANG) ret = waitpid(p.id, status, WNOHANG)
else: if WIFEXITED(status):
var status : cint = 1 p.exitCode = status
ret = waitpid(p.id, status, WNOHANG)
if WIFEXITED(status):
p.exitCode = status
if ret == 0: return true # Can't establish status. Assume running. if ret == 0: return true # Can't establish status. Assume running.
result = ret == int(p.id) result = ret == int(p.id)
@ -982,9 +979,10 @@ elif not defined(useNimRtl):
proc waitForExit(p: Process, timeout: int = -1): int = proc waitForExit(p: Process, timeout: int = -1): int =
if p.exitCode != -3: return p.exitCode if p.exitCode != -3: return p.exitCode
if timeout == -1: if timeout == -1:
if waitpid(p.id, p.exitCode, 0) < 0: var status : cint = 1
p.exitCode = -3 if waitpid(p.id, status, 0) < 0:
raiseOSError(osLastError()) raiseOSError(osLastError())
p.exitCode = status
else: else:
var kqFD = kqueue() var kqFD = kqueue()
if kqFD == -1: if kqFD == -1:
@ -1004,6 +1002,7 @@ elif not defined(useNimRtl):
try: try:
while true: while true:
var status : cint = 1
var count = kevent(kqFD, addr(kevIn), 1, addr(kevOut), 1, var count = kevent(kqFD, addr(kevIn), 1, addr(kevOut), 1,
addr(tmspec)) addr(tmspec))
if count < 0: if count < 0:
@ -1014,15 +1013,15 @@ elif not defined(useNimRtl):
# timeout expired, so we trying to kill process # timeout expired, so we trying to kill process
if posix.kill(p.id, SIGKILL) == -1: if posix.kill(p.id, SIGKILL) == -1:
raiseOSError(osLastError()) raiseOSError(osLastError())
if waitpid(p.id, p.exitCode, 0) < 0: if waitpid(p.id, status, 0) < 0:
p.exitCode = -3
raiseOSError(osLastError()) raiseOSError(osLastError())
p.exitCode = status
break break
else: else:
if kevOut.ident == p.id.uint and kevOut.filter == EVFILT_PROC: if kevOut.ident == p.id.uint and kevOut.filter == EVFILT_PROC:
if waitpid(p.id, p.exitCode, 0) < 0: if waitpid(p.id, status, 0) < 0:
p.exitCode = -3
raiseOSError(osLastError()) raiseOSError(osLastError())
p.exitCode = status
break break
else: else:
raiseOSError(osLastError()) raiseOSError(osLastError())
@ -1036,6 +1035,8 @@ elif not defined(useNimRtl):
const const
hasThreadSupport = compileOption("threads") and not defined(nimscript) hasThreadSupport = compileOption("threads") and not defined(nimscript)
var status : cint = 1
proc waitForExit(p: Process, timeout: int = -1): int = proc waitForExit(p: Process, timeout: int = -1): int =
template adjustTimeout(t, s, e: Timespec) = template adjustTimeout(t, s, e: Timespec) =
var diff: int var diff: int
@ -1067,9 +1068,9 @@ elif not defined(useNimRtl):
# initialized with -3, wrong success exit codes are prevented. # initialized with -3, wrong success exit codes are prevented.
if p.exitCode != -3: return p.exitCode if p.exitCode != -3: return p.exitCode
if timeout == -1: if timeout == -1:
if waitpid(p.id, p.exitCode, 0) < 0: if waitpid(p.id, status, 0) < 0:
p.exitCode = -3
raiseOSError(osLastError()) raiseOSError(osLastError())
p.exitCode = status
else: else:
var nmask, omask: Sigset var nmask, omask: Sigset
var sinfo: SigInfo var sinfo: SigInfo
@ -1100,9 +1101,9 @@ elif not defined(useNimRtl):
let res = sigtimedwait(nmask, sinfo, tmspec) let res = sigtimedwait(nmask, sinfo, tmspec)
if res == SIGCHLD: if res == SIGCHLD:
if sinfo.si_pid == p.id: if sinfo.si_pid == p.id:
if waitpid(p.id, p.exitCode, 0) < 0: if waitpid(p.id, status, 0) < 0:
p.exitCode = -3
raiseOSError(osLastError()) raiseOSError(osLastError())
p.exitCode = status
break break
else: else:
# we have SIGCHLD, but not for process we are waiting, # we have SIGCHLD, but not for process we are waiting,
@ -1122,9 +1123,9 @@ elif not defined(useNimRtl):
# timeout expired, so we trying to kill process # timeout expired, so we trying to kill process
if posix.kill(p.id, SIGKILL) == -1: if posix.kill(p.id, SIGKILL) == -1:
raiseOSError(osLastError()) raiseOSError(osLastError())
if waitpid(p.id, p.exitCode, 0) < 0: if waitpid(p.id, status, 0) < 0:
p.exitCode = -3
raiseOSError(osLastError()) raiseOSError(osLastError())
p.exitCode = status
break break
else: else:
raiseOSError(err) raiseOSError(err)
@ -1139,14 +1140,16 @@ elif not defined(useNimRtl):
result = int(p.exitCode) shr 8 result = int(p.exitCode) shr 8
proc peekExitCode(p: Process): int = proc peekExitCode(p: Process): int =
var status : cint = 1
if p.exitCode != -3: return p.exitCode if p.exitCode != -3: return p.exitCode
var ret = waitpid(p.id, p.exitCode, WNOHANG) var ret = waitpid(p.id, status, WNOHANG)
var b = ret == int(p.id) var b = ret == int(p.id)
if b: result = -1 if b: result = -1
if not WIFEXITED(p.exitCode): if WIFEXITED(status):
p.exitCode = -3 p.exitCode = status
result = p.exitCode.int shr 8
else:
result = -1 result = -1
else: result = p.exitCode.int shr 8
proc createStream(stream: var Stream, handle: var FileHandle, proc createStream(stream: var Stream, handle: var FileHandle,
fileMode: FileMode) = fileMode: FileMode) =