xref: /netbsd-src/sys/kern/kern_module.c (revision 6cf6fe02a981b55727c49c3d37b0d8191a98c0ee)
1 /*	$NetBSD: kern_module.c,v 1.101 2014/09/05 06:50:58 matt Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software developed for The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Kernel module support.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.101 2014/09/05 06:50:58 matt Exp $");
38 
39 #define _MODULE_INTERNAL
40 
41 #ifdef _KERNEL_OPT
42 #include "opt_ddb.h"
43 #include "opt_modular.h"
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/proc.h>
50 #include <sys/kauth.h>
51 #include <sys/kobj.h>
52 #include <sys/kmem.h>
53 #include <sys/module.h>
54 #include <sys/kthread.h>
55 #include <sys/sysctl.h>
56 #include <sys/lock.h>
57 
58 #include <uvm/uvm_extern.h>
59 
60 struct vm_map *module_map;
61 char	*module_machine;
62 char	module_base[MODULE_BASE_SIZE];
63 
64 struct modlist        module_list = TAILQ_HEAD_INITIALIZER(module_list);
65 struct modlist        module_builtins = TAILQ_HEAD_INITIALIZER(module_builtins);
66 static struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
67 
68 static module_t	*module_active;
69 static bool	module_verbose_on;
70 #ifdef MODULAR_DEFAULT_AUTOLOAD
71 static bool	module_autoload_on = true;
72 #else
73 static bool	module_autoload_on = false;
74 #endif
75 u_int		module_count;
76 u_int		module_builtinlist;
77 u_int		module_autotime = 10;
78 u_int		module_gen = 1;
79 static kcondvar_t module_thread_cv;
80 static kmutex_t module_thread_lock;
81 static int	module_thread_ticks;
82 int (*module_load_vfs_vec)(const char *, int, bool, module_t *,
83 			   prop_dictionary_t *) = (void *)eopnotsupp;
84 
85 static kauth_listener_t	module_listener;
86 
87 /* Ensure that the kernel's link set isn't empty. */
88 static modinfo_t module_dummy;
89 __link_set_add_rodata(modules, module_dummy);
90 
91 static module_t	*module_newmodule(modsrc_t);
92 static void	module_require_force(module_t *);
93 static int	module_do_load(const char *, bool, int, prop_dictionary_t,
94 		    module_t **, modclass_t modclass, bool);
95 static int	module_do_unload(const char *, bool);
96 static int	module_do_builtin(const char *, module_t **, prop_dictionary_t);
97 static int	module_fetch_info(module_t *);
98 static void	module_thread(void *);
99 
100 static module_t	*module_lookup(const char *);
101 static void	module_enqueue(module_t *);
102 
103 static bool	module_merge_dicts(prop_dictionary_t, const prop_dictionary_t);
104 
105 static void	sysctl_module_setup(void);
106 static int	sysctl_module_autotime(SYSCTLFN_PROTO);
107 
108 #define MODULE_CLASS_MATCH(mi, modclass) \
109 	((modclass) == MODULE_CLASS_ANY || (modclass) == (mi)->mi_class)
110 
111 static void
112 module_incompat(const modinfo_t *mi, int modclass)
113 {
114 	module_error("incompatible module class for `%s' (%d != %d)",
115 	    mi->mi_name, modclass, mi->mi_class);
116 }
117 
118 /*
119  * module_error:
120  *
121  *	Utility function: log an error.
122  */
123 void
124 module_error(const char *fmt, ...)
125 {
126 	va_list ap;
127 
128 	va_start(ap, fmt);
129 	printf("WARNING: module error: ");
130 	vprintf(fmt, ap);
131 	printf("\n");
132 	va_end(ap);
133 }
134 
135 /*
136  * module_print:
137  *
138  *	Utility function: log verbose output.
139  */
140 void
141 module_print(const char *fmt, ...)
142 {
143 	va_list ap;
144 
145 	if (module_verbose_on) {
146 		va_start(ap, fmt);
147 		printf("DEBUG: module: ");
148 		vprintf(fmt, ap);
149 		printf("\n");
150 		va_end(ap);
151 	}
152 }
153 
154 static int
155 module_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
156     void *arg0, void *arg1, void *arg2, void *arg3)
157 {
158 	int result;
159 
160 	result = KAUTH_RESULT_DEFER;
161 
162 	if (action != KAUTH_SYSTEM_MODULE)
163 		return result;
164 
165 	if ((uintptr_t)arg2 != 0)	/* autoload */
166 		result = KAUTH_RESULT_ALLOW;
167 
168 	return result;
169 }
170 
171 /*
172  * Allocate a new module_t
173  */
174 static module_t *
175 module_newmodule(modsrc_t source)
176 {
177 	module_t *mod;
178 
179 	mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
180 	if (mod != NULL) {
181 		mod->mod_source = source;
182 		mod->mod_info = NULL;
183 		mod->mod_flags = 0;
184 	}
185 	return mod;
186 }
187 
188 /*
189  * Require the -f (force) flag to load a module
190  */
191 static void
192 module_require_force(struct module *mod)
193 {
194 	mod->mod_flags |= MODFLG_MUST_FORCE;
195 }
196 
197 /*
198  * Add modules to the builtin list.  This can done at boottime or
199  * at runtime if the module is linked into the kernel with an
200  * external linker.  All or none of the input will be handled.
201  * Optionally, the modules can be initialized.  If they are not
202  * initialized, module_init_class() or module_load() can be used
203  * later, but these are not guaranteed to give atomic results.
204  */
205 int
206 module_builtin_add(modinfo_t *const *mip, size_t nmodinfo, bool init)
207 {
208 	struct module **modp = NULL, *mod_iter;
209 	int rv = 0, i, mipskip;
210 
211 	if (init) {
212 		rv = kauth_authorize_system(kauth_cred_get(),
213 		    KAUTH_SYSTEM_MODULE, 0, (void *)(uintptr_t)MODCTL_LOAD,
214 		    (void *)(uintptr_t)1, NULL);
215 		if (rv) {
216 			return rv;
217 		}
218 	}
219 
220 	for (i = 0, mipskip = 0; i < nmodinfo; i++) {
221 		if (mip[i] == &module_dummy) {
222 			KASSERT(nmodinfo > 0);
223 			nmodinfo--;
224 		}
225 	}
226 	if (nmodinfo == 0)
227 		return 0;
228 
229 	modp = kmem_zalloc(sizeof(*modp) * nmodinfo, KM_SLEEP);
230 	for (i = 0, mipskip = 0; i < nmodinfo; i++) {
231 		if (mip[i+mipskip] == &module_dummy) {
232 			mipskip++;
233 			continue;
234 		}
235 		modp[i] = module_newmodule(MODULE_SOURCE_KERNEL);
236 		modp[i]->mod_info = mip[i+mipskip];
237 	}
238 	kernconfig_lock();
239 
240 	/* do this in three stages for error recovery and atomicity */
241 
242 	/* first check for presence */
243 	for (i = 0; i < nmodinfo; i++) {
244 		TAILQ_FOREACH(mod_iter, &module_builtins, mod_chain) {
245 			if (strcmp(mod_iter->mod_info->mi_name,
246 			    modp[i]->mod_info->mi_name) == 0)
247 				break;
248 		}
249 		if (mod_iter) {
250 			rv = EEXIST;
251 			goto out;
252 		}
253 
254 		if (module_lookup(modp[i]->mod_info->mi_name) != NULL) {
255 			rv = EEXIST;
256 			goto out;
257 		}
258 	}
259 
260 	/* then add to list */
261 	for (i = 0; i < nmodinfo; i++) {
262 		TAILQ_INSERT_TAIL(&module_builtins, modp[i], mod_chain);
263 		module_builtinlist++;
264 	}
265 
266 	/* finally, init (if required) */
267 	if (init) {
268 		for (i = 0; i < nmodinfo; i++) {
269 			rv = module_do_builtin(modp[i]->mod_info->mi_name,
270 			    NULL, NULL);
271 			/* throw in the towel, recovery hard & not worth it */
272 			if (rv)
273 				panic("builtin module \"%s\" init failed: %d",
274 				    modp[i]->mod_info->mi_name, rv);
275 		}
276 	}
277 
278  out:
279 	kernconfig_unlock();
280 	if (rv != 0) {
281 		for (i = 0; i < nmodinfo; i++) {
282 			if (modp[i])
283 				kmem_free(modp[i], sizeof(*modp[i]));
284 		}
285 	}
286 	kmem_free(modp, sizeof(*modp) * nmodinfo);
287 	return rv;
288 }
289 
290 /*
291  * Optionally fini and remove builtin module from the kernel.
292  * Note: the module will now be unreachable except via mi && builtin_add.
293  */
294 int
295 module_builtin_remove(modinfo_t *mi, bool fini)
296 {
297 	struct module *mod;
298 	int rv = 0;
299 
300 	if (fini) {
301 		rv = kauth_authorize_system(kauth_cred_get(),
302 		    KAUTH_SYSTEM_MODULE, 0, (void *)(uintptr_t)MODCTL_UNLOAD,
303 		    NULL, NULL);
304 		if (rv)
305 			return rv;
306 
307 		kernconfig_lock();
308 		rv = module_do_unload(mi->mi_name, true);
309 		if (rv) {
310 			goto out;
311 		}
312 	} else {
313 		kernconfig_lock();
314 	}
315 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
316 		if (strcmp(mod->mod_info->mi_name, mi->mi_name) == 0)
317 			break;
318 	}
319 	if (mod) {
320 		TAILQ_REMOVE(&module_builtins, mod, mod_chain);
321 		module_builtinlist--;
322 	} else {
323 		KASSERT(fini == false);
324 		rv = ENOENT;
325 	}
326 
327  out:
328 	kernconfig_unlock();
329 	return rv;
330 }
331 
332 /*
333  * module_init:
334  *
335  *	Initialize the module subsystem.
336  */
337 void
338 module_init(void)
339 {
340 	__link_set_decl(modules, modinfo_t);
341 	extern struct vm_map *module_map;
342 	modinfo_t *const *mip;
343 	int rv;
344 
345 	if (module_map == NULL) {
346 		module_map = kernel_map;
347 	}
348 	cv_init(&module_thread_cv, "mod_unld");
349 	mutex_init(&module_thread_lock, MUTEX_DEFAULT, IPL_NONE);
350 
351 #ifdef MODULAR	/* XXX */
352 	module_init_md();
353 #endif
354 
355 	if (!module_machine)
356 		module_machine = machine;
357 #if __NetBSD_Version__ / 1000000 % 100 == 99	/* -current */
358 	snprintf(module_base, sizeof(module_base), "/stand/%s/%s/modules",
359 	    module_machine, osrelease);
360 #else						/* release */
361 	snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules",
362 	    module_machine, __NetBSD_Version__ / 100000000,
363 	    __NetBSD_Version__ / 1000000 % 100);
364 #endif
365 
366 	module_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
367 	    module_listener_cb, NULL);
368 
369 	__link_set_foreach(mip, modules) {
370 		if ((rv = module_builtin_add(mip, 1, false)) != 0)
371 			module_error("builtin %s failed: %d\n",
372 			    (*mip)->mi_name, rv);
373 	}
374 
375 	sysctl_module_setup();
376 	aprint_normal("kern.module.path=%s\n", module_base);
377 }
378 
379 /*
380  * module_start_unload_thread:
381  *
382  *	Start the auto unload kthread.
383  */
384 void
385 module_start_unload_thread(void)
386 {
387 	int error;
388 
389 	error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, module_thread,
390 	    NULL, NULL, "modunload");
391 	if (error != 0)
392 		panic("module_init: %d", error);
393 }
394 
395 /*
396  * module_builtin_require_force
397  *
398  * Require MODCTL_MUST_FORCE to load any built-in modules that have
399  * not yet been initialized
400  */
401 void
402 module_builtin_require_force(void)
403 {
404 	module_t *mod;
405 
406 	kernconfig_lock();
407 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
408 		module_require_force(mod);
409 	}
410 	kernconfig_unlock();
411 }
412 
413 static struct sysctllog *module_sysctllog;
414 
415 static int
416 sysctl_module_autotime(SYSCTLFN_ARGS)
417 {
418 	struct sysctlnode node;
419 	int t, error;
420 
421 	t = *(int *)rnode->sysctl_data;
422 
423 	node = *rnode;
424 	node.sysctl_data = &t;
425 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
426 	if (error || newp == NULL)
427 		return (error);
428 
429 	if (t < 0)
430 		return (EINVAL);
431 
432 	*(int *)rnode->sysctl_data = t;
433 	return (0);
434 }
435 
436 static void
437 sysctl_module_setup(void)
438 {
439 	const struct sysctlnode *node = NULL;
440 
441 	sysctl_createv(&module_sysctllog, 0, NULL, &node,
442 		CTLFLAG_PERMANENT,
443 		CTLTYPE_NODE, "module",
444 		SYSCTL_DESCR("Module options"),
445 		NULL, 0, NULL, 0,
446 		CTL_KERN, CTL_CREATE, CTL_EOL);
447 
448 	if (node == NULL)
449 		return;
450 
451 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
452 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
453 		CTLTYPE_BOOL, "autoload",
454 		SYSCTL_DESCR("Enable automatic load of modules"),
455 		NULL, 0, &module_autoload_on, 0,
456 		CTL_CREATE, CTL_EOL);
457 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
458 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
459 		CTLTYPE_BOOL, "verbose",
460 		SYSCTL_DESCR("Enable verbose output"),
461 		NULL, 0, &module_verbose_on, 0,
462 		CTL_CREATE, CTL_EOL);
463 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
464 		CTLFLAG_PERMANENT | CTLFLAG_READONLY,
465 		CTLTYPE_STRING, "path",
466 		SYSCTL_DESCR("Default module load path"),
467 		NULL, 0, module_base, 0,
468 		CTL_CREATE, CTL_EOL);
469 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
470 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
471 		CTLTYPE_INT, "autotime",
472 		SYSCTL_DESCR("Auto-unload delay"),
473 		sysctl_module_autotime, 0, &module_autotime, 0,
474 		CTL_CREATE, CTL_EOL);
475 }
476 
477 /*
478  * module_init_class:
479  *
480  *	Initialize all built-in and pre-loaded modules of the
481  *	specified class.
482  */
483 void
484 module_init_class(modclass_t modclass)
485 {
486 	TAILQ_HEAD(, module) bi_fail = TAILQ_HEAD_INITIALIZER(bi_fail);
487 	module_t *mod;
488 	modinfo_t *mi;
489 
490 	kernconfig_lock();
491 	/*
492 	 * Builtins first.  These will not depend on pre-loaded modules
493 	 * (because the kernel would not link).
494 	 */
495 	do {
496 		TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
497 			mi = mod->mod_info;
498 			if (!MODULE_CLASS_MATCH(mi, modclass))
499 				continue;
500 			/*
501 			 * If initializing a builtin module fails, don't try
502 			 * to load it again.  But keep it around and queue it
503 			 * on the builtins list after we're done with module
504 			 * init.  Don't set it to MODFLG_MUST_FORCE in case a
505 			 * future attempt to initialize can be successful.
506 			 * (If the module has previously been set to
507 			 * MODFLG_MUST_FORCE, don't try to override that!)
508 			 */
509 			if ((mod->mod_flags & MODFLG_MUST_FORCE) ||
510 			    module_do_builtin(mi->mi_name, NULL, NULL) != 0) {
511 				TAILQ_REMOVE(&module_builtins, mod, mod_chain);
512 				TAILQ_INSERT_TAIL(&bi_fail, mod, mod_chain);
513 			}
514 			break;
515 		}
516 	} while (mod != NULL);
517 
518 	/*
519 	 * Now preloaded modules.  These will be pulled off the
520 	 * list as we call module_do_load();
521 	 */
522 	do {
523 		TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
524 			mi = mod->mod_info;
525 			if (!MODULE_CLASS_MATCH(mi, modclass))
526 				continue;
527 			module_do_load(mi->mi_name, false, 0, NULL, NULL,
528 			    modclass, false);
529 			break;
530 		}
531 	} while (mod != NULL);
532 
533 	/* return failed builtin modules to builtin list */
534 	while ((mod = TAILQ_FIRST(&bi_fail)) != NULL) {
535 		TAILQ_REMOVE(&bi_fail, mod, mod_chain);
536 		TAILQ_INSERT_TAIL(&module_builtins, mod, mod_chain);
537 	}
538 
539 	kernconfig_unlock();
540 }
541 
542 /*
543  * module_compatible:
544  *
545  *	Return true if the two supplied kernel versions are said to
546  *	have the same binary interface for kernel code.  The entire
547  *	version is signficant for the development tree (-current),
548  *	major and minor versions are significant for official
549  *	releases of the system.
550  */
551 bool
552 module_compatible(int v1, int v2)
553 {
554 
555 #if __NetBSD_Version__ / 1000000 % 100 == 99	/* -current */
556 	return v1 == v2;
557 #else						/* release */
558 	return abs(v1 - v2) < 10000;
559 #endif
560 }
561 
562 /*
563  * module_load:
564  *
565  *	Load a single module from the file system.
566  */
567 int
568 module_load(const char *filename, int flags, prop_dictionary_t props,
569 	    modclass_t modclass)
570 {
571 	int error;
572 
573 	/* Authorize. */
574 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
575 	    0, (void *)(uintptr_t)MODCTL_LOAD, NULL, NULL);
576 	if (error != 0) {
577 		return error;
578 	}
579 
580 	kernconfig_lock();
581 	error = module_do_load(filename, false, flags, props, NULL, modclass,
582 	    false);
583 	kernconfig_unlock();
584 
585 	return error;
586 }
587 
588 /*
589  * module_autoload:
590  *
591  *	Load a single module from the file system, system initiated.
592  */
593 int
594 module_autoload(const char *filename, modclass_t modclass)
595 {
596 	int error;
597 
598 	kernconfig_lock();
599 
600 	/* Nothing if the user has disabled it. */
601 	if (!module_autoload_on) {
602 		kernconfig_unlock();
603 		return EPERM;
604 	}
605 
606         /* Disallow path separators and magic symlinks. */
607         if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL ||
608             strchr(filename, '.') != NULL) {
609 		kernconfig_unlock();
610         	return EPERM;
611 	}
612 
613 	/* Authorize. */
614 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
615 	    0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL);
616 
617 	if (error == 0)
618 		error = module_do_load(filename, false, 0, NULL, NULL, modclass,
619 		    true);
620 
621 	kernconfig_unlock();
622 	return error;
623 }
624 
625 /*
626  * module_unload:
627  *
628  *	Find and unload a module by name.
629  */
630 int
631 module_unload(const char *name)
632 {
633 	int error;
634 
635 	/* Authorize. */
636 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
637 	    0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL);
638 	if (error != 0) {
639 		return error;
640 	}
641 
642 	kernconfig_lock();
643 	error = module_do_unload(name, true);
644 	kernconfig_unlock();
645 
646 	return error;
647 }
648 
649 /*
650  * module_lookup:
651  *
652  *	Look up a module by name.
653  */
654 module_t *
655 module_lookup(const char *name)
656 {
657 	module_t *mod;
658 
659 	KASSERT(kernconfig_is_held());
660 
661 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
662 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
663 			break;
664 		}
665 	}
666 
667 	return mod;
668 }
669 
670 /*
671  * module_hold:
672  *
673  *	Add a single reference to a module.  It's the caller's
674  *	responsibility to ensure that the reference is dropped
675  *	later.
676  */
677 int
678 module_hold(const char *name)
679 {
680 	module_t *mod;
681 
682 	kernconfig_lock();
683 	mod = module_lookup(name);
684 	if (mod == NULL) {
685 		kernconfig_unlock();
686 		return ENOENT;
687 	}
688 	mod->mod_refcnt++;
689 	kernconfig_unlock();
690 
691 	return 0;
692 }
693 
694 /*
695  * module_rele:
696  *
697  *	Release a reference acquired with module_hold().
698  */
699 void
700 module_rele(const char *name)
701 {
702 	module_t *mod;
703 
704 	kernconfig_lock();
705 	mod = module_lookup(name);
706 	if (mod == NULL) {
707 		kernconfig_unlock();
708 		panic("module_rele: gone");
709 	}
710 	mod->mod_refcnt--;
711 	kernconfig_unlock();
712 }
713 
714 /*
715  * module_enqueue:
716  *
717  *	Put a module onto the global list and update counters.
718  */
719 void
720 module_enqueue(module_t *mod)
721 {
722 	int i;
723 
724 	KASSERT(kernconfig_is_held());
725 
726 	/*
727 	 * If there are requisite modules, put at the head of the queue.
728 	 * This is so that autounload can unload requisite modules with
729 	 * only one pass through the queue.
730 	 */
731 	if (mod->mod_nrequired) {
732 		TAILQ_INSERT_HEAD(&module_list, mod, mod_chain);
733 
734 		/* Add references to the requisite modules. */
735 		for (i = 0; i < mod->mod_nrequired; i++) {
736 			KASSERT(mod->mod_required[i] != NULL);
737 			mod->mod_required[i]->mod_refcnt++;
738 		}
739 	} else {
740 		TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
741 	}
742 	module_count++;
743 	module_gen++;
744 }
745 
746 /*
747  * module_do_builtin:
748  *
749  *	Initialize a module from the list of modules that are
750  *	already linked into the kernel.
751  */
752 static int
753 module_do_builtin(const char *name, module_t **modp, prop_dictionary_t props)
754 {
755 	const char *p, *s;
756 	char buf[MAXMODNAME];
757 	modinfo_t *mi = NULL;
758 	module_t *mod, *mod2, *mod_loaded, *prev_active;
759 	size_t len;
760 	int error;
761 
762 	KASSERT(kernconfig_is_held());
763 
764 	/*
765 	 * Search the list to see if we have a module by this name.
766 	 */
767 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
768 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
769 			mi = mod->mod_info;
770 			break;
771 		}
772 	}
773 
774 	/*
775 	 * Check to see if already loaded.  This might happen if we
776 	 * were already loaded as a dependency.
777 	 */
778 	if ((mod_loaded = module_lookup(name)) != NULL) {
779 		KASSERT(mod == NULL);
780 		if (modp)
781 			*modp = mod_loaded;
782 		return 0;
783 	}
784 
785 	/* Note! This is from TAILQ, not immediate above */
786 	if (mi == NULL) {
787 		/*
788 		 * XXX: We'd like to panic here, but currently in some
789 		 * cases (such as nfsserver + nfs), the dependee can be
790 		 * succesfully linked without the dependencies.
791 		 */
792 		module_error("can't find builtin dependency `%s'", name);
793 		return ENOENT;
794 	}
795 
796 	/*
797 	 * Initialize pre-requisites.
798 	 */
799 	if (mi->mi_required != NULL) {
800 		for (s = mi->mi_required; *s != '\0'; s = p) {
801 			if (*s == ',')
802 				s++;
803 			p = s;
804 			while (*p != '\0' && *p != ',')
805 				p++;
806 			len = min(p - s + 1, sizeof(buf));
807 			strlcpy(buf, s, len);
808 			if (buf[0] == '\0')
809 				break;
810 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
811 				module_error("too many required modules "
812 				    "%d >= %d", mod->mod_nrequired,
813 				    MAXMODDEPS - 1);
814 				return EINVAL;
815 			}
816 			error = module_do_builtin(buf, &mod2, NULL);
817 			if (error != 0) {
818 				return error;
819 			}
820 			mod->mod_required[mod->mod_nrequired++] = mod2;
821 		}
822 	}
823 
824 	/*
825 	 * Try to initialize the module.
826 	 */
827 	prev_active = module_active;
828 	module_active = mod;
829 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
830 	module_active = prev_active;
831 	if (error != 0) {
832 		module_error("builtin module `%s' "
833 		    "failed to init, error %d", mi->mi_name, error);
834 		return error;
835 	}
836 
837 	/* load always succeeds after this point */
838 
839 	TAILQ_REMOVE(&module_builtins, mod, mod_chain);
840 	module_builtinlist--;
841 	if (modp != NULL) {
842 		*modp = mod;
843 	}
844 	module_enqueue(mod);
845 	return 0;
846 }
847 
848 /*
849  * module_do_load:
850  *
851  *	Helper routine: load a module from the file system, or one
852  *	pushed by the boot loader.
853  */
854 static int
855 module_do_load(const char *name, bool isdep, int flags,
856 	       prop_dictionary_t props, module_t **modp, modclass_t modclass,
857 	       bool autoload)
858 {
859 #define MODULE_MAX_DEPTH 6
860 
861 	TAILQ_HEAD(pending_t, module);
862 	static int depth = 0;
863 	static struct pending_t *pending_lists[MODULE_MAX_DEPTH];
864 	struct pending_t *pending;
865 	struct pending_t new_pending = TAILQ_HEAD_INITIALIZER(new_pending);
866 	modinfo_t *mi;
867 	module_t *mod, *mod2, *prev_active;
868 	prop_dictionary_t filedict;
869 	char buf[MAXMODNAME];
870 	const char *s, *p;
871 	int error;
872 	size_t len;
873 
874 	KASSERT(kernconfig_is_held());
875 
876 	filedict = NULL;
877 	error = 0;
878 
879 	/*
880 	 * Avoid recursing too far.
881 	 */
882 	if (++depth > MODULE_MAX_DEPTH) {
883 		module_error("recursion too deep for `%s' %d > %d", name,
884 		    depth, MODULE_MAX_DEPTH);
885 		depth--;
886 		return EMLINK;
887 	}
888 
889 	/*
890 	 * Set up the pending list for this depth.  If this is a
891 	 * recursive entry, then use same list as for outer call,
892 	 * else use the locally allocated list.  In either case,
893 	 * remember which one we're using.
894 	 */
895 	if (isdep) {
896 		KASSERT(depth > 1);
897 		pending = pending_lists[depth - 2];
898 	} else
899 		pending = &new_pending;
900 	pending_lists[depth - 1] = pending;
901 
902 	/*
903 	 * Search the list of disabled builtins first.
904 	 */
905 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
906 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
907 			break;
908 		}
909 	}
910 	if (mod) {
911 		if ((mod->mod_flags & MODFLG_MUST_FORCE) &&
912 		    (flags & MODCTL_LOAD_FORCE) == 0) {
913 			if (!autoload) {
914 				module_error("use -f to reinstate "
915 				    "builtin module `%s'", name);
916 			}
917 			depth--;
918 			return EPERM;
919 		} else {
920 			error = module_do_builtin(name, modp, props);
921 			depth--;
922 			return error;
923 		}
924 	}
925 
926 	/*
927 	 * Load the module and link.  Before going to the file system,
928 	 * scan the list of modules loaded by the boot loader.
929 	 */
930 	TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
931 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
932 			TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
933 			break;
934 		}
935 	}
936 	if (mod != NULL) {
937 		TAILQ_INSERT_TAIL(pending, mod, mod_chain);
938 	} else {
939 		/*
940 		 * If a requisite module, check to see if it is
941 		 * already present.
942 		 */
943 		if (isdep) {
944 			mod = module_lookup(name);
945 			if (mod != NULL) {
946 				if (modp != NULL) {
947 					*modp = mod;
948 				}
949 				depth--;
950 				return 0;
951 			}
952 		}
953 		mod = module_newmodule(MODULE_SOURCE_FILESYS);
954 		if (mod == NULL) {
955 			module_error("out of memory for `%s'", name);
956 			depth--;
957 			return ENOMEM;
958 		}
959 
960 		error = module_load_vfs_vec(name, flags, autoload, mod,
961 					    &filedict);
962 		if (error != 0) {
963 #ifdef DEBUG
964 			/*
965 			 * The exec class of modules contains a list of
966 			 * modules that is the union of all the modules
967 			 * available for each architecture, so we don't
968 			 * print an error if they are missing.
969 			 */
970 			if (modclass != MODULE_CLASS_EXEC || error != ENOENT)
971 				module_error("vfs load failed for `%s', "
972 				    "error %d", name, error);
973 #endif
974 			kmem_free(mod, sizeof(*mod));
975 			depth--;
976 			return error;
977 		}
978 		TAILQ_INSERT_TAIL(pending, mod, mod_chain);
979 
980 		error = module_fetch_info(mod);
981 		if (error != 0) {
982 			module_error("cannot fetch info for `%s', error %d",
983 			    name, error);
984 			goto fail;
985 		}
986 	}
987 
988 	/*
989 	 * Check compatibility.
990 	 */
991 	mi = mod->mod_info;
992 	if (strlen(mi->mi_name) >= MAXMODNAME) {
993 		error = EINVAL;
994 		module_error("module name `%s' longer than %d", mi->mi_name,
995 		    MAXMODNAME);
996 		goto fail;
997 	}
998 	if (!module_compatible(mi->mi_version, __NetBSD_Version__)) {
999 		module_error("module `%s' built for `%d', system `%d'",
1000 		    mi->mi_name, mi->mi_version, __NetBSD_Version__);
1001 		if ((flags & MODCTL_LOAD_FORCE) != 0) {
1002 			module_error("forced load, system may be unstable");
1003 		} else {
1004 			error = EPROGMISMATCH;
1005 			goto fail;
1006 		}
1007 	}
1008 
1009 	/*
1010 	 * If a specific kind of module was requested, ensure that we have
1011 	 * a match.
1012 	 */
1013 	if (!MODULE_CLASS_MATCH(mi, modclass)) {
1014 		module_incompat(mi, modclass);
1015 		error = ENOENT;
1016 		goto fail;
1017 	}
1018 
1019 	/*
1020 	 * If loading a dependency, `name' is a plain module name.
1021 	 * The name must match.
1022 	 */
1023 	if (isdep && strcmp(mi->mi_name, name) != 0) {
1024 		module_error("dependency name mismatch (`%s' != `%s')",
1025 		    name, mi->mi_name);
1026 		error = ENOENT;
1027 		goto fail;
1028 	}
1029 
1030 	/*
1031 	 * Check to see if the module is already loaded.  If so, we may
1032 	 * have been recursively called to handle a dependency, so be sure
1033 	 * to set modp.
1034 	 */
1035 	if ((mod2 = module_lookup(mi->mi_name)) != NULL) {
1036 		if (modp != NULL)
1037 			*modp = mod2;
1038 		module_print("module `%s' already loaded", mi->mi_name);
1039 		error = EEXIST;
1040 		goto fail;
1041 	}
1042 
1043 	/*
1044 	 * Block circular dependencies.
1045 	 */
1046 	TAILQ_FOREACH(mod2, pending, mod_chain) {
1047 		if (mod == mod2) {
1048 			continue;
1049 		}
1050 		if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
1051 		    	error = EDEADLK;
1052 			module_error("circular dependency detected for `%s'",
1053 			    mi->mi_name);
1054 		    	goto fail;
1055 		}
1056 	}
1057 
1058 	/*
1059 	 * Now try to load any requisite modules.
1060 	 */
1061 	if (mi->mi_required != NULL) {
1062 		for (s = mi->mi_required; *s != '\0'; s = p) {
1063 			if (*s == ',')
1064 				s++;
1065 			p = s;
1066 			while (*p != '\0' && *p != ',')
1067 				p++;
1068 			len = p - s + 1;
1069 			if (len >= MAXMODNAME) {
1070 				error = EINVAL;
1071 				module_error("required module name `%s' "
1072 				    "longer than %d", mi->mi_required,
1073 				    MAXMODNAME);
1074 				goto fail;
1075 			}
1076 			strlcpy(buf, s, len);
1077 			if (buf[0] == '\0')
1078 				break;
1079 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
1080 				error = EINVAL;
1081 				module_error("too many required modules "
1082 				    "%d >= %d", mod->mod_nrequired,
1083 				    MAXMODDEPS - 1);
1084 				goto fail;
1085 			}
1086 			if (strcmp(buf, mi->mi_name) == 0) {
1087 				error = EDEADLK;
1088 				module_error("self-dependency detected for "
1089 				   "`%s'", mi->mi_name);
1090 				goto fail;
1091 			}
1092 			error = module_do_load(buf, true, flags, NULL,
1093 			    &mod2, MODULE_CLASS_ANY, true);
1094 			if (error != 0) {
1095 				module_error("recursive load failed for `%s' "
1096 				    "(`%s' required), error %d", mi->mi_name,
1097 				    buf, error);
1098 				goto fail;
1099 			}
1100 			mod->mod_required[mod->mod_nrequired++] = mod2;
1101 		}
1102 	}
1103 
1104 	/*
1105 	 * We loaded all needed modules successfully: perform global
1106 	 * relocations and initialize.
1107 	 */
1108 	error = kobj_affix(mod->mod_kobj, mi->mi_name);
1109 	if (error != 0) {
1110 		/* Cannot touch 'mi' as the module is now gone. */
1111 		module_error("unable to affix module `%s', error %d", name,
1112 		    error);
1113 		goto fail2;
1114 	}
1115 
1116 	if (filedict) {
1117 		if (!module_merge_dicts(filedict, props)) {
1118 			module_error("module properties failed for %s", name);
1119 			error = EINVAL;
1120 			goto fail;
1121 		}
1122 	}
1123 	prev_active = module_active;
1124 	module_active = mod;
1125 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, filedict ? filedict : props);
1126 	module_active = prev_active;
1127 	if (filedict) {
1128 		prop_object_release(filedict);
1129 		filedict = NULL;
1130 	}
1131 	if (error != 0) {
1132 		module_error("modcmd function failed for `%s', error %d",
1133 		    mi->mi_name, error);
1134 		goto fail;
1135 	}
1136 
1137 	/*
1138 	 * Good, the module loaded successfully.  Put it onto the
1139 	 * list and add references to its requisite modules.
1140 	 */
1141 	TAILQ_REMOVE(pending, mod, mod_chain);
1142 	module_enqueue(mod);
1143 	if (modp != NULL) {
1144 		*modp = mod;
1145 	}
1146 	if (autoload && module_autotime > 0) {
1147 		/*
1148 		 * Arrange to try unloading the module after
1149 		 * a short delay unless auto-unload is disabled.
1150 		 */
1151 		mod->mod_autotime = time_second + module_autotime;
1152 		mod->mod_flags |= MODFLG_AUTO_LOADED;
1153 		module_thread_kick();
1154 	}
1155 	depth--;
1156 	return 0;
1157 
1158  fail:
1159 	kobj_unload(mod->mod_kobj);
1160  fail2:
1161 	if (filedict != NULL) {
1162 		prop_object_release(filedict);
1163 		filedict = NULL;
1164 	}
1165 	TAILQ_REMOVE(pending, mod, mod_chain);
1166 	kmem_free(mod, sizeof(*mod));
1167 	depth--;
1168 	return error;
1169 }
1170 
1171 /*
1172  * module_do_unload:
1173  *
1174  *	Helper routine: do the dirty work of unloading a module.
1175  */
1176 static int
1177 module_do_unload(const char *name, bool load_requires_force)
1178 {
1179 	module_t *mod, *prev_active;
1180 	int error;
1181 	u_int i;
1182 
1183 	KASSERT(kernconfig_is_held());
1184 	KASSERT(name != NULL);
1185 
1186 	mod = module_lookup(name);
1187 	if (mod == NULL) {
1188 		module_error("module `%s' not found", name);
1189 		return ENOENT;
1190 	}
1191 	if (mod->mod_refcnt != 0) {
1192 		module_print("module `%s' busy", name);
1193 		return EBUSY;
1194 	}
1195 
1196 	/*
1197 	 * Builtin secmodels are there to stay.
1198 	 */
1199 	if (mod->mod_source == MODULE_SOURCE_KERNEL &&
1200 	    mod->mod_info->mi_class == MODULE_CLASS_SECMODEL) {
1201 		return EPERM;
1202 	}
1203 
1204 	prev_active = module_active;
1205 	module_active = mod;
1206 	error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
1207 	module_active = prev_active;
1208 	if (error != 0) {
1209 		module_print("cannot unload module `%s' error=%d", name,
1210 		    error);
1211 		return error;
1212 	}
1213 	module_count--;
1214 	TAILQ_REMOVE(&module_list, mod, mod_chain);
1215 	for (i = 0; i < mod->mod_nrequired; i++) {
1216 		mod->mod_required[i]->mod_refcnt--;
1217 	}
1218 	module_print("unloaded module `%s'", name);
1219 	if (mod->mod_kobj != NULL) {
1220 		kobj_unload(mod->mod_kobj);
1221 	}
1222 	if (mod->mod_source == MODULE_SOURCE_KERNEL) {
1223 		mod->mod_nrequired = 0; /* will be re-parsed */
1224 		if (load_requires_force)
1225 			module_require_force(mod);
1226 		TAILQ_INSERT_TAIL(&module_builtins, mod, mod_chain);
1227 		module_builtinlist++;
1228 	} else {
1229 		kmem_free(mod, sizeof(*mod));
1230 	}
1231 	module_gen++;
1232 
1233 	return 0;
1234 }
1235 
1236 /*
1237  * module_prime:
1238  *
1239  *	Push a module loaded by the bootloader onto our internal
1240  *	list.
1241  */
1242 int
1243 module_prime(const char *name, void *base, size_t size)
1244 {
1245 	module_t *mod;
1246 	int error;
1247 
1248 	mod = module_newmodule(MODULE_SOURCE_BOOT);
1249 	if (mod == NULL) {
1250 		return ENOMEM;
1251 	}
1252 
1253 	error = kobj_load_mem(&mod->mod_kobj, name, base, size);
1254 	if (error != 0) {
1255 		kmem_free(mod, sizeof(*mod));
1256 		module_error("unable to load `%s' pushed by boot loader, "
1257 		    "error %d", name, error);
1258 		return error;
1259 	}
1260 	error = module_fetch_info(mod);
1261 	if (error != 0) {
1262 		kobj_unload(mod->mod_kobj);
1263 		kmem_free(mod, sizeof(*mod));
1264 		module_error("unable to load `%s' pushed by boot loader, "
1265 		    "error %d", name, error);
1266 		return error;
1267 	}
1268 
1269 	TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
1270 
1271 	return 0;
1272 }
1273 
1274 /*
1275  * module_fetch_into:
1276  *
1277  *	Fetch modinfo record from a loaded module.
1278  */
1279 static int
1280 module_fetch_info(module_t *mod)
1281 {
1282 	int error;
1283 	void *addr;
1284 	size_t size;
1285 
1286 	/*
1287 	 * Find module info record and check compatibility.
1288 	 */
1289 	error = kobj_find_section(mod->mod_kobj, "link_set_modules",
1290 	    &addr, &size);
1291 	if (error != 0) {
1292 		module_error("`link_set_modules' section not present, "
1293 		    "error %d", error);
1294 		return error;
1295 	}
1296 	if (size != sizeof(modinfo_t **)) {
1297 		module_error("`link_set_modules' section wrong size %zu != %zu",
1298 		    size, sizeof(modinfo_t **));
1299 		return ENOEXEC;
1300 	}
1301 	mod->mod_info = *(modinfo_t **)addr;
1302 
1303 	return 0;
1304 }
1305 
1306 /*
1307  * module_find_section:
1308  *
1309  *	Allows a module that is being initialized to look up a section
1310  *	within its ELF object.
1311  */
1312 int
1313 module_find_section(const char *name, void **addr, size_t *size)
1314 {
1315 
1316 	KASSERT(kernconfig_is_held());
1317 	KASSERT(module_active != NULL);
1318 
1319 	return kobj_find_section(module_active->mod_kobj, name, addr, size);
1320 }
1321 
1322 /*
1323  * module_thread:
1324  *
1325  *	Automatically unload modules.  We try once to unload autoloaded
1326  *	modules after module_autotime seconds.  If the system is under
1327  *	severe memory pressure, we'll try unloading all modules, else if
1328  *	module_autotime is zero, we don't try to unload, even if the
1329  *	module was previously scheduled for unload.
1330  */
1331 static void
1332 module_thread(void *cookie)
1333 {
1334 	module_t *mod, *next;
1335 	modinfo_t *mi;
1336 	int error;
1337 
1338 	for (;;) {
1339 		kernconfig_lock();
1340 		for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) {
1341 			next = TAILQ_NEXT(mod, mod_chain);
1342 
1343 			/* skip built-in modules */
1344 			if (mod->mod_source == MODULE_SOURCE_KERNEL)
1345 				continue;
1346 			/* skip modules that weren't auto-loaded */
1347 			if ((mod->mod_flags & MODFLG_AUTO_LOADED) == 0)
1348 				continue;
1349 
1350 			if (uvmexp.free < uvmexp.freemin) {
1351 				module_thread_ticks = hz;
1352 			} else if (module_autotime == 0 ||
1353 				   mod->mod_autotime == 0) {
1354 				continue;
1355 			} else if (time_second < mod->mod_autotime) {
1356 				module_thread_ticks = hz;
1357 			    	continue;
1358 			} else {
1359 				mod->mod_autotime = 0;
1360 			}
1361 
1362 			/*
1363 			 * If this module wants to avoid autounload then
1364 			 * skip it.  Some modules can ping-pong in and out
1365 			 * because their use is transient but often.
1366 			 * Example: exec_script.
1367 			 */
1368 			mi = mod->mod_info;
1369 			error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL);
1370 			if (error == 0 || error == ENOTTY) {
1371 				(void)module_do_unload(mi->mi_name, false);
1372 			}
1373 		}
1374 		kernconfig_unlock();
1375 
1376 		mutex_enter(&module_thread_lock);
1377 		(void)cv_timedwait(&module_thread_cv, &module_thread_lock,
1378 		    module_thread_ticks);
1379 		module_thread_ticks = 0;
1380 		mutex_exit(&module_thread_lock);
1381 	}
1382 }
1383 
1384 /*
1385  * module_thread:
1386  *
1387  *	Kick the module thread into action, perhaps because the
1388  *	system is low on memory.
1389  */
1390 void
1391 module_thread_kick(void)
1392 {
1393 
1394 	mutex_enter(&module_thread_lock);
1395 	module_thread_ticks = hz;
1396 	cv_broadcast(&module_thread_cv);
1397 	mutex_exit(&module_thread_lock);
1398 }
1399 
1400 #ifdef DDB
1401 /*
1402  * module_whatis:
1403  *
1404  *	Helper routine for DDB.
1405  */
1406 void
1407 module_whatis(uintptr_t addr, void (*pr)(const char *, ...))
1408 {
1409 	module_t *mod;
1410 	size_t msize;
1411 	vaddr_t maddr;
1412 
1413 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
1414 		if (mod->mod_kobj == NULL) {
1415 			continue;
1416 		}
1417 		if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0)
1418 			continue;
1419 		if (addr < maddr || addr >= maddr + msize) {
1420 			continue;
1421 		}
1422 		(*pr)("%p is %p+%zu, in kernel module `%s'\n",
1423 		    (void *)addr, (void *)maddr,
1424 		    (size_t)(addr - maddr), mod->mod_info->mi_name);
1425 	}
1426 }
1427 
1428 /*
1429  * module_print_list:
1430  *
1431  *	Helper routine for DDB.
1432  */
1433 void
1434 module_print_list(void (*pr)(const char *, ...))
1435 {
1436 	const char *src;
1437 	module_t *mod;
1438 	size_t msize;
1439 	vaddr_t maddr;
1440 
1441 	(*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE");
1442 
1443 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
1444 		switch (mod->mod_source) {
1445 		case MODULE_SOURCE_KERNEL:
1446 			src = "builtin";
1447 			break;
1448 		case MODULE_SOURCE_FILESYS:
1449 			src = "filesys";
1450 			break;
1451 		case MODULE_SOURCE_BOOT:
1452 			src = "boot";
1453 			break;
1454 		default:
1455 			src = "unknown";
1456 			break;
1457 		}
1458 		if (mod->mod_kobj == NULL) {
1459 			maddr = 0;
1460 			msize = 0;
1461 		} else if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0)
1462 			continue;
1463 		(*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name,
1464 		    (long)maddr, (long)msize, src);
1465 	}
1466 }
1467 #endif	/* DDB */
1468 
1469 static bool
1470 module_merge_dicts(prop_dictionary_t existing_dict,
1471 		   const prop_dictionary_t new_dict)
1472 {
1473 	prop_dictionary_keysym_t props_keysym;
1474 	prop_object_iterator_t props_iter;
1475 	prop_object_t props_obj;
1476 	const char *props_key;
1477 	bool error;
1478 
1479 	if (new_dict == NULL) {			/* nothing to merge */
1480 		return true;
1481 	}
1482 
1483 	error = false;
1484 	props_iter = prop_dictionary_iterator(new_dict);
1485 	if (props_iter == NULL) {
1486 		return false;
1487 	}
1488 
1489 	while ((props_obj = prop_object_iterator_next(props_iter)) != NULL) {
1490 		props_keysym = (prop_dictionary_keysym_t)props_obj;
1491 		props_key = prop_dictionary_keysym_cstring_nocopy(props_keysym);
1492 		props_obj = prop_dictionary_get_keysym(new_dict, props_keysym);
1493 		if ((props_obj == NULL) || !prop_dictionary_set(existing_dict,
1494 		    props_key, props_obj)) {
1495 			error = true;
1496 			goto out;
1497 		}
1498 	}
1499 	error = false;
1500 
1501 out:
1502 	prop_object_iterator_release(props_iter);
1503 
1504 	return !error;
1505 }
1506