1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-13 23:21:08 +03:00

process: return ESRCH when a PID is not valid anymore

so far, when we read something from /proc/$PID we would pass on the
ENOENT from the kernel as error, if the process was missing. With this
change we systematically convert this to ESRCH, which is the more
appropriate error code, and what all the other glibc/syscalls like
kill() use.

All code that calls these functions should be fine with this change. In
fact, one invocation of get_process_exe() in bus-creds.c already assumed
ESRCH would be returned if a process is missing, and this assumption is
now validated after the change.
This commit is contained in:
Lennart Poettering 2015-07-23 23:44:40 +02:00
parent 901108257e
commit a644184a17

View File

@ -43,7 +43,10 @@ int get_process_state(pid_t pid) {
assert(pid >= 0); assert(pid >= 0);
p = procfs_file_alloca(pid, "stat"); p = procfs_file_alloca(pid, "stat");
r = read_one_line_file(p, &line); r = read_one_line_file(p, &line);
if (r == -ENOENT)
return -ESRCH;
if (r < 0) if (r < 0)
return r; return r;
@ -87,8 +90,11 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
p = procfs_file_alloca(pid, "cmdline"); p = procfs_file_alloca(pid, "cmdline");
f = fopen(p, "re"); f = fopen(p, "re");
if (!f) if (!f) {
if (errno == ENOENT)
return -ESRCH;
return -errno; return -errno;
}
if (max_length == 0) { if (max_length == 0) {
size_t len = 0, allocated = 0; size_t len = 0, allocated = 0;
@ -182,8 +188,11 @@ int is_kernel_thread(pid_t pid) {
p = procfs_file_alloca(pid, "cmdline"); p = procfs_file_alloca(pid, "cmdline");
f = fopen(p, "re"); f = fopen(p, "re");
if (!f) if (!f) {
if (errno == ENOENT)
return -ESRCH;
return -errno; return -errno;
}
count = fread(&c, 1, 1, f); count = fread(&c, 1, 1, f);
eof = feof(f); eof = feof(f);
@ -199,13 +208,18 @@ int is_kernel_thread(pid_t pid) {
int get_process_capeff(pid_t pid, char **capeff) { int get_process_capeff(pid_t pid, char **capeff) {
const char *p; const char *p;
int r;
assert(capeff); assert(capeff);
assert(pid >= 0); assert(pid >= 0);
p = procfs_file_alloca(pid, "status"); p = procfs_file_alloca(pid, "status");
return get_status_field(p, "\nCapEff:", capeff); r = get_status_field(p, "\nCapEff:", capeff);
if (r == -ENOENT)
return -ESRCH;
return r;
} }
static int get_process_link_contents(const char *proc_file, char **name) { static int get_process_link_contents(const char *proc_file, char **name) {
@ -215,8 +229,10 @@ static int get_process_link_contents(const char *proc_file, char **name) {
assert(name); assert(name);
r = readlink_malloc(proc_file, name); r = readlink_malloc(proc_file, name);
if (r == -ENOENT)
return -ESRCH;
if (r < 0) if (r < 0)
return r == -ENOENT ? -ESRCH : r; return r;
return 0; return 0;
} }
@ -253,8 +269,11 @@ static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
p = procfs_file_alloca(pid, "status"); p = procfs_file_alloca(pid, "status");
f = fopen(p, "re"); f = fopen(p, "re");
if (!f) if (!f) {
if (errno == ENOENT)
return -ESRCH;
return -errno; return -errno;
}
FOREACH_LINE(line, f, return -errno) { FOREACH_LINE(line, f, return -errno) {
char *l; char *l;
@ -316,8 +335,11 @@ int get_process_environ(pid_t pid, char **env) {
p = procfs_file_alloca(pid, "environ"); p = procfs_file_alloca(pid, "environ");
f = fopen(p, "re"); f = fopen(p, "re");
if (!f) if (!f) {
if (errno == ENOENT)
return -ESRCH;
return -errno; return -errno;
}
while ((c = fgetc(f)) != EOF) { while ((c = fgetc(f)) != EOF) {
if (!GREEDY_REALLOC(outcome, allocated, sz + 5)) if (!GREEDY_REALLOC(outcome, allocated, sz + 5))
@ -355,6 +377,8 @@ int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
p = procfs_file_alloca(pid, "stat"); p = procfs_file_alloca(pid, "stat");
r = read_one_line_file(p, &line); r = read_one_line_file(p, &line);
if (r == -ENOENT)
return -ESRCH;
if (r < 0) if (r < 0)
return r; return r;
@ -475,8 +499,11 @@ int getenv_for_pid(pid_t pid, const char *field, char **_value) {
path = procfs_file_alloca(pid, "environ"); path = procfs_file_alloca(pid, "environ");
f = fopen(path, "re"); f = fopen(path, "re");
if (!f) if (!f) {
if (errno == ENOENT)
return -ESRCH;
return -errno; return -errno;
}
l = strlen(field); l = strlen(field);
r = 0; r = 0;
@ -535,7 +562,7 @@ bool pid_is_alive(pid_t pid) {
return false; return false;
r = get_process_state(pid); r = get_process_state(pid);
if (r == -ENOENT || r == 'Z') if (r == -ESRCH || r == 'Z')
return false; return false;
return true; return true;