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

1.11    ! deraadt     1: /*     $OpenBSD: modstat.c,v 1.10 1997/11/18 23:23:18 provos 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 <sys/param.h>
                     37: #include <sys/ioctl.h>
                     38: #include <sys/conf.h>
                     39: #include <sys/mount.h>
                     40: #include <sys/lkm.h>
                     41: #include <sys/file.h>
1.9       deraadt    42: #include <stdio.h>
                     43: #include <stdlib.h>
                     44: #include <unistd.h>
                     45: #include <err.h>
                     46: #include <string.h>
                     47: #include <a.out.h>
1.1       deraadt    48: #include <errno.h>
                     49: #include "pathnames.h"
                     50:
                     51: void
                     52: usage()
                     53: {
                     54:
1.4       deraadt    55:        fprintf(stderr,
1.6       deraadt    56:            "usage: modstat [-i moduleid] [-n modulename]\n");
1.1       deraadt    57:        exit(1);
                     58: }
                     59:
                     60: static char *type_names[] = {
                     61:        "SYSCALL",
                     62:        "VFS",
                     63:        "DEV",
                     64:        "STRMOD",
                     65:        "EXEC",
                     66:        "MISC"
                     67: };
                     68:
                     69: int
                     70: dostat(devfd, modnum, modname)
                     71:        int devfd;
                     72:        int modnum;
                     73:        char *modname;
                     74: {
                     75:        struct lmc_stat sbuf;
1.2       mickey     76:        char name[MAXLKMNAME] = "";
                     77:
1.8       deraadt    78:        bzero(&sbuf, sizeof sbuf);
1.2       mickey     79:        sbuf.id = modnum;
                     80:        sbuf.name = name;
1.1       deraadt    81:
1.6       deraadt    82:        if (modname != NULL) {
1.10      provos     83:                if (strlen(modname) >= sizeof(name))
1.6       deraadt    84:                        return 4;
1.1       deraadt    85:                strcpy(sbuf.name, modname);
1.6       deraadt    86:        }
1.1       deraadt    87:
                     88:        if (ioctl(devfd, LMSTAT, &sbuf) == -1) {
                     89:                switch (errno) {
                     90:                case EINVAL:            /* out of range */
                     91:                        return 2;
                     92:                case ENOENT:            /* no such entry */
                     93:                        return 1;
                     94:                default:                /* other error (EFAULT, etc) */
                     95:                        warn("LMSTAT");
                     96:                        return 4;
                     97:                }
                     98:        }
                     99:
                    100:        /*
                    101:         * Decode this stat buffer...
                    102:         */
1.5       mickey    103:        printf("%-7s %3d %3d %08x %04x %8x %3d %s\n",
1.1       deraadt   104:            type_names[sbuf.type],
                    105:            sbuf.id,            /* module id */
                    106:            sbuf.offset,        /* offset into modtype struct */
                    107:            sbuf.area,          /* address module loaded at */
                    108:            sbuf.size,          /* size in pages(K) */
                    109:            sbuf.private,       /* kernel address of private area */
                    110:            sbuf.ver,           /* Version; always 1 for now */
                    111:            sbuf.name           /* name from private area */
                    112:        );
                    113:
                    114:        /*
                    115:         * Done (success).
                    116:         */
                    117:        return 0;
                    118: }
                    119:
                    120: int devfd;
                    121:
                    122: void
                    123: cleanup()
                    124: {
                    125:
                    126:        close(devfd);
                    127: }
                    128:
                    129: int
                    130: main(argc, argv)
                    131:        int argc;
                    132:        char *argv[];
                    133: {
                    134:        int c;
                    135:        int modnum = -1;
                    136:        char *modname = NULL;
                    137:
1.7       millert   138:        while ((c = getopt(argc, argv, "i:n:")) != -1) {
1.1       deraadt   139:                switch (c) {
                    140:                case 'i':
                    141:                        modnum = atoi(optarg);
                    142:                        break;  /* number */
                    143:                case 'n':
                    144:                        modname = optarg;
                    145:                        break;  /* name */
1.6       deraadt   146:                default:
1.1       deraadt   147:                        usage();
                    148:                        break;
                    149:                }
                    150:        }
                    151:        argc -= optind;
                    152:        argv += optind;
                    153:
                    154:        if (argc != 0)
                    155:                usage();
                    156:
                    157:        /*
                    158:         * Open the virtual device device driver for exclusive use (needed
                    159:         * to ioctl() to retrive the loaded module(s) status).
                    160:         */
                    161:        if ((devfd = open(_PATH_LKM, O_RDONLY, 0)) == -1)
                    162:                err(2, _PATH_LKM);
1.11    ! deraadt   163:
        !           164:        setegid(getgid());
        !           165:        setgid(getgid());
1.1       deraadt   166:
                    167:        atexit(cleanup);
                    168:
1.5       mickey    169:        printf("Type     Id Off Loadaddr Size Info     Rev Module Name\n");
1.1       deraadt   170:
                    171:        /*
                    172:         * Oneshot?
                    173:         */
                    174:        if (modnum != -1 || modname != NULL) {
                    175:                if (dostat(devfd, modnum, modname))
                    176:                        exit(3);
                    177:                exit(0);
                    178:        }
                    179:
                    180:        /*
                    181:         * Start at 0 and work up until "EINVAL".
                    182:         */
                    183:        for (modnum = 0; dostat(devfd, modnum, NULL) < 2; modnum++)
                    184:                ;
                    185:
                    186:        exit(0);
                    187: }