【问题标题】:Stale PID file after reboot重启后过时的 PID 文件
【发布时间】:2013-03-22 11:28:59
【问题描述】:

重启后如何处理陈旧的 pidfile?我想我不能相信文件中的数字,因为另一个进程可能已经用相同的 PID 取代了它。

我可以/应该确保 pidfile 不会在重新启动后继续存在吗?当具有该 pid 的进程正在运行时,检测过时的 pidfile 的正确方法是什么,该文件可能会或可能不会从先前的引导中遗留下来?

【问题讨论】:

    标签: linux pid


    【解决方案1】:

    通常,pidfile 被写入/var/run//run/ (在许多系统上,有一个从/var/run/run 的符号链接,所以它们是相同的)。请参阅Filesystem Hierarchy Standard 了解更多信息。 /run/ 目录应该在启动时尽早清除(即因为它被挂载为tmpfs),所以它不会在重新启动后存活。另请参阅Linux Standard Base 4.1 规范。

    因此您不应该关心过时的 pidfile。这不应该发生,如果发生,可能是因为系统管理员把事情搞砸了。如果该 pidfile 已经存在,我会退出并显示某种错误消息。

    【讨论】:

      【解决方案2】:

      如果这是您自己的守护程序,您可以通过扫描 /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(如果存在) .

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-27
        • 1970-01-01
        • 1970-01-01
        • 2011-01-09
        • 2017-09-07
        • 2020-03-24
        • 2015-02-22
        • 1970-01-01
        相关资源
        最近更新 更多