Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!seismo!brl-adm!brl-smoke!gwyn From: gwyn@brl-smoke.UUCP Newsgroups: comp.unix.questions Subject: Re: A couple questions Message-ID: <5767@brl-smoke.ARPA> Date: Thu, 16-Apr-87 09:55:12 EST Article-I.D.: brl-smok.5767 Posted: Thu Apr 16 09:55:12 1987 Date-Received: Fri, 17-Apr-87 05:41:48 EST References: <3164@jade.BERKELEY.EDU> <1296@decuac.DEC.COM> <80@eps2.UUCP> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 30 In article <80@eps2.UUCP> jon@eps2.UUCP (Jonathan Hue) writes: -In article <1296@decuac.DEC.COM>, avolio@gildor.dec.com (Frederick M. Avolio) writes: -> if ((pid = fork()) == 0) /* IF CHILD */ -> { -> execl("/bin/sh", "sh", "-c", arg, 0); -> printf("HELP! SOMEONE STOLE THE SHELL!\n"); -> exit(1); - -Use _exit() in this instance instead of exit() because there is the -possibility of flushing the stdio buffers twice, once in the child and -once in the parent, which wouldn't really be right. You might see some -output twice. I would probably use an fputs to stderr here, to avoid -buffering. This was probably just an oversight on Frederick's followup. -To really confuse people running ps, replace "sh" with a clever saying in -double quotes. The above advice, which is good as far as it goes, is insufficient. Using _exit() will mean that the printf may not be seen (although with default line-buffering to the terminal it probably would be), since stdio buffer flushing would be skipped. Before the fork(), there should be an fflush(stdout) to clear out any buffered parent data. Error messages should probably be written to stderr rather than stdout (stdout is often directed down a pipe). Finally, it is much better style to return an error indication to a higher level of program control and let the higher level determine strategy (such as whether to print an error message). The fact that it is hard to get this stuff exactly right is why one should use the library routines such as system() instead, whenever possible. (If the library routine is broken, get it fixed!)