_cleanup_set_free_ Set *allocated_set = NULL;
bool done = false;
int r, ret = 0, ret_log_kill = 0;
- pid_t my_pid;
assert(sig >= 0);
return -ENOMEM;
}
- my_pid = getpid_cached();
-
do {
_cleanup_fclose_ FILE *f = NULL;
pid_t pid = 0;
return ret;
}
- while ((r = cg_read_pid(f, &pid)) > 0) {
+ for (;;) {
+ _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
+
+ r = cg_read_pidref(f, &pidref);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ break;
- if ((flags & CGROUP_IGNORE_SELF) && pid == my_pid)
+ if ((flags & CGROUP_IGNORE_SELF) && pidref_is_self(&pidref))
continue;
- if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid))
+ if (set_get(s, PID_TO_PTR(pidref.pid)) == PID_TO_PTR(pidref.pid))
continue;
if (log_kill)
- ret_log_kill = log_kill(pid, sig, userdata);
+ ret_log_kill = log_kill(&pidref, sig, userdata);
- /* If we haven't killed this process yet, kill
- * it */
- if (kill(pid, sig) < 0) {
- if (ret >= 0 && errno != ESRCH)
- ret = -errno;
+ /* If we haven't killed this process yet, kill it */
+ r = pidref_kill(&pidref, sig);
+ if (r < 0) {
+ if (ret >= 0 && r != -ESRCH)
+ ret = r;
} else {
if (flags & CGROUP_SIGCONT)
- (void) kill(pid, SIGCONT);
+ (void) pidref_kill(&pidref, SIGCONT);
if (ret == 0) {
if (log_kill)
return TAKE_PTR(pid_set);
}
-static int kill_common_log(pid_t pid, int signo, void *userdata) {
+static int kill_common_log(const PidRef *pid, int signo, void *userdata) {
_cleanup_free_ char *comm = NULL;
Unit *u = ASSERT_PTR(userdata);
- (void) pid_get_comm(pid, &comm);
+ (void) pidref_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));
+ signal_to_string(signo), pid->pid, strna(comm));
return 1;
}
if (pidref_is_set(main_pid) &&
IN_SET(who, KILL_MAIN, KILL_MAIN_FAIL, KILL_ALL, KILL_ALL_FAIL)) {
-
_cleanup_free_ char *comm = NULL;
(void) pidref_get_comm(main_pid, &comm);
return 0;
}
-static int log_kill(pid_t pid, int sig, void *userdata) {
+static int log_kill(const PidRef *pid, int sig, void *userdata) {
_cleanup_free_ char *comm = NULL;
- (void) pid_get_comm(pid, &comm);
+ assert(pidref_is_set(pid));
+
+ (void) pidref_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. */
log_unit_notice(userdata,
"Killing process " PID_FMT " (%s) with signal SIG%s.",
- pid,
+ pid->pid,
strna(comm),
signal_to_string(sig));
if (pidref_is_set(main_pid)) {
if (log_func)
- log_func(main_pid->pid, sig, u);
+ log_func(main_pid, sig, u);
r = pidref_kill_and_sigcont(main_pid, sig);
if (r < 0 && r != -ESRCH) {
if (pidref_is_set(control_pid)) {
if (log_func)
- log_func(control_pid->pid, sig, u);
+ log_func(control_pid, sig, u);
r = pidref_kill_and_sigcont(control_pid, sig);
if (r < 0 && r != -ESRCH) {
return comm && comm[0] == '('; /* Most likely our own helper process (PAM?), ignore */
}
-int unit_log_leftover_process_start(pid_t pid, int sig, void *userdata) {
+int unit_log_leftover_process_start(const PidRef *pid, int sig, void *userdata) {
_cleanup_free_ char *comm = NULL;
- (void) pid_get_comm(pid, &comm);
+ assert(pidref_is_set(pid));
+
+ (void) pidref_get_comm(pid, &comm);
if (ignore_leftover_process(comm))
return 0;
log_unit_warning(userdata,
"Found left-over process " PID_FMT " (%s) in control group while starting unit. Ignoring.\n"
"This usually indicates unclean termination of a previous run, or service implementation deficiencies.",
- pid, strna(comm));
+ pid->pid, strna(comm));
return 1;
}
-int unit_log_leftover_process_stop(pid_t pid, int sig, void *userdata) {
+int unit_log_leftover_process_stop(const PidRef *pid, int sig, void *userdata) {
_cleanup_free_ char *comm = NULL;
- (void) pid_get_comm(pid, &comm);
+ assert(pidref_is_set(pid));
+
+ (void) pidref_get_comm(pid, &comm);
if (ignore_leftover_process(comm))
return 0;
log_unit_info(userdata,
"Unit process " PID_FMT " (%s) remains running after unit stopped.",
- pid, strna(comm));
+ pid->pid, strna(comm));
return 1;
}