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; |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 15 | int mountpoint_main(int argc, char **argv) |
| 16 | { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 17 | struct stat st; |
| 18 | char *arg; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 19 | int opt = getopt32(argv, "qdx"); |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 20 | #define OPT_q (1) |
| 21 | #define OPT_d (2) |
| 22 | #define OPT_x (4) |
| 23 | |
| 24 | if (optind != argc - 1) |
| 25 | bb_show_usage(); |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 26 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 27 | arg = argv[optind]; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 28 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 29 | if ( (opt & OPT_x && stat(arg, &st) == 0) || (lstat(arg, &st) == 0) ) { |
| 30 | if (opt & OPT_x) { |
| 31 | if (S_ISBLK(st.st_mode)) { |
| 32 | printf("%u:%u\n", major(st.st_rdev), |
| 33 | minor(st.st_rdev)); |
| 34 | return EXIT_SUCCESS; |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 35 | } else { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 36 | if (opt & OPT_q) |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 37 | bb_putchar('\n'); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 38 | else |
| 39 | bb_error_msg("%s: not a block device", arg); |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 40 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 41 | return EXIT_FAILURE; |
| 42 | } else |
| 43 | if (S_ISDIR(st.st_mode)) { |
| 44 | dev_t st_dev = st.st_dev; |
| 45 | ino_t st_ino = st.st_ino; |
| 46 | char *p = xasprintf("%s/..", arg); |
| 47 | |
| 48 | if (stat(p, &st) == 0) { |
| 49 | int ret = (st_dev != st.st_dev) || |
| 50 | (st_dev == st.st_dev && st_ino == st.st_ino); |
| 51 | if (opt & OPT_d) |
| 52 | printf("%u:%u\n", major(st_dev), minor(st_dev)); |
| 53 | else if (!(opt & OPT_q)) |
| 54 | printf("%s is %sa mountpoint\n", arg, ret?"":"not "); |
| 55 | return !ret; |
| 56 | } |
| 57 | } else { |
| 58 | if (!(opt & OPT_q)) |
| 59 | bb_error_msg("%s: not a directory", arg); |
| 60 | return EXIT_FAILURE; |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 61 | } |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 62 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 63 | if (!(opt & OPT_q)) |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 64 | bb_simple_perror_msg(arg); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 65 | return EXIT_FAILURE; |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 66 | } |