]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/mountpoint.c
Use --help suggestion on invalid option
[thirdparty/util-linux.git] / sys-utils / mountpoint.c
CommitLineData
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
f112f41e
SK
43struct 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};
2a1d943d 52
f112f41e 53static int dir_to_device(struct mountpoint_control *ctl)
2a1d943d 54{
39edf681 55 struct libmnt_table *tb = mnt_new_table_from_file(_PATH_PROC_MOUNTINFO);
2a1d943d 56 struct libmnt_fs *fs;
eb9d3706 57 struct libmnt_cache *cache;
eac83fbc 58 int rc = -1;
2a1d943d 59
dfd2c714
KZ
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 */
f112f41e 65 struct stat pst;
eb9d3706 66 char buf[PATH_MAX], *cn;
dfd2c714
KZ
67 int len;
68
f112f41e 69 cn = mnt_resolve_path(ctl->path, NULL); /* canonicalize */
eb9d3706 70
f112f41e 71 len = snprintf(buf, sizeof(buf), "%s/..", cn ? cn : ctl->path);
eb9d3706
KZ
72 free(cn);
73
06fa5817 74 if (len < 0 || (size_t) len >= sizeof(buf))
04f087ec 75 return -1;
dfd2c714 76 if (stat(buf, &pst) !=0)
04f087ec 77 return -1;
dfd2c714 78
f112f41e
SK
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;
04f087ec
ZL
82 return 0;
83 }
dfd2c714 84
04f087ec 85 return -1;
dfd2c714 86 }
2a1d943d 87
eb9d3706
KZ
88 /* to canonicalize all necessary paths */
89 cache = mnt_new_cache();
90 mnt_table_set_cache(tb, cache);
6195f9e6 91 mnt_unref_cache(cache);
eb9d3706 92
f112f41e 93 fs = mnt_table_find_target(tb, ctl->path, MNT_ITER_BACKWARD);
eac83fbc 94 if (fs && mnt_fs_get_target(fs)) {
f112f41e 95 ctl->dev = mnt_fs_get_devno(fs);
eac83fbc
DR
96 rc = 0;
97 }
2a1d943d 98
50fccba1 99 mnt_unref_table(tb);
eac83fbc 100 return rc;
2a1d943d
KZ
101}
102
f112f41e 103static int print_devno(const struct mountpoint_control *ctl)
2a1d943d 104{
f112f41e
SK
105 if (!S_ISBLK(ctl->st.st_mode)) {
106 if (!ctl->quiet)
107 warnx(_("%s: not a block device"), ctl->path);
2a1d943d
KZ
108 return -1;
109 }
f112f41e 110 printf("%u:%u\n", major(ctl->st.st_rdev), minor(ctl->st.st_rdev));
2a1d943d
KZ
111 return 0;
112}
113
114static void __attribute__((__noreturn__)) usage(FILE *out)
115{
30b61b7b 116 fputs(USAGE_HEADER, out);
4f92adb6
KZ
117 fprintf(out,
118 _(" %1$s [-qd] /path/to/directory\n"
119 " %1$s -x /dev/device\n"), program_invocation_short_name);
120
451dbcfa
BS
121 fputs(USAGE_SEPARATOR, out);
122 fputs(_("Check whether a directory or file is a mountpoint.\n"), out);
123
30b61b7b 124 fputs(USAGE_OPTIONS, out);
4f92adb6
KZ
125 fputs(_(" -q, --quiet quiet mode - don't print anything\n"
126 " -d, --fs-devno print maj:min device number of the filesystem\n"
30b61b7b
SK
127 " -x, --devno print maj:min device number of the block device\n"), out);
128 fputs(USAGE_SEPARATOR, out);
129 fputs(USAGE_HELP, out);
130 fputs(USAGE_VERSION, out);
131 fprintf(out, USAGE_MAN_TAIL("mountpoint(1)"));
2a1d943d
KZ
132
133 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
134}
135
136int main(int argc, char **argv)
137{
ee5de88c 138 int c;
f112f41e 139 struct mountpoint_control ctl = { 0 };
2a1d943d
KZ
140
141 static const struct option longopts[] = {
142 { "quiet", 0, 0, 'q' },
143 { "fs-devno", 0, 0, 'd' },
144 { "devno", 0, 0, 'x' },
145 { "help", 0, 0, 'h' },
30b61b7b 146 { "version", 0, 0, 'V' },
2a1d943d
KZ
147 { NULL, 0, 0, 0 }
148 };
149
150 setlocale(LC_ALL, "");
151 bindtextdomain(PACKAGE, LOCALEDIR);
152 textdomain(PACKAGE);
efb8854f 153 atexit(close_stdout);
2a1d943d
KZ
154
155 mnt_init_debug(0);
156
30b61b7b 157 while ((c = getopt_long(argc, argv, "qdxhV", longopts, NULL)) != -1) {
2a1d943d
KZ
158
159 switch(c) {
160 case 'q':
f112f41e 161 ctl.quiet = 1;
2a1d943d
KZ
162 break;
163 case 'd':
f112f41e 164 ctl.fs_devno = 1;
2a1d943d
KZ
165 break;
166 case 'x':
f112f41e 167 ctl.dev_devno = 1;
2a1d943d
KZ
168 break;
169 case 'h':
170 usage(stdout);
171 break;
30b61b7b
SK
172 case 'V':
173 printf(UTIL_LINUX_VERSION);
174 return EXIT_SUCCESS;
2a1d943d 175 default:
677ec86c 176 errtryhelp(EXIT_FAILURE);
2a1d943d
KZ
177 }
178 }
179
180 if (optind + 1 != argc)
181 usage(stderr);
182
f112f41e 183 ctl.path = argv[optind];
2a1d943d 184
f112f41e
SK
185 if (stat(ctl.path, &ctl.st)) {
186 if (!ctl.quiet)
187 err(EXIT_FAILURE, "%s", ctl.path);
b031befc 188 return EXIT_FAILURE;
2a1d943d 189 }
f112f41e 190 if (ctl.dev_devno)
ee5de88c
SK
191 return print_devno(&ctl) ? EXIT_FAILURE : EXIT_SUCCESS;
192 if (dir_to_device(&ctl)) {
193 if (!ctl.quiet)
194 printf(_("%s is not a mountpoint\n"), ctl.path);
195 return EXIT_FAILURE;
2a1d943d 196 }
ee5de88c
SK
197 if (ctl.fs_devno)
198 printf("%u:%u\n", major(ctl.dev), minor(ctl.dev));
199 else if (!ctl.quiet)
200 printf(_("%s is a mountpoint\n"), ctl.path);
201 return EXIT_SUCCESS;
2a1d943d 202}