fixes #10273 execShellCmd now returns nonzero when child killed with signal + other fixes (#10274)

* s/exitStatus(...)/exitStatusLikeShell(...)/
* fix #10273 execShellCmd now returns nonzero when child exits with signal
* test case for #10249 and explanation for the bug
* fix test failure
* add tests/nim.cfg
This commit is contained in:
Timothee Cour
2019-01-13 00:00:39 -08:00
committed by Andreas Rumpf
parent f5cc2e2de5
commit 9af85fb69f
8 changed files with 148 additions and 41 deletions

View File

@@ -1287,6 +1287,17 @@ proc moveFile*(source, dest: string) {.rtl, extern: "nos$1",
discard tryRemoveFile(dest)
raise
proc exitStatusLikeShell*(status: cint): cint =
## converts exit code from `c_system` into a shell exit code
when defined(posix) and not weirdTarget:
if WIFSIGNALED(status):
# like the shell!
128 + WTERMSIG(status)
else:
WEXITSTATUS(status)
else:
status
proc execShellCmd*(command: string): int {.rtl, extern: "nos$1",
tags: [ExecIOEffect], noNimScript.} =
## Executes a `shell command`:idx:.
@@ -1295,14 +1306,8 @@ proc execShellCmd*(command: string): int {.rtl, extern: "nos$1",
## line arguments given to program. The proc returns the error code
## of the shell when it has finished. The proc does not return until
## the process has finished. To execute a program without having a
## shell involved, use the `execProcess` proc of the `osproc`
## module.
when defined(macosx):
result = c_system(command) shr 8
elif defined(posix):
result = WEXITSTATUS(c_system(command))
else:
result = c_system(command)
## shell involved, use `osproc.execProcess`.
result = exitStatusLikeShell(c_system(command))
# Templates for filtering directories and files
when defined(windows) and not weirdTarget:

View File

@@ -724,13 +724,6 @@ elif not defined(useNimRtl):
proc isExitStatus(status: cint): bool =
WIFEXITED(status) or WIFSIGNALED(status)
proc exitStatus(status: cint): cint =
if WIFSIGNALED(status):
# like the shell!
128 + WTERMSIG(status)
else:
WEXITSTATUS(status)
proc envToCStringArray(t: StringTableRef): cstringArray =
result = cast[cstringArray](alloc0((t.len + 1) * sizeof(cstring)))
var i = 0
@@ -1054,7 +1047,7 @@ elif not defined(useNimRtl):
proc waitForExit(p: Process, timeout: int = -1): int =
if p.exitFlag:
return exitStatus(p.exitStatus)
return exitStatusLikeShell(p.exitStatus)
if timeout == -1:
var status: cint = 1
@@ -1109,7 +1102,7 @@ elif not defined(useNimRtl):
finally:
discard posix.close(kqFD)
result = exitStatus(p.exitStatus)
result = exitStatusLikeShell(p.exitStatus)
else:
import times
@@ -1142,7 +1135,7 @@ elif not defined(useNimRtl):
s.tv_nsec = b.tv_nsec
if p.exitFlag:
return exitStatus(p.exitStatus)
return exitStatusLikeShell(p.exitStatus)
if timeout == -1:
var status: cint = 1
@@ -1220,20 +1213,20 @@ elif not defined(useNimRtl):
if sigprocmask(SIG_UNBLOCK, nmask, omask) == -1:
raiseOSError(osLastError())
result = exitStatus(p.exitStatus)
result = exitStatusLikeShell(p.exitStatus)
proc peekExitCode(p: Process): int =
var status = cint(0)
result = -1
if p.exitFlag:
return exitStatus(p.exitStatus)
return exitStatusLikeShell(p.exitStatus)
var ret = waitpid(p.id, status, WNOHANG)
if ret > 0:
if isExitStatus(status):
p.exitFlag = true
p.exitStatus = status
result = exitStatus(status)
result = exitStatusLikeShell(status)
proc createStream(stream: var Stream, handle: var FileHandle,
fileMode: FileMode) =
@@ -1265,7 +1258,7 @@ elif not defined(useNimRtl):
proc execCmd(command: string): int =
when defined(linux):
let tmp = csystem(command)
result = if tmp == -1: tmp else: exitStatus(tmp)
result = if tmp == -1: tmp else: exitStatusLikeShell(tmp)
else:
result = csystem(command)