kern_ktrace.c revision 1.76 1 /* $NetBSD: kern_ktrace.c,v 1.76 2003/08/04 18:53:06 drochner 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.76 2003/08/04 18:53:06 drochner 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 proc *, int, int, int, struct file *);
63 void ktrinitheader(struct ktr_header *, struct proc *, int);
64 int ktrops(struct proc *, struct proc *, int, int, struct file *);
65 int ktrsetchildren(struct proc *, struct proc *, int, int,
66 struct file *);
67 int ktrwrite(struct proc *, 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, p, type)
119 struct ktr_header *kth;
120 struct proc *p;
121 int type;
122 {
123
124 memset(kth, 0, sizeof(*kth));
125 kth->ktr_type = type;
126 microtime(&kth->ktr_time);
127 kth->ktr_pid = p->p_pid;
128 memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
129 }
130
131 void
132 ktrsyscall(p, code, realcode, callp, args)
133 struct proc *p;
134 register_t code;
135 register_t realcode;
136 const struct sysent *callp;
137 register_t args[];
138 {
139 struct ktr_header kth;
140 struct ktr_syscall *ktp;
141 register_t *argp;
142 int argsize;
143 size_t len;
144 u_int i;
145
146 if (callp == NULL)
147 callp = p->p_emul->e_sysent;
148
149 argsize = callp[code].sy_argsize;
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, code)
305 struct proc *p;
306 int sig;
307 sig_t action;
308 sigset_t *mask;
309 int code;
310 {
311 struct ktr_header kth;
312 struct ktr_psig kp;
313
314 p->p_traceflag |= KTRFAC_ACTIVE;
315 ktrinitheader(&kth, p, KTR_PSIG);
316 kp.signo = (char)sig;
317 kp.action = action;
318 kp.mask = *mask;
319 kp.code = code;
320 kth.ktr_buf = (caddr_t)&kp;
321 kth.ktr_len = sizeof(struct ktr_psig);
322
323 (void) ktrwrite(p, &kth);
324 p->p_traceflag &= ~KTRFAC_ACTIVE;
325 }
326
327 void
328 ktrcsw(p, out, user)
329 struct proc *p;
330 int out;
331 int user;
332 {
333 struct ktr_header kth;
334 struct ktr_csw kc;
335
336 p->p_traceflag |= KTRFAC_ACTIVE;
337 ktrinitheader(&kth, p, KTR_CSW);
338 kc.out = out;
339 kc.user = user;
340 kth.ktr_buf = (caddr_t)&kc;
341 kth.ktr_len = sizeof(struct ktr_csw);
342
343 (void) ktrwrite(p, &kth);
344 p->p_traceflag &= ~KTRFAC_ACTIVE;
345 }
346
347 void
348 ktruser(p, id, addr, len, ustr)
349 struct proc *p;
350 const char *id;
351 void *addr;
352 size_t len;
353 int ustr;
354 {
355 struct ktr_header kth;
356 struct ktr_user *ktp;
357 caddr_t user_dta;
358
359 p->p_traceflag |= KTRFAC_ACTIVE;
360 ktrinitheader(&kth, p, KTR_USER);
361 ktp = malloc(sizeof(struct ktr_user) + len, M_TEMP, M_WAITOK);
362 if (ustr) {
363 if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
364 ktp->ktr_id[0] = '\0';
365 } else
366 strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
367 ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
368
369 user_dta = (caddr_t) ((char *)ktp + sizeof(struct ktr_user));
370 if (copyin(addr, (void *) user_dta, len) != 0)
371 len = 0;
372
373 kth.ktr_buf = (void *)ktp;
374 kth.ktr_len = sizeof(struct ktr_user) + len;
375 (void) ktrwrite(p, &kth);
376
377 free(ktp, M_TEMP);
378 p->p_traceflag &= ~KTRFAC_ACTIVE;
379
380 }
381
382 void
383 ktrmmsg(p, msgh, size)
384 struct proc *p;
385 const void *msgh;
386 size_t size;
387 {
388 struct ktr_header kth;
389 struct ktr_mmsg *kp;
390
391 p->p_traceflag |= KTRFAC_ACTIVE;
392 ktrinitheader(&kth, p, KTR_MMSG);
393
394 kp = (struct ktr_mmsg *)msgh;
395 kth.ktr_buf = (caddr_t)kp;
396 kth.ktr_len = size;
397 (void) ktrwrite(p, &kth);
398 p->p_traceflag &= ~KTRFAC_ACTIVE;
399 }
400
401 /* Interface and common routines */
402
403 int
404 ktrace_common(curp, ops, facs, pid, fp)
405 struct proc *curp;
406 int ops;
407 int facs;
408 int pid;
409 struct file *fp;
410 {
411 int ret = 0;
412 int error = 0;
413 int one = 1;
414 int descend;
415 struct proc *p;
416 struct pgrp *pg;
417
418 curp->p_traceflag |= KTRFAC_ACTIVE;
419 descend = ops & KTRFLAG_DESCEND;
420 facs = facs & ~((unsigned) KTRFAC_ROOT);
421
422 /*
423 * Clear all uses of the tracefile
424 */
425 if (KTROP(ops) == KTROP_CLEARFILE) {
426 proclist_lock_read();
427 for (p = LIST_FIRST(&allproc); p != NULL;
428 p = LIST_NEXT(p, p_list)) {
429 if (ktrsamefile(p->p_tracep, fp)) {
430 if (ktrcanset(curp, p))
431 ktrderef(p);
432 else
433 error = EPERM;
434 }
435 }
436 proclist_unlock_read();
437 goto done;
438 }
439
440 /*
441 * Mark fp non-blocking, to avoid problems from possible deadlocks.
442 */
443
444 if (fp != NULL) {
445 fp->f_flag |= FNONBLOCK;
446 (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&one, curp);
447 }
448
449 /*
450 * need something to (un)trace (XXX - why is this here?)
451 */
452 if (!facs) {
453 error = EINVAL;
454 goto done;
455 }
456 /*
457 * do it
458 */
459 if (pid < 0) {
460 /*
461 * by process group
462 */
463 pg = pgfind(-pid);
464 if (pg == NULL) {
465 error = ESRCH;
466 goto done;
467 }
468 for (p = LIST_FIRST(&pg->pg_members); p != NULL;
469 p = LIST_NEXT(p, p_pglist)) {
470 if (descend)
471 ret |= ktrsetchildren(curp, p, ops, facs, fp);
472 else
473 ret |= ktrops(curp, p, ops, facs, fp);
474 }
475
476 } else {
477 /*
478 * by pid
479 */
480 p = pfind(pid);
481 if (p == NULL) {
482 error = ESRCH;
483 goto done;
484 }
485 if (descend)
486 ret |= ktrsetchildren(curp, p, ops, facs, fp);
487 else
488 ret |= ktrops(curp, p, ops, facs, fp);
489 }
490 if (!ret)
491 error = EPERM;
492 done:
493 curp->p_traceflag &= ~KTRFAC_ACTIVE;
494 return (error);
495 }
496
497 /*
498 * ktrace system call
499 */
500 /* ARGSUSED */
501 int
502 sys_fktrace(l, v, retval)
503 struct lwp *l;
504 void *v;
505 register_t *retval;
506 {
507 struct sys_fktrace_args /* {
508 syscallarg(int) fd;
509 syscallarg(int) ops;
510 syscallarg(int) facs;
511 syscallarg(int) pid;
512 } */ *uap = v;
513 struct proc *curp = l->l_proc;
514 struct file *fp = NULL;
515 struct filedesc *fdp = curp->p_fd;
516 int error;
517
518 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
519 return (EBADF);
520
521 FILE_USE(fp);
522
523 if ((fp->f_flag & FWRITE) == 0)
524 error = EBADF;
525 else
526 error = ktrace_common(curp, SCARG(uap, ops),
527 SCARG(uap, facs), SCARG(uap, pid), fp);
528
529 FILE_UNUSE(fp, curp);
530
531 return error;
532 }
533
534 /*
535 * ktrace system call
536 */
537 /* ARGSUSED */
538 int
539 sys_ktrace(l, v, retval)
540 struct lwp *l;
541 void *v;
542 register_t *retval;
543 {
544 struct sys_ktrace_args /* {
545 syscallarg(const char *) fname;
546 syscallarg(int) ops;
547 syscallarg(int) facs;
548 syscallarg(int) pid;
549 } */ *uap = v;
550 struct proc *curp = l->l_proc;
551 struct vnode *vp = NULL;
552 struct file *fp = NULL;
553 int fd;
554 int ops = SCARG(uap, ops);
555 int error = 0;
556 struct nameidata nd;
557
558 ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
559
560 curp->p_traceflag |= KTRFAC_ACTIVE;
561 if ((ops & KTROP_CLEAR) == 0) {
562 /*
563 * an operation which requires a file argument.
564 */
565 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
566 curp);
567 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
568 curp->p_traceflag &= ~KTRFAC_ACTIVE;
569 return (error);
570 }
571 vp = nd.ni_vp;
572 VOP_UNLOCK(vp, 0);
573 if (vp->v_type != VREG) {
574 (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
575 curp->p_traceflag &= ~KTRFAC_ACTIVE;
576 return (EACCES);
577 }
578 /*
579 * XXX This uses up a file descriptor slot in the
580 * tracing process for the duration of this syscall.
581 * This is not expected to be a problem. If
582 * falloc(NULL, ...) DTRT we could skip that part, but
583 * that would require changing its interface to allow
584 * the caller to pass in a ucred..
585 *
586 * This will FILE_USE the fp it returns, if any.
587 * Keep it in use until we return.
588 */
589 if ((error = falloc(curp, &fp, &fd)) != 0)
590 goto done;
591
592 fp->f_flag = FWRITE|FAPPEND;
593 fp->f_type = DTYPE_VNODE;
594 fp->f_ops = &vnops;
595 fp->f_data = (caddr_t)vp;
596 FILE_SET_MATURE(fp);
597 vp = NULL;
598 }
599 error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
600 SCARG(uap, pid), fp);
601 done:
602 if (vp != NULL)
603 (void) vn_close(vp, FWRITE, curp->p_ucred, curp);
604 if (fp != NULL) {
605 FILE_UNUSE(fp, curp); /* release file */
606 fdrelease(curp, fd); /* release fd table slot */
607 }
608 return (error);
609 }
610
611 int
612 ktrops(curp, p, ops, facs, fp)
613 struct proc *curp;
614 struct proc *p;
615 int ops;
616 int facs;
617 struct file *fp;
618 {
619
620 if (!ktrcanset(curp, p))
621 return (0);
622 if (KTROP(ops) == KTROP_SET) {
623 if (p->p_tracep != fp) {
624 /*
625 * if trace file already in use, relinquish
626 */
627 ktrderef(p);
628 p->p_tracep = fp;
629 ktradref(p);
630 }
631 p->p_traceflag |= facs;
632 if (curp->p_ucred->cr_uid == 0)
633 p->p_traceflag |= KTRFAC_ROOT;
634 } else {
635 /* KTROP_CLEAR */
636 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
637 /* no more tracing */
638 ktrderef(p);
639 }
640 }
641
642 /*
643 * Emit an emulation record, every time there is a ktrace
644 * change/attach request.
645 */
646 if (KTRPOINT(p, KTR_EMUL))
647 ktremul(p);
648 #ifdef __HAVE_SYSCALL_INTERN
649 (*p->p_emul->e_syscall_intern)(p);
650 #endif
651
652 return (1);
653 }
654
655 int
656 ktrsetchildren(curp, top, ops, facs, fp)
657 struct proc *curp;
658 struct proc *top;
659 int ops;
660 int facs;
661 struct file *fp;
662 {
663 struct proc *p;
664 int ret = 0;
665
666 p = top;
667 for (;;) {
668 ret |= ktrops(curp, p, ops, facs, fp);
669 /*
670 * If this process has children, descend to them next,
671 * otherwise do any siblings, and if done with this level,
672 * follow back up the tree (but not past top).
673 */
674 if (LIST_FIRST(&p->p_children) != NULL)
675 p = LIST_FIRST(&p->p_children);
676 else for (;;) {
677 if (p == top)
678 return (ret);
679 if (LIST_NEXT(p, p_sibling) != NULL) {
680 p = LIST_NEXT(p, p_sibling);
681 break;
682 }
683 p = p->p_pptr;
684 }
685 }
686 /*NOTREACHED*/
687 }
688
689 int
690 ktrwrite(p, kth)
691 struct proc *p;
692 struct ktr_header *kth;
693 {
694 struct uio auio;
695 struct iovec aiov[2];
696 int error, tries;
697 struct file *fp = p->p_tracep;
698
699 if (fp == NULL)
700 return 0;
701
702 auio.uio_iov = &aiov[0];
703 auio.uio_offset = 0;
704 auio.uio_segflg = UIO_SYSSPACE;
705 auio.uio_rw = UIO_WRITE;
706 aiov[0].iov_base = (caddr_t)kth;
707 aiov[0].iov_len = sizeof(struct ktr_header);
708 auio.uio_resid = sizeof(struct ktr_header);
709 auio.uio_iovcnt = 1;
710 auio.uio_procp = (struct proc *)0;
711 if (kth->ktr_len > 0) {
712 auio.uio_iovcnt++;
713 aiov[1].iov_base = (void *)kth->ktr_buf;
714 aiov[1].iov_len = kth->ktr_len;
715 auio.uio_resid += kth->ktr_len;
716 }
717
718 simple_lock(&fp->f_slock);
719 FILE_USE(fp);
720
721 tries = 0;
722 do {
723 error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
724 fp->f_cred, FOF_UPDATE_OFFSET);
725 tries++;
726 if (error == EWOULDBLOCK)
727 preempt(1);
728 } while ((error == EWOULDBLOCK) && (tries < 3));
729 FILE_UNUSE(fp, NULL);
730
731 if (__predict_true(error == 0))
732 return (0);
733 /*
734 * If error encountered, give up tracing on this vnode. Don't report
735 * EPIPE as this can easily happen with fktrace()/ktruss.
736 */
737 if (error != EPIPE)
738 log(LOG_NOTICE,
739 "ktrace write failed, errno %d, tracing stopped\n",
740 error);
741 proclist_lock_read();
742 for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) {
743 if (ktrsamefile(p->p_tracep, fp))
744 ktrderef(p);
745 }
746 proclist_unlock_read();
747
748 return (error);
749 }
750
751 /*
752 * Return true if caller has permission to set the ktracing state
753 * of target. Essentially, the target can't possess any
754 * more permissions than the caller. KTRFAC_ROOT signifies that
755 * root previously set the tracing status on the target process, and
756 * so, only root may further change it.
757 *
758 * TODO: check groups. use caller effective gid.
759 */
760 int
761 ktrcanset(callp, targetp)
762 struct proc *callp;
763 struct proc *targetp;
764 {
765 struct pcred *caller = callp->p_cred;
766 struct pcred *target = targetp->p_cred;
767
768 if ((caller->pc_ucred->cr_uid == target->p_ruid &&
769 target->p_ruid == target->p_svuid &&
770 caller->p_rgid == target->p_rgid && /* XXX */
771 target->p_rgid == target->p_svgid &&
772 (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
773 (targetp->p_flag & P_SUGID) == 0) ||
774 caller->pc_ucred->cr_uid == 0)
775 return (1);
776
777 return (0);
778 }
779 #endif /* KTRACE */
780
781 /*
782 * Put user defined entry to ktrace records.
783 */
784 int
785 sys_utrace(l, v, retval)
786 struct lwp *l;
787 void *v;
788 register_t *retval;
789 {
790 #ifdef KTRACE
791 struct sys_utrace_args /* {
792 syscallarg(const char *) label;
793 syscallarg(void *) addr;
794 syscallarg(size_t) len;
795 } */ *uap = v;
796 struct proc *p = l->l_proc;
797 if (!KTRPOINT(p, KTR_USER))
798 return (0);
799
800 if (SCARG(uap, len) > KTR_USER_MAXLEN)
801 return (EINVAL);
802
803 ktruser(p, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
804
805 return (0);
806 #else /* !KTRACE */
807 return ENOSYS;
808 #endif /* KTRACE */
809 }
810