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).",
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;
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,
/* 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;
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
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);
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);
_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),
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));
_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));
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) {
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) {
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. */
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 {
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 {
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;
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;
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");
/* 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;
}
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)
/* 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;
}
/* 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;
}
if (ucred) {
if (!identifier) {
- (void) get_process_comm(ucred->pid, &ident_buf);
+ (void) pid_get_comm(ucred->pid, &ident_buf);
identifier = ident_buf;
}
}
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;
* - 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);
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;
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,
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)
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 */
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.",
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);
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.",
_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 "
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));
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));
}
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);
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\".",
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);
}
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);
}
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
#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;
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);
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());
}
}
}
}
-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);
-
- 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");
+ assert_se(pid_get_comm(0, &saved) >= 0);
+
+ 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);
}