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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/queue.h>
33 #include <sys/sysctl.h>
43 /* the default sysctl name */
44 #define PATHCTL "kern.module_path"
46 /* queue structure for the module path broken down into components */
47 TAILQ_HEAD(pathhead, pathentry);
50 TAILQ_ENTRY(pathentry) next;
53 /* the Management Information Base entries for the search path sysctl */
56 /* the sysctl name, defaults to PATHCTL */
58 /* the sysctl value - the current module search path */
60 /* flag whether user actions require changing the sysctl value */
63 /* Top-level path management functions */
64 static void addpath(struct pathhead *, char *, int, int);
65 static void rempath(struct pathhead *, char *, int, int);
66 static void showpath(struct pathhead *);
68 /* Low-level path management functions */
69 static char *qstring(struct pathhead *);
71 /* sysctl-related functions */
72 static void getmib(void);
73 static void getpath(void);
74 static void parsepath(struct pathhead *, char *, int);
75 static void setpath(struct pathhead *);
77 static void usage(void);
79 /* Get the MIB entry for our sysctl */
84 /* have we already fetched it? */
88 miblen = sizeof(mib) / sizeof(mib[0]);
89 if (sysctlnametomib(pathctl, mib, &miblen) != 0)
90 err(1, "sysctlnametomib(%s)", pathctl);
93 /* Get the current module search path */
100 if (modpath != NULL) {
107 if (sysctl(mib, miblen, NULL, &sz, NULL, 0) == -1)
108 err(1, "getting path: sysctl(%s) - size only", pathctl);
109 if ((path = malloc(sz + 1)) == NULL) {
111 err(1, "allocating %lu bytes for the path",
112 (unsigned long)sz+1);
114 if (sysctl(mib, miblen, path, &sz, NULL, 0) == -1)
115 err(1, "getting path: sysctl(%s)", pathctl);
119 /* Set the module search path after changing it */
121 setpath(struct pathhead *pathq)
127 if ((newpath = qstring(pathq)) == NULL) {
129 err(1, "building path string");
131 if (sysctl(mib, miblen, NULL, NULL, newpath, strlen(newpath)+1) == -1)
132 err(1, "setting path: sysctl(%s)", pathctl);
139 /* Add/insert a new component to the module search path */
141 addpath(struct pathhead *pathq, char *path, int force, int insert)
143 struct pathentry *pe, *pskip;
144 char pathbuf[MAXPATHLEN+1];
146 static unsigned added = 0;
150 * If the path exists, use it; otherwise, take the user-specified
151 * path at face value - may be a removed directory.
153 if (realpath(path, pathbuf) == NULL)
154 strlcpy(pathbuf, path, sizeof(pathbuf));
156 len = strlen(pathbuf);
157 /* remove a terminating slash if present */
158 if ((len > 0) && (pathbuf[len-1] == '/'))
159 pathbuf[--len] = '\0';
161 /* is it already in there? */
162 TAILQ_FOREACH(pe, pathq, next)
163 if (!strcmp(pe->path, pathbuf))
168 errx(1, "already in the module search path: %s", pathbuf);
171 /* OK, allocate and add it. */
172 if (((pe = malloc(sizeof(*pe))) == NULL) ||
173 ((pe->path = strdup(pathbuf)) == NULL)) {
175 err(1, "allocating path component");
178 TAILQ_INSERT_TAIL(pathq, pe, next);
180 for (i = 0, pskip = TAILQ_FIRST(pathq); i < added; i++)
181 pskip = TAILQ_NEXT(pskip, next);
183 TAILQ_INSERT_BEFORE(pskip, pe, next);
185 TAILQ_INSERT_TAIL(pathq, pe, next);
191 /* Remove a path component from the module search path */
193 rempath(struct pathhead *pathq, char *path, int force, int insert __unused)
195 char pathbuf[MAXPATHLEN+1];
196 struct pathentry *pe;
199 /* same logic as in addpath() */
200 if (realpath(path, pathbuf) == NULL)
201 strlcpy(pathbuf, path, sizeof(pathbuf));
203 len = strlen(pathbuf);
204 /* remove a terminating slash if present */
205 if ((len > 0) && (pathbuf[len-1] == '/'))
206 pathbuf[--len] = '\0';
208 /* Is it in there? */
209 TAILQ_FOREACH(pe, pathq, next)
210 if (!strcmp(pe->path, pathbuf))
215 errx(1, "not in module search path: %s", pathbuf);
218 /* OK, remove it now.. */
219 TAILQ_REMOVE(pathq, pe, next);
223 /* Display the retrieved module search path */
225 showpath(struct pathhead *pathq)
229 if ((s = qstring(pathq)) == NULL) {
231 err(1, "building path string");
237 /* Break a string down into path components, store them into a queue */
239 parsepath(struct pathhead *pathq, char *path, int uniq)
242 struct pathentry *pe;
244 while ((p = strsep(&path, ";")) != NULL)
246 if (((pe = malloc(sizeof(*pe))) == NULL) ||
247 ((pe->path = strdup(p)) == NULL)) {
249 err(1, "allocating path element");
251 TAILQ_INSERT_TAIL(pathq, pe, next);
253 addpath(pathq, p, 1, 0);
257 /* Recreate a path string from a components queue */
259 qstring(struct pathhead *pathq)
262 struct pathentry *pe;
265 TAILQ_FOREACH(pe, pathq, next) {
266 asprintf(&p, "%s%s%s",
267 s, pe->path, (TAILQ_NEXT(pe, next) != NULL? ";": ""));
282 fprintf(stderr, "%s\n%s\n",
283 "usage:\tkldconfig [-dfimnUv] [-S sysctlname] [path ...]",
290 main(int argc, char *argv[])
292 /* getopt() iterator */
294 /* iterator over argv[] path components */
296 /* Command-line flags: */
297 /* "-f" - no diagnostic messages */
299 /* "-i" - insert before the first element */
301 /* "-m" - merge into the existing path, do not replace it */
303 /* "-n" - do not actually set the new module path */
305 /* "-r" - print out the current search path */
307 /* "-U" - remove duplicate values from the path */
309 /* "-v" - verbose operation (currently a no-op) */
311 /* The higher-level function to call - add/remove */
312 void (*act)(struct pathhead *, char *, int, int);
313 /* The original path */
315 /* The module search path broken down into components */
316 struct pathhead pathq;
318 fflag = iflag = mflag = nflag = rflag = uniqflag = vflag = 0;
321 if ((pathctl = strdup(PATHCTL)) == NULL) {
322 /* this is just too paranoid ;) */
324 err(1, "initializing sysctl name %s", PATHCTL);
327 /* If no arguments and no options are specified, force '-m' */
331 while ((c = getopt(argc, argv, "dfimnrS:Uv")) != -1)
359 if ((pathctl = strdup(optarg)) == NULL) {
361 err(1, "sysctl name %s", optarg);
377 /* The '-r' flag cannot be used when paths are also specified */
378 if (rflag && (argc > 0))
383 /* Retrieve and store the path from the sysctl value */
385 if ((origpath = strdup(modpath)) == NULL) {
387 err(1, "saving the original search path");
391 * Break down the path into the components queue if:
392 * - we are NOT adding paths, OR
393 * - the 'merge' flag is specified, OR
394 * - the 'print only' flag is specified, OR
395 * - the 'unique' flag is specified.
397 if ((act != addpath) || mflag || rflag || uniqflag)
398 parsepath(&pathq, modpath, uniqflag);
399 else if (modpath[0] != '\0')
402 /* Process the path arguments */
403 for (i = 0; i < argc; i++)
404 act(&pathq, argv[i], fflag, iflag);
406 if (changed && !nflag)
409 if (rflag || (changed && vflag)) {
410 if (changed && (vflag > 1))
411 printf("%s -> ", origpath);