[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.1.1.1

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