kern_ktrace.c revision 1.33.6.1 1 1.33.6.1 he /* $NetBSD: kern_ktrace.c,v 1.33.6.1 2000/04/30 12:08:33 he Exp $ */
2 1.11 cgd
3 1.1 cgd /*
4 1.9 cgd * Copyright (c) 1989, 1993
5 1.9 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.25 fvdl * @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95
36 1.1 cgd */
37 1.29 thorpej
38 1.29 thorpej #include "opt_ktrace.h"
39 1.1 cgd
40 1.9 cgd #ifdef KTRACE
41 1.9 cgd
42 1.7 mycroft #include <sys/param.h>
43 1.13 cgd #include <sys/systm.h>
44 1.7 mycroft #include <sys/proc.h>
45 1.7 mycroft #include <sys/file.h>
46 1.7 mycroft #include <sys/namei.h>
47 1.7 mycroft #include <sys/vnode.h>
48 1.7 mycroft #include <sys/ktrace.h>
49 1.7 mycroft #include <sys/malloc.h>
50 1.7 mycroft #include <sys/syslog.h>
51 1.28 christos #include <sys/filedesc.h>
52 1.1 cgd
53 1.13 cgd #include <sys/mount.h>
54 1.13 cgd #include <sys/syscallargs.h>
55 1.22 christos
56 1.33.6.1 he #include <vm/vm.h> /* XXX for uvmexp, needed by PAGE_SIZE */
57 1.33.6.1 he #include <uvm/uvm.h> /* XXX for uvmexp, needed by PAGE_SIZE */
58 1.33.6.1 he
59 1.33.6.1 he void ktrinitheader __P((struct ktr_header *, struct proc *, int));
60 1.33.6.1 he int ktrops __P((struct proc *, struct proc *, int, int, void *));
61 1.33.6.1 he int ktrsetchildren __P((struct proc *, struct proc *, int, int, void *));
62 1.33.6.1 he int ktrwrite __P((struct proc *, void *, struct ktr_header *));
63 1.33.6.1 he int ktrcanset __P((struct proc *, struct proc *));
64 1.22 christos
65 1.28 christos void
66 1.28 christos ktrderef(p)
67 1.28 christos struct proc *p;
68 1.28 christos {
69 1.28 christos if (p->p_tracep == NULL)
70 1.28 christos return;
71 1.28 christos
72 1.28 christos if (p->p_traceflag & KTRFAC_FD) {
73 1.28 christos struct file *fp = p->p_tracep;
74 1.28 christos
75 1.28 christos closef(fp, NULL);
76 1.28 christos } else {
77 1.28 christos struct vnode *vp = p->p_tracep;
78 1.28 christos
79 1.28 christos vrele(vp);
80 1.28 christos }
81 1.28 christos p->p_tracep = NULL;
82 1.28 christos p->p_traceflag = 0;
83 1.28 christos }
84 1.28 christos
85 1.28 christos void
86 1.28 christos ktradref(p)
87 1.28 christos struct proc *p;
88 1.28 christos {
89 1.28 christos if (p->p_traceflag & KTRFAC_FD) {
90 1.28 christos struct file *fp = p->p_tracep;
91 1.28 christos
92 1.28 christos fp->f_count++;
93 1.28 christos } else {
94 1.28 christos struct vnode *vp = p->p_tracep;
95 1.28 christos
96 1.28 christos VREF(vp);
97 1.28 christos }
98 1.28 christos }
99 1.28 christos
100 1.33.6.1 he void
101 1.33.6.1 he ktrinitheader(kth, p, type)
102 1.33.6.1 he struct ktr_header *kth;
103 1.33.6.1 he struct proc *p;
104 1.4 andrew int type;
105 1.1 cgd {
106 1.1 cgd
107 1.33.6.1 he memset(kth, 0, sizeof(*kth));
108 1.1 cgd kth->ktr_type = type;
109 1.1 cgd microtime(&kth->ktr_time);
110 1.1 cgd kth->ktr_pid = p->p_pid;
111 1.32 perry memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
112 1.1 cgd }
113 1.1 cgd
114 1.17 cgd void
115 1.28 christos ktrsyscall(v, code, argsize, args)
116 1.28 christos void *v;
117 1.16 mycroft register_t code;
118 1.16 mycroft size_t argsize;
119 1.16 mycroft register_t args[];
120 1.1 cgd {
121 1.33.6.1 he struct ktr_header kth;
122 1.33.6.1 he struct ktr_syscall *ktp;
123 1.9 cgd struct proc *p = curproc; /* XXX */
124 1.17 cgd register_t *argp;
125 1.33.6.1 he size_t len = sizeof(struct ktr_syscall) + argsize;
126 1.17 cgd int i;
127 1.1 cgd
128 1.9 cgd p->p_traceflag |= KTRFAC_ACTIVE;
129 1.33.6.1 he ktrinitheader(&kth, p, KTR_SYSCALL);
130 1.33.6.1 he ktp = malloc(len, M_TEMP, M_WAITOK);
131 1.1 cgd ktp->ktr_code = code;
132 1.17 cgd ktp->ktr_argsize = argsize;
133 1.17 cgd argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
134 1.31 perry for (i = 0; i < (argsize / sizeof(*argp)); i++)
135 1.1 cgd *argp++ = args[i];
136 1.33.6.1 he kth.ktr_buf = (caddr_t)ktp;
137 1.33.6.1 he kth.ktr_len = len;
138 1.33.6.1 he (void) ktrwrite(p, v, &kth);
139 1.33.6.1 he free(ktp, M_TEMP);
140 1.9 cgd p->p_traceflag &= ~KTRFAC_ACTIVE;
141 1.1 cgd }
142 1.1 cgd
143 1.17 cgd void
144 1.28 christos ktrsysret(v, code, error, retval)
145 1.28 christos void *v;
146 1.16 mycroft register_t code;
147 1.16 mycroft int error;
148 1.16 mycroft register_t retval;
149 1.1 cgd {
150 1.33.6.1 he struct ktr_header kth;
151 1.1 cgd struct ktr_sysret ktp;
152 1.9 cgd struct proc *p = curproc; /* XXX */
153 1.1 cgd
154 1.9 cgd p->p_traceflag |= KTRFAC_ACTIVE;
155 1.33.6.1 he ktrinitheader(&kth, p, KTR_SYSRET);
156 1.1 cgd ktp.ktr_code = code;
157 1.1 cgd ktp.ktr_error = error;
158 1.1 cgd ktp.ktr_retval = retval; /* what about val2 ? */
159 1.1 cgd
160 1.33.6.1 he kth.ktr_buf = (caddr_t)&ktp;
161 1.33.6.1 he kth.ktr_len = sizeof(struct ktr_sysret);
162 1.1 cgd
163 1.33.6.1 he (void) ktrwrite(p, v, &kth);
164 1.9 cgd p->p_traceflag &= ~KTRFAC_ACTIVE;
165 1.1 cgd }
166 1.1 cgd
167 1.17 cgd void
168 1.28 christos ktrnamei(v, path)
169 1.28 christos void *v;
170 1.1 cgd char *path;
171 1.1 cgd {
172 1.33.6.1 he struct ktr_header kth;
173 1.9 cgd struct proc *p = curproc; /* XXX */
174 1.1 cgd
175 1.9 cgd p->p_traceflag |= KTRFAC_ACTIVE;
176 1.33.6.1 he ktrinitheader(&kth, p, KTR_NAMEI);
177 1.33.6.1 he kth.ktr_len = strlen(path);
178 1.33.6.1 he kth.ktr_buf = path;
179 1.18 christos
180 1.33.6.1 he (void) ktrwrite(p, v, &kth);
181 1.18 christos p->p_traceflag &= ~KTRFAC_ACTIVE;
182 1.18 christos }
183 1.18 christos
184 1.18 christos void
185 1.28 christos ktremul(v, p, emul)
186 1.28 christos void *v;
187 1.28 christos struct proc *p;
188 1.18 christos char *emul;
189 1.18 christos {
190 1.33.6.1 he struct ktr_header kth;
191 1.18 christos
192 1.18 christos p->p_traceflag |= KTRFAC_ACTIVE;
193 1.33.6.1 he ktrinitheader(&kth, p, KTR_EMUL);
194 1.33.6.1 he kth.ktr_len = strlen(emul);
195 1.33.6.1 he kth.ktr_buf = emul;
196 1.1 cgd
197 1.33.6.1 he (void) ktrwrite(p, v, &kth);
198 1.9 cgd p->p_traceflag &= ~KTRFAC_ACTIVE;
199 1.1 cgd }
200 1.1 cgd
201 1.17 cgd void
202 1.28 christos ktrgenio(v, fd, rw, iov, len, error)
203 1.28 christos void *v;
204 1.1 cgd int fd;
205 1.1 cgd enum uio_rw rw;
206 1.28 christos struct iovec *iov;
207 1.4 andrew int len, error;
208 1.1 cgd {
209 1.33.6.1 he struct ktr_header kth;
210 1.28 christos struct ktr_genio *ktp;
211 1.28 christos caddr_t cp;
212 1.28 christos int resid = len, cnt;
213 1.9 cgd struct proc *p = curproc; /* XXX */
214 1.33.6.1 he int buflen;
215 1.33.6.1 he
216 1.1 cgd if (error)
217 1.1 cgd return;
218 1.33.6.1 he
219 1.9 cgd p->p_traceflag |= KTRFAC_ACTIVE;
220 1.33.6.1 he
221 1.33.6.1 he buflen = min(PAGE_SIZE, len + sizeof(struct ktr_genio));
222 1.33.6.1 he
223 1.33.6.1 he ktrinitheader(&kth, p, KTR_GENIO);
224 1.33.6.1 he ktp = malloc(buflen, M_TEMP, M_WAITOK);
225 1.1 cgd ktp->ktr_fd = fd;
226 1.1 cgd ktp->ktr_rw = rw;
227 1.33.6.1 he
228 1.33.6.1 he kth.ktr_buf = (caddr_t)ktp;
229 1.33.6.1 he
230 1.31 perry cp = (caddr_t)((char *)ktp + sizeof(struct ktr_genio));
231 1.33.6.1 he buflen -= sizeof(struct ktr_genio);
232 1.33.6.1 he
233 1.1 cgd while (resid > 0) {
234 1.33.6.1 he if (p->p_schedflags & PSCHED_SHOULDYIELD)
235 1.33.6.1 he preempt(NULL);
236 1.33.6.1 he
237 1.33.6.1 he cnt = min(iov->iov_len, buflen);
238 1.33.6.1 he if (cnt > resid)
239 1.1 cgd cnt = resid;
240 1.33.6.1 he if (copyin(iov->iov_base, cp, cnt))
241 1.33.6.1 he break;
242 1.33.6.1 he
243 1.33.6.1 he kth.ktr_len = cnt + sizeof(struct ktr_genio);
244 1.33.6.1 he
245 1.33.6.1 he if (ktrwrite(p, v, &kth) != 0)
246 1.33.6.1 he break;
247 1.33.6.1 he
248 1.33.6.1 he iov->iov_base = (caddr_t)iov->iov_base + cnt;
249 1.33.6.1 he iov->iov_len -= cnt;
250 1.33.6.1 he
251 1.33.6.1 he if (iov->iov_len == 0)
252 1.33.6.1 he iov++;
253 1.33.6.1 he
254 1.1 cgd resid -= cnt;
255 1.1 cgd }
256 1.1 cgd
257 1.33.6.1 he free(ktp, M_TEMP);
258 1.9 cgd p->p_traceflag &= ~KTRFAC_ACTIVE;
259 1.1 cgd }
260 1.1 cgd
261 1.17 cgd void
262 1.28 christos ktrpsig(v, sig, action, mask, code)
263 1.28 christos void *v;
264 1.9 cgd int sig;
265 1.9 cgd sig_t action;
266 1.33 mycroft sigset_t *mask;
267 1.33 mycroft int code;
268 1.1 cgd {
269 1.33.6.1 he struct ktr_header kth;
270 1.1 cgd struct ktr_psig kp;
271 1.9 cgd struct proc *p = curproc; /* XXX */
272 1.1 cgd
273 1.9 cgd p->p_traceflag |= KTRFAC_ACTIVE;
274 1.33.6.1 he ktrinitheader(&kth, p, KTR_PSIG);
275 1.1 cgd kp.signo = (char)sig;
276 1.1 cgd kp.action = action;
277 1.33 mycroft kp.mask = *mask;
278 1.1 cgd kp.code = code;
279 1.33.6.1 he kth.ktr_buf = (caddr_t)&kp;
280 1.33.6.1 he kth.ktr_len = sizeof(struct ktr_psig);
281 1.1 cgd
282 1.33.6.1 he (void) ktrwrite(p, v, &kth);
283 1.9 cgd p->p_traceflag &= ~KTRFAC_ACTIVE;
284 1.9 cgd }
285 1.9 cgd
286 1.17 cgd void
287 1.28 christos ktrcsw(v, out, user)
288 1.28 christos void *v;
289 1.9 cgd int out, user;
290 1.9 cgd {
291 1.33.6.1 he struct ktr_header kth;
292 1.33.6.1 he struct ktr_csw kc;
293 1.9 cgd struct proc *p = curproc; /* XXX */
294 1.9 cgd
295 1.9 cgd p->p_traceflag |= KTRFAC_ACTIVE;
296 1.33.6.1 he ktrinitheader(&kth, p, KTR_CSW);
297 1.9 cgd kc.out = out;
298 1.9 cgd kc.user = user;
299 1.33.6.1 he kth.ktr_buf = (caddr_t)&kc;
300 1.33.6.1 he kth.ktr_len = sizeof(struct ktr_csw);
301 1.9 cgd
302 1.33.6.1 he (void) ktrwrite(p, v, &kth);
303 1.9 cgd p->p_traceflag &= ~KTRFAC_ACTIVE;
304 1.1 cgd }
305 1.1 cgd
306 1.1 cgd /* Interface and common routines */
307 1.1 cgd
308 1.1 cgd /*
309 1.1 cgd * ktrace system call
310 1.1 cgd */
311 1.1 cgd /* ARGSUSED */
312 1.17 cgd int
313 1.28 christos sys_fktrace(curp, v, retval)
314 1.28 christos struct proc *curp;
315 1.28 christos void *v;
316 1.28 christos register_t *retval;
317 1.28 christos {
318 1.28 christos struct sys_fktrace_args /* {
319 1.28 christos syscallarg(int) fd;
320 1.28 christos syscallarg(int) ops;
321 1.28 christos syscallarg(int) facs;
322 1.28 christos syscallarg(int) pid;
323 1.28 christos } */ *uap = v;
324 1.28 christos struct file *fp = NULL;
325 1.28 christos struct proc *p;
326 1.28 christos struct filedesc *fdp = curp->p_fd;
327 1.28 christos struct pgrp *pg;
328 1.28 christos int facs;
329 1.28 christos int ops;
330 1.28 christos int descend;
331 1.28 christos int ret = 0;
332 1.28 christos int error = 0;
333 1.28 christos
334 1.28 christos if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
335 1.28 christos (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
336 1.28 christos (fp->f_flag & FWRITE) == 0)
337 1.28 christos return (EBADF);
338 1.28 christos
339 1.28 christos ops = KTROP(SCARG(uap, ops)) | KTRFLAG_FD;
340 1.28 christos descend = SCARG(uap, ops) & KTRFLAG_DESCEND;
341 1.28 christos facs = SCARG(uap, facs) & ~((unsigned) KTRFAC_ROOT);
342 1.28 christos curp->p_traceflag |= KTRFAC_ACTIVE;
343 1.28 christos
344 1.28 christos /*
345 1.28 christos * Clear all uses of the tracefile
346 1.28 christos */
347 1.28 christos if (KTROP(ops) == KTROP_CLEARFILE) {
348 1.28 christos for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
349 1.28 christos if (p->p_tracep == fp) {
350 1.28 christos if (ktrcanset(curp, p))
351 1.28 christos ktrderef(p);
352 1.28 christos else
353 1.28 christos error = EPERM;
354 1.28 christos }
355 1.28 christos }
356 1.28 christos goto done;
357 1.28 christos }
358 1.28 christos /*
359 1.28 christos * need something to (un)trace (XXX - why is this here?)
360 1.28 christos */
361 1.28 christos if (!facs) {
362 1.28 christos error = EINVAL;
363 1.28 christos goto done;
364 1.28 christos }
365 1.28 christos /*
366 1.28 christos * do it
367 1.28 christos */
368 1.28 christos if (SCARG(uap, pid) < 0) {
369 1.28 christos /*
370 1.28 christos * by process group
371 1.28 christos */
372 1.28 christos pg = pgfind(-SCARG(uap, pid));
373 1.28 christos if (pg == NULL) {
374 1.28 christos error = ESRCH;
375 1.28 christos goto done;
376 1.28 christos }
377 1.33.6.1 he for (p = LIST_FIRST(&pg->pg_members); p != NULL;
378 1.33.6.1 he p = LIST_NEXT(p, p_pglist)) {
379 1.28 christos if (descend)
380 1.28 christos ret |= ktrsetchildren(curp, p, ops, facs, fp);
381 1.28 christos else
382 1.28 christos ret |= ktrops(curp, p, ops, facs, fp);
383 1.33.6.1 he }
384 1.28 christos
385 1.28 christos } else {
386 1.28 christos /*
387 1.28 christos * by pid
388 1.28 christos */
389 1.28 christos p = pfind(SCARG(uap, pid));
390 1.28 christos if (p == NULL) {
391 1.28 christos error = ESRCH;
392 1.28 christos goto done;
393 1.28 christos }
394 1.28 christos if (descend)
395 1.28 christos ret |= ktrsetchildren(curp, p, ops, facs, fp);
396 1.28 christos else
397 1.28 christos ret |= ktrops(curp, p, ops, facs, fp);
398 1.28 christos }
399 1.28 christos if (!ret)
400 1.28 christos error = EPERM;
401 1.28 christos done:
402 1.28 christos curp->p_traceflag &= ~KTRFAC_ACTIVE;
403 1.28 christos return (error);
404 1.28 christos }
405 1.28 christos
406 1.28 christos /*
407 1.28 christos * ktrace system call
408 1.28 christos */
409 1.28 christos /* ARGSUSED */
410 1.28 christos int
411 1.20 mycroft sys_ktrace(curp, v, retval)
412 1.1 cgd struct proc *curp;
413 1.19 thorpej void *v;
414 1.19 thorpej register_t *retval;
415 1.19 thorpej {
416 1.28 christos struct sys_ktrace_args /* {
417 1.24 mycroft syscallarg(const char *) fname;
418 1.13 cgd syscallarg(int) ops;
419 1.13 cgd syscallarg(int) facs;
420 1.13 cgd syscallarg(int) pid;
421 1.19 thorpej } */ *uap = v;
422 1.28 christos struct vnode *vp = NULL;
423 1.28 christos struct proc *p;
424 1.1 cgd struct pgrp *pg;
425 1.22 christos int facs = SCARG(uap, facs) & ~((unsigned) KTRFAC_ROOT);
426 1.13 cgd int ops = KTROP(SCARG(uap, ops));
427 1.13 cgd int descend = SCARG(uap, ops) & KTRFLAG_DESCEND;
428 1.1 cgd int ret = 0;
429 1.1 cgd int error = 0;
430 1.1 cgd struct nameidata nd;
431 1.1 cgd
432 1.9 cgd curp->p_traceflag |= KTRFAC_ACTIVE;
433 1.1 cgd if (ops != KTROP_CLEAR) {
434 1.1 cgd /*
435 1.1 cgd * an operation which requires a file argument.
436 1.1 cgd */
437 1.13 cgd NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
438 1.13 cgd curp);
439 1.22 christos if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
440 1.9 cgd curp->p_traceflag &= ~KTRFAC_ACTIVE;
441 1.1 cgd return (error);
442 1.9 cgd }
443 1.1 cgd vp = nd.ni_vp;
444 1.25 fvdl VOP_UNLOCK(vp, 0);
445 1.1 cgd if (vp->v_type != VREG) {
446 1.1 cgd (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
447 1.9 cgd curp->p_traceflag &= ~KTRFAC_ACTIVE;
448 1.1 cgd return (EACCES);
449 1.1 cgd }
450 1.1 cgd }
451 1.1 cgd /*
452 1.1 cgd * Clear all uses of the tracefile
453 1.1 cgd */
454 1.28 christos if (KTROP(ops) == KTROP_CLEARFILE) {
455 1.12 mycroft for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
456 1.27 mycroft if (p->p_tracep == vp &&
457 1.27 mycroft !ktrops(curp, p, KTROP_CLEAR, ~0, vp))
458 1.27 mycroft error = EPERM;
459 1.1 cgd }
460 1.1 cgd goto done;
461 1.1 cgd }
462 1.1 cgd /*
463 1.1 cgd * need something to (un)trace (XXX - why is this here?)
464 1.1 cgd */
465 1.1 cgd if (!facs) {
466 1.1 cgd error = EINVAL;
467 1.1 cgd goto done;
468 1.1 cgd }
469 1.1 cgd /*
470 1.1 cgd * do it
471 1.1 cgd */
472 1.13 cgd if (SCARG(uap, pid) < 0) {
473 1.1 cgd /*
474 1.1 cgd * by process group
475 1.1 cgd */
476 1.13 cgd pg = pgfind(-SCARG(uap, pid));
477 1.1 cgd if (pg == NULL) {
478 1.1 cgd error = ESRCH;
479 1.1 cgd goto done;
480 1.1 cgd }
481 1.33.6.1 he for (p = LIST_FIRST(&pg->pg_members); p != NULL;
482 1.33.6.1 he p = LIST_NEXT(p, p_pglist)) {
483 1.1 cgd if (descend)
484 1.1 cgd ret |= ktrsetchildren(curp, p, ops, facs, vp);
485 1.1 cgd else
486 1.1 cgd ret |= ktrops(curp, p, ops, facs, vp);
487 1.33.6.1 he }
488 1.1 cgd
489 1.1 cgd } else {
490 1.1 cgd /*
491 1.1 cgd * by pid
492 1.1 cgd */
493 1.13 cgd p = pfind(SCARG(uap, pid));
494 1.1 cgd if (p == NULL) {
495 1.1 cgd error = ESRCH;
496 1.1 cgd goto done;
497 1.1 cgd }
498 1.1 cgd if (descend)
499 1.1 cgd ret |= ktrsetchildren(curp, p, ops, facs, vp);
500 1.1 cgd else
501 1.1 cgd ret |= ktrops(curp, p, ops, facs, vp);
502 1.1 cgd }
503 1.1 cgd if (!ret)
504 1.1 cgd error = EPERM;
505 1.1 cgd done:
506 1.1 cgd if (vp != NULL)
507 1.1 cgd (void) vn_close(vp, FWRITE, curp->p_ucred, curp);
508 1.9 cgd curp->p_traceflag &= ~KTRFAC_ACTIVE;
509 1.1 cgd return (error);
510 1.1 cgd }
511 1.1 cgd
512 1.4 andrew int
513 1.28 christos ktrops(curp, p, ops, facs, v)
514 1.9 cgd struct proc *p, *curp;
515 1.4 andrew int ops, facs;
516 1.28 christos void *v;
517 1.1 cgd {
518 1.1 cgd
519 1.1 cgd if (!ktrcanset(curp, p))
520 1.1 cgd return (0);
521 1.28 christos if (KTROP(ops) == KTROP_SET) {
522 1.28 christos if (p->p_tracep != v) {
523 1.1 cgd /*
524 1.1 cgd * if trace file already in use, relinquish
525 1.1 cgd */
526 1.28 christos ktrderef(p);
527 1.28 christos if (ops & KTRFLAG_FD)
528 1.28 christos p->p_traceflag = KTRFAC_FD;
529 1.28 christos p->p_tracep = v;
530 1.28 christos ktradref(p);
531 1.1 cgd }
532 1.1 cgd p->p_traceflag |= facs;
533 1.1 cgd if (curp->p_ucred->cr_uid == 0)
534 1.1 cgd p->p_traceflag |= KTRFAC_ROOT;
535 1.1 cgd } else {
536 1.1 cgd /* KTROP_CLEAR */
537 1.1 cgd if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
538 1.1 cgd /* no more tracing */
539 1.28 christos ktrderef(p);
540 1.1 cgd }
541 1.1 cgd }
542 1.21 christos
543 1.21 christos /*
544 1.21 christos * Emit an emulation record, every time there is a ktrace
545 1.21 christos * change/attach request.
546 1.21 christos */
547 1.21 christos if (KTRPOINT(p, KTR_EMUL))
548 1.28 christos ktremul(p->p_tracep, p, p->p_emul->e_name);
549 1.1 cgd
550 1.1 cgd return (1);
551 1.1 cgd }
552 1.1 cgd
553 1.22 christos int
554 1.28 christos ktrsetchildren(curp, top, ops, facs, v)
555 1.1 cgd struct proc *curp, *top;
556 1.4 andrew int ops, facs;
557 1.28 christos void *v;
558 1.1 cgd {
559 1.28 christos struct proc *p;
560 1.28 christos int ret = 0;
561 1.1 cgd
562 1.1 cgd p = top;
563 1.1 cgd for (;;) {
564 1.28 christos ret |= ktrops(curp, p, ops, facs, v);
565 1.1 cgd /*
566 1.1 cgd * If this process has children, descend to them next,
567 1.1 cgd * otherwise do any siblings, and if done with this level,
568 1.1 cgd * follow back up the tree (but not past top).
569 1.1 cgd */
570 1.33.6.1 he if (LIST_FIRST(&p->p_children) != NULL)
571 1.33.6.1 he p = LIST_FIRST(&p->p_children);
572 1.1 cgd else for (;;) {
573 1.1 cgd if (p == top)
574 1.1 cgd return (ret);
575 1.33.6.1 he if (LIST_NEXT(p, p_sibling) != NULL) {
576 1.33.6.1 he p = LIST_NEXT(p, p_sibling);
577 1.1 cgd break;
578 1.1 cgd }
579 1.12 mycroft p = p->p_pptr;
580 1.1 cgd }
581 1.1 cgd }
582 1.1 cgd /*NOTREACHED*/
583 1.1 cgd }
584 1.1 cgd
585 1.33.6.1 he int
586 1.28 christos ktrwrite(p, v, kth)
587 1.28 christos struct proc *p;
588 1.28 christos void *v;
589 1.28 christos struct ktr_header *kth;
590 1.1 cgd {
591 1.1 cgd struct uio auio;
592 1.1 cgd struct iovec aiov[2];
593 1.1 cgd int error;
594 1.1 cgd
595 1.28 christos if (v == NULL)
596 1.33.6.1 he return (0);
597 1.1 cgd auio.uio_iov = &aiov[0];
598 1.1 cgd auio.uio_offset = 0;
599 1.1 cgd auio.uio_segflg = UIO_SYSSPACE;
600 1.1 cgd auio.uio_rw = UIO_WRITE;
601 1.1 cgd aiov[0].iov_base = (caddr_t)kth;
602 1.1 cgd aiov[0].iov_len = sizeof(struct ktr_header);
603 1.1 cgd auio.uio_resid = sizeof(struct ktr_header);
604 1.1 cgd auio.uio_iovcnt = 1;
605 1.1 cgd auio.uio_procp = (struct proc *)0;
606 1.1 cgd if (kth->ktr_len > 0) {
607 1.1 cgd auio.uio_iovcnt++;
608 1.1 cgd aiov[1].iov_base = kth->ktr_buf;
609 1.1 cgd aiov[1].iov_len = kth->ktr_len;
610 1.1 cgd auio.uio_resid += kth->ktr_len;
611 1.1 cgd }
612 1.28 christos if (p->p_traceflag & KTRFAC_FD) {
613 1.28 christos struct file *fp = v;
614 1.28 christos
615 1.30 thorpej error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
616 1.30 thorpej fp->f_cred, FOF_UPDATE_OFFSET);
617 1.28 christos }
618 1.28 christos else {
619 1.28 christos struct vnode *vp = v;
620 1.28 christos
621 1.28 christos vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
622 1.28 christos error = VOP_WRITE(vp, &auio, IO_UNIT|IO_APPEND, p->p_ucred);
623 1.28 christos VOP_UNLOCK(vp, 0);
624 1.28 christos }
625 1.33.6.1 he if (error == 0)
626 1.33.6.1 he return (0);
627 1.1 cgd /*
628 1.33.6.1 he * If error encountered, give up tracing on this vnode. Don't report
629 1.33.6.1 he * EPIPE as this can easily happen with fktrace()/ktruss.
630 1.1 cgd */
631 1.33.6.1 he if (error != EPIPE)
632 1.33.6.1 he log(LOG_NOTICE,
633 1.33.6.1 he "ktrace write failed, errno %d, tracing stopped\n",
634 1.33.6.1 he error);
635 1.12 mycroft for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
636 1.28 christos if (p->p_tracep == v)
637 1.28 christos ktrderef(p);
638 1.1 cgd }
639 1.33.6.1 he
640 1.33.6.1 he return (error);
641 1.1 cgd }
642 1.1 cgd
643 1.1 cgd /*
644 1.1 cgd * Return true if caller has permission to set the ktracing state
645 1.1 cgd * of target. Essentially, the target can't possess any
646 1.1 cgd * more permissions than the caller. KTRFAC_ROOT signifies that
647 1.1 cgd * root previously set the tracing status on the target process, and
648 1.1 cgd * so, only root may further change it.
649 1.1 cgd *
650 1.1 cgd * TODO: check groups. use caller effective gid.
651 1.1 cgd */
652 1.22 christos int
653 1.1 cgd ktrcanset(callp, targetp)
654 1.1 cgd struct proc *callp, *targetp;
655 1.1 cgd {
656 1.28 christos struct pcred *caller = callp->p_cred;
657 1.28 christos struct pcred *target = targetp->p_cred;
658 1.1 cgd
659 1.1 cgd if ((caller->pc_ucred->cr_uid == target->p_ruid &&
660 1.1 cgd target->p_ruid == target->p_svuid &&
661 1.1 cgd caller->p_rgid == target->p_rgid && /* XXX */
662 1.1 cgd target->p_rgid == target->p_svgid &&
663 1.1 cgd (targetp->p_traceflag & KTRFAC_ROOT) == 0) ||
664 1.1 cgd caller->pc_ucred->cr_uid == 0)
665 1.1 cgd return (1);
666 1.1 cgd
667 1.1 cgd return (0);
668 1.1 cgd }
669 1.9 cgd
670 1.9 cgd #endif
671