]> git.saurik.com Git - apple/system_cmds.git/blob - kmodstat.tproj/kmodstat.c
system_cmds-433.8.tar.gz
[apple/system_cmds.git] / kmodstat.tproj / kmodstat.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 * "kldstat.c,v 1.5 1998/11/07 00:29:09 des Exp";
51 */
52
53 #ifndef lint
54 static const char rcsid[] =
55 "$Id: kmodstat.c,v 1.4 2002/04/18 18:48:42 lindak Exp $";
56 #endif /* not lint */
57
58 #include <err.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <unistd.h>
62 #include <sys/types.h>
63 #include <sys/param.h>
64
65 #include <mach/mach.h>
66 #include <mach/mach_error.h>
67 #include <mach/mach_host.h>
68
69 static void
70 machwarn(int error, const char *message)
71 {
72 if (error == KERN_SUCCESS) return;
73 fprintf(stderr, "kmodstat: %s: %s\n", message, mach_error_string(error));
74 }
75
76 static void
77 macherr(int error, const char *message)
78 {
79 if (error == KERN_SUCCESS) return;
80 fprintf(stderr, "kmodstat: %s: %s\n", message, mach_error_string(error));
81 exit(1);
82 }
83
84 static int
85 kmod_compare(const void *a, const void *b)
86 {
87 return (((kmod_info_t *)a)->id - ((kmod_info_t *)b)->id);
88 }
89
90 static void
91 usage(void)
92 {
93 fprintf(stderr, "usage: kmodstat [-i id] [-n name]\n");
94 exit(1);
95 }
96
97 int
98 main(int argc, char** argv)
99 {
100 int c, idset = 0, id = 0;
101 char* name = 0;
102 kmod_info_t *info, *k;
103 kmod_reference_t *r;
104 int i, j, rc, foundit, count, rcount;
105 mach_port_t kernel_port;
106
107 fprintf(stderr, "%s is deprecated; use kextstat(8) instead\n", argv[0]);
108 sleep(5);
109
110 while ((c = getopt(argc, argv, "i:n:")) != -1)
111 switch (c) {
112 case 'i':
113 idset++;
114 id = atoi(optarg);
115 break;
116 case 'n':
117 name = optarg;
118 break;
119 default:
120 usage();
121 }
122 argc -= optind;
123 argv += optind;
124
125 if (!idset && !name && (argc == 1)) {
126 name = *argv;
127 argc--;
128 }
129
130 if (argc != 0) usage();
131
132 rc = task_for_pid(mach_task_self(), 0, &kernel_port);
133 machwarn(rc, "unable to get kernel task port");
134 if (rc) {
135 fprintf(stderr, "kmodstat: Are you running as root?\n");
136 exit(1);
137 }
138
139 rc= kmod_get_info(kernel_port, (void *)&info, &count);
140 macherr(rc, "kmod_get_info() failed");
141
142 k = info; count = 0;
143 while (k) {
144 count++;
145 k = (k->next) ? (k + 1) : 0;
146 }
147
148 k = info; r = (kmod_reference_t *)(info + count);
149 while (k) {
150 if ((rcount = (int)k->reference_list)) {
151 k->reference_list = r;
152 for (i=0; i < rcount; i++) {
153 foundit = 0;
154 for (j=0; j < count; j++) {
155 if (r->info == info[j].next) {
156 r->info = (kmod_info_t *)info[j].id;
157 foundit++;
158 break;
159 }
160 }
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;
163 r->next = r + 1;
164 r++;
165 }
166 k->reference_list[rcount - 1].next = 0;
167 }
168 k = (k->next) ? (k + 1) : 0;
169 }
170
171 printf("Id Refs Address Size Wired Name (Version) <Linked Against>\n");
172
173 if (!count) return 0;
174
175 qsort(info, count, sizeof(kmod_info_t), kmod_compare);
176
177 if (idset || name) {
178 kmod_info_t *k = info;
179 int match_count = 0;
180 for (i=0; i < count; i++, k++) {
181 if ((idset && id == k->id) || (name && !strcmp(k->name, name))) {
182 info[match_count++] = *k;
183 }
184 }
185 count = match_count;
186 }
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);
192
193 if ((r = info->reference_list)) {
194 printf(" <%d", (int)r->info);
195 r = r->next;
196 while (r) {
197 printf(" %d", (int)r->info);
198 r = r->next;
199 }
200 printf(">");
201 }
202 printf("\n");
203 }
204
205 return 0;
206 }