]> git.saurik.com Git - apple/system_cmds.git/blame - kmodunload.tproj/kmodunload.c
system_cmds-196.tar.gz
[apple/system_cmds.git] / kmodunload.tproj / kmodunload.c
CommitLineData
1815bff5
A
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
54static const char rcsid[] =
55 "$Id: kmodunload.c,v 1.3 2001/02/05 19:53:16 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
67static int verbose = 0;
68#define v_printf if (verbose) printf
69
70static void
71machwarn(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
77static void
78macherr(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
85static mach_port_t kernel_priv_port;
86
87static void
88stop_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
100static void
101unload_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
111static void
112usage(void)
113{
114 fprintf(stderr, "usage: kmodunload [-v] -i id\n");
115 fprintf(stderr, " kmodunload [-v] -n name\n");
116 exit(1);
117}
118
119int
120main(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 while ((c = getopt(argc, argv, "i:n:v")) != -1)
131 switch (c) {
132 case 'i':
133 id = atoi(optarg);
134 break;
135 case 'n':
136 name = optarg;
137 break;
138 case 'v':
139 verbose = 1;
140 break;
141 default:
142 usage();
143 }
144 argc -= optind;
145 argv += optind;
146
147 if (!id && !name && (argc == 1)) {
148 name = *argv;
149 argc--;
150 }
151
152 if ((argc != 0) || (id && name))
153 usage();
154
155 if ((id == 0) && (name == 0))
156 usage();
157
158 r = task_for_pid(mach_task_self(), 0, &kernel_port);
159 machwarn(r, "unable to get kernel task port");
160 if (r) {
161 fprintf(stderr, "kmodunload: Are you running as root?\n");
162 exit(1);
163 }
164
165 r = kmod_get_info(kernel_port, (void *)&info, &count);
166 macherr(r, "kmod_get_info() failed");
167
168 if (count < 1) {
169 fprintf(stderr, "kmodunload: there is nothing to unload?\n");
170 exit(1);
171 }
172
173 if (name) {
174 kmod_info_t *k = info;
175 while (k) {
176 if (!strcmp(k->name, name)) {
177 id = k->id;
178 break;
179 }
180 k = (k->next) ? (k + 1) : 0;
181 }
182 if (!k) {
183 fprintf(stderr, "kmodunload: can't kmod named: %s.\n", name);
184 exit(1);
185 }
186 } else {
187 kmod_info_t *k = info;
188 while (k) {
189 if (id == k->id) {
190 name = k->name;
191 break;
192 }
193 k = (k->next) ? (k + 1) : 0;
194 }
195 if (!name) {
196 fprintf(stderr, "kmodunload: can't find kmod id %d.\n", id);
197 exit(1);
198 }
199 }
200
201 v_printf("kmodunload: found kmod %s, id %d.\n", name, id);
202 kernel_priv_port = mach_host_self(); /* if we are privileged */
203
204 stop_module(id);
205 unload_module(id);
206
207 return 0;
208}
209