Changeset 1549

Show
Ignore:
Timestamp:
06/14/08 11:57:19 (3 months ago)
Author:
alo
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • cherokee/trunk/ChangeLog

    r1547 r1549  
     12008-06-14  Alvaro Lopez Ortega  <alvaro@alobbs.com> 
     2 
     3        * cherokee/main_guardian.c: Improves PID file management. Now it 
     4        cleans it up before exiting. 
     5 
    162008-06-13  Alvaro Lopez Ortega  <alvaro@alobbs.com> 
    27 
  • cherokee/trunk/cherokee/main_guardian.c

    r1547 r1549  
    2727#include <unistd.h> 
    2828#include <sys/wait.h> 
     29#include <sys/stat.h> 
    2930#include <errno.h> 
    3031#include "server.h" 
     
    4142static pid_t              pid; 
    4243 
     44static void 
     45pid_file_save (const char *pid_file, int pid) 
     46{ 
     47        FILE *file; 
     48        char  tmp[10]; 
     49 
     50        file = fopen (pid_file, "w+"); 
     51        if (file == NULL) { 
     52                PRINT_MSG ("Cannot write PID file '%s'\n", pid_file); 
     53                return; 
     54        } 
     55 
     56        snprintf (tmp, sizeof(tmp), "%d\n", pid); 
     57        fwrite (tmp, 1, strlen(tmp), file); 
     58        fclose (file); 
     59} 
     60 
     61static void 
     62pid_file_clean (const char *pid_file) 
     63{ 
     64        struct stat info; 
     65 
     66        if (lstat (pid_file, &info) != 0)  
     67                return; 
     68        if (! S_ISREG(info.st_mode)) 
     69                return; 
     70        if (info.st_uid != getuid()) 
     71                return; 
     72        if (info.st_size > sizeof("65535\r\n")) 
     73                return; 
     74 
     75        unlink (pid_file); 
     76} 
    4377 
    4478static ret_t 
     
    68102} 
    69103 
    70  
    71104static void  
    72105guardian_signals_handler (int sig, siginfo_t *si, void *context)  
     
    93126                kill (pid, SIGTERM); 
    94127                process_wait (pid); 
     128                pid_file_clean (PID_FILE); 
    95129                exit(0); 
    96130 
     
    164198} 
    165199 
    166  
    167 static void 
    168 save_pid_file (int pid) 
    169 { 
    170         FILE *file; 
    171         char  tmp[10]; 
    172  
    173         UNUSED(pid); 
    174  
    175         file = fopen (PID_FILE, "w+"); 
    176         if (file == NULL) { 
    177                 PRINT_MSG ("Cannot write PID file '%s'\n", PID_FILE); 
    178                 return; 
    179         } 
    180  
    181         snprintf (tmp, sizeof(tmp), "%d\n", getpid()); 
    182         fwrite (tmp, 1, strlen(tmp), file); 
    183         fclose (file); 
    184 } 
    185  
    186200static cherokee_boolean_t 
    187201is_single_execution (int argc, char *argv[]) 
     
    209223        single_time = is_single_execution (argc, argv); 
    210224 
     225        if (! single_time) 
     226                pid_file_save (PID_FILE, getpid()); 
     227 
    211228        do { 
    212229                pid = process_launch (CHEROKEE_SRV_PATH, argc, argv); 
     
    216233                } 
    217234 
    218                 if (! single_time)  
    219                         save_pid_file(pid); 
    220  
    221                 ret = process_wait (pid); 
    222                  
     235                ret = process_wait (pid);                
    223236                if (single_time) 
    224237                        break; 
     
    229242        } while (! exit_guardian); 
    230243 
     244        pid_file_clean (PID_FILE); 
    231245        return 0; 
    232246}