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

Contents of /trunk/src/mountinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20