- if ((fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666)) < 0) { /* stdout */
- report(stderr, "open %s (%s)\n", logfile, strerror(errno));
- return(PS_IOERR);
- }
+ if ((logfd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666)) < 0) { /* stdout */
+ report(stderr, "cannot open %s: %s\n", logfile, strerror(errno));
+ return PS_IOERR;
+ }
+ } else
+ logfd = 0; /* else use /dev/null */
+
+ /* Close any/all open file descriptors */
+#if defined(HAVE_GETDTABLESIZE)
+ fd = getdtablesize() - 1;
+#elif defined(NOFILE)
+ fd = NOFILE - 1;
+#else /* make an educated guess */
+ fd = 1023;
+#endif
+ while (fd >= 1) {
+ if (fd != logfd)
+ close(fd); /* not checking this should be safe, no writes */
+ -- fd;