blob: 8b9e1d779d6fdd6cf79bc961b9609c0b3c9d9382 [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
12//config: bool "mountpoint"
13//config: default y
14//config: help
15//config: mountpoint checks if the directory is a mountpoint.
Rob Landleyd00b3a52005-08-20 05:07:08 +000016
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010017//applet:IF_MOUNTPOINT(APPLET(mountpoint, BB_DIR_BIN, BB_SUID_DROP))
18
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
46 opt_complementary = "=1"; /* must have one argument */
47 opt = getopt32(argv, "qdxn");
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}