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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2