2013-03-22 51 views
3

重啓後如何處理陳舊的pid文件?我認爲我不能相信文件中的數字,因爲另一個進程可能已經採用了相同的PID。重啓後陳舊的PID文件

可以/我應該確保pidfiles在重新啓動後無法存活嗎?什麼時候檢測一個陳舊的pid文件的正確方法,可能會或可能不會從以前的引導中遺留下來,當一個使用該pid的進程正在運行時?

回答

2

通常,PID文件被寫入/var/run//run/(在許多系統沒有從/var/run/run一個符號,使他們都是一樣的)。有關更多信息,請參閱Filesystem Hierarchy Standard。並且該/run/目錄應該在啓動時被提前清除(即因爲它被掛載爲tmpfs),所以它不會在重新啓動後存活。另見Linux Standard Base 4.1規範。

因此,你不應該關心陳舊的pidfiles。這不應該發生,如果這樣做可能是因爲系統管理員糟糕的東西搞砸了。如果該pidfile已經存在,我會退出某種錯誤消息。

1

如果這是您自己的守護進程,您可以通過掃描/proc/PID/cmdine或發送一個信號來處理該PID來檢測陳舊的PID文件。

對於掃描/proc的一個簡單的例子,我告訴我的解決方案:

#define PROC_BASE "/proc" 

#include <stdio.h>  // printf, fopen, ... 
#include <unistd.h>  // getpid 
#include <stdio.h>  // perror 
#include <sys/types.h> // opendir 
#include <dirent.h>  // opendir 
#include <sys/stat.h> // stat 
#include <fcntl.h>  // fcntl 
#include <stdlib.h>  // exit 
#include <string.h>  // memset 

/** 
* read process name from /proc/PID/cmdline 
* @param pid - PID of interesting process 
* @return filename or NULL if not found 
*  don't use this function twice for different names without copying 
*  its returning by strdup, because `name` contains in static array 
*/ 
char *readname(pid_t pid){ 
    static char name[256]; 
    char *pp = name, byte, path[256]; 
    FILE *file; 
    int cntr = 0; 
    size_t sz; 
    snprintf (path, 255, PROC_BASE "/%d/cmdline", pid); 
    file = fopen(path, "r"); 
    if(!file) return NULL; // there's no such file 
    do{ // read basename 
     sz = fread(&byte, 1, 1, file); 
     if(sz != 1) break; 
     if(byte != '/') *pp++ = byte; 
     else{ 
      pp = name; 
      cntr = 0; 
     } 
    }while(byte && cntr++ < 255); 
    name[cntr] = 0; 
    fclose(file); 
    return name; 
} 

void iffound_default(pid_t pid){ 
    fprintf(stderr, "\nFound running process (pid=%d), exit.\n", pid); 
    exit(0); 
} 

/** 
* check wether there is a same running process 
* exit if there is a running process or error 
* Checking have 3 steps: 
*  1) lock executable file 
*  2) check pidfile (if you run a copy?) 
*  3) check /proc for executables with the same name (no/wrong pidfile) 
* @param argv - argument of main() or NULL for non-locking, call this function before getopt() 
* @param pidfilename - name of pidfile or NULL if none 
* @param iffound - action to run if file found or NULL for exit(0) 
*/ 
void check4running(char **argv, char *pidfilename, void (*iffound)(pid_t pid)){ 
    DIR *dir; 
    FILE *pidfile, *fself; 
    struct dirent *de; 
    struct stat s_buf; 
    pid_t pid = 0, self; 
    struct flock fl; 
    char *name, *myname; 
    if(!iffound) iffound = iffound_default; 
    if(argv){ // block self 
     fself = fopen(argv[0], "r"); // open self binary to lock 
     memset(&fl, 0, sizeof(struct flock)); 
     fl.l_type = F_WRLCK; 
     if(fcntl(fileno(fself), F_GETLK, &fl) == -1){ // check locking 
      perror("fcntl"); 
      exit(1); 
     } 
     if(fl.l_type != F_UNLCK){ // file is locking - exit 
      printf("Found locker, PID = %d!\n", fl.l_pid); 
      exit(1); 
     } 
     fl.l_type = F_RDLCK; 
     if(fcntl(fileno(fself), F_SETLKW, &fl) == -1){ 
      perror("fcntl"); 
      exit(1); 
     } 
    } 
    self = getpid(); // get self PID 
    if(!(dir = opendir(PROC_BASE))){ // open /proc directory 
     perror(PROC_BASE); 
     exit(1); 
    } 
    if(!(name = readname(self))){ // error reading self name 
     perror("Can't read self name"); 
     exit(1); 
    } 
    myname = strdup(name); 
    if(pidfilename && stat(pidfilename, &s_buf) == 0){ // pidfile exists 
     pidfile = fopen(pidfilename, "r"); 
     if(pidfile){ 
      fscanf(pidfile, "%d", &pid); // read PID of (possibly) running process 
      fclose(pidfile); 
      if((name = readname(pid)) && strncmp(name, myname, 255) == 0) 
       iffound(pid); 
     } 
    } 
    // There is no pidfile or it consists a wrong record 
    while((de = readdir(dir))){ // scan /proc 
     if(!(pid = (pid_t)atoi(de->d_name)) || pid == self) // pass non-PID files and self 
      continue; 
     if((name = readname(pid)) && strncmp(name, myname, 255) == 0) 
      iffound(pid); 
    } 
    closedir(dir); 
    if(pidfilename){ 
     pidfile = fopen(pidfilename, "w"); 
     fprintf(pidfile, "%d\n", self); // write self PID to pidfile 
     fclose(pidfile); 
    } 
} 

如果你運行功能check4running,你就可以保證你的守護進程是單:第一阻擋被鎖定自我可執行的,如果這是不可能的,它掃描/proc/並檢查pidfile(如果存在)。