findfs.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #include "findfs.h"
  8. /* verify that a mountpoint is actually the type we want */
  9. int valid_mountpoint(const char *mount, long magic)
  10. {
  11. struct statfs st_fs;
  12. if (statfs(mount, &st_fs) < 0)
  13. return -ENOENT;
  14. else if ((long)st_fs.f_type != magic)
  15. return -ENOENT;
  16. return 0;
  17. }
  18. /* find the path to a mounted file system */
  19. const char *find_mountpoint(const char *fstype, long magic,
  20. char *mountpoint, int len,
  21. const char * const *known_mountpoints)
  22. {
  23. const char * const *ptr;
  24. char format[128];
  25. char type[100];
  26. FILE *fp;
  27. if (known_mountpoints) {
  28. ptr = known_mountpoints;
  29. while (*ptr) {
  30. if (valid_mountpoint(*ptr, magic) == 0) {
  31. strncpy(mountpoint, *ptr, len - 1);
  32. mountpoint[len-1] = 0;
  33. return mountpoint;
  34. }
  35. ptr++;
  36. }
  37. }
  38. /* give up and parse /proc/mounts */
  39. fp = fopen("/proc/mounts", "r");
  40. if (fp == NULL)
  41. return NULL;
  42. snprintf(format, 128, "%%*s %%%ds %%99s %%*s %%*d %%*d\n", len);
  43. while (fscanf(fp, format, mountpoint, type) == 2) {
  44. if (strcmp(type, fstype) == 0)
  45. break;
  46. }
  47. fclose(fp);
  48. if (strcmp(type, fstype) != 0)
  49. return NULL;
  50. return mountpoint;
  51. }