]> git.saurik.com Git - apple/system_cmds.git/blob - kmodunload.tproj/kmodunload.c
system_cmds-433.tar.gz
[apple/system_cmds.git] / kmodunload.tproj / kmodunload.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*-
25 * Copyright (c) 1997 Doug Rabson
26 * All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 *
49 * Original code from:
50 * "kldunload.c,v 1.7 1998/11/07 00:42:52 des Exp"
51 */
52
53 #ifndef lint
54 static const char rcsid[] =
55 "$Id: kmodunload.c,v 1.5 2002/04/24 20:03:48 lindak Exp $";
56 #endif /* not lint */
57
58 #include <err.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <unistd.h>
62
63 #include <mach/mach.h>
64 #include <mach/mach_error.h>
65 #include <mach/mach_host.h>
66
67 static int verbose = 0;
68 #define v_printf if (verbose) printf
69
70 static void
71 machwarn(int error, const char *message)
72 {
73 if (error == KERN_SUCCESS) return;
74 fprintf(stderr, "kmodunload: %s: %s\n", message, mach_error_string(error));
75 }
76
77 static void
78 macherr(int error, const char *message)
79 {
80 if (error == KERN_SUCCESS) return;
81 fprintf(stderr, "kmodunload: %s: %s\n", message, mach_error_string(error));
82 exit(1);
83 }
84
85 static mach_port_t kernel_priv_port;
86
87 static void
88 stop_module(kmod_t id)
89 {
90 int r;
91 void * args = 0;
92 int argsCount= 0;
93
94 r = kmod_control(kernel_priv_port, id, KMOD_CNTL_STOP, &args, &argsCount);
95 macherr(r, "kmod_control(stop) failed");
96
97 v_printf("kmodunload: kmod id %d successfully stopped.\n", id);
98 }
99
100 static void
101 unload_module(kmod_t id)
102 {
103 int r;
104
105 r = kmod_destroy(kernel_priv_port, id);
106 macherr(r, "kmod_destroy() failed");
107
108 v_printf("kmodunload: kmod id %d successfully unloaded.\n", id);
109 }
110
111 static void
112 usage(void)
113 {
114 fprintf(stderr, "usage: kmodunload [-v] -i id\n");
115 fprintf(stderr, " kmodunload [-v] -n name\n");
116 exit(1);
117 }
118
119 int
120 main(int argc, char** argv)
121 {
122 int c;
123 int id = 0;
124 char* name = 0;
125 kmod_info_t *info;
126 int r;
127 int count;
128 mach_port_t kernel_port;
129
130 fprintf(stderr, "%s is deprecated; use kextunload(8) instead\n", argv[0]);
131 sleep(5);
132
133 while ((c = getopt(argc, argv, "i:n:v")) != -1)
134 switch (c) {
135 case 'i':
136 id = atoi(optarg);
137 break;
138 case 'n':
139 name = optarg;
140 break;
141 case 'v':
142 verbose = 1;
143 break;
144 default:
145 usage();
146 }
147 argc -= optind;
148 argv += optind;
149
150 if (!id && !name && (argc == 1)) {
151 name = *argv;
152 argc--;
153 }
154
155 if ((argc != 0) || (id && name))
156 usage();
157
158 if ((id == 0) && (name == 0))
159 usage();
160
161 r = task_for_pid(mach_task_self(), 0, &kernel_port);
162 machwarn(r, "unable to get kernel task port");
163 if (r) {
164 fprintf(stderr, "kmodunload: Are you running as root?\n");
165 exit(1);
166 }
167
168 r = kmod_get_info(kernel_port, (void *)&info, &count);
169 macherr(r, "kmod_get_info() failed");
170
171 if (count < 1) {
172 fprintf(stderr, "kmodunload: there is nothing to unload?\n");
173 exit(1);
174 }
175
176 if (name) {
177 kmod_info_t *k = info;
178 while (k) {
179 if (!strcmp(k->name, name)) {
180 id = k->id;
181 break;
182 }
183 k = (k->next) ? (k + 1) : 0;
184 }
185 if (!k) {
186 fprintf(stderr, "kmodunload: can't kmod named: %s.\n", name);
187 exit(1);
188 }
189 } else {
190 kmod_info_t *k = info;
191 while (k) {
192 if (id == k->id) {
193 name = k->name;
194 break;
195 }
196 k = (k->next) ? (k + 1) : 0;
197 }
198 if (!name) {
199 fprintf(stderr, "kmodunload: can't find kmod id %d.\n", id);
200 exit(1);
201 }
202 }
203
204 v_printf("kmodunload: found kmod %s, id %d.\n", name, id);
205 kernel_priv_port = mach_host_self(); /* if we are privileged */
206
207 stop_module(id);
208 unload_module(id);
209
210 return 0;
211 }
212