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>
31 #include "process-util.h"
36 int get_process_state(pid_t pid) {
40 _cleanup_free_ char *line = NULL;
44 p = procfs_file_alloca(pid, "stat");
45 r = read_one_line_file(p, &line);
49 p = strrchr(line, ')');
55 if (sscanf(p, " %c", &state) != 1)
58 return (unsigned char) state;
61 int get_process_comm(pid_t pid, char **name) {
68 p = procfs_file_alloca(pid, "comm");
70 r = read_one_line_file(p, name);
77 int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
78 _cleanup_fclose_ FILE *f = NULL;
86 p = procfs_file_alloca(pid, "cmdline");
92 if (max_length == 0) {
93 size_t len = 0, allocated = 0;
95 while ((c = getc(f)) != EOF) {
97 if (!GREEDY_REALLOC(r, allocated, len+2)) {
102 r[len++] = isprint(c) ? c : ' ';
112 r = new(char, max_length);
118 while ((c = getc(f)) != EOF) {
140 size_t n = MIN(left-1, 3U);
147 /* Kernel threads have no argv[] */
149 _cleanup_free_ char *t = NULL;
157 h = get_process_comm(pid, &t);
161 r = strjoin("[", t, "]", NULL);
170 int is_kernel_thread(pid_t pid) {
182 p = procfs_file_alloca(pid, "cmdline");
187 count = fread(&c, 1, 1, f);
191 /* Kernel threads have an empty cmdline */
194 return eof ? 1 : -errno;
199 int get_process_capeff(pid_t pid, char **capeff) {
205 p = procfs_file_alloca(pid, "status");
207 return get_status_field(p, "\nCapEff:", capeff);
210 static int get_process_link_contents(const char *proc_file, char **name) {
216 r = readlink_malloc(proc_file, name);
218 return r == -ENOENT ? -ESRCH : r;
223 int get_process_exe(pid_t pid, char **name) {
230 p = procfs_file_alloca(pid, "exe");
231 r = get_process_link_contents(p, name);
235 d = endswith(*name, " (deleted)");
242 static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
243 _cleanup_fclose_ FILE *f = NULL;
253 p = procfs_file_alloca(pid, "status");
258 FOREACH_LINE(line, f, return -errno) {
263 if (startswith(l, field)) {
265 l += strspn(l, WHITESPACE);
267 l[strcspn(l, WHITESPACE)] = 0;
269 return parse_uid(l, uid);
276 int get_process_uid(pid_t pid, uid_t *uid) {
277 return get_process_id(pid, "Uid:", uid);
280 int get_process_gid(pid_t pid, gid_t *gid) {
281 assert_cc(sizeof(uid_t) == sizeof(gid_t));
282 return get_process_id(pid, "Gid:", gid);
285 int get_process_cwd(pid_t pid, char **cwd) {
290 p = procfs_file_alloca(pid, "cwd");
292 return get_process_link_contents(p, cwd);
295 int get_process_root(pid_t pid, char **root) {
300 p = procfs_file_alloca(pid, "root");
302 return get_process_link_contents(p, root);
305 int get_process_environ(pid_t pid, char **env) {
306 _cleanup_fclose_ FILE *f = NULL;
307 _cleanup_free_ char *outcome = NULL;
310 size_t allocated = 0, sz = 0;
315 p = procfs_file_alloca(pid, "environ");
321 while ((c = fgetc(f)) != EOF) {
322 if (!GREEDY_REALLOC(outcome, allocated, sz + 5))
326 outcome[sz++] = '\n';
328 sz += cescape_char(c, outcome + sz);
338 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
340 _cleanup_free_ char *line = NULL;
352 p = procfs_file_alloca(pid, "stat");
353 r = read_one_line_file(p, &line);
357 /* Let's skip the pid and comm fields. The latter is enclosed
358 * in () but does not escape any () in its value, so let's
359 * skip over it manually */
361 p = strrchr(line, ')');
373 if ((long unsigned) (pid_t) ppid != ppid)
376 *_ppid = (pid_t) ppid;
381 int wait_for_terminate(pid_t pid, siginfo_t *status) {
392 if (waitid(P_PID, pid, status, WEXITED) < 0) {
406 * < 0 : wait_for_terminate() failed to get the state of the
407 * process, the process was terminated by a signal, or
408 * failed for an unknown reason.
409 * >=0 : The process terminated normally, and its exit code is
412 * That is, success is indicated by a return value of zero, and an
413 * error is indicated by a non-zero value.
415 * A warning is emitted if the process terminates abnormally,
416 * and also if it returns non-zero unless check_exit_code is true.
418 int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code) {
425 r = wait_for_terminate(pid, &status);
427 return log_warning_errno(r, "Failed to wait for %s: %m", name);
429 if (status.si_code == CLD_EXITED) {
430 if (status.si_status != 0)
431 log_full(check_exit_code ? LOG_WARNING : LOG_DEBUG,
432 "%s failed with error code %i.", name, status.si_status);
434 log_debug("%s succeeded.", name);
436 return status.si_status;
437 } else if (status.si_code == CLD_KILLED ||
438 status.si_code == CLD_DUMPED) {
440 log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status));
444 log_warning("%s failed due to unknown reason.", name);
448 int kill_and_sigcont(pid_t pid, int sig) {
451 r = kill(pid, sig) < 0 ? -errno : 0;
459 int getenv_for_pid(pid_t pid, const char *field, char **_value) {
460 _cleanup_fclose_ FILE *f = NULL;
471 path = procfs_file_alloca(pid, "environ");
473 f = fopen(path, "re");
484 for (i = 0; i < sizeof(line)-1; i++) {
488 if (_unlikely_(c == EOF)) {
498 if (memcmp(line, field, l) == 0 && line[l] == '=') {
499 value = strdup(line + l + 1);
513 bool pid_is_unwaited(pid_t pid) {
514 /* Checks whether a PID is still valid at all, including a zombie */
519 if (kill(pid, 0) >= 0)
522 return errno != ESRCH;
525 bool pid_is_alive(pid_t pid) {
528 /* Checks whether a PID is still valid and not a zombie */
533 r = get_process_state(pid);
534 if (r == -ENOENT || r == 'Z')