/[baselayout]/trunk/src/mountinfo.c
Gentoo

Contents of /trunk/src/mountinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20