mirror of
https://github.com/systemd/systemd.git
synced 2025-01-09 01:18:19 +03:00
process-util: make sure we don't report ppid == 0
Previously, if pid == 0 and we're PID 1, get_process_ppid() would set ret to getppid(), i.e. 0, which is inconsistent when pid is explicitly set to 1. Ensure we always handle such case by returning -EADDRNOTAVAIL.
This commit is contained in:
parent
07612aab66
commit
61263e1436
@ -702,15 +702,17 @@ int get_process_ppid(pid_t pid, pid_t *ret) {
|
|||||||
|
|
||||||
assert(pid >= 0);
|
assert(pid >= 0);
|
||||||
|
|
||||||
if (pid == 0 || pid == getpid_cached()) {
|
if (pid == 0)
|
||||||
|
pid = getpid_cached();
|
||||||
|
if (pid == 1) /* PID 1 has no parent, shortcut this case */
|
||||||
|
return -EADDRNOTAVAIL;
|
||||||
|
|
||||||
|
if (pid == getpid_cached()) {
|
||||||
if (ret)
|
if (ret)
|
||||||
*ret = getppid();
|
*ret = getppid();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pid == 1) /* PID 1 has no parent, shortcut this case */
|
|
||||||
return -EADDRNOTAVAIL;
|
|
||||||
|
|
||||||
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)
|
if (r == -ENOENT)
|
||||||
@ -724,7 +726,6 @@ int get_process_ppid(pid_t pid, pid_t *ret) {
|
|||||||
p = strrchr(line, ')');
|
p = strrchr(line, ')');
|
||||||
if (!p)
|
if (!p)
|
||||||
return -EIO;
|
return -EIO;
|
||||||
|
|
||||||
p++;
|
p++;
|
||||||
|
|
||||||
if (sscanf(p, " "
|
if (sscanf(p, " "
|
||||||
@ -733,9 +734,9 @@ int get_process_ppid(pid_t pid, pid_t *ret) {
|
|||||||
&ppid) != 1)
|
&ppid) != 1)
|
||||||
return -EIO;
|
return -EIO;
|
||||||
|
|
||||||
/* If ppid is zero the process has no parent. Which might be the case for PID 1 but also for
|
/* If ppid is zero the process has no parent. Which might be the case for PID 1 (caught above)
|
||||||
* processes originating in other namespaces that are inserted into a pidns. Return a recognizable
|
* but also for processes originating in other namespaces that are inserted into a pidns.
|
||||||
* error in this case. */
|
* Return a recognizable error in this case. */
|
||||||
if (ppid == 0)
|
if (ppid == 0)
|
||||||
return -EADDRNOTAVAIL;
|
return -EADDRNOTAVAIL;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user