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

Annotation of src/usr.bin/ktrace/ktrace.c, Revision 1.37

1.37    ! tedu        1: /*     $OpenBSD: ktrace.c,v 1.36 2019/06/28 13:35:01 deraadt Exp $     */
1.1       deraadt     2: /*     $NetBSD: ktrace.c,v 1.4 1995/08/31 23:01:44 jtc Exp $   */
                      3:
                      4: /*-
                      5:  * Copyright (c) 1988, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.16      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY 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 REGENTS OR CONTRIBUTORS 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:
1.31      deraadt    33: #include <sys/param.h> /* MAXCOMLEN */
                     34: #include <sys/signal.h>
1.1       deraadt    35: #include <sys/stat.h>
                     36: #include <sys/time.h>
                     37: #include <sys/uio.h>
                     38: #include <sys/ktrace.h>
                     39:
                     40: #include <err.h>
1.19      millert    41: #include <errno.h>
1.8       millert    42: #include <fcntl.h>
1.10      deraadt    43: #include <stdlib.h>
1.1       deraadt    44: #include <stdio.h>
1.14      deraadt    45: #include <string.h>
1.1       deraadt    46: #include <unistd.h>
                     47:
                     48: #include "ktrace.h"
1.11      espie      49: #include "extern.h"
1.1       deraadt    50:
1.25      miod       51: extern char *__progname;
                     52:
1.13      millert    53: static int rpid(const char *);
                     54: static void no_ktrace(int);
                     55: static void usage(void);
1.1       deraadt    56:
1.25      miod       57: int    is_ltrace;
                     58:
1.10      deraadt    59: int
1.17      deraadt    60: main(int argc, char *argv[])
1.1       deraadt    61: {
                     62:        enum { NOTSET, CLEAR, CLEARALL } clear;
1.10      deraadt    63:        int append, ch, fd, inherit, ops, pidset, trpoints;
                     64:        pid_t pid;
1.25      miod       65:        char *tracefile, *tracespec;
1.3       deraadt    66:        mode_t omask;
1.6       angelos    67:        struct stat sb;
1.1       deraadt    68:
1.25      miod       69:        is_ltrace = strcmp(__progname, "ltrace") == 0;
                     70:
1.1       deraadt    71:        clear = NOTSET;
1.21      tedu       72:        append = ops = pidset = inherit = pid = 0;
1.25      miod       73:        trpoints = is_ltrace ? KTRFAC_USER : DEF_POINTS;
1.1       deraadt    74:        tracefile = DEF_TRACEFILE;
1.25      miod       75:        tracespec = NULL;
                     76:
                     77:        if (is_ltrace) {
1.28      miod       78:                while ((ch = getopt(argc, argv, "af:it:u:")) != -1)
1.25      miod       79:                        switch ((char)ch) {
                     80:                        case 'a':
                     81:                                append = 1;
                     82:                                break;
                     83:                        case 'f':
                     84:                                tracefile = optarg;
                     85:                                break;
                     86:                        case 'i':
                     87:                                inherit = 1;
1.28      miod       88:                                break;
                     89:                        case 't':
1.33      guenther   90:                                trpoints = getpoints(optarg, KTRFAC_USER);
1.28      miod       91:                                if (trpoints < 0) {
                     92:                                        warnx("unknown facility in %s", optarg);
                     93:                                        usage();
                     94:                                }
1.25      miod       95:                                break;
                     96:                        case 'u':
                     97:                                tracespec = optarg;
                     98:                                break;
                     99:                        default:
                    100:                                usage();
                    101:                        }
                    102:        } else {
1.37    ! tedu      103:                while ((ch = getopt(argc, argv, "aBCcdf:g:ip:t:T")) != -1)
1.25      miod      104:                        switch ((char)ch) {
                    105:                        case 'a':
                    106:                                append = 1;
                    107:                                break;
                    108:                        case 'B':
                    109:                                putenv("LD_BIND_NOW=");
                    110:                                break;
                    111:                        case 'C':
                    112:                                clear = CLEARALL;
1.35      tedu      113:                                tracefile = NULL;
1.25      miod      114:                                pidset = 1;
                    115:                                break;
                    116:                        case 'c':
                    117:                                clear = CLEAR;
                    118:                                break;
                    119:                        case 'd':
                    120:                                ops |= KTRFLAG_DESCEND;
                    121:                                break;
                    122:                        case 'f':
                    123:                                tracefile = optarg;
                    124:                                break;
                    125:                        case 'g':
                    126:                                pid = -rpid(optarg);
                    127:                                pidset = 1;
                    128:                                break;
                    129:                        case 'i':
                    130:                                inherit = 1;
                    131:                                break;
                    132:                        case 'p':
                    133:                                pid = rpid(optarg);
                    134:                                pidset = 1;
                    135:                                break;
                    136:                        case 't':
1.33      guenther  137:                                trpoints = getpoints(optarg, DEF_POINTS);
1.25      miod      138:                                if (trpoints < 0) {
                    139:                                        warnx("unknown facility in %s", optarg);
                    140:                                        usage();
                    141:                                }
                    142:                                break;
1.37    ! tedu      143:                        case 'T':
        !           144:                                putenv("LIBC_NOUSERTC=");
        !           145:                                break;
1.25      miod      146:                        default:
1.1       deraadt   147:                                usage();
                    148:                        }
1.25      miod      149:        }
                    150:
1.1       deraadt   151:        argv += optind;
                    152:        argc -= optind;
                    153:
1.15      deraadt   154:        if ((pidset && *argv) || (!pidset && !*argv && clear != CLEAR))
1.1       deraadt   155:                usage();
1.25      miod      156:
1.1       deraadt   157:        if (inherit)
                    158:                trpoints |= KTRFAC_INHERIT;
                    159:
                    160:        (void)signal(SIGSYS, no_ktrace);
                    161:        if (clear != NOTSET) {
                    162:                if (clear == CLEARALL) {
                    163:                        ops = KTROP_CLEAR | KTRFLAG_DESCEND;
                    164:                        trpoints = ALL_POINTS;
                    165:                        pid = 1;
                    166:                } else
                    167:                        ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
                    168:
1.36      deraadt   169:                if (ktrace(tracefile, ops, trpoints, pid) == -1) {
1.30      deraadt   170:                        if (errno == ESRCH)
                    171:                                err(1, "%d", pid);
1.9       millert   172:                        err(1, "%s", tracefile);
1.30      deraadt   173:                }
1.1       deraadt   174:                exit(0);
                    175:        }
                    176:
1.3       deraadt   177:        omask = umask(S_IRWXG|S_IRWXO);
1.5       deraadt   178:        if (append) {
1.36      deraadt   179:                if ((fd = open(tracefile, O_CREAT | O_WRONLY, DEFFILEMODE)) == -1)
1.9       millert   180:                        err(1, "%s", tracefile);
1.5       deraadt   181:                if (fstat(fd, &sb) != 0 || sb.st_uid != getuid())
                    182:                        errx(1, "Refuse to append to %s: not owned by you.",
                    183:                            tracefile);
                    184:        } else {
                    185:                if (unlink(tracefile) == -1 && errno != ENOENT)
                    186:                        err(1, "unlink %s", tracefile);
                    187:                if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY,
1.36      deraadt   188:                    DEFFILEMODE)) == -1)
1.9       millert   189:                        err(1, "%s", tracefile);
1.5       deraadt   190:        }
1.3       deraadt   191:        (void)umask(omask);
1.1       deraadt   192:        (void)close(fd);
                    193:
                    194:        if (*argv) {
1.25      miod      195:                if (is_ltrace) {
                    196:                        if (setenv("LD_TRACE_PLT", inherit ? "i" : "", 1) < 0)
                    197:                                err(1, "setenv(LD_TRACE_PLT)");
                    198:                        if (tracespec &&
                    199:                            setenv("LD_TRACE_PLTSPEC", tracespec, 1) < 0)
                    200:                                err(1, "setenv(LD_TRACE_PLTSPEC)");
                    201:                }
1.36      deraadt   202:                if (ktrace(tracefile, ops, trpoints, getpid()) == -1)
1.9       millert   203:                        err(1, "%s", tracefile);
1.1       deraadt   204:                execvp(argv[0], &argv[0]);
                    205:                err(1, "exec of '%s' failed", argv[0]);
                    206:        }
1.36      deraadt   207:        else if (ktrace(tracefile, ops, trpoints, pid) == -1) {
1.30      deraadt   208:                if (errno == ESRCH)
                    209:                        err(1, "%d", pid);
1.9       millert   210:                err(1, "%s", tracefile);
1.30      deraadt   211:        }
1.1       deraadt   212:        exit(0);
                    213: }
                    214:
1.11      espie     215: static int
1.17      deraadt   216: rpid(const char *p)
1.1       deraadt   217: {
1.32      deraadt   218:        const char *errstr;
1.1       deraadt   219:        static int first;
1.32      deraadt   220:        pid_t pid;
1.1       deraadt   221:
                    222:        if (first++) {
                    223:                warnx("only one -g or -p flag is permitted.");
                    224:                usage();
                    225:        }
                    226:        if (!*p) {
                    227:                warnx("illegal process id.");
                    228:                usage();
                    229:        }
1.32      deraadt   230:        pid = strtonum(p, 1, INT_MAX, &errstr);
                    231:        if (errstr) {
                    232:                warnx("illegal process id: %s", errstr);
                    233:                usage();
                    234:        }
                    235:        return pid;
1.1       deraadt   236: }
                    237:
1.11      espie     238: static void
1.17      deraadt   239: usage(void)
1.1       deraadt   240: {
1.25      miod      241:        if (is_ltrace)
1.29      jmc       242:                fprintf(stderr, "usage: %s [-ai] [-f trfile] [-t trstr]"
                    243:                    " [-u trspec] command\n",
1.25      miod      244:                    __progname);
                    245:        else
1.37    ! tedu      246:                fprintf(stderr, "usage: %s [-aCcdi] [-f trfile] [-g pgid]"
1.25      miod      247:                    " [-p pid] [-t trstr]\n"
1.37    ! tedu      248:                    "       %s [-aBdiT] [-f trfile] [-t trstr] command\n",
1.25      miod      249:                    __progname, __progname);
1.1       deraadt   250:        exit(1);
                    251: }
                    252:
1.18      deraadt   253: /* ARGSUSED */
1.11      espie     254: static void
1.17      deraadt   255: no_ktrace(int signo)
1.1       deraadt   256: {
1.34      awolk     257:        dprintf(STDERR_FILENO,
1.15      deraadt   258: "error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'option KTRACE'\n");
1.12      deraadt   259:        _exit(1);
1.1       deraadt   260: }