From: Thomas Weißschuh Date: Sat, 24 Dec 2022 16:09:24 +0000 (+0000) Subject: lib: procfs: prefix support for tests X-Git-Tag: v2.39-rc1~356^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a446842f5d0a222ad5a884ce6875df698055d360;p=thirdparty%2Futil-linux.git lib: procfs: prefix support for tests This will be used in the unitttests. --- diff --git a/lib/procfs.c b/lib/procfs.c index e7a4cee80c..1789505326 100644 --- a/lib/procfs.c +++ b/lib/procfs.c @@ -446,7 +446,7 @@ char *pid_get_cmdline(pid_t pid) #ifdef TEST_PROGRAM_PROCFS -static int test_tasks(int argc, char *argv[]) +static int test_tasks(int argc, char *argv[], const char *prefix) { DIR *sub = NULL; struct path_cxt *pc; @@ -458,7 +458,7 @@ static int test_tasks(int argc, char *argv[]) pid = strtol(argv[1], (char **) NULL, 10); printf("PID=%d, TIDs:", pid); - pc = ul_new_procfs_path(pid, NULL); + pc = ul_new_procfs_path(pid, prefix); if (!pc) err(EXIT_FAILURE, "alloc procfs handler failed"); @@ -470,7 +470,7 @@ static int test_tasks(int argc, char *argv[]) return EXIT_SUCCESS; } -static int test_fds(int argc, char *argv[]) +static int test_fds(int argc, char *argv[], const char *prefix) { DIR *sub = NULL; struct path_cxt *pc; @@ -483,7 +483,7 @@ static int test_fds(int argc, char *argv[]) pid = strtol(argv[1], (char **) NULL, 10); printf("PID=%d, FDs:", pid); - pc = ul_new_procfs_path(pid, NULL); + pc = ul_new_procfs_path(pid, prefix); if (!pc) err(EXIT_FAILURE, "alloc procfs handler failed"); @@ -530,7 +530,7 @@ static int test_processes(int argc, char *argv[]) return EXIT_SUCCESS; } -static int test_one_process(int argc, char *argv[]) +static int test_one_process(int argc, char *argv[], const char *prefix) { pid_t pid; struct path_cxt *pc; @@ -541,7 +541,7 @@ static int test_one_process(int argc, char *argv[]) return EXIT_FAILURE; pid = strtol(argv[1], (char **) NULL, 10); - pc = ul_new_procfs_path(pid, NULL); + pc = ul_new_procfs_path(pid, prefix); if (!pc) err(EXIT_FAILURE, "cannot alloc procfs handler"); @@ -576,7 +576,7 @@ static int test_isprocfs(int argc, char *argv[]) return is ? EXIT_SUCCESS : EXIT_FAILURE; } -static int test_process_stat_nth(int argc, char *argv[]) +static int test_process_stat_nth(int argc, char *argv[], const char *prefix) { pid_t pid; struct path_cxt *pc; @@ -588,7 +588,7 @@ static int test_process_stat_nth(int argc, char *argv[]) pid = strtol(argv[1], (char **) NULL, 10); n = strtol(argv[2], (char **) NULL, 10); - pc = ul_new_procfs_path(pid, NULL); + pc = ul_new_procfs_path(pid, prefix); if (!pc) err(EXIT_FAILURE, "cannot alloc procfs handler"); @@ -603,29 +603,37 @@ static int test_process_stat_nth(int argc, char *argv[]) int main(int argc, char *argv[]) { + const char *prefix = NULL; + + if (argc > 2 && strcmp(argv[1], "--prefix") == 0) { + prefix = argv[2]; + argc -= 2; + argv += 2; + } + if (argc < 2) { - fprintf(stderr, "usage: %1$s --tasks \n" - " %1$s --fds \n" + fprintf(stderr, "usage: %1$s [--prefix ] --tasks \n" + " %1$s [--prefix ] --fds \n" " %1$s --is-procfs []\n" " %1$s --processes [--name ] [--uid ]\n" - " %1$s --one \n" - " %1$s --stat-nth \n", + " %1$s [--prefix ] --one \n" + " %1$s [--prefix ] --stat-nth \n", program_invocation_short_name); return EXIT_FAILURE; } if (strcmp(argv[1], "--tasks") == 0) - return test_tasks(argc - 1, argv + 1); + return test_tasks(argc - 1, argv + 1, prefix); if (strcmp(argv[1], "--fds") == 0) - return test_fds(argc - 1, argv + 1); + return test_fds(argc - 1, argv + 1, prefix); if (strcmp(argv[1], "--processes") == 0) return test_processes(argc - 1, argv + 1); if (strcmp(argv[1], "--is-procfs") == 0) return test_isprocfs(argc - 1, argv + 1); if (strcmp(argv[1], "--one") == 0) - return test_one_process(argc - 1, argv + 1); + return test_one_process(argc - 1, argv + 1, prefix); if (strcmp(argv[1], "--stat-nth") == 0) - return test_process_stat_nth(argc - 1, argv + 1); + return test_process_stat_nth(argc - 1, argv + 1, prefix); return EXIT_FAILURE; }