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