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 int rdlnk_errno = errno;
77 if (rdlnk_errno != EACCES) {
80 if ((rc < 0) && (errno == ESRCH)) {
84 if (last_asked_pid !=
pid) {
85 if (rdlnk_errno == EACCES) {
87 proc_path,
strerror(rdlnk_errno), rdlnk_errno);
90 proc_path,
strerror(rdlnk_errno), rdlnk_errno);
94 if ((rdlnk_errno == EACCES) && checked_through_kill) {
97 }
else if (rdlnk_errno == EACCES) {
106 rc = snprintf(myexe_path,
sizeof(myexe_path),
CRM_DAEMON_DIR"/%s",
109 rc = snprintf(myexe_path,
sizeof(myexe_path),
"%s",
daemon);
112 if (rc > 0 && rc <
sizeof(myexe_path) && !strcmp(exe_path, myexe_path)) {
120 #define LOCKSTRLEN 11 137 long long pid_read = 0;
140 CRM_CHECK((filename != NULL) && (
pid != NULL),
return EINVAL);
142 fd = open(filename, O_RDONLY);
147 if ((fstat(fd, &sbuf) >= 0) && (sbuf.st_size <
LOCKSTRLEN)) {
153 if (read(fd, buf,
sizeof(buf)) < 1) {
158 if (sscanf(buf,
"%lld", &pid_read) > 0) {
163 *
pid = (pid_t) pid_read;
164 crm_trace(
"Read pid %lld from %s", pid_read, filename);
187 const char *expected_name, pid_t *
pid)
189 pid_t pidfile_pid = 0;
201 }
else if ((expected_pid > 0) && (pidfile_pid == expected_pid)) {
210 }
else if ((expected_pid > 0) && (pidfile_pid != expected_pid)) {
230 pid_t mypid = getpid();
241 fd = open(filename, O_CREAT | O_WRONLY | O_EXCL, 0644);
246 snprintf(buf,
sizeof(buf),
"%*lld\n",
LOCKSTRLEN - 1, (
long long) mypid);
251 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...)