mountpoint.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. //config:config MOUNTPOINT
  12. //config: bool "mountpoint"
  13. //config: default y
  14. //config: help
  15. //config: mountpoint checks if the directory is a mountpoint.
  16. //applet:IF_MOUNTPOINT(APPLET(mountpoint, BB_DIR_BIN, BB_SUID_DROP))
  17. //kbuild:lib-$(CONFIG_MOUNTPOINT) += mountpoint.o
  18. //usage:#define mountpoint_trivial_usage
  19. //usage: "[-q] <[-dn] DIR | -x DEVICE>"
  20. //usage:#define mountpoint_full_usage "\n\n"
  21. //usage: "Check if the directory is a mountpoint\n"
  22. //usage: "\n -q Quiet"
  23. //usage: "\n -d Print major/minor device number of the filesystem"
  24. //usage: "\n -n Print device name of the filesystem"
  25. //usage: "\n -x Print major/minor device number of the blockdevice"
  26. //usage:
  27. //usage:#define mountpoint_example_usage
  28. //usage: "$ mountpoint /proc\n"
  29. //usage: "/proc is not a mountpoint\n"
  30. //usage: "$ mountpoint /sys\n"
  31. //usage: "/sys is a mountpoint\n"
  32. #include "libbb.h"
  33. int mountpoint_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  34. int mountpoint_main(int argc UNUSED_PARAM, char **argv)
  35. {
  36. struct stat st;
  37. const char *msg;
  38. char *arg;
  39. int rc, opt;
  40. opt_complementary = "=1"; /* must have one argument */
  41. opt = getopt32(argv, "qdxn");
  42. #define OPT_q (1)
  43. #define OPT_d (2)
  44. #define OPT_x (4)
  45. #define OPT_n (8)
  46. arg = argv[optind];
  47. msg = "%s";
  48. rc = (opt & OPT_x) ? stat(arg, &st) : lstat(arg, &st);
  49. if (rc != 0)
  50. goto err;
  51. if (opt & OPT_x) {
  52. if (S_ISBLK(st.st_mode)) {
  53. printf("%u:%u\n", major(st.st_rdev),
  54. minor(st.st_rdev));
  55. return EXIT_SUCCESS;
  56. }
  57. errno = 0; /* make perror_msg work as error_msg */
  58. msg = "%s: not a block device";
  59. goto err;
  60. }
  61. errno = ENOTDIR;
  62. if (S_ISDIR(st.st_mode)) {
  63. dev_t st_dev = st.st_dev;
  64. ino_t st_ino = st.st_ino;
  65. char *p = xasprintf("%s/..", arg);
  66. if (stat(p, &st) == 0) {
  67. //int is_mnt = (st_dev != st.st_dev) || (st_dev == st.st_dev && st_ino == st.st_ino);
  68. int is_not_mnt = (st_dev == st.st_dev) && (st_ino != st.st_ino);
  69. if (opt & OPT_d)
  70. printf("%u:%u\n", major(st_dev), minor(st_dev));
  71. if (opt & OPT_n) {
  72. const char *d = find_block_device(arg);
  73. /* name is undefined, but device is mounted -> anonymous superblock! */
  74. /* happens with btrfs */
  75. if (!d) {
  76. d = "UNKNOWN";
  77. /* TODO: iterate /proc/mounts, or /proc/self/mountinfo
  78. * to find out the device name */
  79. }
  80. printf("%s %s\n", d, arg);
  81. }
  82. if (!(opt & (OPT_q | OPT_d | OPT_n)))
  83. printf("%s is %sa mountpoint\n", arg, is_not_mnt ? "not " : "");
  84. return is_not_mnt;
  85. }
  86. arg = p;
  87. /* else: stat had set errno, just fall through */
  88. }
  89. err:
  90. if (!(opt & OPT_q))
  91. bb_perror_msg(msg, arg);
  92. return EXIT_FAILURE;
  93. }