changed BINMODE= 6555 and CATMODE= 0664
[unix-history] / sys / kern / kern_ktrace.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
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 the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)kern_ktrace.c 7.15 (Berkeley) 6/21/91
34 */
35
36#ifdef KTRACE
37
38#include "param.h"
39#include "proc.h"
40#include "file.h"
41#include "namei.h"
42#include "vnode.h"
43#include "ktrace.h"
44#include "malloc.h"
45#include "syslog.h"
46
47struct ktr_header *
48ktrgetheader(type)
49{
50 register struct ktr_header *kth;
51 struct proc *p = curproc; /* XXX */
52
53 MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
54 M_TEMP, M_WAITOK);
55 kth->ktr_type = type;
56 microtime(&kth->ktr_time);
57 kth->ktr_pid = p->p_pid;
58 bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN);
59 return (kth);
60}
61
62ktrsyscall(vp, code, narg, args)
63 struct vnode *vp;
64 int code, narg, args[];
65{
66 struct ktr_header *kth = ktrgetheader(KTR_SYSCALL);
67 struct ktr_syscall *ktp;
68 register len = sizeof(struct ktr_syscall) + (narg * sizeof(int));
69 int *argp, i;
70
71 MALLOC(ktp, struct ktr_syscall *, len, M_TEMP, M_WAITOK);
72 ktp->ktr_code = code;
73 ktp->ktr_narg = narg;
74 argp = (int *)((char *)ktp + sizeof(struct ktr_syscall));
75 for (i = 0; i < narg; i++)
76 *argp++ = args[i];
77 kth->ktr_buf = (caddr_t)ktp;
78 kth->ktr_len = len;
79 ktrwrite(vp, kth);
80 FREE(ktp, M_TEMP);
81 FREE(kth, M_TEMP);
82}
83
84ktrsysret(vp, code, error, retval)
85 struct vnode *vp;
86 int code, error, retval;
87{
88 struct ktr_header *kth = ktrgetheader(KTR_SYSRET);
89 struct ktr_sysret ktp;
90
91 ktp.ktr_code = code;
92 ktp.ktr_error = error;
93 ktp.ktr_retval = retval; /* what about val2 ? */
94
95 kth->ktr_buf = (caddr_t)&ktp;
96 kth->ktr_len = sizeof(struct ktr_sysret);
97
98 ktrwrite(vp, kth);
99 FREE(kth, M_TEMP);
100}
101
102ktrnamei(vp, path)
103 struct vnode *vp;
104 char *path;
105{
106 struct ktr_header *kth = ktrgetheader(KTR_NAMEI);
107
108 kth->ktr_len = strlen(path);
109 kth->ktr_buf = path;
110
111 ktrwrite(vp, kth);
112 FREE(kth, M_TEMP);
113}
114
115ktrgenio(vp, fd, rw, iov, len, error)
116 struct vnode *vp;
117 int fd;
118 enum uio_rw rw;
119 register struct iovec *iov;
120{
121 struct ktr_header *kth = ktrgetheader(KTR_GENIO);
122 register struct ktr_genio *ktp;
123 register caddr_t cp;
124 register int resid = len, cnt;
125
126 if (error)
127 return;
128 MALLOC(ktp, struct ktr_genio *, sizeof(struct ktr_genio) + len,
129 M_TEMP, M_WAITOK);
130 ktp->ktr_fd = fd;
131 ktp->ktr_rw = rw;
132 cp = (caddr_t)((char *)ktp + sizeof (struct ktr_genio));
133 while (resid > 0) {
134 if ((cnt = iov->iov_len) > resid)
135 cnt = resid;
136 if (copyin(iov->iov_base, cp, (unsigned)cnt))
137 goto done;
138 cp += cnt;
139 resid -= cnt;
140 iov++;
141 }
142 kth->ktr_buf = (caddr_t)ktp;
143 kth->ktr_len = sizeof (struct ktr_genio) + len;
144
145 ktrwrite(vp, kth);
146done:
147 FREE(kth, M_TEMP);
148 FREE(ktp, M_TEMP);
149}
150
151ktrpsig(vp, sig, action, mask, code)
152 struct vnode *vp;
153 sig_t action;
154{
155 struct ktr_header *kth = ktrgetheader(KTR_PSIG);
156 struct ktr_psig kp;
157
158 kp.signo = (char)sig;
159 kp.action = action;
160 kp.mask = mask;
161 kp.code = code;
162 kth->ktr_buf = (caddr_t)&kp;
163 kth->ktr_len = sizeof (struct ktr_psig);
164
165 ktrwrite(vp, kth);
166 FREE(kth, M_TEMP);
167}
168
169/* Interface and common routines */
170
171/*
172 * ktrace system call
173 */
3c7eb27c
DG
174
175struct ktrace_args {
176 char *fname;
177 int ops;
178 int facs;
179 int pid;
180};
181
15637ed4
RG
182/* ARGSUSED */
183ktrace(curp, uap, retval)
184 struct proc *curp;
3c7eb27c 185 register struct ktrace_args *uap;
15637ed4
RG
186 int *retval;
187{
188 register struct vnode *vp = NULL;
189 register struct proc *p;
190 struct pgrp *pg;
191 int facs = uap->facs & ~KTRFAC_ROOT;
192 int ops = KTROP(uap->ops);
193 int descend = uap->ops & KTRFLAG_DESCEND;
194 int ret = 0;
195 int error = 0;
196 struct nameidata nd;
197
198 if (ops != KTROP_CLEAR) {
199 /*
200 * an operation which requires a file argument.
201 */
202 nd.ni_segflg = UIO_USERSPACE;
203 nd.ni_dirp = uap->fname;
204 if (error = vn_open(&nd, curp, FREAD|FWRITE, 0))
205 return (error);
206 vp = nd.ni_vp;
207 VOP_UNLOCK(vp);
208 if (vp->v_type != VREG) {
209 (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
210 return (EACCES);
211 }
212 }
213 /*
214 * Clear all uses of the tracefile
215 */
216 if (ops == KTROP_CLEARFILE) {
217 for (p = allproc; p != NULL; p = p->p_nxt) {
218 if (p->p_tracep == vp) {
219 if (ktrcanset(curp, p)) {
220 p->p_tracep = NULL;
221 p->p_traceflag = 0;
222 (void) vn_close(vp, FREAD|FWRITE,
223 p->p_ucred, p);
224 } else
225 error = EPERM;
226 }
227 }
228 goto done;
229 }
230 /*
231 * need something to (un)trace (XXX - why is this here?)
232 */
233 if (!facs) {
234 error = EINVAL;
235 goto done;
236 }
237 /*
238 * do it
239 */
240 if (uap->pid < 0) {
241 /*
242 * by process group
243 */
244 pg = pgfind(-uap->pid);
245 if (pg == NULL) {
246 error = ESRCH;
247 goto done;
248 }
249 for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt)
250 if (descend)
251 ret |= ktrsetchildren(curp, p, ops, facs, vp);
252 else
253 ret |= ktrops(curp, p, ops, facs, vp);
254
255 } else {
256 /*
257 * by pid
258 */
259 p = pfind(uap->pid);
260 if (p == NULL) {
261 error = ESRCH;
262 goto done;
263 }
264 if (descend)
265 ret |= ktrsetchildren(curp, p, ops, facs, vp);
266 else
267 ret |= ktrops(curp, p, ops, facs, vp);
268 }
269 if (!ret)
270 error = EPERM;
271done:
272 if (vp != NULL)
273 (void) vn_close(vp, FWRITE, curp->p_ucred, curp);
274 return (error);
275}
276
277ktrops(curp, p, ops, facs, vp)
278 struct proc *curp, *p;
279 struct vnode *vp;
280{
281
282 if (!ktrcanset(curp, p))
283 return (0);
284 if (ops == KTROP_SET) {
285 if (p->p_tracep != vp) {
286 /*
287 * if trace file already in use, relinquish
288 */
289 if (p->p_tracep != NULL)
290 vrele(p->p_tracep);
291 VREF(vp);
292 p->p_tracep = vp;
293 }
294 p->p_traceflag |= facs;
295 if (curp->p_ucred->cr_uid == 0)
296 p->p_traceflag |= KTRFAC_ROOT;
297 } else {
298 /* KTROP_CLEAR */
299 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
300 /* no more tracing */
301 p->p_traceflag = 0;
302 if (p->p_tracep != NULL) {
303 vrele(p->p_tracep);
304 p->p_tracep = NULL;
305 }
306 }
307 }
308
309 return (1);
310}
311
312ktrsetchildren(curp, top, ops, facs, vp)
313 struct proc *curp, *top;
314 struct vnode *vp;
315{
316 register struct proc *p;
317 register int ret = 0;
318
319 p = top;
320 for (;;) {
321 ret |= ktrops(curp, p, ops, facs, vp);
322 /*
323 * If this process has children, descend to them next,
324 * otherwise do any siblings, and if done with this level,
325 * follow back up the tree (but not past top).
326 */
327 if (p->p_cptr)
328 p = p->p_cptr;
329 else if (p == top)
330 return (ret);
331 else if (p->p_osptr)
332 p = p->p_osptr;
333 else for (;;) {
334 p = p->p_pptr;
335 if (p == top)
336 return (ret);
337 if (p->p_osptr) {
338 p = p->p_osptr;
339 break;
340 }
341 }
342 }
343 /*NOTREACHED*/
344}
345
346ktrwrite(vp, kth)
347 struct vnode *vp;
348 register struct ktr_header *kth;
349{
350 struct uio auio;
351 struct iovec aiov[2];
352 register struct proc *p = curproc; /* XXX */
353 int error;
354
355 if (vp == NULL)
356 return;
357 auio.uio_iov = &aiov[0];
358 auio.uio_offset = 0;
359 auio.uio_segflg = UIO_SYSSPACE;
360 auio.uio_rw = UIO_WRITE;
361 aiov[0].iov_base = (caddr_t)kth;
362 aiov[0].iov_len = sizeof(struct ktr_header);
363 auio.uio_resid = sizeof(struct ktr_header);
364 auio.uio_iovcnt = 1;
365 auio.uio_procp = (struct proc *)0;
366 if (kth->ktr_len > 0) {
367 auio.uio_iovcnt++;
368 aiov[1].iov_base = kth->ktr_buf;
369 aiov[1].iov_len = kth->ktr_len;
370 auio.uio_resid += kth->ktr_len;
371 }
372 VOP_LOCK(vp);
373 error = VOP_WRITE(vp, &auio, IO_UNIT|IO_APPEND, p->p_ucred);
374 VOP_UNLOCK(vp);
375 if (!error)
376 return;
377 /*
378 * If error encountered, give up tracing on this vnode.
379 */
380 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
381 error);
382 for (p = allproc; p != NULL; p = p->p_nxt) {
383 if (p->p_tracep == vp) {
384 p->p_tracep = NULL;
385 p->p_traceflag = 0;
386 vrele(vp);
387 }
388 }
389}
390
391/*
392 * Return true if caller has permission to set the ktracing state
393 * of target. Essentially, the target can't possess any
394 * more permissions than the caller. KTRFAC_ROOT signifies that
395 * root previously set the tracing status on the target process, and
396 * so, only root may further change it.
397 *
398 * TODO: check groups. use caller effective gid.
399 */
400ktrcanset(callp, targetp)
401 struct proc *callp, *targetp;
402{
403 register struct pcred *caller = callp->p_cred;
404 register struct pcred *target = targetp->p_cred;
405
406 if ((caller->pc_ucred->cr_uid == target->p_ruid &&
407 target->p_ruid == target->p_svuid &&
408 caller->p_rgid == target->p_rgid && /* XXX */
409 target->p_rgid == target->p_svgid &&
410 (targetp->p_traceflag & KTRFAC_ROOT) == 0) ||
411 caller->pc_ucred->cr_uid == 0)
412 return (1);
413
414 return (0);
415}
416
417#endif