mirror of
https://github.com/systemd/systemd-stable.git
synced 2024-12-22 13:33:56 +03:00
shared: add process-util.[ch]
This commit is contained in:
parent
6482f6269c
commit
0b452006de
1
.gitignore
vendored
1
.gitignore
vendored
@ -234,6 +234,7 @@
|
||||
/test-path-util
|
||||
/test-pppoe
|
||||
/test-prioq
|
||||
/test-process-util
|
||||
/test-pty
|
||||
/test-qcow2
|
||||
/test-ratelimit
|
||||
|
@ -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
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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;
|
||||
|
@ -73,6 +73,7 @@
|
||||
#include "smack-setup.h"
|
||||
#include "kmod-setup.h"
|
||||
#include "formats-util.h"
|
||||
#include "process-util.h"
|
||||
|
||||
static enum {
|
||||
ACTION_RUN,
|
||||
|
@ -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)
|
||||
|
@ -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,
|
||||
|
@ -42,6 +42,7 @@
|
||||
#include "cgroup-util.h"
|
||||
#include "def.h"
|
||||
#include "switch-root.h"
|
||||
#include "process-util.h"
|
||||
|
||||
#define FINALIZE_ATTEMPTS 50
|
||||
|
||||
|
@ -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,
|
||||
|
@ -32,6 +32,7 @@
|
||||
#include "pager.h"
|
||||
#include "build.h"
|
||||
#include "strv.h"
|
||||
#include "process-util.h"
|
||||
|
||||
static const char prefixes[] =
|
||||
"/etc\0"
|
||||
|
@ -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"
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "virt.h"
|
||||
#include "fileio.h"
|
||||
#include "path-util.h"
|
||||
#include "process-util.h"
|
||||
|
||||
static const char *arg_dest = "/tmp";
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -29,6 +29,7 @@
|
||||
#include "capability.h"
|
||||
#include "pull-job.h"
|
||||
#include "pull-common.h"
|
||||
#include "process-util.h"
|
||||
|
||||
#define FILENAME_ESCAPE "/.#\"\'"
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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))
|
||||
|
@ -37,6 +37,7 @@
|
||||
#include "journal-internal.h"
|
||||
#include "compress.h"
|
||||
#include "sigbus.h"
|
||||
#include "process-util.h"
|
||||
|
||||
static enum {
|
||||
ACTION_NONE,
|
||||
|
@ -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) {
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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>
|
||||
|
@ -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)
|
||||
|
@ -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,
|
||||
|
@ -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;
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
|
@ -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;
|
||||
|
@ -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"
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "process-util.h"
|
||||
|
||||
static bool arg_skip = false;
|
||||
static bool arg_force = false;
|
||||
|
@ -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) {
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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 */
|
||||
|
@ -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"
|
||||
|
@ -27,6 +27,7 @@
|
||||
|
||||
#include "pager.h"
|
||||
#include "util.h"
|
||||
#include "process-util.h"
|
||||
#include "macro.h"
|
||||
|
||||
static pid_t pager_pid = 0;
|
||||
|
538
src/shared/process-util.c
Normal file
538
src/shared/process-util.c
Normal file
@ -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;
|
||||
}
|
65
src/shared/process-util.h
Normal file
65
src/shared/process-util.h
Normal file
@ -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);
|
@ -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"
|
||||
|
@ -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;
|
||||
|
@ -27,6 +27,7 @@
|
||||
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
#include "process-util.h"
|
||||
#include "spawn-polkit-agent.h"
|
||||
|
||||
#ifdef ENABLE_POLKIT
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "process-util.h"
|
||||
#include "virt.h"
|
||||
#include "fileio.h"
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "process-util.h"
|
||||
#include "fileio.h"
|
||||
#include "strv.h"
|
||||
#include "env-util.h"
|
||||
|
@ -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;
|
||||
|
138
src/test/test-process-util.c
Normal file
138
src/test/test-process-util.c
Normal file
@ -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;
|
||||
}
|
@ -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();
|
||||
|
@ -42,6 +42,7 @@
|
||||
#include "strv.h"
|
||||
#include "build.h"
|
||||
#include "def.h"
|
||||
#include "process-util.h"
|
||||
|
||||
static enum {
|
||||
ACTION_LIST,
|
||||
|
@ -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];
|
||||
|
Loading…
Reference in New Issue
Block a user