diff options
author | Lennart Poettering <lennart@poettering.net> | 2023-10-16 23:18:05 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2023-10-18 14:39:33 +0200 |
commit | fc87713bedbc96d21b89e63acb9158c4f9363d17 (patch) | |
tree | 7ac23b0b780b1ef14b5e8b66ec9949907d8cdcb1 | |
parent | process-util: add pidref_get_cmdline() (diff) | |
download | systemd-fc87713bedbc96d21b89e63acb9158c4f9363d17.tar.xz systemd-fc87713bedbc96d21b89e63acb9158c4f9363d17.zip |
process-util: add pidref_is_kernel_thread()
-rw-r--r-- | src/basic/process-util.c | 19 | ||||
-rw-r--r-- | src/basic/process-util.h | 3 | ||||
-rw-r--r-- | src/cgtop/cgtop.c | 2 | ||||
-rw-r--r-- | src/core/dbus-unit.c | 2 | ||||
-rw-r--r-- | src/core/unit.c | 2 | ||||
-rw-r--r-- | src/shared/cgroup-setup.c | 2 | ||||
-rw-r--r-- | src/shared/cgroup-show.c | 2 | ||||
-rw-r--r-- | src/shared/killall.c | 2 | ||||
-rw-r--r-- | src/test/test-cgroup-util.c | 2 | ||||
-rw-r--r-- | src/test/test-process-util.c | 2 |
10 files changed, 28 insertions, 10 deletions
diff --git a/src/basic/process-util.c b/src/basic/process-util.c index f02d66e556..d607c7eb73 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -389,7 +389,7 @@ int namespace_get_leader(pid_t pid, NamespaceType type, pid_t *ret) { } } -int is_kernel_thread(pid_t pid) { +int pid_is_kernel_thread(pid_t pid) { _cleanup_free_ char *line = NULL; unsigned long long flags; size_t l, i; @@ -447,6 +447,23 @@ int is_kernel_thread(pid_t pid) { return !!(flags & PF_KTHREAD); } +int pidref_is_kernel_thread(const PidRef *pid) { + int result, r; + + if (!pidref_is_set(pid)) + return -ESRCH; + + result = pid_is_kernel_thread(pid->pid); + if (result < 0) + return result; + + r = pidref_verify(pid); /* Verify that the PID wasn't reused since */ + if (r < 0) + return r; + + return result; +} + int get_process_capeff(pid_t pid, char **ret) { const char *p; int r; diff --git a/src/basic/process-util.h b/src/basic/process-util.h index 78f7d409a0..c6f2687697 100644 --- a/src/basic/process-util.h +++ b/src/basic/process-util.h @@ -79,7 +79,8 @@ void sigkill_nowaitp(pid_t *pid); int kill_and_sigcont(pid_t pid, int sig); -int is_kernel_thread(pid_t pid); +int pid_is_kernel_thread(pid_t pid); +int pidref_is_kernel_thread(const PidRef *pid); int getenv_for_pid(pid_t pid, const char *field, char **_value); diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index a6adb8ce31..e34da7cf72 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -209,7 +209,7 @@ static int process( g->n_tasks = 0; while (cg_read_pid(f, &pid) > 0) { - if (arg_count == COUNT_USERSPACE_PROCESSES && is_kernel_thread(pid) > 0) + if (arg_count == COUNT_USERSPACE_PROCESSES && pid_is_kernel_thread(pid) > 0) continue; g->n_tasks++; diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c index ec3cf519e9..501055c10b 100644 --- a/src/core/dbus-unit.c +++ b/src/core/dbus-unit.c @@ -1295,7 +1295,7 @@ static int append_cgroup(sd_bus_message *reply, const char *p, Set *pids) { if (r < 0) return r; - if (is_kernel_thread(pid) > 0) + if (pid_is_kernel_thread(pid) > 0) continue; r = append_process(reply, p, pid, pids); diff --git a/src/core/unit.c b/src/core/unit.c index 4aef2e97de..69e758e6d3 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -5953,7 +5953,7 @@ int unit_pid_attachable(Unit *u, PidRef *pid, sd_bus_error *error) { return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process " PID_FMT " is a manager process, refusing.", pid->pid); /* Don't even begin to bother with kernel threads */ - r = is_kernel_thread(pid->pid); + r = pidref_is_kernel_thread(pid); if (r == -ESRCH) return sd_bus_error_setf(error, SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN, "Process with ID " PID_FMT " does not exist.", pid->pid); if (r < 0) diff --git a/src/shared/cgroup-setup.c b/src/shared/cgroup-setup.c index fac2e08afc..811f129f6c 100644 --- a/src/shared/cgroup-setup.c +++ b/src/shared/cgroup-setup.c @@ -611,7 +611,7 @@ int cg_migrate( * them there. */ if (cfrom && empty_or_root(pfrom) && - is_kernel_thread(pid) > 0) + pid_is_kernel_thread(pid) > 0) continue; r = cg_attach(cto, pto, pid); diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c index 9837577633..b77bab0215 100644 --- a/src/shared/cgroup-show.c +++ b/src/shared/cgroup-show.c @@ -114,7 +114,7 @@ static int show_cgroup_one_by_path( if (r < 0) return r; - if (!(flags & OUTPUT_KERNEL_THREADS) && is_kernel_thread(pid) > 0) + if (!(flags & OUTPUT_KERNEL_THREADS) && pid_is_kernel_thread(pid) > 0) continue; if (!GREEDY_REALLOC(pids, n + 1)) diff --git a/src/shared/killall.c b/src/shared/killall.c index ef715dbdfe..4da6335e10 100644 --- a/src/shared/killall.c +++ b/src/shared/killall.c @@ -77,7 +77,7 @@ static bool ignore_proc(pid_t pid, bool warn_rootfs) { return true; /* Ignore kernel threads */ - r = is_kernel_thread(pid); + r = pid_is_kernel_thread(pid); if (r != 0) return true; /* also ignore processes where we can't determine this */ diff --git a/src/test/test-cgroup-util.c b/src/test/test-cgroup-util.c index afa6283011..57facfe632 100644 --- a/src/test/test-cgroup-util.c +++ b/src/test/test-cgroup-util.c @@ -209,7 +209,7 @@ TEST(proc) { if (r < 0) continue; - if (is_kernel_thread(pid)) + if (pid_is_kernel_thread(pid) != 0) continue; cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path); diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index 53c7184e22..460cd318db 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -76,7 +76,7 @@ static void test_get_process_comm_one(pid_t pid) { assert_se(e > 0); } - assert_se(is_kernel_thread(pid) == 0 || pid != 1); + assert_se(pid_is_kernel_thread(pid) == 0 || pid != 1); r = get_process_exe(pid, &f); assert_se(r >= 0 || r == -EACCES); |