Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 11 Feb 2013 11:45:57 +0200
From:      Peter Pentchev <roam@ringlet.net>
To:        Erich Dollansky <erichsfreebsdlist@alogt.com>
Cc:        freebsd-doc@freebsd.org
Subject:   Re: missing information in man 3 system
Message-ID:  <20130211094557.GB5801@straylight.m.ringlet.net>
In-Reply-To: <20130211120438.730ce5d3@X220.ovitrap.com>
References:  <20130211120438.730ce5d3@X220.ovitrap.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--eJnRUKwClWJh1Khz
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Mon, Feb 11, 2013 at 12:04:38PM +0700, Erich Dollansky wrote:
> Hi,
>=20
> man 3 system says:
>=20
> RETURN VALUES
>=20
> The system() function returns the exit status of the shell as returned
> by waitpid(2), or -1 if an error occurred when invoking fork(2) or
> waitpid(2). A return value of 127 means the execution of the shell
> failed.
[snip]
>=20
> system () returns the result from 'error.c' in the result but shifted
> by 8 bits to the left.
>=20
> I could not find this behaviour in the FreeBSD documentation.
>=20
> So, is this the intended behaviour of system ()?
>=20
> Should the documentation be uptdated?
>=20
> Or did I simply miss something?

Yes, this is indeed the intended behavior of system(3) - it does say "as
returned by waitpid(2)" :)  Look at the manual page for the waitpid
system call (in section 2, as noted by the number in parentheses) - what
it returns is not the exact exit code of the finished program, it
encodes a bit more information there.  This return code ought to be
examined using the <sys/wait.h> macros described in the wait/waitpid
manual page; you need to do something like:

	if (WIFEXITED(code)) {
		printf("The program exited with code %d\n",
		    WEXITSTATUS(code));
	} else if (WIFSIGNALED(code)) {
		printf("The program was terminated by signal %d\n",
		    WTERMSIG(code));
	} else if (WIFSTOPPED(code)) {
		/**
		 * You will only get here if you explicitly ask to be
		 * notified about child processes being stopped using
		 * the flags of the waitpid() syscall; you will never
		 * get here when examining the exit code of system(3).
		 */
		printf("The program was stopped by signal %d\n",
		    WSTOPSIG(code));
	} else {
		printf("The program neither exited, nor was "
		    "terminated or stopped by a signal; what in "
		    "the world does wait() return value %d mean?!\n",
		    code);
	}

Yes, it may seem a bit convoluted at first glance, but it is indeed
necessary for the purpose of providing all the information in a single
return value.  Don't worry, this is something that everyone stumbles
into when making their first steps in system programming :) (pun
unintended ;)

Hope that helps!

G'luck,
Peter

--=20
Peter Pentchev	roam@ringlet.net roam@FreeBSD.org p.penchev@storpool.com
PGP key:	http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint 2EE7 A7A5 17FC 124C F115  C354 651E EFB0 2527 DF13
This sentence claims to be an Epimenides paradox, but it is lying.

--eJnRUKwClWJh1Khz
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iQIcBAEBCAAGBQJRGL3VAAoJEGUe77AlJ98TlawP/1YAvlWl+TFWAZbj5TUYxwE0
gQTO/bIIZR9mF296GGIOJJCT93ZFBEzBaAGC1hKFwSo3B22xFPTfb2/z+vVzvhOv
j1MI5M5iFkR5f+mvpkP6cmNuB491j3mRsSRZeFv+xpC2szhLpzvffnG8024EBYAT
jjtR22oqYq5A+yTxhjzTsQR/9KMlNRxfASDSgjKAhwrWwrTCiqe0oTBhtNlCS5Qr
baiyBeNfrKea9/S8fxQnEJI+Oe1nxQe2lAefA8eSDfgQdh+ccCpvXg3f9xKYW8+N
jCd8tpwwnHjfatWkNt7bjT4PVzlJb1rnHjbDS0Qs567WRJ94zzGG9AqM+mfwso+l
1+8Ei78SYslmxwEFnvkYJLdq3O84FcaTfmOEgdf9hsDVN33teaNolJ8vVx4Gd6Lt
L40TYOhypk5HRZPg+24ezg6T6gtXy+3MDaIkmQnLyLNJ9a4c/5ZRCxm5uO8RHk2D
BLt8tVQocZ5zZ5RfRD4QXfcEmI4rfeUBTtmaJkDobKzRYx3Fc6l2BU3TdxJ4bAez
DfqOh7LAXs1mBh8Dbh1ei7k73q4PUftV1ty5/Xf7Bu15fbqODWSBIdNxTKYcx/qh
YY53psDVFj6+/Io12aGrIMK8hLgCNWhdxvuWKdAtzIgvi3QcAhwOK5zjDPh/T/mq
qqgGiCBKA0kYxWDq1nld
=JIRY
-----END PGP SIGNATURE-----

--eJnRUKwClWJh1Khz--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20130211094557.GB5801>