2 * Copyright (c) 2001 Peter Pentchev
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 static const char rcsid[] =
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/queue.h>
35 #include <sys/sysctl.h>
45 #if defined(__FreeBSD_version)
46 #if __FreeBSD_version < 500000
47 #define NEED_SLASHTERM
49 #else /* defined(__FreeBSD_version) */
51 #define NEED_SLASHTERM
52 #endif /* defined(__FreeBSD_version) */
54 /* the default sysctl name */
55 #define PATHCTL "kern.module_path"
57 /* queue structure for the module path broken down into components */
58 TAILQ_HEAD(pathhead, pathentry);
61 TAILQ_ENTRY(pathentry) next;
64 /* the Management Information Base entries for the search path sysctl */
67 /* the sysctl name, defaults to PATHCTL */
69 /* the sysctl value - the current module search path */
71 /* flag whether user actions require changing the sysctl value */
74 /* Top-level path management functions */
75 static void addpath(struct pathhead *, char *, int, int);
76 static void rempath(struct pathhead *, char *, int, int);
77 static void showpath(struct pathhead *);
79 /* Low-level path management functions */
80 static char *qstring(struct pathhead *);
82 /* sysctl-related functions */
83 static void getmib(void);
84 static void getpath(void);
85 static void parsepath(struct pathhead *, char *, int);
86 static void setpath(struct pathhead *);
88 static void usage(void);
90 /* Get the MIB entry for our sysctl */
95 /* have we already fetched it? */
99 miblen = sizeof(mib) / sizeof(mib[0]);
100 if (sysctlnametomib(pathctl, mib, &miblen) != 0)
101 err(1, "sysctlnametomib(%s)", pathctl);
104 /* Get the current module search path */
111 if (modpath != NULL) {
118 if (sysctl(mib, miblen, NULL, &sz, NULL, NULL) == -1)
119 err(1, "getting path: sysctl(%s) - size only", pathctl);
120 if ((path = malloc(sz + 1)) == NULL) {
122 err(1, "allocating %lu bytes for the path",
123 (unsigned long)sz+1);
125 if (sysctl(mib, miblen, path, &sz, NULL, NULL) == -1)
126 err(1, "getting path: sysctl(%s)", pathctl);
130 /* Set the module search path after changing it */
132 setpath(struct pathhead *pathq)
138 if ((newpath = qstring(pathq)) == NULL) {
140 err(1, "building path string");
142 if (sysctl(mib, miblen, NULL, NULL, newpath, strlen(newpath)+1) == -1)
143 err(1, "setting path: sysctl(%s)", pathctl);
150 /* Add/insert a new component to the module search path */
152 addpath(struct pathhead *pathq, char *path, int force, int insert)
154 struct pathentry *pe, *pskip;
155 char pathbuf[MAXPATHLEN+1];
157 static unsigned added = 0;
161 * If the path exists, use it; otherwise, take the user-specified
162 * path at face value - may be a removed directory.
164 if (realpath(path, pathbuf) == NULL)
165 strlcpy(pathbuf, path, sizeof(pathbuf));
167 len = strlen(pathbuf);
168 #ifdef NEED_SLASHTERM
169 /* slash-terminate, because the kernel linker said so. */
170 if ((len == 0) || (pathbuf[len-1] != '/')) {
171 if (len == sizeof(pathbuf) - 1)
172 errx(1, "path too long: %s", pathbuf);
175 #else /* NEED_SLASHTERM */
176 /* remove a terminating slash if present */
177 if ((len > 0) && (pathbuf[len-1] == '/'))
178 pathbuf[--len] = '\0';
179 #endif /* NEED_SLASHTERM */
181 /* is it already in there? */
182 TAILQ_FOREACH(pe, pathq, next)
183 if (!strcmp(pe->path, pathbuf))
188 errx(1, "already in the module search path: %s", pathbuf);
191 /* OK, allocate and add it. */
192 if (((pe = malloc(sizeof(*pe))) == NULL) ||
193 ((pe->path = strdup(pathbuf)) == NULL)) {
195 err(1, "allocating path component");
198 TAILQ_INSERT_TAIL(pathq, pe, next);
200 for (i = 0, pskip = TAILQ_FIRST(pathq); i < added; i++)
201 pskip = TAILQ_NEXT(pskip, next);
203 TAILQ_INSERT_BEFORE(pskip, pe, next);
205 TAILQ_INSERT_TAIL(pathq, pe, next);
211 /* Remove a path component from the module search path */
213 rempath(struct pathhead *pathq, char *path, int force, int insert __unused)
215 char pathbuf[MAXPATHLEN+1];
216 struct pathentry *pe;
219 /* same logic as in addpath() */
220 if (realpath(path, pathbuf) == NULL)
221 strlcpy(pathbuf, path, sizeof(pathbuf));
223 len = strlen(pathbuf);
224 #ifdef NEED_SLASHTERM
225 /* slash-terminate, because the kernel linker said so. */
226 if ((len == 0) || (pathbuf[len-1] != '/')) {
227 if (len == sizeof(pathbuf) - 1)
228 errx(1, "path too long: %s", pathbuf);
231 #else /* NEED_SLASHTERM */
232 /* remove a terminating slash if present */
233 if ((len > 0) && (pathbuf[len-1] == '/'))
234 pathbuf[--len] = '\0';
235 #endif /* NEED_SLASHTERM */
237 /* Is it in there? */
238 TAILQ_FOREACH(pe, pathq, next)
239 if (!strcmp(pe->path, pathbuf))
244 errx(1, "not in module search path: %s", pathbuf);
247 /* OK, remove it now.. */
248 TAILQ_REMOVE(pathq, pe, next);
252 /* Display the retrieved module search path */
254 showpath(struct pathhead *pathq)
258 if ((s = qstring(pathq)) == NULL) {
260 err(1, "building path string");
266 /* Break a string down into path components, store them into a queue */
268 parsepath(struct pathhead *pathq, char *path, int uniq)
271 struct pathentry *pe;
273 while ((p = strsep(&path, ";")) != NULL)
275 if (((pe = malloc(sizeof(pe))) == NULL) ||
276 ((pe->path = strdup(p)) == NULL)) {
278 err(1, "allocating path element");
280 TAILQ_INSERT_TAIL(pathq, pe, next);
282 addpath(pathq, p, 1, 0);
286 /* Recreate a path string from a components queue */
288 qstring(struct pathhead *pathq)
291 struct pathentry *pe;
294 TAILQ_FOREACH(pe, pathq, next) {
295 asprintf(&p, "%s%s%s",
296 s, pe->path, (TAILQ_NEXT(pe, next) != NULL? ";": ""));
311 fprintf(stderr, "%s\n%s\n",
312 "usage:\tkldconfig [-dfimnUv] [-S sysctlname] [path..]",
319 main(int argc, char *argv[])
321 /* getopt() iterator */
323 /* iterator over argv[] path components */
325 /* Command-line flags: */
326 /* "-f" - no diagnostic messages */
328 /* "-i" - insert before the first element */
330 /* "-m" - merge into the existing path, do not replace it */
332 /* "-n" - do not actually set the new module path */
334 /* "-r" - print out the current search path */
336 /* "-U" - remove duplicate values from the path */
338 /* "-v" - verbose operation (currently a no-op) */
340 /* The higher-level function to call - add/remove */
341 void (*act)(struct pathhead *, char *, int, int);
342 /* The original path */
344 /* The module search path broken down into components */
345 struct pathhead pathq;
347 fflag = iflag = mflag = nflag = rflag = uniqflag = vflag = 0;
350 if ((pathctl = strdup(PATHCTL)) == NULL) {
351 /* this is just too paranoid ;) */
353 err(1, "initializing sysctl name %s", PATHCTL);
356 /* If no arguments and no options are specified, force '-m' */
360 while ((c = getopt(argc, argv, "dfimnrS:Uv")) != -1)
388 if ((pathctl = strdup(optarg)) == NULL) {
390 err(1, "sysctl name %s", optarg);
406 /* The '-r' flag cannot be used when paths are also specified */
407 if (rflag && (argc > 0))
412 /* Retrieve and store the path from the sysctl value */
414 if ((origpath = strdup(modpath)) == NULL) {
416 err(1, "saving the original search path");
420 * Break down the path into the components queue if:
421 * - we are NOT adding paths, OR
422 * - the 'merge' flag is specified, OR
423 * - the 'print only' flag is specified, OR
424 * - the 'unique' flag is specified.
426 if ((act != addpath) || mflag || rflag || uniqflag)
427 parsepath(&pathq, modpath, uniqflag);
428 else if (modpath[0] != '\0')
431 /* Process the path arguments */
432 for (i = 0; i < argc; i++)
433 act(&pathq, argv[i], fflag, iflag);
435 if (changed && !nflag)
438 if (rflag || (changed && vflag)) {
439 if (changed && (vflag > 1))
440 printf("%s -> ", origpath);