blob: a1a5b396ae3347222d2f87c506453683905dd076 [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 */
11
Pere Orga5bc8c002011-04-11 03:29:49 +020012//usage:#define mountpoint_trivial_usage
13//usage: "[-q] <[-dn] DIR | -x DEVICE>"
14//usage:#define mountpoint_full_usage "\n\n"
15//usage: "Check if the directory is a mountpoint\n"
16//usage: "\nOptions:"
17//usage: "\n -q Quiet"
18//usage: "\n -d Print major/minor device number of the filesystem"
19//usage: "\n -n Print device name of the filesystem"
20//usage: "\n -x Print major/minor device number of the blockdevice"
21//usage:
22//usage:#define mountpoint_example_usage
23//usage: "$ mountpoint /proc\n"
24//usage: "/proc is not a mountpoint\n"
25//usage: "$ mountpoint /sys\n"
26//usage: "/sys is a mountpoint\n"
27
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Rob Landleyd00b3a52005-08-20 05:07:08 +000029
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000030int mountpoint_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000031int mountpoint_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyd00b3a52005-08-20 05:07:08 +000032{
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000033 struct stat st;
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000034 const char *msg;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000035 char *arg;
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000036 int rc, opt;
37
38 opt_complementary = "=1"; /* must have one argument */
39 opt = getopt32(argv, "qdxn");
Rob Landleyd00b3a52005-08-20 05:07:08 +000040#define OPT_q (1)
41#define OPT_d (2)
42#define OPT_x (4)
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000043#define OPT_n (8)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000044 arg = argv[optind];
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000045 msg = "%s";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000046
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000047 rc = (opt & OPT_x) ? stat(arg, &st) : lstat(arg, &st);
48 if (rc != 0)
49 goto err;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000050
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000051 if (opt & OPT_x) {
52 if (S_ISBLK(st.st_mode)) {
53 printf("%u:%u\n", major(st.st_rdev),
54 minor(st.st_rdev));
55 return EXIT_SUCCESS;
Rob Landleyd00b3a52005-08-20 05:07:08 +000056 }
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000057 errno = 0; /* make perror_msg work as error_msg */
58 msg = "%s: not a block device";
59 goto err;
Rob Landleyd00b3a52005-08-20 05:07:08 +000060 }
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000061
62 errno = ENOTDIR;
63 if (S_ISDIR(st.st_mode)) {
64 dev_t st_dev = st.st_dev;
65 ino_t st_ino = st.st_ino;
66 char *p = xasprintf("%s/..", arg);
67
68 if (stat(p, &st) == 0) {
69 //int is_mnt = (st_dev != st.st_dev) || (st_dev == st.st_dev && st_ino == st.st_ino);
70 int is_not_mnt = (st_dev == st.st_dev) && (st_ino != st.st_ino);
71
72 if (opt & OPT_d)
73 printf("%u:%u\n", major(st_dev), minor(st_dev));
Vladimir Dronnikov4214f8b2009-11-01 04:33:23 +010074 if (opt & OPT_n) {
75 const char *d = find_block_device(arg);
76 /* name is undefined, but device is mounted -> anonymous superblock! */
77 /* happens with btrfs */
78 if (!d) {
79 d = "UNKNOWN";
80 /* TODO: iterate /proc/mounts, or /proc/self/mountinfo
81 * to find out the device name */
82 }
83 printf("%s %s\n", d, arg);
84 }
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000085 if (!(opt & (OPT_q | OPT_d | OPT_n)))
86 printf("%s is %sa mountpoint\n", arg, is_not_mnt ? "not " : "");
87 return is_not_mnt;
88 }
89 arg = p;
90 /* else: stat had set errno, just fall through */
91 }
92
93 err:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000094 if (!(opt & OPT_q))
Denis Vlasenko7a1ddf22008-11-29 12:54:16 +000095 bb_perror_msg(msg, arg);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000096 return EXIT_FAILURE;
Rob Landleyd00b3a52005-08-20 05:07:08 +000097}