]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared: add process-util.[ch]
authorRonny Chevalier <chevalier.ronny@gmail.com>
Fri, 10 Apr 2015 17:10:00 +0000 (19:10 +0200)
committerRonny Chevalier <chevalier.ronny@gmail.com>
Fri, 10 Apr 2015 21:54:49 +0000 (23:54 +0200)
62 files changed:
.gitignore
Makefile.am
src/core/automount.c
src/core/cgroup.c
src/core/execute.c
src/core/killall.c
src/core/machine-id-setup.c
src/core/main.c
src/core/manager.c
src/core/service.c
src/core/shutdown.c
src/core/unit.c
src/delta/delta.c
src/fsck/fsck.c
src/getty-generator/getty-generator.c
src/import/export-tar.c
src/import/import-tar.c
src/import/importd.c
src/import/pull-common.c
src/import/pull-dkr.c
src/import/pull-tar.c
src/journal/coredump.c
src/journal/coredumpctl.c
src/journal/journald-console.c
src/journal/journald-kmsg.c
src/journal/journald-server.c
src/journal/journald-syslog.c
src/journal/journald-wall.c
src/libsystemd-network/test-pppoe.c
src/libsystemd/sd-bus/bus-container.c
src/libsystemd/sd-bus/bus-creds.c
src/login/inhibit.c
src/login/loginctl.c
src/login/logind-action.c
src/login/logind-dbus.c
src/machine/machine-dbus.c
src/machine/machinectl.c
src/nspawn/nspawn.c
src/quotacheck/quotacheck.c
src/shared/audit.c
src/shared/cgroup-show.c
src/shared/cgroup-util.c
src/shared/log.c
src/shared/logs-show.c
src/shared/machine-pool.c
src/shared/pager.c
src/shared/process-util.c [new file with mode: 0644]
src/shared/process-util.h [new file with mode: 0644]
src/shared/smack-util.c
src/shared/spawn-ask-password-agent.c
src/shared/spawn-polkit-agent.c
src/shared/util.c
src/shared/util.h
src/shared/virt.c
src/systemctl/systemctl.c
src/test/test-cgroup-util.c
src/test/test-fileio.c
src/test/test-namespace.c
src/test/test-process-util.c [new file with mode: 0644]
src/test/test-util.c
src/tty-ask-password-agent/tty-ask-password-agent.c
src/vconsole/vconsole-setup.c

index 875ada5ebd9765f019973fe07402a6de8923c0ec..7330c8077d93bb59863c1de935b90e306d165ec0 100644 (file)
 /test-path-util
 /test-pppoe
 /test-prioq
+/test-process-util
 /test-pty
 /test-qcow2
 /test-ratelimit
index 0a57389447cd49d9445a3c535e9211c7e79897f3..ceb1d7d565f41806b79ee46938dc09ac99bb0b4e 100644 (file)
@@ -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
 
index e1ca2a48ca2cce73a6d112acc0b758ef37952ff6..ce484ff1cd16a08408d49b9cf16ada8055583ef4 100644 (file)
@@ -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,
index 6b8abb4802ba76635b9ecaa0a17ff3ac796641af..e337c0fd13b4be88dc297196f1d9c33cdc76ecb0 100644 (file)
@@ -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"
index c87e9004bbcc77991ade4a18aaa45c6bc66bc5cb..b00d5104198b91e71471fb73e3e7135cee9e5052 100644 (file)
@@ -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"
index 504051467da877100ce5bdb05caf2142967cf3aa..31bec01bbfedfea667e88c5eabcc134a105da914 100644 (file)
@@ -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)
 
index 2ffb2a7aa6f6d357f47b011d8594063ef53833d9..e083c5b347afb3f0142f346881bdfaf14ccdb906 100644 (file)
@@ -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;
index 5d1aed8d3f84209607984e2c09533e8a13916d42..80a51ee2e54e80645e4a863fa55cec4b90107fa4 100644 (file)
@@ -73,6 +73,7 @@
 #include "smack-setup.h"
 #include "kmod-setup.h"
 #include "formats-util.h"
+#include "process-util.h"
 
 static enum {
         ACTION_RUN,
index 1c912fcdf77af8cad8764f253fef29aaf9d1a337..6d1729b6e0b25a291da2d0cd152303d2c6faf119 100644 (file)
@@ -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)
index c6ea6554f05503c558e3fb257efdd1c53e008a2e..fa818fc871c7e8b0f986dae98168bbbc978c574a 100644 (file)
@@ -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,
index 70a461e38c79bc08097dc79ad4882c4b714f3c0c..f037a38a0caa1fc882506b67d21294cd608ee7e5 100644 (file)
@@ -42,6 +42,7 @@
 #include "cgroup-util.h"
 #include "def.h"
 #include "switch-root.h"
+#include "process-util.h"
 
 #define FINALIZE_ATTEMPTS 50
 
index 4b9c406e2e62a0238ba475e59e88e13004c8cfa0..3aa1bf25f73ee6fbbbc94c972c9f048c73cdaeba 100644 (file)
@@ -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,
index 9f1f8f333b545d798382ebcf034424445e33c2bf..106c03675f37d6e7cb0110170b145714f7273c2a 100644 (file)
@@ -32,6 +32,7 @@
 #include "pager.h"
 #include "build.h"
 #include "strv.h"
+#include "process-util.h"
 
 static const char prefixes[] =
         "/etc\0"
index 7eaf902e75b37533c5f201db006e8ec34c7635ca..6a0f67f6e9e0a8773d22d75a14ad8af83c9cd8cb 100644 (file)
@@ -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"
index f8f5fb30c2433cae500cebb0b9164b19f0c4c230..0f59f5ac925af73f37747c7293b806fb3968827e 100644 (file)
@@ -31,6 +31,7 @@
 #include "virt.h"
 #include "fileio.h"
 #include "path-util.h"
+#include "process-util.h"
 
 static const char *arg_dest = "/tmp";
 
index 9bd02516960c63d88ebcee27f2e735f8be7caac0..73e1faecf3f84771376ad2ecbc1983f041a87162 100644 (file)
@@ -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)
 
index 6ec55042652af32258aeffc7bcf9b97cc6da4380..12701bfcefb3ac4f4c85632e02915457c537617a 100644 (file)
@@ -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;
index 998e96008d1761567e3b649d9f5fd06932e404c2..d4da4b29a2622ec73815ebed38f51b15830bf3f7 100644 (file)
@@ -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;
index 57323531e22c3fac4899b4d9fc0e6654bc8f7f4a..59091ba7cb346f2d33e988cb1de0ed333a01af56 100644 (file)
@@ -29,6 +29,7 @@
 #include "capability.h"
 #include "pull-job.h"
 #include "pull-common.h"
+#include "process-util.h"
 
 #define FILENAME_ESCAPE "/.#\"\'"
 
index c922bace45e24e364f635d203f94e3e5f4696ac0..0eefec562ebbf2765aa7eeef8ee3cde2ac9a2347 100644 (file)
@@ -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,
index 0efa07d3f388b548ec789f879a93d1639c8b3dd3..27a9af804df2d8b43f9dd26bcc773b0bdf91e096 100644 (file)
@@ -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,
index 4e23e487fbbe55d735f22f8a5173ec261edab350..1c747aa2b44aadbc6ecc2745de89e5b044f4ab87 100644 (file)
@@ -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))
index 4026a1c26f14e414f202e23e8ec22b3c5b8683c0..4f6ddfae2e6bf14f256a082a35b14421b7a24c9c 100644 (file)
@@ -37,6 +37,7 @@
 #include "journal-internal.h"
 #include "compress.h"
 #include "sigbus.h"
+#include "process-util.h"
 
 static enum {
         ACTION_NONE,
index 4ccacd7a5f5fadc591fcfe1e5bcf37370764387b..6c155307c1c88cf30097ed3f5acd77fd78c7eaa1 100644 (file)
@@ -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) {
 
index cb4bc38f2cd9cf567c49732e656bd7705c9ed99e..e5be7f77666301c21245d812d874a7964132843f 100644 (file)
@@ -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,
index 7e20b961a9b5d76e2e1b606b1a4566cbbba21686..5e07ce3c49e532ab4179b62de1c4d1a1a124a924 100644 (file)
@@ -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>
index 6c46a303599d3f704f2657465e2cd089fc032a65..26fdf62acd2c555fcfef604588be32b86206f12d 100644 (file)
@@ -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)
index b4e07179ac23d933599c316e9d5db7cdd6370878..5298e45be9502865e5e0724afede3c03aa6f4db2 100644 (file)
@@ -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,
index 40d04fdb217d3ea624298027d3c51118c7c1e6bd..9c8d6f777948724d1e45445ec2d1ce62b945ab93 100644 (file)
@@ -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;
index 3b3a5d357f755cd211da24ff44f8163bc703cc18..f157c25bbab83cfa9c3a37b7307bfb7ced80c9ae 100644 (file)
@@ -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"
index 4141cc3331c8ab7fbcf0a0adc0e9989732cf7453..a5d3574ec5a6e0c2af159abaa7bdb111fb0b98ad 100644 (file)
@@ -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"
index 1f78e4b883d565179a354da6ffe19196e6fd403e..57cfb5d0b50855b801bb37cf41715ab8666281d1 100644 (file)
@@ -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;
index ec102ae4c7c7e62e0d9ab2f82ceecf2d2250ef08..7393eb3b9b6e9a15a2873358fa4db80fa2167137 100644 (file)
@@ -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;
index d7ae175bd3f947241c08de4413b4923b56945518..29e7ed007ca9b1bd4ee664e7c750e64286ec3d09 100644 (file)
@@ -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,
index 45aedad59480ead677e4153b622a05770e08da94..6bf92058310cde90a19280978b145600d3a0803c 100644 (file)
@@ -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;
index 6cc46afbdde337b2c23a19035814b99ed39a0b0f..2d710a9a9b3c18c9f3442a79e7c315b297d815df 100644 (file)
@@ -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,
index 688c5109072fb83c1ab9e9c592769ffc681610e5..87e15d74fb0687c9dffe1a4f1af6af6ad20b2cb1 100644 (file)
@@ -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;
index 8ec6743702d1b66130dc97e92003e694c00c4f30..96075327df449adbfb702edfb2aa9baff6290a7b 100644 (file)
@@ -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"
index 819706a72f6b825d07c32e6756f3c8bf92643bdb..a729f592cf3e95a10c93921842aa7c3e60d68ee3 100644 (file)
@@ -25,6 +25,7 @@
 #include <unistd.h>
 
 #include "util.h"
+#include "process-util.h"
 
 static bool arg_skip = false;
 static bool arg_force = false;
index 4c1496f601c5cf5bffa42e991eb11560712454f5..84181d3321fe0e8e92281671b086690defe35d62 100644 (file)
@@ -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) {
index fed72ac175addc2adef03b4a5f6e3907eeb4a0fb..0f263e958c5cb644d96068bba12bf5bf50f6d50d 100644 (file)
@@ -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"
index 481708f19ef8e9a33576cd8314ba2a139ddb6cc2..5b04702ac25ad91ad39b4cd5ebbed4553f0ba6fc 100644 (file)
@@ -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"
index 32ec581d8d591e5b3bf9637b8505bfd9b0545dd3..7edcf1f18a6b54e6b05ba343cc8b26f9c37b1f45 100644 (file)
@@ -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)
 
index e179b8a7b4e20ddbca46bba2672b8c53c16a3907..7a7a1e934f2ba9135f22f255153e878037d20ccd 100644 (file)
@@ -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 */
index 0fae623944d9a705eb5e36bb80db2084aeef4381..41aa1b7ac6fd413f16e239b2fcc8006688c16f10 100644 (file)
@@ -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"
index f12bfb3287f0cdca2a762b41d22ceb0a18444037..b5a584ba0159f564147392d197a6870b1dc873cb 100644 (file)
@@ -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 (file)
index 0000000..92a69f5
--- /dev/null
@@ -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 (file)
index 0000000..07431d0
--- /dev/null
@@ -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);
index a73a996cf6d5623608ecd25436cd0d286e31b35f..2e24b1ea992e0dea7deb8079ec276d8c8cc54a9a 100644 (file)
@@ -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"
index 0d7458806dc3e7ef7a9decbf5f45719c722be93d..70466d17e5e3af461122bdfe4d17ad5ba5a66bc8 100644 (file)
@@ -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;
index bc1810da985de88b26f4356a75dd0cffb4ade618..4db249e1cac8fd298fe072c15116d2925581f061 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "log.h"
 #include "util.h"
+#include "process-util.h"
 #include "spawn-polkit-agent.h"
 
 #ifdef ENABLE_POLKIT
index 7321f1bcbbea71caf027fe20f6aeae04e054c755..d4753f1ee8779d5dabf5321f51ed2a5f0b5b6d5a 100644 (file)
@@ -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;
index d17b987f334fb9ea638ff7bba7249c5ee1d38871..b939d7f67ee213d3f67212689c6b2271faa1bb95 100644 (file)
@@ -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);
 
index 54c465520d86dee05cc0af26b496d4fe7f106ff8..1299a75ed54d242ef9b93964b7a24dfe302fff9c 100644 (file)
@@ -24,6 +24,7 @@
 #include <unistd.h>
 
 #include "util.h"
+#include "process-util.h"
 #include "virt.h"
 #include "fileio.h"
 
index 915482f6d49ccc094cdb3ebc4b18f8d01b7c1e1d..04b7e7bbda00ece4db9b06c8cd717fbfd4da1182 100644 (file)
@@ -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;
index c0f4abe8665f7fb4a1af7b1396d00f7f5e723d4b..aca4f868a154ef1f1cfa939e0fcac11fe157956e 100644 (file)
@@ -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;
index 63e4a19b7638e41c6ac86324f7875380698c7aa0..4c31b776bd97ea35e640a616a0e53c39d2d8d26a 100644 (file)
@@ -24,6 +24,7 @@
 #include <unistd.h>
 
 #include "util.h"
+#include "process-util.h"
 #include "fileio.h"
 #include "strv.h"
 #include "env-util.h"
index 2397db5fffe40afaa1afe142763c72b90101b0b5..7d7e08dc5d1480a0b0e7b6cb8f0d1be3f9486e83 100644 (file)
@@ -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 (file)
index 0000000..a17ef14
--- /dev/null
@@ -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;
+}
index 4d36eb26e59dedcb0018dc291965c44e9f57fab0..bfd4df946d0ef8dcb1178e7656c031b28a2c8da8 100644 (file)
@@ -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();
index 47093b850dcf4f319a3634748dd9fb0e3e5949f8..8f4031a1f688e7b7fdfc8da890823b017481254d 100644 (file)
@@ -42,6 +42,7 @@
 #include "strv.h"
 #include "build.h"
 #include "def.h"
+#include "process-util.h"
 
 static enum {
         ACTION_LIST,
index 243e51dfc965b5912e7591e704be3e0b71e9e84f..3f935242013fc91b66522b098aaa4b7f65ca49e0 100644 (file)
@@ -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];