1997-05-07 18:19:54 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
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"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
char *path, *tmppath, *element;
|
|
|
|
struct stat sb;
|
2017-02-05 05:36:51 +00:00
|
|
|
int mib[5];
|
|
|
|
char kldpath[MAXPATHLEN];
|
|
|
|
size_t miblen, pathlen;
|
2009-06-04 23:43:08 +00:00
|
|
|
dev_t dev;
|
|
|
|
ino_t ino;
|
2017-02-05 05:36:51 +00:00
|
|
|
int found;
|
2009-06-04 23:43:08 +00:00
|
|
|
|
2017-02-05 05:36:51 +00:00
|
|
|
if (strchr(kldname, '/') != NULL)
|
2009-06-04 23:43:08 +00:00
|
|
|
return (0);
|
2017-02-05 05:36:51 +00:00
|
|
|
if (strstr(kldname, ".ko") == NULL)
|
2009-06-04 23:43:08 +00:00
|
|
|
return (0);
|
2017-02-05 05:36:51 +00:00
|
|
|
if (stat(kldname, &sb) != 0)
|
2009-06-04 23:43:08 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
found = 0;
|
|
|
|
dev = sb.st_dev;
|
|
|
|
ino = sb.st_ino;
|
|
|
|
|
2016-04-19 04:52:51 +00:00
|
|
|
miblen = nitems(mib);
|
2017-02-05 05:36:51 +00:00
|
|
|
if (sysctlnametomib(PATHCTL, mib, &miblen) != 0)
|
2009-06-04 23:43:08 +00:00
|
|
|
err(1, "sysctlnametomib(%s)", PATHCTL);
|
2017-02-05 05:36:51 +00:00
|
|
|
if (sysctl(mib, miblen, NULL, &pathlen, NULL, 0) == -1)
|
2009-06-04 23:43:08 +00:00
|
|
|
err(1, "getting path: sysctl(%s) - size only", PATHCTL);
|
|
|
|
path = malloc(pathlen + 1);
|
2017-02-05 05:36:51 +00:00
|
|
|
if (path == NULL)
|
2009-06-04 23:43:08 +00:00
|
|
|
err(1, "allocating %lu bytes for the path",
|
|
|
|
(unsigned long)pathlen + 1);
|
2017-02-05 05:36:51 +00:00
|
|
|
if (sysctl(mib, miblen, path, &pathlen, NULL, 0) == -1)
|
2009-06-04 23:43:08 +00:00
|
|
|
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);
|
2017-02-05 05:36:51 +00:00
|
|
|
|
|
|
|
if (stat(kldpath, &sb) == -1)
|
2009-06-04 23:43:08 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
found = 1;
|
|
|
|
|
|
|
|
if (sb.st_dev != dev || sb.st_ino != ino) {
|
2017-02-05 05:36:51 +00:00
|
|
|
if (!quiet)
|
2009-06-04 23:43:08 +00:00
|
|
|
warnx("%s will be loaded from %s, not the "
|
|
|
|
"current directory", kldname, element);
|
|
|
|
break;
|
2017-02-05 05:36:51 +00:00
|
|
|
} else if (sb.st_dev == dev && sb.st_ino == ino)
|
2009-06-04 23:43:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(path);
|
2017-02-05 05:36:51 +00:00
|
|
|
|
2009-06-04 23:43:08 +00:00
|
|
|
if (!found) {
|
2017-02-05 05:36:51 +00:00
|
|
|
if (!quiet)
|
2009-06-04 23:43:08 +00:00
|
|
|
warnx("%s is not in the module path", kldname);
|
|
|
|
return (-1);
|
|
|
|
}
|
2017-02-05 05:36:51 +00:00
|
|
|
|
2009-06-04 23:43:08 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1997-10-21 09:59:26 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
1997-05-07 18:19:54 +00:00
|
|
|
{
|
2017-02-05 05:36:51 +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;
|
2017-02-05 05:36:51 +00:00
|
|
|
int check_loaded;
|
2009-06-04 23:31:05 +00:00
|
|
|
int errors;
|
|
|
|
int fileid;
|
2009-06-04 23:43:08 +00:00
|
|
|
int quiet;
|
2017-02-05 05:36:51 +00:00
|
|
|
int verbose;
|
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;
|
2017-02-05 05:36:51 +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 "
|
2017-02-18 18:57:29 +00:00
|
|
|
"loading module %s. "
|
2014-01-13 17:14:10 +00:00
|
|
|
"Please check dmesg(8) for "
|
2017-02-18 18:57:29 +00:00
|
|
|
"more details.", argv[0]);
|
2014-01-13 16:23:09 +00:00
|
|
|
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
|
|
|
}
|
2017-02-05 05:36:51 +00:00
|
|
|
} else
|
2009-06-04 23:43:08 +00:00
|
|
|
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
|
|
|
}
|