Home | History | Annotate | Line # | Download | only in kern
kern_module_vfs.c revision 1.3.2.3
      1  1.3.2.3  yamt /*	$NetBSD: kern_module_vfs.c,v 1.3.2.3 2010/08/11 22:54:39 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.3  yamt __KERNEL_RCSID(0, "$NetBSD: kern_module_vfs.c,v 1.3.2.3 2010/08/11 22:54:39 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.3  yamt void
     56  1.3.2.3  yamt module_load_vfs_init(void)
     57  1.3.2.3  yamt {
     58  1.3.2.3  yamt 	module_load_vfs_vec = module_load_vfs;
     59  1.3.2.3  yamt }
     60  1.3.2.3  yamt 
     61  1.3.2.2  yamt int
     62  1.3.2.2  yamt module_load_vfs(const char *name, int flags, bool autoload,
     63  1.3.2.2  yamt 	module_t *mod, prop_dictionary_t *filedictp)
     64  1.3.2.2  yamt {
     65  1.3.2.2  yamt 	char *path;
     66  1.3.2.2  yamt 	bool nochroot;
     67  1.3.2.2  yamt 	int error;
     68  1.3.2.2  yamt 
     69  1.3.2.2  yamt 	nochroot = false;
     70  1.3.2.2  yamt 	error = 0;
     71  1.3.2.2  yamt 	path = NULL;
     72  1.3.2.2  yamt 	if (filedictp)
     73  1.3.2.2  yamt 		*filedictp = NULL;
     74  1.3.2.2  yamt 	path = PNBUF_GET();
     75  1.3.2.2  yamt 
     76  1.3.2.2  yamt 	if (!autoload) {
     77  1.3.2.2  yamt 		nochroot = false;
     78  1.3.2.2  yamt 		snprintf(path, MAXPATHLEN, "%s", 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 (autoload || (error == ENOENT)) {
     82  1.3.2.2  yamt 		nochroot = true;
     83  1.3.2.2  yamt 		snprintf(path, MAXPATHLEN, "%s/%s/%s.kmod",
     84  1.3.2.2  yamt 		    module_base, name, name);
     85  1.3.2.2  yamt 		error = kobj_load_vfs(&mod->mod_kobj, path, nochroot);
     86  1.3.2.2  yamt 	}
     87  1.3.2.2  yamt 	if (error != 0) {
     88  1.3.2.2  yamt 		PNBUF_PUT(path);
     89  1.3.2.2  yamt 		if (autoload) {
     90  1.3.2.2  yamt 			module_print("Cannot load kernel object `%s'"
     91  1.3.2.2  yamt 			    " error=%d", name, error);
     92  1.3.2.2  yamt 		} else {
     93  1.3.2.2  yamt 			module_error("Cannot load kernel object `%s'"
     94  1.3.2.2  yamt 			    " error=%d", name, error);
     95  1.3.2.2  yamt 		}
     96  1.3.2.2  yamt 		return error;
     97  1.3.2.2  yamt 	}
     98  1.3.2.2  yamt 
     99  1.3.2.2  yamt 	/*
    100  1.3.2.2  yamt 	 * Load and process <module>.prop if it exists.
    101  1.3.2.2  yamt 	 */
    102  1.3.2.2  yamt 	if ((flags & MODCTL_NO_PROP) == 0 && filedictp) {
    103  1.3.2.2  yamt 		error = module_load_plist_vfs(path, nochroot, filedictp);
    104  1.3.2.2  yamt 		if (error != 0) {
    105  1.3.2.2  yamt 			module_print("plist load returned error %d for `%s'",
    106  1.3.2.2  yamt 			    error, path);
    107  1.3.2.2  yamt 			if (error != ENOENT)
    108  1.3.2.2  yamt 				goto fail;
    109  1.3.2.2  yamt 		}
    110  1.3.2.2  yamt 	}
    111  1.3.2.2  yamt 
    112  1.3.2.2  yamt 	PNBUF_PUT(path);
    113  1.3.2.2  yamt 	return 0;
    114  1.3.2.2  yamt 
    115  1.3.2.2  yamt  fail:
    116  1.3.2.2  yamt 	kobj_unload(mod->mod_kobj);
    117  1.3.2.2  yamt 	PNBUF_PUT(path);
    118  1.3.2.2  yamt 	return error;
    119  1.3.2.2  yamt }
    120  1.3.2.2  yamt 
    121  1.3.2.2  yamt /*
    122  1.3.2.2  yamt  * module_load_plist_vfs:
    123  1.3.2.2  yamt  *
    124  1.3.2.2  yamt  *	Load a plist located in the file system into memory.
    125  1.3.2.2  yamt  */
    126  1.3.2.2  yamt static int
    127  1.3.2.2  yamt module_load_plist_vfs(const char *modpath, const bool nochroot,
    128  1.3.2.2  yamt 		       prop_dictionary_t *filedictp)
    129  1.3.2.2  yamt {
    130  1.3.2.2  yamt 	struct nameidata nd;
    131  1.3.2.2  yamt 	struct stat sb;
    132  1.3.2.2  yamt 	void *base;
    133  1.3.2.2  yamt 	char *proppath;
    134  1.3.2.2  yamt 	const size_t plistsize = 8192;
    135  1.3.2.2  yamt 	size_t resid;
    136  1.3.2.2  yamt 	int error, pathlen;
    137  1.3.2.2  yamt 
    138  1.3.2.2  yamt 	KASSERT(filedictp != NULL);
    139  1.3.2.2  yamt 	base = NULL;
    140  1.3.2.2  yamt 
    141  1.3.2.2  yamt 	proppath = PNBUF_GET();
    142  1.3.2.2  yamt 	strcpy(proppath, modpath);
    143  1.3.2.2  yamt 	pathlen = strlen(proppath);
    144  1.3.2.2  yamt 	if ((pathlen >= 5) && (strcmp(&proppath[pathlen - 5], ".kmod") == 0)) {
    145  1.3.2.2  yamt 		strcpy(&proppath[pathlen - 5], ".prop");
    146  1.3.2.2  yamt 	} else if (pathlen < MAXPATHLEN - 5) {
    147  1.3.2.2  yamt 			strcat(proppath, ".prop");
    148  1.3.2.2  yamt 	} else {
    149  1.3.2.2  yamt 		error = ENOENT;
    150  1.3.2.2  yamt 		goto out1;
    151  1.3.2.2  yamt 	}
    152  1.3.2.2  yamt 
    153  1.3.2.2  yamt 	NDINIT(&nd, LOOKUP, FOLLOW | (nochroot ? NOCHROOT : 0),
    154  1.3.2.2  yamt 	    UIO_SYSSPACE, proppath);
    155  1.3.2.2  yamt 
    156  1.3.2.3  yamt 	error = vn_open(&nd, FREAD, 0);
    157  1.3.2.3  yamt  	if (error != 0) {
    158  1.3.2.3  yamt 	 	goto out1;
    159  1.3.2.2  yamt 	}
    160  1.3.2.2  yamt 
    161  1.3.2.2  yamt 	error = vn_stat(nd.ni_vp, &sb);
    162  1.3.2.2  yamt 	if (error != 0) {
    163  1.3.2.3  yamt 		goto out;
    164  1.3.2.2  yamt 	}
    165  1.3.2.2  yamt 	if (sb.st_size >= (plistsize - 1)) {	/* leave space for term \0 */
    166  1.3.2.2  yamt 		error = EFBIG;
    167  1.3.2.3  yamt 		goto out;
    168  1.3.2.2  yamt 	}
    169  1.3.2.2  yamt 
    170  1.3.2.2  yamt 	base = kmem_alloc(plistsize, KM_SLEEP);
    171  1.3.2.2  yamt 	if (base == NULL) {
    172  1.3.2.2  yamt 		error = ENOMEM;
    173  1.3.2.2  yamt 		goto out;
    174  1.3.2.2  yamt 	}
    175  1.3.2.2  yamt 
    176  1.3.2.2  yamt 	error = vn_rdwr(UIO_READ, nd.ni_vp, base, sb.st_size, 0,
    177  1.3.2.2  yamt 	    UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid, curlwp);
    178  1.3.2.2  yamt 	*((uint8_t *)base + sb.st_size) = '\0';
    179  1.3.2.2  yamt 	if (error == 0 && resid != 0) {
    180  1.3.2.2  yamt 		error = EFBIG;
    181  1.3.2.2  yamt 	}
    182  1.3.2.2  yamt 	if (error != 0) {
    183  1.3.2.2  yamt 		kmem_free(base, plistsize);
    184  1.3.2.2  yamt 		base = NULL;
    185  1.3.2.2  yamt 		goto out;
    186  1.3.2.2  yamt 	}
    187  1.3.2.2  yamt 
    188  1.3.2.2  yamt 	*filedictp = prop_dictionary_internalize(base);
    189  1.3.2.2  yamt 	if (*filedictp == NULL) {
    190  1.3.2.2  yamt 		error = EINVAL;
    191  1.3.2.2  yamt 	}
    192  1.3.2.2  yamt 	kmem_free(base, plistsize);
    193  1.3.2.2  yamt 	base = NULL;
    194  1.3.2.2  yamt 	KASSERT(error == 0);
    195  1.3.2.2  yamt 
    196  1.3.2.2  yamt out:
    197  1.3.2.3  yamt 	VOP_UNLOCK(nd.ni_vp);
    198  1.3.2.2  yamt 	vn_close(nd.ni_vp, FREAD, kauth_cred_get());
    199  1.3.2.2  yamt 
    200  1.3.2.2  yamt out1:
    201  1.3.2.2  yamt 	PNBUF_PUT(proppath);
    202  1.3.2.2  yamt 	return error;
    203  1.3.2.2  yamt }
    204