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) {
80 if ((errno == EACCES) && checked_through_kill) {
83 }
else if (errno == EACCES) {
95 rc = snprintf(myexe_path,
sizeof(myexe_path),
"%s",
daemon);
98 if (
rc > 0 &&
rc <
sizeof(myexe_path) && !strcmp(exe_path, myexe_path)) {
106 #define LOCKSTRLEN 11 123 long long pid_read = 0;
126 CRM_CHECK((filename != NULL) && (
pid != NULL),
return EINVAL);
128 fd = open(filename, O_RDONLY);
133 if ((fstat(fd, &sbuf) >= 0) && (sbuf.st_size <
LOCKSTRLEN)) {
139 if (read(fd, buf,
sizeof(buf)) < 1) {
144 if (sscanf(buf,
"%lld", &pid_read) > 0) {
149 *
pid = (pid_t) pid_read;
150 crm_trace(
"Read pid %lld from %s", pid_read, filename);
173 const char *expected_name, pid_t *
pid)
175 pid_t pidfile_pid = 0;
187 }
else if ((expected_pid > 0) && (pidfile_pid == expected_pid)) {
196 }
else if ((expected_pid > 0) && (pidfile_pid != expected_pid)) {
216 pid_t mypid = getpid();
227 fd = open(filename, O_CREAT | O_WRONLY | O_EXCL, 0644);
232 snprintf(buf,
sizeof(buf),
"%*lld\n",
LOCKSTRLEN - 1, (
long long) mypid);
237 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...)