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

Contents of /trunk/src/mountinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20