blob: 28b1e7a54952b3433907f08949bb7b5adf040efb [file] [log] [blame]
Rob Landleyd00b3a52005-08-20 05:07:08 +00001/* vi: set sw=4 ts=4: */
2/*
3 * mountpoint implementation for busybox
4 *
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00005 * Copyright (C) 2005 Bernhard Reutner-Fischer
Rob Landleyd00b3a52005-08-20 05:07:08 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Rob Landleyd00b3a52005-08-20 05:07:08 +00008 *
9 * Based on sysvinit's mountpoint
10 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +010011//config:config MOUNTPOINT
Denys Vlasenkob097a842018-12-28 03:20:17 +010012//config: bool "mountpoint (4.9 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010013//config: default y
14//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: mountpoint checks if the directory is a mountpoint.
Rob Landleyd00b3a52005-08-20 05:07:08 +000016
Denys Vlasenko9f598492017-08-05 01:29:12 +020017//applet:IF_MOUNTPOINT(APPLET_NOEXEC(mountpoint, mountpoint, BB_DIR_BIN, BB_SUID_DROP, mountpoint))
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010018
19//kbuild:lib-$(CONFIG_MOUNTPOINT) += mountpoint.o
20
Pere Orga5bc8c002011-04-11 03:29:49 +020021//usage:#define mountpoint_trivial_usage
Denys Vlasenko6f7b10c2021-06-13 03:51:55 +020022//usage: "[-q] { [-dn] DIR | -x DEVICE }"
Pere Orga5bc8c002011-04-11 03:29:49 +020023//usage:#define mountpoint_full_usage "\n\n"
Denys Vlasenko6f7b10c2021-06-13 03:51:55 +020024//usage: "Check if DIR is a mountpoint\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020025//usage: "\n -q Quiet"
Denys Vlasenko6f7b10c2021-06-13 03:51:55 +020026//usage: "\n -d Print major:minor of the filesystem"
Pere Orga5bc8c002011-04-11 03:29:49 +020027//usage: "\n -n Print device name of the filesystem"
Denys Vlasenko6f7b10c2021-06-13 03:51:55 +020028//////// -n is not supported by util-linux-2.36.1 ^^^^^^^^^^^^^^^^^^
29//usage: "\n -x Print major:minor of DEVICE"
Pere Orga5bc8c002011-04-11 03:29:49 +020030//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 Vlasenkob6adbf12007-05-26 19:00:18 +000037#include "libbb.h"
Rob Landleyd00b3a52005-08-20 05:07:08 +000038
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000039int mountpoint_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000040int mountpoint_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyd00b3a52005-08-20 05:07:08 +000041{
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000042 struct stat st;
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000043 const char *msg;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000044 char *arg;
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000045 int rc, opt;
46
Denys Vlasenko22542ec2017-08-08 21:55:02 +020047 opt = getopt32(argv, "^" "qdxn" "\0" "=1");
Rob Landleyd00b3a52005-08-20 05:07:08 +000048#define OPT_q (1)
49#define OPT_d (2)
50#define OPT_x (4)
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000051#define OPT_n (8)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000052 arg = argv[optind];
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000053 msg = "%s";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000054
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000055 rc = (opt & OPT_x) ? stat(arg, &st) : lstat(arg, &st);
56 if (rc != 0)
57 goto err;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000058
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000059 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 Landleyd00b3a52005-08-20 05:07:08 +000064 }
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000065 errno = 0; /* make perror_msg work as error_msg */
66 msg = "%s: not a block device";
67 goto err;
Rob Landleyd00b3a52005-08-20 05:07:08 +000068 }
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000069
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 Dronnikov4214f8b2009-11-01 04:33:23 +010082 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 Vlasenko7a1ddf22008-11-29 12:54:16 +000093 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 Vlasenkof0ed3762006-10-26 23:21:47 +0000102 if (!(opt & OPT_q))
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +0000103 bb_perror_msg(msg, arg);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000104 return EXIT_FAILURE;
Rob Landleyd00b3a52005-08-20 05:07:08 +0000105}