]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
process-util: introduce proc_set_comm() helper
authorLennart Poettering <lennart@amutable.com>
Mon, 13 Jul 2026 08:32:41 +0000 (10:32 +0200)
committerLennart Poettering <lennart@amutable.com>
Mon, 13 Jul 2026 13:39:16 +0000 (15:39 +0200)
This operation is done at a bunch of places, let's add a type-safe
helper for it.

src/basic/argv-util.c
src/basic/process-util.c
src/basic/process-util.h
src/core/main.c
src/shared/async.c
src/test/test-process-util.c

index 0602193809cf66988f8fc5641910014e0efdfcdf..68cdea11aecd28db742b07b376d82815a9d42fee 100644 (file)
@@ -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;
 
index 74f0d81b2aee0834e191ff6e2bff4c8d3f679536..1f7bdedc16e9fb27692a894a3928212df2a6bc1c 100644 (file)
@@ -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);
+}
index e2f834a7445747a96d492630a418acdc50babe99..0bdd8077b5652c2ba98a04d5969af2bafdf95b40 100644 (file)
@@ -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);
index 2b4036381cca887f29c7730e8d3dc71a82fc257f..d67146a7ede17678865d8b518dcd0ff54a291174 100644 (file)
@@ -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);
index f4843deb78a5b8ee11bdacbcd7dc25eae06cadef..7aefe62bb1e8b6f6ce6e0a4d1a9e8d5b4b172564 100644 (file)
@@ -1,7 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <sched.h>
-#include <sys/prctl.h>
 #include <sys/wait.h>
 #include <unistd.h>
 
@@ -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
index 63458117cb9378a41a8398cea9c5f980137f1979..e1d16165ba84cae1b06bb645b95c4a9e01930d69 100644 (file)
@@ -7,7 +7,6 @@
 #include <sys/eventfd.h>
 #include <sys/mount.h>
 #include <sys/personality.h>
-#include <sys/prctl.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <unistd.h>
@@ -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);