(file) Return to ServerProcessUnix.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Service

  1 karl  1.8 //%2006////////////////////////////////////////////////////////////////////////
  2 h.sterling 1.1 //
  3                // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4                // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5                // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6                // IBM Corp.; EMC Corporation, The Open Group.
  7                // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8                // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9                // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10                // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 karl       1.8 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12                // EMC Corporation; Symantec Corporation; The Open Group.
 13 h.sterling 1.1 //
 14                // Permission is hereby granted, free of charge, to any person obtaining a copy
 15                // of this software and associated documentation files (the "Software"), to
 16                // deal in the Software without restriction, including without limitation the
 17                // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 18                // sell copies of the Software, and to permit persons to whom the Software is
 19                // furnished to do so, subject to the following conditions:
 20                // 
 21                // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22                // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 23                // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 24                // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 25                // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 26                // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 27                // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 28                // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 29                //
 30                //==============================================================================
 31                //
 32                //%/////////////////////////////////////////////////////////////////////////////
 33 kumpf      1.14 
 34 h.sterling 1.1  #include <fcntl.h>
 35                 #include <unistd.h>
 36 kumpf      1.14 
 37 h.sterling 1.1  #include <Pegasus/Common/Signal.h>
 38 kumpf      1.16 #include <Pegasus/Common/Executor.h>
 39 kumpf      1.14 
 40 w.otsuka   1.4  #define MAX_WAIT_TIME 240
 41 h.sterling 1.1  
 42                 PEGASUS_USING_PEGASUS;
 43                 PEGASUS_USING_STD;
 44                 
 45                 Boolean handleSigUsr1 = false;
 46                 Boolean graveError = false;
 47                 
 48                 void sigUsr1Handler(int s_n, PEGASUS_SIGINFO_T * s_info, void * sig)
 49                 {
 50                     handleSigUsr1 = true;
 51                 }
 52                 
 53                 void sigTermHandler(int s_n, PEGASUS_SIGINFO_T * s_info, void * sig)
 54                 {
 55                     graveError= handleSigUsr1=true;
 56 kumpf      1.17 }
 57 h.sterling 1.1  
 58                 
 59                 //constructor
 60                 ServerProcess::ServerProcess() {}
 61                 
 62                 //destructor
 63                 ServerProcess::~ServerProcess() {}
 64                 
 65                 // no-ops
 66                 void ServerProcess::cimserver_set_process(void* p) {}
 67                 void ServerProcess::cimserver_exitRC(int rc) {}
 68 kumpf      1.17 int ServerProcess::cimserver_initialize() { return 1; }
 69 marek      1.10 
 70                 // for all OSes supporting signals provide a cimserver_wait function
 71                 // that waits to be awakened by signal PEGASUS_SIGTERM or PEGASUS_SIGHUP
 72                 #ifdef PEGASUS_HAS_SIGNALS
 73 kumpf      1.17 int ServerProcess::cimserver_wait()
 74 marek      1.10 {
 75                     int sig = -1;
 76                     sigset_t set;
 77                     sigemptyset(&set);
 78                     sigaddset(&set, PEGASUS_SIGTERM);
 79                     sigaddset(&set, PEGASUS_SIGHUP);
 80                     errno = 0;
 81                     do
 82                     {
 83                 #if defined(PEGASUS_OS_ZOS) || defined(PEGASUS_OS_SOLARIS)
 84                         sig = sigwait(&set);
 85                 #else // else for platforms = LINUX, HPUX, AIX
 86                         sigwait(&set, &sig);
 87                 #endif
 88                     } while (errno == EINTR);
 89                     return sig;
 90                 }
 91                 #else
 92 kumpf      1.17 int ServerProcess::cimserver_wait() { return 1; }
 93 marek      1.10 #endif
 94                 
 95 kumpf      1.17 String ServerProcess::getHome() { return String::EMPTY; }
 96 h.sterling 1.1  
 97                 // daemon_init , RW Stevens, "Advance UNIX Programming"
 98                 
 99 kumpf      1.17 int ServerProcess::cimserver_fork()
100                 {
101 kumpf      1.15     getSigHandle()->registerHandler(SIGTERM, sigTermHandler);
102                     getSigHandle()->activate(SIGTERM);
103                     umask(S_IRWXG | S_IRWXO);
104                 
105 kumpf      1.16     if (Executor::detectExecutor() == 0)
106                     {
107                         // We don't need to fork if we're running with Privilege Separation
108 kumpf      1.17.8.1         setsid();
109 kumpf      1.16             return 0;
110                         }
111 kumpf      1.15     
112 h.sterling 1.1          getSigHandle()->registerHandler(PEGASUS_SIGUSR1, sigUsr1Handler);
113                         getSigHandle()->activate(PEGASUS_SIGUSR1);
114 kumpf      1.17     
115                         pid_t pid;
116                         if( (pid = fork() ) < 0)
117                         {
118                             getSigHandle()->deactivate(PEGASUS_SIGUSR1);
119                             getSigHandle()->deactivate(SIGTERM);
120                             return -1;
121                         }
122                         else if (pid != 0)
123                         {
124                             //
125                             // parent wait for child
126                             // if there is a problem with signal, parent process terminates
127                             // when waitTime expires
128                             //
129                             Uint32 waitTime = MAX_WAIT_TIME;
130                     
131                             while (!handleSigUsr1 && waitTime > 0)
132 w.otsuka   1.4              {
133 kumpf      1.17                 sleep(1);
134                                 waitTime--;
135                             }
136                     
137                             if (!handleSigUsr1)
138                             {
139                                 MessageLoaderParms parms(
140                                     "src.Service.ServerProcessUnix.CIMSERVER_START_TIMEOUT",
141                                     "The cimserver command timed out waiting for the CIM server "
142                                         "to start.");
143                                 PEGASUS_STD(cerr) << MessageLoader::getMessage(parms) <<
144                                     PEGASUS_STD(endl);
145                             }
146                             exit(graveError);
147                         }
148                     
149                         setsid();
150                         umask(S_IRWXG | S_IRWXO);
151                     
152                         // spawned daemon process doesn't need the old signal handlers of its parent
153                         getSigHandle()->deactivate(PEGASUS_SIGUSR1);
154 kumpf      1.17         getSigHandle()->deactivate(SIGTERM);
155 h.sterling 1.1      
156 kumpf      1.17         return 0;
157 h.sterling 1.1      }
158                     
159                     
160                     // notify parent process to terminate so user knows that cimserver
161                     // is ready to serve CIM requests.
162                     void ServerProcess::notify_parent(int id)
163                     {
164 kumpf      1.17         pid_t ppid = getppid();
165                         if (id)
166                             kill(ppid, SIGTERM);
167                         else
168                             kill(ppid, PEGASUS_SIGUSR1);
169 h.sterling 1.1      }
170                     
171                     
172                     // Platform specific run
173 kumpf      1.7      int ServerProcess::platform_run(
174                         int argc,
175                         char** argv,
176                         Boolean shutdownOption,
177                         Boolean debugOutputOption)
178 h.sterling 1.1      {
179 kumpf      1.7          return cimserver_run(argc, argv, shutdownOption, debugOutputOption);
180 h.sterling 1.1      }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2