From: Lennart Poettering Date: Mon, 13 Jul 2026 08:32:41 +0000 (+0200) Subject: process-util: introduce proc_set_comm() helper X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0e077d3eea41747e5d729038749da9909cf301cd;p=thirdparty%2Fsystemd.git process-util: introduce proc_set_comm() helper This operation is done at a bunch of places, let's add a type-safe helper for it. --- diff --git a/src/basic/argv-util.c b/src/basic/argv-util.c index 0602193809c..68cdea11aec 100644 --- a/src/basic/argv-util.c +++ b/src/basic/argv-util.c @@ -182,6 +182,7 @@ static int update_argv(const char name[], size_t l) { int rename_process_full(const char *comm, const char *invocation) { bool truncated = false; + int r; /* This is a like a poor man's setproctitle(). It changes the comm field by the name specified by * 'comm', and changes argv[0] and the glibc's internally used names of the process @@ -204,8 +205,9 @@ int rename_process_full(const char *comm, const char *invocation) { /* First step, change the comm field. The main thread's comm is identical to the process comm. This means we * can use PR_SET_NAME, which sets the thread name for the calling thread. */ - if (prctl(PR_SET_NAME, comm) < 0) - log_debug_errno(errno, "PR_SET_NAME failed: %m"); + r = proc_set_comm(comm); + if (r < 0) + log_debug_errno(r, "PR_SET_NAME failed: %m"); if (l >= TASK_COMM_LEN) /* Linux userspace process names can be 15 chars at max */ truncated = true; diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 74f0d81b2ae..1f7bdedc16e 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -2288,3 +2288,7 @@ int prctl_safe(int op, unsigned long arg2, unsigned long arg3, unsigned long arg return RET_NERRNO(prctl(op, arg2, arg3, arg4, arg5)); } + +int proc_set_comm(const char *comm) { + return prctl_safe(PR_SET_NAME, (unsigned long) comm, 0, 0, 0); +} diff --git a/src/basic/process-util.h b/src/basic/process-util.h index e2f834a7445..0bdd8077b56 100644 --- a/src/basic/process-util.h +++ b/src/basic/process-util.h @@ -257,3 +257,5 @@ _noreturn_ void report_errno_and_exit(int errno_fd, int error); int read_errno(int errno_fd); int prctl_safe(int op, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5); + +int proc_set_comm(const char *comm); diff --git a/src/core/main.c b/src/core/main.c index 2b4036381cc..d67146a7ede 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -3567,7 +3567,7 @@ static int run_systemd(int argc, char *argv[]) { * reexecution we are then called 'systemd'. That is confusing, hence let's call us systemd * right-away. */ program_invocation_short_name = systemd; - (void) prctl(PR_SET_NAME, systemd); + (void) proc_set_comm(systemd); /* Save the original command line */ save_argc_argv(argc, argv); diff --git a/src/shared/async.c b/src/shared/async.c index f4843deb78a..7aefe62bb1e 100644 --- a/src/shared/async.c +++ b/src/shared/async.c @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include -#include #include #include @@ -64,7 +63,7 @@ int asynchronous_fsync(int fd, PidRef *ret_pid) { static int close_func(void *p) { unsigned v = PTR_TO_UINT(p); - (void) prctl(PR_SET_NAME, (unsigned long*) "(sd-close)"); + (void) proc_set_comm("(sd-close)"); /* Note: 💣 This function is invoked in a child process created via glibc's clone() wrapper. In such * children memory allocation is not allowed, since glibc does not release malloc mutexes in diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index 63458117cb9..e1d16165ba8 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -175,7 +174,7 @@ static void test_pid_get_comm_escape_one(const char *input, const char *output) log_debug("input: <%s> — output: <%s>", input, output); - ASSERT_OK_ERRNO(prctl(PR_SET_NAME, input)); + ASSERT_OK(proc_set_comm(input)); ASSERT_OK(pid_get_comm(0, &n)); log_debug("got: <%s>", n); @@ -199,7 +198,7 @@ TEST(pid_get_comm_escape) { test_pid_get_comm_escape_one("xxxxäöüß", "xxxx\\303\\244\\303\\266\\303\\274\\303\\237"); test_pid_get_comm_escape_one("xxxxxäöüß", "xxxxx\\303\\244\\303\\266\\303\\274\\303\\237"); - ASSERT_OK_ERRNO(prctl(PR_SET_NAME, saved)); + ASSERT_OK(proc_set_comm(saved)); } TEST(pid_is_unwaited) { @@ -314,7 +313,7 @@ TEST(pid_get_cmdline_harder) { ASSERT_OK_ERRNO(unlink(path)); - ASSERT_OK_ERRNO(prctl(PR_SET_NAME, "testa")); + ASSERT_OK(proc_set_comm("testa")); ASSERT_ERROR(pid_get_cmdline(0, SIZE_MAX, 0, &line), ENOENT); @@ -477,7 +476,7 @@ TEST(pid_get_cmdline_harder) { args = strv_free(args); ASSERT_OK_ERRNO(ftruncate(fd, 0)); - ASSERT_OK_ERRNO(prctl(PR_SET_NAME, "aaaa bbbb cccc")); + ASSERT_OK(proc_set_comm("aaaa bbbb cccc")); ASSERT_ERROR(pid_get_cmdline(0, SIZE_MAX, 0, &line), ENOENT);