root/lib/common/pid.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. pcmk__pid_active
  2. pcmk__read_pidfile
  3. pcmk__pidfile_matches
  4. pcmk__lock_pidfile

   1 /*
   2  * Copyright 2004-2020 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #ifndef _GNU_SOURCE
  13 #  define _GNU_SOURCE
  14 #endif
  15 
  16 #include <stdio.h>
  17 #include <string.h>
  18 #include <sys/stat.h>
  19 
  20 #include <crm/crm.h>
  21 
  22 int
  23 pcmk__pid_active(pid_t pid, const char *daemon)
     /* [previous][next][first][last][top][bottom][index][help] */
  24 {
  25     static pid_t last_asked_pid = 0;  /* log spam prevention */
  26 #if SUPPORT_PROCFS
  27     static int have_proc_pid = 0;
  28 #else
  29     static int have_proc_pid = -1;
  30 #endif
  31     int rc = 0;
  32     bool no_name_check = ((daemon == NULL) || (have_proc_pid == -1));
  33 
  34     if (have_proc_pid == 0) {
  35         /* evaluation of /proc/PID/exe applicability via self-introspection */
  36         char proc_path[PATH_MAX], exe_path[PATH_MAX];
  37         snprintf(proc_path, sizeof(proc_path), "/proc/%lld/exe",
  38                  (long long) getpid());
  39         have_proc_pid = 1;
  40         if (readlink(proc_path, exe_path, sizeof(exe_path) - 1) < 0) {
  41             have_proc_pid = -1;
  42         }
  43     }
  44 
  45     if (pid <= 0) {
  46         return EINVAL;
  47     }
  48 
  49     rc = kill(pid, 0);
  50     if ((rc < 0) && (errno == ESRCH)) {
  51         return ESRCH;  /* no such PID detected */
  52 
  53     } else if ((rc < 0) && no_name_check) {
  54         rc = errno;
  55         if (last_asked_pid != pid) {
  56             crm_info("Cannot examine PID %lld: %s",
  57                      (long long) pid, strerror(errno));
  58             last_asked_pid = pid;
  59         }
  60         return rc; /* errno != ESRCH */
  61 
  62     } else if ((rc == 0) && no_name_check) {
  63         return pcmk_rc_ok; /* kill as the only indicator, cannot double check */
  64 
  65     } else if (daemon != NULL) {
  66         /* make sure PID hasn't been reused by another process
  67            XXX: might still be just a zombie, which could confuse decisions */
  68         bool checked_through_kill = (rc == 0);
  69         char proc_path[PATH_MAX], exe_path[PATH_MAX], myexe_path[PATH_MAX];
  70         snprintf(proc_path, sizeof(proc_path), "/proc/%lld/exe",
  71                  (long long) pid);
  72 
  73         rc = readlink(proc_path, exe_path, sizeof(exe_path) - 1);
  74         if (rc < 0) {
  75             if (last_asked_pid != pid) {
  76                 crm_err("Could not read from %s: %s " CRM_XS " errno=%d",
  77                         proc_path, strerror(errno), errno);
  78                 last_asked_pid = pid;
  79             }
  80             if ((errno == EACCES) && checked_through_kill) {
  81                 // Trust kill result, can't double-check via path
  82                 return pcmk_rc_ok;
  83             } else if (errno == EACCES) {
  84                 return EACCES;
  85             } else {
  86                 return ESRCH;  /* most likely errno == ENOENT */
  87             }
  88         }
  89         exe_path[rc] = '\0';
  90 
  91         if (daemon[0] != '/') {
  92             rc = snprintf(myexe_path, sizeof(myexe_path), CRM_DAEMON_DIR"/%s",
  93                           daemon);
  94         } else {
  95             rc = snprintf(myexe_path, sizeof(myexe_path), "%s", daemon);
  96         }
  97 
  98         if (rc > 0 && rc < sizeof(myexe_path) && !strcmp(exe_path, myexe_path)) {
  99             return pcmk_rc_ok;
 100         }
 101     }
 102 
 103     return ESRCH;
 104 }
 105 
 106 #define LOCKSTRLEN      11
 107 
 108 /*!
 109  * \internal
 110  * \brief Read a process ID from a file
 111  *
 112  * \param[in]  filename  Process ID file to read
 113  * \param[out] pid       Where to put PID that was read
 114  *
 115  * \return Standard Pacemaker return code
 116  */
 117 int
 118 pcmk__read_pidfile(const char *filename, pid_t *pid)
     /* [previous][next][first][last][top][bottom][index][help] */
 119 {
 120     int fd;
 121     struct stat sbuf;
 122     int rc = pcmk_rc_unknown_format;
 123     long long pid_read = 0;
 124     char buf[LOCKSTRLEN + 1];
 125 
 126     CRM_CHECK((filename != NULL) && (pid != NULL), return EINVAL);
 127 
 128     fd = open(filename, O_RDONLY);
 129     if (fd < 0) {
 130         return errno;
 131     }
 132 
 133     if ((fstat(fd, &sbuf) >= 0) && (sbuf.st_size < LOCKSTRLEN)) {
 134         sleep(2);           /* if someone was about to create one,
 135                              * give'm a sec to do so
 136                              */
 137     }
 138 
 139     if (read(fd, buf, sizeof(buf)) < 1) {
 140         rc = errno;
 141         goto bail;
 142     }
 143 
 144     if (sscanf(buf, "%lld", &pid_read) > 0) {
 145         if (pid_read <= 0) {
 146             rc = ESRCH;
 147         } else {
 148             rc = pcmk_rc_ok;
 149             *pid = (pid_t) pid_read;
 150             crm_trace("Read pid %lld from %s", pid_read, filename);
 151         }
 152     }
 153 
 154   bail:
 155     close(fd);
 156     return rc;
 157 }
 158 
 159 /*!
 160  * \internal
 161  * \brief Check whether a process from a PID file matches expected values
 162  *
 163  * \param[in]  filename       Path of PID file
 164  * \param[in]  expected_pid   If positive, compare to this PID
 165  * \param[in]  expected_name  If not NULL, the PID from the PID file is valid
 166  *                            only if it is active as a process with this name
 167  * \param[out] pid            If not NULL, store PID found in PID file here
 168  *
 169  * \return Standard Pacemaker return code
 170  */
 171 int
 172 pcmk__pidfile_matches(const char *filename, pid_t expected_pid,
     /* [previous][next][first][last][top][bottom][index][help] */
 173                       const char *expected_name, pid_t *pid)
 174 {
 175     pid_t pidfile_pid = 0;
 176     int rc = pcmk__read_pidfile(filename, &pidfile_pid);
 177 
 178     if (pid) {
 179         *pid = pidfile_pid;
 180     }
 181 
 182     if (rc != pcmk_rc_ok) {
 183         // Error reading PID file or invalid contents
 184         unlink(filename);
 185         rc = ENOENT;
 186 
 187     } else if ((expected_pid > 0) && (pidfile_pid == expected_pid)) {
 188         // PID in file matches what was expected
 189         rc = pcmk_rc_ok;
 190 
 191     } else if (pcmk__pid_active(pidfile_pid, expected_name) == ESRCH) {
 192         // Contains a stale value
 193         unlink(filename);
 194         rc = ENOENT;
 195 
 196     } else if ((expected_pid > 0) && (pidfile_pid != expected_pid)) {
 197         // Locked by existing process
 198         rc = EEXIST;
 199     }
 200 
 201     return rc;
 202 }
 203 
 204 /*!
 205  * \internal
 206  * \brief Create a PID file for the current process (if not already existent)
 207  *
 208  * \param[in] filename   Name of PID file to create
 209  * \param[in] name       Name of current process
 210  *
 211  * \return Standard Pacemaker return code
 212  */
 213 int
 214 pcmk__lock_pidfile(const char *filename, const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 215 {
 216     pid_t mypid = getpid();
 217     int fd = 0;
 218     int rc = 0;
 219     char buf[LOCKSTRLEN + 2];
 220 
 221     rc = pcmk__pidfile_matches(filename, 0, name, NULL);
 222     if ((rc != pcmk_rc_ok) && (rc != ENOENT)) {
 223         // Locked by existing process
 224         return rc;
 225     }
 226 
 227     fd = open(filename, O_CREAT | O_WRONLY | O_EXCL, 0644);
 228     if (fd < 0) {
 229         return errno;
 230     }
 231 
 232     snprintf(buf, sizeof(buf), "%*lld\n", LOCKSTRLEN - 1, (long long) mypid);
 233     rc = write(fd, buf, LOCKSTRLEN);
 234     close(fd);
 235 
 236     if (rc != LOCKSTRLEN) {
 237         crm_perror(LOG_ERR, "Incomplete write to %s", filename);
 238         return errno;
 239     }
 240 
 241     rc = pcmk__pidfile_matches(filename, mypid, name, NULL);
 242     if (rc != pcmk_rc_ok) {
 243         // Something is really wrong -- maybe I/O error on read back?
 244         unlink(filename);
 245     }
 246     return rc;
 247 }

/* [previous][next][first][last][top][bottom][index][help] */