2
* mountpoint(1) - see if a directory is a mountpoint
4
* This is libmount based reimplementation of the mountpoit(1)
5
* from sysvinit project.
8
* Copyright (C) 2011 Red Hat, Inc. All rights reserved.
9
* Written by Karel Zak <kzak@redhat.com>
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.
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.
21
* You should have received a copy of the GNU General Public License
22
* along with this program; if not, write to the Free Software Foundation,
23
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32
#include <sys/types.h>
43
static dev_t dir_to_device(const char *spec)
45
struct libmnt_table *tb = mnt_new_table_from_file("/proc/self/mountinfo");
47
struct libmnt_cache *cache;
52
* Fallback. Traditional way to detect mountpoints. This way
53
* is independent on /proc, but not able to detect bind mounts.
56
char buf[PATH_MAX], *cn;
59
if (stat(spec, &st) != 0)
62
cn = mnt_resolve_path(spec, NULL); /* canonicalize */
64
len = snprintf(buf, sizeof(buf), "%s/..", cn ? cn : spec);
67
if (len < 0 || (size_t) len + 1 > sizeof(buf))
69
if (stat(buf, &pst) !=0)
72
if ((st.st_dev != pst.st_dev) ||
73
(st.st_dev == pst.st_dev && st.st_ino == pst.st_ino))
79
/* to canonicalize all necessary paths */
80
cache = mnt_new_cache();
81
mnt_table_set_cache(tb, cache);
83
fs = mnt_table_find_target(tb, spec, MNT_ITER_BACKWARD);
84
if (fs && mnt_fs_get_target(fs))
85
res = mnt_fs_get_devno(fs);
88
mnt_free_cache(cache);
92
static int print_devno(const char *devname, struct stat *st)
96
if (!st && stat(devname, &stbuf) == 0)
100
if (!S_ISBLK(st->st_mode)) {
102
warnx(_("%s: not a block device"), devname);
105
printf("%u:%u\n", major(st->st_rdev), minor(st->st_rdev));
109
static void __attribute__((__noreturn__)) usage(FILE *out)
111
fputs(_("\nUsage:\n"), out);
113
_(" %1$s [-qd] /path/to/directory\n"
114
" %1$s -x /dev/device\n"), program_invocation_short_name);
116
fputs(_("\nOptions:\n"), out);
117
fputs(_(" -q, --quiet quiet mode - don't print anything\n"
118
" -d, --fs-devno print maj:min device number of the filesystem\n"
119
" -x, --devno print maj:min device number of the block device\n"
120
" -h, --help this help\n"), out);
122
fprintf(out, _("\nFor more information see mountpoint(1).\n"));
124
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
127
int main(int argc, char **argv)
129
int c, fs_devno = 0, dev_devno = 0, rc = 0;
133
static const struct option longopts[] = {
134
{ "quiet", 0, 0, 'q' },
135
{ "fs-devno", 0, 0, 'd' },
136
{ "devno", 0, 0, 'x' },
137
{ "help", 0, 0, 'h' },
141
setlocale(LC_ALL, "");
142
bindtextdomain(PACKAGE, LOCALEDIR);
147
while ((c = getopt_long(argc, argv, "qdxh", longopts, NULL)) != -1) {
168
if (optind + 1 != argc)
171
spec = argv[optind++];
173
if (stat(spec, &st)) {
175
err(EXIT_FAILURE, "%s", spec);
179
rc = print_devno(spec, &st);
183
if (!S_ISDIR(st.st_mode)) {
185
errx(EXIT_FAILURE, _("%s: not a directory"), spec);
188
src = dir_to_device(spec);
191
printf(_("%s is not a mountpoint\n"), spec);
195
printf("%u:%u\n", major(src), minor(src));
197
printf(_("%s is a mountpoint\n"), spec);
200
return rc ? EXIT_FAILURE : EXIT_SUCCESS;