From owner-freebsd-hackers Tue Feb 25 19:13:15 1997 Return-Path: Received: (from root@localhost) by freefall.freebsd.org (8.8.5/8.8.5) id TAA09280 for hackers-outgoing; Tue, 25 Feb 1997 19:13:15 -0800 (PST) Received: from bunyip.cc.uq.edu.au (daemon@bunyip.cc.uq.edu.au [130.102.2.1]) by freefall.freebsd.org (8.8.5/8.8.5) with ESMTP id TAA09271 for ; Tue, 25 Feb 1997 19:12:58 -0800 (PST) Received: (from daemon@localhost) by bunyip.cc.uq.edu.au (8.8.5/8.8.5) id NAA19728; Wed, 26 Feb 1997 13:10:46 +1000 Received: by ogre.devetir.qld.gov.au (8.7.5/DEVETIR-E0.3a) id NAA24882; Wed, 26 Feb 1997 13:11:14 +1000 (EST) Date: Wed, 26 Feb 1997 13:11:14 +1000 (EST) From: Stephen McKay Message-Id: <199702260311.NAA24882@ogre.devetir.qld.gov.au> To: j@uriah.heep.sax.de (J Wunsch), Naoki Hamada cc: freebsd-hackers@freebsd.org, syssgm@devetir.qld.gov.au Subject: Re: MALLOC(3) writes to stderr... X-Newsreader: NN version 6.5.0 #1 (NOV) Sender: owner-hackers@freebsd.org X-Loop: FreeBSD.org Precedence: bulk j@uriah.heep.sax.de (J Wunsch) wrote: >As Naoki Hamada wrote: > >> Malloc(3) of 2.2-GAMMA writes error/warning messages to stderr. There >> is a daemon named jserver, which provides translations between >> Japanese characters, which does close stderr before opening its >> dictionary files. Sometimes jserver gets its dictionary file >> completely damaged by malloc's error/warning message. > >Hmm, i think it's a common misconception in Unix that everybody >blindly assumes that file descriptors 0, 1, and 2 refer to stdin, >stdout, and stderr respectively. OTOH, there's no opportunity for a >library module to check where stderr really goes (or whether it has >been closed). I bet malloc isn't the only library module that would >write there. > >Your safest bet is to never close these descriptors, but redirect >/dev/null on them. So, they remain `reserved'. Although not enforced by the kernel, file descriptors 0, 1 and 2 have a conventional use that should always be respected. If you don't want to see stderr output, open /dev/null as descriptor 2. This should work: fd = open("/dev/null", O_WRONLY); if (fd == -1) { perror("/dev/null"); exit(1); } if (fd != 2) { dup2(fd, 2); close(fd); } The test of fd != 2 is for the unlikely case that descriptor 2 is closed already. If the open of "/dev/null" fails, exiting is probably the kindest result; you've got some repair work to do. Some others responded that stderr and descriptor 2 are logically different and recommend using fileno(stderr) instead of 2. We of the old school all know that stdio is an upstart young library, and will never be allowed to use anything but descriptor 2 on Unix. :-) Feel free to use fileno(stderr) if the hairy revolutionaries have got to you. You might like to use STDERR_FILENO from and/or _PATH_DEVNULL from and/or err() instead of perror()/exit(), but these are upstart newbies too. BTW, the dup2() man page implies that newd will be closed even if it is equal to oldd. In reality, it is not, and (as far as I can remember) never was for any version of Unix. Similarly, newd is not closed if oldd is invalid. I'll update the man page as soon as I get a chance. Stephen.