1 /* $NetBSD: kern_module.c,v 1.53 2009/11/05 14:09:14 pooka Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software developed for The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
44 #include <sys/module.h>
45 #include <sys/namei.h>
48 #include <sys/vnode.h>
50 #include <prop/proplib.h>
52 static int module_load_plist_file(const char *, const bool,
56 module_load_vfs(const char *name
, int flags
, bool autoload
,
57 module_t
*mod
, prop_dictionary_t
*filedictp
)
72 snprintf(path
, MAXPATHLEN
, "%s", name
);
73 error
= kobj_load_file(&mod
->mod_kobj
, path
, nochroot
);
75 if (autoload
|| (error
== ENOENT
)) {
77 snprintf(path
, MAXPATHLEN
, "%s/%s/%s.kmod",
78 module_base
, name
, name
);
79 error
= kobj_load_file(&mod
->mod_kobj
, path
, nochroot
);
84 module_print("Cannot load kernel object `%s'"
85 " error=%d", name
, error
);
87 module_error("Cannot load kernel object `%s'"
88 " error=%d", name
, 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
);
99 module_print("plist load returned error %d for `%s'",
110 kobj_unload(mod
->mod_kobj
);
116 * module_load_plist_file:
118 * Load a plist located in the file system into memory.
121 module_load_plist_file(const char *modpath
, const bool nochroot
,
122 prop_dictionary_t
*filedictp
)
128 const size_t plistsize
= 8192;
132 KASSERT(filedictp
!= 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");
147 NDINIT(&nd
, LOOKUP
, FOLLOW
| (nochroot
? NOCHROOT
: 0),
148 UIO_SYSSPACE
, proppath
);
155 error
= vn_stat(nd
.ni_vp
, &sb
);
156 if (sb
.st_size
>= (plistsize
- 1)) { /* leave space for term \0 */
163 error
= vn_open(&nd
, FREAD
, 0);
168 base
= kmem_alloc(plistsize
, KM_SLEEP
);
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) {
181 kmem_free(base
, plistsize
);
186 *filedictp
= prop_dictionary_internalize(base
);
187 if (*filedictp
== NULL
) {
190 kmem_free(base
, plistsize
);
195 VOP_UNLOCK(nd
.ni_vp
, 0);
196 vn_close(nd
.ni_vp
, FREAD
, kauth_cred_get());