diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libkvm/kvm_file2.c | 58 | ||||
-rw-r--r-- | lib/libkvm/kvm_proc.c | 19 | ||||
-rw-r--r-- | lib/libkvm/kvm_proc2.c | 35 |
3 files changed, 69 insertions, 43 deletions
diff --git a/lib/libkvm/kvm_file2.c b/lib/libkvm/kvm_file2.c index d1e5f72d328..0930a13048a 100644 --- a/lib/libkvm/kvm_file2.c +++ b/lib/libkvm/kvm_file2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kvm_file2.c,v 1.35 2014/03/30 21:54:49 guenther Exp $ */ +/* $OpenBSD: kvm_file2.c,v 1.36 2014/07/04 05:58:31 guenther Exp $ */ /* * Copyright (c) 2009 Todd C. Miller <Todd.Miller@courtesan.com> @@ -121,7 +121,7 @@ static struct kinfo_file *kvm_deadfile_byfile(kvm_t *, int, int, static struct kinfo_file *kvm_deadfile_byid(kvm_t *, int, int, size_t, int *); static int fill_file(kvm_t *, struct kinfo_file *, struct file *, u_long, - struct vnode *, struct proc *, int, pid_t); + struct vnode *, struct process *, int, pid_t); static int filestat(kvm_t *, struct kinfo_file *, struct vnode *); LIST_HEAD(processlist, process); @@ -315,6 +315,10 @@ kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt) goto cleanup; } + /* skip system, exiting, embryonic and undead processes */ + if (process.ps_flags & (PS_SYSTEM | PS_EMBRYO | PS_EXITING)) + continue; + if (process.ps_mainproc == NULL) continue; if (KREAD(kd, (u_long)process.ps_mainproc, &proc)) { @@ -323,11 +327,6 @@ kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt) goto cleanup; } - /* skip system, exiting, embryonic and undead processes */ - if (proc.p_flag & P_SYSTEM || process.ps_flags & PS_EXITING || - proc.p_stat == SIDL || proc.p_stat == SZOMB) - continue; - if (op == KERN_FILE_BYPID && arg > 0 && proc.p_pid != (pid_t)arg) { /* not the pid we are looking for */ @@ -344,17 +343,17 @@ kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt) process.ps_ucred = &ucred; if (op == KERN_FILE_BYUID && arg >= 0 && - proc.p_ucred->cr_uid != (uid_t)arg) { + process.ps_ucred->cr_uid != (uid_t)arg) { /* not the uid we are looking for */ continue; } - if (KREAD(kd, (u_long)proc.p_fd, &filed0)) { + if (KREAD(kd, (u_long)process.ps_fd, &filed0)) { _kvm_err(kd, kd->program, "can't read filedesc at %lx", - (u_long)proc.p_fd); + (u_long)process.ps_fd); goto cleanup; } - if ((char *)proc.p_fd + offsetof(struct filedesc0, fd_dfiles) + if ((char *)process.ps_fd + offsetof(struct filedesc0,fd_dfiles) == (char *)filed.fd_ofiles) { filed.fd_ofiles = filed0.fd_dfiles; filed.fd_ofileflags = filed0.fd_dfileflags; @@ -377,13 +376,13 @@ kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt) filed.fd_ofileflags = filebuf + (filed.fd_nfiles * sizeof(struct file *)); } - proc.p_fd = &filed; + process.ps_fd = &filed; if (process.ps_textvp) { if (buflen < esize) goto done; if (fill_file(kd, &kf, NULL, 0, process.ps_textvp, - &proc, KERN_FILE_TEXT, proc.p_pid) == -1) + &process, KERN_FILE_TEXT, proc.p_pid) == -1) goto cleanup; memcpy(where, &kf, esize); where += esize; @@ -393,8 +392,8 @@ kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt) if (filed.fd_cdir) { if (buflen < esize) goto done; - if (fill_file(kd, &kf, NULL, 0, filed.fd_cdir, &proc, - KERN_FILE_CDIR, proc.p_pid) == -1) + if (fill_file(kd, &kf, NULL, 0, filed.fd_cdir, + &process, KERN_FILE_CDIR, proc.p_pid) == -1) goto cleanup; memcpy(where, &kf, esize); where += esize; @@ -404,8 +403,8 @@ kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt) if (filed.fd_rdir) { if (buflen < esize) goto done; - if (fill_file(kd, &kf, NULL, 0, filed.fd_rdir, &proc, - KERN_FILE_RDIR, proc.p_pid) == -1) + if (fill_file(kd, &kf, NULL, 0, filed.fd_rdir, + &process, KERN_FILE_RDIR, proc.p_pid) == -1) goto cleanup; memcpy(where, &kf, esize); where += esize; @@ -416,7 +415,7 @@ kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt) if (buflen < esize) goto done; if (fill_file(kd, &kf, NULL, 0, process.ps_tracevp, - &proc, KERN_FILE_TRACE, proc.p_pid) == -1) + &process, KERN_FILE_TRACE, proc.p_pid) == -1) goto cleanup; memcpy(where, &kf, esize); where += esize; @@ -429,7 +428,7 @@ kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt) filed.fd_freefile > filed.fd_lastfile + 1) { _kvm_err(kd, kd->program, "filedesc corrupted at %lx for pid %d", - (u_long)proc.p_fd, proc.p_pid); + (u_long)process.ps_fd, proc.p_pid); goto cleanup; } @@ -443,7 +442,7 @@ kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt) goto cleanup; } if (fill_file(kd, &kf, &file, (u_long)fp, NULL, - &proc, i, proc.p_pid) == -1) + &process, i, proc.p_pid) == -1) goto cleanup; memcpy(where, &kf, esize); where += esize; @@ -461,8 +460,8 @@ cleanup: } static int -fill_file(kvm_t *kd, struct kinfo_file *kf, struct file *fp, u_long fpaddr, struct vnode *vp, - struct proc *p, int fd, pid_t pid) +fill_file(kvm_t *kd, struct kinfo_file *kf, struct file *fp, u_long fpaddr, + struct vnode *vp, struct process *pr, int fd, pid_t pid) { struct ucred f_cred; @@ -683,14 +682,15 @@ fill_file(kvm_t *kd, struct kinfo_file *kf, struct file *fp, u_long fpaddr, stru } /* per-process information for KERN_FILE_BY[PU]ID */ - if (p != NULL) { + if (pr != NULL) { kf->p_pid = pid; - kf->p_uid = p->p_ucred->cr_uid; - kf->p_gid = p->p_ucred->cr_gid; - kf->p_tid = p->p_pid + THREAD_PID_OFFSET; - strlcpy(kf->p_comm, p->p_comm, sizeof(kf->p_comm)); - if (p->p_fd != NULL) - kf->fd_ofileflags = p->p_fd->fd_ofileflags[fd]; + kf->p_uid = pr->ps_ucred->cr_uid; + kf->p_gid = pr->ps_ucred->cr_gid; + kf->p_tid = -1; + strlcpy(kf->p_comm, pr->ps_mainproc->p_comm, + sizeof(kf->p_comm)); + if (pr->ps_fd != NULL) + kf->fd_ofileflags = pr->ps_fd->fd_ofileflags[fd]; } return (0); diff --git a/lib/libkvm/kvm_proc.c b/lib/libkvm/kvm_proc.c index 28cc3c96e62..66dc14d2746 100644 --- a/lib/libkvm/kvm_proc.c +++ b/lib/libkvm/kvm_proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kvm_proc.c,v 1.49 2013/11/01 15:57:56 deraadt Exp $ */ +/* $OpenBSD: kvm_proc.c,v 1.50 2014/07/04 05:58:31 guenther Exp $ */ /* $NetBSD: kvm_proc.c,v 1.30 1999/03/24 05:50:50 mrg Exp $ */ /*- * Copyright (c) 1998 The NetBSD Foundation, Inc. @@ -69,6 +69,7 @@ * most other applications are interested only in open/close/read/nlist). */ +#define __need_process #include <sys/param.h> #include <sys/proc.h> #include <sys/exec.h> @@ -344,16 +345,22 @@ static int proc_verify(kvm_t *kd, const struct kinfo_proc *p) { struct proc kernproc; + struct process kernprocess; + + if (p->p_psflags & (PS_EMBRYO | PS_ZOMBIE)) + return (0); /* * Just read in the whole proc. It's not that big relative * to the cost of the read system call. */ - if (kvm_read(kd, (u_long)p->p_paddr, &kernproc, sizeof(kernproc)) != - sizeof(kernproc)) + if (KREAD(kd, (u_long)p->p_paddr, &kernproc)) + return (0); + if (p->p_pid != kernproc.p_pid) + return (0); + if (KREAD(kd, (u_long)kernproc.p_p, &kernprocess)) return (0); - return (p->p_pid == kernproc.p_pid && - (kernproc.p_stat != SZOMB || p->p_stat == SZOMB)); + return ((kernprocess.ps_flags & (PS_EMBRYO | PS_ZOMBIE)) == 0); } static char ** @@ -381,7 +388,7 @@ kvm_doargv(kvm_t *kd, const struct kinfo_proc *p, int nchr, /* * Pointers are stored at the top of the user stack. */ - if (p->p_stat == SZOMB || + if (p->p_psflags & (PS_EMBRYO | PS_ZOMBIE) || kvm_ureadm(kd, p, (u_long)ps, (char *)&arginfo, sizeof(arginfo)) != sizeof(arginfo)) return (0); diff --git a/lib/libkvm/kvm_proc2.c b/lib/libkvm/kvm_proc2.c index d9cf5fca2f6..2bc877e5198 100644 --- a/lib/libkvm/kvm_proc2.c +++ b/lib/libkvm/kvm_proc2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kvm_proc2.c,v 1.22 2014/05/25 20:28:28 guenther Exp $ */ +/* $OpenBSD: kvm_proc2.c,v 1.23 2014/07/04 05:58:31 guenther Exp $ */ /* $NetBSD: kvm_proc.c,v 1.30 1999/03/24 05:50:50 mrg Exp $ */ /*- * Copyright (c) 1998 The NetBSD Foundation, Inc. @@ -134,13 +134,13 @@ kvm_proclist(kvm_t *kd, int op, int arg, struct process *pr, } if (process.ps_pgrp == NULL) continue; + if (process.ps_flags & PS_EMBRYO) + continue; if (KREAD(kd, (u_long)process.ps_mainproc, &proc)) { _kvm_err(kd, kd->program, "can't read proc at %lx", (u_long)process.ps_mainproc); return (-1); } - if (proc.p_stat == SIDL) - continue; process_pid = proc.p_pid; if (KREAD(kd, (u_long)process.ps_ucred, &ucred)) { _kvm_err(kd, kd->program, "can't read ucred at %lx", @@ -275,7 +275,7 @@ kvm_proclist(kvm_t *kd, int op, int arg, struct process *pr, vmp = NULL; - if (proc.p_stat != SIDL && !P_ZOMBIE(&proc) && + if ((process.ps_flags & PS_ZOMBIE) == 0 && !KREAD(kd, (u_long)process.ps_vmspace, &vm)) vmp = &vm; @@ -306,7 +306,13 @@ kvm_proclist(kvm_t *kd, int op, int arg, struct process *pr, } /* update %cpu for all threads */ - if (!dothreads) { + if (dothreads) { + kp.p_pctcpu = proc.p_pctcpu; + kp.p_stat = proc.p_stat; + } else { + kp.p_pctcpu = 0; + kp.p_stat = (process.ps_flags & PS_ZOMBIE) ? SDEAD : + SIDL; for (p = TAILQ_FIRST(&process.ps_threads); p != NULL; p = TAILQ_NEXT(&proc, p_thr_link)) { if (KREAD(kd, (u_long)p, &proc)) { @@ -315,11 +321,24 @@ kvm_proclist(kvm_t *kd, int op, int arg, struct process *pr, (u_long)p); return (-1); } - if (p == process.ps_mainproc) - continue; kp.p_pctcpu += proc.p_pctcpu; + /* + * find best state: + * ONPROC > RUN > STOP > SLEEP > ... + */ + if (proc.p_stat == SONPROC || + kp.p_stat == SONPROC) + kp.p_stat = SONPROC; + else if (proc.p_stat == SRUN || + kp.p_stat == SRUN) + kp.p_stat = SRUN; + else if (proc.p_stat == SSTOP || + kp.p_stat == SSTOP) + kp.p_stat = SSTOP; + else if (proc.p_stat == SSLEEP) + kp.p_stat = SSLEEP; } - } + } memcpy(bp, &kp, esize); bp += esize; |