2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1997 Doug Rabson
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
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.
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
50 * "kldunload.c,v 1.7 1998/11/07 00:42:52 des Exp"
54 static const char rcsid
[] =
55 "$Id: kmodunload.c,v 1.5 2002/04/24 20:03:48 lindak Exp $";
63 #include <mach/mach.h>
64 #include <mach/mach_error.h>
65 #include <mach/mach_host.h>
67 static int verbose
= 0;
68 #define v_printf if (verbose) printf
71 machwarn(int error
, const char *message
)
73 if (error
== KERN_SUCCESS
) return;
74 fprintf(stderr
, "kmodunload: %s: %s\n", message
, mach_error_string(error
));
78 macherr(int error
, const char *message
)
80 if (error
== KERN_SUCCESS
) return;
81 fprintf(stderr
, "kmodunload: %s: %s\n", message
, mach_error_string(error
));
85 static mach_port_t kernel_priv_port
;
88 stop_module(kmod_t id
)
94 r
= kmod_control(kernel_priv_port
, id
, KMOD_CNTL_STOP
, &args
, &argsCount
);
95 macherr(r
, "kmod_control(stop) failed");
97 v_printf("kmodunload: kmod id %d successfully stopped.\n", id
);
101 unload_module(kmod_t id
)
105 r
= kmod_destroy(kernel_priv_port
, id
);
106 macherr(r
, "kmod_destroy() failed");
108 v_printf("kmodunload: kmod id %d successfully unloaded.\n", id
);
114 fprintf(stderr
, "usage: kmodunload [-v] -i id\n");
115 fprintf(stderr
, " kmodunload [-v] -n name\n");
120 main(int argc
, char** argv
)
128 mach_port_t kernel_port
;
130 fprintf(stderr
, "%s is deprecated; use kextunload(8) instead\n", argv
[0]);
133 while ((c
= getopt(argc
, argv
, "i:n:v")) != -1)
150 if (!id
&& !name
&& (argc
== 1)) {
155 if ((argc
!= 0) || (id
&& name
))
158 if ((id
== 0) && (name
== 0))
161 r
= task_for_pid(mach_task_self(), 0, &kernel_port
);
162 machwarn(r
, "unable to get kernel task port");
164 fprintf(stderr
, "kmodunload: Are you running as root?\n");
168 r
= kmod_get_info(kernel_port
, (void *)&info
, &count
);
169 macherr(r
, "kmod_get_info() failed");
172 fprintf(stderr
, "kmodunload: there is nothing to unload?\n");
177 kmod_info_t
*k
= info
;
179 if (!strcmp(k
->name
, name
)) {
183 k
= (k
->next
) ? (k
+ 1) : 0;
186 fprintf(stderr
, "kmodunload: can't kmod named: %s.\n", name
);
190 kmod_info_t
*k
= info
;
196 k
= (k
->next
) ? (k
+ 1) : 0;
199 fprintf(stderr
, "kmodunload: can't find kmod id %d.\n", id
);
204 v_printf("kmodunload: found kmod %s, id %d.\n", name
, id
);
205 kernel_priv_port
= mach_host_self(); /* if we are privileged */