Annotation of src/usr.bin/modstat/modstat.c, Revision 1.3
1.3 ! deraadt 1: /* * $OpenBSD: modstat.c,v 1.2 1996/06/21 11:40:09 mickey Exp $*/
1.1 deraadt 2: /*
3: * Copyright (c) 1993 Terrence R. Lambert.
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: * 3. All advertising materials mentioning features or use of this software
15: * must display the following acknowledgement:
16: * This product includes software developed by Terrence R. Lambert.
17: * 4. The name Terrence R. Lambert may not be used to endorse or promote
18: * products derived from this software without specific prior written
19: * permission.
20: *
21: * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY
22: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24: * ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
25: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31: * SUCH DAMAGE.
32: *
33: */
34:
35: #include <stdio.h>
36: #include <stdlib.h>
37: #include <err.h>
38: #include <string.h>
39: #include <a.out.h>
40: #include <sys/param.h>
41: #include <sys/ioctl.h>
42: #include <sys/conf.h>
43: #include <sys/mount.h>
44: #include <sys/lkm.h>
45: #include <sys/file.h>
46: #include <errno.h>
47: #include "pathnames.h"
48:
49: void
50: usage()
51: {
52:
53: fprintf(stderr, "usage:\n");
54: fprintf(stderr, "modstat [-i <module id>] [-n <module name>]\n");
55: exit(1);
56: }
57:
58: static char *type_names[] = {
59: "SYSCALL",
60: "VFS",
61: "DEV",
62: "STRMOD",
63: "EXEC",
64: "MISC"
65: };
66:
67: int
68: dostat(devfd, modnum, modname)
69: int devfd;
70: int modnum;
71: char *modname;
72: {
73: struct lmc_stat sbuf;
1.2 mickey 74: char name[MAXLKMNAME] = "";
75:
76: sbuf.id = modnum;
77: sbuf.name = name;
1.1 deraadt 78:
79: if (modname != NULL)
80: strcpy(sbuf.name, modname);
81:
82: if (ioctl(devfd, LMSTAT, &sbuf) == -1) {
83: switch (errno) {
84: case EINVAL: /* out of range */
85: return 2;
86: case ENOENT: /* no such entry */
87: return 1;
88: default: /* other error (EFAULT, etc) */
89: warn("LMSTAT");
90: return 4;
91: }
92: }
93:
94: /*
95: * Decode this stat buffer...
96: */
1.2 mickey 97: printf("%-7s %3d %3d %08x %04x %8x %3d s\n",
1.1 deraadt 98: type_names[sbuf.type],
99: sbuf.id, /* module id */
100: sbuf.offset, /* offset into modtype struct */
101: sbuf.area, /* address module loaded at */
102: sbuf.size, /* size in pages(K) */
103: sbuf.private, /* kernel address of private area */
104: sbuf.ver, /* Version; always 1 for now */
105: sbuf.name /* name from private area */
106: );
107:
108: /*
109: * Done (success).
110: */
111: return 0;
112: }
113:
114: int devfd;
115:
116: void
117: cleanup()
118: {
119:
120: close(devfd);
121: }
122:
123: int
124: main(argc, argv)
125: int argc;
126: char *argv[];
127: {
128: int c;
129: int modnum = -1;
130: char *modname = NULL;
131:
132: while ((c = getopt(argc, argv, "i:n:")) != EOF) {
133: switch (c) {
134: case 'i':
135: modnum = atoi(optarg);
136: break; /* number */
137: case 'n':
138: modname = optarg;
139: break; /* name */
140: case '?':
141: usage();
142: default:
143: printf("default!\n");
144: break;
145: }
146: }
147: argc -= optind;
148: argv += optind;
149:
150: if (argc != 0)
151: usage();
152:
153: /*
154: * Open the virtual device device driver for exclusive use (needed
155: * to ioctl() to retrive the loaded module(s) status).
156: */
157: if ((devfd = open(_PATH_LKM, O_RDONLY, 0)) == -1)
158: err(2, _PATH_LKM);
159:
160: atexit(cleanup);
161:
162: printf("Type Id Off Loadaddr Size Info Rev Module Name\n");
163:
164: /*
165: * Oneshot?
166: */
167: if (modnum != -1 || modname != NULL) {
168: if (dostat(devfd, modnum, modname))
169: exit(3);
170: exit(0);
171: }
172:
173: /*
174: * Start at 0 and work up until "EINVAL".
175: */
176: for (modnum = 0; dostat(devfd, modnum, NULL) < 2; modnum++)
177: ;
178:
179: exit(0);
180: }