25 static pid_t last_asked_pid = 0;
27 static int have_proc_pid = 0;
29 static int have_proc_pid = -1;
32 bool no_name_check = ((
daemon == NULL) || (have_proc_pid == -1));
34 if (have_proc_pid == 0) {
36 char proc_path[PATH_MAX], exe_path[PATH_MAX];
37 snprintf(proc_path,
sizeof(proc_path),
"/proc/%lld/exe",
38 (
long long) getpid());
40 if (readlink(proc_path, exe_path,
sizeof(exe_path) - 1) < 0) {
50 if ((
rc < 0) && (errno == ESRCH)) {
53 }
else if ((
rc < 0) && no_name_check) {
55 if (last_asked_pid !=
pid) {
56 crm_info(
"Cannot examine PID %lld: %s",
62 }
else if ((
rc == 0) && no_name_check) {
65 }
else if (
daemon != NULL) {
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",
73 rc = readlink(proc_path, exe_path,
sizeof(exe_path) - 1);
75 if (last_asked_pid !=
pid) {
76 if (errno == EACCES) {
85 if ((errno == EACCES) && checked_through_kill) {
88 }
else if (errno == EACCES) {
100 rc = snprintf(myexe_path,
sizeof(myexe_path),
"%s",
daemon);
103 if (
rc > 0 &&
rc <
sizeof(myexe_path) && !strcmp(exe_path, myexe_path)) {
111 #define LOCKSTRLEN 11 128 long long pid_read = 0;
131 CRM_CHECK((filename != NULL) && (
pid != NULL),
return EINVAL);
133 fd = open(filename, O_RDONLY);
138 if ((fstat(fd, &sbuf) >= 0) && (sbuf.st_size <
LOCKSTRLEN)) {
144 if (read(fd, buf,
sizeof(buf)) < 1) {
149 if (sscanf(buf,
"%lld", &pid_read) > 0) {
154 *
pid = (pid_t) pid_read;
155 crm_trace(
"Read pid %lld from %s", pid_read, filename);
178 const char *expected_name, pid_t *
pid)
180 pid_t pidfile_pid = 0;
192 }
else if ((expected_pid > 0) && (pidfile_pid == expected_pid)) {
201 }
else if ((expected_pid > 0) && (pidfile_pid != expected_pid)) {
221 pid_t mypid = getpid();
232 fd = open(filename, O_CREAT | O_WRONLY | O_EXCL, 0644);
237 snprintf(buf,
sizeof(buf),
"%*lld\n",
LOCKSTRLEN - 1, (
long long) mypid);
242 crm_perror(LOG_ERR,
"Incomplete write to %s", filename);
#define CRM_CHECK(expr, failure_action)
int pcmk__read_pidfile(const char *filename, pid_t *pid)
int pcmk__pid_active(pid_t pid, const char *daemon)
char * strerror(int errnum)
int daemon(int nochdir, int noclose)
#define crm_trace(fmt, args...)
int pcmk__pidfile_matches(const char *filename, pid_t expected_pid, const char *expected_name, pid_t *pid)
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
#define crm_err(fmt, args...)
int pcmk__lock_pidfile(const char *filename, const char *name)
#define crm_info(fmt, args...)