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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2