From owner-freebsd-hackers Sun Mar 21 22:19:12 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from spinner.netplex.com.au (spinner.netplex.com.au [202.12.86.3]) by hub.freebsd.org (Postfix) with ESMTP id 3D01B14D5C for ; Sun, 21 Mar 1999 22:19:08 -0800 (PST) (envelope-from peter@netplex.com.au) Received: from spinner.netplex.com.au (localhost [127.0.0.1]) by spinner.netplex.com.au (8.9.2/8.9.2/Netplex) with ESMTP id OAA53483; Mon, 22 Mar 1999 14:18:25 +0800 (WST) (envelope-from peter@spinner.netplex.com.au) Message-Id: <199903220618.OAA53483@spinner.netplex.com.au> X-Mailer: exmh version 2.0.2 2/24/98 To: Ollivier Robert Cc: "FreeBSD Hackers' list" Subject: Re: Fwd: Re: cron problem? In-reply-to: Your message of "Mon, 22 Mar 1999 00:13:09 +0100." <19990322001309.A53335@keltia.freenix.fr> Date: Mon, 22 Mar 1999 14:18:25 +0800 From: Peter Wemm Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Ollivier Robert wrote: > Wietse found the following while writing Postfix. Can anyone look at it ? A followup is on the way, I tracked down what is causing it. cron(8) is passed stdout etc pointing to /dev/console at startup. [some time later at the end of the boot sequence] init fires up a getty on /dev/ttyv0 revoke("/dev/ttyv0") is redirecting the /dev/console fd's to deadfs [later] cron does a popen() to sendmail (postfix in this case) stdin is a pipe, stdout/err are inherited deadfs descriptors formerly to /dev/console fstat() on the inherited descriptor returns EBADF. methods of testing open fd's no longer work. > ----- Forwarded message from Wietse Venema ----- > > Subject: Re: cron problem? > Date: Fri, 19 Mar 1999 14:59:34 -0500 (EST) > From: wietse@porcupine.org (Wietse Venema) > > Found it. I'll post a patch later. This must be a *BSD oddity. > > Wietse > > Problem: > > Vixie cron output from an /etc/crontab entry is not mailed, > unless the crontab line ends with an explicit mail command > > System: > > FreeBSD 2.x, BSD/OS 2.x ... (yes I'm not running the latest) > > Analysis: > > For the sake of security and robustness, Postfix tries to > ensure that stdin/out/err are open before opening anything > else. The following code fragment appears in many programs > that I wrote in the course of time: > > for (fd = 0; fd < 3; fd++) > if (fstat(fd, &st) == -1 && open("/dev/null", O_WRONLY) != fd) > msg_fatal("open /dev/null: %m"); > > This fragment examines the status of stdin/stdout/stderr. > When fstat() returns an error, the code assumes the file > descriptor is not in use, and expects that the next open() > will allocate that file descriptor. > > However, the kernel trace shows a small surprise: > > 3817 sendmail CALL fstat(0,0xefbfddc8) > 3817 sendmail RET fstat 0 > 3817 sendmail CALL fstat(0x1,0xefbfddc8) > 3817 sendmail RET fstat -1 errno 9 Bad file descriptor > 3817 sendmail CALL open(0x1c8c,0x1,0xefbfde4c) > 3817 sendmail NAMI "/dev/null" > 3817 sendmail RET open 3 > > So: fstat(1) returns EBADF, and open() returns 3. > > Conclusion: file descriptor 1 is still in use. > > *** Why on earth does fstat() return EBADF on an open file > descriptor? *** > > Workaround: > > Close the file descriptor after error return from fstat(). > > The following code does the job. > > if (fstat(fd, &st) == -1 > && (close(fd), open("/dev/null", O_WRONLY)) != fd) > msg_fatal("open /dev/null: %m"); > > > > > ----- End forwarded message ----- > > -- > Ollivier ROBERT -=- FreeBSD: The Power to Serve! -=- roberto@keltia.freenix.f r > FreeBSD keltia.freenix.fr 4.0-CURRENT #70: Sat Feb 27 09:43:08 CET 1999 > > > > To Unsubscribe: send mail to majordomo@FreeBSD.org > with "unsubscribe freebsd-hackers" in the body of the message > Cheers, -Peter -- Peter Wemm Netplex Consulting "No coffee, No workee!" :-) To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message