Bug Summary

File:src/usr.sbin/config/sem.c
Warning:line 875, column 14
Assigned value is garbage or undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name sem.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.sbin/config/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/usr.sbin/config -I . -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.sbin/config/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/usr.sbin/config/sem.c
1/* $OpenBSD: sem.c,v 1.38 2021/11/28 19:26:03 deraadt Exp $ */
2/* $NetBSD: sem.c,v 1.10 1996/11/11 23:40:11 gwr Exp $ */
3
4/*
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This software was developed by the Computer Systems Engineering group
9 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 * contributed to Berkeley.
11 *
12 * All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by the University of
15 * California, Lawrence Berkeley Laboratories.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * from: @(#)sem.c 8.1 (Berkeley) 6/6/93
42 */
43
44#include <sys/types.h>
45#include <ctype.h>
46#include <err.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50
51#include "config.h"
52#include "sem.h"
53
54/*
55 * config semantics.
56 */
57
58#define NAMESIZE100 100 /* local name buffers */
59
60const char *s_generic;
61const char *s_nfs;
62
63static struct hashtab *attrtab; /* for attribute lookup */
64static struct hashtab *cfhashtab; /* for config lookup */
65static struct hashtab *devitab; /* etc */
66
67static struct attr errattr;
68static struct devbase errdev;
69static struct deva errdeva;
70static struct devbase **nextbase;
71static struct deva **nextdeva;
72static struct config **nextcf;
73static struct devi **nextdevi;
74static struct devi **nextpseudo;
75
76static int has_errobj(struct nvlist *, void *);
77static struct nvlist *addtoattr(struct nvlist *, struct devbase *);
78static int exclude(struct nvlist *, const char *, const char *);
79static int resolve(struct nvlist **, const char *, const char *,
80 struct nvlist *, int);
81static int lresolve(struct nvlist **, const char *, const char *,
82 struct nvlist *, int);
83static struct devi *newdevi(const char *, int, struct devbase *d);
84static struct devi *getdevi(const char *);
85static const char *concat(const char *, int);
86static char *extend(char *, const char *);
87static int split(const char *, size_t, char *, size_t, int *);
88static void selectbase(struct devbase *, struct deva *);
89static int onlist(struct nvlist *, void *);
90static const char **fixloc(const char *, struct attr *, struct nvlist *);
91
92void
93initsem(void)
94{
95
96 attrtab = ht_new();
97 errattr.a_name = "<internal>";
98
99 allbases = NULL((void *)0);
100 nextbase = &allbases;
101
102 alldevas = NULL((void *)0);
103 nextdeva = &alldevas;
104
105 cfhashtab = ht_new();
106 allcf = NULL((void *)0);
107 nextcf = &allcf;
108
109 devitab = ht_new();
110 alldevi = NULL((void *)0);
111 nextdevi = &alldevi;
112 errdev.d_name = "<internal>";
113
114 allpseudo = NULL((void *)0);
115 nextpseudo = &allpseudo;
116
117 s_generic = intern("generic");
118 s_nfs = intern("nfs");
119}
120
121/* Name of include file just ended (set in scan.l) */
122extern const char *lastfile;
123
124void
125enddefs(void)
126{
127 struct devbase *dev;
128
129 for (dev = allbases; dev != NULL((void *)0); dev = dev->d_next) {
130 if (!dev->d_isdef) {
131 (void)fprintf(stderr(&__sF[2]),
132 "%s: device `%s' used but not defined\n",
133 lastfile, dev->d_name);
134 errors++;
135 continue;
136 }
137 }
138 if (errors) {
139 (void)fprintf(stderr(&__sF[2]), "*** Stop.\n");
140 exit(1);
141 }
142}
143
144void
145setdefmaxusers(int min, int def, int max)
146{
147
148 if (min < 1 || min > def || def > max)
149 error("maxusers must have 1 <= min <= default <= max");
150 else {
151 minmaxusers = min;
152 defmaxusers = def;
153 maxmaxusers = max;
154 }
155}
156
157void
158setmaxusers(int n)
159{
160
161 if (maxusers != 0) {
162 warnx("warning: duplicate maxusers parameter, will use latest definition (%d)", n);
163 }
164 maxusers = n;
165 if (n < minmaxusers) {
166 warnx("warning: minimum of %d maxusers assumed", minmaxusers);
167 maxusers = minmaxusers;
168 } else if (n > maxmaxusers) {
169 warnx("warning: maxusers (%d) > %d", n, maxmaxusers);
170 }
171}
172
173/*
174 * Define an attribute, optionally with an interface (a locator list).
175 * Since an empty locator list is logically different from "no interface",
176 * all locator lists include a dummy head node, which we discard here.
177 */
178int
179defattr(const char *name, struct nvlist *locs)
180{
181 struct attr *a;
182 struct nvlist *nv;
183 int len;
184
185 a = emalloc(sizeof *a);
186 if (ht_insert(attrtab, name, a)ht_insrep(attrtab, name, a, 0)) {
187 free(a);
188 error("attribute `%s' already defined", name);
189 nvfreel(locs);
190 return (1);
191 }
192 a->a_name = name;
193 if (locs != NULL((void *)0)) {
194 a->a_iattr = 1;
195 a->a_locs = locs->nv_next;
196 nvfree(locs);
197 } else {
198 a->a_iattr = 0;
199 a->a_locs = NULL((void *)0);
200 }
201 len = 0;
202 for (nv = a->a_locs; nv != NULL((void *)0); nv = nv->nv_next)
203 len++;
204 a->a_loclen = len;
205 a->a_devs = NULL((void *)0);
206 a->a_refs = NULL((void *)0);
207 return (0);
208}
209
210/*
211 * Return true if the given `error object' is embedded in the given
212 * pointer list.
213 */
214static int
215has_errobj(struct nvlist *nv, void *obj)
216{
217
218 for (; nv != NULL((void *)0); nv = nv->nv_next)
219 if (nv->nv_ptrnv_un.un_ptr == obj)
220 return (1);
221 return (0);
222}
223
224/*
225 * Add a device base to a list in an attribute (actually, to any list).
226 * Note that this does not check for duplicates, and does reverse the
227 * list order, but no one cares anyway.
228 */
229static struct nvlist *
230addtoattr(struct nvlist *l, struct devbase *dev)
231{
232 struct nvlist *n;
233
234 n = newnv(NULL((void *)0), NULL((void *)0), dev, 0, l);
235 return (n);
236}
237
238/*
239 * Define a device. This may (or may not) also define an interface
240 * attribute and/or refer to existing attributes.
241 */
242void
243defdev(struct devbase *dev, int ispseudo, struct nvlist *loclist,
244 struct nvlist *attrs)
245{
246 struct nvlist *nv;
247 struct attr *a;
248
249 if (dev == &errdev)
250 goto bad;
251 if (dev->d_isdef) {
252 error("redefinition of `%s'", dev->d_name);
253 goto bad;
254 }
255 dev->d_isdef = 1;
256 if (has_errobj(attrs, &errattr))
257 goto bad;
258
259 /*
260 * Handle implicit attribute definition from locator list. Do
261 * this before scanning the `at' list so that we can have, e.g.:
262 * device foo at other, foo { slot = -1 }
263 * (where you can plug in a foo-bus extender to a foo-bus).
264 */
265 if (loclist != NULL((void *)0)) {
266 nv = loclist;
267 loclist = NULL((void *)0); /* defattr disposes of them for us */
268 if (defattr(dev->d_name, nv))
269 goto bad;
270 attrs = newnv(dev->d_name, NULL((void *)0), getattr(dev->d_name), 0,
271 attrs);
272 }
273
274 /* Committed! Set up fields. */
275 dev->d_ispseudo = ispseudo;
276 dev->d_attrs = attrs;
277
278 /*
279 * For each interface attribute this device refers to, add this
280 * device to its reference list. This makes, e.g., finding all
281 * "scsi"s easier.
282 */
283 for (nv = attrs; nv != NULL((void *)0); nv = nv->nv_next) {
284 a = nv->nv_ptrnv_un.un_ptr;
285 if (a->a_iattr)
286 a->a_refs = addtoattr(a->a_refs, dev);
287 }
288 return;
289bad:
290 nvfreel(loclist);
291 nvfreel(attrs);
292}
293
294/*
295 * Look up a devbase. Also makes sure it is a reasonable name,
296 * i.e., does not end in a digit or contain special characters.
297 */
298struct devbase *
299getdevbase(char *name)
300{
301 u_char *p;
302 struct devbase *dev;
303
304 p = (u_char *)name;
305 if (!isalpha(*p))
306 goto badname;
307 while (*++p) {
308 if (!isalnum(*p) && *p != '_')
309 goto badname;
310 }
311 if (isdigit(*--p)) {
312badname:
313 error("bad device base name `%s'", name);
314 return (&errdev);
315 }
316 dev = ht_lookup(devbasetab, name);
317 if (dev == NULL((void *)0)) {
318 dev = emalloc(sizeof *dev);
319 dev->d_name = name;
320 dev->d_next = NULL((void *)0);
321 dev->d_isdef = 0;
322 dev->d_major = nodev;
323 dev->d_attrs = NULL((void *)0);
324 dev->d_ihead = NULL((void *)0);
325 dev->d_ipp = &dev->d_ihead;
326 dev->d_ahead = NULL((void *)0);
327 dev->d_app = &dev->d_ahead;
328 dev->d_umax = 0;
329 *nextbase = dev;
330 nextbase = &dev->d_next;
331 if (ht_insert(devbasetab, name, dev)ht_insrep(devbasetab, name, dev, 0))
332 panic("getdevbase(%s)", name);
333 }
334 return (dev);
335}
336
337/*
338 * Define some of a device's allowable parent attachments.
339 * There may be a list of (plain) attributes.
340 */
341void
342defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist,
343 struct nvlist *attrs)
344{
345 struct nvlist *nv;
346 struct attr *a;
347 struct deva *da;
348
349 if (dev == &errdev)
350 goto bad;
351 if (deva == NULL((void *)0))
352 deva = getdevattach(dev->d_name);
353 if (deva == &errdeva)
354 goto bad;
355 if (!dev->d_isdef) {
356 error("attaching undefined device `%s'", dev->d_name);
357 goto bad;
358 }
359 if (deva->d_isdef) {
360 error("redefinition of `%s'", deva->d_name);
361 goto bad;
362 }
363 if (dev->d_ispseudo) {
364 error("pseudo-devices can't attach");
365 goto bad;
366 }
367
368 deva->d_isdef = 1;
369 if (has_errobj(attrs, &errattr))
370 goto bad;
371 for (nv = attrs; nv != NULL((void *)0); nv = nv->nv_next) {
372 a = nv->nv_ptrnv_un.un_ptr;
373 if (a == &errattr)
374 continue; /* already complained */
375 if (a->a_iattr)
376 error("`%s' is not a plain attribute", a->a_name);
377 }
378
379 /* Committed! Set up fields. */
380 deva->d_attrs = attrs;
381 deva->d_atlist = atlist;
382 deva->d_devbase = dev;
383
384 /*
385 * Turn the `at' list into interface attributes (map each
386 * nv_name to an attribute, or to NULL for root), and add
387 * this device to those attributes, so that children can
388 * be listed at this particular device if they are supported
389 * by that attribute.
390 */
391 for (nv = atlist; nv != NULL((void *)0); nv = nv->nv_next) {
392 if (nv->nv_name == NULL((void *)0))
393 nv->nv_ptrnv_un.un_ptr = a = NULL((void *)0); /* at root */
394 else
395 nv->nv_ptrnv_un.un_ptr = a = getattr(nv->nv_name);
396 if (a == &errattr)
397 continue; /* already complained */
398
399 /*
400 * Make sure that an attachment spec doesn't
401 * already say how to attach to this attribute.
402 */
403 for (da = dev->d_ahead; da != NULL((void *)0); da = da->d_bsame)
404 if (onlist(da->d_atlist, a))
405 error("attach at `%s' already done by `%s'",
406 a ? a->a_name : "root", da->d_name);
407
408 if (a == NULL((void *)0))
409 continue; /* at root; don't add */
410 if (!a->a_iattr)
411 error("%s cannot be at plain attribute `%s'",
412 dev->d_name, a->a_name);
413 else
414 a->a_devs = addtoattr(a->a_devs, dev);
415 }
416
417 /* attach to parent */
418 *dev->d_app = deva;
419 dev->d_app = &deva->d_bsame;
420 return;
421bad:
422 nvfreel(atlist);
423 nvfreel(attrs);
424}
425
426/*
427 * Look up a device attachment. Also makes sure it is a reasonable
428 * name, i.e., does not contain digits or special characters.
429 */
430struct deva *
431getdevattach(const char *name)
432{
433 u_char *p;
434 struct deva *deva;
435
436 p = (u_char *)name;
437 if (!isalpha(*p))
438 goto badname;
439 while (*++p) {
440 if (!isalnum(*p) && *p != '_')
441 goto badname;
442 }
443 if (isdigit((unsigned char)*--p)) {
444badname:
445 error("bad device attachment name `%s'", name);
446 return (&errdeva);
447 }
448 deva = ht_lookup(devatab, name);
449 if (deva == NULL((void *)0)) {
450 deva = emalloc(sizeof *deva);
451 deva->d_name = name;
452 deva->d_next = NULL((void *)0);
453 deva->d_bsame = NULL((void *)0);
454 deva->d_isdef = 0;
455 deva->d_devbase = NULL((void *)0);
456 deva->d_atlist = NULL((void *)0);
457 deva->d_attrs = NULL((void *)0);
458 deva->d_ihead = NULL((void *)0);
459 deva->d_ipp = &deva->d_ihead;
460 *nextdeva = deva;
461 nextdeva = &deva->d_next;
462 if (ht_insert(devatab, name, deva)ht_insrep(devatab, name, deva, 0))
463 panic("getdeva(%s)", name);
464 }
465 return (deva);
466}
467
468/*
469 * Look up an attribute.
470 */
471struct attr *
472getattr(const char *name)
473{
474 struct attr *a;
475
476 if ((a = ht_lookup(attrtab, name)) == NULL((void *)0)) {
477 error("undefined attribute `%s'", name);
478 a = &errattr;
479 }
480 return (a);
481}
482
483/*
484 * Set the major device number for a device, so that it can be used
485 * as a root/swap/dumps "on" device in a configuration.
486 */
487void
488setmajor(struct devbase *d, int n)
489{
490
491 if (d != &errdev && d->d_major != nodev)
492 error("device `%s' is already major %d",
493 d->d_name, d->d_major);
494 else
495 d->d_major = n;
496}
497
498static int
499exclude(struct nvlist *nv, const char *name, const char *what)
500{
501
502 if (nv != NULL((void *)0)) {
503 error("%s: swap generic must not specify %s", name, what);
504 return (1);
505 }
506 return (0);
507}
508
509/*
510 * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a').
511 * Handle the case where the device number is given but there is no
512 * corresponding name, and map NULL to the default.
513 */
514static int
515resolve(struct nvlist **nvp, const char *name, const char *what,
516 struct nvlist *dflt, int part)
517{
518 struct nvlist *nv;
519 struct devbase *dev;
520 const char *cp;
521 int maj, min, l;
522 int unit;
523 char buf[NAMESIZE100];
524
525 part -= 'a';
526 if ((part >= maxpartitions) || (part < 0))
527 panic("resolve");
528 if ((nv = *nvp) == NULL((void *)0)) {
529 dev_t d = nodev;
530 /*
531 * Apply default. Easiest to do this by number.
532 * Make sure to retain NODEVness, if this is dflt's disposition.
533 */
534 if (dflt->nv_int != nodev) {
535 maj = major(dflt->nv_int)(((unsigned)(dflt->nv_int) >> 8) & 0xff);
536 min = (minor(dflt->nv_int)((unsigned)((dflt->nv_int) & 0xff) | (((dflt->nv_int
) & 0xffff0000) >> 8))
/ maxpartitions) + part;
537 d = makedev(maj, min)((dev_t)((((maj) & 0xff) << 8) | ((min) & 0xff)
| (((min) & 0xffff00) << 8)))
;
538 }
539 *nvp = nv = newnv(NULL((void *)0), NULL((void *)0), NULL((void *)0), d, NULL((void *)0));
540 }
541 if (nv->nv_int != nodev) {
542 /*
543 * By the numbers. Find the appropriate major number
544 * to make a name.
545 */
546 maj = major(nv->nv_int)(((unsigned)(nv->nv_int) >> 8) & 0xff);
547 min = minor(nv->nv_int)((unsigned)((nv->nv_int) & 0xff) | (((nv->nv_int) &
0xffff0000) >> 8))
;
548 for (dev = allbases; dev != NULL((void *)0); dev = dev->d_next)
549 if (dev->d_major == maj)
550 break;
551 if (dev == NULL((void *)0))
552 (void)snprintf(buf, sizeof buf, "<%u/%u>",
553 maj, min);
554 else
555 (void)snprintf(buf, sizeof buf, "%s%u%c",
556 dev->d_name, min / maxpartitions,
557 (min % maxpartitions) + 'a');
558 nv->nv_strnv_un.un_str = intern(buf);
559 return (0);
560 }
561
562 if (nv->nv_strnv_un.un_str == NULL((void *)0) || nv->nv_strnv_un.un_str == s_nfs)
563 /*
564 * NFS spec. Leave as NODEV.
565 */
566 return (0);
567
568 /*
569 * The normal case: things like "ra2b". Check for partition
570 * suffix, remove it if there, and split into name ("ra") and
571 * unit (2).
572 */
573 l = strlen(nv->nv_strnv_un.un_str);
574 cp = &nv->nv_strnv_un.un_str[l];
575 if (l > 1 && *--cp >= 'a' && *cp <= 'a'+maxpartitions &&
576 isdigit((unsigned char)cp[-1])) {
577 l--;
578 part = *cp - 'a';
579 }
580 cp = nv->nv_strnv_un.un_str;
581 if (split(cp, l, buf, sizeof buf, &unit)) {
582 error("%s: invalid %s device name `%s'", name, what, cp);
583 return (1);
584 }
585 dev = ht_lookup(devbasetab, intern(buf));
586 if (dev == NULL((void *)0) || dev->d_major == nodev) {
587 error("%s: can't make %s device from `%s'",
588 name, what, nv->nv_strnv_un.un_str);
589 return (1);
590 }
591 nv->nv_name = dev->d_name;
592 nv->nv_int = makedev(dev->d_major, unit * maxpartitions + part)((dev_t)((((dev->d_major) & 0xff) << 8) | ((unit
* maxpartitions + part) & 0xff) | (((unit * maxpartitions
+ part) & 0xffff00) << 8)))
;
593 return (0);
594}
595
596static int
597lresolve(struct nvlist **nvp, const char *name, const char *what,
598 struct nvlist *dflt, int part)
599{
600 int err;
601
602 while ((err = resolve(nvp, name, what, dflt, part)) == 0 &&
603 (*nvp)->nv_next != NULL((void *)0))
604 nvp = &(*nvp)->nv_next;
605 return (err);
606}
607
608/*
609 * Add a completed configuration to the list.
610 */
611void
612addconf(struct config *cf0)
613{
614 struct config *cf;
615 struct nvlist *nv;
616 const char *name;
617
618 name = cf0->cf_name;
619 cf = emalloc(sizeof *cf);
620 if (ht_insert(cfhashtab, name, cf)ht_insrep(cfhashtab, name, cf, 0)) {
621 error("configuration `%s' already defined", name);
622 free(cf);
623 goto bad;
624 }
625 *cf = *cf0;
626
627 /*
628 * Look for "swap generic".
629 */
630 for (nv = cf->cf_swap; nv != NULL((void *)0); nv = nv->nv_next)
631 if (nv->nv_strnv_un.un_str == s_generic)
632 break;
633 if (nv != NULL((void *)0)) {
634 /*
635 * Make sure no root or dump device specified, and no
636 * other swap devices. Note single | here (check all).
637 */
638 nv = cf->cf_swap;
639 if (exclude(cf->cf_root, name, "root device") |
640 exclude(nv->nv_next, name, "additional swap devices") |
641 exclude(cf->cf_dump, name, "dump device"))
642 goto bad;
643 } else {
644 nv = cf->cf_root;
645 if (nv == NULL((void *)0)) {
646 error("%s: no root device specified", name);
647 goto bad;
648 }
649 if (resolve(&cf->cf_root, name, "root", nv, 'a') |
650 lresolve(&cf->cf_swap, name, "swap", nv, 'b') |
651 resolve(&cf->cf_dump, name, "dumps", nv, 'b'))
652 goto bad;
653 }
654 *nextcf = cf;
655 nextcf = &cf->cf_next;
656 return;
657bad:
658 nvfreel(cf0->cf_root);
659 nvfreel(cf0->cf_swap);
660 nvfreel(cf0->cf_dump);
661}
662
663void
664setconf(struct nvlist **npp, const char *what, struct nvlist *v)
665{
666
667 if (*npp != NULL((void *)0)) {
668 error("duplicate %s specification", what);
669 nvfreel(v);
670 } else
671 *npp = v;
672}
673
674static struct devi *
675newdevi(const char *name, int unit, struct devbase *d)
676{
677 struct devi *i;
678
679 i = emalloc(sizeof *i);
680 i->i_name = name;
681 i->i_unit = unit;
682 i->i_base = d;
683 i->i_next = NULL((void *)0);
684 i->i_bsame = NULL((void *)0);
685 i->i_asame = NULL((void *)0);
686 i->i_alias = NULL((void *)0);
687 i->i_at = NULL((void *)0);
688 i->i_atattr = NULL((void *)0);
689 i->i_atdev = NULL((void *)0);
690 i->i_atdeva = NULL((void *)0);
691 i->i_locs = NULL((void *)0);
692 i->i_cfflags = 0;
693 i->i_cfindex = -1;
694 i->i_lineno = currentline();
695 if (unit >= d->d_umax)
696 d->d_umax = unit + 1;
697 return (i);
698}
699
700/*
701 * Enable an already declared but disabled device.
702 */
703void
704enabledev(const char *name, const char *at)
705{
706 struct devbase *ib, *ab;
707 char atbuf[NAMESIZE100];
708 struct attr *attr;
709 struct nvlist *nv;
710 struct devi *i;
711 const char *cp;
712 int atunit;
713
714 i = ht_lookup(devitab, name);
715 if (i == NULL((void *)0)) {
716 error("invalid device `%s'", name);
717 return;
718 }
719 ib = i->i_base;
720
721 if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) {
722 error("invalid attachment name `%s'", at);
723 return;
724 }
725 cp = intern(atbuf);
726 ab = ht_lookup(devbasetab, cp);
727 if (ab == NULL((void *)0)) {
728 error("invalid attachment device `%s'", cp);
729 return;
730 }
731 for (nv = ab->d_attrs; nv != NULL((void *)0); nv = nv->nv_next) {
732 attr = nv->nv_ptrnv_un.un_ptr;
733 if (onlist(attr->a_devs, ib))
734 goto foundattachment;
735 }
736 error("%s's cannot attach to %s's", ib->d_name, atbuf);
737 return;
738
739foundattachment:
740 while (i && i->i_atdev != ab)
741 i = i->i_alias;
742 if (i == NULL((void *)0)) {
743 error("%s at %s not found", name, at);
744 return;
745 } else
746 i->i_disable = 0; /* Enable */
747}
748
749/*
750 * Add the named device as attaching to the named attribute (or perhaps
751 * another device instead) plus unit number.
752 */
753void
754adddev(const char *name, const char *at, struct nvlist *loclist, int flags,
755 int disable)
756{
757 struct devi *i; /* the new instance */
758 struct attr *attr; /* attribute that allows attach */
759 struct devbase *ib; /* i->i_base */
760 struct devbase *ab; /* not NULL => at another dev */
761 struct nvlist *nv;
762 struct deva *iba; /* devbase attachment used */
763 const char *cp;
764 int atunit;
1
'atunit' declared without an initial value
765 char atbuf[NAMESIZE100];
766 int hit;
767
768 ab = NULL((void *)0);
769 iba = NULL((void *)0);
770 if (at == NULL((void *)0)) {
2
Assuming 'at' is equal to NULL
3
Taking true branch
771 /* "at root" */
772 if ((i = getdevi(name)) == NULL((void *)0))
4
Assuming the condition is false
5
Taking false branch
773 goto bad;
774 /*
775 * Must warn about i_unit > 0 later, after taking care of
776 * the STAR cases (we could do non-star's here but why
777 * bother?). Make sure this device can be at root.
778 */
779 ib = i->i_base;
780 hit = 0;
781 for (iba = ib->d_ahead; iba != NULL((void *)0); iba = iba->d_bsame)
6
Assuming 'iba' is not equal to NULL
7
Loop condition is true. Entering loop body
782 if (onlist(iba->d_atlist, NULL((void *)0))) {
8
Assuming the condition is true
9
Taking true branch
783 hit = 1;
784 break;
10
Execution continues on line 786
785 }
786 if (!hit
10.1
'hit' is 1
) {
11
Taking false branch
787 error("%s's cannot attach to the root", ib->d_name);
788 goto bad;
789 }
790 attr = &errattr; /* a convenient "empty" attr */
791 } else {
792 if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) {
793 error("invalid attachment name `%s'", at);
794 /* (void)getdevi(name); -- ??? */
795 goto bad;
796 }
797 if ((i = getdevi(name)) == NULL((void *)0))
798 goto bad;
799 ib = i->i_base;
800 cp = intern(atbuf);
801
802 /*
803 * Devices can attach to two types of things: Attributes,
804 * and other devices (which have the appropriate attributes
805 * to allow attachment).
806 *
807 * (1) If we're attached to an attribute, then we don't need
808 * look at the parent base device to see what attributes
809 * it has, and make sure that we can attach to them.
810 *
811 * (2) If we're attached to a real device (i.e. named in
812 * the config file), we want to remember that so that
813 * at cross-check time, if the device we're attached to
814 * is missing but other devices which also provide the
815 * attribute are present, we don't get a false "OK."
816 *
817 * (3) If the thing we're attached to is an attribute
818 * but is actually named in the config file, we still
819 * have to remember its devbase.
820 */
821
822 /* Figure out parent's devbase, to satisfy case (3). */
823 ab = ht_lookup(devbasetab, cp);
824
825 /* Find out if it's an attribute. */
826 attr = ht_lookup(attrtab, cp);
827
828 /* Make sure we're _really_ attached to the attr. Case (1). */
829 if (attr != NULL((void *)0) && onlist(attr->a_devs, ib))
830 goto findattachment;
831
832 /*
833 * Else a real device, and not just an attribute. Case (2).
834 *
835 * Have to work a bit harder to see whether we have
836 * something like "tg0 at esp0" (where esp is merely
837 * not an attribute) or "tg0 at nonesuch0" (where
838 * nonesuch is not even a device).
839 */
840 if (ab == NULL((void *)0)) {
841 error("%s at %s: `%s' unknown",
842 name, at, atbuf);
843 goto bad;
844 }
845
846 /*
847 * See if the named parent carries an attribute
848 * that allows it to supervise device ib.
849 */
850 for (nv = ab->d_attrs; nv != NULL((void *)0); nv = nv->nv_next) {
851 attr = nv->nv_ptrnv_un.un_ptr;
852 if (onlist(attr->a_devs, ib))
853 goto findattachment;
854 }
855 error("%s's cannot attach to %s's", ib->d_name, atbuf);
856 goto bad;
857
858findattachment:
859 /* find out which attachment it uses */
860 hit = 0;
861 for (iba = ib->d_ahead; iba != NULL((void *)0); iba = iba->d_bsame)
862 if (onlist(iba->d_atlist, attr)) {
863 hit = 1;
864 break;
865 }
866 if (!hit)
867 panic("adddev: can't figure out attachment");
868 }
869 if ((i->i_locs = fixloc(name, attr, loclist)) == NULL((void *)0))
12
Assuming the condition is false
13
Taking false branch
870 goto bad;
871 i->i_at = at;
872 i->i_atattr = attr;
873 i->i_atdev = ab;
874 i->i_atdeva = iba;
875 i->i_atunit = atunit;
14
Assigned value is garbage or undefined
876 i->i_cfflags = flags;
877 i->i_disable = disable;
878
879 *iba->d_ipp = i;
880 iba->d_ipp = &i->i_asame;
881
882 selectbase(ib, iba);
883 /* all done, fall into ... */
884bad:
885 nvfreel(loclist);
886 return;
887}
888
889void
890addpseudo(const char *name, int number, int disable)
891{
892 struct devbase *d;
893 struct devi *i;
894
895 d = ht_lookup(devbasetab, name);
896 if (d == NULL((void *)0)) {
897 error("undefined pseudo-device %s", name);
898 return;
899 }
900 if (!d->d_ispseudo) {
901 error("%s is a real device, not a pseudo-device", name);
902 return;
903 }
904 if (ht_lookup(devitab, name) != NULL((void *)0)) {
905 warnx("warning: duplicate definition of `%s', will use latest definition", name);
906 d->d_umax = number;
907 return;
908 }
909 i = newdevi(name, number - 1, d); /* foo 16 => "foo0..foo15" */
910 if (ht_insert(devitab, name, i)ht_insrep(devitab, name, i, 0))
911 panic("addpseudo(%s)", name);
912 i->i_disable = disable;
913 selectbase(d, NULL((void *)0));
914 *nextpseudo = i;
915 nextpseudo = &i->i_next;
916 npseudo++;
917}
918
919/*
920 * Define a new instance of a specific device.
921 */
922static struct devi *
923getdevi(const char *name)
924{
925 struct devi *i, *firsti;
926 struct devbase *d;
927 int unit;
928 char base[NAMESIZE100];
929
930 if (split(name, strlen(name), base, sizeof base, &unit)) {
931 error("invalid device name `%s'", name);
932 return (NULL((void *)0));
933 }
934 d = ht_lookup(devbasetab, intern(base));
935 if (d == NULL((void *)0)) {
936 error("%s: unknown device `%s'", name, base);
937 return (NULL((void *)0));
938 }
939 if (d->d_ispseudo) {
940 error("%s: %s is a pseudo-device", name, base);
941 return (NULL((void *)0));
942 }
943 firsti = ht_lookup(devitab, name);
944 i = newdevi(name, unit, d);
945 if (firsti == NULL((void *)0)) {
946 if (ht_insert(devitab, name, i)ht_insrep(devitab, name, i, 0))
947 panic("getdevi(%s)", name);
948 *d->d_ipp = i;
949 d->d_ipp = &i->i_bsame;
950 } else {
951 while (firsti->i_alias)
952 firsti = firsti->i_alias;
953 firsti->i_alias = i;
954 }
955 *nextdevi = i;
956 nextdevi = &i->i_next;
957 ndevi++;
958 return (i);
959}
960
961static const char *
962concat(const char *name, int c)
963{
964 size_t len;
965 char buf[NAMESIZE100];
966
967 len = strlen(name);
968 if (len + 2 > sizeof(buf)) {
969 error("device name `%s%c' too long", name, c);
970 len = sizeof(buf) - 2;
971 }
972 bcopy(name, buf, len);
973 buf[len] = c;
974 buf[len + 1] = 0;
975 return (intern(buf));
976}
977
978const char *
979starref(const char *name)
980{
981
982 return (concat(name, '*'));
983}
984
985const char *
986wildref(const char *name)
987{
988
989 return (concat(name, '?'));
990}
991
992/*
993 * Split a name like "foo0" into base name (foo) and unit number (0).
994 * Return 0 on success. To make this useful for names like "foo0a",
995 * the length of the "foo0" part is one of the arguments.
996 */
997static int
998split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit)
999{
1000 const char *cp;
1001 int c;
1002 size_t l;
1003
1004 l = nlen;
1005 if (l < 2 || l >= bsize || isdigit((unsigned char)*name))
1006 return (1);
1007 c = (u_char)name[--l];
1008 if (!isdigit((unsigned char)c)) {
1009 if (c == '*')
1010 *aunit = STAR(-1);
1011 else if (c == '?')
1012 *aunit = WILD(-2);
1013 else
1014 return (1);
1015 } else {
1016 cp = &name[l];
1017 while (isdigit((unsigned char)cp[-1]))
1018 l--, cp--;
1019 *aunit = atoi(cp);
1020 }
1021 bcopy(name, base, l);
1022 base[l] = 0;
1023 return (0);
1024}
1025
1026/*
1027 * We have an instance of the base foo, so select it and all its
1028 * attributes for "optional foo".
1029 */
1030static void
1031selectbase(struct devbase *d, struct deva *da)
1032{
1033 struct attr *a;
1034 struct nvlist *nv;
1035
1036 (void)ht_insert(selecttab, d->d_name, (char *)d->d_name)ht_insrep(selecttab, d->d_name, (char *)d->d_name, 0);
1037 for (nv = d->d_attrs; nv != NULL((void *)0); nv = nv->nv_next) {
1038 a = nv->nv_ptrnv_un.un_ptr;
1039 (void)ht_insert(selecttab, a->a_name, (char *)a->a_name)ht_insrep(selecttab, a->a_name, (char *)a->a_name, 0);
1040 }
1041 if (da != NULL((void *)0)) {
1042 (void)ht_insert(selecttab, da->d_name, (char *)da->d_name)ht_insrep(selecttab, da->d_name, (char *)da->d_name, 0);
1043 for (nv = da->d_attrs; nv != NULL((void *)0); nv = nv->nv_next) {
1044 a = nv->nv_ptrnv_un.un_ptr;
1045 (void)ht_insert(selecttab, a->a_name,ht_insrep(selecttab, a->a_name, (char *)a->a_name, 0)
1046 (char *)a->a_name)ht_insrep(selecttab, a->a_name, (char *)a->a_name, 0);
1047 }
1048 }
1049}
1050
1051/*
1052 * Is the given pointer on the given list of pointers?
1053 */
1054static int
1055onlist(struct nvlist *nv, void *ptr)
1056{
1057 for (; nv != NULL((void *)0); nv = nv->nv_next)
1058 if (nv->nv_ptrnv_un.un_ptr == ptr)
1059 return (1);
1060 return (0);
1061}
1062
1063static char *
1064extend(char *p, const char *name)
1065{
1066 int l;
1067
1068 l = strlen(name);
1069 bcopy(name, p, l);
1070 p += l;
1071 *p++ = ',';
1072 *p++ = ' ';
1073 return (p);
1074}
1075
1076/*
1077 * Check that we got all required locators, and default any that are
1078 * given as "?" and have defaults. Return 0 on success.
1079 */
1080static const char **
1081fixloc(const char *name, struct attr *attr, struct nvlist *got)
1082{
1083 struct nvlist *m, *n;
1084 int ord;
1085 const char **lp;
1086 int nmissing, nextra, nnodefault;
1087 char *mp, *ep, *ndp;
1088 char missing[1000], extra[1000], nodefault[1000];
1089 static const char *nullvec[1];
1090
1091 /*
1092 * Look for all required locators, and number the given ones
1093 * according to the required order. While we are numbering,
1094 * set default values for defaulted locators.
1095 */
1096 if (attr->a_loclen == 0) /* e.g., "at root" */
1097 lp = nullvec;
1098 else
1099 lp = ereallocarray(NULL((void *)0), attr->a_loclen + 1,
1100 sizeof(const char *));
1101 for (n = got; n != NULL((void *)0); n = n->nv_next)
1102 n->nv_int = -1;
1103 nmissing = 0;
1104 mp = missing;
1105 /* yes, this is O(mn), but m and n should be small */
1106 for (ord = 0, m = attr->a_locs; m != NULL((void *)0); m = m->nv_next, ord++) {
1107 for (n = got; n != NULL((void *)0); n = n->nv_next) {
1108 if (n->nv_name == m->nv_name) {
1109 n->nv_int = ord;
1110 break;
1111 }
1112 }
1113 if (n == NULL((void *)0) && m->nv_int == 0) {
1114 nmissing++;
1115 mp = extend(mp, m->nv_name);
1116 }
1117 lp[ord] = m->nv_strnv_un.un_str;
1118 }
1119 if (ord != attr->a_loclen)
1120 panic("fixloc");
1121 lp[ord] = NULL((void *)0);
1122 nextra = 0;
1123 ep = extra;
1124 nnodefault = 0;
1125 ndp = nodefault;
1126 for (n = got; n != NULL((void *)0); n = n->nv_next) {
1127 if (n->nv_int >= 0) {
1128 if (n->nv_strnv_un.un_str != NULL((void *)0))
1129 lp[n->nv_int] = n->nv_strnv_un.un_str;
1130 else if (lp[n->nv_int] == NULL((void *)0)) {
1131 nnodefault++;
1132 ndp = extend(ndp, n->nv_name);
1133 }
1134 } else {
1135 nextra++;
1136 ep = extend(ep, n->nv_name);
1137 }
1138 }
1139 if (nextra) {
1140 ep[-2] = 0; /* kill ", " */
1141 error("%s: extraneous locator%s: %s",
1142 name, nextra > 1 ? "s" : "", extra);
1143 }
1144 if (nmissing) {
1145 mp[-2] = 0;
1146 error("%s: must specify %s", name, missing);
1147 }
1148 if (nnodefault) {
1149 ndp[-2] = 0;
1150 error("%s: cannot wildcard %s", name, nodefault);
1151 }
1152 if (nmissing || nnodefault) {
1153 free(lp);
1154 lp = NULL((void *)0);
1155 }
1156 return (lp);
1157}