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