1 /* $NetBSD: sys_module.c,v 1.7 2008/05/20 19:20:38 ad 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.7 2008/05/20 19:20:38 ad 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(void *arg) 48 { 49 modctl_load_t *ml = (modctl_load_t *)arg; 50 51 char *path; 52 char *props; 53 int error; 54 prop_dictionary_t dict; 55 size_t propslen; 56 57 if ((ml->ml_props != NULL && ml->ml_propslen == 0) || 58 (ml->ml_props == NULL && ml->ml_propslen > 0)) { 59 error = EINVAL; 60 goto out1; 61 } 62 63 path = PNBUF_GET(); 64 error = copyinstr(ml->ml_filename, path, MAXPATHLEN, NULL); 65 if (error != 0) 66 goto out2; 67 68 propslen = ml->ml_propslen + 1; 69 props = (char *)kmem_alloc(propslen, KM_SLEEP); 70 if (props == NULL) { 71 error = ENOMEM; 72 goto out2; 73 } 74 75 error = copyinstr(ml->ml_props, props, propslen, NULL); 76 if (error != 0) 77 goto out3; 78 79 dict = prop_dictionary_internalize(props); 80 if (dict == NULL) { 81 error = EINVAL; 82 goto out3; 83 } 84 85 error = module_load(path, ml->ml_flags, dict, MODULE_CLASS_ANY, false); 86 87 prop_object_release(dict); 88 89 out3: 90 kmem_free(props, propslen); 91 out2: 92 PNBUF_PUT(path); 93 out1: 94 95 return error; 96 } 97 98 int 99 sys_modctl(struct lwp *l, const struct sys_modctl_args *uap, 100 register_t *retval) 101 { 102 /* { 103 syscallarg(int) cmd; 104 syscallarg(void *) arg; 105 } */ 106 char buf[MAXMODNAME]; 107 size_t mslen; 108 module_t *mod; 109 modinfo_t *mi; 110 modstat_t *ms, *mso; 111 vaddr_t addr; 112 size_t size; 113 struct iovec iov; 114 int error; 115 void *arg; 116 117 arg = SCARG(uap, arg); 118 119 switch (SCARG(uap, cmd)) { 120 case MODCTL_LOAD: 121 error = handle_modctl_load(arg); 122 break; 123 124 case MODCTL_UNLOAD: 125 error = copyinstr(arg, buf, sizeof(buf), NULL); 126 if (error == 0) { 127 error = module_unload(buf); 128 } 129 break; 130 131 case MODCTL_STAT: 132 error = copyin(arg, &iov, sizeof(iov)); 133 if (error != 0) { 134 break; 135 } 136 mutex_enter(&module_lock); 137 mslen = (module_count + 1) * sizeof(modstat_t); 138 mso = kmem_zalloc(mslen, KM_SLEEP); 139 if (mso == NULL) { 140 mutex_exit(&module_lock); 141 return ENOMEM; 142 } 143 ms = mso; 144 TAILQ_FOREACH(mod, &module_list, mod_chain) { 145 mi = mod->mod_info; 146 strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name)); 147 if (mi->mi_required != NULL) { 148 strlcpy(ms->ms_required, mi->mi_required, 149 sizeof(ms->ms_required)); 150 } 151 if (mod->mod_kobj != NULL) { 152 kobj_stat(mod->mod_kobj, &addr, &size); 153 ms->ms_addr = addr; 154 ms->ms_size = size; 155 } 156 ms->ms_class = mi->mi_class; 157 ms->ms_refcnt = mod->mod_refcnt; 158 ms->ms_source = mod->mod_source; 159 ms++; 160 } 161 mutex_exit(&module_lock); 162 error = copyout(mso, iov.iov_base, 163 min(mslen - sizeof(modstat_t), iov.iov_len)); 164 kmem_free(mso, mslen); 165 if (error == 0) { 166 iov.iov_len = mslen - sizeof(modstat_t); 167 error = copyout(&iov, arg, sizeof(iov)); 168 } 169 break; 170 171 default: 172 error = EINVAL; 173 break; 174 } 175 176 return error; 177 } 178