2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <sys/types.h>
34 #include "signal-util.h"
35 #include "process-util.h"
37 int get_process_state(pid_t pid) {
41 _cleanup_free_ char *line = NULL;
45 p = procfs_file_alloca(pid, "stat");
47 r = read_one_line_file(p, &line);
53 p = strrchr(line, ')');
59 if (sscanf(p, " %c", &state) != 1)
62 return (unsigned char) state;
65 int get_process_comm(pid_t pid, char **name) {
72 p = procfs_file_alloca(pid, "comm");
74 r = read_one_line_file(p, name);
81 int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
82 _cleanup_fclose_ FILE *f = NULL;
90 p = procfs_file_alloca(pid, "cmdline");
99 if (max_length == 0) {
100 size_t len = 0, allocated = 0;
102 while ((c = getc(f)) != EOF) {
104 if (!GREEDY_REALLOC(r, allocated, len+2)) {
109 r[len++] = isprint(c) ? c : ' ';
119 r = new(char, max_length);
125 while ((c = getc(f)) != EOF) {
147 size_t n = MIN(left-1, 3U);
154 /* Kernel threads have no argv[] */
156 _cleanup_free_ char *t = NULL;
164 h = get_process_comm(pid, &t);
168 r = strjoin("[", t, "]", NULL);
177 int is_kernel_thread(pid_t pid) {
184 if (pid == 0 || pid == 1) /* pid 1, and we ourselves certainly aren't a kernel thread */
189 p = procfs_file_alloca(pid, "cmdline");
197 count = fread(&c, 1, 1, f);
201 /* Kernel threads have an empty cmdline */
204 return eof ? 1 : -errno;
209 /// UNNEEDED by elogind
211 int get_process_capeff(pid_t pid, char **capeff) {
218 p = procfs_file_alloca(pid, "status");
220 r = get_status_field(p, "\nCapEff:", capeff);
228 static int get_process_link_contents(const char *proc_file, char **name) {
234 r = readlink_malloc(proc_file, name);
243 int get_process_exe(pid_t pid, char **name) {
250 p = procfs_file_alloca(pid, "exe");
251 r = get_process_link_contents(p, name);
255 d = endswith(*name, " (deleted)");
262 /// UNNEEDED by elogind
264 static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
265 _cleanup_fclose_ FILE *f = NULL;
275 p = procfs_file_alloca(pid, "status");
283 FOREACH_LINE(line, f, return -errno) {
288 if (startswith(l, field)) {
290 l += strspn(l, WHITESPACE);
292 l[strcspn(l, WHITESPACE)] = 0;
294 return parse_uid(l, uid);
301 int get_process_uid(pid_t pid, uid_t *uid) {
302 return get_process_id(pid, "Uid:", uid);
305 int get_process_gid(pid_t pid, gid_t *gid) {
306 assert_cc(sizeof(uid_t) == sizeof(gid_t));
307 return get_process_id(pid, "Gid:", gid);
310 int get_process_cwd(pid_t pid, char **cwd) {
315 p = procfs_file_alloca(pid, "cwd");
317 return get_process_link_contents(p, cwd);
320 int get_process_root(pid_t pid, char **root) {
325 p = procfs_file_alloca(pid, "root");
327 return get_process_link_contents(p, root);
330 int get_process_environ(pid_t pid, char **env) {
331 _cleanup_fclose_ FILE *f = NULL;
332 _cleanup_free_ char *outcome = NULL;
335 size_t allocated = 0, sz = 0;
340 p = procfs_file_alloca(pid, "environ");
349 while ((c = fgetc(f)) != EOF) {
350 if (!GREEDY_REALLOC(outcome, allocated, sz + 5))
354 outcome[sz++] = '\n';
356 sz += cescape_char(c, outcome + sz);
360 outcome = strdup("");
373 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
375 _cleanup_free_ char *line = NULL;
387 p = procfs_file_alloca(pid, "stat");
388 r = read_one_line_file(p, &line);
394 /* Let's skip the pid and comm fields. The latter is enclosed
395 * in () but does not escape any () in its value, so let's
396 * skip over it manually */
398 p = strrchr(line, ')');
410 if ((long unsigned) (pid_t) ppid != ppid)
413 *_ppid = (pid_t) ppid;
418 int wait_for_terminate(pid_t pid, siginfo_t *status) {
429 if (waitid(P_PID, pid, status, WEXITED) < 0) {
443 * < 0 : wait_for_terminate() failed to get the state of the
444 * process, the process was terminated by a signal, or
445 * failed for an unknown reason.
446 * >=0 : The process terminated normally, and its exit code is
449 * That is, success is indicated by a return value of zero, and an
450 * error is indicated by a non-zero value.
452 * A warning is emitted if the process terminates abnormally,
453 * and also if it returns non-zero unless check_exit_code is true.
455 int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code) {
462 r = wait_for_terminate(pid, &status);
464 return log_warning_errno(r, "Failed to wait for %s: %m", name);
466 if (status.si_code == CLD_EXITED) {
467 if (status.si_status != 0)
468 log_full(check_exit_code ? LOG_WARNING : LOG_DEBUG,
469 "%s failed with error code %i.", name, status.si_status);
471 log_debug("%s succeeded.", name);
473 return status.si_status;
474 } else if (status.si_code == CLD_KILLED ||
475 status.si_code == CLD_DUMPED) {
477 log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status));
481 log_warning("%s failed due to unknown reason.", name);
485 /// UNNEEDED by elogind
487 int kill_and_sigcont(pid_t pid, int sig) {
490 r = kill(pid, sig) < 0 ? -errno : 0;
499 int getenv_for_pid(pid_t pid, const char *field, char **_value) {
500 _cleanup_fclose_ FILE *f = NULL;
511 path = procfs_file_alloca(pid, "environ");
513 f = fopen(path, "re");
527 for (i = 0; i < sizeof(line)-1; i++) {
531 if (_unlikely_(c == EOF)) {
541 if (memcmp(line, field, l) == 0 && line[l] == '=') {
542 value = strdup(line + l + 1);
556 bool pid_is_unwaited(pid_t pid) {
557 /* Checks whether a PID is still valid at all, including a zombie */
562 if (kill(pid, 0) >= 0)
565 return errno != ESRCH;
568 bool pid_is_alive(pid_t pid) {
571 /* Checks whether a PID is still valid and not a zombie */
576 r = get_process_state(pid);
577 if (r == -ESRCH || r == 'Z')