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