mountpoint.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 (4.5 kb)"
  13. //config: default y
  14. //config: help
  15. //config: mountpoint checks if the directory is a mountpoint.
  16. //applet:IF_MOUNTPOINT(APPLET_NOEXEC(mountpoint, mountpoint, BB_DIR_BIN, BB_SUID_DROP, mountpoint))
  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 = getopt32(argv, "^" "qdxn" "\0" "=1");
  41. #define OPT_q (1)
  42. #define OPT_d (2)
  43. #define OPT_x (4)
  44. #define OPT_n (8)
  45. arg = argv[optind];
  46. msg = "%s";
  47. rc = (opt & OPT_x) ? stat(arg, &st) : lstat(arg, &st);
  48. if (rc != 0)
  49. goto err;
  50. if (opt & OPT_x) {
  51. if (S_ISBLK(st.st_mode)) {
  52. printf("%u:%u\n", major(st.st_rdev),
  53. minor(st.st_rdev));
  54. return EXIT_SUCCESS;
  55. }
  56. errno = 0; /* make perror_msg work as error_msg */
  57. msg = "%s: not a block device";
  58. goto err;
  59. }
  60. errno = ENOTDIR;
  61. if (S_ISDIR(st.st_mode)) {
  62. dev_t st_dev = st.st_dev;
  63. ino_t st_ino = st.st_ino;
  64. char *p = xasprintf("%s/..", arg);
  65. if (stat(p, &st) == 0) {
  66. //int is_mnt = (st_dev != st.st_dev) || (st_dev == st.st_dev && st_ino == st.st_ino);
  67. int is_not_mnt = (st_dev == st.st_dev) && (st_ino != st.st_ino);
  68. if (opt & OPT_d)
  69. printf("%u:%u\n", major(st_dev), minor(st_dev));
  70. if (opt & OPT_n) {
  71. const char *d = find_block_device(arg);
  72. /* name is undefined, but device is mounted -> anonymous superblock! */
  73. /* happens with btrfs */
  74. if (!d) {
  75. d = "UNKNOWN";
  76. /* TODO: iterate /proc/mounts, or /proc/self/mountinfo
  77. * to find out the device name */
  78. }
  79. printf("%s %s\n", d, arg);
  80. }
  81. if (!(opt & (OPT_q | OPT_d | OPT_n)))
  82. printf("%s is %sa mountpoint\n", arg, is_not_mnt ? "not " : "");
  83. return is_not_mnt;
  84. }
  85. arg = p;
  86. /* else: stat had set errno, just fall through */
  87. }
  88. err:
  89. if (!(opt & OPT_q))
  90. bb_perror_msg(msg, arg);
  91. return EXIT_FAILURE;
  92. }