From 0b452006de98294d1690f045f6ea2f7f6630ec3b Mon Sep 17 00:00:00 2001 From: Ronny Chevalier Date: Fri, 10 Apr 2015 19:10:00 +0200 Subject: shared: add process-util.[ch] --- src/test/test-cgroup-util.c | 1 + src/test/test-fileio.c | 1 + src/test/test-namespace.c | 1 + src/test/test-process-util.c | 138 +++++++++++++++++++++++++++++++++++++++++++ src/test/test-util.c | 101 +------------------------------ 5 files changed, 142 insertions(+), 100 deletions(-) create mode 100644 src/test/test-process-util.c (limited to 'src/test') diff --git a/src/test/test-cgroup-util.c b/src/test/test-cgroup-util.c index c0f4abe866..aca4f868a1 100644 --- a/src/test/test-cgroup-util.c +++ b/src/test/test-cgroup-util.c @@ -24,6 +24,7 @@ #include "cgroup-util.h" #include "test-helper.h" #include "formats-util.h" +#include "process-util.h" static void check_p_d_u(const char *path, int code, const char *result) { _cleanup_free_ char *unit = NULL; diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c index 63e4a19b76..4c31b776bd 100644 --- a/src/test/test-fileio.c +++ b/src/test/test-fileio.c @@ -24,6 +24,7 @@ #include #include "util.h" +#include "process-util.h" #include "fileio.h" #include "strv.h" #include "env-util.h" diff --git a/src/test/test-namespace.c b/src/test/test-namespace.c index 2397db5fff..7d7e08dc5d 100644 --- a/src/test/test-namespace.c +++ b/src/test/test-namespace.c @@ -23,6 +23,7 @@ #include "namespace.h" #include "util.h" +#include "process-util.h" static void test_tmpdir(const char *id, const char *A, const char *B) { _cleanup_free_ char *a, *b; diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c new file mode 100644 index 0000000000..a17ef144fc --- /dev/null +++ b/src/test/test-process-util.c @@ -0,0 +1,138 @@ +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + Copyright 2013 Thomas H.P. Andersen + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include +#include + +#include "process-util.h" +#include "log.h" +#include "util.h" +#include "macro.h" +#include "virt.h" + +static void test_get_process_comm(void) { + struct stat st; + _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL, *cwd = NULL, *root = NULL; + _cleanup_free_ char *env = NULL; + pid_t e; + uid_t u; + gid_t g; + dev_t h; + int r; + pid_t me; + + if (stat("/proc/1/comm", &st) == 0) { + assert_se(get_process_comm(1, &a) >= 0); + log_info("pid1 comm: '%s'", a); + } else { + log_warning("/proc/1/comm does not exist."); + } + + assert_se(get_process_cmdline(1, 0, true, &c) >= 0); + log_info("pid1 cmdline: '%s'", c); + + assert_se(get_process_cmdline(1, 8, false, &d) >= 0); + log_info("pid1 cmdline truncated: '%s'", d); + + assert_se(get_parent_of_pid(1, &e) >= 0); + log_info("pid1 ppid: "PID_FMT, e); + assert_se(e == 0); + + assert_se(is_kernel_thread(1) == 0); + + r = get_process_exe(1, &f); + assert_se(r >= 0 || r == -EACCES); + log_info("pid1 exe: '%s'", strna(f)); + + assert_se(get_process_uid(1, &u) == 0); + log_info("pid1 uid: "UID_FMT, u); + assert_se(u == 0); + + assert_se(get_process_gid(1, &g) == 0); + log_info("pid1 gid: "GID_FMT, g); + assert_se(g == 0); + + me = getpid(); + + r = get_process_cwd(me, &cwd); + assert_se(r >= 0 || r == -EACCES); + log_info("pid1 cwd: '%s'", cwd); + + r = get_process_root(me, &root); + assert_se(r >= 0 || r == -EACCES); + log_info("pid1 root: '%s'", root); + + r = get_process_environ(me, &env); + assert_se(r >= 0 || r == -EACCES); + log_info("self strlen(environ): '%zu'", strlen(env)); + + if (!detect_container(NULL)) + assert_se(get_ctty_devnr(1, &h) == -ENOENT); + + getenv_for_pid(1, "PATH", &i); + log_info("pid1 $PATH: '%s'", strna(i)); +} + +static void test_pid_is_unwaited(void) { + pid_t pid; + + pid = fork(); + assert_se(pid >= 0); + if (pid == 0) { + _exit(EXIT_SUCCESS); + } else { + int status; + + waitpid(pid, &status, 0); + assert_se(!pid_is_unwaited(pid)); + } + assert_se(pid_is_unwaited(getpid())); + assert_se(!pid_is_unwaited(-1)); +} + +static void test_pid_is_alive(void) { + pid_t pid; + + pid = fork(); + assert_se(pid >= 0); + if (pid == 0) { + _exit(EXIT_SUCCESS); + } else { + int status; + + waitpid(pid, &status, 0); + assert_se(!pid_is_alive(pid)); + } + assert_se(pid_is_alive(getpid())); + assert_se(!pid_is_alive(-1)); +} + +int main(int argc, char *argv[]) { + log_parse_environment(); + log_open(); + + test_get_process_comm(); + test_pid_is_unwaited(); + test_pid_is_alive(); + + return 0; +} diff --git a/src/test/test-util.c b/src/test/test-util.c index 4d36eb26e5..bfd4df946d 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -37,6 +37,7 @@ #include "fileio.h" #include "conf-parser.h" #include "virt.h" +#include "process-util.h" static void test_streq_ptr(void) { assert_se(streq_ptr(NULL, NULL)); @@ -572,69 +573,6 @@ static void test_u64log2(void) { assert_se(u64log2(1024*1024+5) == 20); } -static void test_get_process_comm(void) { - struct stat st; - _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL, *cwd = NULL, *root = NULL; - _cleanup_free_ char *env = NULL; - pid_t e; - uid_t u; - gid_t g; - dev_t h; - int r; - pid_t me; - - if (stat("/proc/1/comm", &st) == 0) { - assert_se(get_process_comm(1, &a) >= 0); - log_info("pid1 comm: '%s'", a); - } else { - log_warning("/proc/1/comm does not exist."); - } - - assert_se(get_process_cmdline(1, 0, true, &c) >= 0); - log_info("pid1 cmdline: '%s'", c); - - assert_se(get_process_cmdline(1, 8, false, &d) >= 0); - log_info("pid1 cmdline truncated: '%s'", d); - - assert_se(get_parent_of_pid(1, &e) >= 0); - log_info("pid1 ppid: "PID_FMT, e); - assert_se(e == 0); - - assert_se(is_kernel_thread(1) == 0); - - r = get_process_exe(1, &f); - assert_se(r >= 0 || r == -EACCES); - log_info("pid1 exe: '%s'", strna(f)); - - assert_se(get_process_uid(1, &u) == 0); - log_info("pid1 uid: "UID_FMT, u); - assert_se(u == 0); - - assert_se(get_process_gid(1, &g) == 0); - log_info("pid1 gid: "GID_FMT, g); - assert_se(g == 0); - - me = getpid(); - - r = get_process_cwd(me, &cwd); - assert_se(r >= 0 || r == -EACCES); - log_info("pid1 cwd: '%s'", cwd); - - r = get_process_root(me, &root); - assert_se(r >= 0 || r == -EACCES); - log_info("pid1 root: '%s'", root); - - r = get_process_environ(me, &env); - assert_se(r >= 0 || r == -EACCES); - log_info("self strlen(environ): '%zu'", strlen(env)); - - if (!detect_container(NULL)) - assert_se(get_ctty_devnr(1, &h) == -ENOENT); - - getenv_for_pid(1, "PATH", &i); - log_info("pid1 $PATH: '%s'", strna(i)); -} - static void test_protect_errno(void) { errno = 12; { @@ -1138,40 +1076,6 @@ static void test_is_symlink(void) { unlink(name_link); } -static void test_pid_is_unwaited(void) { - pid_t pid; - - pid = fork(); - assert_se(pid >= 0); - if (pid == 0) { - _exit(EXIT_SUCCESS); - } else { - int status; - - waitpid(pid, &status, 0); - assert_se(!pid_is_unwaited(pid)); - } - assert_se(pid_is_unwaited(getpid())); - assert_se(!pid_is_unwaited(-1)); -} - -static void test_pid_is_alive(void) { - pid_t pid; - - pid = fork(); - assert_se(pid >= 0); - if (pid == 0) { - _exit(EXIT_SUCCESS); - } else { - int status; - - waitpid(pid, &status, 0); - assert_se(!pid_is_alive(pid)); - } - assert_se(pid_is_alive(getpid())); - assert_se(!pid_is_alive(-1)); -} - static void test_search_and_fopen(void) { const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL}; char name[] = "/tmp/test-search_and_fopen.XXXXXX"; @@ -1625,7 +1529,6 @@ int main(int argc, char *argv[]) { test_memdup_multiply(); test_hostname_is_valid(); test_u64log2(); - test_get_process_comm(); test_protect_errno(); test_parse_size(); test_config_parse_iec_off(); @@ -1654,8 +1557,6 @@ int main(int argc, char *argv[]) { test_strshorten(); test_strjoina(); test_is_symlink(); - test_pid_is_unwaited(); - test_pid_is_alive(); test_search_and_fopen(); test_search_and_fopen_nulstr(); test_glob_exists(); -- cgit v1.2.3-54-g00ecf