2007-04-05 16:48:42 +05:30
|
|
|
/*
|
|
|
|
mountinfo.c
|
|
|
|
Obtains information about mounted filesystems.
|
|
|
|
|
|
|
|
Copyright 2007 Gentoo Foundation
|
|
|
|
*/
|
|
|
|
|
2007-04-17 18:14:32 +05:30
|
|
|
#define APPLET "mountinfo"
|
|
|
|
|
2007-07-26 02:28:23 +05:30
|
|
|
#if defined(__DragonFly__) || defined(__FreeBSD__) || \
|
|
|
|
defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
#define BSD
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ucred.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#elif defined(__linux__)
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
2007-04-12 15:38:42 +05:30
|
|
|
#include <getopt.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <limits.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "einfo.h"
|
|
|
|
#include "rc.h"
|
|
|
|
#include "rc-misc.h"
|
|
|
|
#include "strlist.h"
|
|
|
|
|
2007-04-12 15:38:42 +05:30
|
|
|
|
2007-07-26 02:28:23 +05:30
|
|
|
#ifdef BSD
|
2007-07-11 22:57:46 +05:30
|
|
|
static char **find_mounts (regex_t *node_regex, regex_t *skip_node_regex,
|
|
|
|
regex_t *fstype_regex, regex_t *skip_fstype_regex,
|
2007-07-16 22:37:15 +05:30
|
|
|
char **mounts, bool node, bool fstype)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
struct statfs *mnts;
|
|
|
|
int nmnts;
|
|
|
|
int i;
|
|
|
|
char **list = NULL;
|
|
|
|
|
|
|
|
if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0)
|
|
|
|
eerrorx ("getmntinfo: %s", strerror (errno));
|
|
|
|
|
|
|
|
for (i = 0; i < nmnts; i++) {
|
|
|
|
if (node_regex &&
|
|
|
|
regexec (node_regex, mnts[i].f_mntfromname, 0, NULL, 0) != 0)
|
|
|
|
continue;
|
2007-07-11 22:57:46 +05:30
|
|
|
if (skip_node_regex &&
|
|
|
|
regexec (skip_node_regex, mnts[i].f_mntfromname, 0, NULL, 0) == 0)
|
|
|
|
continue;
|
2007-04-11 18:14:47 +05:30
|
|
|
if (fstype_regex &&
|
|
|
|
regexec (fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) != 0)
|
|
|
|
continue;
|
2007-07-11 22:57:46 +05:30
|
|
|
if (skip_fstype_regex &&
|
|
|
|
regexec (skip_fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) == 0)
|
|
|
|
continue;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (mounts) {
|
|
|
|
bool found = false;
|
|
|
|
int j;
|
|
|
|
char *mnt;
|
|
|
|
STRLIST_FOREACH (mounts, mnt, j)
|
|
|
|
if (strcmp (mnt, mnts[i].f_mntonname) == 0) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (! found)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-07-16 22:37:15 +05:30
|
|
|
list = rc_strlist_addsortc (list, node ?
|
2007-04-11 18:14:47 +05:30
|
|
|
mnts[i].f_mntfromname :
|
2007-07-16 22:37:15 +05:30
|
|
|
fstype ? mnts[i].f_fstypename :
|
2007-04-11 18:14:47 +05:30
|
|
|
mnts[i].f_mntonname);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (list);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined (__linux__)
|
2007-07-11 22:57:46 +05:30
|
|
|
static char **find_mounts (regex_t *node_regex, regex_t *skip_node_regex,
|
|
|
|
regex_t *fstype_regex, regex_t *skip_fstype_regex,
|
2007-07-16 22:37:15 +05:30
|
|
|
char **mounts, bool node, bool fstype)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp;
|
|
|
|
char buffer[PATH_MAX * 3];
|
|
|
|
char *p;
|
|
|
|
char *from;
|
|
|
|
char *to;
|
2007-07-16 22:37:15 +05:30
|
|
|
char *fst;
|
2007-04-11 18:14:47 +05:30
|
|
|
char **list = NULL;
|
|
|
|
|
|
|
|
if ((fp = fopen ("/proc/mounts", "r")) == NULL)
|
|
|
|
eerrorx ("getmntinfo: %s", strerror (errno));
|
|
|
|
|
|
|
|
while (fgets (buffer, sizeof (buffer), fp)) {
|
|
|
|
p = buffer;
|
|
|
|
from = strsep (&p, " ");
|
|
|
|
if (node_regex &&
|
|
|
|
regexec (node_regex, from, 0, NULL, 0) != 0)
|
|
|
|
continue;
|
2007-07-11 22:57:46 +05:30
|
|
|
if (skip_node_regex &&
|
|
|
|
regexec (skip_node_regex, from, 0, NULL, 0) == 0)
|
|
|
|
continue;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
to = strsep (&p, " ");
|
2007-07-16 22:37:15 +05:30
|
|
|
fst = strsep (&p, " ");
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Skip the really silly rootfs */
|
2007-07-16 22:37:15 +05:30
|
|
|
if (strcmp (fst, "rootfs") == 0)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
if (fstype_regex &&
|
2007-07-16 22:37:15 +05:30
|
|
|
regexec (fstype_regex, fst, 0, NULL, 0) != 0)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
2007-07-11 22:57:46 +05:30
|
|
|
if (skip_fstype_regex &&
|
2007-07-16 22:37:15 +05:30
|
|
|
regexec (skip_fstype_regex, fst, 0, NULL, 0) == 0)
|
2007-07-11 22:57:46 +05:30
|
|
|
continue;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (mounts) {
|
|
|
|
bool found = false;
|
|
|
|
int j;
|
|
|
|
char *mnt;
|
|
|
|
STRLIST_FOREACH (mounts, mnt, j)
|
|
|
|
if (strcmp (mnt, to) == 0) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (! found)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-07-16 22:37:15 +05:30
|
|
|
list = rc_strlist_addsortc (list, node ? from : fstype ? fst : to);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
return (list);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
# error "Operating system not supported!"
|
|
|
|
#endif
|
|
|
|
|
2007-07-11 22:57:46 +05:30
|
|
|
static regex_t *get_regex (char *string)
|
|
|
|
{
|
|
|
|
regex_t *reg = rc_xmalloc (sizeof (regex_t));
|
|
|
|
int result;
|
|
|
|
char buffer[256];
|
|
|
|
|
|
|
|
if ((result = regcomp (reg, string, REG_EXTENDED | REG_NOSUB)) != 0)
|
|
|
|
{
|
|
|
|
regerror (result, reg, buffer, sizeof (buffer));
|
|
|
|
eerrorx ("%s: invalid regex `%s'", APPLET, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (reg);
|
|
|
|
}
|
|
|
|
|
2007-06-28 21:14:14 +05:30
|
|
|
#include "_usage.h"
|
2007-07-11 22:57:46 +05:30
|
|
|
#define getoptstring "f:F:n:N:p:P:os" getoptstring_COMMON
|
2007-04-13 20:39:37 +05:30
|
|
|
static struct option longopts[] = {
|
2007-07-11 22:57:46 +05:30
|
|
|
{ "fstype-regex", 1, NULL, 'f'},
|
|
|
|
{ "skip-fstype-regex", 1, NULL, 'F'},
|
|
|
|
{ "node-regex", 1, NULL, 'n'},
|
|
|
|
{ "skip-node-regex", 1, NULL, 'N'},
|
|
|
|
{ "point-regex", 1, NULL, 'p'},
|
|
|
|
{ "skip-point-regex", 1, NULL, 'P'},
|
2007-07-16 22:37:15 +05:30
|
|
|
{ "node", 0, NULL, 'o'},
|
|
|
|
{ "fstype", 0, NULL, 's'},
|
2007-06-28 21:14:14 +05:30
|
|
|
longopts_COMMON
|
2007-04-13 20:39:37 +05:30
|
|
|
{ NULL, 0, NULL, 0}
|
|
|
|
};
|
2007-04-17 18:14:32 +05:30
|
|
|
#include "_usage.c"
|
2007-04-13 20:39:37 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
int i;
|
|
|
|
regex_t *fstype_regex = NULL;
|
|
|
|
regex_t *node_regex = NULL;
|
2007-07-11 22:57:46 +05:30
|
|
|
regex_t *point_regex = NULL;
|
|
|
|
regex_t *skip_fstype_regex = NULL;
|
|
|
|
regex_t *skip_node_regex = NULL;
|
|
|
|
regex_t *skip_point_regex = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
char **nodes = NULL;
|
2007-07-16 22:37:15 +05:30
|
|
|
char *n;
|
|
|
|
bool node = false;
|
|
|
|
bool fstype = false;
|
2007-04-11 18:14:47 +05:30
|
|
|
char **mounts = NULL;
|
2007-05-14 17:54:18 +05:30
|
|
|
int opt;
|
2007-07-11 22:57:46 +05:30
|
|
|
int result;
|
|
|
|
|
|
|
|
#define DO_REG(_var) \
|
|
|
|
if (_var) free (_var); \
|
|
|
|
_var = get_regex (optarg);
|
2007-04-12 15:38:42 +05:30
|
|
|
|
2007-05-14 17:54:18 +05:30
|
|
|
while ((opt = getopt_long (argc, argv, getoptstring,
|
|
|
|
longopts, (int *) 0)) != -1)
|
2007-07-11 22:57:46 +05:30
|
|
|
{
|
2007-05-14 17:54:18 +05:30
|
|
|
switch (opt) {
|
2007-07-11 22:57:46 +05:30
|
|
|
case 'f':
|
|
|
|
DO_REG (fstype_regex);
|
|
|
|
break;
|
2007-04-12 15:38:42 +05:30
|
|
|
case 'F':
|
2007-07-11 22:57:46 +05:30
|
|
|
DO_REG (skip_fstype_regex);
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
DO_REG (node_regex);
|
2007-04-12 15:38:42 +05:30
|
|
|
break;
|
|
|
|
case 'N':
|
2007-07-11 22:57:46 +05:30
|
|
|
DO_REG (skip_node_regex);
|
2007-04-12 15:38:42 +05:30
|
|
|
break;
|
2007-07-11 22:57:46 +05:30
|
|
|
case 'p':
|
|
|
|
DO_REG (point_regex);
|
2007-04-12 15:38:42 +05:30
|
|
|
break;
|
2007-07-11 22:57:46 +05:30
|
|
|
case 'P':
|
|
|
|
DO_REG (skip_point_regex);
|
2007-04-12 15:38:42 +05:30
|
|
|
break;
|
2007-07-11 22:57:46 +05:30
|
|
|
case 'o':
|
2007-07-16 22:37:15 +05:30
|
|
|
node = true;
|
|
|
|
fstype = false;
|
2007-04-12 15:38:42 +05:30
|
|
|
break;
|
2007-07-11 22:57:46 +05:30
|
|
|
case 's':
|
2007-07-16 22:37:15 +05:30
|
|
|
node = false;
|
|
|
|
fstype = true;
|
2007-04-12 15:38:42 +05:30
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-11 22:57:46 +05:30
|
|
|
case_RC_COMMON_GETOPT
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-07-11 22:57:46 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-12 15:38:42 +05:30
|
|
|
while (optind < argc) {
|
|
|
|
if (argv[optind][0] != '/')
|
|
|
|
eerrorx ("%s: `%s' is not a mount point", argv[0], argv[optind]);
|
|
|
|
mounts = rc_strlist_add (mounts, argv[optind++]);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-07-11 22:57:46 +05:30
|
|
|
|
|
|
|
nodes = find_mounts (node_regex, skip_node_regex,
|
|
|
|
fstype_regex, skip_fstype_regex,
|
2007-07-16 22:37:15 +05:30
|
|
|
mounts, node, fstype);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (node_regex)
|
|
|
|
regfree (node_regex);
|
2007-07-11 22:57:46 +05:30
|
|
|
if (skip_node_regex)
|
|
|
|
regfree (skip_node_regex);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (fstype_regex)
|
|
|
|
regfree (fstype_regex);
|
2007-07-11 22:57:46 +05:30
|
|
|
if (skip_fstype_regex)
|
|
|
|
regfree (skip_fstype_regex);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-11 22:57:46 +05:30
|
|
|
rc_strlist_reverse (nodes);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
result = EXIT_FAILURE;
|
2007-07-16 22:37:15 +05:30
|
|
|
STRLIST_FOREACH (nodes, n, i) {
|
|
|
|
if (point_regex && regexec (point_regex, n, 0, NULL, 0) != 0)
|
2007-07-11 22:57:46 +05:30
|
|
|
continue;
|
2007-07-16 22:37:15 +05:30
|
|
|
if (skip_point_regex && regexec (skip_point_regex, n, 0, NULL, 0) == 0)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
2007-07-16 22:37:15 +05:30
|
|
|
printf ("%s\n", n);
|
2007-04-11 18:14:47 +05:30
|
|
|
result = EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
rc_strlist_free (nodes);
|
|
|
|
|
2007-07-11 22:57:46 +05:30
|
|
|
if (point_regex)
|
|
|
|
regfree (point_regex);
|
|
|
|
if (skip_point_regex)
|
|
|
|
regfree (skip_point_regex);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
exit (result);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|