1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/*
* Copyright 2004-2019 the Pacemaker project contributors
*
* The version control history for this file may have further details.
*
* This source code is licensed under the GNU Lesser General Public License
* version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
*/
#include <crm_internal.h>
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <crm/crm.h>
int
crm_pid_active(long pid, const char *daemon)
{
static int last_asked_pid = 0; /* log spam prevention */
#if SUPPORT_PROCFS
static int have_proc_pid = 0;
#else
static int have_proc_pid = -1;
#endif
int rc = 0;
if (have_proc_pid == 0) {
/* evaluation of /proc/PID/exe applicability via self-introspection */
char proc_path[PATH_MAX], exe_path[PATH_MAX];
snprintf(proc_path, sizeof(proc_path), "/proc/%lu/exe",
(long unsigned int) getpid());
have_proc_pid = 1;
if (readlink(proc_path, exe_path, sizeof(exe_path) - 1) < 0) {
have_proc_pid = -1;
}
}
if (pid <= 0) {
return -1;
} else if ((rc = kill(pid, 0)) < 0 && errno == ESRCH) {
return 0; /* no such PID detected */
} else if (rc < 0 && (daemon == NULL || have_proc_pid == -1)) {
if (last_asked_pid != pid) {
crm_info("Cannot examine PID %ld: %s", pid, strerror(errno));
last_asked_pid = pid;
}
return -2; /* errno != ESRCH */
} else if (rc == 0 && (daemon == NULL || have_proc_pid == -1)) {
return 1; /* kill as the only indicator, cannot double check */
} else if (daemon != NULL) {
/* make sure PID hasn't been reused by another process
XXX: might still be just a zombie, which could confuse decisions */
bool checked_through_kill = (rc == 0);
char proc_path[PATH_MAX], exe_path[PATH_MAX], myexe_path[PATH_MAX];
snprintf(proc_path, sizeof(proc_path), "/proc/%ld/exe", pid);
rc = readlink(proc_path, exe_path, sizeof(exe_path) - 1);
if ((rc < 0) && (errno == EACCES)) {
if (last_asked_pid != pid) {
crm_info("Could not read from %s: %s", proc_path,
strerror(errno));
last_asked_pid = pid;
}
return checked_through_kill ? 1 : -2;
} else if (rc < 0) {
if (last_asked_pid != pid) {
crm_err("Could not read from %s: %s (%d)", proc_path,
strerror(errno), errno);
last_asked_pid = pid;
}
return 0; /* most likely errno == ENOENT */
}
exe_path[rc] = '\0';
if (daemon[0] != '/') {
rc = snprintf(myexe_path, sizeof(myexe_path), CRM_DAEMON_DIR"/%s",
daemon);
} else {
rc = snprintf(myexe_path, sizeof(myexe_path), "%s", daemon);
}
if (rc > 0 && rc < sizeof(myexe_path) && !strcmp(exe_path, myexe_path)) {
return 1;
}
}
return 0;
}
#define LOCKSTRLEN 11
long
crm_read_pidfile(const char *filename)
{
int fd;
struct stat sbuf;
long pid = -ENOENT;
char buf[LOCKSTRLEN + 1];
fd = open(filename, O_RDONLY);
if (fd < 0) {
goto bail;
}
if ((fstat(fd, &sbuf) >= 0) && (sbuf.st_size < LOCKSTRLEN)) {
sleep(2); /* if someone was about to create one,
* give'm a sec to do so
*/
}
if (read(fd, buf, sizeof(buf)) < 1) {
goto bail;
}
if (sscanf(buf, "%ld", &pid) > 0) {
if (pid <= 0) {
pid = -ESRCH;
} else {
crm_trace("Got pid %lu from %s\n", pid, filename);
}
}
bail:
if (fd >= 0) {
close(fd);
}
return pid;
}
long
crm_pidfile_inuse(const char *filename, long mypid, const char *daemon)
{
long pid = crm_read_pidfile(filename);
if (pid < 2) {
// Invalid pid
pid = -ENOENT;
unlink(filename);
} else if (mypid && (pid == mypid)) {
// In use by us
pid = pcmk_ok;
} else if (crm_pid_active(pid, daemon) == FALSE) {
// Contains a stale value
unlink(filename);
pid = -ENOENT;
} else if (mypid && (pid != mypid)) {
// Locked by existing process
pid = -EEXIST;
}
return pid;
}
int
crm_lock_pidfile(const char *filename, const char *name)
{
long mypid = 0;
int fd = 0;
int rc = 0;
char buf[LOCKSTRLEN + 2];
mypid = (unsigned long) getpid();
rc = crm_pidfile_inuse(filename, 0, name);
if (rc == -ENOENT) {
// Exists, but the process is not active
} else if (rc != pcmk_ok) {
// Locked by existing process
return rc;
}
fd = open(filename, O_CREAT | O_WRONLY | O_EXCL, 0644);
if (fd < 0) {
return -errno;
}
snprintf(buf, sizeof(buf), "%*ld\n", LOCKSTRLEN - 1, mypid);
rc = write(fd, buf, LOCKSTRLEN);
close(fd);
if (rc != LOCKSTRLEN) {
crm_perror(LOG_ERR, "Incomplete write to %s", filename);
return -errno;
}
return crm_pidfile_inuse(filename, mypid, name);
}
|