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

  1 karl  1.23 //%2003////////////////////////////////////////////////////////////////////////
  2 mike  1.5  //
  3 karl  1.23 // 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 mike  1.5  //
  8            // Permission is hereby granted, free of charge, to any person obtaining a copy
  9 kumpf 1.8  // of this software and associated documentation files (the "Software"), to
 10            // deal in the Software without restriction, including without limitation the
 11            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 12 mike  1.5  // sell copies of the Software, and to permit persons to whom the Software is
 13            // furnished to do so, subject to the following conditions:
 14            // 
 15 kumpf 1.8  // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 16 mike  1.5  // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 17            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 18 kumpf 1.8  // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 19            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 20            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 21 mike  1.5  // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 22            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 23            //
 24            //==============================================================================
 25            //
 26            // Author: Mike Day (mdday@us.ibm.com)
 27            //
 28 kumpf 1.10 // Modified By:  Jenny Yu, Hewlett-Packard Company (jenny_yu@hp.com)
 29            //		 Yi Zhou, Hewlett-Packard Company (yi_zhou@hp.com)
 30 marek 1.19 //       Marek Szermutzky, IBM (ddt6szer@de.ibm.com)
 31 mike  1.5  //%/////////////////////////////////////////////////////////////////////////////
 32            #include <sys/types.h>
 33            #include <sys/stat.h>
 34 kumpf 1.7  #if defined(PEGASUS_OS_HPUX)
 35 kumpf 1.6  #include <sys/pstat.h>
 36 kumpf 1.7  #endif
 37 mike  1.5  #include <fcntl.h>
 38            #include <unistd.h>
 39 kumpf 1.16 #include <Pegasus/Common/Signal.h>
 40 marek 1.19 #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
 41            #include <sys/ps.h>
 42            #endif
 43 kumpf 1.16 #define MAX_WAIT_TIME 25
 44 kumpf 1.12 
 45 mike  1.5  PEGASUS_USING_PEGASUS;
 46            PEGASUS_USING_STD;
 47 kumpf 1.10 
 48 kumpf 1.16 Boolean handleSigUsr1 = false;
 49 konrad.r 1.20 Boolean graveError = false;
 50 mike     1.5  void cim_server_service(int argc, char **argv ) { return; }  
 51               
 52 kumpf    1.6  pid_t server_pid;
 53               
 54 kumpf    1.16 void sigUsr1Handler(int s_n, PEGASUS_SIGINFO_T * s_info, void * sig)
 55 kumpf    1.10 {
 56 kumpf    1.16     handleSigUsr1 = true;
 57 kumpf    1.10 }
 58               
 59 konrad.r 1.20 void sigTermHandler(int s_n, PEGASUS_SIGINFO_T * s_info, void * sig)
 60               {
 61                   graveError= handleSigUsr1=true;
 62               } 
 63 mike     1.5  // daemon_init , RW Stevens, "Advance UNIX Programming"
 64               
 65               int cimserver_fork(void) 
 66               { 
 67 kumpf    1.16 getSigHandle()->registerHandler(PEGASUS_SIGUSR1, sigUsr1Handler);
 68               getSigHandle()->activate(PEGASUS_SIGUSR1);
 69 konrad.r 1.20 getSigHandle()->registerHandler(SIGTERM, sigTermHandler);
 70               getSigHandle()->activate(SIGTERM);
 71 kumpf    1.10  
 72 mike     1.5    pid_t pid;
 73                 if( (pid = fork() ) < 0) 
 74 kumpf    1.16   {
 75                     getSigHandle()->deactivate(PEGASUS_SIGUSR1);
 76 konrad.r 1.20       getSigHandle()->deactivate(SIGTERM);
 77 kumpf    1.16       return(-1);
 78                 }
 79 mike     1.5    else if (pid != 0)
 80 kumpf    1.10   {
 81 kumpf    1.16       //
 82                     // parent wait for child
 83                     // if there is a problem with signal, parent process terminates
 84                     // when waitTime expires
 85                     //
 86                     Uint32 waitTime = MAX_WAIT_TIME;
 87 kumpf    1.12 
 88 kumpf    1.16       while(!handleSigUsr1 && waitTime > 0)
 89                     {
 90 kumpf    1.12 	    sleep(1);
 91 kumpf    1.16 	    waitTime--;
 92                     }
 93 konrad.r 1.20       exit(graveError);
 94 kumpf    1.10   }
 95 mike     1.5    
 96                 setsid();
 97                 umask(0);
 98 kumpf    1.6  
 99                 // get the pid of the cimserver process
100                 server_pid = getpid();
101                 return(0);
102 kumpf    1.9  }
103               
104 kumpf    1.18 #if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU)
105               
106               //===========================================================================
107               //  NAME          : verify_process_name
108               //  DESCRIPTION   : Opens the 'stat' file in the /proc/<pid> directory to 
109               //                  verify that the process name is that of the cimserver.
110               //===========================================================================
111               int verify_process_name(char *directory) 
112               {
113                   static char filename[80];
114                   static char buffer[512];
115                   int fd, bytesRead;
116               
117                   // generate the name of the stat file in the process's /proc directory,
118                   // and open it
119                   sprintf(filename, "%s/%s", directory, "stat");
120                   if ( (fd = open(filename, O_RDONLY, 0)) == -1 ) 
121                   {
122                       return -1;
123                   }
124               
125 kumpf    1.18     // read the contents
126                   if ( (bytesRead = read( fd, buffer, (sizeof buffer) - 1 )) <= 0 ) 
127                   {
128                       close(fd);
129                       return -1;
130                   }
131               
132                   // null terminate the file contents
133                   buffer[bytesRead] = 0;
134               
135                   close(fd);
136               
137                   // the process name is the second element of the file contents and
138                   // is surrounded by parentheses. 
139                   //
140                   // find the positions of the parentheses in the file contents
141                   char * open_paren;
142                   char * close_paren;
143                   
144                   open_paren = strchr (buffer, '(');
145                   close_paren = strchr (buffer, ')');
146 kumpf    1.18     if (open_paren == NULL || close_paren == NULL || close_paren < open_paren)
147                   {
148                       return -1;
149                   }
150               
151                   // allocate memory for the result
152                   char * process_name;
153                   process_name = (char*) malloc(close_paren - open_paren - 1);
154               
155                   // copy the process name into the result  
156                   strncpy (process_name, open_paren + 1, close_paren - open_paren -1);
157               
158                   // strncpy doesn't NULL-terminate the result, so do it here
159                   process_name[close_paren - open_paren -1] = '\0';
160               
161                   if (strcmp(process_name, "cimserver") != 0)
162                   {
163                       return -1;
164                   }
165               
166                   return 0;
167 kumpf    1.18 }
168               
169               //=============================================================================
170               // NAME           : get_proc
171               // DESCRIPTION    : get_proc() makes a stat() system call on the directory in
172               //                  /proc with a name that matches the pid of the cimserver.
173               //                  It returns 0 if it successfully located the process dir
174               //                  and verified that the process name matches that of the
175               //                  cimserver.  It returns -1 if it fails to open /proc, or
176               //                  the cimserver process does not exist.
177               //=============================================================================
178               int get_proc(int pid)
179               {
180                 static char path[32];
181                 static struct stat stat_buff;
182               
183                 sprintf(path, "/proc/%d", pid);
184                 if (stat(path, &stat_buff) == -1)          // process stopped running
185                 {
186                   return -1;
187                 }
188 kumpf    1.18 
189                 // get the process name to make sure it is the cimserver process
190                 if ((verify_process_name(path)) == -1)
191                 {
192                   return -1;
193                 }
194               
195 kumpf    1.21   //
196                 // Check to see if this command process has the same pid as the cimserver
197                 // daemon process pid stored in the cimserver_start.conf file.  Since the
198                 // command has the same name as the cimserver daemon process, this could
199                 // happen after a system reboot.  If the pids are the same, cimserver 
200                 // isn't really running.
201                 //
202                 pid_t mypid = getpid();
203                 if (mypid == pid)
204                 {
205                     return -1;
206                 }
207 kumpf    1.18   return 0;
208               }
209               #endif
210               
211 marek    1.19 #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
212               Boolean isProcRunning(pid_t pid)
213               {
214                   W_PSPROC buf;                                                              
215                   int token = 0;
216                   memset(&buf, 0x00, sizeof(buf));                                           
217                   buf.ps_conttyptr =(char *) malloc(buf.ps_conttylen =PS_CONTTYBLEN);        
218                   buf.ps_pathptr   =(char *) malloc(buf.ps_pathlen   =PS_PATHBLEN);          
219                   buf.ps_cmdptr    =(char *) malloc(buf.ps_cmdlen    =PS_CMDBLEN);
220               
221                   token = w_getpsent(token, &buf, sizeof(buf));                              
222                   do {                                                                       
223                       token = w_getpsent(token, &buf, sizeof(buf));                          
224                       if (buf.ps_pid==pid) {
225                           free(buf.ps_conttyptr);                                                    
226                           free(buf.ps_pathptr);                                                      
227                           free(buf.ps_cmdptr);
228                           return true;
229                       }
230                   } while(token>0);
231               
232 marek    1.19     free(buf.ps_conttyptr);                                                    
233                   free(buf.ps_pathptr);                                                      
234                   free(buf.ps_cmdptr);
235                   return false;
236               }
237               #endif
238 kumpf    1.18 
239 kumpf    1.9  Boolean isCIMServerRunning(void)
240               {
241                 FILE *pid_file;
242                 pid_t pid = 0;
243               
244                 // open the file containing the CIMServer process ID
245 marek    1.19   pid_file = fopen(CIMSERVER_START_FILE, "r");
246 kumpf    1.9    if (!pid_file)
247                 {
248                     return false;
249                 }
250               
251                 // get the pid from the file
252 kumpf    1.17   fscanf(pid_file, "%d\n", &pid);
253               
254                 fclose(pid_file);
255 kumpf    1.9  
256                 if (pid == 0)
257                 {
258                    return false;
259                 }
260               
261                 //
262                 // check to see if cimserver process is alive
263                 //
264               #if defined(PEGASUS_OS_HPUX)
265                 struct pst_status pstru;
266               
267 kumpf    1.17   int ret_code;
268                 ret_code = pstat_getproc(&pstru, sizeof(struct pst_status), (size_t)0, pid);
269               
270                 if ( (ret_code != -1 ) && (strcmp(pstru.pst_ucomm, "cimserver")) == 0)
271 kumpf    1.9    {
272 kumpf    1.21       //
273                     // Check to see if this command process has the same pid as the 
274                     // cimserver daemon process pid stored in the cimserver_start.conf 
275                     // file.  Since the command has the same name as the cimserver daemon
276                     // process, this could happen after a system reboot.  If the pids are
277                     // the same, cimserver isn't really running.
278                     //
279                     pid_t mypid = getpid();
280                     if (mypid != pid)
281                     {
282                         // cimserver is running
283                         return true;
284                     }
285 kumpf    1.9    }
286               #endif
287 kumpf    1.18 #if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU)
288                 if (get_proc(pid) != -1 )
289                 {
290                     // cimserver is running
291                     return true;
292                 }
293               #endif
294 marek    1.19 #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
295                   return isProcRunning(pid);
296               #endif
297 kumpf    1.9    return false;
298 kumpf    1.6  }
299               
300               int cimserver_kill(void) 
301               { 
302                 FILE *pid_file;
303                 pid_t pid = 0;
304                 
305                 // open the file containing the CIMServer process ID
306 kumpf    1.18   pid_file = fopen(CIMSERVER_START_FILE, "r");
307 kumpf    1.6    if (!pid_file) 
308                 {
309                     return (-1);
310                 }
311               
312                 // get the pid from the file
313 kumpf    1.17   fscanf(pid_file, "%d\n", &pid);
314               
315                 fclose(pid_file);
316 kumpf    1.6  
317                 if (pid == 0)
318                 {
319 kumpf    1.18      System::removeFile(CIMSERVER_START_FILE);
320 kumpf    1.6       return (-1);
321                 }
322               
323                 //
324                 // kill the process if it is still alive
325                 //
326 kumpf    1.7  #if defined(PEGASUS_OS_HPUX)
327 kumpf    1.6    struct pst_status pstru;
328               
329 kumpf    1.17   int ret_code;
330                 ret_code = pstat_getproc(&pstru, sizeof(struct pst_status), (size_t)0, pid);
331               
332                 if ( (ret_code != -1 ) && (strcmp(pstru.pst_ucomm, "cimserver")) == 0)
333 kumpf    1.6    {
334 kumpf    1.17       // cimserver is running, kill the process
335 kumpf    1.6        kill(pid, SIGKILL);
336                 }
337 kumpf    1.7  #endif
338 kumpf    1.18 #if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU)
339                 if (get_proc(pid) != -1 )
340                 {
341                     kill(pid, SIGKILL);
342                 }
343               #endif
344 marek    1.19 #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
345                 if (isProcRunning(pid)) {
346                     kill(pid, SIGKILL);
347                 }
348               #endif
349 kumpf    1.6    // remove the file
350 kumpf    1.18   System::removeFile(CIMSERVER_START_FILE);
351 kumpf    1.6    
352 mike     1.5    return(0);
353               }
354               
355 kumpf    1.10 // notify parent process to terminate so user knows that cimserver
356               // is ready to serve CIM requests.
357 konrad.r 1.20 void notify_parent(int id)
358 kumpf    1.10 {
359                 pid_t ppid = getppid();
360 konrad.r 1.20   if (id)
361                  kill(ppid, SIGTERM);
362                 else
363                  kill(ppid, PEGASUS_SIGUSR1); 
364 kumpf    1.10 }
365 s.hills  1.22 
366               
367               // Platform specific run
368               int platform_run( int argc, char** argv, Boolean shutdownOption )
369               {
370               	return cimserver_run( argc, argv, shutdownOption );
371               }
372               
373               void cimserver_set( CIMServer* s )
374               {
375               }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2