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
26 * $FreeBSD: src/sbin/kldconfig/kldconfig.c,v 1.3.2.1 2001/08/01 05:52:36 obrien Exp $
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/queue.h>
32 #include <sys/sysctl.h>
42 #define NEED_SLASHTERM
44 /* the default sysctl name */
45 #define PATHCTL "kern.module_path"
47 /* queue structure for the module path broken down into components */
48 TAILQ_HEAD(pathhead
, pathentry
);
51 TAILQ_ENTRY(pathentry
) next
;
54 /* the Management Information Base entries for the search path sysctl */
57 /* the sysctl name, defaults to PATHCTL */
59 /* the sysctl value - the current module search path */
61 /* flag whether user actions require changing the sysctl value */
64 /* Top-level path management functions */
65 static void addpath(struct pathhead
*, char *, int, int);
66 static void rempath(struct pathhead
*, char *, int, int);
67 static void showpath(struct pathhead
*);
69 /* Low-level path management functions */
70 static char *qstring(struct pathhead
*);
72 /* sysctl-related functions */
73 static void getmib(void);
74 static void getpath(void);
75 static void parsepath(struct pathhead
*, char *, int);
76 static void setpath(struct pathhead
*);
78 static void usage(void);
80 /* Get the MIB entry for our sysctl */
85 /* have we already fetched it? */
89 miblen
= sizeof(mib
) / sizeof(mib
[0]);
90 if (sysctlnametomib(pathctl
, mib
, &miblen
) != 0)
91 err(1, "sysctlnametomib(%s)", pathctl
);
94 /* Get the current module search path */
101 if (modpath
!= NULL
) {
108 if (sysctl(mib
, miblen
, NULL
, &sz
, NULL
, 0) == -1)
109 err(1, "getting path: sysctl(%s) - size only", pathctl
);
110 if ((path
= malloc(sz
+ 1)) == NULL
) {
112 err(1, "allocating %lu bytes for the path",
113 (unsigned long)sz
+1);
115 if (sysctl(mib
, miblen
, path
, &sz
, NULL
, 0) == -1)
116 err(1, "getting path: sysctl(%s)", pathctl
);
120 /* Set the module search path after changing it */
122 setpath(struct pathhead
*pathq
)
128 if ((newpath
= qstring(pathq
)) == NULL
) {
130 err(1, "building path string");
132 if (sysctl(mib
, miblen
, NULL
, NULL
, newpath
, strlen(newpath
)+1) == -1)
133 err(1, "setting path: sysctl(%s)", pathctl
);
140 /* Add/insert a new component to the module search path */
142 addpath(struct pathhead
*pathq
, char *path
, int force
, int insert
)
144 struct pathentry
*pe
, *pskip
;
145 char pathbuf
[MAXPATHLEN
+1];
147 static unsigned added
= 0;
151 * If the path exists, use it; otherwise, take the user-specified
152 * path at face value - may be a removed directory.
154 if (realpath(path
, pathbuf
) == NULL
)
155 strlcpy(pathbuf
, path
, sizeof(pathbuf
));
157 len
= strlen(pathbuf
);
158 #ifdef NEED_SLASHTERM
159 /* slash-terminate, because the kernel linker said so. */
160 if ((len
== 0) || (pathbuf
[len
-1] != '/')) {
161 if (len
== sizeof(pathbuf
) - 1)
162 errx(1, "path too long: %s", pathbuf
);
165 #else /* NEED_SLASHTERM */
166 /* remove a terminating slash if present */
167 if ((len
> 0) && (pathbuf
[len
-1] == '/'))
168 pathbuf
[--len
] = '\0';
169 #endif /* NEED_SLASHTERM */
171 /* is it already in there? */
172 TAILQ_FOREACH(pe
, pathq
, next
)
173 if (!strcmp(pe
->path
, pathbuf
))
178 errx(1, "already in the module search path: %s", pathbuf
);
181 /* OK, allocate and add it. */
182 if (((pe
= malloc(sizeof(*pe
))) == NULL
) ||
183 ((pe
->path
= strdup(pathbuf
)) == NULL
)) {
185 err(1, "allocating path component");
188 TAILQ_INSERT_TAIL(pathq
, pe
, next
);
190 for (i
= 0, pskip
= TAILQ_FIRST(pathq
); i
< added
; i
++)
191 pskip
= TAILQ_NEXT(pskip
, next
);
193 TAILQ_INSERT_BEFORE(pskip
, pe
, next
);
195 TAILQ_INSERT_TAIL(pathq
, pe
, next
);
201 /* Remove a path component from the module search path */
203 rempath(struct pathhead
*pathq
, char *path
, int force
, int insert __unused
)
205 char pathbuf
[MAXPATHLEN
+1];
206 struct pathentry
*pe
;
209 /* same logic as in addpath() */
210 if (realpath(path
, pathbuf
) == NULL
)
211 strlcpy(pathbuf
, path
, sizeof(pathbuf
));
213 len
= strlen(pathbuf
);
214 #ifdef NEED_SLASHTERM
215 /* slash-terminate, because the kernel linker said so. */
216 if ((len
== 0) || (pathbuf
[len
-1] != '/')) {
217 if (len
== sizeof(pathbuf
) - 1)
218 errx(1, "path too long: %s", pathbuf
);
221 #else /* NEED_SLASHTERM */
222 /* remove a terminating slash if present */
223 if ((len
> 0) && (pathbuf
[len
-1] == '/'))
224 pathbuf
[--len
] = '\0';
225 #endif /* NEED_SLASHTERM */
227 /* Is it in there? */
228 TAILQ_FOREACH(pe
, pathq
, next
)
229 if (!strcmp(pe
->path
, pathbuf
))
234 errx(1, "not in module search path: %s", pathbuf
);
237 /* OK, remove it now.. */
238 TAILQ_REMOVE(pathq
, pe
, next
);
242 /* Display the retrieved module search path */
244 showpath(struct pathhead
*pathq
)
248 if ((s
= qstring(pathq
)) == NULL
) {
250 err(1, "building path string");
256 /* Break a string down into path components, store them into a queue */
258 parsepath(struct pathhead
*pathq
, char *path
, int uniq
)
261 struct pathentry
*pe
;
263 while ((p
= strsep(&path
, ";")) != NULL
)
265 if (((pe
= malloc(sizeof(*pe
))) == NULL
) ||
266 ((pe
->path
= strdup(p
)) == NULL
)) {
268 err(1, "allocating path element");
270 TAILQ_INSERT_TAIL(pathq
, pe
, next
);
272 addpath(pathq
, p
, 1, 0);
276 /* Recreate a path string from a components queue */
278 qstring(struct pathhead
*pathq
)
281 struct pathentry
*pe
;
284 TAILQ_FOREACH(pe
, pathq
, next
) {
285 asprintf(&p
, "%s%s%s",
286 s
, pe
->path
, (TAILQ_NEXT(pe
, next
) != NULL
? ";": ""));
301 fprintf(stderr
, "%s\n%s\n",
302 "usage:\tkldconfig [-dfimnUv] [-S sysctlname] [path..]",
309 main(int argc
, char *argv
[])
311 /* getopt() iterator */
313 /* iterator over argv[] path components */
315 /* Command-line flags: */
316 /* "-f" - no diagnostic messages */
318 /* "-i" - insert before the first element */
320 /* "-m" - merge into the existing path, do not replace it */
322 /* "-n" - do not actually set the new module path */
324 /* "-r" - print out the current search path */
326 /* "-U" - remove duplicate values from the path */
328 /* "-v" - verbose operation (currently a no-op) */
330 /* The higher-level function to call - add/remove */
331 void (*act
)(struct pathhead
*, char *, int, int);
332 /* The original path */
334 /* The module search path broken down into components */
335 struct pathhead pathq
;
337 fflag
= iflag
= mflag
= nflag
= rflag
= uniqflag
= vflag
= 0;
340 if ((pathctl
= strdup(PATHCTL
)) == NULL
) {
341 /* this is just too paranoid ;) */
343 err(1, "initializing sysctl name %s", PATHCTL
);
346 /* If no arguments and no options are specified, force '-m' */
350 while ((c
= getopt(argc
, argv
, "dfimnrS:Uv")) != -1)
378 if ((pathctl
= strdup(optarg
)) == NULL
) {
380 err(1, "sysctl name %s", optarg
);
396 /* The '-r' flag cannot be used when paths are also specified */
397 if (rflag
&& (argc
> 0))
402 /* Retrieve and store the path from the sysctl value */
404 if ((origpath
= strdup(modpath
)) == NULL
) {
406 err(1, "saving the original search path");
410 * Break down the path into the components queue if:
411 * - we are NOT adding paths, OR
412 * - the 'merge' flag is specified, OR
413 * - the 'print only' flag is specified, OR
414 * - the 'unique' flag is specified.
416 if ((act
!= addpath
) || mflag
|| rflag
|| uniqflag
)
417 parsepath(&pathq
, modpath
, uniqflag
);
418 else if (modpath
[0] != '\0')
421 /* Process the path arguments */
422 for (i
= 0; i
< argc
; i
++)
423 act(&pathq
, argv
[i
], fflag
, iflag
);
425 if (changed
&& !nflag
)
428 if (rflag
|| (changed
&& vflag
)) {
429 if (changed
&& (vflag
> 1))
430 printf("%s -> ", origpath
);