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

Contents of /trunk/src/mountinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2582 - (hide annotations) (download) (as text)
Thu Apr 12 13:18:52 2007 UTC (7 years, 3 months ago) by uberlord
File MIME type: text/x-csrc
File size: 5375 byte(s)
Style updates
1 uberlord 2547 /*
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 uberlord 2580 #include <getopt.h>
19 uberlord 2547 #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 uberlord 2580
31 uberlord 2547 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__)
32     static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
33 uberlord 2577 char **mounts, bool list_nodes, bool list_fstype)
34 uberlord 2547 {
35 uberlord 2577 struct statfs *mnts;
36     int nmnts;
37     int i;
38     char **list = NULL;
39 uberlord 2547
40 uberlord 2577 if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0)
41     eerrorx ("getmntinfo: %s", strerror (errno));
42 uberlord 2547
43 uberlord 2577 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 uberlord 2547
51 uberlord 2577 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 uberlord 2547
64 uberlord 2577 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 uberlord 2547
70 uberlord 2577 return (list);
71 uberlord 2547 }
72    
73     #elif defined (__linux__)
74     static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
75 uberlord 2577 char **mounts, bool list_nodes, bool list_fstype)
76 uberlord 2547 {
77 uberlord 2577 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 uberlord 2569
85 uberlord 2577 if ((fp = fopen ("/proc/mounts", "r")) == NULL)
86     eerrorx ("getmntinfo: %s", strerror (errno));
87 uberlord 2547
88 uberlord 2577 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 uberlord 2569
95 uberlord 2577 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 uberlord 2547
104 uberlord 2577 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 uberlord 2547
117 uberlord 2577 list = rc_strlist_addsortc (list,
118     list_nodes ?
119     list_fstype ? fstype :
120     from : to);
121     }
122     fclose (fp);
123 uberlord 2547
124 uberlord 2577 return (list);
125 uberlord 2547 }
126    
127     #else
128     # error "Operating system not supported!"
129     #endif
130    
131     int main (int argc, char **argv)
132     {
133 uberlord 2577 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 uberlord 2580 char c;
146 uberlord 2547
147 uberlord 2580 static struct option longopts[] = {
148 uberlord 2582 { "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 uberlord 2580 };
156 uberlord 2547
157 uberlord 2581 while ((c = getopt_long (argc, argv, "F:N:S:fnr",
158 uberlord 2580 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 uberlord 2547
172 uberlord 2580 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 uberlord 2547
184 uberlord 2581 case 'S':
185 uberlord 2580 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 uberlord 2547
196 uberlord 2580 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 uberlord 2577 }
213 uberlord 2547
214 uberlord 2580 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 uberlord 2577 }
219     nodes = find_mounts (node_regex, fstype_regex, mounts,
220     list_nodes, list_fstype);
221 uberlord 2547
222 uberlord 2577 if (node_regex)
223     regfree (node_regex);
224     if (fstype_regex)
225     regfree (fstype_regex);
226 uberlord 2547
227 uberlord 2577 if (reverse)
228     rc_strlist_reverse (nodes);
229 uberlord 2547
230 uberlord 2577 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 uberlord 2547
239 uberlord 2577 if (skip_regex)
240     free (skip_regex);
241 uberlord 2547
242 uberlord 2577 exit (result);
243 uberlord 2547 }

  ViewVC Help
Powered by ViewVC 1.1.20