Mark up sysctl node with Li, like in sysctl(7).
[netbsd-mini2440.git] / sys / kern / sys_module.c
blob461e1cf25a383aae8b55849cb2989225af2833b4
1 /* $NetBSD: sys_module.c,v 1.8 2008/10/22 11:16:29 ad Exp $ */
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * System calls relating to loadable modules.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: sys_module.c,v 1.8 2008/10/22 11:16:29 ad Exp $");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 #include <sys/namei.h>
40 #include <sys/kmem.h>
41 #include <sys/kobj.h>
42 #include <sys/module.h>
43 #include <sys/syscall.h>
44 #include <sys/syscallargs.h>
46 static int
47 handle_modctl_load(modctl_load_t *ml)
49 char *path;
50 char *props;
51 int error;
52 prop_dictionary_t dict;
53 size_t propslen;
55 if ((ml->ml_props != NULL && ml->ml_propslen == 0) ||
56 (ml->ml_props == NULL && ml->ml_propslen > 0)) {
57 error = EINVAL;
58 goto out1;
61 path = PNBUF_GET();
62 error = copyinstr(ml->ml_filename, path, MAXPATHLEN, NULL);
63 if (error != 0)
64 goto out2;
66 propslen = ml->ml_propslen + 1;
67 props = (char *)kmem_alloc(propslen, KM_SLEEP);
68 if (props == NULL) {
69 error = ENOMEM;
70 goto out2;
73 error = copyinstr(ml->ml_props, props, propslen, NULL);
74 if (error != 0)
75 goto out3;
77 dict = prop_dictionary_internalize(props);
78 if (dict == NULL) {
79 error = EINVAL;
80 goto out3;
83 error = module_load(path, ml->ml_flags, dict, MODULE_CLASS_ANY);
85 prop_object_release(dict);
87 out3:
88 kmem_free(props, propslen);
89 out2:
90 PNBUF_PUT(path);
91 out1:
93 return error;
96 int
97 sys_modctl(struct lwp *l, const struct sys_modctl_args *uap,
98 register_t *retval)
100 /* {
101 syscallarg(int) cmd;
102 syscallarg(void *) arg;
103 } */
104 char buf[MAXMODNAME];
105 size_t mslen;
106 module_t *mod;
107 modinfo_t *mi;
108 modstat_t *ms, *mso;
109 vaddr_t addr;
110 size_t size;
111 struct iovec iov;
112 modctl_load_t ml;
113 int error;
114 void *arg;
116 arg = SCARG(uap, arg);
118 switch (SCARG(uap, cmd)) {
119 case MODCTL_LOAD:
120 error = copyin(arg, &ml, sizeof(ml));
121 if (error != 0)
122 break;
123 error = handle_modctl_load(&ml);
124 break;
126 case MODCTL_UNLOAD:
127 error = copyinstr(arg, buf, sizeof(buf), NULL);
128 if (error == 0) {
129 error = module_unload(buf);
131 break;
133 case MODCTL_STAT:
134 error = copyin(arg, &iov, sizeof(iov));
135 if (error != 0) {
136 break;
138 mutex_enter(&module_lock);
139 mslen = (module_count + 1) * sizeof(modstat_t);
140 mso = kmem_zalloc(mslen, KM_SLEEP);
141 if (mso == NULL) {
142 mutex_exit(&module_lock);
143 return ENOMEM;
145 ms = mso;
146 TAILQ_FOREACH(mod, &module_list, mod_chain) {
147 mi = mod->mod_info;
148 strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
149 if (mi->mi_required != NULL) {
150 strlcpy(ms->ms_required, mi->mi_required,
151 sizeof(ms->ms_required));
153 if (mod->mod_kobj != NULL) {
154 kobj_stat(mod->mod_kobj, &addr, &size);
155 ms->ms_addr = addr;
156 ms->ms_size = size;
158 ms->ms_class = mi->mi_class;
159 ms->ms_refcnt = mod->mod_refcnt;
160 ms->ms_source = mod->mod_source;
161 ms++;
163 mutex_exit(&module_lock);
164 error = copyout(mso, iov.iov_base,
165 min(mslen - sizeof(modstat_t), iov.iov_len));
166 kmem_free(mso, mslen);
167 if (error == 0) {
168 iov.iov_len = mslen - sizeof(modstat_t);
169 error = copyout(&iov, arg, sizeof(iov));
171 break;
173 default:
174 error = EINVAL;
175 break;
178 return error;