/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <fcntl.h>
+#include <linux/oom.h>
#include <sys/mount.h>
#include <sys/personality.h>
#include <sys/prctl.h>
#include "alloc-util.h"
#include "architecture.h"
+#include "dirent-util.h"
+#include "errno-list.h"
#include "errno-util.h"
#include "fd-util.h"
+#include "ioprio-util.h"
#include "log.h"
#include "macro.h"
#include "missing_sched.h"
#include "missing_syscall.h"
#include "parse-util.h"
#include "process-util.h"
+#include "procfs-util.h"
#include "rlimit-util.h"
#include "signal-util.h"
#include "stdio-util.h"
#include "terminal-util.h"
#include "tests.h"
#include "user-util.h"
-#include "util.h"
#include "virt.h"
-static void test_get_process_comm(pid_t pid) {
+static void test_get_process_comm_one(pid_t pid) {
struct stat st;
_cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
_cleanup_free_ char *env = NULL;
dev_t h;
int r;
+ log_info("/* %s */", __func__);
+
xsprintf(path, "/proc/"PID_FMT"/comm", pid);
if (stat(path, &st) == 0) {
assert_se(get_process_cmdline(pid, 1, 0, &d) >= 0);
log_info("PID"PID_FMT" cmdline truncated to 1: '%s'", pid, d);
- assert_se(get_process_ppid(pid, &e) >= 0);
- log_info("PID"PID_FMT" PPID: "PID_FMT, pid, e);
- assert_se(pid == 1 ? e == 0 : e > 0);
+ r = get_process_ppid(pid, &e);
+ assert_se(pid == 1 ? r == -EADDRNOTAVAIL : r >= 0);
+ if (r >= 0) {
+ log_info("PID"PID_FMT" PPID: "PID_FMT, pid, e);
+ assert_se(e > 0);
+ }
assert_se(is_kernel_thread(pid) == 0 || pid != 1);
log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i));
}
+TEST(get_process_comm) {
+ if (saved_argc > 1) {
+ pid_t pid = 0;
+
+ (void) parse_pid(saved_argv[1], &pid);
+ test_get_process_comm_one(pid);
+ } else {
+ TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm_one(1));
+ test_get_process_comm_one(getpid());
+ }
+}
+
+static void test_get_process_cmdline_one(pid_t pid) {
+ _cleanup_free_ char *c = NULL, *d = NULL, *e = NULL, *f = NULL, *g = NULL, *h = NULL;
+ int r;
+
+ r = get_process_cmdline(pid, SIZE_MAX, 0, &c);
+ log_info("PID "PID_FMT": %s", pid, r >= 0 ? c : errno_to_name(r));
+
+ r = get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &d);
+ log_info(" %s", r >= 0 ? d : errno_to_name(r));
+
+ r = get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_QUOTE, &e);
+ log_info(" %s", r >= 0 ? e : errno_to_name(r));
+
+ r = get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_QUOTE | PROCESS_CMDLINE_COMM_FALLBACK, &f);
+ log_info(" %s", r >= 0 ? f : errno_to_name(r));
+
+ r = get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_QUOTE_POSIX, &g);
+ log_info(" %s", r >= 0 ? g : errno_to_name(r));
+
+ r = get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_QUOTE_POSIX | PROCESS_CMDLINE_COMM_FALLBACK, &h);
+ log_info(" %s", r >= 0 ? h : errno_to_name(r));
+}
+
+TEST(get_process_cmdline) {
+ _cleanup_closedir_ DIR *d = NULL;
+
+ assert_se(d = opendir("/proc"));
+
+ FOREACH_DIRENT(de, d, return) {
+ pid_t pid;
+
+ if (de->d_type != DT_DIR)
+ continue;
+
+ if (parse_pid(de->d_name, &pid) < 0)
+ continue;
+
+ test_get_process_cmdline_one(pid);
+ }
+}
+
static void test_get_process_comm_escape_one(const char *input, const char *output) {
_cleanup_free_ char *n = NULL;
- log_info("input: <%s> — output: <%s>", input, output);
+ log_debug("input: <%s> — output: <%s>", input, output);
assert_se(prctl(PR_SET_NAME, input) >= 0);
assert_se(get_process_comm(0, &n) >= 0);
- log_info("got: <%s>", n);
+ log_debug("got: <%s>", n);
assert_se(streq_ptr(n, output));
}
-static void test_get_process_comm_escape(void) {
+TEST(get_process_comm_escape) {
_cleanup_free_ char *saved = NULL;
assert_se(get_process_comm(0, &saved) >= 0);
assert_se(prctl(PR_SET_NAME, saved) >= 0);
}
-static void test_pid_is_unwaited(void) {
+TEST(pid_is_unwaited) {
pid_t pid;
pid = fork();
assert_se(!pid_is_unwaited(-1));
}
-static void test_pid_is_alive(void) {
+TEST(pid_is_alive) {
pid_t pid;
pid = fork();
assert_se(!pid_is_alive(-1));
}
-static void test_personality(void) {
-
+TEST(personality) {
assert_se(personality_to_string(PER_LINUX));
assert_se(!personality_to_string(PERSONALITY_INVALID));
#endif
}
-static void test_get_process_cmdline_harder(void) {
+TEST(get_process_cmdline_harder) {
char path[] = "/tmp/test-cmdlineXXXXXX";
- _cleanup_close_ int fd = -1;
+ _cleanup_close_ int fd = -EBADF;
_cleanup_free_ char *line = NULL;
pid_t pid;
return;
}
+ /* Set RLIMIT_STACK to infinity to test we don't try to allocate unncessarily large values to read
+ * the cmdline. */
+ if (setrlimit(RLIMIT_STACK, &RLIMIT_MAKE_CONST(RLIM_INFINITY)) < 0)
+ log_warning("Testing without RLIMIT_STACK=infinity");
+
assert_se(unlink(path) >= 0);
assert_se(prctl(PR_SET_NAME, "testa") >= 0);
- assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, 0, &line) == -ENOENT);
+ assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) == -ENOENT);
- assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "[testa]"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 0, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
- log_info("'%s'", line);
+ assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK | PROCESS_CMDLINE_QUOTE, &line) >= 0);
+ log_debug("'%s'", line);
+ assert_se(streq(line, "\"[testa]\"")); /* quoting is enabled here */
+ line = mfree(line);
+
+ assert_se(get_process_cmdline(0, 0, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, ""));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
assert_se(streq(line, "…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
assert_se(streq(line, "[…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
assert_se(streq(line, "[t…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
assert_se(streq(line, "[te…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
assert_se(streq(line, "[tes…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
assert_se(streq(line, "[test…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
assert_se(streq(line, "[testa]"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
assert_se(streq(line, "[testa]"));
line = mfree(line);
+ /* Test with multiple arguments that don't require quoting */
+
assert_se(write(fd, "foo\0bar", 8) == 8);
- assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, 0, &line) >= 0);
- log_info("'%s'", line);
+ assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo bar"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
assert_se(streq(line, "foo bar"));
line = mfree(line);
assert_se(write(fd, "quux", 4) == 4);
- assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, 0, &line) >= 0);
- log_info("'%s'", line);
+ assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo bar quux"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo bar quux"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 1, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 2, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "f…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 3, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "fo…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 4, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 5, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo …"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 6, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo b…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 7, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo ba…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 8, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo bar…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 9, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 9, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo bar …"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo bar q…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo bar qu…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo bar quux"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 13, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 13, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo bar quux"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 14, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 14, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo bar quux"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 1000, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 1000, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "foo bar quux"));
line = mfree(line);
assert_se(ftruncate(fd, 0) >= 0);
assert_se(prctl(PR_SET_NAME, "aaaa bbbb cccc") >= 0);
- assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, 0, &line) == -ENOENT);
+ assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) == -ENOENT);
- assert_se(get_process_cmdline(getpid_cached(), SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "[aaaa bbbb cccc]"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 10, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "[aaaa bbb…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 11, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "[aaaa bbbb…"));
line = mfree(line);
- assert_se(get_process_cmdline(getpid_cached(), 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ assert_se(get_process_cmdline(0, 12, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
+ log_debug("'%s'", line);
assert_se(streq(line, "[aaaa bbbb …"));
line = mfree(line);
- safe_close(fd);
- _exit(EXIT_SUCCESS);
-}
-
-static void test_rename_process_now(const char *p, int ret) {
- _cleanup_free_ char *comm = NULL, *cmdline = NULL;
- int r;
-
- r = rename_process(p);
- assert_se(r == ret ||
- (ret == 0 && r >= 0) ||
- (ret > 0 && r > 0));
-
- if (r < 0)
- return;
-
-#if HAVE_VALGRIND_VALGRIND_H
- /* see above, valgrind is weird, we can't verify what we are doing here */
- if (RUNNING_ON_VALGRIND)
- return;
-#endif
-
- assert_se(get_process_comm(0, &comm) >= 0);
- log_info("comm = <%s>", comm);
- assert_se(strneq(comm, p, TASK_COMM_LEN-1));
- /* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the
- * future. We'd only check the initial part, at least until we recompile, but this will still pass. */
-
- r = get_process_cmdline(0, SIZE_MAX, 0, &cmdline);
- assert_se(r >= 0);
- /* we cannot expect cmdline to be renamed properly without privileges */
- if (geteuid() == 0) {
- if (r == 0 && detect_container() > 0)
- log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline);
- else {
- log_info("cmdline = <%s>", cmdline);
- assert_se(strneq(p, cmdline, STRLEN("test-process-util")));
- assert_se(startswith(p, cmdline));
- }
- } else
- log_info("cmdline = <%s> (not verified)", cmdline);
-}
-
-static void test_rename_process_one(const char *p, int ret) {
- siginfo_t si;
- pid_t pid;
-
- pid = fork();
- assert_se(pid >= 0);
+ /* Test with multiple arguments that do require quoting */
- if (pid == 0) {
- /* child */
- test_rename_process_now(p, ret);
- _exit(EXIT_SUCCESS);
- }
+#define CMDLINE1 "foo\0'bar'\0\"bar$\"\0x y z\0!``\0"
+#define EXPECT1 "foo \"'bar'\" \"\\\"bar\\$\\\"\" \"x y z\" \"!\\`\\`\""
+#define EXPECT1p "foo $'\\'bar\\'' $'\"bar$\"' $'x y z' $'!``'"
+ assert_se(lseek(fd, SEEK_SET, 0) == 0);
+ assert_se(write(fd, CMDLINE1, sizeof CMDLINE1) == sizeof CMDLINE1);
+ assert_se(ftruncate(fd, sizeof CMDLINE1) == 0);
- assert_se(wait_for_terminate(pid, &si) >= 0);
- assert_se(si.si_code == CLD_EXITED);
- assert_se(si.si_status == EXIT_SUCCESS);
-}
-
-static void test_rename_process_multi(void) {
- pid_t pid;
+ assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE, &line) >= 0);
+ log_debug("got: ==%s==", line);
+ log_debug("exp: ==%s==", EXPECT1);
+ assert_se(streq(line, EXPECT1));
+ line = mfree(line);
- pid = fork();
- assert_se(pid >= 0);
+ assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE_POSIX, &line) >= 0);
+ log_debug("got: ==%s==", line);
+ log_debug("exp: ==%s==", EXPECT1p);
+ assert_se(streq(line, EXPECT1p));
+ line = mfree(line);
- if (pid > 0) {
- siginfo_t si;
+#define CMDLINE2 "foo\0\1\2\3\0\0"
+#define EXPECT2 "foo \"\\001\\002\\003\""
+#define EXPECT2p "foo $'\\001\\002\\003'"
+ assert_se(lseek(fd, SEEK_SET, 0) == 0);
+ assert_se(write(fd, CMDLINE2, sizeof CMDLINE2) == sizeof CMDLINE2);
+ assert_se(ftruncate(fd, sizeof CMDLINE2) == 0);
- assert_se(wait_for_terminate(pid, &si) >= 0);
- assert_se(si.si_code == CLD_EXITED);
- assert_se(si.si_status == EXIT_SUCCESS);
+ assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE, &line) >= 0);
+ log_debug("got: ==%s==", line);
+ log_debug("exp: ==%s==", EXPECT2);
+ assert_se(streq(line, EXPECT2));
+ line = mfree(line);
- return;
- }
+ assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE_POSIX, &line) >= 0);
+ log_debug("got: ==%s==", line);
+ log_debug("exp: ==%s==", EXPECT2p);
+ assert_se(streq(line, EXPECT2p));
+ line = mfree(line);
- /* child */
- test_rename_process_now("one", 1);
- test_rename_process_now("more", 0); /* longer than "one", hence truncated */
- (void) setresuid(99, 99, 99); /* change uid when running privileged */
- test_rename_process_now("time!", 0);
- test_rename_process_now("0", 1); /* shorter than "one", should fit */
- test_rename_process_one("", -EINVAL);
- test_rename_process_one(NULL, -EINVAL);
+ safe_close(fd);
_exit(EXIT_SUCCESS);
}
-static void test_rename_process(void) {
- test_rename_process_one(NULL, -EINVAL);
- test_rename_process_one("", -EINVAL);
- test_rename_process_one("foo", 1); /* should always fit */
- test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */
- test_rename_process_one("1234567", 1); /* should always fit */
- test_rename_process_multi(); /* multiple invocations and dropped privileges */
-}
-
-static void test_getpid_cached(void) {
+TEST(getpid_cached) {
siginfo_t si;
pid_t a, b, c, d, e, f, child;
assert_se(si.si_code == CLD_EXITED);
}
-#define MEASURE_ITERATIONS (10000000LLU)
-
-static void test_getpid_measure(void) {
- unsigned long long i;
+TEST(getpid_measure) {
usec_t t, q;
+ unsigned long long iterations = slow_tests_enabled() ? 1000000 : 1000;
+
+ log_info("/* %s (%llu iterations) */", __func__, iterations);
+
t = now(CLOCK_MONOTONIC);
- for (i = 0; i < MEASURE_ITERATIONS; i++)
+ for (unsigned long long i = 0; i < iterations; i++)
(void) getpid();
q = now(CLOCK_MONOTONIC) - t;
- log_info(" glibc getpid(): %lf µs each\n", (double) q / MEASURE_ITERATIONS);
+ log_info(" glibc getpid(): %lf µs each\n", (double) q / iterations);
+
+ iterations *= 50; /* _cached() is about 50 times faster, so we need more iterations */
t = now(CLOCK_MONOTONIC);
- for (i = 0; i < MEASURE_ITERATIONS; i++)
+ for (unsigned long long i = 0; i < iterations; i++)
(void) getpid_cached();
q = now(CLOCK_MONOTONIC) - t;
- log_info("getpid_cached(): %lf µs each\n", (double) q / MEASURE_ITERATIONS);
+ log_info("getpid_cached(): %lf µs each\n", (double) q / iterations);
}
-static void test_safe_fork(void) {
+TEST(safe_fork) {
siginfo_t status;
pid_t pid;
int r;
assert_se(status.si_status == 88);
}
-static void test_pid_to_ptr(void) {
-
+TEST(pid_to_ptr) {
assert_se(PTR_TO_PID(NULL) == 0);
assert_se(PID_TO_PTR(0) == NULL);
assert_se(PTR_TO_PID(PID_TO_PTR(INT32_MIN)) == INT32_MIN);
}
-static void test_ioprio_class_from_to_string_one(const char *val, int expected) {
+static void test_ioprio_class_from_to_string_one(const char *val, int expected, int normalized) {
assert_se(ioprio_class_from_string(val) == expected);
if (expected >= 0) {
_cleanup_free_ char *s = NULL;
unsigned ret;
+ int combined;
assert_se(ioprio_class_to_string_alloc(expected, &s) == 0);
- /* We sometimes get a class number and sometimes a number back */
+ /* We sometimes get a class number and sometimes a name back */
assert_se(streq(s, val) ||
safe_atou(val, &ret) == 0);
+
+ /* Make sure normalization works, i.e. NONE → BE gets normalized */
+ combined = ioprio_normalize(ioprio_prio_value(expected, 0));
+ assert_se(ioprio_prio_class(combined) == normalized);
+ assert_se(expected != IOPRIO_CLASS_NONE || ioprio_prio_data(combined) == 4);
}
}
-static void test_ioprio_class_from_to_string(void) {
- test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE);
- test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT);
- test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE);
- test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE);
- test_ioprio_class_from_to_string_one("0", 0);
- test_ioprio_class_from_to_string_one("1", 1);
- test_ioprio_class_from_to_string_one("7", 7);
- test_ioprio_class_from_to_string_one("8", 8);
- test_ioprio_class_from_to_string_one("9", -EINVAL);
- test_ioprio_class_from_to_string_one("-1", -EINVAL);
+TEST(ioprio_class_from_to_string) {
+ test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE, IOPRIO_CLASS_BE);
+ test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT, IOPRIO_CLASS_RT);
+ test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE, IOPRIO_CLASS_BE);
+ test_ioprio_class_from_to_string_one("idle", IOPRIO_CLASS_IDLE, IOPRIO_CLASS_IDLE);
+ test_ioprio_class_from_to_string_one("0", IOPRIO_CLASS_NONE, IOPRIO_CLASS_BE);
+ test_ioprio_class_from_to_string_one("1", 1, 1);
+ test_ioprio_class_from_to_string_one("7", 7, 7);
+ test_ioprio_class_from_to_string_one("8", 8, 8);
+ test_ioprio_class_from_to_string_one("9", -EINVAL, -EINVAL);
+ test_ioprio_class_from_to_string_one("-1", -EINVAL, -EINVAL);
}
-static void test_setpriority_closest(void) {
+TEST(setpriority_closest) {
int r;
- r = safe_fork("(test-setprio)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT|FORK_LOG, NULL);
+ r = safe_fork("(test-setprio)",
+ FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT|FORK_LOG, NULL);
assert_se(r >= 0);
if (r == 0) {
}
}
-int main(int argc, char *argv[]) {
- test_setup_logging(LOG_DEBUG);
+TEST(get_process_ppid) {
+ uint64_t limit;
+ int r;
- save_argc_argv(argc, argv);
+ assert_se(get_process_ppid(1, NULL) == -EADDRNOTAVAIL);
- if (argc > 1) {
- pid_t pid = 0;
+ /* the process with the PID above the global limit definitely doesn't exist. Verify that */
+ assert_se(procfs_get_pid_max(&limit) >= 0);
+ log_debug("kernel.pid_max = %"PRIu64, limit);
- (void) parse_pid(argv[1], &pid);
- test_get_process_comm(pid);
- } else {
- TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1));
- test_get_process_comm(getpid());
+ if (limit < INT_MAX) {
+ r = get_process_ppid(limit + 1, NULL);
+ log_debug_errno(r, "get_process_limit(%"PRIu64") → %d/%m", limit + 1, r);
+ assert(r == -ESRCH);
}
- test_get_process_comm_escape();
- test_pid_is_unwaited();
- test_pid_is_alive();
- test_personality();
- test_get_process_cmdline_harder();
- test_rename_process();
- test_getpid_cached();
- test_getpid_measure();
- test_safe_fork();
- test_pid_to_ptr();
- test_ioprio_class_from_to_string();
- test_setpriority_closest();
-
- return 0;
+ for (pid_t pid = 0;;) {
+ _cleanup_free_ char *c1 = NULL, *c2 = NULL;
+ pid_t ppid;
+
+ r = get_process_ppid(pid, &ppid);
+ if (r == -EADDRNOTAVAIL) {
+ log_info("No further parent PID");
+ break;
+ }
+
+ assert_se(r >= 0);
+
+ assert_se(get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &c1) >= 0);
+ assert_se(get_process_cmdline(ppid, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &c2) >= 0);
+
+ log_info("Parent of " PID_FMT " (%s) is " PID_FMT " (%s).", pid, c1, ppid, c2);
+
+ pid = ppid;
+ }
+}
+
+TEST(set_oom_score_adjust) {
+ int a, b, r;
+
+ assert_se(get_oom_score_adjust(&a) >= 0);
+
+ r = set_oom_score_adjust(OOM_SCORE_ADJ_MIN);
+ assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r));
+
+ if (r >= 0) {
+ assert_se(get_oom_score_adjust(&b) >= 0);
+ assert_se(b == OOM_SCORE_ADJ_MIN);
+ }
+
+ assert_se(set_oom_score_adjust(a) >= 0);
+ assert_se(get_oom_score_adjust(&b) >= 0);
+ assert_se(b == a);
}
+
+static int intro(void) {
+ log_show_color(true);
+ return EXIT_SUCCESS;
+}
+
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);