]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/usr.sbin/mount_portalfs/conf.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / usr.sbin / mount_portalfs / conf.c
1 /*
2  * Copyright (c) 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * All rights reserved.
5  *
6  * This code is derived from software donated to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)conf.c      8.2 (Berkeley) 3/27/94
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <errno.h>
40 #include <limits.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <regex.h>
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/syslog.h>
49
50 #include "portald.h"
51
52 #define ALLOC(ty)       (xmalloc(sizeof(ty)))
53
54 typedef struct path path;
55 struct path {
56         qelem p_q;              /* 2-way linked list */
57         int p_lno;              /* Line number of this record */
58         char *p_args;           /* copy of arg string (malloc) */
59         char *p_key;            /* Pathname to match (also p_argv[0]) */
60         regex_t p_rx;           /* RE to match against pathname () */
61         int p_rxvalid;          /* non-zero if valid regular expression */
62         int p_argc;             /* number of elements in arg string */
63         char **p_argv;          /* argv[] pointers into arg string (malloc) */
64 };
65
66 static char *conf_file;         /* XXX for regerror */
67 static path *curp;              /* XXX for regerror */
68
69 /*
70  * Add an element to a 2-way list,
71  * just after (pred)
72  */
73 static void ins_que(elem, pred)
74 qelem *elem, *pred;
75 {
76         qelem *p = pred->q_forw;
77         elem->q_back = pred;
78         elem->q_forw = p;
79         pred->q_forw = elem;
80         p->q_back = elem;
81 }
82
83 /*
84  * Remove an element from a 2-way list
85  */
86 static void rem_que(elem)
87 qelem *elem;
88 {
89         qelem *p = elem->q_forw;
90         qelem *p2 = elem->q_back;
91         p2->q_forw = p;
92         p->q_back = p2;
93 }
94
95 /*
96  * Error checking malloc
97  */
98 static void *xmalloc(siz)
99 unsigned siz;
100 {
101         void *p = malloc(siz);
102         if (p)
103                 return (p);
104         syslog(LOG_ALERT, "malloc: failed to get %d bytes", siz);
105         exit(1);
106 }
107
108 /*
109  * Insert the path in the list.
110  * If there is already an element with the same key then
111  * the *second* one is ignored (return 0).  If the key is
112  * not found then the path is added to the end of the list
113  * and 1 is returned.
114  */
115 static int pinsert(p0, q0)
116 path *p0;
117 qelem *q0;
118 {
119         qelem *q;
120
121         if (p0->p_argc == 0)
122                 return (0);
123
124         for (q = q0->q_forw; q != q0; q = q->q_forw) {
125                 path *p = (path *) q;
126                 if (strcmp(p->p_key, p0->p_key) == 0)
127                         return (0);
128         }
129         ins_que(&p0->p_q, q0->q_back);
130         return (1);
131
132 }
133
134 static path *palloc(cline, lno)
135 char *cline;
136 int lno;
137 {
138         int c;
139         char *s;
140         char *key;
141         path *p;
142         char **ap;
143
144         /*
145          * Implement comment chars
146          */
147         s = strchr(cline, '#');
148         if (s)
149                 *s = 0;
150
151         /*
152          * Do a pass through the string to count the number
153          * of arguments
154          */
155         c = 0;
156         key = strdup(cline);
157         for (s = key; s != NULL; ) {
158                 char *val;
159                 while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
160                         ;
161                 if (val)
162                         c++;
163         }
164         c++;
165         free(key);
166
167         if (c <= 1)
168                 return (0);
169
170         /*
171          * Now do another pass and generate a new path structure
172          */
173         p = ALLOC(path);
174         p->p_argc = 0;
175         p->p_argv = xmalloc(c * sizeof(char *));
176         p->p_args = strdup(cline);
177         ap = p->p_argv;
178         for (s = p->p_args; s != NULL; ) {
179                 char *val;
180                 while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
181                         ;
182                 if (val) {
183                         *ap++ = val;
184                         p->p_argc++;
185                 }
186         }
187         *ap = 0;
188
189 #ifdef DEBUG
190         for (c = 0; c < p->p_argc; c++)
191                 printf("%sv[%d] = %s\n", c?"\t":"", c, p->p_argv[c]);
192 #endif
193
194         p->p_key = p->p_argv[0];
195         if (strpbrk(p->p_key, RE_CHARS)) {
196                 int val;
197
198                 curp = p;                       /* XXX */
199                 val = regcomp(&p->p_rx, p->p_key, REG_EXTENDED | REG_NOSUB);
200                 if (val) {
201                         char errbuf[_POSIX2_LINE_MAX];
202                         regerror(val, &p->p_rx, errbuf, sizeof errbuf);
203                         syslog(LOG_ERR, "%s:%d: regcomp %s: %s",
204                                conf_file, curp->p_lno, curp->p_key, errbuf);
205                         regfree(&p->p_rx);
206                         p->p_rxvalid = 0;
207                 } else {
208                         p->p_rxvalid = 1;
209                 }
210                 curp = 0;                       /* XXX */
211         } else {
212                 p->p_rxvalid = 0;
213         }
214         p->p_lno = lno;
215
216         return (p);
217 }
218
219 /*
220  * Free a path structure
221  */
222 static void pfree(p)
223 path *p;
224 {
225         free(p->p_args);
226         if (p->p_rxvalid) {
227                 regfree(&p->p_rx);
228         }
229         free((char *) p->p_argv);
230         free((char *) p);
231 }
232
233 /*
234  * Discard all currently held path structures on q0.
235  * and add all the ones on xq.
236  */
237 static void preplace(q0, xq)
238 qelem *q0;
239 qelem *xq;
240 {
241         /*
242          * While the list is not empty,
243          * take the first element off the list
244          * and free it.
245          */
246         while (q0->q_forw != q0) {
247                 qelem *q = q0->q_forw;
248                 rem_que(q);
249                 pfree((path *) q);
250         }
251         while (xq->q_forw != xq) {
252                 qelem *q = xq->q_forw;
253                 rem_que(q);
254                 ins_que(q, q0);
255         }
256 }
257
258 /*
259  * Read the lines from the configuration file and
260  * add them to the list of paths.
261  */
262 static void readfp(q0, fp)
263 qelem *q0;
264 FILE *fp;
265 {
266         char cline[LINE_MAX];
267         int nread = 0;
268         qelem q;
269
270         /*
271          * Make a new empty list.
272          */
273         q.q_forw = q.q_back = &q;
274
275         /*
276          * Read the lines from the configuration file.
277          */
278         while (fgets(cline, sizeof(cline), fp)) {
279                 path *p = palloc(cline, nread+1);
280                 if (p && !pinsert(p, &q))
281                         pfree(p);
282                 nread++;
283         }
284
285         /*
286          * If some records were read, then throw
287          * away the old list and replace with the
288          * new one.
289          */
290         if (nread)
291                 preplace(q0, &q);
292 }
293
294 /*
295  * Read the configuration file (conf) and replace
296  * the existing path list with the new version.
297  * If the file is not readable, then no changes take place
298  */
299 void conf_read(q, conf)
300 qelem *q;
301 char *conf;
302 {
303         FILE *fp = fopen(conf, "r");
304         if (fp) {
305                 conf_file = conf;               /* XXX */
306                 readfp(q, fp);
307                 conf_file = 0;          /* XXX */
308                 (void) fclose(fp);
309         } else {
310                 syslog(LOG_ERR, "open config file \"%s\": %s", conf, strerror(errno));
311         }
312 }
313
314
315 char **conf_match(q0, key)
316 qelem *q0;
317 char *key;
318 {
319         qelem *q;
320
321         for (q = q0->q_forw; q != q0; q = q->q_forw) {
322                 path *p = (path *) q;
323                 if (p->p_rxvalid) {
324                         if (!regexec(&p->p_rx, key, 0, 0, 0)) {
325                                 return p->p_argv + 1;
326                         }
327                 } else {
328                         if (strncmp(p->p_key, key, strlen(p->p_key)) == 0)
329                                 return (p->p_argv+1);
330                 }
331         }
332
333         return (0);
334 }