xref: /freebsd/sbin/kldunload/kldunload.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1997 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/linker.h>
31 
32 #include <err.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sysexits.h>
36 #include <unistd.h>
37 
38 static void
39 usage(void)
40 {
41 	fprintf(stderr, "usage: kldunload [-fv] -i id ...\n");
42 	fprintf(stderr, "       kldunload [-fv] [-n] name ...\n");
43 	exit(EX_USAGE);
44 }
45 
46 #define OPT_NULL	0x00
47 #define OPT_ID		0x01
48 #define OPT_VERBOSE	0x02
49 #define OPT_FORCE	0x04
50 
51 int
52 main(int argc, char** argv)
53 {
54 	struct kld_file_stat stat;
55 	int c, fileid, force, opt;
56 	char *filename;
57 
58 	filename = NULL;
59 	opt = OPT_NULL;
60 
61 	while ((c = getopt(argc, argv, "finv")) != -1) {
62 		switch (c) {
63 		case 'f':
64 			opt |= OPT_FORCE;
65 			break;
66 		case 'i':
67 			opt |= OPT_ID;
68 			break;
69 		case 'n':
70 			/*
71 			 * XXX: For backward compatibility. Currently does
72 			 * nothing
73 			 */
74 			break;
75 		case 'v':
76 			opt |= OPT_VERBOSE;
77 			break;
78 		default:
79 			usage();
80 		}
81 	}
82 
83 	argc -= optind;
84 	argv += optind;
85 
86 	if (argc == 0)
87 		usage();
88 
89 	while ((filename = *argv++) != NULL) {
90 		if (opt & OPT_ID) {
91 			fileid = atoi(filename);
92 			if (fileid < 0)
93 				errx(EXIT_FAILURE, "Invalid ID %s", optarg);
94 		} else {
95 			if ((fileid = kldfind(filename)) < 0)
96 				errx(EXIT_FAILURE, "can't find file %s",
97 				    filename);
98 		}
99 		if (opt & OPT_VERBOSE) {
100 			stat.version = sizeof(stat);
101 			if (kldstat(fileid, &stat) < 0)
102 				err(EXIT_FAILURE, "can't stat file");
103 			(void) printf("Unloading %s, id=%d\n", stat.name,
104 			    fileid);
105 		}
106 		if (opt & OPT_FORCE)
107 			force = LINKER_UNLOAD_FORCE;
108 		else
109 			force = LINKER_UNLOAD_NORMAL;
110 
111 		if (kldunloadf(fileid, force) < 0)
112 			err(EXIT_FAILURE, "can't unload file");
113 	}
114 
115 	return (EXIT_SUCCESS);
116 }
117