Re: system() exit code

From: Lew Pitcher (lpitcher_at_sympatico.ca)
Date: 05/15/04


Date: Fri, 14 May 2004 22:40:50 -0400


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stephen Morgan wrote:
| Hi all,
|
| This script, call it "st"
|
| #! /bin/sh
| exit 123
|
| exits with exit code of 123, as it should. However, this C code:
|
| int rc;
| rc = system("/root/yada/yada/st");
| printf("return code is: %d\n", rc);
|
| prints "return code is: 31488" which is 123 shifted left 8 bits. I've
| tried this with a few values and the 8-bit shift is consistent.
|
| Is this normal behavior for the system() function, and if so, is it
| documented somewhere?

Yes, it is normal. Yes it is documented.

In fact, it is the normal way that returncodes are expressed to any parent
process, whether the parent be the caller of system() or the caller of fork()

The system(3) manpage says...
~ RETURN VALUE
~ The value returned is -1 on error (e.g. fork failed), and
~ the return status of the command otherwise. This latter
~ return status is in the format specified in wait(2).
**.....................====================================*******

The status/returncode retrieved by wait(2) consists of two parts, packed into
a single 16bit unsigned integer:
1) a signal status that indicates why the process terminated, and
2) the process termination value (as returned from main() or given to exit()
~ or _exit().)

You use macros to seperate these two values out from the wait(2) status (or,
in your case, the system(3) return value):

~ WTERMSIG(status)
~ returns the number of the signal that caused the
~ child process to terminate. This macro can only be
~ evaluated if WIFSIGNALED returned non-zero.
and
~ WEXITSTATUS(status)
~ evaluates to the least significant eight bits of
~ the return code of the child which terminated,
~ which may have been set as the argument to a call
~ to exit() or as the argument for a return statement
~ in the main program. This macro can only be evalu-
~ ated if WIFEXITED returned non-zero.

These are documented in the wait(2) manpage, along with the other supporting
macros WIFEXITED, WIFSTOPPED, WIFSIGNALED, and WSTOPSIG

| Before I commit to code that relies on this behavior,
| I'd like to know that it's reliable :-)
|
| Thanks for your help,
|
| Stephen Morgan
|
|

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFApYMyagVFX4UWr64RAj26AKCrJJmrO1fFyDLO7OxKoS6wXqpklgCcC2Ds
cM6+bi31ZMIc4sIV6cUQ9Cc=
=oqT5
-----END PGP SIGNATURE-----



Relevant Pages

  • Re: atexit doubt
    ... >I have registered a cleanup function with atexit. ... I don't see exit being called...is it shutdown that invokes ... Anyway, process termination ...
    (comp.unix.programmer)
  • tcltest problem
    ... lappend argv --full-testing ... ## test if the start was allready in test directory "make check" ... Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/ ...
    (comp.lang.tcl)
  • Re: [opensuse] Software Manager (YaST2) - a question about its behaviour
    ... The action time ago was a kind of summary or dialog, exit or restart (non ... Then it was changed to "exit", ... Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/ ...
    (SuSE)
  • script(1) broken on 7.4 - bin/146189 - r207453 fixes
    ... Hash: SHA1 ... scriptwas returning the wrong exit status ... Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ ...
    (freebsd-hackers)
  • Re: [OT] Does "~" always point to $HOME?
    ... simple script, homedir, might look like this: ... (This doesn't work with python-1.5.1 on an ancient Linux system ... Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org ...
    (freebsd-questions)