Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * mountpoint implementation for busybox |
| 4 | * |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 5 | * Copyright (C) 2005 Bernhard Reutner-Fischer |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 8 | * |
| 9 | * Based on sysvinit's mountpoint |
| 10 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 11 | //config:config MOUNTPOINT |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 12 | //config: bool "mountpoint (4.9 kb)" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 13 | //config: default y |
| 14 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 15 | //config: mountpoint checks if the directory is a mountpoint. |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 16 | |
Denys Vlasenko | 9f59849 | 2017-08-05 01:29:12 +0200 | [diff] [blame] | 17 | //applet:IF_MOUNTPOINT(APPLET_NOEXEC(mountpoint, mountpoint, BB_DIR_BIN, BB_SUID_DROP, mountpoint)) |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 18 | |
| 19 | //kbuild:lib-$(CONFIG_MOUNTPOINT) += mountpoint.o |
| 20 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 21 | //usage:#define mountpoint_trivial_usage |
Denys Vlasenko | 6f7b10c | 2021-06-13 03:51:55 +0200 | [diff] [blame] | 22 | //usage: "[-q] { [-dn] DIR | -x DEVICE }" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 23 | //usage:#define mountpoint_full_usage "\n\n" |
Denys Vlasenko | 6f7b10c | 2021-06-13 03:51:55 +0200 | [diff] [blame] | 24 | //usage: "Check if DIR is a mountpoint\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 25 | //usage: "\n -q Quiet" |
Denys Vlasenko | 6f7b10c | 2021-06-13 03:51:55 +0200 | [diff] [blame] | 26 | //usage: "\n -d Print major:minor of the filesystem" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 27 | //usage: "\n -n Print device name of the filesystem" |
Denys Vlasenko | 6f7b10c | 2021-06-13 03:51:55 +0200 | [diff] [blame] | 28 | //////// -n is not supported by util-linux-2.36.1 ^^^^^^^^^^^^^^^^^^ |
| 29 | //usage: "\n -x Print major:minor of DEVICE" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 30 | //usage: |
| 31 | //usage:#define mountpoint_example_usage |
| 32 | //usage: "$ mountpoint /proc\n" |
| 33 | //usage: "/proc is not a mountpoint\n" |
| 34 | //usage: "$ mountpoint /sys\n" |
| 35 | //usage: "/sys is a mountpoint\n" |
| 36 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 37 | #include "libbb.h" |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 39 | int mountpoint_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 40 | int mountpoint_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 41 | { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 42 | struct stat st; |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 43 | const char *msg; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 44 | char *arg; |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 45 | int rc, opt; |
| 46 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 47 | opt = getopt32(argv, "^" "qdxn" "\0" "=1"); |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 48 | #define OPT_q (1) |
| 49 | #define OPT_d (2) |
| 50 | #define OPT_x (4) |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 51 | #define OPT_n (8) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 52 | arg = argv[optind]; |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 53 | msg = "%s"; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 54 | |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 55 | rc = (opt & OPT_x) ? stat(arg, &st) : lstat(arg, &st); |
| 56 | if (rc != 0) |
| 57 | goto err; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 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; |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 64 | } |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 65 | errno = 0; /* make perror_msg work as error_msg */ |
| 66 | msg = "%s: not a block device"; |
| 67 | goto err; |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 68 | } |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 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)); |
Vladimir Dronnikov | 4214f8b | 2009-11-01 04:33:23 +0100 | [diff] [blame] | 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 | } |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 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: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 102 | if (!(opt & OPT_q)) |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 103 | bb_perror_msg(msg, arg); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 104 | return EXIT_FAILURE; |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 105 | } |