]>
Commit | Line | Data |
---|---|---|
2a1d943d KZ |
1 | /* |
2 | * mountpoint(1) - see if a directory is a mountpoint | |
3 | * | |
9e930041 | 4 | * This is libmount based reimplementation of the mountpoint(1) |
2a1d943d KZ |
5 | * from sysvinit project. |
6 | * | |
7 | * | |
8 | * Copyright (C) 2011 Red Hat, Inc. All rights reserved. | |
9 | * Written by Karel Zak <kzak@redhat.com> | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it would be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
7cebf0bb SK |
21 | * You should have received a copy of the GNU General Public License along |
22 | * with this program; if not, write to the Free Software Foundation, Inc., | |
23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
2a1d943d KZ |
24 | */ |
25 | ||
26 | #include <stdio.h> | |
27 | #include <stdlib.h> | |
28 | #include <errno.h> | |
29 | #include <string.h> | |
30 | #include <getopt.h> | |
31 | #include <unistd.h> | |
32 | #include <sys/types.h> | |
33 | #include <sys/stat.h> | |
2a1d943d KZ |
34 | |
35 | #include <libmount.h> | |
36 | ||
37 | #include "nls.h" | |
38 | #include "xalloc.h" | |
39 | #include "c.h" | |
efb8854f | 40 | #include "closestream.h" |
39edf681 | 41 | #include "pathnames.h" |
2a1d943d | 42 | |
e0ecd196 KZ |
43 | #define MOUNTPOINT_EXIT_NOMNT 32 |
44 | ||
f112f41e SK |
45 | struct mountpoint_control { |
46 | char *path; | |
47 | dev_t dev; | |
48 | struct stat st; | |
49 | unsigned int | |
50 | dev_devno:1, | |
51 | fs_devno:1, | |
6d562512 | 52 | nofollow:1, |
f112f41e SK |
53 | quiet:1; |
54 | }; | |
2a1d943d | 55 | |
f112f41e | 56 | static int dir_to_device(struct mountpoint_control *ctl) |
2a1d943d | 57 | { |
39edf681 | 58 | struct libmnt_table *tb = mnt_new_table_from_file(_PATH_PROC_MOUNTINFO); |
2a1d943d | 59 | struct libmnt_fs *fs; |
eb9d3706 | 60 | struct libmnt_cache *cache; |
eac83fbc | 61 | int rc = -1; |
2a1d943d | 62 | |
dfd2c714 KZ |
63 | if (!tb) { |
64 | /* | |
65 | * Fallback. Traditional way to detect mountpoints. This way | |
66 | * is independent on /proc, but not able to detect bind mounts. | |
67 | */ | |
f112f41e | 68 | struct stat pst; |
eb9d3706 | 69 | char buf[PATH_MAX], *cn; |
dfd2c714 KZ |
70 | int len; |
71 | ||
f112f41e | 72 | cn = mnt_resolve_path(ctl->path, NULL); /* canonicalize */ |
eb9d3706 | 73 | |
f112f41e | 74 | len = snprintf(buf, sizeof(buf), "%s/..", cn ? cn : ctl->path); |
eb9d3706 KZ |
75 | free(cn); |
76 | ||
06fa5817 | 77 | if (len < 0 || (size_t) len >= sizeof(buf)) |
04f087ec | 78 | return -1; |
dfd2c714 | 79 | if (stat(buf, &pst) !=0) |
04f087ec | 80 | return -1; |
dfd2c714 | 81 | |
8d69fd43 | 82 | if (ctl->st.st_dev != pst.st_dev || ctl->st.st_ino == pst.st_ino) { |
f112f41e | 83 | ctl->dev = ctl->st.st_dev; |
04f087ec ZL |
84 | return 0; |
85 | } | |
dfd2c714 | 86 | |
04f087ec | 87 | return -1; |
dfd2c714 | 88 | } |
2a1d943d | 89 | |
eb9d3706 KZ |
90 | /* to canonicalize all necessary paths */ |
91 | cache = mnt_new_cache(); | |
92 | mnt_table_set_cache(tb, cache); | |
6195f9e6 | 93 | mnt_unref_cache(cache); |
eb9d3706 | 94 | |
f112f41e | 95 | fs = mnt_table_find_target(tb, ctl->path, MNT_ITER_BACKWARD); |
eac83fbc | 96 | if (fs && mnt_fs_get_target(fs)) { |
f112f41e | 97 | ctl->dev = mnt_fs_get_devno(fs); |
eac83fbc DR |
98 | rc = 0; |
99 | } | |
2a1d943d | 100 | |
50fccba1 | 101 | mnt_unref_table(tb); |
eac83fbc | 102 | return rc; |
2a1d943d KZ |
103 | } |
104 | ||
f112f41e | 105 | static int print_devno(const struct mountpoint_control *ctl) |
2a1d943d | 106 | { |
f112f41e SK |
107 | if (!S_ISBLK(ctl->st.st_mode)) { |
108 | if (!ctl->quiet) | |
109 | warnx(_("%s: not a block device"), ctl->path); | |
2a1d943d KZ |
110 | return -1; |
111 | } | |
f112f41e | 112 | printf("%u:%u\n", major(ctl->st.st_rdev), minor(ctl->st.st_rdev)); |
2a1d943d KZ |
113 | return 0; |
114 | } | |
115 | ||
6e1eda6f | 116 | static void __attribute__((__noreturn__)) usage(void) |
2a1d943d | 117 | { |
6e1eda6f | 118 | FILE *out = stdout; |
30b61b7b | 119 | fputs(USAGE_HEADER, out); |
4f92adb6 KZ |
120 | fprintf(out, |
121 | _(" %1$s [-qd] /path/to/directory\n" | |
122 | " %1$s -x /dev/device\n"), program_invocation_short_name); | |
123 | ||
451dbcfa BS |
124 | fputs(USAGE_SEPARATOR, out); |
125 | fputs(_("Check whether a directory or file is a mountpoint.\n"), out); | |
126 | ||
30b61b7b | 127 | fputs(USAGE_OPTIONS, out); |
4f92adb6 | 128 | fputs(_(" -q, --quiet quiet mode - don't print anything\n" |
6d562512 | 129 | " --nofollow do not follow symlink\n" |
4f92adb6 | 130 | " -d, --fs-devno print maj:min device number of the filesystem\n" |
30b61b7b SK |
131 | " -x, --devno print maj:min device number of the block device\n"), out); |
132 | fputs(USAGE_SEPARATOR, out); | |
bad4c729 MY |
133 | fprintf(out, USAGE_HELP_OPTIONS(20)); |
134 | fprintf(out, USAGE_MAN_TAIL("mountpoint(1)")); | |
2a1d943d | 135 | |
6e1eda6f | 136 | exit(EXIT_SUCCESS); |
2a1d943d KZ |
137 | } |
138 | ||
139 | int main(int argc, char **argv) | |
140 | { | |
ee5de88c | 141 | int c; |
87918040 | 142 | struct mountpoint_control ctl = { NULL }; |
2a1d943d | 143 | |
6d562512 SK |
144 | enum { |
145 | OPT_NOFOLLOW = CHAR_MAX + 1 | |
146 | }; | |
147 | ||
2a1d943d | 148 | static const struct option longopts[] = { |
87918040 | 149 | { "quiet", no_argument, NULL, 'q' }, |
6d562512 | 150 | { "nofollow", no_argument, NULL, OPT_NOFOLLOW }, |
87918040 SK |
151 | { "fs-devno", no_argument, NULL, 'd' }, |
152 | { "devno", no_argument, NULL, 'x' }, | |
153 | { "help", no_argument, NULL, 'h' }, | |
154 | { "version", no_argument, NULL, 'V' }, | |
155 | { NULL, 0, NULL, 0 } | |
2a1d943d KZ |
156 | }; |
157 | ||
158 | setlocale(LC_ALL, ""); | |
159 | bindtextdomain(PACKAGE, LOCALEDIR); | |
160 | textdomain(PACKAGE); | |
2c308875 | 161 | close_stdout_atexit(); |
2a1d943d KZ |
162 | |
163 | mnt_init_debug(0); | |
164 | ||
30b61b7b | 165 | while ((c = getopt_long(argc, argv, "qdxhV", longopts, NULL)) != -1) { |
2a1d943d KZ |
166 | |
167 | switch(c) { | |
168 | case 'q': | |
f112f41e | 169 | ctl.quiet = 1; |
2a1d943d | 170 | break; |
6d562512 SK |
171 | case OPT_NOFOLLOW: |
172 | ctl.nofollow = 1; | |
173 | break; | |
2a1d943d | 174 | case 'd': |
f112f41e | 175 | ctl.fs_devno = 1; |
2a1d943d KZ |
176 | break; |
177 | case 'x': | |
f112f41e | 178 | ctl.dev_devno = 1; |
2a1d943d | 179 | break; |
2c308875 | 180 | |
2a1d943d | 181 | case 'h': |
6e1eda6f | 182 | usage(); |
30b61b7b | 183 | case 'V': |
2c308875 | 184 | print_version(EXIT_SUCCESS); |
2a1d943d | 185 | default: |
677ec86c | 186 | errtryhelp(EXIT_FAILURE); |
2a1d943d KZ |
187 | } |
188 | } | |
189 | ||
6e1eda6f RM |
190 | if (optind + 1 != argc) { |
191 | warnx(_("bad usage")); | |
192 | errtryhelp(EXIT_FAILURE); | |
193 | } | |
6d562512 SK |
194 | if (ctl.nofollow && ctl.dev_devno) |
195 | errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), | |
196 | "--devno", "--nofollow"); | |
2a1d943d | 197 | |
f112f41e | 198 | ctl.path = argv[optind]; |
6d562512 SK |
199 | c = ctl.nofollow ? lstat(ctl.path, &ctl.st) : stat(ctl.path, &ctl.st); |
200 | if (c) { | |
f112f41e SK |
201 | if (!ctl.quiet) |
202 | err(EXIT_FAILURE, "%s", ctl.path); | |
b031befc | 203 | return EXIT_FAILURE; |
2a1d943d | 204 | } |
f112f41e | 205 | if (ctl.dev_devno) |
e0ecd196 KZ |
206 | return print_devno(&ctl) ? MOUNTPOINT_EXIT_NOMNT : EXIT_SUCCESS; |
207 | ||
6d562512 | 208 | if ((ctl.nofollow && S_ISLNK(ctl.st.st_mode)) || dir_to_device(&ctl)) { |
ee5de88c SK |
209 | if (!ctl.quiet) |
210 | printf(_("%s is not a mountpoint\n"), ctl.path); | |
e0ecd196 | 211 | return MOUNTPOINT_EXIT_NOMNT; |
2a1d943d | 212 | } |
ee5de88c SK |
213 | if (ctl.fs_devno) |
214 | printf("%u:%u\n", major(ctl.dev), minor(ctl.dev)); | |
215 | else if (!ctl.quiet) | |
216 | printf(_("%s is a mountpoint\n"), ctl.path); | |
e0ecd196 | 217 | |
ee5de88c | 218 | return EXIT_SUCCESS; |
2a1d943d | 219 | } |