]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib: procfs: prefix support for tests
authorThomas Weißschuh <thomas@t-8ch.de>
Sat, 24 Dec 2022 16:09:24 +0000 (16:09 +0000)
committerThomas Weißschuh <thomas@t-8ch.de>
Sat, 24 Dec 2022 17:01:35 +0000 (17:01 +0000)
This will be used in the unitttests.

lib/procfs.c

index e7a4cee80c2de421a1c6c21b91a588138bb93d55..1789505326d91eed375270a8656ff5a51c9292d3 100644 (file)
@@ -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 <pid>\n"
-                               "       %1$s --fds <pid>\n"
+               fprintf(stderr, "usage: %1$s [--prefix <prefix>] --tasks <pid>\n"
+                               "       %1$s [--prefix <prefix>] --fds <pid>\n"
                                "       %1$s --is-procfs [<dir>]\n"
                                "       %1$s --processes [--name <name>] [--uid <uid>]\n"
-                               "       %1$s --one <pid>\n"
-                               "       %1$s --stat-nth <pid> <n>\n",
+                               "       %1$s [--prefix <prefix>] --one <pid>\n"
+                               "       %1$s [--prefix <prefix>] --stat-nth <pid> <n>\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;
 }