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.
This commit is contained in:
Lennart Poettering
2026-07-13 15:39:16 +02:00
parent d3c0348c8b
commit 0e077d3eea
6 changed files with 16 additions and 10 deletions
+4 -2
View 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;
+4
View 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);
}
+2
View 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);
+1 -1
View 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);
+1 -2
View 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
+4 -5
View 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);