lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, May 17, 2013 at 11:46 AM, Gyepi SAM <self-lua@gyepi.com> wrote:

> We need to make a distinction here between wait() and system().
> Everything written here applies to wait, but does differ for system()
> because system calls "/bin/sh -c" (or the moral equivalent) and returns
> the shell's exit code.

I don't understand your implications at all. The interface to wait()'s
status parameter is the same as system()'s return value.

POSIX 2008, Vol. 2, p. 66609:

"If command is not a null pointer, system ( ) shall return the
termination status of the command
language interpreter in the format specified by waitpid ( ). The
termination status shall be as
defined for the sh utility; otherwise, the termination status is
unspecified. If some error prevents
the command language interpreter from executing after the child
process is created, the return
value from system ( ) shall be as if the command language interpreter
had terminated using
exit(127) or _exit(127). If a child process cannot be created, or if
the termination status for the
command language interpreter cannot be obtained, system ( ) shall
return -1 and set errno to
indicate the error."

What you wrote would only hold if you couldn't wait() on a process
whose semantics match those of sh's.

> When a process terminates normally, the shell returns
> its exit code. However, when the process is terminated by a signal, it
> returns 128 + the signal number.

Actually, the standard only specifies that the exit status be greater
than 128, so you can't deterministically infer what signal caused the
exit by assuming it's 128 + sig. #.

POSIX 2008, Vol. 3, p 74314:

"Internally, for purposes of deciding whether a command exits with a
non-zero exit status, the
shell shall recognize the entire status value retrieved for the
command by the equivalent of the
wait ( ) function WEXITSTATUS macro (as defined in the System
Interfaces volume of
POSIX.1-2008). When reporting the exit status with the special
parameter '?', the shell shall
report the full eight bits of exit status available. The exit status
of a command that terminated
because it received a signal shall be reported as greater than 128."

> wait is defined by POSIX and system is defined by C89, C99 and POSIX.
> However, the C standard leaves the return value as an implementation detail
> whereas POSIX requires wait semantics (with the tepid note that its requirements are
> aligned with C). Mind the gap.
>
> Finally, keep in mind that system is just a convenience wrapper around fork, exec and
> waitpid.

system() is commonly implemented as wrappers around these functions,
but that's not necessarily true.