(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 karl       1.21 #if defined(PEGASUS_OS_ZOS)
 84 marek      1.10         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     umask(S_IRWXG | S_IRWXO);
102                 
103 kumpf      1.16     if (Executor::detectExecutor() == 0)
104                     {
105                         // We don't need to fork if we're running with Privilege Separation
106                         return 0;
107                     }
108 kumpf      1.15 
109 kumpf      1.20     getSigHandle()->registerHandler(SIGTERM, sigTermHandler);
110                     getSigHandle()->activate(SIGTERM);
111 h.sterling 1.1      getSigHandle()->registerHandler(PEGASUS_SIGUSR1, sigUsr1Handler);
112                     getSigHandle()->activate(PEGASUS_SIGUSR1);
113 kumpf      1.17 
114                     pid_t pid;
115                     if( (pid = fork() ) < 0)
116                     {
117                         getSigHandle()->deactivate(PEGASUS_SIGUSR1);
118                         getSigHandle()->deactivate(SIGTERM);
119                         return -1;
120                     }
121                     else if (pid != 0)
122                     {
123                         //
124                         // parent wait for child
125                         // if there is a problem with signal, parent process terminates
126                         // when waitTime expires
127                         //
128                         Uint32 waitTime = MAX_WAIT_TIME;
129                 
130                         while (!handleSigUsr1 && waitTime > 0)
131 w.otsuka   1.4          {
132 kumpf      1.17             sleep(1);
133                             waitTime--;
134                         }
135                 
136                         if (!handleSigUsr1)
137                         {
138                             MessageLoaderParms parms(
139                                 "src.Service.ServerProcessUnix.CIMSERVER_START_TIMEOUT",
140                                 "The cimserver command timed out waiting for the CIM server "
141                                     "to start.");
142                             PEGASUS_STD(cerr) << MessageLoader::getMessage(parms) <<
143                                 PEGASUS_STD(endl);
144                         }
145                         exit(graveError);
146                     }
147                 
148                     setsid();
149                     umask(S_IRWXG | S_IRWXO);
150                 
151                     // spawned daemon process doesn't need the old signal handlers of its parent
152                     getSigHandle()->deactivate(PEGASUS_SIGUSR1);
153 kumpf      1.17     getSigHandle()->deactivate(SIGTERM);
154 h.sterling 1.1  
155 kumpf      1.17     return 0;
156 h.sterling 1.1  }
157                 
158                 
159                 // notify parent process to terminate so user knows that cimserver
160                 // is ready to serve CIM requests.
161                 void ServerProcess::notify_parent(int id)
162                 {
163 kumpf      1.17     pid_t ppid = getppid();
164                     if (id)
165 kumpf      1.19     {
166 kumpf      1.17         kill(ppid, SIGTERM);
167 kumpf      1.19     }
168 kumpf      1.17     else
169 kumpf      1.19     {
170                         if (Executor::detectExecutor() == 0)
171                         {
172 kumpf      1.20             Executor::daemonizeExecutor();
173 kumpf      1.19         }
174                         else
175                         {
176                             kill(ppid, PEGASUS_SIGUSR1);
177                         }
178                     }
179 h.sterling 1.1  }
180                 
181                 
182                 // Platform specific run
183 kumpf      1.7  int ServerProcess::platform_run(
184                     int argc,
185                     char** argv,
186                     Boolean shutdownOption,
187                     Boolean debugOutputOption)
188 h.sterling 1.1  {
189 kumpf      1.7      return cimserver_run(argc, argv, shutdownOption, debugOutputOption);
190 h.sterling 1.1  }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2