From owner-freebsd-doc@FreeBSD.ORG Mon Feb 11 09:51:26 2013 Return-Path: Delivered-To: freebsd-doc@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 50A0DB77 for ; Mon, 11 Feb 2013 09:51:26 +0000 (UTC) (envelope-from roam@ringlet.net) Received: from nimbus.fccf.net (nimbus.fccf.net [77.77.144.35]) by mx1.freebsd.org (Postfix) with ESMTP id CD5331789 for ; Mon, 11 Feb 2013 09:51:25 +0000 (UTC) Received: from straylight.m.ringlet.net (unknown [78.90.13.150]) by nimbus.fccf.net (Postfix) with ESMTPSA id AF987B56 for ; Mon, 11 Feb 2013 11:45:57 +0200 (EET) Received: from roam (uid 1000) (envelope-from roam@ringlet.net) id dae02a by straylight.m.ringlet.net (DragonFly Mail Agent); Mon, 11 Feb 2013 11:45:57 +0200 Date: Mon, 11 Feb 2013 11:45:57 +0200 From: Peter Pentchev To: Erich Dollansky Subject: Re: missing information in man 3 system Message-ID: <20130211094557.GB5801@straylight.m.ringlet.net> Mail-Followup-To: Erich Dollansky , freebsd-doc@freebsd.org References: <20130211120438.730ce5d3@X220.ovitrap.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="eJnRUKwClWJh1Khz" Content-Disposition: inline In-Reply-To: <20130211120438.730ce5d3@X220.ovitrap.com> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: freebsd-doc@freebsd.org X-BeenThere: freebsd-doc@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Documentation project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Feb 2013 09:51:26 -0000 --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 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--