cam - Fix bus registration race
[dragonfly.git] / sbin / mount_portal / conf.c
blobd73def57663d849ed75f3dbce9cf1e5c94f5fa75
1 /*
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 * All rights reserved.
6 * This code is derived from software donated to Berkeley by
7 * Jan-Simon Pendry.
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 * 3. 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.
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.
33 * @(#)conf.c 8.2 (Berkeley) 3/27/94
35 * $FreeBSD: src/sbin/mount_portal/conf.c,v 1.8 1999/08/28 00:13:35 peter Exp $
38 #include <errno.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <regex.h>
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/syslog.h>
49 #include "portald.h"
51 #define ALLOC(ty) (xmalloc(sizeof(ty)))
53 typedef struct path path;
54 struct path {
55 qelem p_q; /* 2-way linked list */
56 int p_lno; /* Line number of this record */
57 char *p_args; /* copy of arg string (malloc) */
58 char *p_key; /* Pathname to match (also p_argv[0]) */
59 regex_t p_rx; /* RE to match against pathname () */
60 int p_rxvalid; /* non-zero if valid regular expression */
61 int p_argc; /* number of elements in arg string */
62 char **p_argv; /* argv[] pointers into arg string (malloc) */
65 static char *conf_file; /* XXX for regerror */
66 static path *curp; /* XXX for regerror */
69 * Add an element to a 2-way list,
70 * just after (pred)
72 static void
73 ins_que(qelem *elem, qelem *pred)
75 qelem *p = pred->q_forw;
76 elem->q_back = pred;
77 elem->q_forw = p;
78 pred->q_forw = elem;
79 p->q_back = elem;
83 * Remove an element from a 2-way list
85 static void
86 rem_que(qelem *elem)
88 qelem *p = elem->q_forw;
89 qelem *p2 = elem->q_back;
90 p2->q_forw = p;
91 p->q_back = p2;
95 * Error checking malloc
97 static void *
98 xmalloc(unsigned siz)
100 void *p = malloc(siz);
101 if (p)
102 return (p);
103 syslog(LOG_ALERT, "malloc: failed to get %d bytes", siz);
104 exit(1);
108 * Insert the path in the list.
109 * If there is already an element with the same key then
110 * the *second* one is ignored (return 0). If the key is
111 * not found then the path is added to the end of the list
112 * and 1 is returned.
114 static int
115 pinsert(path *p0, qelem *q0)
117 qelem *q;
119 if (p0->p_argc == 0)
120 return (0);
122 for (q = q0->q_forw; q != q0; q = q->q_forw) {
123 path *p = (path *) q;
124 if (strcmp(p->p_key, p0->p_key) == 0)
125 return (0);
127 ins_que(&p0->p_q, q0->q_back);
128 return (1);
132 static path *
133 palloc(char *cline, int lno)
135 int c;
136 char *s;
137 char *key;
138 path *p;
139 char **ap;
142 * Implement comment chars
144 s = strchr(cline, '#');
145 if (s)
146 *s = 0;
149 * Do a pass through the string to count the number
150 * of arguments
152 c = 0;
153 key = strdup(cline);
154 for (s = key; s != NULL; ) {
155 char *val;
156 while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
158 if (val)
159 c++;
161 c++;
162 free(key);
164 if (c <= 1)
165 return (0);
168 * Now do another pass and generate a new path structure
170 p = ALLOC(path);
171 p->p_argc = 0;
172 p->p_argv = xmalloc(c * sizeof(char *));
173 p->p_args = strdup(cline);
174 ap = p->p_argv;
175 for (s = p->p_args; s != NULL; ) {
176 char *val;
177 while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
179 if (val) {
180 *ap++ = val;
181 p->p_argc++;
184 *ap = NULL;
186 #ifdef DEBUG
187 for (c = 0; c < p->p_argc; c++)
188 printf("%sv[%d] = %s\n", c?"\t":"", c, p->p_argv[c]);
189 #endif
191 p->p_key = p->p_argv[0];
192 if (strpbrk(p->p_key, RE_CHARS)) {
193 int val;
195 curp = p; /* XXX */
196 val = regcomp(&p->p_rx, p->p_key, REG_EXTENDED | REG_NOSUB);
197 if (val) {
198 char errbuf[_POSIX2_LINE_MAX];
199 regerror(val, &p->p_rx, errbuf, sizeof errbuf);
200 syslog(LOG_ERR, "%s:%d: regcomp %s: %s",
201 conf_file, curp->p_lno, curp->p_key, errbuf);
202 regfree(&p->p_rx);
203 p->p_rxvalid = 0;
204 } else {
205 p->p_rxvalid = 1;
207 curp = NULL; /* XXX */
208 } else {
209 p->p_rxvalid = 0;
211 p->p_lno = lno;
213 return (p);
217 * Free a path structure
219 static void
220 pfree(path *p)
222 free(p->p_args);
223 if (p->p_rxvalid) {
224 regfree(&p->p_rx);
226 free((char *) p->p_argv);
227 free((char *) p);
231 * Discard all currently held path structures on q0.
232 * and add all the ones on xq.
234 static void
235 preplace(qelem *q0, qelem *xq)
238 * While the list is not empty,
239 * take the first element off the list
240 * and free it.
242 while (q0->q_forw != q0) {
243 qelem *q = q0->q_forw;
244 rem_que(q);
245 pfree((path *) q);
247 while (xq->q_forw != xq) {
248 qelem *q = xq->q_forw;
249 rem_que(q);
250 ins_que(q, q0);
255 * Read the lines from the configuration file and
256 * add them to the list of paths.
258 static void
259 readfp(qelem *q0, FILE *fp)
261 char cline[LINE_MAX];
262 int nread = 0;
263 qelem q;
266 * Make a new empty list.
268 q.q_forw = q.q_back = &q;
271 * Read the lines from the configuration file.
273 while (fgets(cline, sizeof(cline), fp)) {
274 path *p = palloc(cline, nread+1);
275 if (p && !pinsert(p, &q))
276 pfree(p);
277 nread++;
281 * If some records were read, then throw
282 * away the old list and replace with the
283 * new one.
285 if (nread)
286 preplace(q0, &q);
290 * Read the configuration file (conf) and replace
291 * the existing path list with the new version.
292 * If the file is not readable, then no changes take place
294 void
295 conf_read(qelem *q, char *conf)
297 FILE *fp = fopen(conf, "r");
298 if (fp) {
299 conf_file = conf; /* XXX */
300 readfp(q, fp);
301 conf_file = NULL; /* XXX */
302 fclose(fp);
303 } else {
304 syslog(LOG_ERR, "open config file \"%s\": %s", conf, strerror(errno));
309 char **
310 conf_match(qelem *q0, char *key)
312 qelem *q;
314 for (q = q0->q_forw; q != q0; q = q->q_forw) {
315 path *p = (path *) q;
316 if (p->p_rxvalid) {
317 if (!regexec(&p->p_rx, key, 0, 0, 0)) {
318 return p->p_argv + 1;
320 } else {
321 if (strncmp(p->p_key, key, strlen(p->p_key)) == 0)
322 return (p->p_argv+1);
326 return (0);