From 901cf821ac8c13c78e1acb884f59baf41316fb8f Mon Sep 17 00:00:00 2001 From: Ronny Chevalier Date: Fri, 10 Apr 2015 19:10:00 +0200 Subject: [PATCH] shared: add process-util.[ch] --- src/libelogind/sd-bus/bus-container.c | 1 + src/libelogind/sd-bus/bus-creds.c | 2 + src/login/inhibit.c | 2 + src/login/loginctl.c | 1 + src/login/logind-action.c | 2 + src/login/logind-dbus.c | 9 + src/shared/audit.c | 1 + src/shared/cgroup-show.c | 2 + src/shared/cgroup-util.c | 2 + src/shared/log.c | 2 + src/shared/logs-show.c | 2 + src/shared/machine-pool.c | 1 + src/shared/pager.c | 1 + src/shared/process-util.c | 538 ++++++++++++++++++++++++++ src/shared/process-util.h | 65 ++++ src/shared/smack-util.c | 1 + src/shared/spawn-ask-password-agent.c | 1 + src/shared/spawn-polkit-agent.c | 1 + src/shared/util.c | 529 +------------------------ src/shared/util.h | 41 +- src/shared/virt.c | 1 + src/test/test-process-util.c | 138 +++++++ 22 files changed, 777 insertions(+), 566 deletions(-) create mode 100644 src/shared/process-util.c create mode 100644 src/shared/process-util.h create mode 100644 src/test/test-process-util.c diff --git a/src/libelogind/sd-bus/bus-container.c b/src/libelogind/sd-bus/bus-container.c index 3b3a5d357..f157c25bb 100644 --- a/src/libelogind/sd-bus/bus-container.c +++ b/src/libelogind/sd-bus/bus-container.c @@ -23,6 +23,7 @@ #include #include "util.h" +#include "process-util.h" #include "bus-internal.h" #include "bus-socket.h" #include "bus-container.h" diff --git a/src/libelogind/sd-bus/bus-creds.c b/src/libelogind/sd-bus/bus-creds.c index aea379d66..603bc7320 100644 --- a/src/libelogind/sd-bus/bus-creds.c +++ b/src/libelogind/sd-bus/bus-creds.c @@ -23,6 +23,8 @@ #include #include "util.h" +#include "formats-util.h" +#include "process-util.h" #include "capability.h" #include "cgroup-util.h" #include "fileio.h" diff --git a/src/login/inhibit.c b/src/login/inhibit.c index 9f349b7c7..57cfb5d0b 100644 --- a/src/login/inhibit.c +++ b/src/login/inhibit.c @@ -31,6 +31,8 @@ #include "util.h" #include "build.h" #include "strv.h" +#include "formats-util.h" +#include "process-util.h" static const char* arg_what = "idle:sleep:shutdown"; static const char* arg_who = NULL; diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 16d80ab57..4ca51f676 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -42,6 +42,7 @@ #include "cgroup-util.h" #include "spawn-polkit-agent.h" #include "verbs.h" +#include "process-util.h" static char **arg_property = NULL; static bool arg_all = false; diff --git a/src/login/logind-action.c b/src/login/logind-action.c index d7b1538a3..c83d51d54 100644 --- a/src/login/logind-action.c +++ b/src/login/logind-action.c @@ -35,6 +35,8 @@ #include "bus-error.h" #include "bus-util.h" #include "logind-action.h" +#include "formats-util.h" +#include "process-util.h" int manager_handle_action( Manager *m, diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index a6cc9be66..7245ee7ab 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -40,6 +40,8 @@ #include "selinux-util.h" #include "efivars.h" #include "logind.h" +#include "formats-util.h" +#include "process-util.h" int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret) { _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; @@ -1526,6 +1528,7 @@ static int method_do_shutdown_or_sleep( const char *action_multiple_sessions, const char *action_ignore_inhibit, const char *sleep_verb, + sd_bus_message_handler_t method, sd_bus_error *error) { _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; @@ -1540,6 +1543,7 @@ static int method_do_shutdown_or_sleep( assert(action); assert(action_multiple_sessions); assert(action_ignore_inhibit); + assert(method); r = sd_bus_message_read(message, "b", &interactive); if (r < 0) @@ -1615,6 +1619,7 @@ static int method_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata, "org.freedesktop.login1.power-off-multiple-sessions", "org.freedesktop.login1.power-off-ignore-inhibit", NULL, + method_poweroff, error); } @@ -1629,6 +1634,7 @@ static int method_reboot(sd_bus *bus, sd_bus_message *message, void *userdata, s "org.freedesktop.login1.reboot-multiple-sessions", "org.freedesktop.login1.reboot-ignore-inhibit", NULL, + method_reboot, error); } @@ -1643,6 +1649,7 @@ static int method_suspend(sd_bus *bus, sd_bus_message *message, void *userdata, "org.freedesktop.login1.suspend-multiple-sessions", "org.freedesktop.login1.suspend-ignore-inhibit", "suspend", + method_suspend, error); } @@ -1657,6 +1664,7 @@ static int method_hibernate(sd_bus *bus, sd_bus_message *message, void *userdata "org.freedesktop.login1.hibernate-multiple-sessions", "org.freedesktop.login1.hibernate-ignore-inhibit", "hibernate", + method_hibernate, error); } @@ -1671,6 +1679,7 @@ static int method_hybrid_sleep(sd_bus *bus, sd_bus_message *message, void *userd "org.freedesktop.login1.hibernate-multiple-sessions", "org.freedesktop.login1.hibernate-ignore-inhibit", "hybrid-sleep", + method_hybrid_sleep, error); } diff --git a/src/shared/audit.c b/src/shared/audit.c index 4c1496f60..84181d332 100644 --- a/src/shared/audit.c +++ b/src/shared/audit.c @@ -25,6 +25,7 @@ #include "macro.h" #include "audit.h" #include "util.h" +#include "process-util.h" #include "fileio.h" int audit_session_from_pid(pid_t pid, uint32_t *id) { diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c index cbd94e86d..0f263e958 100644 --- a/src/shared/cgroup-show.c +++ b/src/shared/cgroup-show.c @@ -25,6 +25,8 @@ #include #include "util.h" +#include "formats-util.h" +#include "process-util.h" #include "macro.h" #include "path-util.h" #include "cgroup-util.h" diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 2e2984a35..63166a3a9 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -33,6 +33,8 @@ #include "set.h" #include "macro.h" #include "util.h" +#include "formats-util.h" +#include "process-util.h" #include "path-util.h" #include "fileio.h" #include "special.h" diff --git a/src/shared/log.c b/src/shared/log.c index 646a1d638..7edcf1f18 100644 --- a/src/shared/log.c +++ b/src/shared/log.c @@ -34,6 +34,8 @@ #include "missing.h" #include "macro.h" #include "socket-util.h" +#include "formats-util.h" +#include "process-util.h" #define SNDBUF_SIZE (8*1024*1024) diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index 4e1b87814..7a7a1e934 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -31,6 +31,8 @@ #include "utf8.h" #include "hashmap.h" #include "journal-internal.h" +#include "formats-util.h" +#include "process-util.h" /* up to three lines (each up to 100 characters), or 300 characters, whichever is less */ diff --git a/src/shared/machine-pool.c b/src/shared/machine-pool.c index 0fae62394..41aa1b7ac 100644 --- a/src/shared/machine-pool.c +++ b/src/shared/machine-pool.c @@ -25,6 +25,7 @@ #include #include "util.h" +#include "process-util.h" #include "mkdir.h" #include "btrfs-util.h" #include "path-util.h" diff --git a/src/shared/pager.c b/src/shared/pager.c index f12bfb328..b5a584ba0 100644 --- a/src/shared/pager.c +++ b/src/shared/pager.c @@ -27,6 +27,7 @@ #include "pager.h" #include "util.h" +#include "process-util.h" #include "macro.h" static pid_t pager_pid = 0; diff --git a/src/shared/process-util.c b/src/shared/process-util.c new file mode 100644 index 000000000..92a69f50e --- /dev/null +++ b/src/shared/process-util.c @@ -0,0 +1,538 @@ +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "process-util.h" +#include "fileio.h" +#include "util.h" +#include "log.h" + +int get_process_state(pid_t pid) { + const char *p; + char state; + int r; + _cleanup_free_ char *line = NULL; + + assert(pid >= 0); + + p = procfs_file_alloca(pid, "stat"); + r = read_one_line_file(p, &line); + if (r < 0) + return r; + + p = strrchr(line, ')'); + if (!p) + return -EIO; + + p++; + + if (sscanf(p, " %c", &state) != 1) + return -EIO; + + return (unsigned char) state; +} + +int get_process_comm(pid_t pid, char **name) { + const char *p; + int r; + + assert(name); + assert(pid >= 0); + + p = procfs_file_alloca(pid, "comm"); + + r = read_one_line_file(p, name); + if (r == -ENOENT) + return -ESRCH; + + return r; +} + +int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) { + _cleanup_fclose_ FILE *f = NULL; + char *r = NULL, *k; + const char *p; + int c; + + assert(line); + assert(pid >= 0); + + p = procfs_file_alloca(pid, "cmdline"); + + f = fopen(p, "re"); + if (!f) + return -errno; + + if (max_length == 0) { + size_t len = 0, allocated = 0; + + while ((c = getc(f)) != EOF) { + + if (!GREEDY_REALLOC(r, allocated, len+2)) { + free(r); + return -ENOMEM; + } + + r[len++] = isprint(c) ? c : ' '; + } + + if (len > 0) + r[len-1] = 0; + + } else { + bool space = false; + size_t left; + + r = new(char, max_length); + if (!r) + return -ENOMEM; + + k = r; + left = max_length; + while ((c = getc(f)) != EOF) { + + if (isprint(c)) { + if (space) { + if (left <= 4) + break; + + *(k++) = ' '; + left--; + space = false; + } + + if (left <= 4) + break; + + *(k++) = (char) c; + left--; + } else + space = true; + } + + if (left <= 4) { + size_t n = MIN(left-1, 3U); + memcpy(k, "...", n); + k[n] = 0; + } else + *k = 0; + } + + /* Kernel threads have no argv[] */ + if (isempty(r)) { + _cleanup_free_ char *t = NULL; + int h; + + free(r); + + if (!comm_fallback) + return -ENOENT; + + h = get_process_comm(pid, &t); + if (h < 0) + return h; + + r = strjoin("[", t, "]", NULL); + if (!r) + return -ENOMEM; + } + + *line = r; + return 0; +} + +int is_kernel_thread(pid_t pid) { + const char *p; + size_t count; + char c; + bool eof; + FILE *f; + + if (pid == 0) + return 0; + + assert(pid > 0); + + p = procfs_file_alloca(pid, "cmdline"); + f = fopen(p, "re"); + if (!f) + return -errno; + + count = fread(&c, 1, 1, f); + eof = feof(f); + fclose(f); + + /* Kernel threads have an empty cmdline */ + + if (count <= 0) + return eof ? 1 : -errno; + + return 0; +} + +int get_process_capeff(pid_t pid, char **capeff) { + const char *p; + + assert(capeff); + assert(pid >= 0); + + p = procfs_file_alloca(pid, "status"); + + return get_status_field(p, "\nCapEff:", capeff); +} + +static int get_process_link_contents(const char *proc_file, char **name) { + int r; + + assert(proc_file); + assert(name); + + r = readlink_malloc(proc_file, name); + if (r < 0) + return r == -ENOENT ? -ESRCH : r; + + return 0; +} + +int get_process_exe(pid_t pid, char **name) { + const char *p; + char *d; + int r; + + assert(pid >= 0); + + p = procfs_file_alloca(pid, "exe"); + r = get_process_link_contents(p, name); + if (r < 0) + return r; + + d = endswith(*name, " (deleted)"); + if (d) + *d = '\0'; + + return 0; +} + +static int get_process_id(pid_t pid, const char *field, uid_t *uid) { + _cleanup_fclose_ FILE *f = NULL; + char line[LINE_MAX]; + const char *p; + + assert(field); + assert(uid); + + if (pid == 0) + return getuid(); + + p = procfs_file_alloca(pid, "status"); + f = fopen(p, "re"); + if (!f) + return -errno; + + FOREACH_LINE(line, f, return -errno) { + char *l; + + l = strstrip(line); + + if (startswith(l, field)) { + l += strlen(field); + l += strspn(l, WHITESPACE); + + l[strcspn(l, WHITESPACE)] = 0; + + return parse_uid(l, uid); + } + } + + return -EIO; +} + +int get_process_uid(pid_t pid, uid_t *uid) { + return get_process_id(pid, "Uid:", uid); +} + +int get_process_gid(pid_t pid, gid_t *gid) { + assert_cc(sizeof(uid_t) == sizeof(gid_t)); + return get_process_id(pid, "Gid:", gid); +} + +int get_process_cwd(pid_t pid, char **cwd) { + const char *p; + + assert(pid >= 0); + + p = procfs_file_alloca(pid, "cwd"); + + return get_process_link_contents(p, cwd); +} + +int get_process_root(pid_t pid, char **root) { + const char *p; + + assert(pid >= 0); + + p = procfs_file_alloca(pid, "root"); + + return get_process_link_contents(p, root); +} + +int get_process_environ(pid_t pid, char **env) { + _cleanup_fclose_ FILE *f = NULL; + _cleanup_free_ char *outcome = NULL; + int c; + const char *p; + size_t allocated = 0, sz = 0; + + assert(pid >= 0); + assert(env); + + p = procfs_file_alloca(pid, "environ"); + + f = fopen(p, "re"); + if (!f) + return -errno; + + while ((c = fgetc(f)) != EOF) { + if (!GREEDY_REALLOC(outcome, allocated, sz + 5)) + return -ENOMEM; + + if (c == '\0') + outcome[sz++] = '\n'; + else + sz += cescape_char(c, outcome + sz); + } + + outcome[sz] = '\0'; + *env = outcome; + outcome = NULL; + + return 0; +} + +int get_parent_of_pid(pid_t pid, pid_t *_ppid) { + int r; + _cleanup_free_ char *line = NULL; + long unsigned ppid; + const char *p; + + assert(pid >= 0); + assert(_ppid); + + if (pid == 0) { + *_ppid = getppid(); + return 0; + } + + p = procfs_file_alloca(pid, "stat"); + r = read_one_line_file(p, &line); + if (r < 0) + return r; + + /* Let's skip the pid and comm fields. The latter is enclosed + * in () but does not escape any () in its value, so let's + * skip over it manually */ + + p = strrchr(line, ')'); + if (!p) + return -EIO; + + p++; + + if (sscanf(p, " " + "%*c " /* state */ + "%lu ", /* ppid */ + &ppid) != 1) + return -EIO; + + if ((long unsigned) (pid_t) ppid != ppid) + return -ERANGE; + + *_ppid = (pid_t) ppid; + + return 0; +} + +int wait_for_terminate(pid_t pid, siginfo_t *status) { + siginfo_t dummy; + + assert(pid >= 1); + + if (!status) + status = &dummy; + + for (;;) { + zero(*status); + + if (waitid(P_PID, pid, status, WEXITED) < 0) { + + if (errno == EINTR) + continue; + + return -errno; + } + + return 0; + } +} + +/* + * Return values: + * < 0 : wait_for_terminate() failed to get the state of the + * process, the process was terminated by a signal, or + * failed for an unknown reason. + * >=0 : The process terminated normally, and its exit code is + * returned. + * + * That is, success is indicated by a return value of zero, and an + * error is indicated by a non-zero value. + * + * A warning is emitted if the process terminates abnormally, + * and also if it returns non-zero unless check_exit_code is true. + */ +int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code) { + int r; + siginfo_t status; + + assert(name); + assert(pid > 1); + + r = wait_for_terminate(pid, &status); + if (r < 0) + return log_warning_errno(r, "Failed to wait for %s: %m", name); + + if (status.si_code == CLD_EXITED) { + if (status.si_status != 0) + log_full(check_exit_code ? LOG_WARNING : LOG_DEBUG, + "%s failed with error code %i.", name, status.si_status); + else + log_debug("%s succeeded.", name); + + return status.si_status; + } else if (status.si_code == CLD_KILLED || + status.si_code == CLD_DUMPED) { + + log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status)); + return -EPROTO; + } + + log_warning("%s failed due to unknown reason.", name); + return -EPROTO; +} + +int kill_and_sigcont(pid_t pid, int sig) { + int r; + + r = kill(pid, sig) < 0 ? -errno : 0; + + if (r >= 0) + kill(pid, SIGCONT); + + return r; +} + +int getenv_for_pid(pid_t pid, const char *field, char **_value) { + _cleanup_fclose_ FILE *f = NULL; + char *value = NULL; + int r; + bool done = false; + size_t l; + const char *path; + + assert(pid >= 0); + assert(field); + assert(_value); + + path = procfs_file_alloca(pid, "environ"); + + f = fopen(path, "re"); + if (!f) + return -errno; + + l = strlen(field); + r = 0; + + do { + char line[LINE_MAX]; + unsigned i; + + for (i = 0; i < sizeof(line)-1; i++) { + int c; + + c = getc(f); + if (_unlikely_(c == EOF)) { + done = true; + break; + } else if (c == 0) + break; + + line[i] = c; + } + line[i] = 0; + + if (memcmp(line, field, l) == 0 && line[l] == '=') { + value = strdup(line + l + 1); + if (!value) + return -ENOMEM; + + r = 1; + break; + } + + } while (!done); + + *_value = value; + return r; +} + +bool pid_is_unwaited(pid_t pid) { + /* Checks whether a PID is still valid at all, including a zombie */ + + if (pid <= 0) + return false; + + if (kill(pid, 0) >= 0) + return true; + + return errno != ESRCH; +} + +bool pid_is_alive(pid_t pid) { + int r; + + /* Checks whether a PID is still valid and not a zombie */ + + if (pid <= 0) + return false; + + r = get_process_state(pid); + if (r == -ENOENT || r == 'Z') + return false; + + return true; +} diff --git a/src/shared/process-util.h b/src/shared/process-util.h new file mode 100644 index 000000000..07431d043 --- /dev/null +++ b/src/shared/process-util.h @@ -0,0 +1,65 @@ +#pragma once + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include +#include +#include +#include + +#include "formats-util.h" + +#define procfs_file_alloca(pid, field) \ + ({ \ + pid_t _pid_ = (pid); \ + const char *_r_; \ + if (_pid_ == 0) { \ + _r_ = ("/proc/self/" field); \ + } else { \ + _r_ = alloca(strlen("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \ + sprintf((char*) _r_, "/proc/"PID_FMT"/" field, _pid_); \ + } \ + _r_; \ + }) + +int get_process_state(pid_t pid); +int get_process_comm(pid_t pid, char **name); +int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line); +int get_process_exe(pid_t pid, char **name); +int get_process_uid(pid_t pid, uid_t *uid); +int get_process_gid(pid_t pid, gid_t *gid); +int get_process_capeff(pid_t pid, char **capeff); +int get_process_cwd(pid_t pid, char **cwd); +int get_process_root(pid_t pid, char **root); +int get_process_environ(pid_t pid, char **environ); + +int wait_for_terminate(pid_t pid, siginfo_t *status); +int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code); + +int kill_and_sigcont(pid_t pid, int sig); +pid_t get_parent_of_pid(pid_t pid, pid_t *ppid); +void rename_process(const char name[8]); +int is_kernel_thread(pid_t pid); +int getenv_for_pid(pid_t pid, const char *field, char **_value); + +bool pid_is_alive(pid_t pid); +bool pid_is_unwaited(pid_t pid); diff --git a/src/shared/smack-util.c b/src/shared/smack-util.c index a73a996cf..2e24b1ea9 100644 --- a/src/shared/smack-util.c +++ b/src/shared/smack-util.c @@ -24,6 +24,7 @@ #include #include "util.h" +#include "process-util.h" #include "path-util.h" #include "fileio.h" #include "smack-util.h" diff --git a/src/shared/spawn-ask-password-agent.c b/src/shared/spawn-ask-password-agent.c index 0d7458806..70466d17e 100644 --- a/src/shared/spawn-ask-password-agent.c +++ b/src/shared/spawn-ask-password-agent.c @@ -25,6 +25,7 @@ #include "log.h" #include "util.h" +#include "process-util.h" #include "spawn-ask-password-agent.h" static pid_t agent_pid = 0; diff --git a/src/shared/spawn-polkit-agent.c b/src/shared/spawn-polkit-agent.c index bc1810da9..4db249e1c 100644 --- a/src/shared/spawn-polkit-agent.c +++ b/src/shared/spawn-polkit-agent.c @@ -27,6 +27,7 @@ #include "log.h" #include "util.h" +#include "process-util.h" #include "spawn-polkit-agent.h" #ifdef ENABLE_POLKIT diff --git a/src/shared/util.c b/src/shared/util.c index 6f0fb162d..863371c0d 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -92,6 +92,8 @@ #include "virt.h" #include "def.h" #include "sparse-endian.h" +#include "formats-util.h" +#include "process-util.h" /* Put this test here for a lack of better place */ assert_cc(EAGAIN == EWOULDBLOCK); @@ -150,27 +152,6 @@ char* endswith(const char *s, const char *postfix) { return (char*) s + sl - pl; } -char* endswith_no_case(const char *s, const char *postfix) { - size_t sl, pl; - - assert(s); - assert(postfix); - - sl = strlen(s); - pl = strlen(postfix); - - if (pl == 0) - return (char*) s + sl; - - if (sl < pl) - return NULL; - - if (strcasecmp(s + sl - pl, postfix) != 0) - return NULL; - - return (char*) s + sl - pl; -} - char* first_word(const char *s, const char *word) { size_t sl, wl; const char *p; @@ -205,7 +186,7 @@ char* first_word(const char *s, const char *word) { return (char*) p; } -static size_t cescape_char(char c, char *buf) { +size_t cescape_char(char c, char *buf) { char * buf_old = buf; switch (c) { @@ -618,49 +599,6 @@ const char* split(const char **state, size_t *l, const char *separator, bool quo return current; } -int get_parent_of_pid(pid_t pid, pid_t *_ppid) { - int r; - _cleanup_free_ char *line = NULL; - long unsigned ppid; - const char *p; - - assert(pid >= 0); - assert(_ppid); - - if (pid == 0) { - *_ppid = getppid(); - return 0; - } - - p = procfs_file_alloca(pid, "stat"); - r = read_one_line_file(p, &line); - if (r < 0) - return r; - - /* Let's skip the pid and comm fields. The latter is enclosed - * in () but does not escape any () in its value, so let's - * skip over it manually */ - - p = strrchr(line, ')'); - if (!p) - return -EIO; - - p++; - - if (sscanf(p, " " - "%*c " /* state */ - "%lu ", /* ppid */ - &ppid) != 1) - return -EIO; - - if ((long unsigned) (pid_t) ppid != ppid) - return -ERANGE; - - *_ppid = (pid_t) ppid; - - return 0; -} - int fchmod_umask(int fd, mode_t m) { mode_t u; int r; @@ -679,308 +617,6 @@ char *truncate_nl(char *s) { return s; } -int get_process_state(pid_t pid) { - const char *p; - char state; - int r; - _cleanup_free_ char *line = NULL; - - assert(pid >= 0); - - p = procfs_file_alloca(pid, "stat"); - r = read_one_line_file(p, &line); - if (r < 0) - return r; - - p = strrchr(line, ')'); - if (!p) - return -EIO; - - p++; - - if (sscanf(p, " %c", &state) != 1) - return -EIO; - - return (unsigned char) state; -} - -int get_process_comm(pid_t pid, char **name) { - const char *p; - int r; - - assert(name); - assert(pid >= 0); - - p = procfs_file_alloca(pid, "comm"); - - r = read_one_line_file(p, name); - if (r == -ENOENT) - return -ESRCH; - - return r; -} - -int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) { - _cleanup_fclose_ FILE *f = NULL; - char *r = NULL, *k; - const char *p; - int c; - - assert(line); - assert(pid >= 0); - - p = procfs_file_alloca(pid, "cmdline"); - - f = fopen(p, "re"); - if (!f) - return -errno; - - if (max_length == 0) { - size_t len = 0, allocated = 0; - - while ((c = getc(f)) != EOF) { - - if (!GREEDY_REALLOC(r, allocated, len+2)) { - free(r); - return -ENOMEM; - } - - r[len++] = isprint(c) ? c : ' '; - } - - if (len > 0) - r[len-1] = 0; - - } else { - bool space = false; - size_t left; - - r = new(char, max_length); - if (!r) - return -ENOMEM; - - k = r; - left = max_length; - while ((c = getc(f)) != EOF) { - - if (isprint(c)) { - if (space) { - if (left <= 4) - break; - - *(k++) = ' '; - left--; - space = false; - } - - if (left <= 4) - break; - - *(k++) = (char) c; - left--; - } else - space = true; - } - - if (left <= 4) { - size_t n = MIN(left-1, 3U); - memcpy(k, "...", n); - k[n] = 0; - } else - *k = 0; - } - - /* Kernel threads have no argv[] */ - if (isempty(r)) { - _cleanup_free_ char *t = NULL; - int h; - - free(r); - - if (!comm_fallback) - return -ENOENT; - - h = get_process_comm(pid, &t); - if (h < 0) - return h; - - r = strjoin("[", t, "]", NULL); - if (!r) - return -ENOMEM; - } - - *line = r; - return 0; -} - -int is_kernel_thread(pid_t pid) { - const char *p; - size_t count; - char c; - bool eof; - FILE *f; - - if (pid == 0) - return 0; - - assert(pid > 0); - - p = procfs_file_alloca(pid, "cmdline"); - f = fopen(p, "re"); - if (!f) - return -errno; - - count = fread(&c, 1, 1, f); - eof = feof(f); - fclose(f); - - /* Kernel threads have an empty cmdline */ - - if (count <= 0) - return eof ? 1 : -errno; - - return 0; -} - -int get_process_capeff(pid_t pid, char **capeff) { - const char *p; - - assert(capeff); - assert(pid >= 0); - - p = procfs_file_alloca(pid, "status"); - - return get_status_field(p, "\nCapEff:", capeff); -} - -static int get_process_link_contents(const char *proc_file, char **name) { - int r; - - assert(proc_file); - assert(name); - - r = readlink_malloc(proc_file, name); - if (r < 0) - return r == -ENOENT ? -ESRCH : r; - - return 0; -} - -int get_process_exe(pid_t pid, char **name) { - const char *p; - char *d; - int r; - - assert(pid >= 0); - - p = procfs_file_alloca(pid, "exe"); - r = get_process_link_contents(p, name); - if (r < 0) - return r; - - d = endswith(*name, " (deleted)"); - if (d) - *d = '\0'; - - return 0; -} - -static int get_process_id(pid_t pid, const char *field, uid_t *uid) { - _cleanup_fclose_ FILE *f = NULL; - char line[LINE_MAX]; - const char *p; - - assert(field); - assert(uid); - - if (pid == 0) - return getuid(); - - p = procfs_file_alloca(pid, "status"); - f = fopen(p, "re"); - if (!f) - return -errno; - - FOREACH_LINE(line, f, return -errno) { - char *l; - - l = strstrip(line); - - if (startswith(l, field)) { - l += strlen(field); - l += strspn(l, WHITESPACE); - - l[strcspn(l, WHITESPACE)] = 0; - - return parse_uid(l, uid); - } - } - - return -EIO; -} - -int get_process_uid(pid_t pid, uid_t *uid) { - return get_process_id(pid, "Uid:", uid); -} - -int get_process_gid(pid_t pid, gid_t *gid) { - assert_cc(sizeof(uid_t) == sizeof(gid_t)); - return get_process_id(pid, "Gid:", gid); -} - -int get_process_cwd(pid_t pid, char **cwd) { - const char *p; - - assert(pid >= 0); - - p = procfs_file_alloca(pid, "cwd"); - - return get_process_link_contents(p, cwd); -} - -int get_process_root(pid_t pid, char **root) { - const char *p; - - assert(pid >= 0); - - p = procfs_file_alloca(pid, "root"); - - return get_process_link_contents(p, root); -} - -int get_process_environ(pid_t pid, char **env) { - _cleanup_fclose_ FILE *f = NULL; - _cleanup_free_ char *outcome = NULL; - int c; - const char *p; - size_t allocated = 0, sz = 0; - - assert(pid >= 0); - assert(env); - - p = procfs_file_alloca(pid, "environ"); - - f = fopen(p, "re"); - if (!f) - return -errno; - - while ((c = fgetc(f)) != EOF) { - if (!GREEDY_REALLOC(outcome, allocated, sz + 5)) - return -ENOMEM; - - if (c == '\0') - outcome[sz++] = '\n'; - else - sz += cescape_char(c, outcome + sz); - } - - outcome[sz] = '\0'; - *env = outcome; - outcome = NULL; - - return 0; -} - char *strnappend(const char *s, const char *suffix, size_t b) { size_t a; char *r; @@ -3696,73 +3332,6 @@ static char *unquote(const char *s, const char* quotes) { return strdup(s); } -int wait_for_terminate(pid_t pid, siginfo_t *status) { - siginfo_t dummy; - - assert(pid >= 1); - - if (!status) - status = &dummy; - - for (;;) { - zero(*status); - - if (waitid(P_PID, pid, status, WEXITED) < 0) { - - if (errno == EINTR) - continue; - - return -errno; - } - - return 0; - } -} - -/* - * Return values: - * < 0 : wait_for_terminate() failed to get the state of the - * process, the process was terminated by a signal, or - * failed for an unknown reason. - * >=0 : The process terminated normally, and its exit code is - * returned. - * - * That is, success is indicated by a return value of zero, and an - * error is indicated by a non-zero value. - * - * A warning is emitted if the process terminates abnormally, - * and also if it returns non-zero unless check_exit_code is true. - */ -int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code) { - int r; - siginfo_t status; - - assert(name); - assert(pid > 1); - - r = wait_for_terminate(pid, &status); - if (r < 0) - return log_warning_errno(r, "Failed to wait for %s: %m", name); - - if (status.si_code == CLD_EXITED) { - if (status.si_status != 0) - log_full(check_exit_code ? LOG_WARNING : LOG_DEBUG, - "%s failed with error code %i.", name, status.si_status); - else - log_debug("%s succeeded.", name); - - return status.si_status; - } else if (status.si_code == CLD_KILLED || - status.si_code == CLD_DUMPED) { - - log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status)); - return -EPROTO; - } - - log_warning("%s failed due to unknown reason.", name); - return -EPROTO; -} - noreturn void freeze(void) { /* Make sure nobody waits for us on a socket anymore */ @@ -4139,17 +3708,6 @@ void execute_directories(const char* const* directories, usec_t timeout, char *a wait_for_terminate_and_warn(name, executor_pid, true); } -int kill_and_sigcont(pid_t pid, int sig) { - int r; - - r = kill(pid, sig) < 0 ? -errno : 0; - - if (r >= 0) - kill(pid, SIGCONT); - - return r; -} - bool nulstr_contains(const char*nulstr, const char *needle) { const char *i; @@ -5358,60 +4916,6 @@ int setrlimit_closest(int resource, const struct rlimit *rlim) { return 0; } -int getenv_for_pid(pid_t pid, const char *field, char **_value) { - _cleanup_fclose_ FILE *f = NULL; - char *value = NULL; - int r; - bool done = false; - size_t l; - const char *path; - - assert(pid >= 0); - assert(field); - assert(_value); - - path = procfs_file_alloca(pid, "environ"); - - f = fopen(path, "re"); - if (!f) - return -errno; - - l = strlen(field); - r = 0; - - do { - char line[LINE_MAX]; - unsigned i; - - for (i = 0; i < sizeof(line)-1; i++) { - int c; - - c = getc(f); - if (_unlikely_(c == EOF)) { - done = true; - break; - } else if (c == 0) - break; - - line[i] = c; - } - line[i] = 0; - - if (memcmp(line, field, l) == 0 && line[l] == '=') { - value = strdup(line + l + 1); - if (!value) - return -ENOMEM; - - r = 1; - break; - } - - } while (!done); - - *_value = value; - return r; -} - bool http_etag_is_valid(const char *etag) { if (isempty(etag)) return false; @@ -6517,33 +6021,6 @@ int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd) { return 0; } -bool pid_is_unwaited(pid_t pid) { - /* Checks whether a PID is still valid at all, including a zombie */ - - if (pid <= 0) - return false; - - if (kill(pid, 0) >= 0) - return true; - - return errno != ESRCH; -} - -bool pid_is_alive(pid_t pid) { - int r; - - /* Checks whether a PID is still valid and not a zombie */ - - if (pid <= 0) - return false; - - r = get_process_state(pid); - if (r == -ENOENT || r == 'Z') - return false; - - return true; -} - int getpeercred(int fd, struct ucred *ucred) { socklen_t n = sizeof(struct ucred); struct ucred u; diff --git a/src/shared/util.h b/src/shared/util.h index dc2611fa6..9271578d5 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -268,8 +268,6 @@ const char* split(const char **state, size_t *l, const char *separator, bool quo #define _FOREACH_WORD(word, length, s, separator, quoted, state) \ for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted))) -pid_t get_parent_of_pid(pid_t pid, pid_t *ppid); - char *strappend(const char *s, const char *suffix); char *strnappend(const char *s, const char *suffix, size_t length); @@ -293,17 +291,6 @@ char *file_in_same_dir(const char *path, const char *filename); int rmdir_parents(const char *path, const char *stop); -int get_process_state(pid_t pid); -int get_process_comm(pid_t pid, char **name); -int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line); -int get_process_exe(pid_t pid, char **name); -int get_process_uid(pid_t pid, uid_t *uid); -int get_process_gid(pid_t pid, gid_t *gid); -int get_process_capeff(pid_t pid, char **capeff); -int get_process_cwd(pid_t pid, char **cwd); -int get_process_root(pid_t pid, char **root); -int get_process_environ(pid_t pid, char **environ); - char hexchar(int x) _const_; int unhexchar(char c) _const_; char octchar(int x) _const_; @@ -312,6 +299,7 @@ char decchar(int x) _const_; int undecchar(char c) _const_; char *cescape(const char *s); +size_t cescape_char(char c, char *buf); typedef enum UnescapeFlags { UNESCAPE_RELAX = 1, @@ -447,8 +435,6 @@ bool is_device_path(const char *path); int dir_is_empty(const char *path); char* dirname_malloc(const char *path); -void rename_process(const char name[8]); - void sigset_add_many(sigset_t *ss, ...); int sigprocmask_many(int how, ...); @@ -523,9 +509,6 @@ char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigne int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode); int touch(const char *path); -int wait_for_terminate(pid_t pid, siginfo_t *status); -int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code); - noreturn void freeze(void); bool null_or_empty(struct stat *st) _pure_; @@ -545,8 +528,6 @@ const char *default_term_for_tty(const char *tty); void execute_directories(const char* const* directories, usec_t timeout, char *argv[]); -int kill_and_sigcont(pid_t pid, int sig); - bool nulstr_contains(const char*nulstr, const char *needle); bool plymouth_running(void); @@ -645,8 +626,6 @@ int fd_wait_for_event(int fd, int event, usec_t timeout); void* memdup(const void *p, size_t l) _alloc_(2); -int is_kernel_thread(pid_t pid); - int fd_inc_sndbuf(int fd, size_t n); int fd_inc_rcvbuf(int fd, size_t n); @@ -654,8 +633,6 @@ int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *pa int setrlimit_closest(int resource, const struct rlimit *rlim); -int getenv_for_pid(pid_t pid, const char *field, char **_value); - bool http_url_is_valid(const char *url) _pure_; bool documentation_url_is_valid(const char *url) _pure_; @@ -932,19 +909,6 @@ int unlink_noerrno(const char *path); _d_; \ }) -#define procfs_file_alloca(pid, field) \ - ({ \ - pid_t _pid_ = (pid); \ - const char *_r_; \ - if (_pid_ == 0) { \ - _r_ = ("/proc/self/" field); \ - } else { \ - _r_ = alloca(strlen("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \ - sprintf((char*) _r_, "/proc/"PID_FMT"/" field, _pid_); \ - } \ - _r_; \ - }) - bool id128_is_valid(const char *s) _pure_; int split_pair(const char *s, const char *sep, char **l, char **r); @@ -972,9 +936,6 @@ int container_get_leader(const char *machine, pid_t *pid); int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *root_fd); int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd); -bool pid_is_alive(pid_t pid); -bool pid_is_unwaited(pid_t pid); - int getpeercred(int fd, struct ucred *ucred); int getpeersec(int fd, char **ret); diff --git a/src/shared/virt.c b/src/shared/virt.c index 54c465520..1299a75ed 100644 --- a/src/shared/virt.c +++ b/src/shared/virt.c @@ -24,6 +24,7 @@ #include #include "util.h" +#include "process-util.h" #include "virt.h" #include "fileio.h" diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c new file mode 100644 index 000000000..a17ef144f --- /dev/null +++ b/src/test/test-process-util.c @@ -0,0 +1,138 @@ +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + Copyright 2013 Thomas H.P. Andersen + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include +#include + +#include "process-util.h" +#include "log.h" +#include "util.h" +#include "macro.h" +#include "virt.h" + +static void test_get_process_comm(void) { + struct stat st; + _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL, *cwd = NULL, *root = NULL; + _cleanup_free_ char *env = NULL; + pid_t e; + uid_t u; + gid_t g; + dev_t h; + int r; + pid_t me; + + if (stat("/proc/1/comm", &st) == 0) { + assert_se(get_process_comm(1, &a) >= 0); + log_info("pid1 comm: '%s'", a); + } else { + log_warning("/proc/1/comm does not exist."); + } + + assert_se(get_process_cmdline(1, 0, true, &c) >= 0); + log_info("pid1 cmdline: '%s'", c); + + assert_se(get_process_cmdline(1, 8, false, &d) >= 0); + log_info("pid1 cmdline truncated: '%s'", d); + + assert_se(get_parent_of_pid(1, &e) >= 0); + log_info("pid1 ppid: "PID_FMT, e); + assert_se(e == 0); + + assert_se(is_kernel_thread(1) == 0); + + r = get_process_exe(1, &f); + assert_se(r >= 0 || r == -EACCES); + log_info("pid1 exe: '%s'", strna(f)); + + assert_se(get_process_uid(1, &u) == 0); + log_info("pid1 uid: "UID_FMT, u); + assert_se(u == 0); + + assert_se(get_process_gid(1, &g) == 0); + log_info("pid1 gid: "GID_FMT, g); + assert_se(g == 0); + + me = getpid(); + + r = get_process_cwd(me, &cwd); + assert_se(r >= 0 || r == -EACCES); + log_info("pid1 cwd: '%s'", cwd); + + r = get_process_root(me, &root); + assert_se(r >= 0 || r == -EACCES); + log_info("pid1 root: '%s'", root); + + r = get_process_environ(me, &env); + assert_se(r >= 0 || r == -EACCES); + log_info("self strlen(environ): '%zu'", strlen(env)); + + if (!detect_container(NULL)) + assert_se(get_ctty_devnr(1, &h) == -ENOENT); + + getenv_for_pid(1, "PATH", &i); + log_info("pid1 $PATH: '%s'", strna(i)); +} + +static void test_pid_is_unwaited(void) { + pid_t pid; + + pid = fork(); + assert_se(pid >= 0); + if (pid == 0) { + _exit(EXIT_SUCCESS); + } else { + int status; + + waitpid(pid, &status, 0); + assert_se(!pid_is_unwaited(pid)); + } + assert_se(pid_is_unwaited(getpid())); + assert_se(!pid_is_unwaited(-1)); +} + +static void test_pid_is_alive(void) { + pid_t pid; + + pid = fork(); + assert_se(pid >= 0); + if (pid == 0) { + _exit(EXIT_SUCCESS); + } else { + int status; + + waitpid(pid, &status, 0); + assert_se(!pid_is_alive(pid)); + } + assert_se(pid_is_alive(getpid())); + assert_se(!pid_is_alive(-1)); +} + +int main(int argc, char *argv[]) { + log_parse_environment(); + log_open(); + + test_get_process_comm(); + test_pid_is_unwaited(); + test_pid_is_alive(); + + return 0; +} -- 2.30.2