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