mountpoint.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * mountpoint implementation for busybox
  4. *
  5. * Copyright (C) 2005 Bernhard Reutner-Fischer
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. *
  9. * Based on sysvinit's mountpoint
  10. */
  11. //usage:#define mountpoint_trivial_usage
  12. //usage: "[-q] <[-dn] DIR | -x DEVICE>"
  13. //usage:#define mountpoint_full_usage "\n\n"
  14. //usage: "Check if the directory is a mountpoint\n"
  15. //usage: "\n -q Quiet"
  16. //usage: "\n -d Print major/minor device number of the filesystem"
  17. //usage: "\n -n Print device name of the filesystem"
  18. //usage: "\n -x Print major/minor device number of the blockdevice"
  19. //usage:
  20. //usage:#define mountpoint_example_usage
  21. //usage: "$ mountpoint /proc\n"
  22. //usage: "/proc is not a mountpoint\n"
  23. //usage: "$ mountpoint /sys\n"
  24. //usage: "/sys is a mountpoint\n"
  25. #include "libbb.h"
  26. int mountpoint_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  27. int mountpoint_main(int argc UNUSED_PARAM, char **argv)
  28. {
  29. struct stat st;
  30. const char *msg;
  31. char *arg;
  32. int rc, opt;
  33. opt_complementary = "=1"; /* must have one argument */
  34. opt = getopt32(argv, "qdxn");
  35. #define OPT_q (1)
  36. #define OPT_d (2)
  37. #define OPT_x (4)
  38. #define OPT_n (8)
  39. arg = argv[optind];
  40. msg = "%s";
  41. rc = (opt & OPT_x) ? stat(arg, &st) : lstat(arg, &st);
  42. if (rc != 0)
  43. goto err;
  44. if (opt & OPT_x) {
  45. if (S_ISBLK(st.st_mode)) {
  46. printf("%u:%u\n", major(st.st_rdev),
  47. minor(st.st_rdev));
  48. return EXIT_SUCCESS;
  49. }
  50. errno = 0; /* make perror_msg work as error_msg */
  51. msg = "%s: not a block device";
  52. goto err;
  53. }
  54. errno = ENOTDIR;
  55. if (S_ISDIR(st.st_mode)) {
  56. dev_t st_dev = st.st_dev;
  57. ino_t st_ino = st.st_ino;
  58. char *p = xasprintf("%s/..", arg);
  59. if (stat(p, &st) == 0) {
  60. //int is_mnt = (st_dev != st.st_dev) || (st_dev == st.st_dev && st_ino == st.st_ino);
  61. int is_not_mnt = (st_dev == st.st_dev) && (st_ino != st.st_ino);
  62. if (opt & OPT_d)
  63. printf("%u:%u\n", major(st_dev), minor(st_dev));
  64. if (opt & OPT_n) {
  65. const char *d = find_block_device(arg);
  66. /* name is undefined, but device is mounted -> anonymous superblock! */
  67. /* happens with btrfs */
  68. if (!d) {
  69. d = "UNKNOWN";
  70. /* TODO: iterate /proc/mounts, or /proc/self/mountinfo
  71. * to find out the device name */
  72. }
  73. printf("%s %s\n", d, arg);
  74. }
  75. if (!(opt & (OPT_q | OPT_d | OPT_n)))
  76. printf("%s is %sa mountpoint\n", arg, is_not_mnt ? "not " : "");
  77. return is_not_mnt;
  78. }
  79. arg = p;
  80. /* else: stat had set errno, just fall through */
  81. }
  82. err:
  83. if (!(opt & OPT_q))
  84. bb_perror_msg(msg, arg);
  85. return EXIT_FAILURE;
  86. }