From: Zbigniew Jędrzejewski-Szmek Date: Thu, 1 Apr 2021 14:46:01 +0000 (+0200) Subject: test-process-util: add test that prints all cmdlines X-Git-Tag: v249-rc1~274^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=510c7a953ea5e12d8f2e4ab243dec546eb0325a1;p=thirdparty%2Fsystemd.git test-process-util: add test that prints all cmdlines --- diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index f79df46d293..ca21eadaba6 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -15,6 +15,8 @@ #include "alloc-util.h" #include "architecture.h" #include "errno-util.h" +#include "errno-list.h" +#include "dirent-util.h" #include "fd-util.h" #include "log.h" #include "macro.h" @@ -90,6 +92,52 @@ static void test_get_process_comm(pid_t pid) { log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i)); } +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)); +} + +static void test_get_process_cmdline(void) { + _cleanup_closedir_ DIR *d = NULL; + struct dirent *de; + + log_info("/* %s */", __func__); + + assert_se(d = opendir("/proc")); + + FOREACH_DIRENT(de, d, return) { + pid_t pid; + + dirent_ensure_type(d, de); + + 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; @@ -808,6 +856,7 @@ int main(int argc, char *argv[]) { } test_get_process_comm_escape(); + test_get_process_cmdline(); test_pid_is_unwaited(); test_pid_is_alive(); test_personality();