1 /* $NetBSD: sys_module.c,v 1.9 2009/04/28 17:57:00 skrll Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 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. 15 * 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. 27 */ 28 29 /* 30 * System calls relating to loadable modules. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: sys_module.c,v 1.9 2009/04/28 17:57:00 skrll Exp $"); 35 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> 45 46 static int 47 handle_modctl_load(modctl_load_t *ml) 48 { 49 char *path; 50 char *props; 51 int error; 52 prop_dictionary_t dict; 53 size_t propslen; 54 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; 59 } 60 61 path = PNBUF_GET(); 62 error = copyinstr(ml->ml_filename, path, MAXPATHLEN, NULL); 63 if (error != 0) 64 goto out2; 65 66 propslen = ml->ml_propslen + 1; 67 props = (char *)kmem_alloc(propslen, KM_SLEEP); 68 if (props == NULL) { 69 error = ENOMEM; 70 goto out2; 71 } 72 73 error = copyinstr(ml->ml_props, props, propslen, NULL); 74 if (error != 0) 75 goto out3; 76 77 dict = prop_dictionary_internalize(props); 78 if (dict == NULL) { 79 error = EINVAL; 80 goto out3; 81 } 82 83 error = module_load(path, ml->ml_flags, dict, MODULE_CLASS_ANY); 84 85 prop_object_release(dict); 86 87 out3: 88 kmem_free(props, propslen); 89 out2: 90 PNBUF_PUT(path); 91 out1: 92 93 return error; 94 } 95 96 int 97 sys_modctl(struct lwp *l, const struct sys_modctl_args *uap, 98 register_t *retval) 99 { 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; 115 116 arg = SCARG(uap, arg); 117 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; 125 126 case MODCTL_UNLOAD: 127 error = copyinstr(arg, buf, sizeof(buf), NULL); 128 if (error == 0) { 129 error = module_unload(buf); 130 } 131 break; 132 133 case MODCTL_STAT: 134 error = copyin(arg, &iov, sizeof(iov)); 135 if (error != 0) { 136 break; 137 } 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; 144 } 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)); 152 } 153 if (mod->mod_kobj != NULL) { 154 kobj_stat(mod->mod_kobj, &addr, &size); 155 ms->ms_addr = addr; 156 ms->ms_size = size; 157 } 158 ms->ms_class = mi->mi_class; 159 ms->ms_refcnt = mod->mod_refcnt; 160 ms->ms_source = mod->mod_source; 161 ms++; 162 } 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)); 170 } 171 break; 172 173 default: 174 error = EINVAL; 175 break; 176 } 177 178 return error; 179 } 180