myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [2987] Added possibility to specify a list of descript


From: Giuseppe Scrivano
Subject: [myserver-commit] [2987] Added possibility to specify a list of descriptors to close after the fork .
Date: Sat, 31 Jan 2009 17:55:29 +0000

Revision: 2987
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2987
Author:   gscrivano
Date:     2009-01-31 17:55:29 +0000 (Sat, 31 Jan 2009)

Log Message:
-----------
Added possibility to specify a list of descriptors to close after the fork.

Modified Paths:
--------------
    trunk/myserver/include/base/process/process.h
    trunk/myserver/src/base/process/fork_server.cpp
    trunk/myserver/src/base/process/process.cpp
    trunk/myserver/tests/test_fork_server.cpp

Modified: trunk/myserver/include/base/process/process.h
===================================================================
--- trunk/myserver/include/base/process/process.h       2009-01-31 17:02:57 UTC 
(rev 2986)
+++ trunk/myserver/include/base/process/process.h       2009-01-31 17:55:29 UTC 
(rev 2987)
@@ -33,6 +33,13 @@
  */
 struct StartProcInfo
 {
+  StartProcInfo ()
+  {
+    gid = uid = 0;
+    envString = NULL;
+    handlesToClose = NULL;
+  }
+
        /*! STDIN file for new process.  */
        FileHandle stdIn;       
        
@@ -57,11 +64,9 @@
 
        void *envString;
 
-  StartProcInfo ()
-  {
-    gid = uid = 0;
-    envString = NULL;
-  }
+  /*! Pointer to a NULL terminated array of 
+   *  file pointers to close.  */
+  FileHandle *handlesToClose; 
 };
 
 class Process

Modified: trunk/myserver/src/base/process/fork_server.cpp
===================================================================
--- trunk/myserver/src/base/process/fork_server.cpp     2009-01-31 17:02:57 UTC 
(rev 2986)
+++ trunk/myserver/src/base/process/fork_server.cpp     2009-01-31 17:55:29 UTC 
(rev 2987)
@@ -211,6 +211,7 @@
   spi.cmd.assign (exec);
   spi.arg.assign (arg);
   spi.cwd.assign (cwd);
+  spi.handlesToClose = (FileHandle[]){serverSock->getFirstHandle (), NULL};
 
   Process pi;
   int pid = pi.exec (&spi, false);

Modified: trunk/myserver/src/base/process/process.cpp
===================================================================
--- trunk/myserver/src/base/process/process.cpp 2009-01-31 17:02:57 UTC (rev 
2986)
+++ trunk/myserver/src/base/process/process.cpp 2009-01-31 17:55:29 UTC (rev 
2987)
@@ -1,6 +1,6 @@
 /*
 MyServer
-Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
+Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008, 2009 Free Software 
Foundation, Inc.
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
@@ -218,8 +218,8 @@
 
       if (spi->uid)
         Process::setuid (spi->uid);
-      
 
+
       if (generateArgList (args, spi->cmd.c_str (), spi->arg))
         exit (1);
 
@@ -229,8 +229,7 @@
       // change to working dir
       if (spi->cwd.length ())
         {
-          ret = chdir ((const char*)(spi->cwd.c_str()));
-          if (ret == -1)
+          if (chdir ((const char*)(spi->cwd.c_str())) == -1)
             exit (1);
         }
  
@@ -241,37 +240,46 @@
         spi->stdError = (FileHandle)open ("/dev/null", O_WRONLY);
 
       // map stdio to files
-      ret = close(0); // close stdin
-      if (ret == -1)
+      if (close(0) == -1) // close stdin
         exit (1);
 
-
       if (spi->stdIn != -1)
         {
-          ret = dup2(spi->stdIn, 0);
-          if (ret == -1)
+          if (dup2 (spi->stdIn, 0) == -1)
             exit (1);
-          ret = close(spi->stdIn);
+          
+          if (close (spi->stdIn) == -1)
+            exit (1);
         }
 
-      if (ret == -1)
+      if (close (1) == -1) // close stdout
         exit (1);
-      ret = close (1); // close stdout
-      if (ret == -1)
+
+      if (dup2 (spi->stdOut, 1) == -1)
         exit (1);
-    ret = dup2(spi->stdOut, 1);
-    if (ret == -1)
-      exit (1);
-    ret = close (spi->stdOut);
-    if (ret == -1)
-      exit (1);
-    //close(2); // close stderr
-    //dup2((int)spi->stdError, 2);
-    // Run the script
-    ret = execve ((const char*)args[0], 
-                 (char* const*)args, (char* const*) envp);
-    exit (0);
 
+      if (close (2) == -1) // close stderr
+        exit (1);
+
+      if (dup2 (spi->stdError, 1) == -1)
+        exit (1);
+      
+      if (spi->handlesToClose)
+        {
+          FileHandle* h = spi->handlesToClose;
+          while (*h)
+            {
+              if (close (*h) == -1)
+                exit (1);
+              h++;
+            }
+        }
+      // Run the script
+      execve ((const char*)args[0], 
+              (char* const*)args, (char* const*) envp);
+
+    exit (1);
+
   }
 
   if (waitEnd)

Modified: trunk/myserver/tests/test_fork_server.cpp
===================================================================
--- trunk/myserver/tests/test_fork_server.cpp   2009-01-31 17:02:57 UTC (rev 
2986)
+++ trunk/myserver/tests/test_fork_server.cpp   2009-01-31 17:55:29 UTC (rev 
2987)
@@ -1,6 +1,6 @@
 /*
  MyServer
- Copyright (C) 2008 Free Software Foundation, Inc.
+ Copyright (C) 2008, 2009 Free Software Foundation, Inc.
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or






reply via email to

[Prev in Thread] Current Thread [Next in Thread]