]>
git.saurik.com Git - apple/system_cmds.git/blob - kmodstat.tproj/kmodstat.c
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 * "kldstat.c,v 1.5 1998/11/07 00:29:09 des Exp";
54 static const char rcsid
[] =
55 "$Id: kmodstat.c,v 1.4 2002/04/18 18:48:42 lindak Exp $";
62 #include <sys/types.h>
63 #include <sys/param.h>
65 #include <mach/mach.h>
66 #include <mach/mach_error.h>
67 #include <mach/mach_host.h>
70 machwarn(int error
, const char *message
)
72 if (error
== KERN_SUCCESS
) return;
73 fprintf(stderr
, "kmodstat: %s: %s\n", message
, mach_error_string(error
));
77 macherr(int error
, const char *message
)
79 if (error
== KERN_SUCCESS
) return;
80 fprintf(stderr
, "kmodstat: %s: %s\n", message
, mach_error_string(error
));
85 kmod_compare(const void *a
, const void *b
)
87 return (((kmod_info_t
*)a
)->id
- ((kmod_info_t
*)b
)->id
);
93 fprintf(stderr
, "usage: kmodstat [-i id] [-n name]\n");
98 main(int argc
, char** argv
)
100 int c
, idset
= 0, id
= 0;
102 kmod_info_t
*info
, *k
;
104 int i
, j
, rc
, foundit
, count
, rcount
;
105 mach_port_t kernel_port
;
107 fprintf(stderr
, "%s is deprecated; use kextstat(8) instead\n", argv
[0]);
110 while ((c
= getopt(argc
, argv
, "i:n:")) != -1)
125 if (!idset
&& !name
&& (argc
== 1)) {
130 if (argc
!= 0) usage();
132 rc
= task_for_pid(mach_task_self(), 0, &kernel_port
);
133 machwarn(rc
, "unable to get kernel task port");
135 fprintf(stderr
, "kmodstat: Are you running as root?\n");
139 rc
= kmod_get_info(kernel_port
, (void *)&info
, &count
);
140 macherr(rc
, "kmod_get_info() failed");
145 k
= (k
->next
) ? (k
+ 1) : 0;
148 k
= info
; r
= (kmod_reference_t
*)(info
+ count
);
150 if ((rcount
= (int)k
->reference_list
)) {
151 k
->reference_list
= r
;
152 for (i
=0; i
< rcount
; i
++) {
154 for (j
=0; j
< count
; j
++) {
155 if (r
->info
== info
[j
].next
) {
156 r
->info
= (kmod_info_t
*)info
[j
].id
;
161 // force the id in here, the sorting below messes up the pointers
162 if (!foundit
) r
->info
= (kmod_info_t
*)info
[count
- 1].id
;
166 k
->reference_list
[rcount
- 1].next
= 0;
168 k
= (k
->next
) ? (k
+ 1) : 0;
171 printf("Id Refs Address Size Wired Name (Version) <Linked Against>\n");
173 if (!count
) return 0;
175 qsort(info
, count
, sizeof(kmod_info_t
), kmod_compare
);
178 kmod_info_t
*k
= info
;
180 for (i
=0; i
< count
; i
++, k
++) {
181 if ((idset
&& id
== k
->id
) || (name
&& !strcmp(k
->name
, name
))) {
182 info
[match_count
++] = *k
;
187 for (i
=0; i
< count
; i
++, info
++) {
188 printf("%2d %4d %-10p %-10p %-10p %s (%s)",
189 info
->id
, info
->reference_count
, (void *)info
->address
,
190 (void *)info
->size
, (void *)(info
->size
- info
->hdr_size
),
191 info
->name
, info
->version
);
193 if ((r
= info
->reference_list
)) {
194 printf(" <%d", (int)r
->info
);
197 printf(" %d", (int)r
->info
);