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