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

Contents of /trunk/src/mountinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20