summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRonny Chevalier <chevalier.ronny@gmail.com>2015-04-10 19:10:00 +0200
committerRonny Chevalier <chevalier.ronny@gmail.com>2015-04-10 23:54:49 +0200
commit0b452006de98294d1690f045f6ea2f7f6630ec3b (patch)
tree852bde2d8fd7dac434636bd25a276b5cf0eece9f
parent6482f6269c87d2249e52e889a63adbdd50f2d691 (diff)
shared: add process-util.[ch]
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am9
-rw-r--r--src/core/automount.c1
-rw-r--r--src/core/cgroup.c1
-rw-r--r--src/core/execute.c1
-rw-r--r--src/core/killall.c1
-rw-r--r--src/core/machine-id-setup.c1
-rw-r--r--src/core/main.c1
-rw-r--r--src/core/manager.c1
-rw-r--r--src/core/service.c1
-rw-r--r--src/core/shutdown.c1
-rw-r--r--src/core/unit.c1
-rw-r--r--src/delta/delta.c1
-rw-r--r--src/fsck/fsck.c1
-rw-r--r--src/getty-generator/getty-generator.c1
-rw-r--r--src/import/export-tar.c1
-rw-r--r--src/import/import-tar.c1
-rw-r--r--src/import/importd.c1
-rw-r--r--src/import/pull-common.c1
-rw-r--r--src/import/pull-dkr.c1
-rw-r--r--src/import/pull-tar.c1
-rw-r--r--src/journal/coredump.c1
-rw-r--r--src/journal/coredumpctl.c1
-rw-r--r--src/journal/journald-console.c1
-rw-r--r--src/journal/journald-kmsg.c1
-rw-r--r--src/journal/journald-server.c1
-rw-r--r--src/journal/journald-syslog.c1
-rw-r--r--src/journal/journald-wall.c1
-rw-r--r--src/libsystemd-network/test-pppoe.c1
-rw-r--r--src/libsystemd/sd-bus/bus-container.c1
-rw-r--r--src/libsystemd/sd-bus/bus-creds.c1
-rw-r--r--src/login/inhibit.c1
-rw-r--r--src/login/loginctl.c1
-rw-r--r--src/login/logind-action.c1
-rw-r--r--src/login/logind-dbus.c1
-rw-r--r--src/machine/machine-dbus.c1
-rw-r--r--src/machine/machinectl.c1
-rw-r--r--src/nspawn/nspawn.c1
-rw-r--r--src/quotacheck/quotacheck.c1
-rw-r--r--src/shared/audit.c1
-rw-r--r--src/shared/cgroup-show.c1
-rw-r--r--src/shared/cgroup-util.c1
-rw-r--r--src/shared/log.c1
-rw-r--r--src/shared/logs-show.c1
-rw-r--r--src/shared/machine-pool.c1
-rw-r--r--src/shared/pager.c1
-rw-r--r--src/shared/process-util.c538
-rw-r--r--src/shared/process-util.h65
-rw-r--r--src/shared/smack-util.c1
-rw-r--r--src/shared/spawn-ask-password-agent.c1
-rw-r--r--src/shared/spawn-polkit-agent.c1
-rw-r--r--src/shared/util.c507
-rw-r--r--src/shared/util.h41
-rw-r--r--src/shared/virt.c1
-rw-r--r--src/systemctl/systemctl.c1
-rw-r--r--src/test/test-cgroup-util.c1
-rw-r--r--src/test/test-fileio.c1
-rw-r--r--src/test/test-namespace.c1
-rw-r--r--src/test/test-process-util.c138
-rw-r--r--src/test/test-util.c101
-rw-r--r--src/tty-ask-password-agent/tty-ask-password-agent.c1
-rw-r--r--src/vconsole/vconsole-setup.c1
62 files changed, 809 insertions, 645 deletions
diff --git a/.gitignore b/.gitignore
index 875ada5ebd..7330c8077d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -234,6 +234,7 @@
/test-path-util
/test-pppoe
/test-prioq
+/test-process-util
/test-pty
/test-qcow2
/test-ratelimit
diff --git a/Makefile.am b/Makefile.am
index 0a57389447..ceb1d7d565 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -899,6 +899,8 @@ libsystemd_shared_la_SOURCES = \
src/shared/base-filesystem.h \
src/shared/memfd-util.c \
src/shared/memfd-util.h \
+ src/shared/process-util.c \
+ src/shared/process-util.h \
src/shared/uid-range.c \
src/shared/uid-range.h \
src/shared/nss-util.h \
@@ -1389,6 +1391,7 @@ tests += \
test-utf8 \
test-ellipsize \
test-util \
+ test-process-util \
test-path-lookup \
test-ring \
test-barrier \
@@ -1664,6 +1667,12 @@ test_util_LDADD = \
libsystemd-label.la \
libsystemd-shared.la
+test_process_util_SOURCES = \
+ src/test/test-process-util.c
+
+test_process_util_LDADD = \
+ libsystemd-shared.la
+
test_path_lookup_SOURCES = \
src/test/test-path-lookup.c
diff --git a/src/core/automount.c b/src/core/automount.c
index e1ca2a48ca..ce484ff1cd 100644
--- a/src/core/automount.c
+++ b/src/core/automount.c
@@ -41,6 +41,7 @@
#include "bus-util.h"
#include "bus-error.h"
#include "formats-util.h"
+#include "process-util.h"
static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = {
[AUTOMOUNT_DEAD] = UNIT_INACTIVE,
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
index 6b8abb4802..e337c0fd13 100644
--- a/src/core/cgroup.c
+++ b/src/core/cgroup.c
@@ -22,6 +22,7 @@
#include <fcntl.h>
#include <fnmatch.h>
+#include "process-util.h"
#include "path-util.h"
#include "special.h"
#include "cgroup-util.h"
diff --git a/src/core/execute.c b/src/core/execute.c
index c87e9004bb..b00d510419 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -77,6 +77,7 @@
#include "bus-endpoint.h"
#include "cap-list.h"
#include "formats-util.h"
+#include "process-util.h"
#ifdef HAVE_APPARMOR
#include "apparmor-util.h"
diff --git a/src/core/killall.c b/src/core/killall.c
index 504051467d..31bec01bbf 100644
--- a/src/core/killall.c
+++ b/src/core/killall.c
@@ -28,6 +28,7 @@
#include "killall.h"
#include "set.h"
#include "formats-util.h"
+#include "process-util.h"
#define TIMEOUT_USEC (10 * USEC_PER_SEC)
diff --git a/src/core/machine-id-setup.c b/src/core/machine-id-setup.c
index 2ffb2a7aa6..e083c5b347 100644
--- a/src/core/machine-id-setup.c
+++ b/src/core/machine-id-setup.c
@@ -36,6 +36,7 @@
#include "virt.h"
#include "fileio.h"
#include "path-util.h"
+#include "process-util.h"
static int shorten_uuid(char destination[34], const char source[36]) {
unsigned i, j;
diff --git a/src/core/main.c b/src/core/main.c
index 5d1aed8d3f..80a51ee2e5 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -73,6 +73,7 @@
#include "smack-setup.h"
#include "kmod-setup.h"
#include "formats-util.h"
+#include "process-util.h"
static enum {
ACTION_RUN,
diff --git a/src/core/manager.c b/src/core/manager.c
index 1c912fcdf7..6d1729b6e0 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -71,6 +71,7 @@
#include "dbus-manager.h"
#include "bus-kernel.h"
#include "time-util.h"
+#include "process-util.h"
/* Initial delay and the interval for printing status messages about running jobs */
#define JOBS_IN_PROGRESS_WAIT_USEC (5*USEC_PER_SEC)
diff --git a/src/core/service.c b/src/core/service.c
index c6ea6554f0..fa818fc871 100644
--- a/src/core/service.c
+++ b/src/core/service.c
@@ -46,6 +46,7 @@
#include "bus-util.h"
#include "bus-kernel.h"
#include "formats-util.h"
+#include "process-util.h"
static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
[SERVICE_DEAD] = UNIT_INACTIVE,
diff --git a/src/core/shutdown.c b/src/core/shutdown.c
index 70a461e38c..f037a38a0c 100644
--- a/src/core/shutdown.c
+++ b/src/core/shutdown.c
@@ -42,6 +42,7 @@
#include "cgroup-util.h"
#include "def.h"
#include "switch-root.h"
+#include "process-util.h"
#define FINALIZE_ATTEMPTS 50
diff --git a/src/core/unit.c b/src/core/unit.c
index 4b9c406e2e..3aa1bf25f7 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -47,6 +47,7 @@
#include "execute.h"
#include "dropin.h"
#include "formats-util.h"
+#include "process-util.h"
const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
[UNIT_SERVICE] = &service_vtable,
diff --git a/src/delta/delta.c b/src/delta/delta.c
index 9f1f8f333b..106c03675f 100644
--- a/src/delta/delta.c
+++ b/src/delta/delta.c
@@ -32,6 +32,7 @@
#include "pager.h"
#include "build.h"
#include "strv.h"
+#include "process-util.h"
static const char prefixes[] =
"/etc\0"
diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c
index 7eaf902e75..6a0f67f6e9 100644
--- a/src/fsck/fsck.c
+++ b/src/fsck/fsck.c
@@ -32,6 +32,7 @@
#include "sd-device.h"
#include "util.h"
+#include "process-util.h"
#include "special.h"
#include "bus-util.h"
#include "bus-error.h"
diff --git a/src/getty-generator/getty-generator.c b/src/getty-generator/getty-generator.c
index f8f5fb30c2..0f59f5ac92 100644
--- a/src/getty-generator/getty-generator.c
+++ b/src/getty-generator/getty-generator.c
@@ -31,6 +31,7 @@
#include "virt.h"
#include "fileio.h"
#include "path-util.h"
+#include "process-util.h"
static const char *arg_dest = "/tmp";
diff --git a/src/import/export-tar.c b/src/import/export-tar.c
index 9bd0251696..73e1faecf3 100644
--- a/src/import/export-tar.c
+++ b/src/import/export-tar.c
@@ -27,6 +27,7 @@
#include "btrfs-util.h"
#include "import-common.h"
#include "export-tar.h"
+#include "process-util.h"
#define COPY_BUFFER_SIZE (16*1024)
diff --git a/src/import/import-tar.c b/src/import/import-tar.c
index 6ec5504265..12701bfcef 100644
--- a/src/import/import-tar.c
+++ b/src/import/import-tar.c
@@ -35,6 +35,7 @@
#include "import-compress.h"
#include "import-common.h"
#include "import-tar.h"
+#include "process-util.h"
struct TarImport {
sd_event *event;
diff --git a/src/import/importd.c b/src/import/importd.c
index 998e96008d..d4da4b29a2 100644
--- a/src/import/importd.c
+++ b/src/import/importd.c
@@ -33,6 +33,7 @@
#include "machine-pool.h"
#include "path-util.h"
#include "import-util.h"
+#include "process-util.h"
typedef struct Transfer Transfer;
typedef struct Manager Manager;
diff --git a/src/import/pull-common.c b/src/import/pull-common.c
index 57323531e2..59091ba7cb 100644
--- a/src/import/pull-common.c
+++ b/src/import/pull-common.c
@@ -29,6 +29,7 @@
#include "capability.h"
#include "pull-job.h"
#include "pull-common.h"
+#include "process-util.h"
#define FILENAME_ESCAPE "/.#\"\'"
diff --git a/src/import/pull-dkr.c b/src/import/pull-dkr.c
index c922bace45..0eefec562e 100644
--- a/src/import/pull-dkr.c
+++ b/src/import/pull-dkr.c
@@ -37,6 +37,7 @@
#include "pull-common.h"
#include "import-common.h"
#include "pull-dkr.h"
+#include "process-util.h"
typedef enum DkrProgress {
DKR_SEARCHING,
diff --git a/src/import/pull-tar.c b/src/import/pull-tar.c
index 0efa07d3f3..27a9af804d 100644
--- a/src/import/pull-tar.c
+++ b/src/import/pull-tar.c
@@ -38,6 +38,7 @@
#include "pull-job.h"
#include "pull-common.h"
#include "pull-tar.h"
+#include "process-util.h"
typedef enum TarProgress {
TAR_DOWNLOADING,
diff --git a/src/journal/coredump.c b/src/journal/coredump.c
index 4e23e487fb..1c747aa2b4 100644
--- a/src/journal/coredump.c
+++ b/src/journal/coredump.c
@@ -48,6 +48,7 @@
#include "capability.h"
#include "journald-native.h"
#include "coredump-vacuum.h"
+#include "process-util.h"
/* The maximum size up to which we process coredumps */
#define PROCESS_SIZE_MAX ((off_t) (2LLU*1024LLU*1024LLU*1024LLU))
diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
index 4026a1c26f..4f6ddfae2e 100644
--- a/src/journal/coredumpctl.c
+++ b/src/journal/coredumpctl.c
@@ -37,6 +37,7 @@
#include "journal-internal.h"
#include "compress.h"
#include "sigbus.h"
+#include "process-util.h"
static enum {
ACTION_NONE,
diff --git a/src/journal/journald-console.c b/src/journal/journald-console.c
index 4ccacd7a5f..6c155307c1 100644
--- a/src/journal/journald-console.c
+++ b/src/journal/journald-console.c
@@ -27,6 +27,7 @@
#include "journald-server.h"
#include "journald-console.h"
#include "formats-util.h"
+#include "process-util.h"
static bool prefix_timestamp(void) {
diff --git a/src/journal/journald-kmsg.c b/src/journal/journald-kmsg.c
index cb4bc38f2c..e5be7f7766 100644
--- a/src/journal/journald-kmsg.c
+++ b/src/journal/journald-kmsg.c
@@ -32,6 +32,7 @@
#include "journald-kmsg.h"
#include "journald-syslog.h"
#include "formats-util.h"
+#include "process-util.h"
void server_forward_kmsg(
Server *s,
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
index 7e20b961a9..5e07ce3c49 100644
--- a/src/journal/journald-server.c
+++ b/src/journal/journald-server.c
@@ -51,6 +51,7 @@
#include "journald-server.h"
#include "acl-util.h"
#include "formats-util.h"
+#include "process-util.h"
#ifdef HAVE_SELINUX
#include <selinux/selinux.h>
diff --git a/src/journal/journald-syslog.c b/src/journal/journald-syslog.c
index 6c46a30359..26fdf62acd 100644
--- a/src/journal/journald-syslog.c
+++ b/src/journal/journald-syslog.c
@@ -32,6 +32,7 @@
#include "journald-console.h"
#include "journald-wall.h"
#include "formats-util.h"
+#include "process-util.h"
/* Warn once every 30s if we missed syslog message */
#define WARN_FORWARD_SYSLOG_MISSED_USEC (30 * USEC_PER_SEC)
diff --git a/src/journal/journald-wall.c b/src/journal/journald-wall.c
index b4e07179ac..5298e45be9 100644
--- a/src/journal/journald-wall.c
+++ b/src/journal/journald-wall.c
@@ -23,6 +23,7 @@
#include "journald-server.h"
#include "journald-wall.h"
#include "formats-util.h"
+#include "process-util.h"
void server_forward_wall(
Server *s,
diff --git a/src/libsystemd-network/test-pppoe.c b/src/libsystemd-network/test-pppoe.c
index 40d04fdb21..9c8d6f7779 100644
--- a/src/libsystemd-network/test-pppoe.c
+++ b/src/libsystemd-network/test-pppoe.c
@@ -31,6 +31,7 @@
#include "event-util.h"
#include "sd-rtnl.h"
#include "sd-pppoe.h"
+#include "process-util.h"
static void pppoe_handler(sd_pppoe *ppp, int event, void *userdata) {
static int pppoe_state = -1;
diff --git a/src/libsystemd/sd-bus/bus-container.c b/src/libsystemd/sd-bus/bus-container.c
index 3b3a5d357f..f157c25bba 100644
--- a/src/libsystemd/sd-bus/bus-container.c
+++ b/src/libsystemd/sd-bus/bus-container.c
@@ -23,6 +23,7 @@
#include <fcntl.h>
#include "util.h"
+#include "process-util.h"
#include "bus-internal.h"
#include "bus-socket.h"
#include "bus-container.h"
diff --git a/src/libsystemd/sd-bus/bus-creds.c b/src/libsystemd/sd-bus/bus-creds.c
index 4141cc3331..a5d3574ec5 100644
--- a/src/libsystemd/sd-bus/bus-creds.c
+++ b/src/libsystemd/sd-bus/bus-creds.c
@@ -24,6 +24,7 @@
#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 1f78e4b883..57cfb5d0b5 100644
--- a/src/login/inhibit.c
+++ b/src/login/inhibit.c
@@ -32,6 +32,7 @@
#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 ec102ae4c7..7393eb3b9b 100644
--- a/src/login/loginctl.c
+++ b/src/login/loginctl.c
@@ -41,6 +41,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 d7ae175bd3..29e7ed007c 100644
--- a/src/login/logind-action.c
+++ b/src/login/logind-action.c
@@ -28,6 +28,7 @@
#include "bus-error.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 45aedad594..6bf9205831 100644
--- a/src/login/logind-dbus.c
+++ b/src/login/logind-dbus.c
@@ -41,6 +41,7 @@
#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;
diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c
index 6cc46afbdd..2d710a9a9b 100644
--- a/src/machine/machine-dbus.c
+++ b/src/machine/machine-dbus.c
@@ -43,6 +43,7 @@
#include "machine.h"
#include "machine-dbus.h"
#include "formats-util.h"
+#include "process-util.h"
static int property_get_id(
sd_bus *bus,
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
index 688c510907..87e15d74fb 100644
--- a/src/machine/machinectl.c
+++ b/src/machine/machinectl.c
@@ -52,6 +52,7 @@
#include "copy.h"
#include "verbs.h"
#include "import-util.h"
+#include "process-util.h"
static char **arg_property = NULL;
static bool arg_all = false;
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 8ec6743702..96075327df 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -92,6 +92,7 @@
#include "fw-util.h"
#include "local-addresses.h"
#include "formats-util.h"
+#include "process-util.h"
#ifdef HAVE_SECCOMP
#include "seccomp-util.h"
diff --git a/src/quotacheck/quotacheck.c b/src/quotacheck/quotacheck.c
index 819706a72f..a729f592cf 100644
--- a/src/quotacheck/quotacheck.c
+++ b/src/quotacheck/quotacheck.c
@@ -25,6 +25,7 @@
#include <unistd.h>
#include "util.h"
+#include "process-util.h"
static bool arg_skip = false;
static bool arg_force = false;
diff --git a/src/shared/audit.c b/src/shared/audit.c
index 4c1496f601..84181d3321 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 fed72ac175..0f263e958c 100644
--- a/src/shared/cgroup-show.c
+++ b/src/shared/cgroup-show.c
@@ -26,6 +26,7 @@
#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 481708f19e..5b04702ac2 100644
--- a/src/shared/cgroup-util.c
+++ b/src/shared/cgroup-util.c
@@ -34,6 +34,7 @@
#include "macro.h"
#include "util.h"
#include "formats-util.h"
+#include "process-util.h"
#include "path-util.h"
#include "unit-name.h"
#include "fileio.h"
diff --git a/src/shared/log.c b/src/shared/log.c
index 32ec581d8d..7edcf1f18a 100644
--- a/src/shared/log.c
+++ b/src/shared/log.c
@@ -35,6 +35,7 @@
#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 e179b8a7b4..7a7a1e934f 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -32,6 +32,7 @@
#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 0fae623944..41aa1b7ac6 100644
--- a/src/shared/machine-pool.c
+++ b/src/shared/machine-pool.c
@@ -25,6 +25,7 @@
#include <sys/mount.h>
#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 f12bfb3287..b5a584ba01 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 0000000000..92a69f50e7
--- /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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdbool.h>
+#include <sys/types.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <signal.h>
+#include <ctype.h>
+
+#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 0000000000..07431d043b
--- /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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdbool.h>
+#include <sys/types.h>
+#include <alloca.h>
+#include <stdio.h>
+#include <string.h>
+#include <signal.h>
+
+#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 a73a996cf6..2e24b1ea99 100644
--- a/src/shared/smack-util.c
+++ b/src/shared/smack-util.c
@@ -24,6 +24,7 @@
#include <sys/xattr.h>
#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 0d7458806d..70466d17e5 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 bc1810da98..4db249e1ca 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 7321f1bcbb..d4753f1ee8 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -93,6 +93,7 @@
#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);
@@ -185,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) {
@@ -598,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;
@@ -659,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;
@@ -3676,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 */
@@ -4119,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;
@@ -5338,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;
@@ -6497,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 d17b987f33..b939d7f67e 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -227,8 +227,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);
@@ -252,17 +250,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_;
@@ -271,6 +258,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,
@@ -406,8 +394,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, ...);
@@ -482,9 +468,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_;
@@ -504,8 +487,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);
@@ -604,8 +585,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);
@@ -613,8 +592,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_;
@@ -891,19 +868,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);
@@ -931,9 +895,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 54c465520d..1299a75ed5 100644
--- a/src/shared/virt.c
+++ b/src/shared/virt.c
@@ -24,6 +24,7 @@
#include <unistd.h>
#include "util.h"
+#include "process-util.h"
#include "virt.h"
#include "fileio.h"
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 915482f6d4..04b7e7bbda 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -70,6 +70,7 @@
#include "dropin.h"
#include "efivars.h"
#include "formats-util.h"
+#include "process-util.h"
static char **arg_types = NULL;
static char **arg_states = NULL;
diff --git a/src/test/test-cgroup-util.c b/src/test/test-cgroup-util.c
index c0f4abe866..aca4f868a1 100644
--- a/src/test/test-cgroup-util.c
+++ b/src/test/test-cgroup-util.c
@@ -24,6 +24,7 @@
#include "cgroup-util.h"
#include "test-helper.h"
#include "formats-util.h"
+#include "process-util.h"
static void check_p_d_u(const char *path, int code, const char *result) {
_cleanup_free_ char *unit = NULL;
diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c
index 63e4a19b76..4c31b776bd 100644
--- a/src/test/test-fileio.c
+++ b/src/test/test-fileio.c
@@ -24,6 +24,7 @@
#include <unistd.h>
#include "util.h"
+#include "process-util.h"
#include "fileio.h"
#include "strv.h"
#include "env-util.h"
diff --git a/src/test/test-namespace.c b/src/test/test-namespace.c
index 2397db5fff..7d7e08dc5d 100644
--- a/src/test/test-namespace.c
+++ b/src/test/test-namespace.c
@@ -23,6 +23,7 @@
#include "namespace.h"
#include "util.h"
+#include "process-util.h"
static void test_tmpdir(const char *id, const char *A, const char *B) {
_cleanup_free_ char *a, *b;
diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c
new file mode 100644
index 0000000000..a17ef144fc
--- /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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#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;
+}
diff --git a/src/test/test-util.c b/src/test/test-util.c
index 4d36eb26e5..bfd4df946d 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -37,6 +37,7 @@
#include "fileio.h"
#include "conf-parser.h"
#include "virt.h"
+#include "process-util.h"
static void test_streq_ptr(void) {
assert_se(streq_ptr(NULL, NULL));
@@ -572,69 +573,6 @@ static void test_u64log2(void) {
assert_se(u64log2(1024*1024+5) == 20);
}
-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_protect_errno(void) {
errno = 12;
{
@@ -1138,40 +1076,6 @@ static void test_is_symlink(void) {
unlink(name_link);
}
-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));
-}
-
static void test_search_and_fopen(void) {
const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL};
char name[] = "/tmp/test-search_and_fopen.XXXXXX";
@@ -1625,7 +1529,6 @@ int main(int argc, char *argv[]) {
test_memdup_multiply();
test_hostname_is_valid();
test_u64log2();
- test_get_process_comm();
test_protect_errno();
test_parse_size();
test_config_parse_iec_off();
@@ -1654,8 +1557,6 @@ int main(int argc, char *argv[]) {
test_strshorten();
test_strjoina();
test_is_symlink();
- test_pid_is_unwaited();
- test_pid_is_alive();
test_search_and_fopen();
test_search_and_fopen_nulstr();
test_glob_exists();
diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c
index 47093b850d..8f4031a1f6 100644
--- a/src/tty-ask-password-agent/tty-ask-password-agent.c
+++ b/src/tty-ask-password-agent/tty-ask-password-agent.c
@@ -42,6 +42,7 @@
#include "strv.h"
#include "build.h"
#include "def.h"
+#include "process-util.h"
static enum {
ACTION_LIST,
diff --git a/src/vconsole/vconsole-setup.c b/src/vconsole/vconsole-setup.c
index 243e51dfc9..3f93524201 100644
--- a/src/vconsole/vconsole-setup.c
+++ b/src/vconsole/vconsole-setup.c
@@ -35,6 +35,7 @@
#include "log.h"
#include "virt.h"
#include "fileio.h"
+#include "process-util.h"
static bool is_vconsole(int fd) {
unsigned char data[1];