kernel - Move MP lock inward, plus misc other stuff
[dragonfly.git] / sys / kern / kern_ktrace.c
blob5af53133e542677fef79aa99c2c29345b77756b5
1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93
34 * $FreeBSD: src/sys/kern/kern_ktrace.c,v 1.35.2.6 2002/07/05 22:36:38 darrenr Exp $
35 * $DragonFly: src/sys/kern/kern_ktrace.c,v 1.30 2008/04/14 12:01:50 dillon Exp $
38 #include "opt_ktrace.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
43 #include <sys/kernel.h>
44 #include <sys/proc.h>
45 #include <sys/fcntl.h>
46 #include <sys/lock.h>
47 #include <sys/nlookup.h>
48 #include <sys/vnode.h>
49 #include <sys/ktrace.h>
50 #include <sys/malloc.h>
51 #include <sys/syslog.h>
52 #include <sys/sysent.h>
54 #include <vm/vm_zone.h>
55 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
57 #ifdef KTRACE
58 static struct ktr_header *ktrgetheader (int type);
59 static void ktrwrite (struct proc *, struct ktr_header *, struct uio *);
60 static int ktrcanset (struct proc *,struct proc *);
61 static int ktrsetchildren (struct proc *,struct proc *,int,int, ktrace_node_t);
62 static int ktrops (struct proc *,struct proc *,int,int, ktrace_node_t);
65 * MPSAFE
67 static struct ktr_header *
68 ktrgetheader(int type)
70 struct ktr_header *kth;
71 struct proc *p = curproc; /* XXX */
72 struct lwp *lp = curthread->td_lwp;
74 MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
75 M_KTRACE, M_WAITOK);
76 kth->ktr_type = type;
77 /* XXX threaded flag is a hack at the moment */
78 kth->ktr_flags = (p->p_nthreads > 1) ? KTRH_THREADED : 0;
79 microtime(&kth->ktr_time);
80 kth->ktr_pid = p->p_pid;
81 kth->ktr_tid = lp->lwp_tid;
82 bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN + 1);
83 return (kth);
86 void
87 ktrsyscall(struct lwp *lp, int code, int narg, register_t args[])
89 struct ktr_header *kth;
90 struct ktr_syscall *ktp;
91 int len;
92 register_t *argp;
93 int i;
95 len = offsetof(struct ktr_syscall, ktr_args) +
96 (narg * sizeof(register_t));
99 * Setting the active bit prevents a ktrace recursion from the
100 * ktracing op itself.
102 lp->lwp_traceflag |= KTRFAC_ACTIVE;
103 kth = ktrgetheader(KTR_SYSCALL);
104 MALLOC(ktp, struct ktr_syscall *, len, M_KTRACE, M_WAITOK);
105 ktp->ktr_code = code;
106 ktp->ktr_narg = narg;
107 argp = &ktp->ktr_args[0];
108 for (i = 0; i < narg; i++)
109 *argp++ = args[i];
110 kth->ktr_buf = (caddr_t)ktp;
111 kth->ktr_len = len;
112 ktrwrite(lp->lwp_proc, kth, NULL);
113 FREE(ktp, M_KTRACE);
114 FREE(kth, M_KTRACE);
115 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
118 void
119 ktrsysret(struct lwp *lp, int code, int error, register_t retval)
121 struct ktr_header *kth;
122 struct ktr_sysret ktp;
124 lp->lwp_traceflag |= KTRFAC_ACTIVE;
125 kth = ktrgetheader(KTR_SYSRET);
126 ktp.ktr_code = code;
127 ktp.ktr_error = error;
128 ktp.ktr_retval = retval; /* what about val2 ? */
130 kth->ktr_buf = (caddr_t)&ktp;
131 kth->ktr_len = sizeof(struct ktr_sysret);
133 ktrwrite(lp->lwp_proc, kth, NULL);
134 FREE(kth, M_KTRACE);
135 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
138 void
139 ktrnamei(struct lwp *lp, char *path)
141 struct ktr_header *kth;
143 lp->lwp_traceflag |= KTRFAC_ACTIVE;
144 kth = ktrgetheader(KTR_NAMEI);
145 kth->ktr_len = strlen(path);
146 kth->ktr_buf = path;
148 ktrwrite(lp->lwp_proc, kth, NULL);
149 FREE(kth, M_KTRACE);
150 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
153 void
154 ktrgenio(struct lwp *lp, int fd, enum uio_rw rw, struct uio *uio, int error)
156 struct ktr_header *kth;
157 struct ktr_genio ktg;
159 if (error)
160 return;
161 lp->lwp_traceflag |= KTRFAC_ACTIVE;
162 kth = ktrgetheader(KTR_GENIO);
163 ktg.ktr_fd = fd;
164 ktg.ktr_rw = rw;
165 kth->ktr_buf = (caddr_t)&ktg;
166 kth->ktr_len = sizeof(struct ktr_genio);
167 uio->uio_offset = 0;
168 uio->uio_rw = UIO_WRITE;
170 ktrwrite(lp->lwp_proc, kth, uio);
171 FREE(kth, M_KTRACE);
172 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
175 void
176 ktrpsig(struct lwp *lp, int sig, sig_t action, sigset_t *mask, int code)
178 struct ktr_header *kth;
179 struct ktr_psig kp;
181 lp->lwp_traceflag |= KTRFAC_ACTIVE;
182 kth = ktrgetheader(KTR_PSIG);
183 kp.signo = (char)sig;
184 kp.action = action;
185 kp.mask = *mask;
186 kp.code = code;
187 kth->ktr_buf = (caddr_t)&kp;
188 kth->ktr_len = sizeof (struct ktr_psig);
190 ktrwrite(lp->lwp_proc, kth, NULL);
191 FREE(kth, M_KTRACE);
192 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
195 void
196 ktrcsw(struct lwp *lp, int out, int user)
198 struct ktr_header *kth;
199 struct ktr_csw kc;
201 lp->lwp_traceflag |= KTRFAC_ACTIVE;
202 kth = ktrgetheader(KTR_CSW);
203 kc.out = out;
204 kc.user = user;
205 kth->ktr_buf = (caddr_t)&kc;
206 kth->ktr_len = sizeof (struct ktr_csw);
208 ktrwrite(lp->lwp_proc, kth, NULL);
209 FREE(kth, M_KTRACE);
210 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
212 #endif
214 /* Interface and common routines */
216 #ifdef KTRACE
218 * ktrace system call
220 struct ktrace_clear_info {
221 ktrace_node_t tracenode;
222 int rootclear;
223 int error;
226 static int ktrace_clear_callback(struct proc *p, void *data);
228 #endif
231 * MPALMOSTSAFE
234 sys_ktrace(struct ktrace_args *uap)
236 #ifdef KTRACE
237 struct ktrace_clear_info info;
238 struct thread *td = curthread;
239 struct proc *curp = td->td_proc;
240 struct proc *p;
241 struct pgrp *pg;
242 int facs = uap->facs & ~KTRFAC_ROOT;
243 int ops = KTROP(uap->ops);
244 int descend = uap->ops & KTRFLAG_DESCEND;
245 int ret = 0;
246 int error = 0;
247 struct nlookupdata nd;
248 ktrace_node_t tracenode = NULL;
250 get_mplock();
251 curp->p_traceflag |= KTRFAC_ACTIVE;
252 if (ops != KTROP_CLEAR) {
254 * an operation which requires a file argument.
256 error = nlookup_init(&nd, uap->fname,
257 UIO_USERSPACE, NLC_LOCKVP);
258 if (error == 0)
259 error = vn_open(&nd, NULL, FREAD|FWRITE|O_NOFOLLOW, 0);
260 if (error == 0 && nd.nl_open_vp->v_type != VREG)
261 error = EACCES;
262 if (error) {
263 curp->p_traceflag &= ~KTRFAC_ACTIVE;
264 nlookup_done(&nd);
265 goto done;
267 MALLOC(tracenode, ktrace_node_t, sizeof (struct ktrace_node),
268 M_KTRACE, M_WAITOK | M_ZERO);
269 tracenode->kn_vp = nd.nl_open_vp;
270 tracenode->kn_refs = 1;
271 nd.nl_open_vp = NULL;
272 nlookup_done(&nd);
273 vn_unlock(tracenode->kn_vp);
276 * Clear all uses of the tracefile. Not the most efficient operation
277 * in the world.
279 if (ops == KTROP_CLEARFILE) {
280 info.tracenode = tracenode;
281 info.error = 0;
282 info.rootclear = 0;
283 allproc_scan(ktrace_clear_callback, &info);
284 error = info.error;
285 goto done;
288 * need something to (un)trace (XXX - why is this here?)
290 if (!facs) {
291 error = EINVAL;
292 goto done;
295 * do it
297 if (uap->pid < 0) {
299 * by process group
301 pg = pgfind(-uap->pid);
302 if (pg == NULL) {
303 error = ESRCH;
304 goto done;
306 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
307 if (descend)
308 ret |= ktrsetchildren(curp, p, ops, facs, tracenode);
309 else
310 ret |= ktrops(curp, p, ops, facs, tracenode);
312 } else {
314 * by pid
316 p = pfind(uap->pid);
317 if (p == NULL) {
318 error = ESRCH;
319 goto done;
321 if (descend)
322 ret |= ktrsetchildren(curp, p, ops, facs, tracenode);
323 else
324 ret |= ktrops(curp, p, ops, facs, tracenode);
326 if (!ret)
327 error = EPERM;
328 done:
329 if (tracenode)
330 ktrdestroy(&tracenode);
331 curp->p_traceflag &= ~KTRFAC_ACTIVE;
332 rel_mplock();
333 return (error);
334 #else
335 return ENOSYS;
336 #endif
339 #ifdef KTRACE
342 * NOTE: NOT MPSAFE (yet)
344 static int
345 ktrace_clear_callback(struct proc *p, void *data)
347 struct ktrace_clear_info *info = data;
349 if (p->p_tracenode) {
350 if (info->rootclear) {
351 if (p->p_tracenode == info->tracenode) {
352 ktrdestroy(&p->p_tracenode);
353 p->p_traceflag = 0;
355 } else {
356 if (p->p_tracenode->kn_vp == info->tracenode->kn_vp) {
357 if (ktrcanset(curproc, p)) {
358 ktrdestroy(&p->p_tracenode);
359 p->p_traceflag = 0;
360 } else {
361 info->error = EPERM;
366 return(0);
369 #endif
372 * utrace system call
374 * MPALMOSTSAFE
377 sys_utrace(struct utrace_args *uap)
379 #ifdef KTRACE
380 struct ktr_header *kth;
381 struct thread *td = curthread; /* XXX */
382 struct proc *p = td->td_proc;
383 caddr_t cp;
385 if (!KTRPOINT(td, KTR_USER))
386 return (0);
387 if (uap->len > KTR_USER_MAXLEN)
388 return (EINVAL);
389 td->td_lwp->lwp_traceflag |= KTRFAC_ACTIVE;
390 kth = ktrgetheader(KTR_USER);
391 MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
392 if (!copyin(uap->addr, cp, uap->len)) {
393 kth->ktr_buf = cp;
394 kth->ktr_len = uap->len;
395 get_mplock();
396 ktrwrite(p, kth, NULL);
397 rel_mplock();
399 FREE(kth, M_KTRACE);
400 FREE(cp, M_KTRACE);
401 td->td_lwp->lwp_traceflag &= ~KTRFAC_ACTIVE;
403 return (0);
404 #else
405 return (ENOSYS);
406 #endif
409 void
410 ktrdestroy(struct ktrace_node **tracenodep)
412 ktrace_node_t tracenode;
414 if ((tracenode = *tracenodep) != NULL) {
415 *tracenodep = NULL;
416 KKASSERT(tracenode->kn_refs > 0);
417 /* XXX not MP safe yet */
418 --tracenode->kn_refs;
419 if (tracenode->kn_refs == 0) {
420 vn_close(tracenode->kn_vp, FREAD|FWRITE);
421 tracenode->kn_vp = NULL;
422 FREE(tracenode, M_KTRACE);
428 * This allows a process to inherit a ref on a tracenode and is also used
429 * as a temporary ref to prevent a tracenode from being destroyed out from
430 * under an active operation.
432 ktrace_node_t
433 ktrinherit(ktrace_node_t tracenode)
435 if (tracenode) {
436 KKASSERT(tracenode->kn_refs > 0);
437 ++tracenode->kn_refs;
439 return(tracenode);
442 #ifdef KTRACE
443 static int
444 ktrops(struct proc *curp, struct proc *p, int ops, int facs,
445 ktrace_node_t tracenode)
447 ktrace_node_t oldnode;
449 if (!ktrcanset(curp, p))
450 return (0);
451 if (ops == KTROP_SET) {
452 if ((oldnode = p->p_tracenode) != tracenode) {
453 p->p_tracenode = ktrinherit(tracenode);
454 ktrdestroy(&oldnode);
456 p->p_traceflag |= facs;
457 if (curp->p_ucred->cr_uid == 0)
458 p->p_traceflag |= KTRFAC_ROOT;
459 } else {
460 /* KTROP_CLEAR */
461 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
462 /* no more tracing */
463 p->p_traceflag = 0;
464 ktrdestroy(&p->p_tracenode);
468 return (1);
471 static int
472 ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs,
473 ktrace_node_t tracenode)
475 struct proc *p;
476 int ret = 0;
478 p = top;
479 for (;;) {
480 ret |= ktrops(curp, p, ops, facs, tracenode);
482 * If this process has children, descend to them next,
483 * otherwise do any siblings, and if done with this level,
484 * follow back up the tree (but not past top).
486 if (!LIST_EMPTY(&p->p_children))
487 p = LIST_FIRST(&p->p_children);
488 else for (;;) {
489 if (p == top)
490 return (ret);
491 if (LIST_NEXT(p, p_sibling)) {
492 p = LIST_NEXT(p, p_sibling);
493 break;
495 p = p->p_pptr;
498 /*NOTREACHED*/
501 static void
502 ktrwrite(struct proc *p, struct ktr_header *kth, struct uio *uio)
504 struct ktrace_clear_info info;
505 struct uio auio;
506 struct iovec aiov[2];
507 int error;
508 ktrace_node_t tracenode;
511 * We have to ref our tracenode to prevent it from being ripped out
512 * from under us while we are trying to use it. p_tracenode can
513 * go away at any time if another process gets a write error.
515 * XXX not MP safe
517 if (p->p_tracenode == NULL)
518 return;
519 tracenode = ktrinherit(p->p_tracenode);
520 auio.uio_iov = &aiov[0];
521 auio.uio_offset = 0;
522 auio.uio_segflg = UIO_SYSSPACE;
523 auio.uio_rw = UIO_WRITE;
524 aiov[0].iov_base = (caddr_t)kth;
525 aiov[0].iov_len = sizeof(struct ktr_header);
526 auio.uio_resid = sizeof(struct ktr_header);
527 auio.uio_iovcnt = 1;
528 auio.uio_td = curthread;
529 if (kth->ktr_len > 0) {
530 auio.uio_iovcnt++;
531 aiov[1].iov_base = kth->ktr_buf;
532 aiov[1].iov_len = kth->ktr_len;
533 auio.uio_resid += kth->ktr_len;
534 if (uio != NULL)
535 kth->ktr_len += uio->uio_resid;
537 vn_lock(tracenode->kn_vp, LK_EXCLUSIVE | LK_RETRY);
538 error = VOP_WRITE(tracenode->kn_vp, &auio,
539 IO_UNIT | IO_APPEND, p->p_ucred);
540 if (error == 0 && uio != NULL) {
541 error = VOP_WRITE(tracenode->kn_vp, uio,
542 IO_UNIT | IO_APPEND, p->p_ucred);
544 vn_unlock(tracenode->kn_vp);
545 if (error) {
547 * If an error occured, give up tracing on all processes
548 * using this tracenode. This is not MP safe but is
549 * blocking-safe.
551 log(LOG_NOTICE,
552 "ktrace write failed, errno %d, tracing stopped\n", error);
553 info.tracenode = tracenode;
554 info.error = 0;
555 info.rootclear = 1;
556 allproc_scan(ktrace_clear_callback, &info);
558 ktrdestroy(&tracenode);
562 * Return true if caller has permission to set the ktracing state
563 * of target. Essentially, the target can't possess any
564 * more permissions than the caller. KTRFAC_ROOT signifies that
565 * root previously set the tracing status on the target process, and
566 * so, only root may further change it.
568 * TODO: check groups. use caller effective gid.
570 static int
571 ktrcanset(struct proc *callp, struct proc *targetp)
573 struct ucred *caller = callp->p_ucred;
574 struct ucred *target = targetp->p_ucred;
576 if (!PRISON_CHECK(caller, target))
577 return (0);
578 if ((caller->cr_uid == target->cr_ruid &&
579 target->cr_ruid == target->cr_svuid &&
580 caller->cr_rgid == target->cr_rgid && /* XXX */
581 target->cr_rgid == target->cr_svgid &&
582 (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
583 (targetp->p_flag & P_SUGID) == 0) ||
584 caller->cr_uid == 0)
585 return (1);
587 return (0);
590 #endif /* KTRACE */