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