| 1 |
/*
|
| 2 |
mountinfo.c
|
| 3 |
Obtains information about mounted filesystems.
|
| 4 |
|
| 5 |
Copyright 2007 Gentoo Foundation
|
| 6 |
*/
|
| 7 |
|
| 8 |
#include <sys/types.h>
|
| 9 |
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
| 10 |
#include <sys/param.h>
|
| 11 |
#include <sys/ucred.h>
|
| 12 |
#include <sys/mount.h>
|
| 13 |
#elif defined(__linux__)
|
| 14 |
#include <limits.h>
|
| 15 |
#endif
|
| 16 |
|
| 17 |
#include <errno.h>
|
| 18 |
#include <getopt.h>
|
| 19 |
#include <limits.h>
|
| 20 |
#include <regex.h>
|
| 21 |
#include <stdio.h>
|
| 22 |
#include <stdlib.h>
|
| 23 |
#include <string.h>
|
| 24 |
|
| 25 |
#include "einfo.h"
|
| 26 |
#include "rc.h"
|
| 27 |
#include "rc-misc.h"
|
| 28 |
#include "strlist.h"
|
| 29 |
|
| 30 |
|
| 31 |
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__)
|
| 32 |
static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
|
| 33 |
char **mounts, bool list_nodes, bool list_fstype)
|
| 34 |
{
|
| 35 |
struct statfs *mnts;
|
| 36 |
int nmnts;
|
| 37 |
int i;
|
| 38 |
char **list = NULL;
|
| 39 |
|
| 40 |
if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0)
|
| 41 |
eerrorx ("getmntinfo: %s", strerror (errno));
|
| 42 |
|
| 43 |
for (i = 0; i < nmnts; i++) {
|
| 44 |
if (node_regex &&
|
| 45 |
regexec (node_regex, mnts[i].f_mntfromname, 0, NULL, 0) != 0)
|
| 46 |
continue;
|
| 47 |
if (fstype_regex &&
|
| 48 |
regexec (fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) != 0)
|
| 49 |
continue;
|
| 50 |
|
| 51 |
if (mounts) {
|
| 52 |
bool found = false;
|
| 53 |
int j;
|
| 54 |
char *mnt;
|
| 55 |
STRLIST_FOREACH (mounts, mnt, j)
|
| 56 |
if (strcmp (mnt, mnts[i].f_mntonname) == 0) {
|
| 57 |
found = true;
|
| 58 |
break;
|
| 59 |
}
|
| 60 |
if (! found)
|
| 61 |
continue;
|
| 62 |
}
|
| 63 |
|
| 64 |
list = rc_strlist_addsortc (list, list_nodes ?
|
| 65 |
mnts[i].f_mntfromname :
|
| 66 |
list_fstype ? mnts[i].f_fstypename :
|
| 67 |
mnts[i].f_mntonname);
|
| 68 |
}
|
| 69 |
|
| 70 |
return (list);
|
| 71 |
}
|
| 72 |
|
| 73 |
#elif defined (__linux__)
|
| 74 |
static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
|
| 75 |
char **mounts, bool list_nodes, bool list_fstype)
|
| 76 |
{
|
| 77 |
FILE *fp;
|
| 78 |
char buffer[PATH_MAX * 3];
|
| 79 |
char *p;
|
| 80 |
char *from;
|
| 81 |
char *to;
|
| 82 |
char *fstype;
|
| 83 |
char **list = NULL;
|
| 84 |
|
| 85 |
if ((fp = fopen ("/proc/mounts", "r")) == NULL)
|
| 86 |
eerrorx ("getmntinfo: %s", strerror (errno));
|
| 87 |
|
| 88 |
while (fgets (buffer, sizeof (buffer), fp)) {
|
| 89 |
p = buffer;
|
| 90 |
from = strsep (&p, " ");
|
| 91 |
if (node_regex &&
|
| 92 |
regexec (node_regex, from, 0, NULL, 0) != 0)
|
| 93 |
continue;
|
| 94 |
|
| 95 |
to = strsep (&p, " ");
|
| 96 |
fstype = strsep (&p, " ");
|
| 97 |
/* Skip the really silly rootfs */
|
| 98 |
if (strcmp (fstype, "rootfs") == 0)
|
| 99 |
continue;
|
| 100 |
if (fstype_regex &&
|
| 101 |
regexec (fstype_regex, fstype, 0, NULL, 0) != 0)
|
| 102 |
continue;
|
| 103 |
|
| 104 |
if (mounts) {
|
| 105 |
bool found = false;
|
| 106 |
int j;
|
| 107 |
char *mnt;
|
| 108 |
STRLIST_FOREACH (mounts, mnt, j)
|
| 109 |
if (strcmp (mnt, to) == 0) {
|
| 110 |
found = true;
|
| 111 |
break;
|
| 112 |
}
|
| 113 |
if (! found)
|
| 114 |
continue;
|
| 115 |
}
|
| 116 |
|
| 117 |
list = rc_strlist_addsortc (list,
|
| 118 |
list_nodes ?
|
| 119 |
list_fstype ? fstype :
|
| 120 |
from : to);
|
| 121 |
}
|
| 122 |
fclose (fp);
|
| 123 |
|
| 124 |
return (list);
|
| 125 |
}
|
| 126 |
|
| 127 |
#else
|
| 128 |
# error "Operating system not supported!"
|
| 129 |
#endif
|
| 130 |
|
| 131 |
int main (int argc, char **argv)
|
| 132 |
{
|
| 133 |
int i;
|
| 134 |
regex_t *fstype_regex = NULL;
|
| 135 |
regex_t *node_regex = NULL;
|
| 136 |
regex_t *skip_regex = NULL;
|
| 137 |
char **nodes = NULL;
|
| 138 |
char *node;
|
| 139 |
int result;
|
| 140 |
char buffer[256];
|
| 141 |
bool list_nodes = false;
|
| 142 |
bool list_fstype = false;
|
| 143 |
bool reverse = false;
|
| 144 |
char **mounts = NULL;
|
| 145 |
char c;
|
| 146 |
|
| 147 |
static struct option longopts[] = {
|
| 148 |
{ "fstype-regex", 1, NULL, 'F'},
|
| 149 |
{ "node-regex", 1, NULL, 'N'},
|
| 150 |
{ "skip-regex", 1, NULL, 'S'},
|
| 151 |
{ "fstype", 0, NULL, 'f'},
|
| 152 |
{ "node", 0, NULL, 'n'},
|
| 153 |
{ "reverse", 0, NULL, 'r'},
|
| 154 |
{ NULL, 0, NULL, 0}
|
| 155 |
};
|
| 156 |
|
| 157 |
while ((c = getopt_long (argc, argv, "F:N:S:fnr",
|
| 158 |
longopts, (int *) 0)) != -1)
|
| 159 |
switch (c) {
|
| 160 |
case 'F':
|
| 161 |
if (fstype_regex)
|
| 162 |
free (fstype_regex);
|
| 163 |
fstype_regex = rc_xmalloc (sizeof (regex_t));
|
| 164 |
if ((result = regcomp (fstype_regex, optarg,
|
| 165 |
REG_EXTENDED | REG_NOSUB)) != 0)
|
| 166 |
{
|
| 167 |
regerror (result, fstype_regex, buffer, sizeof (buffer));
|
| 168 |
eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
|
| 169 |
}
|
| 170 |
break;
|
| 171 |
|
| 172 |
case 'N':
|
| 173 |
if (node_regex)
|
| 174 |
free (node_regex);
|
| 175 |
node_regex = rc_xmalloc (sizeof (regex_t));
|
| 176 |
if ((result = regcomp (node_regex, optarg,
|
| 177 |
REG_EXTENDED | REG_NOSUB)) != 0)
|
| 178 |
{
|
| 179 |
regerror (result, node_regex, buffer, sizeof (buffer));
|
| 180 |
eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
|
| 181 |
}
|
| 182 |
break;
|
| 183 |
|
| 184 |
case 'S':
|
| 185 |
if (skip_regex)
|
| 186 |
free (skip_regex);
|
| 187 |
skip_regex = rc_xmalloc (sizeof (regex_t));
|
| 188 |
if ((result = regcomp (skip_regex, optarg,
|
| 189 |
REG_EXTENDED | REG_NOSUB)) != 0)
|
| 190 |
{
|
| 191 |
regerror (result, skip_regex, buffer, sizeof (buffer));
|
| 192 |
eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
|
| 193 |
}
|
| 194 |
break;
|
| 195 |
|
| 196 |
case 'f':
|
| 197 |
list_fstype = true;
|
| 198 |
list_nodes = false;
|
| 199 |
break;
|
| 200 |
|
| 201 |
case 'n':
|
| 202 |
list_nodes = true;
|
| 203 |
list_fstype = false;
|
| 204 |
break;
|
| 205 |
|
| 206 |
case 'r':
|
| 207 |
reverse = true;
|
| 208 |
break;
|
| 209 |
|
| 210 |
default:
|
| 211 |
exit (EXIT_FAILURE);
|
| 212 |
}
|
| 213 |
|
| 214 |
while (optind < argc) {
|
| 215 |
if (argv[optind][0] != '/')
|
| 216 |
eerrorx ("%s: `%s' is not a mount point", argv[0], argv[optind]);
|
| 217 |
mounts = rc_strlist_add (mounts, argv[optind++]);
|
| 218 |
}
|
| 219 |
nodes = find_mounts (node_regex, fstype_regex, mounts,
|
| 220 |
list_nodes, list_fstype);
|
| 221 |
|
| 222 |
if (node_regex)
|
| 223 |
regfree (node_regex);
|
| 224 |
if (fstype_regex)
|
| 225 |
regfree (fstype_regex);
|
| 226 |
|
| 227 |
if (reverse)
|
| 228 |
rc_strlist_reverse (nodes);
|
| 229 |
|
| 230 |
result = EXIT_FAILURE;
|
| 231 |
STRLIST_FOREACH (nodes, node, i) {
|
| 232 |
if (skip_regex && regexec (skip_regex, node, 0, NULL, 0) == 0)
|
| 233 |
continue;
|
| 234 |
printf ("%s\n", node);
|
| 235 |
result = EXIT_SUCCESS;
|
| 236 |
}
|
| 237 |
rc_strlist_free (nodes);
|
| 238 |
|
| 239 |
if (skip_regex)
|
| 240 |
free (skip_regex);
|
| 241 |
|
| 242 |
exit (result);
|
| 243 |
}
|
| 244 |
|