kern_ktrace.c revision 1.82 1 /* $NetBSD: kern_ktrace.c,v 1.82 2003/11/12 21:07:38 dsl Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.82 2003/11/12 21:07:38 dsl Exp $");
36
37 #include "opt_ktrace.h"
38 #include "opt_compat_mach.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/file.h>
44 #include <sys/namei.h>
45 #include <sys/vnode.h>
46 #include <sys/ktrace.h>
47 #include <sys/malloc.h>
48 #include <sys/syslog.h>
49 #include <sys/filedesc.h>
50 #include <sys/ioctl.h>
51
52 #include <sys/mount.h>
53 #include <sys/sa.h>
54 #include <sys/syscallargs.h>
55
56 #ifdef KTRACE
57
58 int ktrace_common(struct proc *, int, int, int, struct file *);
59 void ktrinitheader(struct ktr_header *, struct proc *, int);
60 int ktrops(struct proc *, struct proc *, int, int, struct file *);
61 int ktrsetchildren(struct proc *, struct proc *, int, int,
62 struct file *);
63 int ktrwrite(struct proc *, struct ktr_header *);
64 int ktrcanset(struct proc *, struct proc *);
65 int ktrsamefile(struct file *, struct file *);
66
67 /*
68 * "deep" compare of two files for the purposes of clearing a trace.
69 * Returns true if they're the same open file, or if they point at the
70 * same underlying vnode/socket.
71 */
72
73 int
74 ktrsamefile(f1, f2)
75 struct file *f1;
76 struct file *f2;
77 {
78 return ((f1 == f2) ||
79 ((f1 != NULL) && (f2 != NULL) &&
80 (f1->f_type == f2->f_type) &&
81 (f1->f_data == f2->f_data)));
82 }
83
84 void
85 ktrderef(p)
86 struct proc *p;
87 {
88 struct file *fp = p->p_tracep;
89 p->p_traceflag = 0;
90 if (fp == NULL)
91 return;
92 simple_lock(&fp->f_slock);
93 FILE_USE(fp);
94
95 /*
96 * ktrace file descriptor can't be watched (are not visible to
97 * userspace), so no kqueue stuff here
98 */
99 closef(fp, NULL);
100
101 p->p_tracep = NULL;
102 }
103
104 void
105 ktradref(p)
106 struct proc *p;
107 {
108 struct file *fp = p->p_tracep;
109
110 fp->f_count++;
111 }
112
113 void
114 ktrinitheader(kth, p, type)
115 struct ktr_header *kth;
116 struct proc *p;
117 int type;
118 {
119
120 memset(kth, 0, sizeof(*kth));
121 kth->ktr_type = type;
122 microtime(&kth->ktr_time);
123 kth->ktr_pid = p->p_pid;
124 memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
125 }
126
127 void
128 ktrsyscall(p, code, realcode, callp, args)
129 struct proc *p;
130 register_t code;
131 register_t realcode;
132 const struct sysent *callp;
133 register_t args[];
134 {
135 struct ktr_header kth;
136 struct ktr_syscall *ktp;
137 register_t *argp;
138 int argsize;
139 size_t len;
140 u_int i;
141
142 if (callp == NULL)
143 callp = p->p_emul->e_sysent;
144
145 argsize = callp[code].sy_argsize;
146 len = sizeof(struct ktr_syscall) + argsize;
147
148 p->p_traceflag |= KTRFAC_ACTIVE;
149 ktrinitheader(&kth, p, KTR_SYSCALL);
150 ktp = malloc(len, M_TEMP, M_WAITOK);
151 ktp->ktr_code = realcode;
152 ktp->ktr_argsize = argsize;
153 argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
154 for (i = 0; i < (argsize / sizeof(*argp)); i++)
155 *argp++ = args[i];
156 kth.ktr_buf = (caddr_t)ktp;
157 kth.ktr_len = len;
158 (void) ktrwrite(p, &kth);
159 free(ktp, M_TEMP);
160 p->p_traceflag &= ~KTRFAC_ACTIVE;
161 }
162
163 void
164 ktrsysret(p, code, error, retval)
165 struct proc *p;
166 register_t code;
167 int error;
168 register_t *retval;
169 {
170 struct ktr_header kth;
171 struct ktr_sysret ktp;
172
173 p->p_traceflag |= KTRFAC_ACTIVE;
174 ktrinitheader(&kth, p, KTR_SYSRET);
175 ktp.ktr_code = code;
176 ktp.ktr_eosys = 0; /* XXX unused */
177 ktp.ktr_error = error;
178 ktp.ktr_retval = retval ? retval[0] : 0;
179 ktp.ktr_retval_1 = retval ? retval[1] : 0;
180
181 kth.ktr_buf = (caddr_t)&ktp;
182 kth.ktr_len = sizeof(struct ktr_sysret);
183
184 (void) ktrwrite(p, &kth);
185 p->p_traceflag &= ~KTRFAC_ACTIVE;
186 }
187
188 void
189 ktrnamei(p, path)
190 struct proc *p;
191 char *path;
192 {
193 struct ktr_header kth;
194
195 p->p_traceflag |= KTRFAC_ACTIVE;
196 ktrinitheader(&kth, p, KTR_NAMEI);
197 kth.ktr_len = strlen(path);
198 kth.ktr_buf = path;
199
200 (void) ktrwrite(p, &kth);
201 p->p_traceflag &= ~KTRFAC_ACTIVE;
202 }
203
204 void
205 ktremul(p)
206 struct proc *p;
207 {
208 struct ktr_header kth;
209 const char *emul = p->p_emul->e_name;
210
211 p->p_traceflag |= KTRFAC_ACTIVE;
212 ktrinitheader(&kth, p, KTR_EMUL);
213 kth.ktr_len = strlen(emul);
214 kth.ktr_buf = (caddr_t)emul;
215
216 (void) ktrwrite(p, &kth);
217 p->p_traceflag &= ~KTRFAC_ACTIVE;
218 }
219
220 void
221 ktrkmem(struct proc *p, int ktr, const void *buf, size_t len)
222 {
223 struct ktr_header kth;
224
225 p->p_traceflag |= KTRFAC_ACTIVE;
226 ktrinitheader(&kth, p, ktr);
227 kth.ktr_len = len;
228 kth.ktr_buf = buf;
229
230 (void)ktrwrite(p, &kth);
231 p->p_traceflag &= ~KTRFAC_ACTIVE;
232 }
233
234 void
235 ktrgenio(p, fd, rw, iov, len, error)
236 struct proc *p;
237 int fd;
238 enum uio_rw rw;
239 struct iovec *iov;
240 int len;
241 int error;
242 {
243 struct ktr_header kth;
244 struct ktr_genio *ktp;
245 caddr_t cp;
246 int resid = len, cnt;
247 int buflen;
248
249 if (error)
250 return;
251
252 p->p_traceflag |= KTRFAC_ACTIVE;
253
254 buflen = min(PAGE_SIZE, len + sizeof(struct ktr_genio));
255
256 ktrinitheader(&kth, p, KTR_GENIO);
257 ktp = malloc(buflen, M_TEMP, M_WAITOK);
258 ktp->ktr_fd = fd;
259 ktp->ktr_rw = rw;
260
261 kth.ktr_buf = (caddr_t)ktp;
262
263 cp = (caddr_t)((char *)ktp + sizeof(struct ktr_genio));
264 buflen -= sizeof(struct ktr_genio);
265
266 while (resid > 0) {
267 #if 0 /* XXX NJWLWP */
268 KDASSERT(p->p_cpu != NULL);
269 KDASSERT(p->p_cpu == curcpu());
270 #endif
271 /* XXX NJWLWP */
272 if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
273 preempt(1);
274
275 cnt = min(iov->iov_len, buflen);
276 if (cnt > resid)
277 cnt = resid;
278 if (copyin(iov->iov_base, cp, cnt))
279 break;
280
281 kth.ktr_len = cnt + sizeof(struct ktr_genio);
282
283 if (__predict_false(ktrwrite(p, &kth) != 0))
284 break;
285
286 iov->iov_base = (caddr_t)iov->iov_base + cnt;
287 iov->iov_len -= cnt;
288
289 if (iov->iov_len == 0)
290 iov++;
291
292 resid -= cnt;
293 }
294
295 free(ktp, M_TEMP);
296 p->p_traceflag &= ~KTRFAC_ACTIVE;
297 }
298
299 void
300 ktrpsig(p, sig, action, mask, ksi)
301 struct proc *p;
302 int sig;
303 sig_t action;
304 const sigset_t *mask;
305 const ksiginfo_t *ksi;
306 {
307 struct ktr_header kth;
308 struct {
309 struct ktr_psig kp;
310 siginfo_t si;
311 } kbuf;
312
313 p->p_traceflag |= KTRFAC_ACTIVE;
314 ktrinitheader(&kth, p, KTR_PSIG);
315 kbuf.kp.signo = (char)sig;
316 kbuf.kp.action = action;
317 kbuf.kp.mask = *mask;
318 kth.ktr_buf = (caddr_t)&kbuf;
319 if (ksi) {
320 kbuf.kp.code = KSI_TRAPCODE(ksi);
321 (void)memset(&kbuf.si, 0, sizeof(kbuf.si));
322 kbuf.si._info = ksi->ksi_info;
323 kth.ktr_len = sizeof(kbuf);
324 } else {
325 kbuf.kp.code = 0;
326 kth.ktr_len = sizeof(struct ktr_psig);
327 }
328 (void) ktrwrite(p, &kth);
329 p->p_traceflag &= ~KTRFAC_ACTIVE;
330 }
331
332 void
333 ktrcsw(p, out, user)
334 struct proc *p;
335 int out;
336 int user;
337 {
338 struct ktr_header kth;
339 struct ktr_csw kc;
340
341 p->p_traceflag |= KTRFAC_ACTIVE;
342 ktrinitheader(&kth, p, KTR_CSW);
343 kc.out = out;
344 kc.user = user;
345 kth.ktr_buf = (caddr_t)&kc;
346 kth.ktr_len = sizeof(struct ktr_csw);
347
348 (void) ktrwrite(p, &kth);
349 p->p_traceflag &= ~KTRFAC_ACTIVE;
350 }
351
352 void
353 ktruser(p, id, addr, len, ustr)
354 struct proc *p;
355 const char *id;
356 void *addr;
357 size_t len;
358 int ustr;
359 {
360 struct ktr_header kth;
361 struct ktr_user *ktp;
362 caddr_t user_dta;
363
364 p->p_traceflag |= KTRFAC_ACTIVE;
365 ktrinitheader(&kth, p, KTR_USER);
366 ktp = malloc(sizeof(struct ktr_user) + len, M_TEMP, M_WAITOK);
367 if (ustr) {
368 if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
369 ktp->ktr_id[0] = '\0';
370 } else
371 strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
372 ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
373
374 user_dta = (caddr_t) ((char *)ktp + sizeof(struct ktr_user));
375 if (copyin(addr, (void *) user_dta, len) != 0)
376 len = 0;
377
378 kth.ktr_buf = (void *)ktp;
379 kth.ktr_len = sizeof(struct ktr_user) + len;
380 (void) ktrwrite(p, &kth);
381
382 free(ktp, M_TEMP);
383 p->p_traceflag &= ~KTRFAC_ACTIVE;
384
385 }
386
387 void
388 ktrmmsg(p, msgh, size)
389 struct proc *p;
390 const void *msgh;
391 size_t size;
392 {
393 struct ktr_header kth;
394 struct ktr_mmsg *kp;
395
396 p->p_traceflag |= KTRFAC_ACTIVE;
397 ktrinitheader(&kth, p, KTR_MMSG);
398
399 kp = (struct ktr_mmsg *)msgh;
400 kth.ktr_buf = (caddr_t)kp;
401 kth.ktr_len = size;
402 (void) ktrwrite(p, &kth);
403 p->p_traceflag &= ~KTRFAC_ACTIVE;
404 }
405
406 /* Interface and common routines */
407
408 int
409 ktrace_common(curp, ops, facs, pid, fp)
410 struct proc *curp;
411 int ops;
412 int facs;
413 int pid;
414 struct file *fp;
415 {
416 int ret = 0;
417 int error = 0;
418 int one = 1;
419 int descend;
420 struct proc *p;
421 struct pgrp *pg;
422
423 curp->p_traceflag |= KTRFAC_ACTIVE;
424 descend = ops & KTRFLAG_DESCEND;
425 facs = facs & ~((unsigned) KTRFAC_ROOT);
426
427 /*
428 * Clear all uses of the tracefile
429 */
430 if (KTROP(ops) == KTROP_CLEARFILE) {
431 proclist_lock_read();
432 LIST_FOREACH(p, &allproc, p_list) {
433 if (ktrsamefile(p->p_tracep, fp)) {
434 if (ktrcanset(curp, p))
435 ktrderef(p);
436 else
437 error = EPERM;
438 }
439 }
440 proclist_unlock_read();
441 goto done;
442 }
443
444 /*
445 * Mark fp non-blocking, to avoid problems from possible deadlocks.
446 */
447
448 if (fp != NULL) {
449 fp->f_flag |= FNONBLOCK;
450 (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&one, curp);
451 }
452
453 /*
454 * need something to (un)trace (XXX - why is this here?)
455 */
456 if (!facs) {
457 error = EINVAL;
458 goto done;
459 }
460 /*
461 * do it
462 */
463 if (pid < 0) {
464 /*
465 * by process group
466 */
467 pg = pg_find(-pid, PFIND_UNLOCK_FAIL);
468 if (pg == NULL) {
469 error = ESRCH;
470 goto done;
471 }
472 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
473 if (descend)
474 ret |= ktrsetchildren(curp, p, ops, facs, fp);
475 else
476 ret |= ktrops(curp, p, ops, facs, fp);
477 }
478
479 } else {
480 /*
481 * by pid
482 */
483 p = p_find(pid, PFIND_UNLOCK_FAIL);
484 if (p == NULL) {
485 error = ESRCH;
486 goto done;
487 }
488 if (descend)
489 ret |= ktrsetchildren(curp, p, ops, facs, fp);
490 else
491 ret |= ktrops(curp, p, ops, facs, fp);
492 }
493 proclist_unlock_read(); /* taken by p{g}_find */
494 if (!ret)
495 error = EPERM;
496 done:
497 curp->p_traceflag &= ~KTRFAC_ACTIVE;
498 return (error);
499 }
500
501 /*
502 * ktrace system call
503 */
504 /* ARGSUSED */
505 int
506 sys_fktrace(l, v, retval)
507 struct lwp *l;
508 void *v;
509 register_t *retval;
510 {
511 struct sys_fktrace_args /* {
512 syscallarg(int) fd;
513 syscallarg(int) ops;
514 syscallarg(int) facs;
515 syscallarg(int) pid;
516 } */ *uap = v;
517 struct proc *curp = l->l_proc;
518 struct file *fp = NULL;
519 struct filedesc *fdp = curp->p_fd;
520 int error;
521
522 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
523 return (EBADF);
524
525 FILE_USE(fp);
526
527 if ((fp->f_flag & FWRITE) == 0)
528 error = EBADF;
529 else
530 error = ktrace_common(curp, SCARG(uap, ops),
531 SCARG(uap, facs), SCARG(uap, pid), fp);
532
533 FILE_UNUSE(fp, curp);
534
535 return error;
536 }
537
538 /*
539 * ktrace system call
540 */
541 /* ARGSUSED */
542 int
543 sys_ktrace(l, v, retval)
544 struct lwp *l;
545 void *v;
546 register_t *retval;
547 {
548 struct sys_ktrace_args /* {
549 syscallarg(const char *) fname;
550 syscallarg(int) ops;
551 syscallarg(int) facs;
552 syscallarg(int) pid;
553 } */ *uap = v;
554 struct proc *curp = l->l_proc;
555 struct vnode *vp = NULL;
556 struct file *fp = NULL;
557 int fd;
558 int ops = SCARG(uap, ops);
559 int error = 0;
560 struct nameidata nd;
561
562 ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
563
564 curp->p_traceflag |= KTRFAC_ACTIVE;
565 if ((ops & KTROP_CLEAR) == 0) {
566 /*
567 * an operation which requires a file argument.
568 */
569 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
570 curp);
571 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
572 curp->p_traceflag &= ~KTRFAC_ACTIVE;
573 return (error);
574 }
575 vp = nd.ni_vp;
576 VOP_UNLOCK(vp, 0);
577 if (vp->v_type != VREG) {
578 (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
579 curp->p_traceflag &= ~KTRFAC_ACTIVE;
580 return (EACCES);
581 }
582 /*
583 * XXX This uses up a file descriptor slot in the
584 * tracing process for the duration of this syscall.
585 * This is not expected to be a problem. If
586 * falloc(NULL, ...) DTRT we could skip that part, but
587 * that would require changing its interface to allow
588 * the caller to pass in a ucred..
589 *
590 * This will FILE_USE the fp it returns, if any.
591 * Keep it in use until we return.
592 */
593 if ((error = falloc(curp, &fp, &fd)) != 0)
594 goto done;
595
596 fp->f_flag = FWRITE|FAPPEND;
597 fp->f_type = DTYPE_VNODE;
598 fp->f_ops = &vnops;
599 fp->f_data = (caddr_t)vp;
600 FILE_SET_MATURE(fp);
601 vp = NULL;
602 }
603 error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
604 SCARG(uap, pid), fp);
605 done:
606 if (vp != NULL)
607 (void) vn_close(vp, FWRITE, curp->p_ucred, curp);
608 if (fp != NULL) {
609 FILE_UNUSE(fp, curp); /* release file */
610 fdrelease(curp, fd); /* release fd table slot */
611 }
612 return (error);
613 }
614
615 int
616 ktrops(curp, p, ops, facs, fp)
617 struct proc *curp;
618 struct proc *p;
619 int ops;
620 int facs;
621 struct file *fp;
622 {
623
624 if (!ktrcanset(curp, p))
625 return (0);
626 if (KTROP(ops) == KTROP_SET) {
627 if (p->p_tracep != fp) {
628 /*
629 * if trace file already in use, relinquish
630 */
631 ktrderef(p);
632 p->p_tracep = fp;
633 ktradref(p);
634 }
635 p->p_traceflag |= facs;
636 if (curp->p_ucred->cr_uid == 0)
637 p->p_traceflag |= KTRFAC_ROOT;
638 } else {
639 /* KTROP_CLEAR */
640 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
641 /* no more tracing */
642 ktrderef(p);
643 }
644 }
645
646 /*
647 * Emit an emulation record, every time there is a ktrace
648 * change/attach request.
649 */
650 if (KTRPOINT(p, KTR_EMUL))
651 ktremul(p);
652 #ifdef __HAVE_SYSCALL_INTERN
653 (*p->p_emul->e_syscall_intern)(p);
654 #endif
655
656 return (1);
657 }
658
659 int
660 ktrsetchildren(curp, top, ops, facs, fp)
661 struct proc *curp;
662 struct proc *top;
663 int ops;
664 int facs;
665 struct file *fp;
666 {
667 struct proc *p;
668 int ret = 0;
669
670 p = top;
671 for (;;) {
672 ret |= ktrops(curp, p, ops, facs, fp);
673 /*
674 * If this process has children, descend to them next,
675 * otherwise do any siblings, and if done with this level,
676 * follow back up the tree (but not past top).
677 */
678 if (LIST_FIRST(&p->p_children) != NULL) {
679 p = LIST_FIRST(&p->p_children);
680 continue;
681 }
682 for (;;) {
683 if (p == top)
684 return (ret);
685 if (LIST_NEXT(p, p_sibling) != NULL) {
686 p = LIST_NEXT(p, p_sibling);
687 break;
688 }
689 p = p->p_pptr;
690 }
691 }
692 /*NOTREACHED*/
693 }
694
695 int
696 ktrwrite(p, kth)
697 struct proc *p;
698 struct ktr_header *kth;
699 {
700 struct uio auio;
701 struct iovec aiov[2];
702 int error, tries;
703 struct file *fp = p->p_tracep;
704
705 if (fp == NULL)
706 return 0;
707
708 auio.uio_iov = &aiov[0];
709 auio.uio_offset = 0;
710 auio.uio_segflg = UIO_SYSSPACE;
711 auio.uio_rw = UIO_WRITE;
712 aiov[0].iov_base = (caddr_t)kth;
713 aiov[0].iov_len = sizeof(struct ktr_header);
714 auio.uio_resid = sizeof(struct ktr_header);
715 auio.uio_iovcnt = 1;
716 auio.uio_procp = (struct proc *)0;
717 if (kth->ktr_len > 0) {
718 auio.uio_iovcnt++;
719 aiov[1].iov_base = (void *)kth->ktr_buf;
720 aiov[1].iov_len = kth->ktr_len;
721 auio.uio_resid += kth->ktr_len;
722 }
723
724 simple_lock(&fp->f_slock);
725 FILE_USE(fp);
726
727 tries = 0;
728 do {
729 error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
730 fp->f_cred, FOF_UPDATE_OFFSET);
731 tries++;
732 if (error == EWOULDBLOCK)
733 preempt(1);
734 } while ((error == EWOULDBLOCK) && (tries < 3));
735 FILE_UNUSE(fp, NULL);
736
737 if (__predict_true(error == 0))
738 return (0);
739 /*
740 * If error encountered, give up tracing on this vnode. Don't report
741 * EPIPE as this can easily happen with fktrace()/ktruss.
742 */
743 if (error != EPIPE)
744 log(LOG_NOTICE,
745 "ktrace write failed, errno %d, tracing stopped\n",
746 error);
747 proclist_lock_read();
748 LIST_FOREACH(p, &allproc, p_list) {
749 if (ktrsamefile(p->p_tracep, fp))
750 ktrderef(p);
751 }
752 proclist_unlock_read();
753
754 return (error);
755 }
756
757 /*
758 * Return true if caller has permission to set the ktracing state
759 * of target. Essentially, the target can't possess any
760 * more permissions than the caller. KTRFAC_ROOT signifies that
761 * root previously set the tracing status on the target process, and
762 * so, only root may further change it.
763 *
764 * TODO: check groups. use caller effective gid.
765 */
766 int
767 ktrcanset(callp, targetp)
768 struct proc *callp;
769 struct proc *targetp;
770 {
771 struct pcred *caller = callp->p_cred;
772 struct pcred *target = targetp->p_cred;
773
774 if ((caller->pc_ucred->cr_uid == target->p_ruid &&
775 target->p_ruid == target->p_svuid &&
776 caller->p_rgid == target->p_rgid && /* XXX */
777 target->p_rgid == target->p_svgid &&
778 (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
779 (targetp->p_flag & P_SUGID) == 0) ||
780 caller->pc_ucred->cr_uid == 0)
781 return (1);
782
783 return (0);
784 }
785 #endif /* KTRACE */
786
787 /*
788 * Put user defined entry to ktrace records.
789 */
790 int
791 sys_utrace(l, v, retval)
792 struct lwp *l;
793 void *v;
794 register_t *retval;
795 {
796 #ifdef KTRACE
797 struct sys_utrace_args /* {
798 syscallarg(const char *) label;
799 syscallarg(void *) addr;
800 syscallarg(size_t) len;
801 } */ *uap = v;
802 struct proc *p = l->l_proc;
803 if (!KTRPOINT(p, KTR_USER))
804 return (0);
805
806 if (SCARG(uap, len) > KTR_USER_MAXLEN)
807 return (EINVAL);
808
809 ktruser(p, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
810
811 return (0);
812 #else /* !KTRACE */
813 return ENOSYS;
814 #endif /* KTRACE */
815 }
816