summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bin/ps/print.c8
-rw-r--r--lib/libkvm/kvm_file2.c58
-rw-r--r--lib/libkvm/kvm_proc.c19
-rw-r--r--lib/libkvm/kvm_proc2.c35
-rw-r--r--sys/kern/kern_exit.c6
-rw-r--r--sys/kern/kern_fork.c24
-rw-r--r--sys/kern/kern_proc.c4
-rw-r--r--sys/kern/kern_sig.c5
-rw-r--r--sys/kern/kern_sysctl.c109
-rw-r--r--sys/kern/sched_bsd.c3
-rw-r--r--sys/kern/tty.c7
-rw-r--r--sys/miscfs/procfs/procfs_cmdline.c9
-rw-r--r--sys/sys/proc.h18
-rw-r--r--sys/sys/sysctl.h7
-rw-r--r--usr.bin/tmux/procname.c4
-rw-r--r--usr.bin/top/machine.c4
-rw-r--r--usr.bin/w/proc_compare.c4
-rw-r--r--usr.bin/w/w.c4
-rw-r--r--usr.sbin/snmpd/mib.c3
19 files changed, 185 insertions, 146 deletions
diff --git a/bin/ps/print.c b/bin/ps/print.c
index 92ae064ba18..afad45dd671 100644
--- a/bin/ps/print.c
+++ b/bin/ps/print.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: print.c,v 1.56 2013/11/11 23:07:15 deraadt Exp $ */
+/* $OpenBSD: print.c,v 1.57 2014/07/04 05:58:31 guenther Exp $ */
/* $NetBSD: print.c,v 1.27 1995/09/29 21:58:12 cgd Exp $ */
/*-
@@ -238,7 +238,7 @@ state(const struct kinfo_proc *kp, VARENT *ve)
state = *cp = 'R';
break;
- case SZOMB:
+ case SDEAD:
*cp = 'Z';
break;
@@ -255,7 +255,7 @@ state(const struct kinfo_proc *kp, VARENT *ve)
*cp++ = 'X';
if (flag & P_SYSTRACE)
*cp++ = 'x';
- if (flag & P_WEXIT && kp->p_stat != SZOMB)
+ if ((kp->p_psflags & (PS_EXITING | PS_ZOMBIE)) == PS_EXITING)
*cp++ = 'E';
if (kp->p_psflags & PS_ISPWAIT)
*cp++ = 'V';
@@ -489,7 +489,7 @@ cputime(const struct kinfo_proc *kp, VARENT *ve)
char obuff[128];
v = ve->var;
- if (kp->p_stat == SZOMB || !kp->p_uvalid) {
+ if (kp->p_stat == SDEAD || !kp->p_uvalid) {
secs = 0;
psecs = 0;
} else {
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;
diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c
index 97d1a76b3f2..4d1d8445c2b 100644
--- a/sys/kern/kern_exit.c
+++ b/sys/kern/kern_exit.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_exit.c,v 1.143 2014/06/11 20:39:18 matthew Exp $ */
+/* $OpenBSD: kern_exit.c,v 1.144 2014/07/04 05:58:30 guenther Exp $ */
/* $NetBSD: kern_exit.c,v 1.39 1996/04/22 01:38:25 christos Exp $ */
/*
@@ -460,7 +460,7 @@ reaper(void)
if ((pr->ps_flags & PS_NOZOMBIE) == 0) {
/* Process is now a true zombie. */
- p->p_stat = SZOMB;
+ atomic_setbits_int(&pr->ps_flags, PS_ZOMBIE);
prsignal(pr->ps_pptr, SIGCHLD);
/* Wake up the parent so it can get exit status. */
@@ -528,7 +528,7 @@ loop:
continue;
nfound++;
- if (p->p_stat == SZOMB) {
+ if (pr->ps_flags & PS_ZOMBIE) {
retval[0] = p->p_pid;
if (statusp != NULL)
diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index 30e29bc0993..859a2bcdfdc 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_fork.c,v 1.167 2014/05/15 03:52:25 guenther Exp $ */
+/* $OpenBSD: kern_fork.c,v 1.168 2014/07/04 05:58:30 guenther Exp $ */
/* $NetBSD: kern_fork.c,v 1.29 1996/02/09 18:59:34 christos Exp $ */
/*
@@ -189,9 +189,8 @@ process_new(struct proc *p, struct process *parent, int flags)
timeout_set(&pr->ps_realit_to, realitexpire, pr);
pr->ps_flags = parent->ps_flags & (PS_SUGID | PS_SUGIDEXEC);
- if (parent->ps_session->s_ttyvp != NULL &&
- parent->ps_flags & PS_CONTROLT)
- atomic_setbits_int(&pr->ps_flags, PS_CONTROLT);
+ if (parent->ps_session->s_ttyvp != NULL)
+ pr->ps_flags |= parent->ps_flags & PS_CONTROLT;
p->p_p = pr;
@@ -214,12 +213,18 @@ process_new(struct proc *p, struct process *parent, int flags)
if (parent->ps_flags & PS_PROFIL)
startprofclock(pr);
- if ((flags & FORK_PTRACE) && (parent->ps_flags & PS_TRACED))
- atomic_setbits_int(&pr->ps_flags, PS_TRACED);
+ if (flags & FORK_PTRACE)
+ pr->ps_flags |= parent->ps_flags & PS_TRACED;
if (flags & FORK_NOZOMBIE)
- atomic_setbits_int(&pr->ps_flags, PS_NOZOMBIE);
+ pr->ps_flags |= PS_NOZOMBIE;
if (flags & FORK_SYSTEM)
- atomic_setbits_int(&pr->ps_flags, PS_SYSTEM);
+ pr->ps_flags |= PS_SYSTEM;
+
+ /*
+ * Mark as embryo to protect against others.
+ * Done with atomic_* to force visibility of all of the above flags
+ */
+ atomic_setbits_int(&pr->ps_flags, PS_EMBRYO);
/* it's sufficiently inited to be globally visible */
LIST_INSERT_HEAD(&allprocess, pr, ps_list);
@@ -463,11 +468,12 @@ fork1(struct proc *curp, int flags, void *stack, pid_t *tidptr,
}
/*
- * For new processes, set accounting bits
+ * For new processes, set accounting bits and mark as complete.
*/
if ((flags & FORK_THREAD) == 0) {
getnanotime(&pr->ps_start);
pr->ps_acflag = AFORK;
+ atomic_clearbits_int(&pr->ps_flags, PS_EMBRYO);
}
/*
diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c
index 757878a1b4e..df2d4e38ecf 100644
--- a/sys/kern/kern_proc.c
+++ b/sys/kern/kern_proc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_proc.c,v 1.58 2014/05/15 03:52:25 guenther Exp $ */
+/* $OpenBSD: kern_proc.c,v 1.59 2014/07/04 05:58:30 guenther Exp $ */
/* $NetBSD: kern_proc.c,v 1.14 1996/02/09 18:59:41 christos Exp $ */
/*
@@ -348,7 +348,7 @@ fixjobc(struct process *pr, struct pgrp *pgrp, int entering)
LIST_FOREACH(pr, &pr->ps_children, ps_sibling)
if ((hispgrp = pr->ps_pgrp) != pgrp &&
hispgrp->pg_session == mysession &&
- P_ZOMBIE(pr->ps_mainproc) == 0) {
+ (pr->ps_flags & PS_ZOMBIE) == 0) {
if (entering)
hispgrp->pg_jobc++;
else if (--hispgrp->pg_jobc == 0)
diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c
index d1bc53bf39c..f4da9b91b47 100644
--- a/sys/kern/kern_sig.c
+++ b/sys/kern/kern_sig.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_sig.c,v 1.167 2014/06/21 20:58:30 guenther Exp $ */
+/* $OpenBSD: kern_sig.c,v 1.168 2014/07/04 05:58:30 guenther Exp $ */
/* $NetBSD: kern_sig.c,v 1.54 1996/04/22 01:38:32 christos Exp $ */
/*
@@ -1058,7 +1058,7 @@ ptsignal(struct proc *p, int signum, enum signal_type type)
/* FALLTHROUGH */
default:
/*
- * SRUN, SIDL, SZOMB do nothing with the signal,
+ * SRUN, SIDL, SDEAD do nothing with the signal,
* other than kicking ourselves if we are running.
* It will either never be noticed, or noticed very soon.
*/
@@ -1963,7 +1963,6 @@ single_thread_set(struct proc *p, enum single_thread_mode mode, int deep)
pr->ps_singlecount++;
}
break;
- case SZOMB:
case SDEAD:
break;
case SONPROC:
diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index a6aa9a95d80..b7f84b91360 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_sysctl.c,v 1.252 2014/06/14 22:00:28 deraadt Exp $ */
+/* $OpenBSD: kern_sysctl.c,v 1.253 2014/07/04 05:58:31 guenther Exp $ */
/* $NetBSD: kern_sysctl.c,v 1.17 1996/05/20 17:49:05 mrg Exp $ */
/*-
@@ -123,8 +123,8 @@ int sysctl_emul(int *, u_int, void *, size_t *, void *, size_t);
int sysctl_cptime2(int *, u_int, void *, size_t *, void *, size_t);
void fill_file(struct kinfo_file *, struct file *, struct filedesc *,
- int, struct vnode *, struct proc *, struct proc *, int);
-void fill_kproc(struct proc *, struct kinfo_proc *, int, int);
+ int, struct vnode *, struct process *, struct proc *, int);
+void fill_kproc(struct process *, struct kinfo_proc *, struct proc *, int);
int (*cpu_cpuspeed)(int *);
void (*cpu_setperf)(int);
@@ -997,7 +997,7 @@ sysctl_rdstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
#ifndef SMALL_KERNEL
void
fill_file(struct kinfo_file *kf, struct file *fp, struct filedesc *fdp,
- int fd, struct vnode *vp, struct proc *pp, struct proc *p,
+ int fd, struct vnode *vp, struct process *pr, struct proc *p,
int show_pointers)
{
struct vattr va;
@@ -1168,12 +1168,13 @@ fill_file(struct kinfo_file *kf, struct file *fp, struct filedesc *fdp,
}
/* per-process information for KERN_FILE_BY[PU]ID */
- if (pp != NULL) {
- kf->p_pid = pp->p_p->ps_pid;
- kf->p_uid = pp->p_ucred->cr_uid;
- kf->p_gid = pp->p_ucred->cr_gid;
- kf->p_tid = pp->p_pid + THREAD_PID_OFFSET;
- strlcpy(kf->p_comm, pp->p_comm, sizeof(kf->p_comm));
+ if (pr != NULL) {
+ kf->p_pid = pr->ps_pid;
+ 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 (fdp != NULL)
kf->fd_ofileflags = fdp->fd_ofileflags[fd];
@@ -1189,7 +1190,6 @@ sysctl_file(int *name, u_int namelen, char *where, size_t *sizep,
struct kinfo_file *kf;
struct filedesc *fdp;
struct file *fp;
- struct proc *pp;
struct process *pr;
size_t buflen, elem_size, elem_count, outsize;
char *dp = where;
@@ -1216,9 +1216,9 @@ sysctl_file(int *name, u_int namelen, char *where, size_t *sizep,
kf = malloc(sizeof(*kf), M_TEMP, M_WAITOK);
-#define FILLIT(fp, fdp, i, vp, pp) do { \
+#define FILLIT(fp, fdp, i, vp, pr) do { \
if (buflen >= elem_size && elem_count > 0) { \
- fill_file(kf, fp, fdp, i, vp, pp, p, show_pointers); \
+ fill_file(kf, fp, fdp, i, vp, pr, p, show_pointers); \
error = copyout(kf, dp, outsize); \
if (error) \
break; \
@@ -1253,9 +1253,7 @@ sysctl_file(int *name, u_int namelen, char *where, size_t *sizep,
* skip system, exiting, embryonic and undead
* processes
*/
- pp = pr->ps_mainproc;
- if ((pr->ps_flags & (PS_SYSTEM | PS_EXITING))
- || pp->p_stat == SIDL || pp->p_stat == SZOMB)
+ if (pr->ps_flags & (PS_SYSTEM | PS_EMBRYO | PS_EXITING))
continue;
if (arg > 0 && pr->ps_pid != (pid_t)arg) {
/* not the pid we are looking for */
@@ -1263,31 +1261,29 @@ sysctl_file(int *name, u_int namelen, char *where, size_t *sizep,
}
fdp = pr->ps_fd;
if (pr->ps_textvp)
- FILLIT(NULL, NULL, KERN_FILE_TEXT, pr->ps_textvp, pp);
+ FILLIT(NULL, NULL, KERN_FILE_TEXT, pr->ps_textvp, pr);
if (fdp->fd_cdir)
- FILLIT(NULL, NULL, KERN_FILE_CDIR, fdp->fd_cdir, pp);
+ FILLIT(NULL, NULL, KERN_FILE_CDIR, fdp->fd_cdir, pr);
if (fdp->fd_rdir)
- FILLIT(NULL, NULL, KERN_FILE_RDIR, fdp->fd_rdir, pp);
+ FILLIT(NULL, NULL, KERN_FILE_RDIR, fdp->fd_rdir, pr);
if (pr->ps_tracevp)
- FILLIT(NULL, NULL, KERN_FILE_TRACE, pr->ps_tracevp, pp);
+ FILLIT(NULL, NULL, KERN_FILE_TRACE, pr->ps_tracevp, pr);
for (i = 0; i < fdp->fd_nfiles; i++) {
if ((fp = fdp->fd_ofiles[i]) == NULL)
continue;
if (!FILE_IS_USABLE(fp))
continue;
- FILLIT(fp, fdp, i, NULL, pp);
+ FILLIT(fp, fdp, i, NULL, pr);
}
}
break;
case KERN_FILE_BYUID:
LIST_FOREACH(pr, &allprocess, ps_list) {
- pp = pr->ps_mainproc;
/*
* skip system, exiting, embryonic and undead
* processes
*/
- if ((pr->ps_flags & (PS_SYSTEM | PS_EXITING))
- || pp->p_stat == SIDL || pp->p_stat == SZOMB)
+ if (pr->ps_flags & (PS_SYSTEM | PS_EMBRYO | PS_EXITING))
continue;
if (arg >= 0 && pr->ps_ucred->cr_uid != (uid_t)arg) {
/* not the uid we are looking for */
@@ -1295,17 +1291,17 @@ sysctl_file(int *name, u_int namelen, char *where, size_t *sizep,
}
fdp = pr->ps_fd;
if (fdp->fd_cdir)
- FILLIT(NULL, NULL, KERN_FILE_CDIR, fdp->fd_cdir, pp);
+ FILLIT(NULL, NULL, KERN_FILE_CDIR, fdp->fd_cdir, pr);
if (fdp->fd_rdir)
- FILLIT(NULL, NULL, KERN_FILE_RDIR, fdp->fd_rdir, pp);
+ FILLIT(NULL, NULL, KERN_FILE_RDIR, fdp->fd_rdir, pr);
if (pr->ps_tracevp)
- FILLIT(NULL, NULL, KERN_FILE_TRACE, pr->ps_tracevp, pp);
+ FILLIT(NULL, NULL, KERN_FILE_TRACE, pr->ps_tracevp, pr);
for (i = 0; i < fdp->fd_nfiles; i++) {
if ((fp = fdp->fd_ofiles[i]) == NULL)
continue;
if (!FILE_IS_USABLE(fp))
continue;
- FILLIT(fp, fdp, i, NULL, pp);
+ FILLIT(fp, fdp, i, NULL, pr);
}
}
break;
@@ -1335,7 +1331,7 @@ int
sysctl_doproc(int *name, u_int namelen, char *where, size_t *sizep)
{
struct kinfo_proc *kproc = NULL;
- struct proc *p, *pp;
+ struct proc *p;
struct process *pr;
char *dp;
int arg, buflen, doingzomb, elem_size, elem_count;
@@ -1371,12 +1367,10 @@ again:
if (pr->ps_pgrp == NULL)
continue;
- p = pr->ps_mainproc;
-
/*
* Skip embryonic processes.
*/
- if (p->p_stat == SIDL)
+ if (pr->ps_flags & PS_EMBRYO)
continue;
/*
@@ -1434,16 +1428,7 @@ again:
}
if (buflen >= elem_size && elem_count > 0) {
- fill_kproc(p, kproc, 0, show_pointers);
- /* Update %cpu for all threads */
- if (!dothreads) {
- TAILQ_FOREACH(pp, &pr->ps_threads,
- p_thr_link) {
- if (pp == p)
- continue;
- kproc->p_pctcpu += pp->p_pctcpu;
- }
- }
+ fill_kproc(pr, kproc, NULL, show_pointers);
error = copyout(kproc, dp, elem_size);
if (error)
goto err;
@@ -1459,7 +1444,7 @@ again:
TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) {
if (buflen >= elem_size && elem_count > 0) {
- fill_kproc(p, kproc, 1, show_pointers);
+ fill_kproc(pr, kproc, p, show_pointers);
error = copyout(kproc, dp, elem_size);
if (error)
goto err;
@@ -1495,13 +1480,17 @@ err:
* Fill in a kproc structure for the specified process.
*/
void
-fill_kproc(struct proc *p, struct kinfo_proc *ki, int isthread,
+fill_kproc(struct process *pr, struct kinfo_proc *ki, struct proc *p,
int show_pointers)
{
- struct process *pr = p->p_p;
struct session *s = pr->ps_session;
struct tty *tp;
struct timespec ut, st;
+ int isthread;
+
+ isthread = p != NULL;
+ if (!isthread)
+ p = pr->ps_mainproc; /* XXX */
FILL_KPROC(ki, strlcpy, p, pr, pr->ps_ucred, pr->ps_pgrp,
p, pr, s, pr->ps_vmspace, pr->ps_limit, pr->ps_sigacts, isthread,
@@ -1525,21 +1514,41 @@ fill_kproc(struct proc *p, struct kinfo_proc *ki, int isthread,
}
/* fixups that can only be done in the kernel */
- if (!P_ZOMBIE(p)) {
- if (p->p_stat != SIDL)
+ if ((pr->ps_flags & PS_ZOMBIE) == 0) {
+ if ((pr->ps_flags & PS_EMBRYO) == 0)
ki->p_vm_rssize = vm_resident_count(pr->ps_vmspace);
-
- calctsru(&p->p_tu, &ut, &st, NULL);
+ calctsru(isthread ? &p->p_tu : &pr->ps_tu, &ut, &st, NULL);
ki->p_uutime_sec = ut.tv_sec;
ki->p_uutime_usec = ut.tv_nsec/1000;
ki->p_ustime_sec = st.tv_sec;
ki->p_ustime_usec = st.tv_nsec/1000;
#ifdef MULTIPROCESSOR
- if (p->p_cpu != NULL)
+ if (isthread && p->p_cpu != NULL)
ki->p_cpuid = CPU_INFO_UNIT(p->p_cpu);
#endif
}
+
+ /* get %cpu and schedule state: just one thread or sum of all? */
+ if (isthread) {
+ ki->p_pctcpu = p->p_pctcpu;
+ ki->p_stat = p->p_stat;
+ } else {
+ ki->p_pctcpu = 0;
+ ki->p_stat = (pr->ps_flags & PS_ZOMBIE) ? SDEAD : SIDL;
+ TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) {
+ ki->p_pctcpu += p->p_pctcpu;
+ /* find best state: ONPROC > RUN > STOP > SLEEP > .. */
+ if (p->p_stat == SONPROC || ki->p_stat == SONPROC)
+ ki->p_stat = SONPROC;
+ else if (p->p_stat == SRUN || ki->p_stat == SRUN)
+ ki->p_stat = SRUN;
+ else if (p->p_stat == SSTOP || ki->p_stat == SSTOP)
+ ki->p_stat = SSTOP;
+ else if (p->p_stat == SSLEEP)
+ ki->p_stat = SSLEEP;
+ }
+ }
}
int
diff --git a/sys/kern/sched_bsd.c b/sys/kern/sched_bsd.c
index 1a73afc54d3..98bb478b8bc 100644
--- a/sys/kern/sched_bsd.c
+++ b/sys/kern/sched_bsd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sched_bsd.c,v 1.34 2014/05/15 03:52:25 guenther Exp $ */
+/* $OpenBSD: sched_bsd.c,v 1.35 2014/07/04 05:58:31 guenther Exp $ */
/* $NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $ */
/*-
@@ -493,7 +493,6 @@ setrunnable(struct proc *p)
case 0:
case SRUN:
case SONPROC:
- case SZOMB:
case SDEAD:
case SIDL:
default:
diff --git a/sys/kern/tty.c b/sys/kern/tty.c
index ba7204889af..4b35b831c3c 100644
--- a/sys/kern/tty.c
+++ b/sys/kern/tty.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tty.c,v 1.108 2014/06/06 22:44:18 matthew Exp $ */
+/* $OpenBSD: tty.c,v 1.109 2014/07/04 05:58:31 guenther Exp $ */
/* $NetBSD: tty.c,v 1.68.4.2 1996/06/06 16:04:52 thorpej Exp $ */
/*-
@@ -2183,7 +2183,7 @@ ttyinfo(struct tty *tp)
goto update_pickpr;
/* if p has less cpu or is zombie, then it's worse */
- if (pctcpu2 < pctcpu || P_ZOMBIE(pr->ps_mainproc))
+ if (pctcpu2 < pctcpu || (pr->ps_flags & PS_ZOMBIE))
continue;
update_pickpr:
pickpr = pr;
@@ -2193,8 +2193,7 @@ update_pickpr:
/* Calculate percentage cpu, resident set size. */
calc_pctcpu = (pctcpu * 10000 + FSCALE / 2) >> FSHIFT;
- rss = (!P_ZOMBIE(pickpr->ps_mainproc) &&
- pickpr->ps_mainproc->p_stat != SIDL) ? 0 :
+ rss = (pickpr->ps_flags & (PS_EMBRYO | PS_ZOMBIE)) ? 0 :
vm_resident_count(pickpr->ps_vmspace);
calctsru(&pickpr->ps_tu, &utime, &stime, NULL);
diff --git a/sys/miscfs/procfs/procfs_cmdline.c b/sys/miscfs/procfs/procfs_cmdline.c
index b80aff97186..756804ec954 100644
--- a/sys/miscfs/procfs/procfs_cmdline.c
+++ b/sys/miscfs/procfs/procfs_cmdline.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: procfs_cmdline.c,v 1.10 2012/03/10 05:54:28 guenther Exp $ */
+/* $OpenBSD: procfs_cmdline.c,v 1.11 2014/07/04 05:58:31 guenther Exp $ */
/* $NetBSD: procfs_cmdline.c,v 1.3 1999/03/13 22:26:48 thorpej Exp $ */
/*
@@ -55,6 +55,7 @@
int
procfs_docmdline(struct proc *curp, struct proc *p, struct pfsnode *pfs, struct uio *uio)
{
+ struct process *pr = p->p_p;
struct ps_strings pss;
int count, error, i;
size_t len, xlen, upper_bound;
@@ -78,7 +79,7 @@ procfs_docmdline(struct proc *curp, struct proc *p, struct pfsnode *pfs, struct
* System processes also don't have a user stack. This is what
* ps(1) would display.
*/
- if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0) {
+ if (pr->ps_flags & (PS_ZOMBIE | PS_SYSTEM)) {
len = snprintf(arg, PAGE_SIZE, "(%s)", p->p_comm);
if (uio->uio_offset >= (off_t)len)
error = 0;
@@ -99,11 +100,11 @@ procfs_docmdline(struct proc *curp, struct proc *p, struct pfsnode *pfs, struct
* Lock the process down in memory.
*/
/* XXXCDC: how should locking work here? */
- if ((p->p_p->ps_flags & PS_EXITING) || (p->p_vmspace->vm_refcnt < 1)) {
+ if ((pr->ps_flags & PS_EXITING) || (pr->ps_vmspace->vm_refcnt < 1)) {
free(arg, M_TEMP);
return (EFAULT);
}
- vm = p->p_vmspace;
+ vm = pr->ps_vmspace;
vm->vm_refcnt++; /* XXX */
/*
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
index dffe7120c11..d4d5a1ed7d1 100644
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: proc.h,v 1.186 2014/05/15 03:52:25 guenther Exp $ */
+/* $OpenBSD: proc.h,v 1.187 2014/07/04 05:58:30 guenther Exp $ */
/* $NetBSD: proc.h,v 1.44 1996/04/22 01:23:21 christos Exp $ */
/*-
@@ -245,12 +245,14 @@ struct process {
#define PS_NOZOMBIE 0x00004000 /* No signal or zombie at exit. */
#define PS_STOPPED 0x00008000 /* Just stopped, need sig to parent. */
#define PS_SYSTEM 0x00010000 /* No sigs, stats or swapping. */
+#define PS_EMBRYO 0x00020000 /* New process, not yet fledged */
+#define PS_ZOMBIE 0x00040000 /* Dead and ready to be waited for */
#define PS_BITS \
("\20\01CONTROLT\02EXEC\03INEXEC\04EXITING\05SUGID" \
"\06SUGIDEXEC\07PPWAIT\010ISPWAIT\011PROFIL\012TRACED" \
"\013WAITED\014COREDUMP\015SINGLEEXIT\016SINGLEUNWIND" \
- "\017NOZOMBIE\020STOPPED\021SYSTEM")
+ "\017NOZOMBIE\020STOPPED\021SYSTEM\022EMBRYO\023ZOMBIE")
struct proc {
@@ -343,15 +345,15 @@ struct proc {
};
/* Status values. */
-#define SIDL 1 /* Process being created by fork. */
+#define SIDL 1 /* Thread being created by fork. */
#define SRUN 2 /* Currently runnable. */
#define SSLEEP 3 /* Sleeping on an address. */
-#define SSTOP 4 /* Process debugging or suspension. */
-#define SZOMB 5 /* Awaiting collection by parent. */
-#define SDEAD 6 /* Process is almost a zombie. */
-#define SONPROC 7 /* Process is currently on a CPU. */
+#define SSTOP 4 /* Debugging or suspension. */
+#define SZOMB 5 /* unused */
+#define SDEAD 6 /* Thread is almost gone */
+#define SONPROC 7 /* Thread is currently on a CPU. */
-#define P_ZOMBIE(p) ((p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
+#define P_ZOMBIE(p) ((p)->p_stat == SDEAD)
/*
* These flags are per-thread and kept in p_flag
diff --git a/sys/sys/sysctl.h b/sys/sys/sysctl.h
index 97d355ea034..afc8268044f 100644
--- a/sys/sys/sysctl.h
+++ b/sys/sys/sysctl.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: sysctl.h,v 1.145 2014/05/15 03:52:25 guenther Exp $ */
+/* $OpenBSD: sysctl.h,v 1.146 2014/07/04 05:58:30 guenther Exp $ */
/* $NetBSD: sysctl.h,v 1.16 1996/04/09 20:55:36 cgd Exp $ */
/*
@@ -513,7 +513,6 @@ do { \
(kp)->p_iticks = (pr)->ps_tu.tu_iticks; \
} \
(kp)->p_cpticks = (p)->p_cpticks; \
- (kp)->p_pctcpu = (p)->p_pctcpu; \
\
if (show_addresses) \
(kp)->p_tracep = PTRTOINT64((pr)->ps_tracevp); \
@@ -542,7 +541,7 @@ do { \
if ((sess)->s_leader == (praddr)) \
(kp)->p_eflag |= EPROC_SLEADER; \
\
- if ((p)->p_stat != SIDL && !P_ZOMBIE(p)) { \
+ if (((pr)->ps_flags & (PS_EMBRYO | PS_ZOMBIE)) == 0) { \
if ((vm) != NULL) { \
(kp)->p_vm_rssize = (vm)->vm_rssize; \
(kp)->p_vm_tsize = (vm)->vm_tsize; \
@@ -566,7 +565,7 @@ do { \
(kp)->p_rlim_rss_cur = \
(lim)->pl_rlimit[RLIMIT_RSS].rlim_cur; \
\
- if (!P_ZOMBIE(p)) { \
+ if (((pr)->ps_flags & PS_ZOMBIE) == 0) { \
struct timeval tv; \
\
(kp)->p_uvalid = 1; \
diff --git a/usr.bin/tmux/procname.c b/usr.bin/tmux/procname.c
index 4fb96038fe2..aeaf0fa49a3 100644
--- a/usr.bin/tmux/procname.c
+++ b/usr.bin/tmux/procname.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: procname.c,v 1.12 2014/04/16 23:05:38 nicm Exp $ */
+/* $OpenBSD: procname.c,v 1.13 2014/07/04 05:58:31 guenther Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -33,7 +33,7 @@
#define is_runnable(p) \
((p)->p_stat == SRUN || (p)->p_stat == SIDL || (p)->p_stat == SONPROC)
#define is_stopped(p) \
- ((p)->p_stat == SSTOP || (p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
+ ((p)->p_stat == SSTOP || (p)->p_stat == SDEAD)
struct kinfo_proc *cmp_procs(struct kinfo_proc *, struct kinfo_proc *);
char *get_proc_name(int, char *);
diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c
index ac4baf74755..2d9bccba1e1 100644
--- a/usr.bin/top/machine.c
+++ b/usr.bin/top/machine.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: machine.c,v 1.77 2014/04/08 14:04:11 mpi Exp $ */
+/* $OpenBSD: machine.c,v 1.78 2014/07/04 05:58:31 guenther Exp $ */
/*-
* Copyright (c) 1994 Thorsten Lockert <tholo@sigmasoft.com>
@@ -382,7 +382,7 @@ get_process_info(struct system_info *si, struct process_select *sel,
(show_threads || (pp->p_flag & P_THREAD) == 0)) {
total_procs++;
process_states[(unsigned char) pp->p_stat]++;
- if (pp->p_stat != SZOMB &&
+ if ((pp->p_psflags & PS_ZOMBIE) == 0 &&
(show_idle || pp->p_pctcpu != 0 ||
pp->p_stat == SRUN) &&
(!hide_uid || pp->p_ruid != sel->huid) &&
diff --git a/usr.bin/w/proc_compare.c b/usr.bin/w/proc_compare.c
index 4952157fad9..a2b280f9551 100644
--- a/usr.bin/w/proc_compare.c
+++ b/usr.bin/w/proc_compare.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: proc_compare.c,v 1.13 2012/12/18 21:28:45 millert Exp $ */
+/* $OpenBSD: proc_compare.c,v 1.14 2014/07/04 05:58:31 guenther Exp $ */
/*-
* Copyright (c) 1990, 1993
@@ -88,7 +88,7 @@ proc_compare(const struct kinfo_proc *p1, const struct kinfo_proc *p2)
/*
* weed out zombies
*/
- switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) {
+ switch (TESTAB(p1->p_stat == SDEAD, p2->p_stat == SDEAD)) {
case ONLYA:
return (1);
case ONLYB:
diff --git a/usr.bin/w/w.c b/usr.bin/w/w.c
index a2955c656d9..4c146b81e87 100644
--- a/usr.bin/w/w.c
+++ b/usr.bin/w/w.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: w.c,v 1.54 2013/11/20 21:00:33 deraadt Exp $ */
+/* $OpenBSD: w.c,v 1.55 2014/07/04 05:58:31 guenther Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993, 1994
@@ -221,7 +221,7 @@ main(int argc, char *argv[])
if (kp == NULL)
errx(1, "%s", kvm_geterr(kd));
for (i = 0; i < nentries; i++, kp++) {
- if (kp->p_stat == SIDL || kp->p_stat == SZOMB)
+ if (kp->p_psflags & (PS_EMBRYO | PS_ZOMBIE))
continue;
for (ep = ehead; ep != NULL; ep = ep->next) {
/* ftp is a special case. */
diff --git a/usr.sbin/snmpd/mib.c b/usr.sbin/snmpd/mib.c
index 96c9681b575..2012f1c737a 100644
--- a/usr.sbin/snmpd/mib.c
+++ b/usr.sbin/snmpd/mib.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: mib.c,v 1.70 2014/06/23 03:46:17 guenther Exp $ */
+/* $OpenBSD: mib.c,v 1.71 2014/07/04 05:58:31 guenther Exp $ */
/*
* Copyright (c) 2012 Joel Knight <joel@openbsd.org>
@@ -843,7 +843,6 @@ mib_hrswrun(struct oid *oid, struct ber_oid *o, struct ber_element **elm)
/* notRunnable(3) */
ber = ber_add_integer(ber, 3);
break;
- case SZOMB:
case SDEAD:
default:
/* invalid(4) */