Date: Wed, 26 Feb 1997 13:11:14 +1000 (EST) From: Stephen McKay <syssgm@devetir.qld.gov.au> To: j@uriah.heep.sax.de (J Wunsch), Naoki Hamada <nao@sbl.cl.nec.co.jp> Cc: freebsd-hackers@freebsd.org, syssgm@devetir.qld.gov.au Subject: Re: MALLOC(3) writes to stderr... Message-ID: <199702260311.NAA24882@ogre.devetir.qld.gov.au>
next in thread | raw e-mail | index | archive | help
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 <unistd.h> and/or _PATH_DEVNULL from <paths.h> 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.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199702260311.NAA24882>