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