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