kern_module_vfs.c revision 1.8 1 1.8 jnemeth /* $NetBSD: kern_module_vfs.c,v 1.8 2010/11/18 09:50:47 jnemeth Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.1 pooka * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 pooka * All rights reserved.
6 1.1 pooka *
7 1.1 pooka * This code is derived from software developed for The NetBSD Foundation
8 1.1 pooka * by Andrew Doran.
9 1.1 pooka *
10 1.1 pooka * Redistribution and use in source and binary forms, with or without
11 1.1 pooka * modification, are permitted provided that the following conditions
12 1.1 pooka * are met:
13 1.1 pooka * 1. Redistributions of source code must retain the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer.
15 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 pooka * notice, this list of conditions and the following disclaimer in the
17 1.1 pooka * documentation and/or other materials provided with the distribution.
18 1.1 pooka *
19 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 pooka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 pooka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 pooka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 pooka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 pooka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 pooka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 pooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 pooka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 pooka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 pooka * POSSIBILITY OF SUCH DAMAGE.
30 1.1 pooka */
31 1.1 pooka
32 1.1 pooka /*
33 1.1 pooka * Kernel module file system interaction.
34 1.1 pooka */
35 1.1 pooka
36 1.1 pooka #include <sys/cdefs.h>
37 1.8 jnemeth __KERNEL_RCSID(0, "$NetBSD: kern_module_vfs.c,v 1.8 2010/11/18 09:50:47 jnemeth Exp $");
38 1.1 pooka
39 1.1 pooka #define _MODULE_INTERNAL
40 1.1 pooka #include <sys/param.h>
41 1.1 pooka #include <sys/fcntl.h>
42 1.1 pooka #include <sys/kmem.h>
43 1.1 pooka #include <sys/kobj.h>
44 1.1 pooka #include <sys/module.h>
45 1.1 pooka #include <sys/namei.h>
46 1.1 pooka #include <sys/pool.h>
47 1.1 pooka #include <sys/stat.h>
48 1.1 pooka #include <sys/vnode.h>
49 1.1 pooka
50 1.1 pooka #include <prop/proplib.h>
51 1.1 pooka
52 1.2 pooka static int module_load_plist_vfs(const char *, const bool,
53 1.2 pooka prop_dictionary_t *);
54 1.5 pgoyette
55 1.5 pgoyette void
56 1.5 pgoyette module_load_vfs_init(void)
57 1.5 pgoyette {
58 1.5 pgoyette module_load_vfs_vec = module_load_vfs;
59 1.5 pgoyette }
60 1.1 pooka
61 1.1 pooka int
62 1.1 pooka module_load_vfs(const char *name, int flags, bool autoload,
63 1.1 pooka module_t *mod, prop_dictionary_t *filedictp)
64 1.1 pooka {
65 1.1 pooka char *path;
66 1.1 pooka bool nochroot;
67 1.1 pooka int error;
68 1.8 jnemeth prop_bool_t noload;
69 1.8 jnemeth prop_dictionary_t moduledict;
70 1.1 pooka
71 1.1 pooka nochroot = false;
72 1.1 pooka error = 0;
73 1.1 pooka path = NULL;
74 1.8 jnemeth moduledict = NULL;
75 1.1 pooka if (filedictp)
76 1.1 pooka *filedictp = NULL;
77 1.1 pooka path = PNBUF_GET();
78 1.1 pooka
79 1.1 pooka if (!autoload) {
80 1.1 pooka nochroot = false;
81 1.1 pooka snprintf(path, MAXPATHLEN, "%s", name);
82 1.2 pooka error = kobj_load_vfs(&mod->mod_kobj, path, nochroot);
83 1.1 pooka }
84 1.1 pooka if (autoload || (error == ENOENT)) {
85 1.1 pooka nochroot = true;
86 1.1 pooka snprintf(path, MAXPATHLEN, "%s/%s/%s.kmod",
87 1.1 pooka module_base, name, name);
88 1.2 pooka error = kobj_load_vfs(&mod->mod_kobj, path, nochroot);
89 1.1 pooka }
90 1.1 pooka if (error != 0) {
91 1.1 pooka PNBUF_PUT(path);
92 1.1 pooka if (autoload) {
93 1.1 pooka module_print("Cannot load kernel object `%s'"
94 1.1 pooka " error=%d", name, error);
95 1.1 pooka } else {
96 1.1 pooka module_error("Cannot load kernel object `%s'"
97 1.1 pooka " error=%d", name, error);
98 1.1 pooka }
99 1.1 pooka return error;
100 1.1 pooka }
101 1.1 pooka
102 1.1 pooka /*
103 1.1 pooka * Load and process <module>.prop if it exists.
104 1.1 pooka */
105 1.8 jnemeth if (((flags & MODCTL_NO_PROP) == 0 && filedictp) || autoload) {
106 1.8 jnemeth error = module_load_plist_vfs(path, nochroot, &moduledict);
107 1.1 pooka if (error != 0) {
108 1.1 pooka module_print("plist load returned error %d for `%s'",
109 1.1 pooka error, path);
110 1.1 pooka if (error != ENOENT)
111 1.1 pooka goto fail;
112 1.8 jnemeth } else if (autoload) {
113 1.8 jnemeth noload = prop_dictionary_get(moduledict, "noautoload");
114 1.8 jnemeth if (noload != NULL && prop_bool_true(noload)) {
115 1.8 jnemeth module_error("autoloading is disallowed for %s",
116 1.8 jnemeth path);
117 1.8 jnemeth error = EPERM;
118 1.8 jnemeth goto fail;
119 1.8 jnemeth }
120 1.8 jnemeth }
121 1.8 jnemeth if (error == 0) { /* can get here if error == ENOENT */
122 1.8 jnemeth if ((flags & MODCTL_NO_PROP) == 0 && filedictp)
123 1.8 jnemeth *filedictp = moduledict;
124 1.8 jnemeth else
125 1.8 jnemeth prop_object_release(moduledict);
126 1.1 pooka }
127 1.1 pooka }
128 1.1 pooka
129 1.1 pooka PNBUF_PUT(path);
130 1.1 pooka return 0;
131 1.1 pooka
132 1.1 pooka fail:
133 1.1 pooka kobj_unload(mod->mod_kobj);
134 1.1 pooka PNBUF_PUT(path);
135 1.1 pooka return error;
136 1.1 pooka }
137 1.1 pooka
138 1.1 pooka /*
139 1.2 pooka * module_load_plist_vfs:
140 1.1 pooka *
141 1.1 pooka * Load a plist located in the file system into memory.
142 1.1 pooka */
143 1.1 pooka static int
144 1.2 pooka module_load_plist_vfs(const char *modpath, const bool nochroot,
145 1.1 pooka prop_dictionary_t *filedictp)
146 1.1 pooka {
147 1.1 pooka struct nameidata nd;
148 1.1 pooka struct stat sb;
149 1.1 pooka void *base;
150 1.1 pooka char *proppath;
151 1.1 pooka const size_t plistsize = 8192;
152 1.1 pooka size_t resid;
153 1.1 pooka int error, pathlen;
154 1.1 pooka
155 1.1 pooka KASSERT(filedictp != NULL);
156 1.1 pooka base = NULL;
157 1.1 pooka
158 1.1 pooka proppath = PNBUF_GET();
159 1.1 pooka strcpy(proppath, modpath);
160 1.1 pooka pathlen = strlen(proppath);
161 1.1 pooka if ((pathlen >= 5) && (strcmp(&proppath[pathlen - 5], ".kmod") == 0)) {
162 1.1 pooka strcpy(&proppath[pathlen - 5], ".prop");
163 1.1 pooka } else if (pathlen < MAXPATHLEN - 5) {
164 1.1 pooka strcat(proppath, ".prop");
165 1.1 pooka } else {
166 1.1 pooka error = ENOENT;
167 1.1 pooka goto out1;
168 1.1 pooka }
169 1.1 pooka
170 1.1 pooka NDINIT(&nd, LOOKUP, FOLLOW | (nochroot ? NOCHROOT : 0),
171 1.1 pooka UIO_SYSSPACE, proppath);
172 1.1 pooka
173 1.4 jnemeth error = vn_open(&nd, FREAD, 0);
174 1.4 jnemeth if (error != 0) {
175 1.4 jnemeth goto out1;
176 1.1 pooka }
177 1.1 pooka
178 1.1 pooka error = vn_stat(nd.ni_vp, &sb);
179 1.3 dholland if (error != 0) {
180 1.4 jnemeth goto out;
181 1.3 dholland }
182 1.1 pooka if (sb.st_size >= (plistsize - 1)) { /* leave space for term \0 */
183 1.1 pooka error = EFBIG;
184 1.4 jnemeth goto out;
185 1.1 pooka }
186 1.1 pooka
187 1.1 pooka base = kmem_alloc(plistsize, KM_SLEEP);
188 1.1 pooka if (base == NULL) {
189 1.1 pooka error = ENOMEM;
190 1.1 pooka goto out;
191 1.1 pooka }
192 1.1 pooka
193 1.1 pooka error = vn_rdwr(UIO_READ, nd.ni_vp, base, sb.st_size, 0,
194 1.1 pooka UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid, curlwp);
195 1.1 pooka *((uint8_t *)base + sb.st_size) = '\0';
196 1.1 pooka if (error == 0 && resid != 0) {
197 1.1 pooka error = EFBIG;
198 1.1 pooka }
199 1.1 pooka if (error != 0) {
200 1.1 pooka kmem_free(base, plistsize);
201 1.1 pooka base = NULL;
202 1.1 pooka goto out;
203 1.1 pooka }
204 1.1 pooka
205 1.1 pooka *filedictp = prop_dictionary_internalize(base);
206 1.1 pooka if (*filedictp == NULL) {
207 1.1 pooka error = EINVAL;
208 1.1 pooka }
209 1.1 pooka kmem_free(base, plistsize);
210 1.1 pooka base = NULL;
211 1.1 pooka KASSERT(error == 0);
212 1.1 pooka
213 1.1 pooka out:
214 1.7 hannken VOP_UNLOCK(nd.ni_vp);
215 1.1 pooka vn_close(nd.ni_vp, FREAD, kauth_cred_get());
216 1.1 pooka
217 1.1 pooka out1:
218 1.1 pooka PNBUF_PUT(proppath);
219 1.1 pooka return error;
220 1.1 pooka }
221