[BACK]Return to modstat.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / modstat

Annotation of src/usr.bin/modstat/modstat.c, Revision 1.7

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