From owner-cvs-all Tue Mar 21 21:17:17 2000 Delivered-To: cvs-all@freebsd.org Received: from 1Cust91.tnt1.washington.dc.da.uu.net (localhost [127.0.0.1]) by hub.freebsd.org (Postfix) with ESMTP id 1651E37C090; Tue, 21 Mar 2000 21:17:06 -0800 (PST) (envelope-from green@FreeBSD.org) Date: Wed, 22 Mar 2000 00:17:03 -0500 (EST) From: Brian Fundakowski Feldman X-Sender: green@green.dyndns.org To: Brian Somers Cc: cvs-committers@FreeBSD.org, cvs-all@FreeBSD.org Subject: Re: cvs commit: src/usr.sbin/ppp exec.c In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-cvs-all@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Tue, 21 Mar 2000, Brian Fundakowski Feldman wrote: > On Tue, 21 Mar 2000, Brian Somers wrote: > > > FWIW: > > I tried to implement this by doing a pipe(), setting the > > write desciptors close-on-exec flag in the child and writing > > errno to the descriptor if the exec() fails. The parent can > > then ``if (read()) got errno else exec worked''. > > > > This didn't work though - the child could write() to fd[1] on > > exec failure, but the parent got 0 trying to read() from fd[0] ! > > Is this a bug in execve() ? I don't get that problem, exactly, since the parent gets the right read for me, not 0. It's not an easy problem you're trying to solve, and I wish I could think of a better way than this. Why do you really care the difference between exec failing and the program which was execed failing? Here's a much better example of how it's possible to do what you want: {"/home/green"}$ ./bar Hey, dad! Lemme exec something impossible. Child exec failed (4186). Okay, I'll exec something good now. hello Child exec succeeded. #include #include #include #include #include int main() { fd_set readset; struct timeval tv; int pipes[2]; int error, i; char data[sizeof(int)]; char *const impossible[] = { "/", NULL }; char *const echo[] = { "echo", "hello", NULL }; error = pipe(pipes); if (error != 0) err(1, "pipe"); switch (fork()) { default: for (i = 0; i < 2; i++) { FD_ZERO(&readset); FD_SET(pipes[0], &readset); tv.tv_sec = 0; tv.tv_usec = 100000; error = select(pipes[0] + 1, &readset, NULL, NULL, &tv); switch (error) { case -1: err(1, "select"); case 0: printf("Child exec succeeded.\n"); break; case 1: if (read(pipes[0], data, sizeof(data)) != sizeof(data)) err(1, "parent read"); printf("Child exec failed (%d).\n", *(int *)data); } } exit(0); case -1: err(1, "fork"); case 0: setsid(); printf("Hey, dad! Lemme exec something impossible.\n"); (void)execve(*impossible, impossible, NULL); *(int *)data = 4186; if (write(pipes[1], data, sizeof(data)) != sizeof(data)) err(1, "child write"); printf("Okay, I'll exec something good now.\n"); if (execve("/bin/echo", echo, NULL) != 0) err(1, "execve"); exit(0); } } -- Brian Fundakowski Feldman \ FreeBSD: The Power to Serve! / green@FreeBSD.org `------------------------------' To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe cvs-all" in the body of the message