From: Michael Tremer Date: Tue, 16 Aug 2022 15:32:02 +0000 (+0000) Subject: tests: command: Check if something is a mountpoint X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a88b80ec14e7ee44a93d031630493ede7615fbbd;p=people%2Fstevee%2Fpakfire.git tests: command: Check if something is a mountpoint Signed-off-by: Michael Tremer --- diff --git a/tests/stub/command.c b/tests/stub/command.c index 3fa7802d..458b50b3 100644 --- a/tests/stub/command.c +++ b/tests/stub/command.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include #include @@ -49,6 +50,45 @@ static int random_string(char* s, size_t length) { return 0; } +static int check_mountpoint(int argc, char* argv[]) { + if (argc < 1) { + fprintf(stderr, "check-mountpoint requires an argument\n"); + return EXIT_FAILURE; + } + + const char* mp = argv[0]; + int r = 1; + + FILE* f = fopen("/proc/self/mounts", "r"); + if (!f) { + fprintf(stderr, "Could not open /proc/self/mounts: %m\n"); + return 1; + } + + // Walk through all mountpoints + for (;;) { + struct mntent* entry = getmntent(f); + if (!entry) + break; + + // Check for match + if (strcmp(entry->mnt_dir, mp) == 0) { + r = 0; + break; + } + } + + fclose(f); + + // Print something useful + if (r) + printf("%s is not a mountpoint\n", mp); + else + printf("%s is a mointpoint\n", mp); + + return r; +} + static int check_open_file_descriptors(int argc, char* argv[]) { struct rlimit limits; int r; @@ -311,7 +351,10 @@ int main(int argc, char* argv[]) { int (*callback)(int argc, char* argv[]) = NULL; const char* command = argv[1]; - if (strcmp(command, "check-open-file-descriptors") == 0) + if (strcmp(command, "check-mountpoint") == 0) + callback = check_mountpoint; + + else if (strcmp(command, "check-open-file-descriptors") == 0) callback = check_open_file_descriptors; // Echo