blob: 6b21a5fb3eb93543f8ff65f638de0c2f8801b2f4 [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 Vlasenko4eed2c62017-07-18 22:01:24 +020012//config: bool "mountpoint (4.5 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
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"
Pere Orga5bc8c002011-04-11 03:29:49 +020025//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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000036#include "libbb.h"
Rob Landleyd00b3a52005-08-20 05:07:08 +000037
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000038int mountpoint_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000039int mountpoint_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyd00b3a52005-08-20 05:07:08 +000040{
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000041 struct stat st;
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000042 const char *msg;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000043 char *arg;
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000044 int rc, opt;
45
Denys Vlasenko22542ec2017-08-08 21:55:02 +020046 opt = getopt32(argv, "^" "qdxn" "\0" "=1");
Rob Landleyd00b3a52005-08-20 05:07:08 +000047#define OPT_q (1)
48#define OPT_d (2)
49#define OPT_x (4)
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000050#define OPT_n (8)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000051 arg = argv[optind];
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000052 msg = "%s";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000053
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000054 rc = (opt & OPT_x) ? stat(arg, &st) : lstat(arg, &st);
55 if (rc != 0)
56 goto err;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000057
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000058 if (opt & OPT_x) {
59 if (S_ISBLK(st.st_mode)) {
60 printf("%u:%u\n", major(st.st_rdev),
61 minor(st.st_rdev));
62 return EXIT_SUCCESS;
Rob Landleyd00b3a52005-08-20 05:07:08 +000063 }
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000064 errno = 0; /* make perror_msg work as error_msg */
65 msg = "%s: not a block device";
66 goto err;
Rob Landleyd00b3a52005-08-20 05:07:08 +000067 }
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000068
69 errno = ENOTDIR;
70 if (S_ISDIR(st.st_mode)) {
71 dev_t st_dev = st.st_dev;
72 ino_t st_ino = st.st_ino;
73 char *p = xasprintf("%s/..", arg);
74
75 if (stat(p, &st) == 0) {
76 //int is_mnt = (st_dev != st.st_dev) || (st_dev == st.st_dev && st_ino == st.st_ino);
77 int is_not_mnt = (st_dev == st.st_dev) && (st_ino != st.st_ino);
78
79 if (opt & OPT_d)
80 printf("%u:%u\n", major(st_dev), minor(st_dev));
Vladimir Dronnikov4214f8b2009-11-01 04:33:23 +010081 if (opt & OPT_n) {
82 const char *d = find_block_device(arg);
83 /* name is undefined, but device is mounted -> anonymous superblock! */
84 /* happens with btrfs */
85 if (!d) {
86 d = "UNKNOWN";
87 /* TODO: iterate /proc/mounts, or /proc/self/mountinfo
88 * to find out the device name */
89 }
90 printf("%s %s\n", d, arg);
91 }
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000092 if (!(opt & (OPT_q | OPT_d | OPT_n)))
93 printf("%s is %sa mountpoint\n", arg, is_not_mnt ? "not " : "");
94 return is_not_mnt;
95 }
96 arg = p;
97 /* else: stat had set errno, just fall through */
98 }
99
100 err:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000101 if (!(opt & OPT_q))
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +0000102 bb_perror_msg(msg, arg);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000103 return EXIT_FAILURE;
Rob Landleyd00b3a52005-08-20 05:07:08 +0000104}