| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sched.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#if HAVE_VALGRIND_VALGRIND_H |
| 7 |
# include <valgrind/valgrind.h> |
| 8 |
#endif |
| 9 |
|
| 10 |
#include "argv-util.h" |
| 11 |
#include "process-util.h" |
| 12 |
#include "tests.h" |
| 13 |
#include "virt.h" |
| 14 |
|
| 15 |
static void test_rename_process_now(const char *p, int ret) { |
| 16 |
_cleanup_free_ char *comm = NULL, *cmdline = NULL; |
| 17 |
int r; |
| 18 |
|
| 19 |
log_info("/* %s(%s) */", __func__, p); |
| 20 |
|
| 21 |
r = rename_process(p); |
| 22 |
assert_se(r == ret || |
| 23 |
(ret == 0 && r >= 0) || |
| 24 |
(ret > 0 && r > 0)); |
| 25 |
|
| 26 |
log_debug_errno(r, "rename_process(%s): %m", p); |
| 27 |
|
| 28 |
if (r < 0) |
| 29 |
return; |
| 30 |
|
| 31 |
#if HAVE_VALGRIND_VALGRIND_H |
| 32 |
/* see above, valgrind is weird, we can't verify what we are doing here */ |
| 33 |
if (RUNNING_ON_VALGRIND) |
| 34 |
return; |
| 35 |
#endif |
| 36 |
|
| 37 |
assert_se(pid_get_comm(0, &comm) >= 0); |
| 38 |
log_debug("comm = <%s>", comm); |
| 39 |
assert_se(strneq(comm, p, TASK_COMM_LEN-1)); |
| 40 |
/* We expect comm to be at most 16 bytes (TASK_COMM_LEN). The kernel may raise this limit in the |
| 41 |
* future. We'd only check the initial part, at least until we recompile, but this will still pass. */ |
| 42 |
|
| 43 |
r = pid_get_cmdline(0, SIZE_MAX, 0, &cmdline); |
| 44 |
assert_se(r >= 0); |
| 45 |
/* we cannot expect cmdline to be renamed properly without privileges */ |
| 46 |
if (geteuid() == 0) { |
| 47 |
if (r == 0 && detect_container() > 0) |
| 48 |
log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline); |
| 49 |
else { |
| 50 |
log_info("cmdline = <%s> (expected <%.*s>)", cmdline, (int) strlen("test-process-util"), p); |
| 51 |
|
| 52 |
bool skip = cmdline[0] == '"'; /* A shortcut to check if the string is quoted */ |
| 53 |
|
| 54 |
assert_se(strneq(cmdline + skip, p, strlen("test-process-util"))); |
| 55 |
assert_se(startswith(cmdline + skip, p)); |
| 56 |
} |
| 57 |
} else |
| 58 |
log_info("cmdline = <%s> (not verified)", cmdline); |
| 59 |
} |
| 60 |
|
| 61 |
static void test_rename_process_one(const char *p, int ret) { |
| 62 |
int r; |
| 63 |
|
| 64 |
log_info("/* %s(%s) */", __func__, p); |
| 65 |
|
| 66 |
r = ASSERT_OK(pidref_safe_fork("(rename)", FORK_WAIT|FORK_LOG|FORK_DEATHSIG_SIGKILL, /* ret= */ NULL)); |
| 67 |
|
| 68 |
if (r == 0) { |
| 69 |
/* child */ |
| 70 |
test_rename_process_now(p, ret); |
| 71 |
_exit(EXIT_SUCCESS); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
TEST(rename_process_invalid) { |
| 76 |
ASSERT_ERROR(rename_process(NULL), EINVAL); |
| 77 |
ASSERT_ERROR(rename_process(""), EINVAL); |
| 78 |
} |
| 79 |
|
| 80 |
TEST(rename_process_multi) { |
| 81 |
int r; |
| 82 |
|
| 83 |
r = ASSERT_OK(pidref_safe_fork("(rename)", FORK_WAIT|FORK_LOG|FORK_DEATHSIG_SIGKILL, /* ret= */ NULL)); |
| 84 |
|
| 85 |
if (r == 0) { |
| 86 |
/* child */ |
| 87 |
test_rename_process_now("one", 1); |
| 88 |
test_rename_process_now("more", 0); /* longer than "one", hence truncated */ |
| 89 |
(void) setresuid(99, 99, 99); /* change uid when running privileged */ |
| 90 |
test_rename_process_now("time!", 0); |
| 91 |
test_rename_process_now("0", 1); /* shorter than "one", should fit */ |
| 92 |
_exit(EXIT_SUCCESS); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
TEST(rename_process) { |
| 97 |
test_rename_process_one("foo", 1); /* should always fit */ |
| 98 |
test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */ |
| 99 |
test_rename_process_one("1234567", 1); /* should always fit */ |
| 100 |
} |
| 101 |
|
| 102 |
TEST(argv_help) { |
| 103 |
assert_se(argv_looks_like_help(1, STRV_MAKE("program"))); |
| 104 |
assert_se(argv_looks_like_help(2, STRV_MAKE("program", "help"))); |
| 105 |
assert_se(argv_looks_like_help(3, STRV_MAKE("program", "arg1", "--help"))); |
| 106 |
assert_se(argv_looks_like_help(4, STRV_MAKE("program", "arg1", "arg2", "-h"))); |
| 107 |
assert_se(!argv_looks_like_help(2, STRV_MAKE("program", "arg1"))); |
| 108 |
assert_se(!argv_looks_like_help(4, STRV_MAKE("program", "arg1", "arg2", "--h"))); |
| 109 |
assert_se(!argv_looks_like_help(3, STRV_MAKE("program", "Help", "arg2"))); |
| 110 |
assert_se(argv_looks_like_help(5, STRV_MAKE("program", "--help", "arg1", "-h", "--help"))); |
| 111 |
assert_se(!argv_looks_like_help(4, STRV_MAKE("program","arg1", "arg2", "-H"))); |
| 112 |
assert_se(!argv_looks_like_help(3, STRV_MAKE("program", "--Help", "arg2"))); |
| 113 |
} |
| 114 |
|
| 115 |
static int intro(void) { |
| 116 |
log_show_color(true); |
| 117 |
return EXIT_SUCCESS; |
| 118 |
} |
| 119 |
|
| 120 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro); |