]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/mountpoint.c
misc: never use usage(stderr)
[thirdparty/util-linux.git] / sys-utils / mountpoint.c
1 /*
2 * mountpoint(1) - see if a directory is a mountpoint
3 *
4 * This is libmount based reimplementation of the mountpoint(1)
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 *
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.
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>
34
35 #include <libmount.h>
36
37 #include "nls.h"
38 #include "xalloc.h"
39 #include "c.h"
40 #include "closestream.h"
41 #include "pathnames.h"
42
43 struct mountpoint_control {
44 char *path;
45 dev_t dev;
46 struct stat st;
47 unsigned int
48 dev_devno:1,
49 fs_devno:1,
50 quiet:1;
51 };
52
53 static int dir_to_device(struct mountpoint_control *ctl)
54 {
55 struct libmnt_table *tb = mnt_new_table_from_file(_PATH_PROC_MOUNTINFO);
56 struct libmnt_fs *fs;
57 struct libmnt_cache *cache;
58 int rc = -1;
59
60 if (!tb) {
61 /*
62 * Fallback. Traditional way to detect mountpoints. This way
63 * is independent on /proc, but not able to detect bind mounts.
64 */
65 struct stat pst;
66 char buf[PATH_MAX], *cn;
67 int len;
68
69 cn = mnt_resolve_path(ctl->path, NULL); /* canonicalize */
70
71 len = snprintf(buf, sizeof(buf), "%s/..", cn ? cn : ctl->path);
72 free(cn);
73
74 if (len < 0 || (size_t) len >= sizeof(buf))
75 return -1;
76 if (stat(buf, &pst) !=0)
77 return -1;
78
79 if ((ctl->st.st_dev != pst.st_dev) ||
80 (ctl->st.st_dev == pst.st_dev && ctl->st.st_ino == pst.st_ino)) {
81 ctl->dev = ctl->st.st_dev;
82 return 0;
83 }
84
85 return -1;
86 }
87
88 /* to canonicalize all necessary paths */
89 cache = mnt_new_cache();
90 mnt_table_set_cache(tb, cache);
91 mnt_unref_cache(cache);
92
93 fs = mnt_table_find_target(tb, ctl->path, MNT_ITER_BACKWARD);
94 if (fs && mnt_fs_get_target(fs)) {
95 ctl->dev = mnt_fs_get_devno(fs);
96 rc = 0;
97 }
98
99 mnt_unref_table(tb);
100 return rc;
101 }
102
103 static int print_devno(const struct mountpoint_control *ctl)
104 {
105 if (!S_ISBLK(ctl->st.st_mode)) {
106 if (!ctl->quiet)
107 warnx(_("%s: not a block device"), ctl->path);
108 return -1;
109 }
110 printf("%u:%u\n", major(ctl->st.st_rdev), minor(ctl->st.st_rdev));
111 return 0;
112 }
113
114 static void __attribute__((__noreturn__)) usage(void)
115 {
116 FILE *out = stdout;
117 fputs(USAGE_HEADER, out);
118 fprintf(out,
119 _(" %1$s [-qd] /path/to/directory\n"
120 " %1$s -x /dev/device\n"), program_invocation_short_name);
121
122 fputs(USAGE_SEPARATOR, out);
123 fputs(_("Check whether a directory or file is a mountpoint.\n"), out);
124
125 fputs(USAGE_OPTIONS, out);
126 fputs(_(" -q, --quiet quiet mode - don't print anything\n"
127 " -d, --fs-devno print maj:min device number of the filesystem\n"
128 " -x, --devno print maj:min device number of the block device\n"), out);
129 fputs(USAGE_SEPARATOR, out);
130 fputs(USAGE_HELP, out);
131 fputs(USAGE_VERSION, out);
132 fprintf(out, USAGE_MAN_TAIL("mountpoint(1)"));
133
134 exit(EXIT_SUCCESS);
135 }
136
137 int main(int argc, char **argv)
138 {
139 int c;
140 struct mountpoint_control ctl = { NULL };
141
142 static const struct option longopts[] = {
143 { "quiet", no_argument, NULL, 'q' },
144 { "fs-devno", no_argument, NULL, 'd' },
145 { "devno", no_argument, NULL, 'x' },
146 { "help", no_argument, NULL, 'h' },
147 { "version", no_argument, NULL, 'V' },
148 { NULL, 0, NULL, 0 }
149 };
150
151 setlocale(LC_ALL, "");
152 bindtextdomain(PACKAGE, LOCALEDIR);
153 textdomain(PACKAGE);
154 atexit(close_stdout);
155
156 mnt_init_debug(0);
157
158 while ((c = getopt_long(argc, argv, "qdxhV", longopts, NULL)) != -1) {
159
160 switch(c) {
161 case 'q':
162 ctl.quiet = 1;
163 break;
164 case 'd':
165 ctl.fs_devno = 1;
166 break;
167 case 'x':
168 ctl.dev_devno = 1;
169 break;
170 case 'h':
171 usage();
172 break;
173 case 'V':
174 printf(UTIL_LINUX_VERSION);
175 return EXIT_SUCCESS;
176 default:
177 errtryhelp(EXIT_FAILURE);
178 }
179 }
180
181 if (optind + 1 != argc) {
182 warnx(_("bad usage"));
183 errtryhelp(EXIT_FAILURE);
184 }
185
186 ctl.path = argv[optind];
187
188 if (stat(ctl.path, &ctl.st)) {
189 if (!ctl.quiet)
190 err(EXIT_FAILURE, "%s", ctl.path);
191 return EXIT_FAILURE;
192 }
193 if (ctl.dev_devno)
194 return print_devno(&ctl) ? EXIT_FAILURE : EXIT_SUCCESS;
195 if (dir_to_device(&ctl)) {
196 if (!ctl.quiet)
197 printf(_("%s is not a mountpoint\n"), ctl.path);
198 return EXIT_FAILURE;
199 }
200 if (ctl.fs_devno)
201 printf("%u:%u\n", major(ctl.dev), minor(ctl.dev));
202 else if (!ctl.quiet)
203 printf(_("%s is a mountpoint\n"), ctl.path);
204 return EXIT_SUCCESS;
205 }