]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-process-util.c
tree-wide: use -EBADF for fd initialization
[thirdparty/systemd.git] / src / test / test-process-util.c
index a180f3f9b4dfe5872ea3784494a07d3497eb28ab..1864f8a750b6d43437072c60a9404344fa59daea 100644 (file)
@@ -34,7 +34,6 @@
 #include "terminal-util.h"
 #include "tests.h"
 #include "user-util.h"
-#include "util.h"
 #include "virt.h"
 
 static void test_get_process_comm_one(pid_t pid) {
@@ -135,7 +134,6 @@ static void test_get_process_cmdline_one(pid_t pid) {
 
 TEST(get_process_cmdline) {
         _cleanup_closedir_ DIR *d = NULL;
-        struct dirent *de;
 
         assert_se(d = opendir("/proc"));
 
@@ -242,7 +240,7 @@ TEST(personality) {
 
 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;
 
@@ -484,8 +482,8 @@ TEST(get_process_cmdline_harder) {
         /* Test with multiple arguments that do require quoting */
 
 #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' $'!``' \"\""
+#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);
@@ -503,8 +501,8 @@ TEST(get_process_cmdline_harder) {
         line = mfree(line);
 
 #define CMDLINE2 "foo\0\1\2\3\0\0"
-#define EXPECT2  "foo \"\\001\\002\\003\" \"\" \"\""
-#define EXPECT2p  "foo $'\\001\\002\\003' \"\" \"\""
+#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);
@@ -525,107 +523,6 @@ TEST(get_process_cmdline_harder) {
         _exit(EXIT_SUCCESS);
 }
 
-static void test_rename_process_now(const char *p, int ret) {
-        _cleanup_free_ char *comm = NULL, *cmdline = NULL;
-        int r;
-
-        log_info("/* %s */", __func__);
-
-        r = rename_process(p);
-        assert_se(r == ret ||
-                  (ret == 0 && r >= 0) ||
-                  (ret > 0 && r > 0));
-
-        log_debug_errno(r, "rename_process(%s): %m", p);
-
-        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_debug("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> (expected <%.*s>)", cmdline, (int) strlen("test-process-util"), p);
-
-                        bool skip = cmdline[0] == '"'; /* A shortcut to check if the string is quoted */
-
-                        assert_se(strneq(cmdline + skip, p, strlen("test-process-util")));
-                        assert_se(startswith(cmdline + skip, p));
-                }
-        } 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;
-
-        log_info("/* %s */", __func__);
-
-        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);
-}
-
-TEST(rename_process_multi) {
-        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 */
-        (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);
-        _exit(EXIT_SUCCESS);
-}
-
-TEST(rename_process) {
-        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(getpid_cached) {
         siginfo_t si;
         pid_t a, b, c, d, e, f, child;
@@ -896,4 +793,9 @@ TEST(set_oom_score_adjust) {
         assert_se(b == a);
 }
 
-DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, log_show_color(true), /* no outro */);
+static int intro(void) {
+        log_show_color(true);
+        return EXIT_SUCCESS;
+}
+
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);