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
/* 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;
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);
+}
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);
* 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);
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <sched.h>
-#include <sys/prctl.h>
#include <sys/wait.h>
#include <unistd.h>
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
#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>
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);
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) {
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);
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);