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");
46 r = read_one_line_file(p, &line);
50 p = strrchr(line, ')');
56 if (sscanf(p, " %c", &state) != 1)
59 return (unsigned char) state;
62 int get_process_comm(pid_t pid, char **name) {
69 p = procfs_file_alloca(pid, "comm");
71 r = read_one_line_file(p, name);
78 int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
79 _cleanup_fclose_ FILE *f = NULL;
87 p = procfs_file_alloca(pid, "cmdline");
93 if (max_length == 0) {
94 size_t len = 0, allocated = 0;
96 while ((c = getc(f)) != EOF) {
98 if (!GREEDY_REALLOC(r, allocated, len+2)) {
103 r[len++] = isprint(c) ? c : ' ';
113 r = new(char, max_length);
119 while ((c = getc(f)) != EOF) {
141 size_t n = MIN(left-1, 3U);
148 /* Kernel threads have no argv[] */
150 _cleanup_free_ char *t = NULL;
158 h = get_process_comm(pid, &t);
162 r = strjoin("[", t, "]", NULL);
171 int is_kernel_thread(pid_t pid) {
183 p = procfs_file_alloca(pid, "cmdline");
188 count = fread(&c, 1, 1, f);
192 /* Kernel threads have an empty cmdline */
195 return eof ? 1 : -errno;
200 /// UNNEEDED by elogind
202 int get_process_capeff(pid_t pid, char **capeff) {
208 p = procfs_file_alloca(pid, "status");
210 return get_status_field(p, "\nCapEff:", capeff);
214 static int get_process_link_contents(const char *proc_file, char **name) {
220 r = readlink_malloc(proc_file, name);
222 return r == -ENOENT ? -ESRCH : r;
227 int get_process_exe(pid_t pid, char **name) {
234 p = procfs_file_alloca(pid, "exe");
235 r = get_process_link_contents(p, name);
239 d = endswith(*name, " (deleted)");
246 /// UNNEEDED by elogind
248 static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
249 _cleanup_fclose_ FILE *f = NULL;
259 p = procfs_file_alloca(pid, "status");
264 FOREACH_LINE(line, f, return -errno) {
269 if (startswith(l, field)) {
271 l += strspn(l, WHITESPACE);
273 l[strcspn(l, WHITESPACE)] = 0;
275 return parse_uid(l, uid);
282 int get_process_uid(pid_t pid, uid_t *uid) {
283 return get_process_id(pid, "Uid:", uid);
286 int get_process_gid(pid_t pid, gid_t *gid) {
287 assert_cc(sizeof(uid_t) == sizeof(gid_t));
288 return get_process_id(pid, "Gid:", gid);
291 int get_process_cwd(pid_t pid, char **cwd) {
296 p = procfs_file_alloca(pid, "cwd");
298 return get_process_link_contents(p, cwd);
301 int get_process_root(pid_t pid, char **root) {
306 p = procfs_file_alloca(pid, "root");
308 return get_process_link_contents(p, root);
311 int get_process_environ(pid_t pid, char **env) {
312 _cleanup_fclose_ FILE *f = NULL;
313 _cleanup_free_ char *outcome = NULL;
316 size_t allocated = 0, sz = 0;
321 p = procfs_file_alloca(pid, "environ");
327 while ((c = fgetc(f)) != EOF) {
328 if (!GREEDY_REALLOC(outcome, allocated, sz + 5))
332 outcome[sz++] = '\n';
334 sz += cescape_char(c, outcome + sz);
345 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
347 _cleanup_free_ char *line = NULL;
359 p = procfs_file_alloca(pid, "stat");
360 r = read_one_line_file(p, &line);
364 /* Let's skip the pid and comm fields. The latter is enclosed
365 * in () but does not escape any () in its value, so let's
366 * skip over it manually */
368 p = strrchr(line, ')');
380 if ((long unsigned) (pid_t) ppid != ppid)
383 *_ppid = (pid_t) ppid;
388 int wait_for_terminate(pid_t pid, siginfo_t *status) {
399 if (waitid(P_PID, pid, status, WEXITED) < 0) {
413 * < 0 : wait_for_terminate() failed to get the state of the
414 * process, the process was terminated by a signal, or
415 * failed for an unknown reason.
416 * >=0 : The process terminated normally, and its exit code is
419 * That is, success is indicated by a return value of zero, and an
420 * error is indicated by a non-zero value.
422 * A warning is emitted if the process terminates abnormally,
423 * and also if it returns non-zero unless check_exit_code is true.
425 int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code) {
432 r = wait_for_terminate(pid, &status);
434 return log_warning_errno(r, "Failed to wait for %s: %m", name);
436 if (status.si_code == CLD_EXITED) {
437 if (status.si_status != 0)
438 log_full(check_exit_code ? LOG_WARNING : LOG_DEBUG,
439 "%s failed with error code %i.", name, status.si_status);
441 log_debug("%s succeeded.", name);
443 return status.si_status;
444 } else if (status.si_code == CLD_KILLED ||
445 status.si_code == CLD_DUMPED) {
447 log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status));
451 log_warning("%s failed due to unknown reason.", name);
455 /// UNNEEDED by elogind
457 int kill_and_sigcont(pid_t pid, int sig) {
460 r = kill(pid, sig) < 0 ? -errno : 0;
469 int getenv_for_pid(pid_t pid, const char *field, char **_value) {
470 _cleanup_fclose_ FILE *f = NULL;
481 path = procfs_file_alloca(pid, "environ");
483 f = fopen(path, "re");
494 for (i = 0; i < sizeof(line)-1; i++) {
498 if (_unlikely_(c == EOF)) {
508 if (memcmp(line, field, l) == 0 && line[l] == '=') {
509 value = strdup(line + l + 1);
523 bool pid_is_unwaited(pid_t pid) {
524 /* Checks whether a PID is still valid at all, including a zombie */
529 if (kill(pid, 0) >= 0)
532 return errno != ESRCH;
535 bool pid_is_alive(pid_t pid) {
538 /* Checks whether a PID is still valid and not a zombie */
543 r = get_process_state(pid);
544 if (r == -ENOENT || r == 'Z')