2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1997 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/linker.h>
32 #include <sys/sysctl.h>
41 #define PATHCTL "kern.module_path"
44 * Check to see if the requested module is specified as a filename with no
45 * path. If so and if a file by the same name exists in the module path,
46 * warn the user that the module in the path will be used in preference.
49 path_check(const char *kldname, int quiet)
51 char *path, *tmppath, *element;
54 char kldpath[MAXPATHLEN];
55 size_t miblen, pathlen;
60 if (strchr(kldname, '/') != NULL)
62 if (strstr(kldname, ".ko") == NULL)
64 if (stat(kldname, &sb) != 0)
72 if (sysctlnametomib(PATHCTL, mib, &miblen) != 0)
73 err(1, "sysctlnametomib(%s)", PATHCTL);
74 if (sysctl(mib, miblen, NULL, &pathlen, NULL, 0) == -1)
75 err(1, "getting path: sysctl(%s) - size only", PATHCTL);
76 path = malloc(pathlen + 1);
78 err(1, "allocating %lu bytes for the path",
79 (unsigned long)pathlen + 1);
80 if (sysctl(mib, miblen, path, &pathlen, NULL, 0) == -1)
81 err(1, "getting path: sysctl(%s)", PATHCTL);
84 while ((element = strsep(&tmppath, ";")) != NULL) {
85 strlcpy(kldpath, element, MAXPATHLEN);
86 if (kldpath[strlen(kldpath) - 1] != '/') {
87 strlcat(kldpath, "/", MAXPATHLEN);
89 strlcat(kldpath, kldname, MAXPATHLEN);
91 if (stat(kldpath, &sb) == -1)
96 if (sb.st_dev != dev || sb.st_ino != ino) {
98 warnx("%s will be loaded from %s, not the "
99 "current directory", kldname, element);
101 } else if (sb.st_dev == dev && sb.st_ino == ino)
109 warnx("%s is not in the module path", kldname);
120 fprintf(stderr, "usage: kldload [-nqv] file ...\n");
125 main(int argc, char** argv)
139 while ((c = getopt(argc, argv, "nqv")) != -1) {
162 while (argc-- != 0) {
163 if (path_check(argv[0], quiet) == 0) {
164 fileid = kldload(argv[0]);
166 if (check_loaded != 0 && errno == EEXIST) {
168 printf("%s is already "
169 "loaded\n", argv[0]);
174 warnx("can't load %s: module "
176 "in kernel", argv[0]);
179 warnx("an error occurred while "
180 "loading module %s. "
181 "Please check dmesg(8) for "
182 "more details.", argv[0]);
185 warn("can't load %s", argv[0]);
193 printf("Loaded %s, id=%d\n", argv[0],
201 return (errors ? 1 : 0);