1997-05-07 18:19:54 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1997 Doug Rabson
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-05-03 18:41:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1998-01-05 07:07:44 +00:00
|
|
|
|
2009-06-04 23:43:08 +00:00
|
|
|
#include <sys/types.h>
|
2009-06-04 23:31:05 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/linker.h>
|
2009-06-04 23:43:08 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/stat.h>
|
1997-10-19 11:15:45 +00:00
|
|
|
#include <err.h>
|
1997-05-07 18:19:54 +00:00
|
|
|
#include <stdio.h>
|
2001-06-24 23:04:23 +00:00
|
|
|
#include <stdlib.h>
|
2009-06-04 23:43:08 +00:00
|
|
|
#include <string.h>
|
1997-05-07 18:19:54 +00:00
|
|
|
#include <unistd.h>
|
2012-03-18 09:45:43 +00:00
|
|
|
#include <errno.h>
|
2009-06-04 23:31:05 +00:00
|
|
|
|
2009-06-04 23:43:08 +00:00
|
|
|
#define PATHCTL "kern.module_path"
|
|
|
|
|
|
|
|
static int path_check(const char *, int);
|
2009-06-04 23:31:05 +00:00
|
|
|
static void usage(void);
|
1997-05-07 18:19:54 +00:00
|
|
|
|
2009-06-04 23:43:08 +00:00
|
|
|
/*
|
|
|
|
* Check to see if the requested module is specified as a filename with no
|
|
|
|
* path. If so and if a file by the same name exists in the module path,
|
|
|
|
* warn the user that the module in the path will be used in preference.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
path_check(const char *kldname, int quiet)
|
|
|
|
{
|
|
|
|
int mib[5], found;
|
|
|
|
size_t miblen, pathlen;
|
|
|
|
char kldpath[MAXPATHLEN];
|
|
|
|
char *path, *tmppath, *element;
|
|
|
|
struct stat sb;
|
|
|
|
dev_t dev;
|
|
|
|
ino_t ino;
|
|
|
|
|
|
|
|
if (strchr(kldname, '/') != NULL) {
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (strstr(kldname, ".ko") == NULL) {
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (stat(kldname, &sb) != 0) {
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
found = 0;
|
|
|
|
dev = sb.st_dev;
|
|
|
|
ino = sb.st_ino;
|
|
|
|
|
2016-04-19 04:52:51 +00:00
|
|
|
miblen = nitems(mib);
|
2009-06-04 23:43:08 +00:00
|
|
|
if (sysctlnametomib(PATHCTL, mib, &miblen) != 0) {
|
|
|
|
err(1, "sysctlnametomib(%s)", PATHCTL);
|
|
|
|
}
|
|
|
|
if (sysctl(mib, miblen, NULL, &pathlen, NULL, 0) == -1) {
|
|
|
|
err(1, "getting path: sysctl(%s) - size only", PATHCTL);
|
|
|
|
}
|
|
|
|
path = malloc(pathlen + 1);
|
|
|
|
if (path == NULL) {
|
|
|
|
err(1, "allocating %lu bytes for the path",
|
|
|
|
(unsigned long)pathlen + 1);
|
|
|
|
}
|
|
|
|
if (sysctl(mib, miblen, path, &pathlen, NULL, 0) == -1) {
|
|
|
|
err(1, "getting path: sysctl(%s)", PATHCTL);
|
|
|
|
}
|
|
|
|
tmppath = path;
|
|
|
|
|
|
|
|
while ((element = strsep(&tmppath, ";")) != NULL) {
|
|
|
|
strlcpy(kldpath, element, MAXPATHLEN);
|
|
|
|
if (kldpath[strlen(kldpath) - 1] != '/') {
|
|
|
|
strlcat(kldpath, "/", MAXPATHLEN);
|
|
|
|
}
|
|
|
|
strlcat(kldpath, kldname, MAXPATHLEN);
|
|
|
|
|
|
|
|
if (stat(kldpath, &sb) == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
found = 1;
|
|
|
|
|
|
|
|
if (sb.st_dev != dev || sb.st_ino != ino) {
|
|
|
|
if (!quiet) {
|
|
|
|
warnx("%s will be loaded from %s, not the "
|
|
|
|
"current directory", kldname, element);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else if (sb.st_dev == dev && sb.st_ino == ino) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(path);
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
if (!quiet) {
|
|
|
|
warnx("%s is not in the module path", kldname);
|
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1997-10-21 09:59:26 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
1997-05-07 18:19:54 +00:00
|
|
|
{
|
2012-03-18 09:45:43 +00:00
|
|
|
fprintf(stderr, "usage: kldload [-nqv] file ...\n");
|
2009-06-04 23:31:05 +00:00
|
|
|
exit(1);
|
1997-05-07 18:19:54 +00:00
|
|
|
}
|
|
|
|
|
1997-10-21 09:59:26 +00:00
|
|
|
int
|
|
|
|
main(int argc, char** argv)
|
1997-05-07 18:19:54 +00:00
|
|
|
{
|
2009-06-04 23:31:05 +00:00
|
|
|
int c;
|
|
|
|
int errors;
|
|
|
|
int fileid;
|
|
|
|
int verbose;
|
2009-06-04 23:43:08 +00:00
|
|
|
int quiet;
|
2012-03-18 09:45:43 +00:00
|
|
|
int check_loaded;
|
2002-10-08 09:57:03 +00:00
|
|
|
|
2009-06-04 23:31:05 +00:00
|
|
|
errors = 0;
|
|
|
|
verbose = 0;
|
2009-06-04 23:43:08 +00:00
|
|
|
quiet = 0;
|
2012-03-18 09:45:43 +00:00
|
|
|
check_loaded = 0;
|
2009-06-04 23:43:08 +00:00
|
|
|
|
2012-03-18 09:45:43 +00:00
|
|
|
while ((c = getopt(argc, argv, "nqv")) != -1) {
|
2009-06-04 23:31:05 +00:00
|
|
|
switch (c) {
|
2009-06-04 23:43:08 +00:00
|
|
|
case 'q':
|
|
|
|
quiet = 1;
|
|
|
|
verbose = 0;
|
|
|
|
break;
|
2009-06-04 23:31:05 +00:00
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
2009-06-04 23:43:08 +00:00
|
|
|
quiet = 0;
|
2009-06-04 23:31:05 +00:00
|
|
|
break;
|
2012-03-18 09:45:43 +00:00
|
|
|
case 'n':
|
|
|
|
check_loaded = 1;
|
|
|
|
break;
|
2009-06-04 23:31:05 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
1997-05-07 18:19:54 +00:00
|
|
|
}
|
2009-06-04 23:31:05 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1997-05-07 18:19:54 +00:00
|
|
|
|
2009-06-04 23:31:05 +00:00
|
|
|
if (argc == 0)
|
|
|
|
usage();
|
1997-05-07 18:19:54 +00:00
|
|
|
|
2009-06-04 23:31:05 +00:00
|
|
|
while (argc-- != 0) {
|
2009-06-04 23:43:08 +00:00
|
|
|
if (path_check(argv[0], quiet) == 0) {
|
|
|
|
fileid = kldload(argv[0]);
|
|
|
|
if (fileid < 0) {
|
2012-03-18 09:45:43 +00:00
|
|
|
if (check_loaded != 0 && errno == EEXIST) {
|
|
|
|
if (verbose)
|
|
|
|
printf("%s is already "
|
|
|
|
"loaded\n", argv[0]);
|
|
|
|
} else {
|
2014-01-13 16:23:09 +00:00
|
|
|
switch (errno) {
|
|
|
|
case EEXIST:
|
2014-01-09 15:34:23 +00:00
|
|
|
warnx("can't load %s: module "
|
|
|
|
"already loaded or "
|
|
|
|
"in kernel", argv[0]);
|
2014-01-13 16:23:09 +00:00
|
|
|
break;
|
|
|
|
case ENOEXEC:
|
2014-01-13 16:47:25 +00:00
|
|
|
warnx("an error occurred while "
|
2014-01-13 16:23:09 +00:00
|
|
|
"loading the module. "
|
2014-01-13 17:14:10 +00:00
|
|
|
"Please check dmesg(8) for "
|
2014-01-13 16:23:09 +00:00
|
|
|
"more details.");
|
|
|
|
break;
|
|
|
|
default:
|
2014-01-09 15:34:23 +00:00
|
|
|
warn("can't load %s", argv[0]);
|
2014-01-13 16:23:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-01-09 15:35:35 +00:00
|
|
|
errors++;
|
2012-03-18 09:45:43 +00:00
|
|
|
}
|
2009-06-04 23:43:08 +00:00
|
|
|
} else {
|
|
|
|
if (verbose)
|
|
|
|
printf("Loaded %s, id=%d\n", argv[0],
|
|
|
|
fileid);
|
2009-06-04 23:31:05 +00:00
|
|
|
}
|
2009-06-04 23:43:08 +00:00
|
|
|
} else {
|
|
|
|
errors++;
|
2009-06-04 23:31:05 +00:00
|
|
|
}
|
|
|
|
argv++;
|
|
|
|
}
|
1997-05-07 18:19:54 +00:00
|
|
|
|
2009-06-04 23:31:05 +00:00
|
|
|
return (errors ? 1 : 0);
|
1997-05-07 18:19:54 +00:00
|
|
|
}
|