util-lib: rework rename_process() to be able to make use of PR_SET_MM_ARG_START

PR_SET_MM_ARG_START allows us to relatively cleanly implement process renaming.
However, it's only available with privileges. Hence, let's try to make use of
it, and if we can't fall back to the traditional way of overriding argv[0].

This removes size restrictions on the process name shown in argv[] at least for
privileged processes.
This commit is contained in:
Lennart Poettering
2016-12-14 18:29:30 +01:00
parent e932f5407e
commit 9bfaffd5a9
3 changed files with 151 additions and 14 deletions
+61
View File
@@ -355,10 +355,70 @@ static void test_get_process_cmdline_harder(void) {
_exit(0);
}
static void test_rename_process_one(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;
#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;
#endif
assert_se(get_process_comm(0, &comm) >= 0);
log_info("comm = <%s>", comm);
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));
finish:
_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 */
}
int main(int argc, char *argv[]) {
log_set_max_level(LOG_DEBUG);
log_parse_environment();
log_open();
saved_argc = argc;
saved_argv = argv;
if (argc > 1) {
pid_t pid = 0;
@@ -373,6 +433,7 @@ int main(int argc, char *argv[]) {
test_pid_is_alive();
test_personality();
test_get_process_cmdline_harder();
test_rename_process();
return 0;
}