1
0
mirror of https://github.com/systemd/systemd.git synced 2025-02-04 21:47:31 +03:00

process-util: add pidref_get_comm() and rename get_process_comm() to pid_get_comm()

This commit is contained in:
Lennart Poettering 2023-10-17 10:16:31 +02:00
parent d57d521cae
commit d7d748548b
28 changed files with 80 additions and 59 deletions

View File

@ -1451,7 +1451,7 @@ void log_received_signal(int level, const struct signalfd_siginfo *si) {
if (pid_is_valid(si->ssi_pid)) {
_cleanup_free_ char *p = NULL;
(void) get_process_comm(si->ssi_pid, &p);
(void) pid_get_comm(si->ssi_pid, &p);
log_full(level,
"Received SIG%s from PID %"PRIu32" (%s).",

View File

@ -94,7 +94,7 @@ static int get_process_state(pid_t pid) {
return (unsigned char) state;
}
int get_process_comm(pid_t pid, char **ret) {
int pid_get_comm(pid_t pid, char **ret) {
_cleanup_free_ char *escaped = NULL, *comm = NULL;
int r;
@ -132,6 +132,26 @@ int get_process_comm(pid_t pid, char **ret) {
return 0;
}
int pidref_get_comm(const PidRef *pid, char **ret) {
_cleanup_free_ char *comm = NULL;
int r;
if (!pidref_is_set(pid))
return -ESRCH;
r = pid_get_comm(pid->pid, &comm);
if (r < 0)
return r;
r = pidref_verify(pid);
if (r < 0)
return r;
if (ret)
*ret = TAKE_PTR(comm);
return 0;
}
static int pid_get_cmdline_nulstr(
pid_t pid,
size_t max_size,
@ -170,7 +190,7 @@ static int pid_get_cmdline_nulstr(
/* Kernel threads have no argv[] */
_cleanup_free_ char *comm = NULL;
r = get_process_comm(pid, &comm);
r = pid_get_comm(pid, &comm);
if (r < 0)
return r;
@ -752,7 +772,7 @@ int wait_for_terminate_and_check(const char *name, pid_t pid, WaitFlags flags) {
assert(pid > 1);
if (!name) {
r = get_process_comm(pid, &buffer);
r = pid_get_comm(pid, &buffer);
if (r < 0)
log_debug_errno(r, "Failed to acquire process name of " PID_FMT ", ignoring: %m", pid);
else

View File

@ -39,7 +39,8 @@ typedef enum ProcessCmdlineFlags {
PROCESS_CMDLINE_QUOTE_POSIX = 1 << 3,
} ProcessCmdlineFlags;
int get_process_comm(pid_t pid, char **ret);
int pid_get_comm(pid_t pid, char **ret);
int pidref_get_comm(const PidRef *pid, char **ret);
int pid_get_cmdline(pid_t pid, size_t max_columns, ProcessCmdlineFlags flags, char **ret);
int pidref_get_cmdline(const PidRef *pid, size_t max_columns, ProcessCmdlineFlags flags, char **ret);
int pid_get_cmdline_strv(pid_t pid, ProcessCmdlineFlags flags, char ***ret);

View File

@ -997,7 +997,7 @@ static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, vo
if (packet.v5_packet.pid > 0) {
_cleanup_free_ char *p = NULL;
(void) get_process_comm(packet.v5_packet.pid, &p);
(void) pid_get_comm(packet.v5_packet.pid, &p);
log_unit_info(UNIT(a), "Got automount request for %s, triggered by %"PRIu32" (%s)", a->where, packet.v5_packet.pid, strna(p));
} else
log_unit_debug(UNIT(a), "Got direct mount request on %s", a->where);

View File

@ -2823,7 +2823,7 @@ static int manager_dispatch_sigchld(sd_event_source *source, void *userdata) {
_cleanup_free_ char *name = NULL;
Unit *u1, *u2, **array;
(void) get_process_comm(si.si_pid, &name);
(void) pid_get_comm(si.si_pid, &name);
log_debug("Child "PID_FMT" (%s) died (code=%s, status=%i/%s)",
si.si_pid, strna(name),

View File

@ -2295,7 +2295,7 @@ static void service_kill_control_process(Service *s) {
if (r < 0) {
_cleanup_free_ char *comm = NULL;
(void) get_process_comm(s->control_pid.pid, &comm);
(void) pidref_get_comm(&s->control_pid, &comm);
log_unit_debug_errno(UNIT(s), r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m",
s->control_pid.pid, strna(comm));

View File

@ -4010,7 +4010,7 @@ static int kill_common_log(pid_t pid, int signo, void *userdata) {
_cleanup_free_ char *comm = NULL;
Unit *u = ASSERT_PTR(userdata);
(void) get_process_comm(pid, &comm);
(void) pid_get_comm(pid, &comm);
log_unit_info(u, "Sending signal SIG%s to process " PID_FMT " (%s) on client request.",
signal_to_string(signo), pid, strna(comm));
@ -4081,7 +4081,7 @@ int unit_kill(
if (pidref_is_set(control_pid) &&
IN_SET(who, KILL_CONTROL, KILL_CONTROL_FAIL, KILL_ALL, KILL_ALL_FAIL)) {
_cleanup_free_ char *comm = NULL;
(void) get_process_comm(control_pid->pid, &comm);
(void) pidref_get_comm(control_pid, &comm);
r = kill_or_sigqueue(control_pid, signo, code, value);
if (r < 0) {
@ -4107,7 +4107,7 @@ int unit_kill(
IN_SET(who, KILL_MAIN, KILL_MAIN_FAIL, KILL_ALL, KILL_ALL_FAIL)) {
_cleanup_free_ char *comm = NULL;
(void) get_process_comm(main_pid->pid, &comm);
(void) pidref_get_comm(main_pid, &comm);
r = kill_or_sigqueue(main_pid, signo, code, value);
if (r < 0) {
@ -4732,7 +4732,7 @@ int unit_make_transient(Unit *u) {
static int log_kill(pid_t pid, int sig, void *userdata) {
_cleanup_free_ char *comm = NULL;
(void) get_process_comm(pid, &comm);
(void) pid_get_comm(pid, &comm);
/* Don't log about processes marked with brackets, under the assumption that these are temporary processes
only, like for example systemd's own PAM stub process. */
@ -4820,7 +4820,7 @@ int unit_kill_context(
r = pidref_kill_and_sigcont(main_pid, sig);
if (r < 0 && r != -ESRCH) {
_cleanup_free_ char *comm = NULL;
(void) get_process_comm(main_pid->pid, &comm);
(void) pidref_get_comm(main_pid, &comm);
log_unit_warning_errno(u, r, "Failed to kill main process " PID_FMT " (%s), ignoring: %m", main_pid->pid, strna(comm));
} else {
@ -4839,7 +4839,7 @@ int unit_kill_context(
r = pidref_kill_and_sigcont(control_pid, sig);
if (r < 0 && r != -ESRCH) {
_cleanup_free_ char *comm = NULL;
(void) get_process_comm(control_pid->pid, &comm);
(void) pidref_get_comm(control_pid, &comm);
log_unit_warning_errno(u, r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", control_pid->pid, strna(comm));
} else {
@ -5865,7 +5865,7 @@ static bool ignore_leftover_process(const char *comm) {
int unit_log_leftover_process_start(pid_t pid, int sig, void *userdata) {
_cleanup_free_ char *comm = NULL;
(void) get_process_comm(pid, &comm);
(void) pid_get_comm(pid, &comm);
if (ignore_leftover_process(comm))
return 0;
@ -5883,7 +5883,7 @@ int unit_log_leftover_process_start(pid_t pid, int sig, void *userdata) {
int unit_log_leftover_process_stop(pid_t pid, int sig, void *userdata) {
_cleanup_free_ char *comm = NULL;
(void) get_process_comm(pid, &comm);
(void) pid_get_comm(pid, &comm);
if (ignore_leftover_process(comm))
return 0;

View File

@ -1218,7 +1218,7 @@ static int gather_pid_metadata_from_procfs(struct iovec_wrapper *iovw, Context *
pid = context->pid;
/* The following is mandatory */
r = get_process_comm(pid, &t);
r = pid_get_comm(pid, &t);
if (r < 0)
return log_error_errno(r, "Failed to get COMM: %m");

View File

@ -66,7 +66,7 @@ void server_forward_console(
/* Second: identifier and PID */
if (ucred) {
if (!identifier) {
(void) get_process_comm(ucred->pid, &ident_buf);
(void) pid_get_comm(ucred->pid, &ident_buf);
identifier = ident_buf;
}

View File

@ -224,7 +224,7 @@ static void client_context_read_basic(ClientContext *c) {
assert(c);
assert(pid_is_valid(c->pid));
if (get_process_comm(c->pid, &t) >= 0)
if (pid_get_comm(c->pid, &t) >= 0)
free_and_replace(c->comm, t);
if (get_process_exe(c->pid, &t) >= 0)

View File

@ -61,7 +61,7 @@ void server_forward_kmsg(
/* Second: identifier and PID */
if (ucred) {
if (!identifier) {
(void) get_process_comm(ucred->pid, &ident_buf);
(void) pid_get_comm(ucred->pid, &ident_buf);
identifier = ident_buf;
}

View File

@ -154,7 +154,7 @@ void server_forward_syslog(Server *s, int priority, const char *identifier, cons
/* Third: identifier and PID */
if (ucred) {
if (!identifier) {
(void) get_process_comm(ucred->pid, &ident_buf);
(void) pid_get_comm(ucred->pid, &ident_buf);
identifier = ident_buf;
}

View File

@ -27,7 +27,7 @@ void server_forward_wall(
if (ucred) {
if (!identifier) {
(void) get_process_comm(ucred->pid, &ident_buf);
(void) pid_get_comm(ucred->pid, &ident_buf);
identifier = ident_buf;
}

View File

@ -969,7 +969,7 @@ int bus_creds_add_more(sd_bus_creds *c, uint64_t mask, pid_t pid, pid_t tid) {
}
if (missing & SD_BUS_CREDS_COMM) {
r = get_process_comm(pid, &c->comm);
r = pid_get_comm(pid, &c->comm);
if (r < 0) {
if (!ERRNO_IS_PRIVILEGE(r))
return r;

View File

@ -920,7 +920,7 @@ static int bind_description(sd_bus *b, int fd, int family) {
* - a random 64-bit value (to avoid collisions)
* - our "comm" process name (suppressed if contains "/" to avoid parsing issues)
* - the description string of the bus connection. */
(void) get_process_comm(0, &comm);
(void) pid_get_comm(0, &comm);
if (comm && strchr(comm, '/'))
comm = mfree(comm);

View File

@ -47,7 +47,7 @@ static void *server(void *p) {
const char *comm;
assert_se(sd_bus_creds_get_comm(c, &comm) >= 0);
assert_se(get_process_comm(0, &our_comm) >= 0);
assert_se(pid_get_comm(0, &our_comm) >= 0);
assert_se(streq_ptr(comm, our_comm));
const char *description;

View File

@ -91,7 +91,7 @@ static int print_inhibitors(sd_bus *bus) {
if (arg_mode && !streq(mode, arg_mode))
continue;
(void) get_process_comm(pid, &comm);
(void) pid_get_comm(pid, &comm);
u = uid_to_name(uid);
r = table_add_many(table,

View File

@ -525,7 +525,7 @@ static int print_session_status_info(sd_bus *bus, const char *path) {
if (i.leader > 0) {
_cleanup_free_ char *name = NULL;
(void) get_process_comm(i.leader, &name);
(void) pid_get_comm(i.leader, &name);
r = table_add_cell(table, NULL, TABLE_FIELD, "Leader");
if (r < 0)

View File

@ -239,7 +239,7 @@ int manager_handle_action(
manager_is_inhibited(m, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0, &offending)) {
_cleanup_free_ char *comm = NULL, *u = NULL;
(void) get_process_comm(offending->pid.pid, &comm);
(void) pidref_get_comm(&offending->pid, &comm);
u = uid_to_name(offending->uid);
/* If this is just a recheck of the lid switch then don't warn about anything */

View File

@ -1650,7 +1650,7 @@ int manager_dispatch_delayed(Manager *manager, bool timeout) {
if (!timeout)
return 0;
(void) get_process_comm(offending->pid.pid, &comm);
(void) pidref_get_comm(&offending->pid, &comm);
u = uid_to_name(offending->uid);
log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",

View File

@ -526,7 +526,7 @@ static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
printf("\t Leader: %u", (unsigned) i->leader);
(void) get_process_comm(i->leader, &t);
(void) pid_get_comm(i->leader, &t);
if (t)
printf(" (%s)", t);

View File

@ -14,7 +14,7 @@ int sigrtmin18_handler(sd_event_source *s, const struct signalfd_siginfo *si, vo
assert(s);
assert(si);
(void) get_process_comm(si->ssi_pid, &comm);
(void) pid_get_comm(si->ssi_pid, &comm);
if (si->ssi_code != SI_QUEUE) {
log_notice("Received control signal %s from process " PID_FMT " (%s) without command value, ignoring.",

View File

@ -101,7 +101,7 @@ static bool ignore_proc(pid_t pid, bool warn_rootfs) {
_cleanup_free_ char *comm = NULL;
(void) get_process_comm(pid, &comm);
(void) pid_get_comm(pid, &comm);
log_notice("Process " PID_FMT " (%s) has been marked to be excluded from killing. It is "
"running from the root file system, and thus likely to block re-mounting of the "
@ -120,7 +120,7 @@ static void log_children_no_yet_killed(Set *pids) {
SET_FOREACH(p, pids) {
_cleanup_free_ char *s = NULL;
if (get_process_comm(PTR_TO_PID(p), &s) >= 0)
if (pid_get_comm(PTR_TO_PID(p), &s) >= 0)
r = strextendf(&lst_child, ", " PID_FMT " (%s)", PTR_TO_PID(p), s);
else
r = strextendf(&lst_child, ", " PID_FMT, PTR_TO_PID(p));
@ -248,7 +248,7 @@ static int killall(int sig, Set *pids, bool send_sighup) {
if (sig == SIGKILL) {
_cleanup_free_ char *s = NULL;
(void) get_process_comm(pid, &s);
(void) pid_get_comm(pid, &s);
log_notice("Sending SIGKILL to PID "PID_FMT" (%s).", pid, strna(s));
}

View File

@ -222,7 +222,7 @@ static void log_umount_blockers(const char *mnt) {
continue;
_cleanup_free_ char *comm = NULL;
r = get_process_comm(pid, &comm);
r = pid_get_comm(pid, &comm);
if (r < 0) {
if (r != -ESRCH) /* process gone by now */
log_debug_errno(r, "Failed to read process name of PID " PID_FMT ": %m", pid);

View File

@ -168,7 +168,7 @@ int logind_check_inhibitors(enum action a) {
ACTION_KEXEC) ? "shutdown" : "sleep"))
continue;
(void) get_process_comm(pid, &comm);
(void) pid_get_comm(pid, &comm);
user = uid_to_name(uid);
log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".",

View File

@ -626,7 +626,7 @@ static void print_status_info(
if (arg_transport == BUS_TRANSPORT_LOCAL) {
_cleanup_free_ char *comm = NULL;
(void) get_process_comm(i->main_pid, &comm);
(void) pid_get_comm(i->main_pid, &comm);
if (comm)
printf(" (%s)", comm);
}
@ -661,7 +661,7 @@ static void print_status_info(
printf(PID_FMT, i->control_pid);
if (arg_transport == BUS_TRANSPORT_LOCAL) {
(void) get_process_comm(i->control_pid, &c);
(void) pid_get_comm(i->control_pid, &c);
if (c)
printf(" (%s)", c);
}

View File

@ -35,7 +35,7 @@ static void test_rename_process_now(const char *p, int ret) {
return;
#endif
assert_se(get_process_comm(0, &comm) >= 0);
assert_se(pid_get_comm(0, &comm) >= 0);
log_debug("comm = <%s>", comm);
assert_se(strneq(comm, p, TASK_COMM_LEN-1));
/* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the

View File

@ -38,7 +38,7 @@
#include "user-util.h"
#include "virt.h"
static void test_get_process_comm_one(pid_t pid) {
static void test_pid_get_comm_one(pid_t pid) {
struct stat st;
_cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
_cleanup_free_ char *env = NULL;
@ -54,7 +54,7 @@ static void test_get_process_comm_one(pid_t pid) {
xsprintf(path, "/proc/"PID_FMT"/comm", pid);
if (stat(path, &st) == 0) {
assert_se(get_process_comm(pid, &a) >= 0);
assert_se(pid_get_comm(pid, &a) >= 0);
log_info("PID"PID_FMT" comm: '%s'", pid, a);
} else
log_warning("%s not exist.", path);
@ -99,15 +99,15 @@ static void test_get_process_comm_one(pid_t pid) {
log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i));
}
TEST(get_process_comm) {
TEST(pid_get_comm) {
if (saved_argc > 1) {
pid_t pid = 0;
(void) parse_pid(saved_argv[1], &pid);
test_get_process_comm_one(pid);
test_pid_get_comm_one(pid);
} else {
TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm_one(1));
test_get_process_comm_one(getpid());
TEST_REQ_RUNNING_SYSTEMD(test_pid_get_comm_one(1));
test_pid_get_comm_one(getpid());
}
}
@ -165,34 +165,34 @@ TEST(pid_get_cmdline) {
}
}
static void test_get_process_comm_escape_one(const char *input, const char *output) {
static void test_pid_get_comm_escape_one(const char *input, const char *output) {
_cleanup_free_ char *n = NULL;
log_debug("input: <%s> — output: <%s>", input, output);
assert_se(prctl(PR_SET_NAME, input) >= 0);
assert_se(get_process_comm(0, &n) >= 0);
assert_se(pid_get_comm(0, &n) >= 0);
log_debug("got: <%s>", n);
assert_se(streq_ptr(n, output));
}
TEST(get_process_comm_escape) {
TEST(pid_get_comm_escape) {
_cleanup_free_ char *saved = NULL;
assert_se(get_process_comm(0, &saved) >= 0);
assert_se(pid_get_comm(0, &saved) >= 0);
test_get_process_comm_escape_one("", "");
test_get_process_comm_escape_one("foo", "foo");
test_get_process_comm_escape_one("012345678901234", "012345678901234");
test_get_process_comm_escape_one("0123456789012345", "012345678901234");
test_get_process_comm_escape_one("äöüß", "\\303\\244\\303\\266\\303\\274\\303\\237");
test_get_process_comm_escape_one("xäöüß", "x\\303\\244\\303\\266\\303\\274\\303\\237");
test_get_process_comm_escape_one("xxäöüß", "xx\\303\\244\\303\\266\\303\\274\\303\\237");
test_get_process_comm_escape_one("xxxäöüß", "xxx\\303\\244\\303\\266\\303\\274\\303\\237");
test_get_process_comm_escape_one("xxxxäöüß", "xxxx\\303\\244\\303\\266\\303\\274\\303\\237");
test_get_process_comm_escape_one("xxxxxäöüß", "xxxxx\\303\\244\\303\\266\\303\\274\\303\\237");
test_pid_get_comm_escape_one("", "");
test_pid_get_comm_escape_one("foo", "foo");
test_pid_get_comm_escape_one("012345678901234", "012345678901234");
test_pid_get_comm_escape_one("0123456789012345", "012345678901234");
test_pid_get_comm_escape_one("äöüß", "\\303\\244\\303\\266\\303\\274\\303\\237");
test_pid_get_comm_escape_one("xäöüß", "x\\303\\244\\303\\266\\303\\274\\303\\237");
test_pid_get_comm_escape_one("xxäöüß", "xx\\303\\244\\303\\266\\303\\274\\303\\237");
test_pid_get_comm_escape_one("xxxäöüß", "xxx\\303\\244\\303\\266\\303\\274\\303\\237");
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_se(prctl(PR_SET_NAME, saved) >= 0);
}