Annotation of src/usr.bin/ktrace/ktrace.c, Revision 1.25
1.25 ! miod 1: /* $OpenBSD: ktrace.c,v 1.24 2012/07/12 18:03:29 jmc 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:
33: #include <sys/param.h>
34: #include <sys/stat.h>
35: #include <sys/time.h>
36: #include <sys/uio.h>
37: #include <sys/ktrace.h>
38:
39: #include <err.h>
1.19 millert 40: #include <errno.h>
1.8 millert 41: #include <fcntl.h>
1.10 deraadt 42: #include <stdlib.h>
1.1 deraadt 43: #include <stdio.h>
1.14 deraadt 44: #include <string.h>
1.1 deraadt 45: #include <unistd.h>
46:
47: #include "ktrace.h"
1.11 espie 48: #include "extern.h"
1.1 deraadt 49:
1.25 ! miod 50: extern char *__progname;
! 51:
1.13 millert 52: static int rpid(const char *);
53: static void no_ktrace(int);
54: static void usage(void);
1.1 deraadt 55:
1.25 ! miod 56: int is_ltrace;
! 57:
1.10 deraadt 58: int
1.17 deraadt 59: main(int argc, char *argv[])
1.1 deraadt 60: {
61: enum { NOTSET, CLEAR, CLEARALL } clear;
1.10 deraadt 62: int append, ch, fd, inherit, ops, pidset, trpoints;
63: pid_t pid;
1.25 ! miod 64: char *tracefile, *tracespec;
1.3 deraadt 65: mode_t omask;
1.6 angelos 66: struct stat sb;
1.1 deraadt 67:
1.25 ! miod 68: is_ltrace = strcmp(__progname, "ltrace") == 0;
! 69:
1.1 deraadt 70: clear = NOTSET;
1.21 tedu 71: append = ops = pidset = inherit = pid = 0;
1.25 ! miod 72: trpoints = is_ltrace ? KTRFAC_USER : DEF_POINTS;
1.1 deraadt 73: tracefile = DEF_TRACEFILE;
1.25 ! miod 74: tracespec = NULL;
! 75:
! 76: if (is_ltrace) {
! 77: while ((ch = getopt(argc, argv, "af:iu:")) != -1)
! 78: switch ((char)ch) {
! 79: case 'a':
! 80: append = 1;
! 81: break;
! 82: case 'f':
! 83: tracefile = optarg;
! 84: break;
! 85: case 'i':
! 86: inherit = 1;
! 87: break;
! 88: case 'u':
! 89: tracespec = optarg;
! 90: break;
! 91: default:
! 92: usage();
! 93: }
! 94: } else {
! 95: while ((ch = getopt(argc, argv, "aBCcdf:g:ip:t:")) != -1)
! 96: switch ((char)ch) {
! 97: case 'a':
! 98: append = 1;
! 99: break;
! 100: case 'B':
! 101: putenv("LD_BIND_NOW=");
! 102: break;
! 103: case 'C':
! 104: clear = CLEARALL;
! 105: pidset = 1;
! 106: break;
! 107: case 'c':
! 108: clear = CLEAR;
! 109: break;
! 110: case 'd':
! 111: ops |= KTRFLAG_DESCEND;
! 112: break;
! 113: case 'f':
! 114: tracefile = optarg;
! 115: break;
! 116: case 'g':
! 117: pid = -rpid(optarg);
! 118: pidset = 1;
! 119: break;
! 120: case 'i':
! 121: inherit = 1;
! 122: break;
! 123: case 'p':
! 124: pid = rpid(optarg);
! 125: pidset = 1;
! 126: break;
! 127: case 't':
! 128: trpoints = getpoints(optarg);
! 129: if (trpoints < 0) {
! 130: warnx("unknown facility in %s", optarg);
! 131: usage();
! 132: }
! 133: break;
! 134: default:
1.1 deraadt 135: usage();
136: }
1.25 ! miod 137: }
! 138:
1.1 deraadt 139: argv += optind;
140: argc -= optind;
141:
1.15 deraadt 142: if ((pidset && *argv) || (!pidset && !*argv && clear != CLEAR))
1.1 deraadt 143: usage();
1.25 ! miod 144:
1.1 deraadt 145: if (inherit)
146: trpoints |= KTRFAC_INHERIT;
147:
148: (void)signal(SIGSYS, no_ktrace);
149: if (clear != NOTSET) {
150: if (clear == CLEARALL) {
151: ops = KTROP_CLEAR | KTRFLAG_DESCEND;
152: trpoints = ALL_POINTS;
153: pid = 1;
154: } else
155: ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
156:
157: if (ktrace(tracefile, ops, trpoints, pid) < 0)
1.9 millert 158: err(1, "%s", tracefile);
1.1 deraadt 159: exit(0);
160: }
161:
1.3 deraadt 162: omask = umask(S_IRWXG|S_IRWXO);
1.5 deraadt 163: if (append) {
164: if ((fd = open(tracefile, O_CREAT | O_WRONLY, DEFFILEMODE)) < 0)
1.9 millert 165: err(1, "%s", tracefile);
1.5 deraadt 166: if (fstat(fd, &sb) != 0 || sb.st_uid != getuid())
167: errx(1, "Refuse to append to %s: not owned by you.",
168: tracefile);
169: } else {
170: if (unlink(tracefile) == -1 && errno != ENOENT)
171: err(1, "unlink %s", tracefile);
172: if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY,
173: DEFFILEMODE)) < 0)
1.9 millert 174: err(1, "%s", tracefile);
1.5 deraadt 175: }
1.3 deraadt 176: (void)umask(omask);
1.1 deraadt 177: (void)close(fd);
178:
179: if (*argv) {
1.25 ! miod 180: if (is_ltrace) {
! 181: if (setenv("LD_TRACE_PLT", inherit ? "i" : "", 1) < 0)
! 182: err(1, "setenv(LD_TRACE_PLT)");
! 183: if (tracespec &&
! 184: setenv("LD_TRACE_PLTSPEC", tracespec, 1) < 0)
! 185: err(1, "setenv(LD_TRACE_PLTSPEC)");
! 186: }
1.1 deraadt 187: if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
1.9 millert 188: err(1, "%s", tracefile);
1.1 deraadt 189: execvp(argv[0], &argv[0]);
190: err(1, "exec of '%s' failed", argv[0]);
191: }
192: else if (ktrace(tracefile, ops, trpoints, pid) < 0)
1.9 millert 193: err(1, "%s", tracefile);
1.1 deraadt 194: exit(0);
195: }
196:
1.11 espie 197: static int
1.17 deraadt 198: rpid(const char *p)
1.1 deraadt 199: {
200: static int first;
201:
202: if (first++) {
203: warnx("only one -g or -p flag is permitted.");
204: usage();
205: }
206: if (!*p) {
207: warnx("illegal process id.");
208: usage();
209: }
210: return(atoi(p));
211: }
212:
1.11 espie 213: static void
1.17 deraadt 214: usage(void)
1.1 deraadt 215: {
1.25 ! miod 216: if (is_ltrace)
! 217: fprintf(stderr, "usage: %s [-ai] [-f trfile] [-u trspec]"
! 218: " comand\n",
! 219: __progname);
! 220: else
! 221: fprintf(stderr, "usage: %s [-aBCcdi] [-f trfile] [-g pgid]"
! 222: " [-p pid] [-t trstr]\n"
! 223: " %s [-adi] [-f trfile] [-t trstr] command\n",
! 224: __progname, __progname);
1.1 deraadt 225: exit(1);
226: }
227:
1.18 deraadt 228: /* ARGSUSED */
1.11 espie 229: static void
1.17 deraadt 230: no_ktrace(int signo)
1.1 deraadt 231: {
1.12 deraadt 232: char buf[8192];
233:
234: snprintf(buf, sizeof(buf),
1.15 deraadt 235: "error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'option KTRACE'\n");
1.12 deraadt 236: write(STDERR_FILENO, buf, strlen(buf));
237: _exit(1);
1.1 deraadt 238: }