(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 mike        1.11.16.5 #if defined(PEGASUS_ENABLE_PRIVILEGE_SEPARATION)
124                       
125                           // Register to handle SIGUSR1:
126 h.sterling  1.1           getSigHandle()->registerHandler(PEGASUS_SIGUSR1, sigUsr1Handler);
127                           getSigHandle()->activate(PEGASUS_SIGUSR1);
128 mike        1.11.16.5 
129                           // Register to handle SIGTERM:
130 h.sterling  1.1           getSigHandle()->registerHandler(SIGTERM, sigTermHandler);
131                           getSigHandle()->activate(SIGTERM);
132                       
133 mike        1.11.16.5     // Set umask to use when creating files.
134                           umask(S_IRWXG | S_IRWXO);
135                       
136                           // Save process id.
137                           server_pid = getpid();
138                       
139                           // Ask the executor process to daemonize.
140                           ExecutorClient::daemonizeExecutor();
141                       
142                           return 0;
143                       
144                       #else /* !defined(PEGASUS_ENABLE_PRIVILEGE_SEPARATION) */
145                       
146                           getSigHandle()->registerHandler(PEGASUS_SIGUSR1, sigUsr1Handler);
147                           getSigHandle()->activate(PEGASUS_SIGUSR1);
148                           getSigHandle()->registerHandler(SIGTERM, sigTermHandler);
149                           getSigHandle()->activate(SIGTERM);
150                       
151                           pid_t pid;
152                       
153                           if ((pid = fork()) < 0)
154 mike        1.11.16.5     {
155                               getSigHandle()->deactivate(PEGASUS_SIGUSR1);
156                               getSigHandle()->deactivate(SIGTERM);
157                               return (-1);
158                           }
159                           else if (pid != 0)
160                           {
161                               // 
162                               // parent wait for child
163                               // if there is a problem with signal, parent process terminates
164                               // when waitTime expires
165                               // 
166                               Uint32 waitTime = MAX_WAIT_TIME;
167                       
168                               while (!handleSigUsr1 && waitTime > 0)
169 w.otsuka    1.4               {
170 mike        1.11.16.5             sleep(1);
171                                   waitTime--;
172                               }
173                               if (!handleSigUsr1)
174                               {
175                                   MessageLoaderParms parms(
176                                       "src.Service.ServerProcessUnix.CIMSERVER_START_TIMEOUT",
177                                       "The cimserver command timed out waiting for the "
178                                       "CIM server to start.");
179                                   PEGASUS_STD(cerr) << MessageLoader::
180                                       getMessage(parms) << PEGASUS_STD(endl);
181                               }
182                               exit(graveError);
183                           }
184 mike        1.11.16.1 
185 mike        1.11.16.5     setsid();
186 marek       1.10      
187 mike        1.11.16.5     umask(S_IRWXG | S_IRWXO);
188 h.sterling  1.1       
189 mike        1.11.16.5     // spawned daemon process doesn't need the old signal handlers of its
190                           // parent
191                           getSigHandle()->deactivate(PEGASUS_SIGUSR1);
192                           getSigHandle()->deactivate(SIGTERM);
193 mike        1.11.16.1 
194 mike        1.11.16.5     // get the pid of the cimserver process
195                           server_pid = getpid();
196 mike        1.11.16.1 
197 mike        1.11.16.5     return (0);
198                       
199                       #endif /* !defined(PEGASUS_ENABLE_PRIVILEGE_SEPARATION) */
200 h.sterling  1.1       }
201                       
202                       long ServerProcess::get_server_pid()
203                       {
204                           return server_pid;
205                       }
206                       
207 h.sterling  1.3       void ServerProcess::set_parent_pid(int pid)
208                       {
209                           parentPid = pid;
210                       }
211                       
212                       
213 jim.wunderlich 1.5       #if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU) || defined(PEGASUS_OS_SOLARIS)
214 h.sterling     1.1       
215                          //===========================================================================
216                          //  NAME          : verify_process_name
217                          //  DESCRIPTION   : Opens the 'stat' file in the /proc/<pid> directory to 
218                          //                  verify that the process name is that of the cimserver.
219                          //===========================================================================
220                          int verify_process_name(char *directory, const char* server_process_name) 
221                          {
222                              static char filename[80];
223                              static char buffer[512];
224                              int fd, bytesRead;
225                          
226                              // generate the name of the stat file in the process's /proc directory,
227                              // and open it
228                              sprintf(filename, "%s/%s", directory, "stat");
229                              if ( (fd = open(filename, O_RDONLY, 0)) == -1 ) 
230                              {
231                                  return -1;
232                              }
233                          
234                              // read the contents
235 h.sterling     1.1           if ( (bytesRead = read( fd, buffer, (sizeof buffer) - 1 )) <= 0 ) 
236                              {
237                                  close(fd);
238                                  return -1;
239                              }
240                          
241                              // null terminate the file contents
242                              buffer[bytesRead] = 0;
243                          
244                              close(fd);
245                          
246                              // the process name is the second element of the file contents and
247                              // is surrounded by parentheses. 
248                              //
249                              // find the positions of the parentheses in the file contents
250                              char * open_paren;
251                              char * close_paren;
252                              
253                              open_paren = strchr (buffer, '(');
254                              close_paren = strchr (buffer, ')');
255                              if (open_paren == NULL || close_paren == NULL || close_paren < open_paren)
256 h.sterling     1.1           {
257                                  return -1;
258                              }
259                          
260                              // allocate memory for the result
261 kumpf          1.6           AutoArrayPtr<char> process_name(new char[close_paren - open_paren]);
262 h.sterling     1.1       
263                              // copy the process name into the result  
264 kumpf          1.6           strncpy (process_name.get(), open_paren + 1, close_paren - open_paren -1);
265 h.sterling     1.1       
266                              // strncpy doesn't NULL-terminate the result, so do it here
267 kumpf          1.6           process_name.get()[close_paren - open_paren -1] = '\0';
268 h.sterling     1.1       
269 kumpf          1.6           if (strcmp(process_name.get(), server_process_name) != 0)
270 h.sterling     1.1           {
271                                  return -1;
272                              }
273                          
274                              return 0;
275                          }
276                          
277                          //=============================================================================
278                          // NAME           : get_proc
279                          // DESCRIPTION    : get_proc() makes a stat() system call on the directory in
280                          //                  /proc with a name that matches the pid of the cimserver.
281                          //                  It returns 0 if it successfully located the process dir
282                          //                  and verified that the process name matches that of the
283                          //                  cimserver.  It returns -1 if it fails to open /proc, or
284                          //                  the cimserver process does not exist.
285                          //=============================================================================
286                          int ServerProcess::get_proc(int pid)
287                          {
288                            static char path[32];
289                            static struct stat stat_buff;
290                          
291 h.sterling     1.1         sprintf(path, "/proc/%d", pid);
292                            if (stat(path, &stat_buff) == -1)          // process stopped running
293                            {
294                              return -1;
295                            }
296                          
297                           // get the process name to make sure it is the cimserver process
298                          // ATTN: skip verify for Solaris
299 jim.wunderlich 1.5       #if !defined(PEGASUS_OS_SOLARIS)
300 h.sterling     1.1         if ((verify_process_name(path, getProcessName())) == -1)
301                            {
302                              return -1;
303                            }
304                          #endif
305                          
306                            //
307                            // Check to see if this command process has the same pid as the cimserver
308                            // daemon process pid stored in the cimserver_start.conf file.  Since the
309                            // command has the same name as the cimserver daemon process, this could
310                            // happen after a system reboot.  If the pids are the same, cimserver 
311                            // isn't really running.
312                            //
313                            Uint32 mypid = System::getPID();
314                            if ((mypid == (unsigned)pid) || ((unsigned)parentPid == (unsigned)pid))
315                            {
316                                return -1;
317                            }
318                            return 0;
319                          }
320                          #endif
321 h.sterling     1.1       
322                          #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
323                          Boolean isProcRunning(pid_t pid)
324                          {
325                              W_PSPROC buf;                                                              
326                              int token = 0;
327                              memset(&buf, 0x00, sizeof(buf));                                           
328                              buf.ps_conttyptr =(char *) malloc(buf.ps_conttylen =PS_CONTTYBLEN);        
329                              buf.ps_pathptr   =(char *) malloc(buf.ps_pathlen   =PS_PATHBLEN);          
330                              buf.ps_cmdptr    =(char *) malloc(buf.ps_cmdlen    =PS_CMDBLEN);
331                          
332                              token = w_getpsent(token, &buf, sizeof(buf));                              
333                              do {                                                                       
334                                  token = w_getpsent(token, &buf, sizeof(buf));                          
335                                  if (buf.ps_pid==pid) {
336                                      free(buf.ps_conttyptr);                                                    
337                                      free(buf.ps_pathptr);                                                      
338                                      free(buf.ps_cmdptr);
339                                      return true;
340                                  }
341                              } while(token>0);
342 h.sterling     1.1       
343                              free(buf.ps_conttyptr);                                                    
344                              free(buf.ps_pathptr);                                                      
345                              free(buf.ps_cmdptr);
346                              return false;
347                          }
348                          #endif
349                          
350                          #if defined(PEGASUS_OS_AIX)
351                          
352                          /////////////////////////////////////////////////////////////////////////////
353                          // NAME           : processdata
354                          // FUNCTION       : it calls subroutine getprocs() to get information
355                          //                  about all processes.
356                          /////////////////////////////////////////////////////////////////////////////
357                          // ARGUMENTS:
358                          //              cnt: the number of the processes in the returned table
359                          // RETURN VALUES:
360                          //              when it successfully gets the process table entries,
361                          //              an array of procsinfo structures filled with process table
362                          //              entries are returned. Otherewise, a null pointer is
363 h.sterling     1.1       //              returned.
364                          //  
365                          /////////////////////////////////////////////////////////////////////////////
366                          struct procsinfo *processdata(int *cnt)
367                          {
368                                  struct procsinfo *proctable=NULL, *rtnp=NULL;
369                                  int count=1048576, rtncnt, repeat=1, nextp=0;
370                          
371                                  *cnt=0;
372                                  while ( repeat && (rtncnt=getprocs(rtnp,PROCSIZE,0,0,&nextp, count))>0)
373                                  {
374                                          if (!rtnp)
375                                          {
376                                                  count=rtncnt;
377                                                  proctable=(struct procsinfo *) malloc((size_t) \
378                                                                  PROCSIZE*count);
379                                                  if (!proctable)
380                                                          return NULL;
381                                                  rtnp=proctable;
382                                                  nextp=0;
383                                          } else
384 h.sterling     1.1                       {
385                                                  *cnt+=rtncnt;
386                                                  if (rtncnt>=count)
387                                                  {
388                                                          proctable=(struct procsinfo *) realloc(\
389                                                                  (void*)proctable, (size_t)\
390                                                                  (PROCSIZE*(*cnt+count)));
391                                                          if (!proctable)
392                                                                  return NULL;
393                                                          rtnp=proctable+(*cnt);
394                                                  } else
395                                                          repeat=0;
396                                          } // end of if(!rtnp)
397                                  } //end of while
398                                  return proctable;
399                          }
400                          
401                          /////////////////////////////////////////////////////////////////////////////
402                          // NAME           : aixcimsrvrunning
403                          // FUNCTION       : it figures out if the cimserver process is running
404                          //                  by checking the process table entries in the array
405 h.sterling     1.1       //                  returned from processdata(). If the cimserver process is
406                          //                  running and its pid is the same as the pid in the file
407                          //                  getPIDFileName(), it will return 0, otherwise it will
408                          //                  return -1.
409                          /////////////////////////////////////////////////////////////////////////////
410                          // ARGUMENTS:
411                          //              pid: the process identifier saved in the file
412                          //                   getPIDFileName()
413                          // RETURN VALUES:
414                          //              0: successful
415                          //              -1: errors
416                          //  
417                          /////////////////////////////////////////////////////////////////////////////
418                                 
419 h.sterling     1.2       int aixcimsrvrunning(pid_t pid, const char* processName)
420 h.sterling     1.1       {
421                                  int i,count;
422                                  struct procsinfo *proctable;
423                          
424                                  proctable=processdata(&count);
425                                  if (proctable==NULL)
426                                          return -1;
427                                  for (i=0;i<count;i++)
428 h.sterling     1.2                       if (!strcmp(proctable[i].pi_comm, processName) && \
429 h.sterling     1.1                           proctable[i].pi_pid==pid)
430                                          {
431                                                  free(proctable);
432                                                  return 0;
433                                          }
434                          
435                                  free(proctable);
436                                  return -1;
437                          }
438                          #endif
439                          
440                          Boolean ServerProcess::isCIMServerRunning(void)
441                          {
442                            FILE *pid_file;
443                            pid_t pid = 0;
444                          
445                            // open the file containing the CIMServer process ID
446                            pid_file = fopen(getPIDFileName(), "r");
447                            if (!pid_file)
448                            {
449                                return false;
450 h.sterling     1.1         }
451                          
452                            // get the pid from the file
453                            fscanf(pid_file, "%d\n", &pid);
454                          
455                            fclose(pid_file);
456                          
457                            if (pid == 0)
458                            {
459                               return false;
460                            }
461                          
462                            //
463                            // check to see if cimserver process is alive
464                            //
465                          #if defined(PEGASUS_OS_HPUX)
466                            struct pst_status pstru;
467                          
468                            int ret_code;
469                            ret_code = pstat_getproc(&pstru, sizeof(struct pst_status), (size_t)0, pid);
470                          
471 h.sterling     1.1         if ( (ret_code != -1 ) && (strcmp(pstru.pst_ucomm, getProcessName())) == 0)
472                            {
473                                //
474                                // Check to see if this command process has the same pid as the 
475                                // cimserver daemon process pid stored in the cimserver_start.conf 
476                                // file.  Since the command has the same name as the cimserver daemon
477                                // process, this could happen after a system reboot.  If the pids are
478                                // the same, cimserver isn't really running.
479                                //
480                                Uint32 mypid = System::getPID();
481                                if ((mypid != pid) && (parentPid != pid))
482                                {
483                                    // cimserver is running
484                                    return true;
485                                }
486                            }
487                          #endif
488 jim.wunderlich 1.5       #if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU) || defined(PEGASUS_OS_SOLARIS)
489 h.sterling     1.1         if (get_proc(pid) != -1 )
490                            {
491                                // cimserver is running
492                                return true;
493                            }
494                          #endif
495                          #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
496                              return isProcRunning(pid);
497                          #endif
498                          #if defined(PEGASUS_OS_AIX)
499 h.sterling     1.2           if (aixcimsrvrunning(pid, getProcessName())!=-1)
500 h.sterling     1.1               return true;
501                          #endif
502 mike           1.11.16.2 
503 h.sterling     1.1         return false;
504                          }
505                          
506                          int ServerProcess::cimserver_kill(int id) 
507                          { 
508                            FILE *pid_file;
509                            pid_t pid = 0;
510                            
511                            // open the file containing the CIMServer process ID
512                            pid_file = fopen(getPIDFileName(), "r");
513                            if (!pid_file) 
514                            {
515                                return (-1);
516                            }
517                          
518                            // get the pid from the file
519                            fscanf(pid_file, "%d\n", &pid);
520                          
521                            fclose(pid_file);
522                          
523                            if (pid == 0)
524 h.sterling     1.1         {
525                               System::removeFile(getPIDFileName());
526                               return (-1);
527                            }
528                          
529                            //
530                            // kill the process if it is still alive
531                            //
532                          #if defined(PEGASUS_OS_HPUX)
533                            struct pst_status pstru;
534                          
535                            int ret_code;
536                            ret_code = pstat_getproc(&pstru, sizeof(struct pst_status), (size_t)0, pid);
537                          
538                            if ( (ret_code != -1 ) && (strcmp(pstru.pst_ucomm, getProcessName())) == 0)
539                            {
540                                // cimserver is running, kill the process
541                                kill(pid, SIGKILL);
542                            }
543                          #endif
544 jim.wunderlich 1.5       #if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU) || defined(PEGASUS_OS_SOLARIS)
545 h.sterling     1.1         if (get_proc(pid) != -1 )
546                            {
547                                kill(pid, SIGKILL);
548                            }
549                          #endif
550                          #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
551                            if (isProcRunning(pid)) {
552                                kill(pid, SIGKILL);
553                            }
554                          #endif
555                          #if defined(PEGASUS_OS_AIX)
556 h.sterling     1.2         if (!aixcimsrvrunning(pid, getProcessName()))
557 h.sterling     1.1               kill(pid,SIGKILL);
558                          #endif
559                            // remove the file
560                            System::removeFile(getPIDFileName());
561                            
562                            return(0);
563                          }
564                          
565                          // notify parent process to terminate so user knows that cimserver
566                          // is ready to serve CIM requests.
567                          void ServerProcess::notify_parent(int id)
568                          {
569                            pid_t ppid = getppid();
570                            if (id)
571                             kill(ppid, SIGTERM);
572                            else
573                             kill(ppid, PEGASUS_SIGUSR1); 
574                          }
575                          
576                          
577                          // Platform specific run
578 kumpf          1.7       int ServerProcess::platform_run(
579                              int argc,
580                              char** argv,
581                              Boolean shutdownOption,
582                              Boolean debugOutputOption)
583 h.sterling     1.1       {
584 kumpf          1.7           return cimserver_run(argc, argv, shutdownOption, debugOutputOption);
585 h.sterling     1.1       }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2