summaryrefslogtreecommitdiff
path: root/src/coredump/coredump.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-07-07 15:57:51 +0200
committerLuca Boccassi <luca.boccassi@gmail.com>2021-07-07 18:41:08 +0100
commit0c4d1e6d96a549054bfe0597d197f829838917f1 (patch)
tree03d26661dc8957516a70aa29607767126ea8d80f /src/coredump/coredump.c
parent682047f834112f691716292eb5bd349e51bee3ef (diff)
downloadsystemd-0c4d1e6d96a549054bfe0597d197f829838917f1.tar.gz
process-util: explicitly handle processes lacking parents in get_process_ppid()
Let's make sure we signal out-of-band via an error message if a process doesn't have a parent process whose PID we could return. Otherwise we'll too likely hide errors, as we return an invalid PID 0, which in other contexts has special meaning (i.e. usually "myself"). Replaces: #20153 This is based on work by @dtardon, but goes a different route, by ensuring we propagate a proper error in this case. This modernizes the function in question a bit in other ways, i.e. renames stuff and makes the return parameter optional.
Diffstat (limited to 'src/coredump/coredump.c')
-rw-r--r--src/coredump/coredump.c23
1 files changed, 11 insertions, 12 deletions
diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c
index c6c232c7e7..444b9ec374 100644
--- a/src/coredump/coredump.c
+++ b/src/coredump/coredump.c
@@ -668,8 +668,7 @@ static int get_process_ns(pid_t pid, const char *namespace, ino_t *ns) {
return 0;
}
-static int get_mount_namespace_leader(pid_t pid, pid_t *container_pid) {
- pid_t cpid = pid, ppid = 0;
+static int get_mount_namespace_leader(pid_t pid, pid_t *ret) {
ino_t proc_mntns;
int r;
@@ -679,8 +678,12 @@ static int get_mount_namespace_leader(pid_t pid, pid_t *container_pid) {
for (;;) {
ino_t parent_mntns;
+ pid_t ppid;
- r = get_process_ppid(cpid, &ppid);
+ r = get_process_ppid(pid, &ppid);
+ if (r == -EADDRNOTAVAIL) /* Reached the top (i.e. typically PID 1, but could also be a process
+ * whose parent is not in our pidns) */
+ return -ENOENT;
if (r < 0)
return r;
@@ -688,17 +691,13 @@ static int get_mount_namespace_leader(pid_t pid, pid_t *container_pid) {
if (r < 0)
return r;
- if (proc_mntns != parent_mntns)
- break;
-
- if (ppid == 1)
- return -ENOENT;
+ if (proc_mntns != parent_mntns) {
+ *ret = ppid;
+ return 0;
+ }
- cpid = ppid;
+ pid = ppid;
}
-
- *container_pid = ppid;
- return 0;
}
/* Returns 1 if the parent was found.