No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / sys_module.c
blob78e04f8c4367bd6146894f4de94628c5587aeb3d
1 /* $NetBSD: sys_module.c,v 1.9 2009/04/28 17:57:00 skrll 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.9 2009/04/28 17:57:00 skrll 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 if (ml->ml_props != NULL) {
67 propslen = ml->ml_propslen + 1;
68 props = (char *)kmem_alloc(propslen, KM_SLEEP);
69 if (props == NULL) {
70 error = ENOMEM;
71 goto out2;
74 error = copyinstr(ml->ml_props, props, propslen, NULL);
75 if (error != 0)
76 goto out3;
78 dict = prop_dictionary_internalize(props);
79 if (dict == NULL) {
80 error = EINVAL;
81 goto out3;
83 } else {
84 dict = NULL;
85 props = NULL;
88 error = module_load(path, ml->ml_flags, dict, MODULE_CLASS_ANY);
90 if (dict != NULL) {
91 prop_object_release(dict);
94 out3:
95 if (props != NULL) {
96 kmem_free(props, propslen);
98 out2:
99 PNBUF_PUT(path);
100 out1:
102 return error;
106 sys_modctl(struct lwp *l, const struct sys_modctl_args *uap,
107 register_t *retval)
109 /* {
110 syscallarg(int) cmd;
111 syscallarg(void *) arg;
112 } */
113 char buf[MAXMODNAME];
114 size_t mslen;
115 module_t *mod;
116 modinfo_t *mi;
117 modstat_t *ms, *mso;
118 vaddr_t addr;
119 size_t size;
120 struct iovec iov;
121 modctl_load_t ml;
122 int error;
123 void *arg;
125 arg = SCARG(uap, arg);
127 switch (SCARG(uap, cmd)) {
128 case MODCTL_LOAD:
129 error = copyin(arg, &ml, sizeof(ml));
130 if (error != 0)
131 break;
132 error = handle_modctl_load(&ml);
133 break;
135 case MODCTL_UNLOAD:
136 error = copyinstr(arg, buf, sizeof(buf), NULL);
137 if (error == 0) {
138 error = module_unload(buf);
140 break;
142 case MODCTL_STAT:
143 error = copyin(arg, &iov, sizeof(iov));
144 if (error != 0) {
145 break;
147 mutex_enter(&module_lock);
148 mslen = (module_count + 1) * sizeof(modstat_t);
149 mso = kmem_zalloc(mslen, KM_SLEEP);
150 if (mso == NULL) {
151 mutex_exit(&module_lock);
152 return ENOMEM;
154 ms = mso;
155 TAILQ_FOREACH(mod, &module_list, mod_chain) {
156 mi = mod->mod_info;
157 strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
158 if (mi->mi_required != NULL) {
159 strlcpy(ms->ms_required, mi->mi_required,
160 sizeof(ms->ms_required));
162 if (mod->mod_kobj != NULL) {
163 kobj_stat(mod->mod_kobj, &addr, &size);
164 ms->ms_addr = addr;
165 ms->ms_size = size;
167 ms->ms_class = mi->mi_class;
168 ms->ms_refcnt = mod->mod_refcnt;
169 ms->ms_source = mod->mod_source;
170 ms++;
172 mutex_exit(&module_lock);
173 error = copyout(mso, iov.iov_base,
174 min(mslen - sizeof(modstat_t), iov.iov_len));
175 kmem_free(mso, mslen);
176 if (error == 0) {
177 iov.iov_len = mslen - sizeof(modstat_t);
178 error = copyout(&iov, arg, sizeof(iov));
180 break;
182 default:
183 error = EINVAL;
184 break;
187 return error;