From: Jouke Witteveen Date: Thu, 3 Aug 2017 20:31:46 +0000 (+0200) Subject: test-process-util: test multiple invocations of rename_process X-Git-Tag: v235~271^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F6518%2Fhead;p=thirdparty%2Fsystemd.git test-process-util: test multiple invocations of rename_process --- diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index 046a96fdc88..e7c9766c44f 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -355,38 +355,22 @@ static void test_get_process_cmdline_harder(void) { _exit(0); } -static void test_rename_process_one(const char *p, int ret) { +static void test_rename_process_now(const char *p, int ret) { _cleanup_free_ char *comm = NULL, *cmdline = NULL; - pid_t pid; int r; - pid = fork(); - assert_se(pid >= 0); - - if (pid > 0) { - siginfo_t si; - - assert_se(wait_for_terminate(pid, &si) >= 0); - assert_se(si.si_code == CLD_EXITED); - assert_se(si.si_status == EXIT_SUCCESS); - - return; - } - - /* child */ r = rename_process(p); - assert_se(r == ret || (ret == 0 && r >= 0) || (ret > 0 && r > 0)); if (r < 0) - goto finish; + return; #ifdef HAVE_VALGRIND_VALGRIND_H /* see above, valgrind is weird, we can't verify what we are doing here */ if (RUNNING_ON_VALGRIND) - goto finish; + return; #endif assert_se(get_process_comm(0, &comm) >= 0); @@ -394,11 +378,57 @@ static void test_rename_process_one(const char *p, int ret) { assert_se(strneq(comm, p, 15)); assert_se(get_process_cmdline(0, 0, false, &cmdline) >= 0); - log_info("cmdline = <%s>", cmdline); - assert_se(strneq(p, cmdline, strlen("test-process-util"))); - assert_se(startswith(p, cmdline)); + /* we cannot expect cmdline to be renamed properly without privileges */ + if (geteuid() == 0) { + 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); +} -finish: +static void test_rename_process_one(const char *p, int ret) { + siginfo_t si; + pid_t pid; + + pid = fork(); + assert_se(pid >= 0); + + if (pid == 0) { + /* child */ + test_rename_process_now(p, ret); + _exit(EXIT_SUCCESS); + } + + 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; + + pid = fork(); + assert_se(pid >= 0); + + if (pid > 0) { + siginfo_t si; + + assert_se(wait_for_terminate(pid, &si) >= 0); + assert_se(si.si_code == CLD_EXITED); + assert_se(si.si_status == EXIT_SUCCESS); + + return; + } + + /* child */ + test_rename_process_now("one", 1); + test_rename_process_now("more", 0); /* longer than "one", hence truncated */ + setresuid(99, 99, 99); + 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); _exit(EXIT_SUCCESS); } @@ -408,6 +438,7 @@ static void test_rename_process(void) { 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) {