kern_ktrace.c revision 1.87 1 /* $NetBSD: kern_ktrace.c,v 1.87 2004/02/25 21:34:18 enami 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.87 2004/02/25 21:34:18 enami 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 void ktrinitheader(struct ktr_header *, struct proc *, int);
59 int ktrwrite(struct proc *, struct ktr_header *);
60 int ktrace_common(struct proc *, int, int, int, struct file *);
61 int ktrops(struct proc *, struct proc *, int, int, struct file *);
62 int ktrsetchildren(struct proc *, struct proc *, int, int,
63 struct file *);
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 p->p_tracep = NULL;
93
94 simple_lock(&fp->f_slock);
95 FILE_USE(fp);
96
97 /*
98 * ktrace file descriptor can't be watched (are not visible to
99 * userspace), so no kqueue stuff here
100 */
101 closef(fp, 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 #ifdef _LP64
147 if (p->p_flag & P_32)
148 argsize = argsize << 1;
149 #endif
150 len = sizeof(struct ktr_syscall) + argsize;
151
152 p->p_traceflag |= KTRFAC_ACTIVE;
153 ktrinitheader(&kth, p, KTR_SYSCALL);
154 ktp = malloc(len, M_TEMP, M_WAITOK);
155 ktp->ktr_code = realcode;
156 ktp->ktr_argsize = argsize;
157 argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
158 for (i = 0; i < (argsize / sizeof(*argp)); i++)
159 *argp++ = args[i];
160 kth.ktr_buf = (caddr_t)ktp;
161 kth.ktr_len = len;
162 (void) ktrwrite(p, &kth);
163 free(ktp, M_TEMP);
164 p->p_traceflag &= ~KTRFAC_ACTIVE;
165 }
166
167 void
168 ktrsysret(p, code, error, retval)
169 struct proc *p;
170 register_t code;
171 int error;
172 register_t *retval;
173 {
174 struct ktr_header kth;
175 struct ktr_sysret ktp;
176
177 p->p_traceflag |= KTRFAC_ACTIVE;
178 ktrinitheader(&kth, p, KTR_SYSRET);
179 ktp.ktr_code = code;
180 ktp.ktr_eosys = 0; /* XXX unused */
181 ktp.ktr_error = error;
182 ktp.ktr_retval = retval ? retval[0] : 0;
183 ktp.ktr_retval_1 = retval ? retval[1] : 0;
184
185 kth.ktr_buf = (caddr_t)&ktp;
186 kth.ktr_len = sizeof(struct ktr_sysret);
187
188 (void) ktrwrite(p, &kth);
189 p->p_traceflag &= ~KTRFAC_ACTIVE;
190 }
191
192 void
193 ktrnamei(p, path)
194 struct proc *p;
195 char *path;
196 {
197 struct ktr_header kth;
198
199 p->p_traceflag |= KTRFAC_ACTIVE;
200 ktrinitheader(&kth, p, KTR_NAMEI);
201 kth.ktr_len = strlen(path);
202 kth.ktr_buf = path;
203
204 (void) ktrwrite(p, &kth);
205 p->p_traceflag &= ~KTRFAC_ACTIVE;
206 }
207
208 void
209 ktremul(p)
210 struct proc *p;
211 {
212 struct ktr_header kth;
213 const char *emul = p->p_emul->e_name;
214
215 p->p_traceflag |= KTRFAC_ACTIVE;
216 ktrinitheader(&kth, p, KTR_EMUL);
217 kth.ktr_len = strlen(emul);
218 kth.ktr_buf = (caddr_t)emul;
219
220 (void) ktrwrite(p, &kth);
221 p->p_traceflag &= ~KTRFAC_ACTIVE;
222 }
223
224 void
225 ktrkmem(struct proc *p, int ktr, const void *buf, size_t len)
226 {
227 struct ktr_header kth;
228
229 p->p_traceflag |= KTRFAC_ACTIVE;
230 ktrinitheader(&kth, p, ktr);
231 kth.ktr_len = len;
232 kth.ktr_buf = buf;
233
234 (void)ktrwrite(p, &kth);
235 p->p_traceflag &= ~KTRFAC_ACTIVE;
236 }
237
238 void
239 ktrgenio(p, fd, rw, iov, len, error)
240 struct proc *p;
241 int fd;
242 enum uio_rw rw;
243 struct iovec *iov;
244 int len;
245 int error;
246 {
247 struct ktr_header kth;
248 struct ktr_genio *ktp;
249 caddr_t cp;
250 int resid = len, cnt;
251 int buflen;
252
253 if (error)
254 return;
255
256 p->p_traceflag |= KTRFAC_ACTIVE;
257
258 buflen = min(PAGE_SIZE, len + sizeof(struct ktr_genio));
259
260 ktrinitheader(&kth, p, KTR_GENIO);
261 ktp = malloc(buflen, M_TEMP, M_WAITOK);
262 ktp->ktr_fd = fd;
263 ktp->ktr_rw = rw;
264
265 kth.ktr_buf = (caddr_t)ktp;
266
267 cp = (caddr_t)((char *)ktp + sizeof(struct ktr_genio));
268 buflen -= sizeof(struct ktr_genio);
269
270 while (resid > 0) {
271 #if 0 /* XXX NJWLWP */
272 KDASSERT(p->p_cpu != NULL);
273 KDASSERT(p->p_cpu == curcpu());
274 #endif
275 /* XXX NJWLWP */
276 if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
277 preempt(1);
278
279 cnt = min(iov->iov_len, buflen);
280 if (cnt > resid)
281 cnt = resid;
282 if (copyin(iov->iov_base, cp, cnt))
283 break;
284
285 kth.ktr_len = cnt + sizeof(struct ktr_genio);
286
287 if (__predict_false(ktrwrite(p, &kth) != 0))
288 break;
289
290 iov->iov_base = (caddr_t)iov->iov_base + cnt;
291 iov->iov_len -= cnt;
292
293 if (iov->iov_len == 0)
294 iov++;
295
296 resid -= cnt;
297 }
298
299 free(ktp, M_TEMP);
300 p->p_traceflag &= ~KTRFAC_ACTIVE;
301 }
302
303 void
304 ktrpsig(p, sig, action, mask, ksi)
305 struct proc *p;
306 int sig;
307 sig_t action;
308 const sigset_t *mask;
309 const ksiginfo_t *ksi;
310 {
311 struct ktr_header kth;
312 struct {
313 struct ktr_psig kp;
314 siginfo_t si;
315 } kbuf;
316
317 p->p_traceflag |= KTRFAC_ACTIVE;
318 ktrinitheader(&kth, p, KTR_PSIG);
319 kbuf.kp.signo = (char)sig;
320 kbuf.kp.action = action;
321 kbuf.kp.mask = *mask;
322 kth.ktr_buf = (caddr_t)&kbuf;
323 if (ksi) {
324 kbuf.kp.code = KSI_TRAPCODE(ksi);
325 (void)memset(&kbuf.si, 0, sizeof(kbuf.si));
326 kbuf.si._info = ksi->ksi_info;
327 kth.ktr_len = sizeof(kbuf);
328 } else {
329 kbuf.kp.code = 0;
330 kth.ktr_len = sizeof(struct ktr_psig);
331 }
332 (void) ktrwrite(p, &kth);
333 p->p_traceflag &= ~KTRFAC_ACTIVE;
334 }
335
336 void
337 ktrcsw(p, out, user)
338 struct proc *p;
339 int out;
340 int user;
341 {
342 struct ktr_header kth;
343 struct ktr_csw kc;
344
345 p->p_traceflag |= KTRFAC_ACTIVE;
346 ktrinitheader(&kth, p, KTR_CSW);
347 kc.out = out;
348 kc.user = user;
349 kth.ktr_buf = (caddr_t)&kc;
350 kth.ktr_len = sizeof(struct ktr_csw);
351
352 (void) ktrwrite(p, &kth);
353 p->p_traceflag &= ~KTRFAC_ACTIVE;
354 }
355
356 void
357 ktruser(p, id, addr, len, ustr)
358 struct proc *p;
359 const char *id;
360 void *addr;
361 size_t len;
362 int ustr;
363 {
364 struct ktr_header kth;
365 struct ktr_user *ktp;
366 caddr_t user_dta;
367
368 p->p_traceflag |= KTRFAC_ACTIVE;
369 ktrinitheader(&kth, p, KTR_USER);
370 ktp = malloc(sizeof(struct ktr_user) + len, M_TEMP, M_WAITOK);
371 if (ustr) {
372 if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
373 ktp->ktr_id[0] = '\0';
374 } else
375 strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
376 ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
377
378 user_dta = (caddr_t) ((char *)ktp + sizeof(struct ktr_user));
379 if (copyin(addr, (void *) user_dta, len) != 0)
380 len = 0;
381
382 kth.ktr_buf = (void *)ktp;
383 kth.ktr_len = sizeof(struct ktr_user) + len;
384 (void) ktrwrite(p, &kth);
385
386 free(ktp, M_TEMP);
387 p->p_traceflag &= ~KTRFAC_ACTIVE;
388
389 }
390
391 void
392 ktrmmsg(p, msgh, size)
393 struct proc *p;
394 const void *msgh;
395 size_t size;
396 {
397 struct ktr_header kth;
398 struct ktr_mmsg *kp;
399
400 p->p_traceflag |= KTRFAC_ACTIVE;
401 ktrinitheader(&kth, p, KTR_MMSG);
402
403 kp = (struct ktr_mmsg *)msgh;
404 kth.ktr_buf = (caddr_t)kp;
405 kth.ktr_len = size;
406 (void) ktrwrite(p, &kth);
407 p->p_traceflag &= ~KTRFAC_ACTIVE;
408 }
409
410 void
411 ktrmool(p, kaddr, size, uaddr)
412 struct proc *p;
413 const void *kaddr;
414 size_t size;
415 const void *uaddr;
416 {
417 struct ktr_header kth;
418 struct ktr_mool *kp;
419 struct ktr_mool *buf;
420
421 p->p_traceflag |= KTRFAC_ACTIVE;
422 ktrinitheader(&kth, p, KTR_MOOL);
423
424 kp = malloc(size + sizeof(*kp), M_TEMP, M_WAITOK);
425 kp->uaddr = uaddr;
426 kp->size = size;
427 buf = kp + 1; /* Skip uaddr and size */
428 memcpy(buf, kaddr, size);
429
430 kth.ktr_buf = (caddr_t)kp;
431 kth.ktr_len = size + sizeof(*kp);
432 (void) ktrwrite(p, &kth);
433 free(kp, M_TEMP);
434
435 p->p_traceflag &= ~KTRFAC_ACTIVE;
436 }
437
438
439 /* Interface and common routines */
440
441 int
442 ktrace_common(curp, ops, facs, pid, fp)
443 struct proc *curp;
444 int ops;
445 int facs;
446 int pid;
447 struct file *fp;
448 {
449 int ret = 0;
450 int error = 0;
451 int one = 1;
452 int descend;
453 struct proc *p;
454 struct pgrp *pg;
455
456 curp->p_traceflag |= KTRFAC_ACTIVE;
457 descend = ops & KTRFLAG_DESCEND;
458 facs = facs & ~((unsigned) KTRFAC_ROOT);
459
460 /*
461 * Clear all uses of the tracefile
462 */
463 if (KTROP(ops) == KTROP_CLEARFILE) {
464 proclist_lock_read();
465 LIST_FOREACH(p, &allproc, p_list) {
466 if (ktrsamefile(p->p_tracep, fp)) {
467 if (ktrcanset(curp, p))
468 ktrderef(p);
469 else
470 error = EPERM;
471 }
472 }
473 proclist_unlock_read();
474 goto done;
475 }
476
477 /*
478 * Mark fp non-blocking, to avoid problems from possible deadlocks.
479 */
480
481 if (fp != NULL) {
482 fp->f_flag |= FNONBLOCK;
483 (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&one, curp);
484 }
485
486 /*
487 * need something to (un)trace (XXX - why is this here?)
488 */
489 if (!facs) {
490 error = EINVAL;
491 goto done;
492 }
493 /*
494 * do it
495 */
496 if (pid < 0) {
497 /*
498 * by process group
499 */
500 pg = pg_find(-pid, PFIND_UNLOCK_FAIL);
501 if (pg == NULL) {
502 error = ESRCH;
503 goto done;
504 }
505 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
506 if (descend)
507 ret |= ktrsetchildren(curp, p, ops, facs, fp);
508 else
509 ret |= ktrops(curp, p, ops, facs, fp);
510 }
511
512 } else {
513 /*
514 * by pid
515 */
516 p = p_find(pid, PFIND_UNLOCK_FAIL);
517 if (p == NULL) {
518 error = ESRCH;
519 goto done;
520 }
521 if (descend)
522 ret |= ktrsetchildren(curp, p, ops, facs, fp);
523 else
524 ret |= ktrops(curp, p, ops, facs, fp);
525 }
526 proclist_unlock_read(); /* taken by p{g}_find */
527 if (!ret)
528 error = EPERM;
529 done:
530 curp->p_traceflag &= ~KTRFAC_ACTIVE;
531 return (error);
532 }
533
534 /*
535 * ktrace system call
536 */
537 /* ARGSUSED */
538 int
539 sys_fktrace(l, v, retval)
540 struct lwp *l;
541 void *v;
542 register_t *retval;
543 {
544 struct sys_fktrace_args /* {
545 syscallarg(int) fd;
546 syscallarg(int) ops;
547 syscallarg(int) facs;
548 syscallarg(int) pid;
549 } */ *uap = v;
550 struct proc *curp = l->l_proc;
551 struct file *fp = NULL;
552 struct filedesc *fdp = curp->p_fd;
553 int error;
554
555 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
556 return (EBADF);
557
558 FILE_USE(fp);
559
560 if ((fp->f_flag & FWRITE) == 0)
561 error = EBADF;
562 else
563 error = ktrace_common(curp, SCARG(uap, ops),
564 SCARG(uap, facs), SCARG(uap, pid), fp);
565
566 FILE_UNUSE(fp, curp);
567
568 return error;
569 }
570
571 /*
572 * ktrace system call
573 */
574 /* ARGSUSED */
575 int
576 sys_ktrace(l, v, retval)
577 struct lwp *l;
578 void *v;
579 register_t *retval;
580 {
581 struct sys_ktrace_args /* {
582 syscallarg(const char *) fname;
583 syscallarg(int) ops;
584 syscallarg(int) facs;
585 syscallarg(int) pid;
586 } */ *uap = v;
587 struct proc *curp = l->l_proc;
588 struct vnode *vp = NULL;
589 struct file *fp = NULL;
590 int fd;
591 int ops = SCARG(uap, ops);
592 int error = 0;
593 struct nameidata nd;
594
595 ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
596
597 curp->p_traceflag |= KTRFAC_ACTIVE;
598 if ((ops & KTROP_CLEAR) == 0) {
599 /*
600 * an operation which requires a file argument.
601 */
602 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
603 curp);
604 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
605 curp->p_traceflag &= ~KTRFAC_ACTIVE;
606 return (error);
607 }
608 vp = nd.ni_vp;
609 VOP_UNLOCK(vp, 0);
610 if (vp->v_type != VREG) {
611 (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
612 curp->p_traceflag &= ~KTRFAC_ACTIVE;
613 return (EACCES);
614 }
615 /*
616 * XXX This uses up a file descriptor slot in the
617 * tracing process for the duration of this syscall.
618 * This is not expected to be a problem. If
619 * falloc(NULL, ...) DTRT we could skip that part, but
620 * that would require changing its interface to allow
621 * the caller to pass in a ucred..
622 *
623 * This will FILE_USE the fp it returns, if any.
624 * Keep it in use until we return.
625 */
626 if ((error = falloc(curp, &fp, &fd)) != 0)
627 goto done;
628
629 fp->f_flag = FWRITE|FAPPEND;
630 fp->f_type = DTYPE_VNODE;
631 fp->f_ops = &vnops;
632 fp->f_data = (caddr_t)vp;
633 FILE_SET_MATURE(fp);
634 vp = NULL;
635 }
636 error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
637 SCARG(uap, pid), fp);
638 done:
639 if (vp != NULL)
640 (void) vn_close(vp, FWRITE, curp->p_ucred, curp);
641 if (fp != NULL) {
642 FILE_UNUSE(fp, curp); /* release file */
643 fdrelease(curp, fd); /* release fd table slot */
644 }
645 return (error);
646 }
647
648 int
649 ktrops(curp, p, ops, facs, fp)
650 struct proc *curp;
651 struct proc *p;
652 int ops;
653 int facs;
654 struct file *fp;
655 {
656
657 if (!ktrcanset(curp, p))
658 return (0);
659 if (KTROP(ops) == KTROP_SET) {
660 if (p->p_tracep != fp) {
661 /*
662 * if trace file already in use, relinquish
663 */
664 ktrderef(p);
665 p->p_tracep = fp;
666 ktradref(p);
667 }
668 p->p_traceflag |= facs;
669 if (curp->p_ucred->cr_uid == 0)
670 p->p_traceflag |= KTRFAC_ROOT;
671 } else {
672 /* KTROP_CLEAR */
673 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
674 /* no more tracing */
675 ktrderef(p);
676 }
677 }
678
679 /*
680 * Emit an emulation record, every time there is a ktrace
681 * change/attach request.
682 */
683 if (KTRPOINT(p, KTR_EMUL))
684 p->p_traceflag |= KTRFAC_TRC_EMUL;
685 #ifdef __HAVE_SYSCALL_INTERN
686 (*p->p_emul->e_syscall_intern)(p);
687 #endif
688
689 return (1);
690 }
691
692 int
693 ktrsetchildren(curp, top, ops, facs, fp)
694 struct proc *curp;
695 struct proc *top;
696 int ops;
697 int facs;
698 struct file *fp;
699 {
700 struct proc *p;
701 int ret = 0;
702
703 p = top;
704 for (;;) {
705 ret |= ktrops(curp, p, ops, facs, fp);
706 /*
707 * If this process has children, descend to them next,
708 * otherwise do any siblings, and if done with this level,
709 * follow back up the tree (but not past top).
710 */
711 if (LIST_FIRST(&p->p_children) != NULL) {
712 p = LIST_FIRST(&p->p_children);
713 continue;
714 }
715 for (;;) {
716 if (p == top)
717 return (ret);
718 if (LIST_NEXT(p, p_sibling) != NULL) {
719 p = LIST_NEXT(p, p_sibling);
720 break;
721 }
722 p = p->p_pptr;
723 }
724 }
725 /*NOTREACHED*/
726 }
727
728 int
729 ktrwrite(p, kth)
730 struct proc *p;
731 struct ktr_header *kth;
732 {
733 struct uio auio;
734 struct iovec aiov[2];
735 int error, tries;
736 struct file *fp = p->p_tracep;
737
738 if (fp == NULL)
739 return 0;
740
741 if (p->p_traceflag & KTRFAC_TRC_EMUL) {
742 /* Add emulation trace before first entry for this process */
743 p->p_traceflag &= ~KTRFAC_TRC_EMUL;
744 ktremul(p);
745 }
746
747 auio.uio_iov = &aiov[0];
748 auio.uio_offset = 0;
749 auio.uio_segflg = UIO_SYSSPACE;
750 auio.uio_rw = UIO_WRITE;
751 aiov[0].iov_base = (caddr_t)kth;
752 aiov[0].iov_len = sizeof(struct ktr_header);
753 auio.uio_resid = sizeof(struct ktr_header);
754 auio.uio_iovcnt = 1;
755 auio.uio_procp = (struct proc *)0;
756 if (kth->ktr_len > 0) {
757 auio.uio_iovcnt++;
758 aiov[1].iov_base = (void *)kth->ktr_buf;
759 aiov[1].iov_len = kth->ktr_len;
760 auio.uio_resid += kth->ktr_len;
761 }
762
763 simple_lock(&fp->f_slock);
764 FILE_USE(fp);
765
766 tries = 0;
767 do {
768 error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
769 fp->f_cred, FOF_UPDATE_OFFSET);
770 tries++;
771 if (error == EWOULDBLOCK)
772 preempt(1);
773 } while ((error == EWOULDBLOCK) && (tries < 3));
774 FILE_UNUSE(fp, NULL);
775
776 if (__predict_true(error == 0))
777 return (0);
778 /*
779 * If error encountered, give up tracing on this vnode. Don't report
780 * EPIPE as this can easily happen with fktrace()/ktruss.
781 */
782 if (error != EPIPE)
783 log(LOG_NOTICE,
784 "ktrace write failed, errno %d, tracing stopped\n",
785 error);
786 proclist_lock_read();
787 LIST_FOREACH(p, &allproc, p_list) {
788 if (ktrsamefile(p->p_tracep, fp))
789 ktrderef(p);
790 }
791 proclist_unlock_read();
792
793 return (error);
794 }
795
796 /*
797 * Return true if caller has permission to set the ktracing state
798 * of target. Essentially, the target can't possess any
799 * more permissions than the caller. KTRFAC_ROOT signifies that
800 * root previously set the tracing status on the target process, and
801 * so, only root may further change it.
802 *
803 * TODO: check groups. use caller effective gid.
804 */
805 int
806 ktrcanset(callp, targetp)
807 struct proc *callp;
808 struct proc *targetp;
809 {
810 struct pcred *caller = callp->p_cred;
811 struct pcred *target = targetp->p_cred;
812
813 if ((caller->pc_ucred->cr_uid == target->p_ruid &&
814 target->p_ruid == target->p_svuid &&
815 caller->p_rgid == target->p_rgid && /* XXX */
816 target->p_rgid == target->p_svgid &&
817 (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
818 (targetp->p_flag & P_SUGID) == 0) ||
819 caller->pc_ucred->cr_uid == 0)
820 return (1);
821
822 return (0);
823 }
824 #endif /* KTRACE */
825
826 /*
827 * Put user defined entry to ktrace records.
828 */
829 int
830 sys_utrace(l, v, retval)
831 struct lwp *l;
832 void *v;
833 register_t *retval;
834 {
835 #ifdef KTRACE
836 struct sys_utrace_args /* {
837 syscallarg(const char *) label;
838 syscallarg(void *) addr;
839 syscallarg(size_t) len;
840 } */ *uap = v;
841 struct proc *p = l->l_proc;
842 if (!KTRPOINT(p, KTR_USER))
843 return (0);
844
845 if (SCARG(uap, len) > KTR_USER_MAXLEN)
846 return (EINVAL);
847
848 ktruser(p, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
849
850 return (0);
851 #else /* !KTRACE */
852 return ENOSYS;
853 #endif /* KTRACE */
854 }
855