]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
tests: command: Check if something is a mountpoint
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 16 Aug 2022 15:32:02 +0000 (15:32 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 16 Aug 2022 15:32:02 +0000 (15:32 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/stub/command.c

index 3fa7802d3bf8995c6ea98dbeed39583708d968ba..458b50b3b7a9e2ef06423a7b3b6579e4df3b919f 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <linux/limits.h>
+#include <mntent.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -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