Steven Rostedt (Red Hat) | cde164a | 2015-02-02 14:35:03 -0500 | [diff] [blame] | 1 | #include <errno.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <stdbool.h> |
| 6 | #include <sys/vfs.h> |
| 7 | |
| 8 | #include "findfs.h" |
| 9 | |
| 10 | /* verify that a mountpoint is actually the type we want */ |
| 11 | |
| 12 | int valid_mountpoint(const char *mount, long magic) |
| 13 | { |
| 14 | struct statfs st_fs; |
| 15 | |
| 16 | if (statfs(mount, &st_fs) < 0) |
| 17 | return -ENOENT; |
| 18 | else if ((long)st_fs.f_type != magic) |
| 19 | return -ENOENT; |
| 20 | |
| 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | /* find the path to a mounted file system */ |
| 25 | const char *find_mountpoint(const char *fstype, long magic, |
| 26 | char *mountpoint, int len, |
| 27 | const char * const *known_mountpoints) |
| 28 | { |
| 29 | const char * const *ptr; |
| 30 | char format[128]; |
| 31 | char type[100]; |
| 32 | FILE *fp; |
| 33 | |
| 34 | if (known_mountpoints) { |
| 35 | ptr = known_mountpoints; |
| 36 | while (*ptr) { |
| 37 | if (valid_mountpoint(*ptr, magic) == 0) { |
| 38 | strncpy(mountpoint, *ptr, len - 1); |
| 39 | mountpoint[len-1] = 0; |
| 40 | return mountpoint; |
| 41 | } |
| 42 | ptr++; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /* give up and parse /proc/mounts */ |
| 47 | fp = fopen("/proc/mounts", "r"); |
| 48 | if (fp == NULL) |
| 49 | return NULL; |
| 50 | |
| 51 | snprintf(format, 128, "%%*s %%%ds %%99s %%*s %%*d %%*d\n", len); |
| 52 | |
| 53 | while (fscanf(fp, format, mountpoint, type) == 2) { |
| 54 | if (strcmp(type, fstype) == 0) |
| 55 | break; |
| 56 | } |
| 57 | fclose(fp); |
| 58 | |
| 59 | if (strcmp(type, fstype) != 0) |
| 60 | return NULL; |
| 61 | |
| 62 | return mountpoint; |
| 63 | } |