diff options
author | Karel Zak | 2011-06-21 11:46:36 +0200 |
---|---|---|
committer | Karel Zak | 2011-06-21 11:46:36 +0200 |
commit | 0164c24595bd4209ab7b668fec34f5cdcdd1d5eb (patch) | |
tree | 1ec58f53eea5c35dd9890fca81b9a537fb2e9c58 /sys-utils | |
parent | docs: update TODO file (diff) | |
download | kernel-qcow2-util-linux-0164c24595bd4209ab7b668fec34f5cdcdd1d5eb.tar.gz kernel-qcow2-util-linux-0164c24595bd4209ab7b668fec34f5cdcdd1d5eb.tar.xz kernel-qcow2-util-linux-0164c24595bd4209ab7b668fec34f5cdcdd1d5eb.zip |
mountpoint: add new command
This is libmount based re-implementation of the mountpoint(1) command.
The original implementation is maintained in sysvinit suite.
The mountpoint(1) in util-linux is not enabled by default (for now) --
use --enable-mountpoint to enable the util.
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'sys-utils')
-rw-r--r-- | sys-utils/.gitignore | 1 | ||||
-rw-r--r-- | sys-utils/Makefile.am | 7 | ||||
-rw-r--r-- | sys-utils/mountpoint.1 | 43 | ||||
-rw-r--r-- | sys-utils/mountpoint.c | 174 |
4 files changed, 225 insertions, 0 deletions
diff --git a/sys-utils/.gitignore b/sys-utils/.gitignore index b8af98f1a..3ae7e4a7b 100644 --- a/sys-utils/.gitignore +++ b/sys-utils/.gitignore @@ -18,6 +18,7 @@ lscpu mips32.8 mips64.8 mips.8 +mountpoint parisc32.8 parisc64.8 parisc.8 diff --git a/sys-utils/Makefile.am b/sys-utils/Makefile.am index d916d3d04..53cdc3718 100644 --- a/sys-utils/Makefile.am +++ b/sys-utils/Makefile.am @@ -31,6 +31,13 @@ fstrim_SOURCES = fstrim.c $(top_srcdir)/lib/strutils.c rtcwake_SOURCES = rtcwake.c $(top_srcdir)/lib/strutils.c dmesg_SOURCES = dmesg.c $(top_srcdir)/lib/strutils.c +if BUILD_MOUNTPOINT +bin_PROGRAMS += mountpoint +mountpoint_LDADD = $(ul_libmount_la) +mountpoint_CFLAGS = $(AM_CFLAGS) -I$(ul_libmount_incdir) +dist_man_MANS += mountpoint.1 +endif + if BUILD_FALLOCATE usrbin_exec_PROGRAMS += fallocate fallocate_SOURCES = fallocate.c $(top_srcdir)/lib/strutils.c diff --git a/sys-utils/mountpoint.1 b/sys-utils/mountpoint.1 new file mode 100644 index 000000000..be17c42dd --- /dev/null +++ b/sys-utils/mountpoint.1 @@ -0,0 +1,43 @@ +.\" -*- nroff -*- +.TH MOUNTPOINT 1 "June 2011" +.SH NAME +mountpoint \- see if a directory is a mountpoint +.SH SYNOPSIS +.B mountpoint +.RB [ \-q ] +.RB [ \-d ] +.I directory + +.B mountpoint +.RB \-x +.I device + +.SH DESCRIPTION +.B mountpoint +checks if the directory is mentioned in the /proc/self/mountinfo file. +.SH OPTIONS +.IP "\fB\-h, \-\-help\fP" +Print help and exit. +.IP "\fB\-q, \-\-quiet\fP" +Be quiet - don't print anything. +.IP "\fB\-d, \-\-fs\-devno\fP" +Print major/minor device number of the filesystem on stdout. +.IP "\fB\-x, \-\-devno\fP" +Print major/minor device number of the blockdevice on stdout. +.SH EXIT STATUS +Zero if the directory is a mountpoint, non-zero if not. +.SH AUTHOR +.PP +Karel Zak <kzak@redhat.com> +.SH NOTES +.PP +The util-linux +.B mountpoint +implementation was written from scratch for libmount. The original version +for sysvinit suite was written by Miquel van Smoorenburg. +.SH SEE ALSO +.BR mount (8) +.SH AVAILABILITY +The mountpoint command is part of the util-linux package and is available from +ftp://ftp.kernel.org/pub/linux/utils/util-linux/. + diff --git a/sys-utils/mountpoint.c b/sys-utils/mountpoint.c new file mode 100644 index 000000000..92d283054 --- /dev/null +++ b/sys-utils/mountpoint.c @@ -0,0 +1,174 @@ +/* + * mountpoint(1) - see if a directory is a mountpoint + * + * This is libmount based reimplementation of the mountpoit(1) + * from sysvinit project. + * + * + * Copyright (C) 2011 Red Hat, Inc. All rights reserved. + * Written by Karel Zak <kzak@redhat.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it would be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <getopt.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> + + +#include <libmount.h> + +#include "nls.h" +#include "xalloc.h" +#include "c.h" + +static int quiet; + +static char *dir_to_device(const char *spec) +{ + struct libmnt_table *tb = mnt_new_table_from_file("/proc/self/mountinfo"); + struct libmnt_fs *fs; + char *res = NULL; + + if (!tb) + return NULL; + + fs = mnt_table_find_target(tb, spec, MNT_ITER_BACKWARD); + if (fs && mnt_fs_get_target(fs)) + res = xstrdup(mnt_fs_get_source(fs)); + + mnt_free_table(tb); + return res; +} + +static int print_devno(const char *devname, struct stat *st) +{ + struct stat stbuf; + + if (!st && stat(devname, &stbuf) == 0) + st = &stbuf; + if (!st) + return -1; + if (!S_ISBLK(st->st_mode)) { + if (!quiet) + warnx(_("%s: not a block device"), devname); + return -1; + } + printf("%u:%u\n", major(st->st_rdev), minor(st->st_rdev)); + return 0; +} + +static void __attribute__((__noreturn__)) usage(FILE *out) +{ + fprintf(out, _("Usage:\n" + " %1$s [-qd] /path/to/directory\n" + " %1$s -x /dev/device\n"), + program_invocation_short_name); + + fprintf(out, _( + "\nOptions:\n" + " -q, --quiet quiet mode - don't print anything\n" + " -d, --fs-devno print maj:min device number of the filesystem\n" + " -x, --devno print maj:min device number of the block device\n" + " -h, --help this help\n" + )); + + fprintf(out, _("\nFor more information see mountpoint(1).\n")); + + exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); +} + +int main(int argc, char **argv) +{ + int c, fs_devno = 0, dev_devno = 0, rc = 0; + char *spec; + struct stat st; + + static const struct option longopts[] = { + { "quiet", 0, 0, 'q' }, + { "fs-devno", 0, 0, 'd' }, + { "devno", 0, 0, 'x' }, + { "help", 0, 0, 'h' }, + { NULL, 0, 0, 0 } + }; + + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); + + mnt_init_debug(0); + + while ((c = getopt_long(argc, argv, "qdxh", longopts, NULL)) != -1) { + + switch(c) { + case 'q': + quiet = 1; + break; + case 'd': + fs_devno = 1; + break; + case 'x': + dev_devno = 1; + break; + case 'h': + usage(stdout); + break; + default: + usage(stderr); + break; + } + } + + if (optind + 1 != argc) + usage(stderr); + + spec = argv[optind++]; + + if (stat(spec, &st)) { + if (!quiet) + err(EXIT_FAILURE, "%s", spec); + return EXIT_FAILURE; + } + if (dev_devno) + rc = print_devno(spec, &st); + else { + char *src; + + if (!S_ISDIR(st.st_mode)) { + if (!quiet) + errx(EXIT_FAILURE, _("%s: not a directory"), spec); + return EXIT_FAILURE; + } + src = dir_to_device(spec); + if (!src) { + if (!quiet) + printf(_("%s is not a mountpoint\n"), spec); + return EXIT_FAILURE; + } + if (fs_devno) + rc = print_devno(src, NULL); + else if (!quiet) + printf(_("%s is a mountpoint\n"), spec); + free(src); + } + + return rc ? EXIT_FAILURE : EXIT_SUCCESS; +} |