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

Contents of /trunk/src/mountinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2547 - (hide annotations) (download) (as text)
Thu Apr 5 11:18:42 2007 UTC (7 years ago) by uberlord
File MIME type: text/x-csrc
File size: 5312 byte(s)
    Rewrite the core parts in C. We now provide librc so other programs can
    query runlevels, services and state without using bash. We also provide
    libeinfo so other programs can easily use our informational functions.

    As such, we have dropped the requirement of using bash as the init script
    shell. We now use /bin/sh and have strived to make the scripts as portable
    as possible. Shells that work are bash and dash. busybox works provided
    you disable s-s-d. If you have WIPE_TMP set to yes in conf.d/bootmisc you
    should disable find too.
    zsh and ksh do not work at this time.

    Networking support is currently being re-vamped also as it was heavily bash
    array based. As such, a new config format is available like so
    config_eth0="1.2.3.4/24 5.6.7.8/16"
    or like so
    config_eth0="'1.2.3.4 netmask 255.255.255.0' '5.6.7.8 netmask 255.255.0.0'"

    We will still support the old bash array format provided that /bin/sh IS
    a link it bash.

    ChangeLog for baselayout-1 can be found in our SVN repo.
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     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     {
43     if (node_regex &&
44     regexec (node_regex, mnts[i].f_mntfromname, 0, NULL, 0) != 0)
45     continue;
46     if (fstype_regex &&
47     regexec (fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) != 0)
48     continue;
49    
50     if (mounts)
51     {
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    
65     list = rc_strlist_addsortc (list, list_nodes ?
66     mnts[i].f_mntfromname :
67     list_fstype ? mnts[i].f_fstypename :
68     mnts[i].f_mntonname);
69     }
70    
71     return (list);
72     }
73    
74     #elif defined (__linux__)
75     static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
76     char **mounts, bool list_nodes, bool list_fstype)
77     {
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    
86     if ((fp = fopen ("/proc/mounts", "r")) == NULL)
87     eerrorx ("getmntinfo: %s", strerror (errno));
88    
89     while (fgets (buffer, sizeof (buffer), fp))
90     {
91     p = buffer;
92     from = strsep (&p, " ");
93     if (node_regex &&
94     regexec (node_regex, from, 0, NULL, 0) != 0)
95     continue;
96    
97     to = strsep (&p, " ");
98     fstype = strsep (&p, " ");
99     /* Skip the really silly rootfs */
100     if (strcmp (fstype, "rootfs") == 0)
101     continue;
102     if (fstype_regex &&
103     regexec (fstype_regex, fstype, 0, NULL, 0) != 0)
104     continue;
105    
106     if (mounts)
107     {
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    
121     list = rc_strlist_addsortc (list,
122     list_nodes ?
123     list_fstype ? fstype :
124     from : to);
125     }
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     {
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    
167     if (strcmp (argv[i], "--node-regex") == 0 && (i + 1 < argc))
168     {
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    
182     if (strcmp (argv[i], "--skip-regex") == 0 && (i + 1 < argc))
183     {
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    
197     if (strcmp (argv[i], "--fstype") == 0)
198     {
199     list_fstype = true;
200     continue;
201     }
202    
203     if (strcmp (argv[i], "--node") == 0)
204     {
205     list_nodes = true;
206     continue;
207     }
208     if (strcmp (argv[i], "--reverse") == 0)
209     {
210     reverse = true;
211     continue;
212     }
213    
214     if (argv[i][0] != '/')
215     eerrorx ("%s: `%s' is not a mount point", argv[0], argv[i]);
216    
217     mounts = rc_strlist_add (mounts, argv[i]);
218     }
219    
220     nodes = find_mounts (node_regex, fstype_regex, mounts,
221     list_nodes, list_fstype);
222    
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     continue;
236     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