Re: system() exit code
From: Lew Pitcher (lpitcher_at_sympatico.ca)
Date: 05/15/04
- Next message: Dave: "dynamically linked lib inter-dependency problem"
- Previous message: David A. Ferguson: "How to execute from remote NFS?"
- In reply to: Stephen Morgan: "system() exit code"
- Next in thread: Kasper Dupont: "Re: system() exit code"
- Reply: Kasper Dupont: "Re: system() exit code"
- Reply: Stephen Morgan: "Re: system() exit code"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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-----
- Next message: Dave: "dynamically linked lib inter-dependency problem"
- Previous message: David A. Ferguson: "How to execute from remote NFS?"
- In reply to: Stephen Morgan: "system() exit code"
- Next in thread: Kasper Dupont: "Re: system() exit code"
- Reply: Kasper Dupont: "Re: system() exit code"
- Reply: Stephen Morgan: "Re: system() exit code"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|