(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                // Author: Mike Day (mdday@us.ibm.com)
 33                //
 34 h.sterling 1.1 // Modified By:  Jenny Yu, Hewlett-Packard Company (jenny_yu@hp.com)
 35                //       Yi Zhou, Hewlett-Packard Company (yi_zhou@hp.com)
 36                //       Marek Szermutzky, IBM (ddt6szer@de.ibm.com)
 37                //%/////////////////////////////////////////////////////////////////////////////
 38                #include <sys/types.h>
 39                #include <sys/stat.h>
 40                #if defined(PEGASUS_OS_HPUX)
 41                #include <sys/pstat.h>
 42                #endif
 43                #include <fcntl.h>
 44                #include <unistd.h>
 45                #include <Pegasus/Common/Signal.h>
 46 mike       1.11 #include <Pegasus/Common/AutoPtr.h>
 47 h.sterling 1.1  #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
 48                 #include <sys/ps.h>
 49                 #endif
 50 w.otsuka   1.4  #define MAX_WAIT_TIME 240
 51 h.sterling 1.1  #if defined(PEGASUS_OS_AIX)
 52                 extern "C" {
 53                 #include <procinfo.h>
 54                 extern int getprocs(struct procsinfo *, int, struct fdsinfo *, int,pid_t *,int);
 55                 #define PROCSIZE sizeof(struct procsinfo)
 56                 }
 57                 #endif
 58 mike       1.11.16.4 #include <Pegasus/ExecutorClient/ExecutorClient.h>
 59 mike       1.11.16.1 
 60 h.sterling 1.1       PEGASUS_USING_PEGASUS;
 61                      PEGASUS_USING_STD;
 62                      
 63                      Boolean handleSigUsr1 = false;
 64                      Boolean graveError = false;
 65                      
 66 r.kieninger 1.9       // the current pid is the server pid. After fork it is overwritten.
 67                       pid_t server_pid = getpid();
 68 h.sterling  1.3       Uint32 parentPid = 0;
 69 h.sterling  1.1       
 70                       void sigUsr1Handler(int s_n, PEGASUS_SIGINFO_T * s_info, void * sig)
 71                       {
 72                           handleSigUsr1 = true;
 73                       }
 74                       
 75                       void sigTermHandler(int s_n, PEGASUS_SIGINFO_T * s_info, void * sig)
 76                       {
 77                           graveError= handleSigUsr1=true;
 78                       } 
 79                       
 80                       
 81                       //constructor
 82                       ServerProcess::ServerProcess() {}
 83                       
 84                       //destructor
 85                       ServerProcess::~ServerProcess() {}
 86                       
 87                       // no-ops
 88                       void ServerProcess::cimserver_set_process(void* p) {}
 89                       void ServerProcess::cimserver_exitRC(int rc) {}
 90 h.sterling  1.1       int ServerProcess::cimserver_initialize(void) { return 1; }
 91 marek       1.10      
 92                       // for all OSes supporting signals provide a cimserver_wait function
 93                       // that waits to be awakened by signal PEGASUS_SIGTERM or PEGASUS_SIGHUP
 94                       #ifdef PEGASUS_HAS_SIGNALS
 95                       int ServerProcess::cimserver_wait(void)
 96                       {
 97                           int sig = -1;
 98                           sigset_t set;
 99                           sigemptyset(&set);
100                           sigaddset(&set, PEGASUS_SIGTERM);
101                           sigaddset(&set, PEGASUS_SIGHUP);
102                           errno = 0;
103                           do
104                           {
105                       #if defined(PEGASUS_OS_ZOS) || defined(PEGASUS_OS_SOLARIS)
106                               sig = sigwait(&set);
107                       #else // else for platforms = LINUX, HPUX, AIX
108                               sigwait(&set, &sig);
109                       #endif
110                           } while (errno == EINTR);
111                           return sig;
112 marek       1.10      }
113                       #else
114 h.sterling  1.1       int ServerProcess::cimserver_wait(void) { return 1; }
115 marek       1.10      #endif
116                       
117 h.sterling  1.1       String ServerProcess::getHome(void) { return String::EMPTY; }
118                       
119                       // daemon_init , RW Stevens, "Advance UNIX Programming"
120                       
121                       int ServerProcess::cimserver_fork(void) 
122 mike        1.11.16.1 {
123 h.sterling  1.1           getSigHandle()->registerHandler(PEGASUS_SIGUSR1, sigUsr1Handler);
124                           getSigHandle()->activate(PEGASUS_SIGUSR1);
125                           getSigHandle()->registerHandler(SIGTERM, sigTermHandler);
126                           getSigHandle()->activate(SIGTERM);
127                        
128                         pid_t pid;
129                         if( (pid = fork() ) < 0) 
130                         {
131                             getSigHandle()->deactivate(PEGASUS_SIGUSR1);
132                             getSigHandle()->deactivate(SIGTERM);
133                             return(-1);
134                         }
135                         else if (pid != 0)
136                         {
137                             //
138                             // parent wait for child
139                             // if there is a problem with signal, parent process terminates
140                             // when waitTime expires
141                             //
142                             Uint32 waitTime = MAX_WAIT_TIME;
143                       
144 h.sterling  1.1             while(!handleSigUsr1 && waitTime > 0)
145                             {
146                               sleep(1);
147                               waitTime--;
148                             }
149 w.otsuka    1.4             if( !handleSigUsr1 )
150                               {
151                               MessageLoaderParms parms("src.Service.ServerProcessUnix.CIMSERVER_START_TIMEOUT",
152                                 "The cimserver command timed out waiting for the CIM server to start.");
153                               PEGASUS_STD(cerr) << MessageLoader::getMessage(parms) << PEGASUS_STD(endl);
154                             }
155 h.sterling  1.1             exit(graveError);
156                         }
157 mike        1.11.16.1 
158 h.sterling  1.1         
159                         setsid();
160 marek       1.10        umask(S_IRWXG | S_IRWXO );
161                       
162                         // spawned daemon process doesn't need the old signal handlers of its parent
163                         getSigHandle()->deactivate(PEGASUS_SIGUSR1);
164                         getSigHandle()->deactivate(SIGTERM);
165 h.sterling  1.1       
166                         // get the pid of the cimserver process
167                         server_pid = getpid();
168 mike        1.11.16.1 
169                           // Ask the executor process to daemonize.
170                           ExecutorClient::daemonizeExecutor();
171                       
172 h.sterling  1.1         return(0);
173                       }
174                       
175                       long ServerProcess::get_server_pid()
176                       {
177                           return server_pid;
178                       }
179                       
180 h.sterling  1.3       void ServerProcess::set_parent_pid(int pid)
181                       {
182                           parentPid = pid;
183                       }
184                       
185                       
186 jim.wunderlich 1.5       #if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU) || defined(PEGASUS_OS_SOLARIS)
187 h.sterling     1.1       
188                          //===========================================================================
189                          //  NAME          : verify_process_name
190                          //  DESCRIPTION   : Opens the 'stat' file in the /proc/<pid> directory to 
191                          //                  verify that the process name is that of the cimserver.
192                          //===========================================================================
193                          int verify_process_name(char *directory, const char* server_process_name) 
194                          {
195                              static char filename[80];
196                              static char buffer[512];
197                              int fd, bytesRead;
198                          
199                              // generate the name of the stat file in the process's /proc directory,
200                              // and open it
201                              sprintf(filename, "%s/%s", directory, "stat");
202                              if ( (fd = open(filename, O_RDONLY, 0)) == -1 ) 
203                              {
204                                  return -1;
205                              }
206                          
207                              // read the contents
208 h.sterling     1.1           if ( (bytesRead = read( fd, buffer, (sizeof buffer) - 1 )) <= 0 ) 
209                              {
210                                  close(fd);
211                                  return -1;
212                              }
213                          
214                              // null terminate the file contents
215                              buffer[bytesRead] = 0;
216                          
217                              close(fd);
218                          
219                              // the process name is the second element of the file contents and
220                              // is surrounded by parentheses. 
221                              //
222                              // find the positions of the parentheses in the file contents
223                              char * open_paren;
224                              char * close_paren;
225                              
226                              open_paren = strchr (buffer, '(');
227                              close_paren = strchr (buffer, ')');
228                              if (open_paren == NULL || close_paren == NULL || close_paren < open_paren)
229 h.sterling     1.1           {
230                                  return -1;
231                              }
232                          
233                              // allocate memory for the result
234 kumpf          1.6           AutoArrayPtr<char> process_name(new char[close_paren - open_paren]);
235 h.sterling     1.1       
236                              // copy the process name into the result  
237 kumpf          1.6           strncpy (process_name.get(), open_paren + 1, close_paren - open_paren -1);
238 h.sterling     1.1       
239                              // strncpy doesn't NULL-terminate the result, so do it here
240 kumpf          1.6           process_name.get()[close_paren - open_paren -1] = '\0';
241 h.sterling     1.1       
242 kumpf          1.6           if (strcmp(process_name.get(), server_process_name) != 0)
243 h.sterling     1.1           {
244                                  return -1;
245                              }
246                          
247                              return 0;
248                          }
249                          
250                          //=============================================================================
251                          // NAME           : get_proc
252                          // DESCRIPTION    : get_proc() makes a stat() system call on the directory in
253                          //                  /proc with a name that matches the pid of the cimserver.
254                          //                  It returns 0 if it successfully located the process dir
255                          //                  and verified that the process name matches that of the
256                          //                  cimserver.  It returns -1 if it fails to open /proc, or
257                          //                  the cimserver process does not exist.
258                          //=============================================================================
259                          int ServerProcess::get_proc(int pid)
260                          {
261                            static char path[32];
262                            static struct stat stat_buff;
263                          
264 h.sterling     1.1         sprintf(path, "/proc/%d", pid);
265                            if (stat(path, &stat_buff) == -1)          // process stopped running
266                            {
267                              return -1;
268                            }
269                          
270                           // get the process name to make sure it is the cimserver process
271                          // ATTN: skip verify for Solaris
272 jim.wunderlich 1.5       #if !defined(PEGASUS_OS_SOLARIS)
273 h.sterling     1.1         if ((verify_process_name(path, getProcessName())) == -1)
274                            {
275                              return -1;
276                            }
277                          #endif
278                          
279                            //
280                            // Check to see if this command process has the same pid as the cimserver
281                            // daemon process pid stored in the cimserver_start.conf file.  Since the
282                            // command has the same name as the cimserver daemon process, this could
283                            // happen after a system reboot.  If the pids are the same, cimserver 
284                            // isn't really running.
285                            //
286                            Uint32 mypid = System::getPID();
287                            if ((mypid == (unsigned)pid) || ((unsigned)parentPid == (unsigned)pid))
288                            {
289                                return -1;
290                            }
291                            return 0;
292                          }
293                          #endif
294 h.sterling     1.1       
295                          #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
296                          Boolean isProcRunning(pid_t pid)
297                          {
298                              W_PSPROC buf;                                                              
299                              int token = 0;
300                              memset(&buf, 0x00, sizeof(buf));                                           
301                              buf.ps_conttyptr =(char *) malloc(buf.ps_conttylen =PS_CONTTYBLEN);        
302                              buf.ps_pathptr   =(char *) malloc(buf.ps_pathlen   =PS_PATHBLEN);          
303                              buf.ps_cmdptr    =(char *) malloc(buf.ps_cmdlen    =PS_CMDBLEN);
304                          
305                              token = w_getpsent(token, &buf, sizeof(buf));                              
306                              do {                                                                       
307                                  token = w_getpsent(token, &buf, sizeof(buf));                          
308                                  if (buf.ps_pid==pid) {
309                                      free(buf.ps_conttyptr);                                                    
310                                      free(buf.ps_pathptr);                                                      
311                                      free(buf.ps_cmdptr);
312                                      return true;
313                                  }
314                              } while(token>0);
315 h.sterling     1.1       
316                              free(buf.ps_conttyptr);                                                    
317                              free(buf.ps_pathptr);                                                      
318                              free(buf.ps_cmdptr);
319                              return false;
320                          }
321                          #endif
322                          
323                          #if defined(PEGASUS_OS_AIX)
324                          
325                          /////////////////////////////////////////////////////////////////////////////
326                          // NAME           : processdata
327                          // FUNCTION       : it calls subroutine getprocs() to get information
328                          //                  about all processes.
329                          /////////////////////////////////////////////////////////////////////////////
330                          // ARGUMENTS:
331                          //              cnt: the number of the processes in the returned table
332                          // RETURN VALUES:
333                          //              when it successfully gets the process table entries,
334                          //              an array of procsinfo structures filled with process table
335                          //              entries are returned. Otherewise, a null pointer is
336 h.sterling     1.1       //              returned.
337                          //  
338                          /////////////////////////////////////////////////////////////////////////////
339                          struct procsinfo *processdata(int *cnt)
340                          {
341                                  struct procsinfo *proctable=NULL, *rtnp=NULL;
342                                  int count=1048576, rtncnt, repeat=1, nextp=0;
343                          
344                                  *cnt=0;
345                                  while ( repeat && (rtncnt=getprocs(rtnp,PROCSIZE,0,0,&nextp, count))>0)
346                                  {
347                                          if (!rtnp)
348                                          {
349                                                  count=rtncnt;
350                                                  proctable=(struct procsinfo *) malloc((size_t) \
351                                                                  PROCSIZE*count);
352                                                  if (!proctable)
353                                                          return NULL;
354                                                  rtnp=proctable;
355                                                  nextp=0;
356                                          } else
357 h.sterling     1.1                       {
358                                                  *cnt+=rtncnt;
359                                                  if (rtncnt>=count)
360                                                  {
361                                                          proctable=(struct procsinfo *) realloc(\
362                                                                  (void*)proctable, (size_t)\
363                                                                  (PROCSIZE*(*cnt+count)));
364                                                          if (!proctable)
365                                                                  return NULL;
366                                                          rtnp=proctable+(*cnt);
367                                                  } else
368                                                          repeat=0;
369                                          } // end of if(!rtnp)
370                                  } //end of while
371                                  return proctable;
372                          }
373                          
374                          /////////////////////////////////////////////////////////////////////////////
375                          // NAME           : aixcimsrvrunning
376                          // FUNCTION       : it figures out if the cimserver process is running
377                          //                  by checking the process table entries in the array
378 h.sterling     1.1       //                  returned from processdata(). If the cimserver process is
379                          //                  running and its pid is the same as the pid in the file
380                          //                  getPIDFileName(), it will return 0, otherwise it will
381                          //                  return -1.
382                          /////////////////////////////////////////////////////////////////////////////
383                          // ARGUMENTS:
384                          //              pid: the process identifier saved in the file
385                          //                   getPIDFileName()
386                          // RETURN VALUES:
387                          //              0: successful
388                          //              -1: errors
389                          //  
390                          /////////////////////////////////////////////////////////////////////////////
391                                 
392 h.sterling     1.2       int aixcimsrvrunning(pid_t pid, const char* processName)
393 h.sterling     1.1       {
394                                  int i,count;
395                                  struct procsinfo *proctable;
396                          
397                                  proctable=processdata(&count);
398                                  if (proctable==NULL)
399                                          return -1;
400                                  for (i=0;i<count;i++)
401 h.sterling     1.2                       if (!strcmp(proctable[i].pi_comm, processName) && \
402 h.sterling     1.1                           proctable[i].pi_pid==pid)
403                                          {
404                                                  free(proctable);
405                                                  return 0;
406                                          }
407                          
408                                  free(proctable);
409                                  return -1;
410                          }
411                          #endif
412                          
413                          Boolean ServerProcess::isCIMServerRunning(void)
414                          {
415                            FILE *pid_file;
416                            pid_t pid = 0;
417                          
418                            // open the file containing the CIMServer process ID
419                            pid_file = fopen(getPIDFileName(), "r");
420                            if (!pid_file)
421                            {
422                                return false;
423 h.sterling     1.1         }
424                          
425                            // get the pid from the file
426                            fscanf(pid_file, "%d\n", &pid);
427                          
428                            fclose(pid_file);
429                          
430                            if (pid == 0)
431                            {
432                               return false;
433                            }
434                          
435                            //
436                            // check to see if cimserver process is alive
437                            //
438                          #if defined(PEGASUS_OS_HPUX)
439                            struct pst_status pstru;
440                          
441                            int ret_code;
442                            ret_code = pstat_getproc(&pstru, sizeof(struct pst_status), (size_t)0, pid);
443                          
444 h.sterling     1.1         if ( (ret_code != -1 ) && (strcmp(pstru.pst_ucomm, getProcessName())) == 0)
445                            {
446                                //
447                                // Check to see if this command process has the same pid as the 
448                                // cimserver daemon process pid stored in the cimserver_start.conf 
449                                // file.  Since the command has the same name as the cimserver daemon
450                                // process, this could happen after a system reboot.  If the pids are
451                                // the same, cimserver isn't really running.
452                                //
453                                Uint32 mypid = System::getPID();
454                                if ((mypid != pid) && (parentPid != pid))
455                                {
456                                    // cimserver is running
457                                    return true;
458                                }
459                            }
460                          #endif
461 jim.wunderlich 1.5       #if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU) || defined(PEGASUS_OS_SOLARIS)
462 h.sterling     1.1         if (get_proc(pid) != -1 )
463                            {
464                                // cimserver is running
465                                return true;
466                            }
467                          #endif
468                          #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
469                              return isProcRunning(pid);
470                          #endif
471                          #if defined(PEGASUS_OS_AIX)
472 h.sterling     1.2           if (aixcimsrvrunning(pid, getProcessName())!=-1)
473 h.sterling     1.1               return true;
474                          #endif
475 mike           1.11.16.2 
476 h.sterling     1.1         return false;
477                          }
478                          
479                          int ServerProcess::cimserver_kill(int id) 
480                          { 
481                            FILE *pid_file;
482                            pid_t pid = 0;
483                            
484                            // open the file containing the CIMServer process ID
485                            pid_file = fopen(getPIDFileName(), "r");
486                            if (!pid_file) 
487                            {
488                                return (-1);
489                            }
490                          
491                            // get the pid from the file
492                            fscanf(pid_file, "%d\n", &pid);
493                          
494                            fclose(pid_file);
495                          
496                            if (pid == 0)
497 h.sterling     1.1         {
498                               System::removeFile(getPIDFileName());
499                               return (-1);
500                            }
501                          
502                            //
503                            // kill the process if it is still alive
504                            //
505                          #if defined(PEGASUS_OS_HPUX)
506                            struct pst_status pstru;
507                          
508                            int ret_code;
509                            ret_code = pstat_getproc(&pstru, sizeof(struct pst_status), (size_t)0, pid);
510                          
511                            if ( (ret_code != -1 ) && (strcmp(pstru.pst_ucomm, getProcessName())) == 0)
512                            {
513                                // cimserver is running, kill the process
514                                kill(pid, SIGKILL);
515                            }
516                          #endif
517 jim.wunderlich 1.5       #if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU) || defined(PEGASUS_OS_SOLARIS)
518 h.sterling     1.1         if (get_proc(pid) != -1 )
519                            {
520                                kill(pid, SIGKILL);
521                            }
522                          #endif
523                          #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
524                            if (isProcRunning(pid)) {
525                                kill(pid, SIGKILL);
526                            }
527                          #endif
528                          #if defined(PEGASUS_OS_AIX)
529 h.sterling     1.2         if (!aixcimsrvrunning(pid, getProcessName()))
530 h.sterling     1.1               kill(pid,SIGKILL);
531                          #endif
532                            // remove the file
533                            System::removeFile(getPIDFileName());
534                            
535                            return(0);
536                          }
537                          
538                          // notify parent process to terminate so user knows that cimserver
539                          // is ready to serve CIM requests.
540                          void ServerProcess::notify_parent(int id)
541                          {
542                            pid_t ppid = getppid();
543                            if (id)
544                             kill(ppid, SIGTERM);
545                            else
546                             kill(ppid, PEGASUS_SIGUSR1); 
547                          }
548                          
549                          
550                          // Platform specific run
551 kumpf          1.7       int ServerProcess::platform_run(
552                              int argc,
553                              char** argv,
554                              Boolean shutdownOption,
555                              Boolean debugOutputOption)
556 h.sterling     1.1       {
557 kumpf          1.7           return cimserver_run(argc, argv, shutdownOption, debugOutputOption);
558 h.sterling     1.1       }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2