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 | */ |
| 11 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 12 | //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" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 16 | //usage: "\n -q Quiet" |
| 17 | //usage: "\n -d Print major/minor device number of the filesystem" |
| 18 | //usage: "\n -n Print device name of the filesystem" |
| 19 | //usage: "\n -x Print major/minor device number of the blockdevice" |
| 20 | //usage: |
| 21 | //usage:#define mountpoint_example_usage |
| 22 | //usage: "$ mountpoint /proc\n" |
| 23 | //usage: "/proc is not a mountpoint\n" |
| 24 | //usage: "$ mountpoint /sys\n" |
| 25 | //usage: "/sys is a mountpoint\n" |
| 26 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 27 | #include "libbb.h" |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 28 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 29 | int mountpoint_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 30 | int mountpoint_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 31 | { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 32 | struct stat st; |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 33 | const char *msg; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 34 | char *arg; |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 35 | int rc, opt; |
| 36 | |
| 37 | opt_complementary = "=1"; /* must have one argument */ |
| 38 | opt = getopt32(argv, "qdxn"); |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 39 | #define OPT_q (1) |
| 40 | #define OPT_d (2) |
| 41 | #define OPT_x (4) |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 42 | #define OPT_n (8) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 43 | arg = argv[optind]; |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 44 | msg = "%s"; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 45 | |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 46 | rc = (opt & OPT_x) ? stat(arg, &st) : lstat(arg, &st); |
| 47 | if (rc != 0) |
| 48 | goto err; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 49 | |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 50 | if (opt & OPT_x) { |
| 51 | if (S_ISBLK(st.st_mode)) { |
| 52 | printf("%u:%u\n", major(st.st_rdev), |
| 53 | minor(st.st_rdev)); |
| 54 | return EXIT_SUCCESS; |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 55 | } |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 56 | errno = 0; /* make perror_msg work as error_msg */ |
| 57 | msg = "%s: not a block device"; |
| 58 | goto err; |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 59 | } |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 60 | |
| 61 | errno = ENOTDIR; |
| 62 | if (S_ISDIR(st.st_mode)) { |
| 63 | dev_t st_dev = st.st_dev; |
| 64 | ino_t st_ino = st.st_ino; |
| 65 | char *p = xasprintf("%s/..", arg); |
| 66 | |
| 67 | if (stat(p, &st) == 0) { |
| 68 | //int is_mnt = (st_dev != st.st_dev) || (st_dev == st.st_dev && st_ino == st.st_ino); |
| 69 | int is_not_mnt = (st_dev == st.st_dev) && (st_ino != st.st_ino); |
| 70 | |
| 71 | if (opt & OPT_d) |
| 72 | printf("%u:%u\n", major(st_dev), minor(st_dev)); |
Vladimir Dronnikov | 4214f8b | 2009-11-01 04:33:23 +0100 | [diff] [blame] | 73 | if (opt & OPT_n) { |
| 74 | const char *d = find_block_device(arg); |
| 75 | /* name is undefined, but device is mounted -> anonymous superblock! */ |
| 76 | /* happens with btrfs */ |
| 77 | if (!d) { |
| 78 | d = "UNKNOWN"; |
| 79 | /* TODO: iterate /proc/mounts, or /proc/self/mountinfo |
| 80 | * to find out the device name */ |
| 81 | } |
| 82 | printf("%s %s\n", d, arg); |
| 83 | } |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 84 | if (!(opt & (OPT_q | OPT_d | OPT_n))) |
| 85 | printf("%s is %sa mountpoint\n", arg, is_not_mnt ? "not " : ""); |
| 86 | return is_not_mnt; |
| 87 | } |
| 88 | arg = p; |
| 89 | /* else: stat had set errno, just fall through */ |
| 90 | } |
| 91 | |
| 92 | err: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 93 | if (!(opt & OPT_q)) |
Denis Vlasenko | 7a1ddf2 | 2008-11-29 12:54:16 +0000 | [diff] [blame] | 94 | bb_perror_msg(msg, arg); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 95 | return EXIT_FAILURE; |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 96 | } |