2 * mountpoint(1) - see if a directory is a mountpoint
4 * This is libmount based reimplementation of the mountpoint(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 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.
32 #include <sys/types.h>
40 #include "closestream.h"
41 #include "pathnames.h"
43 struct mountpoint_control
{
53 static int dir_to_device(struct mountpoint_control
*ctl
)
55 struct libmnt_table
*tb
= mnt_new_table_from_file(_PATH_PROC_MOUNTINFO
);
57 struct libmnt_cache
*cache
;
62 * Fallback. Traditional way to detect mountpoints. This way
63 * is independent on /proc, but not able to detect bind mounts.
66 char buf
[PATH_MAX
], *cn
;
69 cn
= mnt_resolve_path(ctl
->path
, NULL
); /* canonicalize */
71 len
= snprintf(buf
, sizeof(buf
), "%s/..", cn
? cn
: ctl
->path
);
74 if (len
< 0 || (size_t) len
>= sizeof(buf
))
76 if (stat(buf
, &pst
) !=0)
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
;
88 /* to canonicalize all necessary paths */
89 cache
= mnt_new_cache();
90 mnt_table_set_cache(tb
, cache
);
91 mnt_unref_cache(cache
);
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
);
103 static int print_devno(const struct mountpoint_control
*ctl
)
105 if (!S_ISBLK(ctl
->st
.st_mode
)) {
107 warnx(_("%s: not a block device"), ctl
->path
);
110 printf("%u:%u\n", major(ctl
->st
.st_rdev
), minor(ctl
->st
.st_rdev
));
114 static void __attribute__((__noreturn__
)) usage(FILE *out
)
116 fputs(USAGE_HEADER
, out
);
118 _(" %1$s [-qd] /path/to/directory\n"
119 " %1$s -x /dev/device\n"), program_invocation_short_name
);
121 fputs(USAGE_SEPARATOR
, out
);
122 fputs(_("Check whether a directory or file is a mountpoint.\n"), out
);
124 fputs(USAGE_OPTIONS
, out
);
125 fputs(_(" -q, --quiet quiet mode - don't print anything\n"
126 " -d, --fs-devno print maj:min device number of the filesystem\n"
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)"));
133 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
136 int main(int argc
, char **argv
)
139 struct mountpoint_control ctl
= { 0 };
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' },
146 { "version", 0, 0, 'V' },
150 setlocale(LC_ALL
, "");
151 bindtextdomain(PACKAGE
, LOCALEDIR
);
153 atexit(close_stdout
);
157 while ((c
= getopt_long(argc
, argv
, "qdxhV", longopts
, NULL
)) != -1) {
173 printf(UTIL_LINUX_VERSION
);
181 if (optind
+ 1 != argc
)
184 ctl
.path
= argv
[optind
];
186 if (stat(ctl
.path
, &ctl
.st
)) {
188 err(EXIT_FAILURE
, "%s", ctl
.path
);
192 return print_devno(&ctl
) ? EXIT_FAILURE
: EXIT_SUCCESS
;
193 if (dir_to_device(&ctl
)) {
195 printf(_("%s is not a mountpoint\n"), ctl
.path
);
199 printf("%u:%u\n", major(ctl
.dev
), minor(ctl
.dev
));
201 printf(_("%s is a mountpoint\n"), ctl
.path
);