openrc/src/mountinfo.c

272 lines
5.8 KiB
C
Raw Normal View History

/*
mountinfo.c
Obtains information about mounted filesystems.
Copyright 2007 Gentoo Foundation
*/
2007-04-17 18:14:32 +05:30
#define APPLET "mountinfo"
2007-07-31 22:10:32 +05:30
#include <sys/types.h>
2007-07-26 02:28:23 +05:30
#if defined(__DragonFly__) || defined(__FreeBSD__) || \
defined(__NetBSD__) || defined(__OpenBSD__)
#define BSD
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#endif
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2007-07-31 22:10:32 +05:30
#include <regex.h>
#include "builtins.h"
#include "einfo.h"
#include "rc.h"
#include "rc-misc.h"
#include "strlist.h"
2007-07-26 02:28:23 +05:30
#ifdef BSD
static char **find_mounts (regex_t *node_regex, regex_t *skip_node_regex,
regex_t *fstype_regex, regex_t *skip_fstype_regex,
char **mounts, bool node, bool fstype)
{
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;
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;
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;
}
list = rc_strlist_addsortc (list, node ?
2007-04-11 18:14:47 +05:30
mnts[i].f_mntfromname :
fstype ? mnts[i].f_fstypename :
2007-04-11 18:14:47 +05:30
mnts[i].f_mntonname);
}
return (list);
}
#elif defined (__linux__)
static char **find_mounts (regex_t *node_regex, regex_t *skip_node_regex,
regex_t *fstype_regex, regex_t *skip_fstype_regex,
char **mounts, bool node, bool fstype)
{
2007-04-11 18:14:47 +05:30
FILE *fp;
char buffer[PATH_MAX * 3];
char *p;
char *from;
char *to;
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;
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, " ");
fst = strsep (&p, " ");
2007-04-11 18:14:47 +05:30
/* Skip the really silly rootfs */
if (strcmp (fst, "rootfs") == 0)
2007-04-11 18:14:47 +05:30
continue;
if (fstype_regex &&
regexec (fstype_regex, fst, 0, NULL, 0) != 0)
2007-04-11 18:14:47 +05:30
continue;
if (skip_fstype_regex &&
regexec (skip_fstype_regex, fst, 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, to) == 0) {
found = true;
break;
}
if (! found)
continue;
}
list = rc_strlist_addsortc (list, node ? from : fstype ? fst : to);
2007-04-11 18:14:47 +05:30
}
fclose (fp);
return (list);
}
#else
# error "Operating system not supported!"
#endif
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"
#define getoptstring "f:F:n:N:p:P:os" getoptstring_COMMON
2007-04-13 20:39:37 +05:30
static struct option longopts[] = {
{ "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'},
{ "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
int mountinfo (int argc, char **argv)
{
2007-04-11 18:14:47 +05:30
int i;
regex_t *fstype_regex = NULL;
regex_t *node_regex = NULL;
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;
char *n;
bool node = false;
bool fstype = false;
2007-04-11 18:14:47 +05:30
char **mounts = NULL;
int opt;
int result;
#define DO_REG(_var) \
if (_var) free (_var); \
_var = get_regex (optarg);
while ((opt = getopt_long (argc, argv, getoptstring,
longopts, (int *) 0)) != -1)
{
switch (opt) {
case 'f':
DO_REG (fstype_regex);
break;
case 'F':
DO_REG (skip_fstype_regex);
break;
case 'n':
DO_REG (node_regex);
break;
case 'N':
DO_REG (skip_node_regex);
break;
case 'p':
DO_REG (point_regex);
break;
case 'P':
DO_REG (skip_point_regex);
break;
case 'o':
node = true;
fstype = false;
break;
case 's':
node = false;
fstype = true;
break;
2007-04-11 18:14:47 +05:30
case_RC_COMMON_GETOPT
2007-04-11 18:14:47 +05:30
}
}
2007-04-11 18:14:47 +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
}
nodes = find_mounts (node_regex, skip_node_regex,
fstype_regex, skip_fstype_regex,
mounts, node, fstype);
2007-04-11 18:14:47 +05:30
if (node_regex)
regfree (node_regex);
if (skip_node_regex)
regfree (skip_node_regex);
2007-04-11 18:14:47 +05:30
if (fstype_regex)
regfree (fstype_regex);
if (skip_fstype_regex)
regfree (skip_fstype_regex);
2007-04-11 18:14:47 +05:30
rc_strlist_reverse (nodes);
2007-04-11 18:14:47 +05:30
result = EXIT_FAILURE;
STRLIST_FOREACH (nodes, n, i) {
if (point_regex && regexec (point_regex, n, 0, NULL, 0) != 0)
continue;
if (skip_point_regex && regexec (skip_point_regex, n, 0, NULL, 0) == 0)
2007-04-11 18:14:47 +05:30
continue;
printf ("%s\n", n);
2007-04-11 18:14:47 +05:30
result = EXIT_SUCCESS;
}
rc_strlist_free (nodes);
if (point_regex)
regfree (point_regex);
if (skip_point_regex)
regfree (skip_point_regex);
2007-04-11 18:14:47 +05:30
exit (result);
}