Make sure standard file descriptors are open before starting properly.
authormdw <mdw>
Wed, 15 Oct 2003 09:27:06 +0000 (09:27 +0000)
committermdw <mdw>
Wed, 15 Oct 2003 09:27:06 +0000 (09:27 +0000)
src/become.c

index 60cb1f3..7c7c2aa 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
 /* -*-c-*-
  *
- * $Id: become.c,v 1.23 2003/10/12 10:00:06 mdw Exp $
+ * $Id: become.c,v 1.24 2003/10/15 09:27:06 mdw Exp $
  *
  * Main code for `become'
  *
  *
  * Main code for `become'
  *
@@ -29,6 +29,9 @@
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: become.c,v $
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: become.c,v $
+ * Revision 1.24  2003/10/15 09:27:06  mdw
+ * Make sure standard file descriptors are open before starting properly.
+ *
  * Revision 1.23  2003/10/12 10:00:06  mdw
  * Fix for daemon mode.  Oops.
  *
  * Revision 1.23  2003/10/12 10:00:06  mdw
  * Fix for daemon mode.  Oops.
  *
 #include <pwd.h>
 #include <syslog.h>
 #include <unistd.h>
 #include <pwd.h>
 #include <syslog.h>
 #include <unistd.h>
+#include <fcntl.h>
 
 extern char **environ;
 
 
 extern char **environ;
 
@@ -558,6 +562,17 @@ int main(int argc, char *argv[])
   if (getuid() != geteuid())
     flags |= f_setuid;
 
   if (getuid() != geteuid())
     flags |= f_setuid;
 
+  /* --- Make sure standard file descriptors are open --- */
+
+  {
+    int fd;
+    do {
+      if ((fd = open("/dev/null", O_RDWR)) < 0)
+       die(1, "couldn't open /dev/null: %s", strerror(errno));
+    } while (fd <= STDERR_FILENO);
+    close(fd);
+  }
+
   /* --- Read the environment into a hashtable --- */
 
   {
   /* --- Read the environment into a hashtable --- */
 
   {
@@ -589,7 +604,7 @@ int main(int argc, char *argv[])
 
       /* --- Group style options --- */
 
 
       /* --- Group style options --- */
 
-      { "group",       gFlag_argReq,   0,      'g' },
+      { "group",       OPTF_ARGREQ,    0,      'g' },
 #ifdef HAVE_SETGROUPS
       { "keep-groups", 0,              0,      'k' },
       { "merge-groups",        0,              0,      'm' },
 #ifdef HAVE_SETGROUPS
       { "keep-groups", 0,              0,      'k' },
       { "merge-groups",        0,              0,      'm' },
@@ -598,23 +613,23 @@ int main(int argc, char *argv[])
 
       /* --- Command to run options --- */
 
 
       /* --- Command to run options --- */
 
-      { "command",     gFlag_argReq,   0,      'c' },
+      { "command",     OPTF_ARGREQ,    0,      'c' },
 
       /* --- Server options --- */
 
 #ifndef NONETWORK
       { "daemon",      0,              0,      'd' },
       { "nofork",      0,              0,      'n' },
 
       /* --- Server options --- */
 
 #ifndef NONETWORK
       { "daemon",      0,              0,      'd' },
       { "nofork",      0,              0,      'n' },
-      { "port",                gFlag_argReq,   0,      'p' },
-      { "config-file", gFlag_argReq,   0,      'f' },
+      { "port",                OPTF_ARGREQ,    0,      'p' },
+      { "config-file", OPTF_ARGREQ,    0,      'f' },
 #endif
 
       /* --- Tracing options --- */
 
 #ifndef NTRACE
 #endif
 
       /* --- Tracing options --- */
 
 #ifndef NTRACE
-      { "impersonate", gFlag_argReq,   0,      'I' },
-      { "trace",       gFlag_argOpt,   0,      'T' },
-      { "trace-level", gFlag_argOpt,   0,      'L' },
+      { "impersonate", OPTF_ARGREQ,    0,      'I' },
+      { "trace",       OPTF_ARGOPT,    0,      'T' },
+      { "trace-level", OPTF_ARGOPT,    0,      'L' },
 #endif
 
       { 0,             0,              0,      0 }
 #endif
 
       { 0,             0,              0,      0 }