From 5dfd06ac148512faf075c4e399e8485fd955578f Mon Sep 17 00:00:00 2001 From: Simon Schubert Date: Thu, 1 Feb 2007 10:33:26 +0000 Subject: [PATCH] Change kinfo_proc interface between kernel and userland. Before, we were embedding a struct proc (among others) into struct kinfo_proc. Every time we change implementation details in the kernel, userland has to be adapted (recompiled). In preparation for the coming LWP changes this interface has been reworked. Now kinfo_proc is a structure which does not depend on other structures on the kernel which are subject to change. Instead, the routines fill_kinfo_proc and fill_kinfo_lwp copy all values which are of interest between the kernel structure and the stable kinfo_proc structure. Furthermore, this change adds infrastructure to export LWP-specific data. If userland requests LWP data, it sets the flag KERN_PROC_FLAG_LWP in the sysctl oid. This leads to multiple kinfo_procs being exported. If not set, the first LWP will used. This is like FreeBSD do it, and it seems easy and simple. Note that userland was not yet adjusted to actually request LWPs and aggregate this information if necessary. Besides, the kernel does not yet have more than one LWP per process anyways. This introduces a new file, kern/kern_kinfo.c, which is shared between kernel and libkvm. This was done to avoid and remove code duplication. Now kvm_getprocs constructs a complete struct proc, including pointers, and then calls fill_kinfo_proc to do its job. In-collaboration-with: Thomas E. Spanjaard --- bin/ps/extern.h | 6 +- bin/ps/keyword.c | 73 +++++----- bin/ps/print.c | 155 +++++++++----------- bin/ps/ps.c | 44 ++---- bin/ps/ps.h | 17 +-- contrib/ipfilter/ipsend/sock.c | 15 +- lib/libc/gen/sysctl.3 | 7 +- lib/libkcore/kcore_file.c | 23 +-- lib/libkvm/Makefile | 10 +- lib/libkvm/kvm.c | 9 +- lib/libkvm/kvm.h | 4 +- lib/libkvm/kvm_getprocs.3 | 8 +- lib/libkvm/kvm_private.h | 3 +- lib/libkvm/kvm_proc.c | 249 +++++++++++++++++++-------------- sys/conf/files | 3 +- sys/kern/imgact_aout.c | 33 +---- sys/kern/kern_exit.c | 4 +- sys/kern/kern_kinfo.c | 186 ++++++++++++++++++++++++ sys/kern/kern_proc.c | 128 +++++++++-------- sys/sys/jail.h | 12 +- sys/sys/kinfo.h | 116 ++++++++++++++- sys/sys/proc.h | 22 ++- sys/sys/sysctl.h | 5 +- sys/sys/user.h | 50 +------ usr.bin/fstat/fstat.c | 31 ++-- usr.bin/gcore/gcore.c | 43 +++--- usr.bin/killall/killall.c | 14 +- usr.bin/pkill/pkill.c | 48 +++---- usr.bin/sockstat/sockstat.c | 4 +- usr.bin/systat/pigs.c | 31 ++-- usr.bin/top/machine.c | 103 +++++++------- usr.bin/w/extern.h | 6 +- usr.bin/w/proc_compare.c | 26 ++-- usr.bin/w/w.c | 22 ++- 34 files changed, 867 insertions(+), 643 deletions(-) create mode 100644 sys/kern/kern_kinfo.c diff --git a/bin/ps/extern.h b/bin/ps/extern.h index 9de2294320..881c4f0232 100644 --- a/bin/ps/extern.h +++ b/bin/ps/extern.h @@ -32,7 +32,7 @@ * * @(#)extern.h 8.3 (Berkeley) 4/2/94 * $FreeBSD: src/bin/ps/extern.h,v 1.9 1999/08/27 23:14:50 peter Exp $ - * $DragonFly: src/bin/ps/extern.h,v 1.12 2005/06/29 01:25:10 dillon Exp $ + * $DragonFly: src/bin/ps/extern.h,v 1.13 2007/02/01 10:33:25 corecode Exp $ */ struct kinfo; @@ -71,7 +71,8 @@ void tdpri(const KINFO *, const struct varent *); void rtprior(const KINFO *, const struct varent *); void printheader(void); void pvar(const KINFO *, const struct varent *); -void pest(const KINFO *, const struct varent *); +void lpvar(const KINFO *, const struct varent *); +void lpest(const KINFO *, const struct varent *); void tvar(const KINFO *, const struct varent *); void rssize(const KINFO *, const struct varent *); void runame(const KINFO *, const struct varent *); @@ -86,7 +87,6 @@ void tsize(const KINFO *, const struct varent *); void ucomm(const KINFO *, const struct varent *); void uname(const KINFO *, const struct varent *); int s_uname(const KINFO *); -void uvar(const KINFO *, const struct varent *); void vsize(const KINFO *, const struct varent *); void wchan(const KINFO *, const struct varent *); __END_DECLS diff --git a/bin/ps/keyword.c b/bin/ps/keyword.c index a2eda1e987..8d4c8c7a65 100644 --- a/bin/ps/keyword.c +++ b/bin/ps/keyword.c @@ -32,7 +32,7 @@ * * @(#)keyword.c 8.5 (Berkeley) 4/2/94 * $FreeBSD: src/bin/ps/keyword.c,v 1.24.2.3 2002/10/10 20:05:32 jmallett Exp $ - * $DragonFly: src/bin/ps/keyword.c,v 1.24 2006/05/18 08:17:14 dillon Exp $ + * $DragonFly: src/bin/ps/keyword.c,v 1.25 2007/02/01 10:33:25 corecode Exp $ */ #include @@ -68,10 +68,8 @@ int utime(), stime(), ixrss(), idrss(), isrss(); #endif /* Compute offset in common structures. */ -#define POFF(x) offsetof(struct proc, x) -#define TOFF(x) offsetof(struct thread, x) -#define EOFF(x) offsetof(struct eproc, x) -#define UOFF(x) offsetof(struct usave, x) +#define POFF(x) offsetof(struct kinfo_proc, kp_##x) +#define LPOFF(x) offsetof(struct kinfo_lwp, kl_##x) #define ROFF(x) offsetof(struct rusage, x) #define UIDFMT "u" @@ -83,17 +81,17 @@ int utime(), stime(), ixrss(), idrss(), isrss(); static const VAR var[] = { {"%cpu", "%CPU", NULL, 0, pcpu, NULL, 4, 0, 0, NULL, NULL}, {"%mem", "%MEM", NULL, 0, pmem, NULL, 4, 0, 0, NULL, NULL}, - {"acflag", "ACFLG", NULL, 0, pvar, NULL, 3, POFF(p_acflag), USHORT, "x", + {"acflag", "ACFLG", NULL, 0, pvar, NULL, 3, POFF(acflag), USHORT, "x", NULL}, {"acflg", "", "acflag", 0, NULL, NULL, 0, 0, 0, NULL, NULL}, - {"batch", "BAT", NULL, 0, pest, NULL, 3, POFF(p_usdata.bsd4.origcpu), UINT, "d", NULL}, + {"batch", "BAT", NULL, 0, lpest, NULL, 3, LPOFF(origcpu), UINT, "d", NULL}, {"blocked", "", "sigmask", 0, NULL, NULL, 0, 0, 0, NULL, NULL}, {"caught", "", "sigcatch", 0, NULL, NULL, 0, 0, 0, NULL, NULL}, {"command", "COMMAND", NULL, COMM|LJUST|USER, command, NULL, 16, 0, 0, NULL, NULL}, - {"cpu", "CPU", NULL, 0, pest, NULL, 3, POFF(p_usdata.bsd4.estcpu), UINT, "d", NULL}, + {"cpu", "CPU", NULL, 0, lpest, NULL, 3, LPOFF(estcpu), UINT, "d", NULL}, {"cputime", "", "time", 0, NULL, NULL, 0, 0, 0, NULL, NULL}, - {"f", "F", NULL, 0, pvar, NULL, 7, POFF(p_flag), INT, "x", NULL}, + {"f", "F", NULL, 0, pvar, NULL, 7, POFF(flags), INT, "x", NULL}, {"flags", "", "f", 0, NULL, NULL, 0, 0, 0, NULL, NULL}, #if 0 {"iac", "IAC", NULL, 0, pvar, NULL, 4, POFF(p_usdata.bsd4.interactive), CHAR, PIDFMT, @@ -103,13 +101,15 @@ static const VAR var[] = { {"inblk", "INBLK", NULL, USER, rvar, NULL, 4, ROFF(ru_inblock), LONG, "ld", NULL}, {"inblock", "", "inblk", 0, NULL, NULL, 0, 0, 0, NULL, NULL}, - {"jail", "JAIL", NULL, 0, evar, NULL, 3, EOFF(e_jailid), INT, "d", NULL}, - {"jobc", "JOBC", NULL, 0, evar, NULL, 4, EOFF(e_jobc), SHORT, "d", NULL}, - {"ktrace", "KTRACE", NULL, 0, pvar, NULL, 8, POFF(p_traceflag), INT, "x", + {"jail", "JAIL", NULL, 0, pvar, NULL, 3, POFF(jailid), INT, "d", NULL}, + {"jobc", "JOBC", NULL, 0, pvar, NULL, 4, POFF(jobc), SHORT, "d", NULL}, + {"ktrace", "KTRACE", NULL, 0, pvar, NULL, 8, POFF(traceflag), INT, "x", NULL}, +#if 0 {"ktracep", "KTRACEP", NULL, 0, pvar, NULL, 8, POFF(p_tracenode), LONG, "lx", NULL}, - {"lastcpu", "C", NULL, 0, evar, NULL, 3, EOFF(e_cpuid), UINT, "d", NULL}, +#endif + {"lastcpu", "C", NULL, 0, lpvar, NULL, 3, LPOFF(cpuid), UINT, "d", NULL}, {"lim", "LIM", NULL, 0, maxrss, NULL, 5, 0, 0, NULL, NULL}, {"login", "LOGIN", NULL, LJUST, logname, NULL, MAXLOGNAME-1, 0, 0, NULL, NULL}, @@ -134,68 +134,71 @@ static const VAR var[] = { NULL}, {"nvcsw", "NVCSW", NULL, USER, rvar, NULL, 5, ROFF(ru_nvcsw), LONG, "ld", NULL}, - {"nwchan", "WCHAN", NULL, 0, tvar, NULL, 8, TOFF(td_wchan), KPTR, "lx", NULL}, + {"nwchan", "WCHAN", NULL, 0, lpvar, NULL, 8, LPOFF(wchan), KPTR, "lx", NULL}, {"oublk", "OUBLK", NULL, USER, rvar, NULL, 4, ROFF(ru_oublock), LONG, "ld", NULL}, {"oublock", "", "oublk", 0, NULL, NULL, 0, 0, 0, NULL, NULL}, - {"p_ru", "P_RU", NULL, 0, pvar, NULL, 6, POFF(p_ru), KPTR, "lx", NULL}, - {"paddr", "PADDR", NULL, 0, evar, NULL, 8, EOFF(e_paddr), KPTR, "lx", NULL}, + {"p_ru", "P_RU", NULL, 0, pvar, NULL, 6, POFF(ru), KPTR, "lx", NULL}, + {"paddr", "PADDR", NULL, 0, pvar, NULL, 8, POFF(paddr), KPTR, "lx", NULL}, {"pagein", "PAGEIN", NULL, USER, pagein, NULL, 6, 0, 0, NULL, NULL}, {"pcpu", "", "%cpu", 0, NULL, NULL, 0, 0, 0, NULL, NULL}, {"pending", "", "sig", 0, NULL, NULL, 0, 0, 0, NULL, NULL}, - {"pgid", "PGID", NULL, 0, evar, NULL, PIDLEN, EOFF(e_pgid), UINT, PIDFMT, + {"pgid", "PGID", NULL, 0, pvar, NULL, PIDLEN, POFF(pgid), UINT, PIDFMT, NULL}, - {"pid", "PID", NULL, 0, pvar, NULL, PIDLEN, POFF(p_pid), UINT, PIDFMT, + {"pid", "PID", NULL, 0, pvar, NULL, PIDLEN, POFF(pid), UINT, PIDFMT, NULL}, {"pmem", "", "%mem", 0, NULL, NULL, 0, 0, 0, NULL, NULL}, - {"ppid", "PPID", NULL, 0, evar, NULL, PIDLEN, EOFF(e_ppid), UINT, PIDFMT, + {"ppid", "PPID", NULL, 0, pvar, NULL, PIDLEN, POFF(ppid), UINT, PIDFMT, NULL}, {"pri", "PRI", NULL, 0, pri, NULL, 3, 0, 0, NULL, NULL}, - {"re", "RE", NULL, 0, pvar, NULL, 3, POFF(p_swtime), UINT, "d", NULL}, - {"rgid", "RGID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_rgid), + {"re", "RE", NULL, 0, lpvar, NULL, 3, LPOFF(swtime), UINT, "d", NULL}, + {"rgid", "RGID", NULL, 0, pvar, NULL, UIDLEN, POFF(rgid), UINT, UIDFMT, NULL}, +#if 0 {"rlink", "RLINK", NULL, 0, pvar, NULL, 8, POFF(p_lwp.lwp_procq.tqe_prev), KPTR, "lx", NULL}, +#endif {"rss", "RSS", NULL, 0, p_rssize, NULL, 4, 0, 0, NULL, NULL}, {"rssize", "", "rsz", 0, NULL, NULL, 0, 0, 0, NULL, NULL}, {"rsz", "RSZ", NULL, 0, rssize, NULL, 4, 0, 0, NULL, NULL}, - {"rtprio", "RTPRIO", NULL, 0, rtprior, NULL, 7, POFF(p_rtprio), 0, NULL, NULL}, - {"ruid", "RUID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_ruid), + {"rtprio", "RTPRIO", NULL, 0, rtprior, NULL, 7, NULL, 0, NULL, NULL}, + {"ruid", "RUID", NULL, 0, pvar, NULL, UIDLEN, POFF(ruid), UINT, UIDFMT, NULL}, {"ruser", "RUSER", NULL, LJUST|DSIZ, runame, s_runame, USERLEN, 0, 0, NULL, NULL}, - {"sess", "SESS", NULL, 0, evar, NULL, 6, EOFF(e_sess), KPTR, "lx", NULL}, - {"sig", "PENDING", NULL, 0, pvar, NULL, 8, POFF(p_siglist), INT, "x", NULL}, - {"sigcatch", "CAUGHT", NULL, 0, evar, NULL, 8, EOFF(e_procsig.ps_sigcatch), + {"sess", "SESS", NULL, 0, pvar, NULL, 6, POFF(sid), UINT, PIDFMT, NULL}, + {"sig", "PENDING", NULL, 0, lpvar, NULL, 8, LPOFF(siglist), INT, "x", NULL}, + {"sigcatch", "CAUGHT", NULL, 0, pvar, NULL, 8, POFF(sigcatch), UINT, "x", NULL}, - {"sigignore", "IGNORED", NULL, 0, evar, NULL, 8, EOFF(e_procsig.ps_sigignore), + {"sigignore", "IGNORED", NULL, 0, pvar, NULL, 8, POFF(sigignore), UINT, "x", NULL}, - {"sigmask", "BLOCKED", NULL, 0, pvar, NULL, 8, POFF(p_sigmask), UINT, "x", + {"sigmask", "BLOCKED", NULL, 0, lpvar, NULL, 8, LPOFF(sigmask), UINT, "x", NULL}, - {"sl", "SL", NULL, 0, pvar, NULL, 3, POFF(p_slptime), UINT, "d", NULL}, + {"sigproc", "PENDING", NULL, 0, pvar, NULL, 8, POFF(siglist), INT, "x", NULL}, + {"sl", "SL", NULL, 0, lpvar, NULL, 3, LPOFF(slptime), UINT, "d", NULL}, {"start", "STARTED", NULL, LJUST|USER, started, NULL, 7, 0, 0, NULL, NULL}, {"stat", "", "state", 0, NULL, NULL, 0, 0, 0, NULL, NULL}, {"state", "STAT", NULL, 0, state, NULL, 4, 0, 0, NULL, NULL}, - {"svgid", "SVGID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_svgid), UINT, + {"svgid", "SVGID", NULL, 0, pvar, NULL, UIDLEN, POFF(svgid), UINT, UIDFMT, NULL}, - {"svuid", "SVUID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_svuid), UINT, + {"svuid", "SVUID", NULL, 0, pvar, NULL, UIDLEN, POFF(svuid), UINT, UIDFMT, NULL}, {"tdev", "TDEV", NULL, 0, tdev, NULL, 4, 0, 0, NULL, NULL}, {"tdpri", "TDPRI", NULL, 0, tdpri, NULL, 5, 0, 0, NULL, NULL}, {"time", "TIME", NULL, USER, cputime, NULL, 9, 0, 0, NULL, NULL}, - {"tpgid", "TPGID", NULL, 0, evar, NULL, 4, EOFF(e_tpgid), UINT, PIDFMT, NULL}, - {"tsess", "TSESS", NULL, 0, evar, NULL, 6, EOFF(e_tsess), KPTR, "lx", NULL}, + {"tpgid", "TPGID", NULL, 0, pvar, NULL, 4, POFF(tpgid), UINT, PIDFMT, NULL}, + {"tsess", "TSESS", NULL, 0, pvar, NULL, 6, POFF(tsid), UINT, PIDFMT, NULL}, {"tsiz", "TSIZ", NULL, 0, tsize, NULL, 4, 0, 0, NULL, NULL}, {"tt", "TT ", NULL, 0, tname, NULL, 4, 0, 0, NULL, NULL}, {"tty", "TTY", NULL, LJUST, longtname, NULL, 8, 0, 0, NULL, NULL}, {"ucomm", "UCOMM", NULL, LJUST, ucomm, NULL, MAXCOMLEN, 0, 0, NULL, NULL}, - {"uid", "UID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_uid), + {"uid", "UID", NULL, 0, pvar, NULL, UIDLEN, POFF(uid), UINT, UIDFMT, NULL}, {"user", "USER", NULL, LJUST|DSIZ, uname, s_uname, USERLEN, 0, 0, NULL, NULL}, {"vsize", "", "vsz", 0, 0, NULL, 0, 0, 0, NULL, NULL}, {"vsz", "VSZ", NULL, 0, vsize, NULL, 5, 0, 0, NULL, NULL}, {"wchan", "WCHAN", NULL, LJUST, wchan, NULL, 6, 0, 0, NULL, NULL}, - {"xstat", "XSTAT", NULL, 0, pvar, NULL, 4, POFF(p_xstat), USHORT, "x", NULL}, + {"xstat", "XSTAT", NULL, 0, pvar, NULL, 4, POFF(exitstat), USHORT, "x", NULL}, {"", NULL, NULL, 0, NULL, NULL, 0, 0, 0, NULL, NULL}, }; diff --git a/bin/ps/print.c b/bin/ps/print.c index a7d21f138f..9cc680a62a 100644 --- a/bin/ps/print.c +++ b/bin/ps/print.c @@ -32,7 +32,7 @@ * * @(#)print.c 8.6 (Berkeley) 4/16/94 * $FreeBSD: src/bin/ps/print.c,v 1.36.2.4 2002/11/30 13:00:14 tjr Exp $ - * $DragonFly: src/bin/ps/print.c,v 1.25 2007/01/01 22:51:17 corecode Exp $ + * $DragonFly: src/bin/ps/print.c,v 1.26 2007/02/01 10:33:25 corecode Exp $ */ #include @@ -105,10 +105,10 @@ command(const KINFO *k, const struct varent *vent) if (cflag) { /* Don't pad the last field. */ if (STAILQ_NEXT(vent, link) == NULL) - printf("%s", make_printable(KI_THREAD(k)->td_comm)); + printf("%s", make_printable(KI_PROC(k, comm))); else printf("%-*s", vent->width, - make_printable(KI_THREAD(k)->td_comm)); + make_printable(KI_PROC(k, comm))); return; } @@ -152,13 +152,13 @@ command(const KINFO *k, const struct varent *vent) void ucomm(const KINFO *k, const struct varent *vent) { - printf("%-*s", vent->width, make_printable(KI_THREAD(k)->td_comm)); + printf("%-*s", vent->width, make_printable(KI_PROC(k, comm))); } void logname(const KINFO *k, const struct varent *vent) { - const char *s = KI_EPROC(k)->e_login; + const char *s = KI_PROC(k, login); printf("%-*s", vent->width, *s != '\0' ? s : "-"); } @@ -166,16 +166,14 @@ logname(const KINFO *k, const struct varent *vent) void state(const KINFO *k, const struct varent *vent) { - struct proc *p; int flag; char *cp; char buf[16]; - p = KI_PROC(k); - flag = p->p_flag; + flag = KI_LWP(k, flags); cp = buf; - switch (p->p_stat) { + switch (KI_PROC(k, stat)) { case SSTOP: *cp = 'T'; @@ -183,8 +181,8 @@ state(const KINFO *k, const struct varent *vent) case SSLEEP: if (flag & P_SINTR) /* interruptable (long) */ - *cp = p->p_slptime >= MAXSLP ? 'I' : 'S'; - else if (KI_THREAD(k)->td_flags & TDF_SINTR) + *cp = KI_LWP(k, slptime) >= MAXSLP ? 'I' : 'S'; + else if (KI_LWP(k, tdflags) & TDF_SINTR) *cp = 'S'; else *cp = 'D'; @@ -193,9 +191,9 @@ state(const KINFO *k, const struct varent *vent) case SRUN: case SIDL: *cp = 'R'; - if (KI_THREAD(k)->td_flags & TDF_RUNNING) { + if (KI_LWP(k, tdflags) & TDF_RUNNING) { ++cp; - sprintf(cp, "%d", KI_EPROC(k)->e_cpuid); + sprintf(cp, "%d", KI_LWP(k, cpuid)); while (cp[1]) ++cp; } @@ -211,25 +209,25 @@ state(const KINFO *k, const struct varent *vent) cp++; if (flag & P_SWAPPEDOUT) *cp++ = 'W'; - if (p->p_nice < NZERO) + if (KI_PROC(k, nice) < NZERO) *cp++ = '<'; - else if (p->p_nice > NZERO) + else if (KI_PROC(k, nice) > NZERO) *cp++ = 'N'; if (flag & P_TRACED) *cp++ = 'X'; - if (flag & P_WEXIT && p->p_stat != SZOMB) + if (flag & P_WEXIT && KI_LWP(k, stat) != SZOMB) *cp++ = 'E'; if (flag & P_PPWAIT) *cp++ = 'V'; - if ((flag & P_SYSTEM) || p->p_lock > 0) + if ((flag & P_SYSTEM) || KI_PROC(k, lock) > 0) *cp++ = 'L'; - if (numcpus > 1 && KI_THREAD(k)->td_mpcount_unused == 0) + if (numcpus > 1 && KI_LWP(k, mpcount) == 0) *cp++ = 'M'; if (flag & P_JAILED) *cp++ = 'J'; - if (KI_EPROC(k)->e_flag & EPROC_SLEADER) + if (KI_PROC(k, auxflags) & KI_SLEADER) *cp++ = 's'; - if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid) + if ((flag & P_CONTROLT) && KI_PROC(k, pgid) == KI_PROC(k, tpgid)) *cp++ = '+'; *cp = '\0'; printf("%-*s", vent->width, buf); @@ -244,17 +242,17 @@ state(const KINFO *k, const struct varent *vent) void pri(const KINFO *k, const struct varent *vent) { - if (KI_THREAD(k)->td_proc) - printf("%*d", vent->width, KI_PROC(k)->p_usdata.bsd4.priority); + if (KI_LWP(k, pid) != -1) + printf("%*d", vent->width, KI_LWP(k, prio)); else - printf("%*d", vent->width, -(KI_THREAD(k)->td_pri & TDPRI_MASK)); + printf("%*d", vent->width, -(KI_LWP(k, tdprio) & TDPRI_MASK)); } void tdpri(const KINFO *k, const struct varent *vent) { char buf[32]; - int val = KI_THREAD(k)->td_pri; + int val = KI_LWP(k, tdprio); snprintf(buf, sizeof(buf), "%02d/%d", val & TDPRI_MASK, val / TDPRI_CRIT); printf("%*s", vent->width, buf); @@ -264,26 +262,26 @@ void uname(const KINFO *k, const struct varent *vent) { printf("%-*s", vent->width, - user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)); + user_from_uid(KI_PROC(k, uid), 0)); } int s_uname(const KINFO *k) { - return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0))); + return (strlen(user_from_uid(KI_PROC(k, uid), 0))); } void runame(const KINFO *k, const struct varent *vent) { printf("%-*s", vent->width, - user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)); + user_from_uid(KI_PROC(k, ruid), 0)); } int s_runame(const KINFO *k) { - return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0))); + return (strlen(user_from_uid(KI_PROC(k, ruid), 0))); } void @@ -292,7 +290,7 @@ tdev(const KINFO *k, const struct varent *vent) dev_t dev; char buff[16]; - dev = KI_EPROC(k)->e_tdev; + dev = KI_PROC(k, tdev); if (dev == NODEV) printf("%*s", vent->width, "??"); else { @@ -307,7 +305,7 @@ tname(const KINFO *k, const struct varent *vent) dev_t dev; const char *ttname; - dev = KI_EPROC(k)->e_tdev; + dev = KI_PROC(k, tdev); if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) printf("%*s ", vent->width-1, "??"); else { @@ -315,7 +313,7 @@ tname(const KINFO *k, const struct varent *vent) strncmp(ttname, "cua", 3) == 0) ttname += 3; printf("%*.*s%c", vent->width-1, vent->width-1, ttname, - KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-'); + KI_PROC(k, auxflags) & KI_CTTY ? ' ' : '-'); } } @@ -325,7 +323,7 @@ longtname(const KINFO *k, const struct varent *vent) dev_t dev; const char *ttname; - dev = KI_EPROC(k)->e_tdev; + dev = KI_PROC(k, tdev); if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) printf("%-*s", vent->width, "??"); else @@ -344,7 +342,7 @@ started(const KINFO *k, const struct varent *vent) if (use_ampm < 0) use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0'); - then = k->ki_u.u_start.tv_sec; + then = KI_PROC(k, start).tv_sec; if (then < btime.tv_sec) { then = btime.tv_sec; } @@ -369,7 +367,7 @@ lstarted(const KINFO *k, const struct varent *vent) time_t then; char buf[100]; - then = k->ki_u.u_start.tv_sec; + then = KI_PROC(k, start).tv_sec; strftime(buf, sizeof(buf) -1, "%c", localtime(&then)); printf("%-*s", vent->width, buf); } @@ -377,13 +375,13 @@ lstarted(const KINFO *k, const struct varent *vent) void wchan(const KINFO *k, const struct varent *vent) { - if (KI_THREAD(k)->td_wchan) { - if (KI_THREAD(k)->td_wmesg) + if (KI_LWP(k, wchan)) { + if (*KI_LWP(k, wmesg) != '\0') printf("%-*.*s", vent->width, vent->width, - KI_EPROC(k)->e_wmesg); + KI_LWP(k, wmesg)); else printf("%-*lx", vent->width, - (long)KI_THREAD(k)->td_wchan); + (long)KI_LWP(k, wchan)); } else printf("%-*s", vent->width, "-"); } @@ -395,20 +393,20 @@ wchan(const KINFO *k, const struct varent *vent) void vsize(const KINFO *k, const struct varent *vent) { - printf("%*d", vent->width, (KI_EPROC(k)->e_vm.vm_map.size/1024)); + printf("%*d", vent->width, (KI_PROC(k, vm_map_size)/1024)); } void rssize(const KINFO *k, const struct varent *vent) { /* XXX don't have info about shared */ - printf("%*lu", vent->width, (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize)); + printf("%*lu", vent->width, (u_long)pgtok(KI_PROC(k, vm_rssize))); } void p_rssize(const KINFO *k, const struct varent *vent) /* doesn't account for text */ { - printf("%*ld", vent->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize)); + printf("%*ld", vent->width, (long)pgtok(KI_PROC(k, vm_rssize))); } void @@ -422,7 +420,7 @@ cputime(const KINFO *k, const struct varent *vent) if (decimal_point == '\0') decimal_point = localeconv()->decimal_point[0]; - if (KI_PROC(k)->p_stat == SZOMB) { + if (KI_LWP(k, stat) == SZOMB) { secs = 0; psecs = 0; } else { @@ -433,15 +431,15 @@ cputime(const KINFO *k, const struct varent *vent) * fix this, but it is not 100% trivial (and interrupt * time fractions only work on the sparc anyway). XXX */ - timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks + - KI_EPROC(k)->e_iticks; + timeus = KI_LWP(k, uticks) + KI_LWP(k, sticks) + + KI_LWP(k, iticks); secs = timeus / 1000000; psecs = timeus % 1000000; if (sumrusage) { - secs += k->ki_u.u_cru.ru_utime.tv_sec + - k->ki_u.u_cru.ru_stime.tv_sec; - psecs += k->ki_u.u_cru.ru_utime.tv_usec + - k->ki_u.u_cru.ru_stime.tv_usec; + secs += KI_PROC(k, cru).ru_utime.tv_sec + + KI_PROC(k, cru).ru_stime.tv_sec; + psecs += KI_PROC(k, cru).ru_utime.tv_usec + + KI_PROC(k, cru).ru_stime.tv_usec; } /* * round and scale to 100's @@ -458,7 +456,6 @@ cputime(const KINFO *k, const struct varent *vent) double getpcpu(const KINFO *k) { - const struct proc *p; static int failure; if (!nlistread) @@ -466,16 +463,15 @@ getpcpu(const KINFO *k) if (failure) return (0.0); - p = KI_PROC(k); #define fxtofl(fixpt) ((double)(fixpt) / fscale) /* XXX - I don't like this */ - if (p->p_swtime == 0 || (p->p_flag & P_SWAPPEDOUT)) + if (KI_LWP(k, swtime) == 0 || (KI_PROC(k, flags) & P_SWAPPEDOUT)) return (0.0); if (rawcpu) - return (100.0 * fxtofl(p->p_pctcpu)); - return (100.0 * fxtofl(p->p_pctcpu) / - (1.0 - exp(p->p_swtime * log(fxtofl(ccpu))))); + return (100.0 * fxtofl(KI_LWP(k, pctcpu))); + return (100.0 * fxtofl(KI_LWP(k, pctcpu)) / + (1.0 - exp(KI_LWP(k, swtime) * log(fxtofl(ccpu))))); } void @@ -489,18 +485,18 @@ pnice(const KINFO *k, const struct varent *vent) { int niceval; - switch (KI_PROC(k)->p_rtprio.type) { + switch (KI_LWP(k, rtprio).type) { case RTP_PRIO_REALTIME: - niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio; + niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_LWP(k, rtprio).prio; break; case RTP_PRIO_IDLE: - niceval = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio; + niceval = PRIO_MAX + 1 + KI_LWP(k, rtprio).prio; break; case RTP_PRIO_THREAD: - niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio; + niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_LWP(k, rtprio).prio; break; default: - niceval = KI_PROC(k)->p_nice - NZERO; + niceval = KI_PROC(k, nice) - NZERO; break; } printf("%*d", vent->width, niceval); @@ -511,8 +507,6 @@ double getpmem(const KINFO *k) { static int failure; - struct proc *p; - struct eproc *e; double fracmem; int szptudot; @@ -521,14 +515,12 @@ getpmem(const KINFO *k) if (failure) return (0.0); - p = KI_PROC(k); - e = KI_EPROC(k); - if (p->p_flag & P_SWAPPEDOUT) + if (KI_PROC(k, flags) & P_SWAPPEDOUT) return (0.0); /* XXX want pmap ptpages, segtab, etc. (per architecture) */ szptudot = UPAGES; /* XXX don't have info about shared */ - fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages; + fracmem = ((float)KI_PROC(k, vm_rssize) + szptudot)/mempages; return (100.0 * fracmem); } @@ -541,21 +533,20 @@ pmem(const KINFO *k, const struct varent *vent) void pagein(const KINFO *k, const struct varent *vent) { - printf("%*ld", vent->width, k->ki_u.u_ru.ru_majflt); + printf("%*ld", vent->width, KI_LWP(k, ru).ru_majflt); } /* ARGSUSED */ void maxrss(const KINFO *k __unused, const struct varent *vent) { - /* XXX not yet */ - printf("%*s", vent->width, "-"); + printf("%*ld", vent->width, KI_PROC(k, ru).ru_maxrss); } void tsize(const KINFO *k, const struct varent *vent) { - printf("%*ld", vent->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize)); + printf("%*ld", vent->width, (long)pgtok(KI_PROC(k, vm_tsize))); } void @@ -565,7 +556,7 @@ rtprior(const KINFO *k, const struct varent *vent) char str[8]; unsigned prio, type; - prtp = (struct rtprio *) ((char *)KI_PROC(k) + vent->var->off); + prtp = &KI_LWP(k, rtprio); prio = prtp->prio; type = prtp->type; switch (type) { @@ -640,42 +631,30 @@ printval(const char *bp, const struct varent *vent) void pvar(const KINFO *k, const struct varent *vent) { - printval((char *)((char *)KI_PROC(k) + vent->var->off), vent); + printval((char *)((char *)k->ki_proc + vent->var->off), vent); } void -pest(const KINFO *k, const struct varent *vent) +lpest(const KINFO *k, const struct varent *vent) { int val; - val = *(int *)((char *)KI_PROC(k) + vent->var->off); + val = *(int *)((char *)&k->ki_proc->kp_lwp + vent->var->off); val = val / 128; printval((char *)&val, vent); } void -tvar(const KINFO *k, const struct varent *vent) +lpvar(const KINFO *k, const struct varent *vent) { - printval((char *)((char *)KI_THREAD(k) + vent->var->off), vent); -} - -void -evar(const KINFO *k, const struct varent *vent) -{ - printval((char *)((char *)KI_EPROC(k) + vent->var->off), vent); -} - -void -uvar(const KINFO *k, const struct varent *vent) -{ - printval(((const char *)&k->ki_u + vent->var->off), vent); + printval((char *)((char *)&k->ki_proc->kp_lwp + vent->var->off), vent); } void rvar(const KINFO *k, const struct varent *vent) { - printval(((const char *)&k->ki_u.u_ru + vent->var->off), vent); + printval(((const char *)&KI_LWP(k, ru) + vent->var->off), vent); } static const char * diff --git a/bin/ps/ps.c b/bin/ps/ps.c index 2d09e364db..860acc5bf1 100644 --- a/bin/ps/ps.c +++ b/bin/ps/ps.c @@ -33,7 +33,7 @@ * @(#) Copyright (c) 1990, 1993, 1994 The Regents of the University of California. All rights reserved. * @(#)ps.c 8.4 (Berkeley) 4/2/94 * $FreeBSD: src/bin/ps/ps.c,v 1.30.2.6 2002/07/04 08:30:37 sobomax Exp $ - * $DragonFly: src/bin/ps/ps.c,v 1.21 2007/01/01 22:51:17 corecode Exp $ + * $DragonFly: src/bin/ps/ps.c,v 1.22 2007/02/01 10:33:25 corecode Exp $ */ #include @@ -367,7 +367,7 @@ main(int argc, char **argv) if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL) err(1, NULL); for (i = nentries; --i >= 0; ++kp) { - kinfo[i].ki_p = kp; + kinfo[i].ki_proc = kp; if (needuser) saveuser(&kinfo[i]); dynsizevars(&kinfo[i]); @@ -389,12 +389,12 @@ main(int argc, char **argv) * for each proc, call each variable output function. */ for (i = lineno = 0; i < nentries; i++) { - if (xflg == 0 && (KI_EPROC(&kinfo[i])->e_tdev == NODEV || - (KI_PROC(&kinfo[i])->p_flag & P_CONTROLT ) == 0)) + if (xflg == 0 && (KI_PROC(&kinfo[i], tdev) == NODEV || + (KI_PROC(&kinfo[i], flags) & P_CONTROLT ) == 0)) continue; if (nuids > 1) { for (uid = 0; uid < nuids; uid++) - if (KI_EPROC(&kinfo[i])->e_ucred.cr_uid == + if (KI_PROC(&kinfo[i], uid) == uids[uid]) break; if (uid == nuids) @@ -522,43 +522,25 @@ getfmt(char **(*fn) (kvm_t *, const struct kinfo_proc *, int), KINFO *ki, char const char *s; if ((s = - fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen)) == NULL) + fmt_argv((*fn)(kd, ki->ki_proc, termwidth), comm, maxlen)) == NULL) err(1, NULL); return (s); } -#define UREADOK(ki) \ - (forceuread || (KI_PROC(ki)->p_flag & P_SWAPPEDOUT) == 0) - static void saveuser(KINFO *ki) { - struct usave *usp; - - usp = &ki->ki_u; - - usp->u_start = KI_PROC(ki)->p_start; - usp->u_ru = KI_PROC(ki)->p_ru; - usp->u_cru = KI_PROC(ki)->p_cru; - /* * save arguments if needed */ - if (needcomm && (UREADOK(ki) || (KI_PROC(ki)->p_args != NULL))) { - ki->ki_args = getfmt(kvm_getargv, ki, KI_THREAD(ki)->td_comm, + if (needcomm) { + ki->ki_args = getfmt(kvm_getargv, ki, KI_PROC(ki, comm), MAXCOMLEN); - } else if (needcomm) { - char *tmp; - tmp = malloc(strlen(KI_THREAD(ki)->td_comm) + 3); - sprintf(tmp, "(%s)", KI_THREAD(ki)->td_comm); - ki->ki_args = tmp; } else { ki->ki_args = NULL; } - if (needenv && UREADOK(ki)) { + if (needenv) { ki->ki_env = getfmt(kvm_getenvv, ki, (char *)NULL, 0); - } else if (needenv) { - ki->ki_env = "()"; } else { ki->ki_env = NULL; } @@ -568,8 +550,8 @@ static int pscomp(const void *a, const void *b) { int i; -#define VSIZE(k) (KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize + \ - KI_EPROC(k)->e_vm.vm_tsize) +#define VSIZE(k) (KI_PROC(k, vm_dsize) + KI_PROC(k, vm_ssize) + \ + KI_PROC(k, vm_tsize)) #if 0 if (sortby == SORTIAC) @@ -579,9 +561,9 @@ pscomp(const void *a, const void *b) return (getpcpu((const KINFO *)b) - getpcpu((const KINFO *)a)); if (sortby == SORTMEM) return (VSIZE((const KINFO *)b) - VSIZE((const KINFO *)a)); - i = KI_EPROC((const KINFO *)a)->e_tdev - KI_EPROC((const KINFO *)b)->e_tdev; + i = KI_PROC((const KINFO *)a, tdev) - KI_PROC((const KINFO *)b, tdev); if (i == 0) - i = KI_PROC((const KINFO *)a)->p_pid - KI_PROC((const KINFO *)b)->p_pid; + i = KI_PROC((const KINFO *)a, pid) - KI_PROC((const KINFO *)b, pid); return (i); } diff --git a/bin/ps/ps.h b/bin/ps/ps.h index c488c5be69..f811308793 100644 --- a/bin/ps/ps.h +++ b/bin/ps/ps.h @@ -32,26 +32,17 @@ * * @(#)ps.h 8.1 (Berkeley) 5/31/93 * $FreeBSD: src/bin/ps/ps.h,v 1.7 1999/08/27 23:14:52 peter Exp $ - * $DragonFly: src/bin/ps/ps.h,v 1.8 2007/01/01 22:51:17 corecode Exp $ + * $DragonFly: src/bin/ps/ps.h,v 1.9 2007/02/01 10:33:25 corecode Exp $ */ #define UNLIMITED 0 /* unlimited terminal width */ enum type { CHAR, UCHAR, SHORT, USHORT, INT, UINT, LONG, ULONG, KPTR }; -struct usave { - struct timeval u_start; - struct rusage u_ru; - struct rusage u_cru; - char u_acflag; -}; - -#define KI_PROC(ki) (&(ki)->ki_p->kp_proc) -#define KI_THREAD(ki) (&(ki)->ki_p->kp_thread) -#define KI_EPROC(ki) (&(ki)->ki_p->kp_eproc) +#define KI_PROC(ki, f) ((ki)->ki_proc->kp_ ## f) +#define KI_LWP(ki, f) ((ki)->ki_proc->kp_lwp.kl_ ## f) typedef struct kinfo { - struct kinfo_proc *ki_p; /* proc structure */ - struct usave ki_u; /* interesting parts of user */ + struct kinfo_proc *ki_proc; /* proc info structure */ const char *ki_args; /* exec args */ const char *ki_env; /* environment */ } KINFO; diff --git a/contrib/ipfilter/ipsend/sock.c b/contrib/ipfilter/ipsend/sock.c index b67f2e5393..4d72d8726a 100644 --- a/contrib/ipfilter/ipsend/sock.c +++ b/contrib/ipfilter/ipsend/sock.c @@ -1,5 +1,5 @@ /* $FreeBSD: src/contrib/ipfilter/ipsend/sock.c,v 1.3.2.4 2003/03/01 03:55:53 darrenr Exp $ */ -/* $DragonFly: src/contrib/ipfilter/ipsend/sock.c,v 1.3 2005/06/22 02:08:19 dillon Exp $ */ +/* $DragonFly: src/contrib/ipfilter/ipsend/sock.c,v 1.4 2007/02/01 10:33:25 corecode Exp $ */ /* * sock.c (C) 1995-1998 Darren Reed * @@ -286,21 +286,12 @@ struct tcpiphdr *ti; return NULL; fd = (struct filedesc *)malloc(sizeof(*fd)); -#if defined( __FreeBSD_version) && __FreeBSD_version >= 500013 - if (KMCPY(fd, p->ki_fd, sizeof(*fd)) == -1) + if (KMCPY(fd, p->kp_fd, sizeof(*fd)) == -1) { fprintf(stderr, "read(%#lx,%#lx) failed\n", - (u_long)p, (u_long)p->ki_fd); + (u_long)p, (u_long)p->kp_fd); return NULL; } -#else - if (KMCPY(fd, p->kp_proc.p_fd, sizeof(*fd)) == -1) - { - fprintf(stderr, "read(%#lx,%#lx) failed\n", - (u_long)p, (u_long)p->kp_proc.p_fd); - return NULL; - } -#endif o = (struct fdnode *)calloc(1, sizeof(*o) * (fd->fd_lastfile + 1)); if (KMCPY(o, fd->fd_files, (fd->fd_lastfile + 1) * sizeof(*o)) == -1) diff --git a/lib/libc/gen/sysctl.3 b/lib/libc/gen/sysctl.3 index 4f6304c4d4..8065ffb56e 100644 --- a/lib/libc/gen/sysctl.3 +++ b/lib/libc/gen/sysctl.3 @@ -31,7 +31,7 @@ .\" .\" @(#)sysctl.3 8.4 (Berkeley) 5/9/95 .\" $FreeBSD: src/lib/libc/gen/sysctl.3,v 1.33.2.13 2002/04/07 04:57:14 dd Exp $ -.\" $DragonFly: src/lib/libc/gen/sysctl.3,v 1.5 2006/05/26 19:39:36 swildner Exp $ +.\" $DragonFly: src/lib/libc/gen/sysctl.3,v 1.6 2007/02/01 10:33:26 corecode Exp $ .\" .Dd January 23, 2001 .Dt SYSCTL 3 @@ -434,6 +434,11 @@ The third and fourth level names are as follows: .It "KERN\_PROC\_UID A user ID" .It "KERN\_PROC\_RUID A real user ID" .El +.Pp +Adding the flag +.Li KERN_PROC_FLAG_LWP +to the third level name signals that information about all +light weight processes of the selected processes should be returned. .It Li KERN_PROF Return profiling information about the kernel. If the kernel is not compiled for profiling, diff --git a/lib/libkcore/kcore_file.c b/lib/libkcore/kcore_file.c index 445e9384cf..6d5f67eac9 100644 --- a/lib/libkcore/kcore_file.c +++ b/lib/libkcore/kcore_file.c @@ -31,7 +31,7 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/lib/libkcore/kcore_file.c,v 1.3 2005/06/22 01:51:40 dillon Exp $ + * $DragonFly: src/lib/libkcore/kcore_file.c,v 1.4 2007/02/01 10:33:25 corecode Exp $ */ #define _KERNEL_STRUCTURES @@ -54,7 +54,7 @@ int kcore_get_files(struct kcore_data *kc, struct kinfo_file **files, size_t *len) { struct kinfo_proc *procs, *oprocs; - struct proc *p; + struct proc p; struct filedesc fdp; struct file fp, *fpp; size_t len_procs; @@ -85,30 +85,35 @@ kcore_get_files(struct kcore_data *kc, struct kinfo_file **files, size_t *len) oprocs = procs; for (; len_procs-- > 0; procs++) { - p = &procs->kp_proc; - if (p->p_fd == NULL || p->p_stat == SIDL) + if (kvm_read(kc->kd, procs->kp_paddr, &p, + sizeof (p)) != sizeof(p)) { + warnx("cannot read proc at %p for pid %d\n", + (void *)procs->kp_paddr, procs->kp_pid); continue; - if (kvm_read(kc->kd, (long)p->p_fd, &fdp, + } + if (p.p_fd == NULL || procs->kp_stat == SIDL) + continue; + if (kvm_read(kc->kd, (long)p.p_fd, &fdp, sizeof (fdp)) != sizeof(fdp)) { warnx("cannot read filedesc at %p for pid %d\n", - p->p_fd, p->p_pid); + p.p_fd, procs->kp_pid); continue; } for (n = 0; n < fdp.fd_nfiles; n++) { if (kvm_read(kc->kd, (long)(&fdp.fd_files[n].fp), &fpp, sizeof(fpp)) != sizeof(fpp)) { warnx("cannot read filep at %p for pid %d\n", - &fdp.fd_files[n].fp, p->p_pid); + &fdp.fd_files[n].fp, procs->kp_pid); } if (fpp == NULL) continue; if (kvm_read(kc->kd, (long)fpp, &fp, sizeof(fp)) != sizeof(fp)) { warnx("cannot read file at %p for pid %d\n", - fpp, p->p_pid); + fpp, procs->kp_pid); continue; } - kcore_make_file(*files + *len, &fp, p->p_pid, 0, n); + kcore_make_file(*files + *len, &fp, procs->kp_pid, 0, n); (*len)++; } } diff --git a/lib/libkvm/Makefile b/lib/libkvm/Makefile index c6c5521ec5..afd5af1cc1 100644 --- a/lib/libkvm/Makefile +++ b/lib/libkvm/Makefile @@ -1,11 +1,15 @@ # @(#)Makefile 8.1 (Berkeley) 6/4/93 # $FreeBSD: src/lib/libkvm/Makefile,v 1.9.2.1 2001/04/25 10:04:32 ru Exp $ -# $DragonFly: src/lib/libkvm/Makefile,v 1.3 2004/04/02 05:46:02 hmp Exp $ +# $DragonFly: src/lib/libkvm/Makefile,v 1.4 2007/02/01 10:33:25 corecode Exp $ + +SYSDIR= ${.CURDIR}/../../sys/ +.PATH: ${SYSDIR}/kern LIB= kvm -CFLAGS+=-DLIBC_SCCS -I${.CURDIR}/../../sys +CFLAGS+=-DLIBC_SCCS -I${SYSDIR} SRCS= kvm.c kvm_${MACHINE_ARCH}.c kvm_file.c kvm_getloadavg.c \ - kvm_getswapinfo.c kvm_proc.c kvm_util.c + kvm_getswapinfo.c kvm_proc.c kvm_util.c \ + kern_kinfo.c INCS= kvm.h MAN= kvm.3 kvm_geterr.3 kvm_getfiles.3 kvm_getloadavg.3 kvm_getprocs.3 \ diff --git a/lib/libkvm/kvm.c b/lib/libkvm/kvm.c index b7cbf2cf4a..935d84b4d7 100644 --- a/lib/libkvm/kvm.c +++ b/lib/libkvm/kvm.c @@ -36,7 +36,7 @@ * * @(#)kvm.c 8.2 (Berkeley) 2/13/94 * $FreeBSD: src/lib/libkvm/kvm.c,v 1.12.2.3 2002/09/13 14:53:43 nectar Exp $ - * $DragonFly: src/lib/libkvm/kvm.c,v 1.8 2006/01/11 01:12:59 corecode Exp $ + * $DragonFly: src/lib/libkvm/kvm.c,v 1.9 2007/02/01 10:33:25 corecode Exp $ */ #include @@ -141,7 +141,8 @@ _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout) kd->pmfd = -1; kd->nlfd = -1; kd->vmst = 0; - kd->procbase = 0; + kd->procbase = NULL; + kd->procend = NULL; kd->argspc = 0; kd->argv = 0; @@ -266,8 +267,8 @@ kvm_close(kvm_t *kd) error |= close(kd->nlfd); if (kd->vmst) _kvm_freevtop(kd); - if (kd->procbase != 0) - free((void *)kd->procbase); + if (kd->procbase != NULL) + free(kd->procbase); if (kd->argv != 0) free((void *)kd->argv); free((void *)kd); diff --git a/lib/libkvm/kvm.h b/lib/libkvm/kvm.h index c71e9f873e..1d4a3808ca 100644 --- a/lib/libkvm/kvm.h +++ b/lib/libkvm/kvm.h @@ -32,7 +32,7 @@ * * @(#)kvm.h 8.1 (Berkeley) 6/2/93 * $FreeBSD: src/lib/libkvm/kvm.h,v 1.11 1999/08/27 23:44:50 peter Exp $ - * $DragonFly: src/lib/libkvm/kvm.h,v 1.6 2006/01/11 01:12:59 corecode Exp $ + * $DragonFly: src/lib/libkvm/kvm.h,v 1.7 2007/02/01 10:33:25 corecode Exp $ */ #ifndef _KVM_H_ @@ -83,7 +83,7 @@ kvm_t *kvm_openfiles __ssize_t kvm_read (kvm_t *, unsigned long, void *, __size_t); char *kvm_readstr(kvm_t *, u_long, char *, size_t *); __ssize_t kvm_uread - (kvm_t *, const struct proc *, unsigned long, char *, __size_t); + (kvm_t *, pid_t, unsigned long, char *, __size_t); __ssize_t kvm_write (kvm_t *, unsigned long, const void *, __size_t); __END_DECLS diff --git a/lib/libkvm/kvm_getprocs.3 b/lib/libkvm/kvm_getprocs.3 index e4dc1a51e1..096235660a 100644 --- a/lib/libkvm/kvm_getprocs.3 +++ b/lib/libkvm/kvm_getprocs.3 @@ -35,7 +35,7 @@ .\" .\" @(#)kvm_getprocs.3 8.1 (Berkeley) 6/4/93 .\" $FreeBSD: src/lib/libkvm/kvm_getprocs.3,v 1.6.2.7 2003/04/29 14:37:04 trhodes Exp $ -.\" $DragonFly: src/lib/libkvm/kvm_getprocs.3,v 1.3 2006/02/17 19:35:07 swildner Exp $ +.\" $DragonFly: src/lib/libkvm/kvm_getprocs.3,v 1.4 2007/02/01 10:33:25 corecode Exp $ .\" .Dd June 4, 1993 .Dt KVM_GETPROCS 3 @@ -95,6 +95,12 @@ processes with real user id .Fa arg .El .Pp +If the list of processes should contain all light weight processes, +add the flag +.Sy KERN_PROC_FLAG_LWP +to +.Fa op . +.Pp The number of processes found is returned in the reference parameter .Fa cnt . The processes are returned as a contiguous array of kinfo_proc structures. diff --git a/lib/libkvm/kvm_private.h b/lib/libkvm/kvm_private.h index 81e8b89467..1dd03d2e49 100644 --- a/lib/libkvm/kvm_private.h +++ b/lib/libkvm/kvm_private.h @@ -36,7 +36,7 @@ * * @(#)kvm_private.h 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libkvm/kvm_private.h,v 1.5 1999/12/27 07:14:58 peter Exp $ - * $DragonFly: src/lib/libkvm/kvm_private.h,v 1.3 2003/11/12 20:21:30 eirikn Exp $ + * $DragonFly: src/lib/libkvm/kvm_private.h,v 1.4 2007/02/01 10:33:25 corecode Exp $ */ struct __kvm { @@ -54,6 +54,7 @@ struct __kvm { int unused; /* was: swap file (e.g., /dev/drum) */ int nlfd; /* namelist file (e.g., /kernel) */ struct kinfo_proc *procbase; + struct kinfo_proc *procend; char *argspc; /* (dynamic) storage for argv strings */ int arglen; /* length of the above */ char **argv; /* (dynamic) storage for argv pointers */ diff --git a/lib/libkvm/kvm_proc.c b/lib/libkvm/kvm_proc.c index 62b467eb32..549c7634a2 100644 --- a/lib/libkvm/kvm_proc.c +++ b/lib/libkvm/kvm_proc.c @@ -35,7 +35,7 @@ * SUCH DAMAGE. * * $FreeBSD: src/lib/libkvm/kvm_proc.c,v 1.25.2.3 2002/08/24 07:27:46 kris Exp $ - * $DragonFly: src/lib/libkvm/kvm_proc.c,v 1.9 2006/11/09 16:52:41 dillon Exp $ + * $DragonFly: src/lib/libkvm/kvm_proc.c,v 1.10 2007/02/01 10:33:25 corecode Exp $ * * @(#)kvm_proc.c 8.3 (Berkeley) 9/23/93 */ @@ -56,6 +56,7 @@ #include #include #include +#include #include #include #include @@ -89,37 +90,63 @@ kvm_readswap(kvm_t *kd, const struct proc *p, u_long va, u_long *cnt) #define KREAD(kd, addr, obj) \ (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj)) + +static struct kinfo_proc * +kinfo_resize_proc(kvm_t *kd, struct kinfo_proc *bp) +{ + if (bp < kd->procend) + return bp; + + size_t pos = bp - kd->procend; + size_t size = kd->procend - kd->procbase; + + if (size == 0) + size = 8; + else + size *= 2; + kd->procbase = _kvm_realloc(kd, kd->procbase, sizeof(*bp) * size); + if (kd->procbase == NULL) + return NULL; + kd->procend = kd->procbase + size; + bp = kd->procbase + pos; + return bp; +} + /* * Read proc's from memory file into buffer bp, which has space to hold * at most maxcnt procs. */ static int kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p, - struct kinfo_proc *bp, int maxcnt) + struct kinfo_proc *bp) { - int cnt = 0; - struct eproc eproc; struct pgrp pgrp; + struct pgrp tpgrp; struct session sess; struct tty tty; struct proc proc; + struct ucred ucred; struct thread thread; struct proc pproc; struct cdev cdev; + struct vmspace vmspace; + struct prison prison; + struct lwp lwp; + uintptr_t lwppos; - for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) { + for (; p != NULL; p = proc.p_list.le_next) { if (KREAD(kd, (u_long)p, &proc)) { _kvm_err(kd, kd->program, "can't read proc at %x", p); return (-1); } - if (KREAD(kd, (u_long)proc.p_thread, &thread)) { - _kvm_err(kd, kd->program, "can't read thread at %x", - proc.p_thread); + if (KREAD(kd, (u_long)proc.p_ucred, &ucred)) { + _kvm_err(kd, kd->program, "can't read ucred at %p", + proc.p_ucred); return (-1); } - KREAD(kd, (u_long)proc.p_ucred, &eproc.e_ucred); + proc.p_ucred = &ucred; - switch(what) { + switch(what & ~KERN_PROC_FLAGMASK) { case KERN_PROC_PID: if (proc.p_pid != (pid_t)arg) @@ -127,131 +154,140 @@ kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p, break; case KERN_PROC_UID: - if (eproc.e_ucred.cr_uid != (uid_t)arg) + if (ucred.cr_uid != (uid_t)arg) continue; break; case KERN_PROC_RUID: - if (eproc.e_ucred.cr_ruid != (uid_t)arg) + if (ucred.cr_ruid != (uid_t)arg) continue; break; } - /* - * We're going to add another proc to the set. If this - * will overflow the buffer, assume the reason is because - * nprocs (or the proc list) is corrupt and declare an error. - */ - if (cnt >= maxcnt) { - _kvm_err(kd, kd->program, "nprocs corrupt"); - return (-1); - } - /* - * gather eproc - */ - eproc.e_paddr = p; + if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { _kvm_err(kd, kd->program, "can't read pgrp at %x", proc.p_pgrp); return (-1); } - if (proc.p_oppid) - eproc.e_ppid = proc.p_oppid; - else if (proc.p_pptr) { + proc.p_pgrp = &pgrp; + if (proc.p_pptr) { if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) { _kvm_err(kd, kd->program, "can't read pproc at %x", proc.p_pptr); return (-1); } - eproc.e_ppid = pproc.p_pid; - } else - eproc.e_ppid = 0; - eproc.e_sess = pgrp.pg_session; - eproc.e_pgid = pgrp.pg_id; - eproc.e_jobc = pgrp.pg_jobc; + proc.p_pptr = &pproc; + } if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { _kvm_err(kd, kd->program, "can't read session at %x", pgrp.pg_session); return (-1); } - (void)memcpy(eproc.e_login, sess.s_login, - sizeof(eproc.e_login)); + pgrp.pg_session = &sess; + if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { _kvm_err(kd, kd->program, "can't read tty at %x", sess.s_ttyp); return (-1); } + sess.s_ttyp = &tty; if (tty.t_dev && tty.t_dev != NOCDEV) { - if (KREAD(kd, (u_long)tty.t_dev, &cdev)) { - eproc.e_tdev = cdev.si_udev; - } else { - eproc.e_tdev = NODEV; - } - } else { - eproc.e_tdev = NODEV; + if (KREAD(kd, (u_long)tty.t_dev, &cdev)) + tty.t_dev = NOCDEV; + else + tty.t_dev = &cdev; } - eproc.e_tsess = tty.t_session; if (tty.t_pgrp != NULL) { - if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { + if (KREAD(kd, (u_long)tty.t_pgrp, &tpgrp)) { _kvm_err(kd, kd->program, "can't read tpgrp at %x", tty.t_pgrp); return (-1); } - eproc.e_tpgid = pgrp.pg_id; - } else - eproc.e_tpgid = -1; - } else - eproc.e_tdev = NODEV; - eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0; - if (sess.s_leader == p) - eproc.e_flag |= EPROC_SLEADER; - if (thread.td_wmesg) - (void)kvm_read(kd, (u_long)thread.td_wmesg, - eproc.e_wmesg, WMESGLEN); - -#ifdef sparc - (void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_rssize, - (char *)&eproc.e_vm.vm_rssize, - sizeof(eproc.e_vm.vm_rssize)); - (void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_tsize, - (char *)&eproc.e_vm.vm_tsize, - 3 * sizeof(eproc.e_vm.vm_rssize)); /* XXX */ -#else - (void)kvm_read(kd, (u_long)proc.p_vmspace, - (char *)&eproc.e_vm, sizeof(eproc.e_vm)); -#endif - eproc.e_xsize = eproc.e_xrssize = 0; - eproc.e_xccount = eproc.e_xswrss = 0; + tty.t_pgrp = &tpgrp; + } + } + + if (KREAD(kd, (u_long)proc.p_vmspace, &vmspace)) { + _kvm_err(kd, kd->program, "can't read vmspace at %p", + proc.p_vmspace); + return (-1); + } + proc.p_vmspace = &vmspace; - switch (what) { + if (ucred.cr_prison != NULL) { + if (KREAD(kd, (u_long)ucred.cr_prison, &prison)) { + _kvm_err(kd, kd->program, "can't read prison at %p", + ucred.cr_prison); + return (-1); + } + ucred.cr_prison = &prison; + } + + switch (what & ~KERN_PROC_FLAGMASK) { case KERN_PROC_PGRP: - if (eproc.e_pgid != (pid_t)arg) + if (proc.p_pgrp->pg_id != (pid_t)arg) continue; break; case KERN_PROC_TTY: if ((proc.p_flag & P_CONTROLT) == 0 || - eproc.e_tdev != (dev_t)arg) + proc.p_pgrp->pg_session->s_ttyp->t_dev->si_udev + != (dev_t)arg) continue; break; } - bcopy(&proc, &bp->kp_proc, sizeof(proc)); - bcopy(&eproc, &bp->kp_eproc, sizeof(eproc)); - ++bp; - ++cnt; + + if ((bp = kinfo_resize_proc(kd, bp)) == NULL) + return (-1); + fill_kinfo_proc(&proc, bp); + bp->kp_paddr = (uintptr_t)p; + + lwppos = (uintptr_t)proc.p_lwps.lh_first; + if (lwppos == 0) + bp++; /* Just export the proc then */ + while (lwppos != 0) { + if (KREAD(kd, lwppos, &lwp)) { + _kvm_err(kd, kd->program, "can't read lwp at %p", + lwppos); + return (-1); + } + if (p != lwp.lwp_proc) { + _kvm_err(kd, kd->program, "lwp has wrong parent"); + return (-1); + } + lwp.lwp_proc = &proc; + if (KREAD(kd, (u_long)lwp.lwp_thread, &thread)) { + _kvm_err(kd, kd->program, "can't read thread at %x", + lwp.lwp_thread); + return (-1); + } + lwp.lwp_thread = &thread; + + if ((bp = kinfo_resize_proc(kd, bp)) == NULL) + return (-1); + fill_kinfo_proc(&proc, bp); + fill_kinfo_lwp(&lwp, &bp->kp_lwp); + bp->kp_paddr = (uintptr_t)p; + bp++; + if ((what & KERN_PROC_FLAG_LWP) == 0) + break; + + lwppos = (uintptr_t)lwp.lwp_list.le_next; + } } - return (cnt); + return (0); } /* * Build proc info array by reading in proc list from a crash dump. - * Return number of procs read. maxcnt is the max we will read. + * We reallocate kd->procbase as necessary. */ static int kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc, - u_long a_zombproc, int maxcnt) + u_long a_zombproc) { struct kinfo_proc *bp = kd->procbase; int acnt, zcnt; @@ -261,7 +297,7 @@ kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc, _kvm_err(kd, kd->program, "cannot read allproc"); return (-1); } - acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); + acnt = kvm_proclist(kd, what, arg, p, bp); if (acnt < 0) return (acnt); @@ -269,7 +305,7 @@ kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc, _kvm_err(kd, kd->program, "cannot read zombproc"); return (-1); } - zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); + zcnt = kvm_proclist(kd, what, arg, p, bp + acnt); if (zcnt < 0) zcnt = 0; @@ -340,13 +376,8 @@ kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt) _kvm_err(kd, kd->program, "can't read nprocs"); return (0); } - size = nprocs * sizeof(struct kinfo_proc); - kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); - if (kd->procbase == 0) - return (0); - nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, - nl[2].n_value, nprocs); + nl[2].n_value); #ifdef notdef size = nprocs * sizeof(struct kinfo_proc); (void)realloc(kd->procbase, size); @@ -382,13 +413,13 @@ _kvm_realloc(kvm_t *kd, void *p, size_t n) #endif /* - * Read in an argument vector from the user address space of process p. + * Read in an argument vector from the user address space of process pid. * addr if the user-space base address of narg null-terminated contiguous * strings. This is used to read in both the command arguments and * environment strings. Read at most maxcnt characters of strings. */ static char ** -kvm_argv(kvm_t *kd, const struct proc *p, u_long addr, int narg, int maxcnt) +kvm_argv(kvm_t *kd, pid_t pid, u_long addr, int narg, int maxcnt) { char *np, *cp, *ep, *ap; u_long oaddr = -1; @@ -446,7 +477,7 @@ kvm_argv(kvm_t *kd, const struct proc *p, u_long addr, int narg, int maxcnt) /* Pull in the target process'es argv vector */ cc = sizeof(char *) * narg; - if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc) + if (kvm_uread(kd, pid, addr, (char *)kd->argv, cc) != cc) return (0); /* * ap : saved start address of string we're working on in kd->argspc @@ -474,7 +505,7 @@ kvm_argv(kvm_t *kd, const struct proc *p, u_long addr, int narg, int maxcnt) /* is it the same page as the last one? */ if (addr != oaddr) { - if (kvm_uread(kd, p, addr, kd->argbuf, PAGE_SIZE) != + if (kvm_uread(kd, pid, addr, kd->argbuf, PAGE_SIZE) != PAGE_SIZE) return (0); oaddr = addr; @@ -580,28 +611,32 @@ ps_str_e(struct ps_strings *p, u_long *addr, int *n) * being wrong are very low. */ static int -proc_verify(kvm_t *kd, u_long kernp, const struct proc *p) +proc_verify(kvm_t *kd, const struct kinfo_proc *p) { struct kinfo_proc kp; int mib[4]; size_t len; + int error; mib[0] = CTL_KERN; mib[1] = KERN_PROC; mib[2] = KERN_PROC_PID; - mib[3] = p->p_pid; + mib[3] = p->kp_pid; + len = sizeof(kp); - if (sysctl(mib, 4, &kp, &len, NULL, 0) == -1) + error = sysctl(mib, 4, &kp, &len, NULL, 0); + if (error) return (0); - return (p->p_pid == kp.kp_proc.p_pid && - (kp.kp_proc.p_stat != SZOMB || p->p_stat == SZOMB)); + + error = (p->kp_pid == kp.kp_pid && + (kp.kp_stat != SZOMB || p->kp_stat == SZOMB)); + return (error); } static char ** kvm_doargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr, void (*info)(struct ps_strings *, u_long *, int *)) { - const struct proc *p = &kp->kp_proc; char **ap; u_long addr; int cnt; @@ -619,20 +654,20 @@ kvm_doargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr, /* * Pointers are stored at the top of the user stack. */ - if (p->p_stat == SZOMB || - kvm_uread(kd, p, ps_strings, (char *)&arginfo, + if (kp->kp_stat == SZOMB || + kvm_uread(kd, kp->kp_pid, ps_strings, (char *)&arginfo, sizeof(arginfo)) != sizeof(arginfo)) return (0); (*info)(&arginfo, &addr, &cnt); if (cnt == 0) return (0); - ap = kvm_argv(kd, p, addr, cnt, nchr); + ap = kvm_argv(kd, kp->kp_pid, addr, cnt, nchr); /* * For live kernels, make sure this process didn't go away. */ if (ap != 0 && ISALIVE(kd) && - !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p)) + !proc_verify(kd, kp)) ap = 0; return (ap); } @@ -675,7 +710,7 @@ kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) oid[0] = CTL_KERN; oid[1] = KERN_PROC; oid[2] = KERN_PROC_ARGS; - oid[3] = kp->kp_proc.p_pid; + oid[3] = kp->kp_pid; bufsz = buflen; i = sysctl(oid, 4, buf, &bufsz, 0, 0); if (i == 0 && bufsz > 0) { @@ -694,7 +729,7 @@ kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) return (bufp); } } - if (kp->kp_proc.p_flag & P_SYSTEM) + if (kp->kp_flags & P_SYSTEM) return (NULL); return (kvm_doargv(kd, kp, nchr, ps_str_a)); } @@ -706,10 +741,10 @@ kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) } /* - * Read from user space. The user context is given by p. + * Read from user space. The user context is given by pid. */ ssize_t -kvm_uread(kvm_t *kd, const struct proc *p, u_long uva, char *buf, size_t len) +kvm_uread(kvm_t *kd, pid_t pid, u_long uva, char *buf, size_t len) { char *cp; char procfile[MAXPATHLEN]; @@ -722,7 +757,7 @@ kvm_uread(kvm_t *kd, const struct proc *p, u_long uva, char *buf, size_t len) return (0); } - sprintf(procfile, "/proc/%d/mem", p->p_pid); + sprintf(procfile, "/proc/%d/mem", pid); fd = open(procfile, O_RDONLY, 0); if (fd < 0) { _kvm_err(kd, kd->program, "cannot open %s", procfile); diff --git a/sys/conf/files b/sys/conf/files index c9c6f5d916..a6c9373d9b 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1,5 +1,5 @@ # $FreeBSD: src/sys/conf/files,v 1.340.2.137 2003/06/04 17:10:30 sam Exp $ -# $DragonFly: src/sys/conf/files,v 1.150 2007/01/17 17:31:19 y0netan1 Exp $ +# $DragonFly: src/sys/conf/files,v 1.151 2007/02/01 10:33:25 corecode Exp $ # # The long compile-with and dependency lines are required because of # limitations in config: backslash-newline doesn't work in strings, and @@ -526,6 +526,7 @@ kern/kern_exit.c standard kern/kern_firmware.c standard kern/kern_fork.c standard kern/kern_jail.c standard +kern/kern_kinfo.c standard kern/kern_kthread.c standard kern/kern_ktr.c optional ktr kern/kern_ktrace.c standard diff --git a/sys/kern/imgact_aout.c b/sys/kern/imgact_aout.c index 9b4609a973..95508f941c 100644 --- a/sys/kern/imgact_aout.c +++ b/sys/kern/imgact_aout.c @@ -24,7 +24,7 @@ * SUCH DAMAGE. * * $FreeBSD: src/sys/kern/imgact_aout.c,v 1.59.2.5 2001/11/03 01:41:08 ps Exp $ - * $DragonFly: src/sys/kern/imgact_aout.c,v 1.13 2006/12/23 23:47:54 swildner Exp $ + * $DragonFly: src/sys/kern/imgact_aout.c,v 1.14 2007/02/01 10:33:25 corecode Exp $ */ #include @@ -71,7 +71,7 @@ struct sysentvec aout_sysvec = { &szsigcode, 0, "FreeBSD a.out", - aout_coredump, + NULL, NULL, MINSIGSTKSZ }; @@ -244,35 +244,6 @@ exec_aout_imgact(struct image_params *imgp) return (0); } -/* - * Dump core, into a file named as described in the comments for - * expand_name(), unless the process was setuid/setgid. - */ -int -aout_coredump(struct proc *p, struct vnode *vp, off_t limit) -{ - struct ucred *cred = p->p_ucred; - struct vmspace *vm = p->p_vmspace; - int error; - - if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >= limit) - return (EFAULT); - bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc)); - fill_eproc(p, &p->p_addr->u_kproc.kp_eproc); - error = cpu_coredump(p->p_thread, vp, cred); - if (error == 0) - error = vn_rdwr_inchunks(UIO_WRITE, vp, vm->vm_daddr, - (int)ctob(vm->vm_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE, - IO_UNIT | IO_DIRECT | IO_CORE, cred, (int *) NULL); - if (error == 0) - error = vn_rdwr_inchunks(UIO_WRITE, vp, - (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)), - round_page(ctob(vm->vm_ssize)), - (off_t)ctob(UPAGES) + ctob(vm->vm_dsize), UIO_USERSPACE, - IO_UNIT | IO_DIRECT | IO_CORE, cred, (int *) NULL); - return (error); -} - /* * Tell kern_execve.c about it, with a little help from the linker. */ diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c index f51a8d9c8b..306ae016ee 100644 --- a/sys/kern/kern_exit.c +++ b/sys/kern/kern_exit.c @@ -37,7 +37,7 @@ * * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 * $FreeBSD: src/sys/kern/kern_exit.c,v 1.92.2.11 2003/01/13 22:51:16 dillon Exp $ - * $DragonFly: src/sys/kern/kern_exit.c,v 1.67 2007/01/01 22:51:17 corecode Exp $ + * $DragonFly: src/sys/kern/kern_exit.c,v 1.68 2007/02/01 10:33:25 corecode Exp $ */ #include "opt_compat.h" @@ -546,8 +546,8 @@ loop: * Finally finished with old proc entry. * Unlink it from its process group and free it. */ - leavepgrp(p); proc_remove_zombie(p); + leavepgrp(p); if (--p->p_procsig->ps_refcnt == 0) { if (p->p_sigacts != &p->p_addr->u_sigacts) diff --git a/sys/kern/kern_kinfo.c b/sys/kern/kern_kinfo.c new file mode 100644 index 0000000000..503c1018cf --- /dev/null +++ b/sys/kern/kern_kinfo.c @@ -0,0 +1,186 @@ +/*- + * Copyright (c) 2007 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Simon 'corecode' Schubert + * by Thomas E. Spanjaard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $DragonFly: src/sys/kern/kern_kinfo.c,v 1.1 2007/02/01 10:33:25 corecode Exp $ + */ + +/* + * This is a source file used by both the kernel and libkvm. + */ + +#ifndef _KERNEL +#define _KERNEL_STRUCTURES +#endif + +#include +#include +#include +#include +#include +#include +#include +#ifdef _KERNEL +#include +#else +#include +#endif + + +/* + * Fill in a struct kinfo_proc. + */ +void +fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) +{ + struct session *sess = p->p_pgrp->pg_session; + + bzero(kp, sizeof(*kp)); + + kp->kp_paddr = (uintptr_t)p; + kp->kp_fd = (uintptr_t)p->p_fd; + + kp->kp_flags = p->p_flag; + kp->kp_stat = p->p_stat; + kp->kp_lock = p->p_lock; + kp->kp_acflag = p->p_acflag; + kp->kp_traceflag = p->p_traceflag; + kp->kp_siglist = p->p_siglist; + kp->kp_sigignore = p->p_sigignore; + kp->kp_sigcatch = p->p_sigcatch; + kp->kp_sigflag = p->p_procsig->ps_flag; + kp->kp_start = p->p_start; + + strncpy(kp->kp_comm, p->p_comm, sizeof(kp->kp_comm) - 1); + kp->kp_comm[sizeof(kp->kp_comm) - 1] = 0; + + kp->kp_uid = p->p_ucred->cr_uid; + kp->kp_ngroups = p->p_ucred->cr_ngroups; + bcopy(p->p_ucred->cr_groups, kp->kp_groups, + NGROUPS * sizeof(kp->kp_groups[0])); + kp->kp_ruid = p->p_ucred->cr_ruid; + kp->kp_svuid = p->p_ucred->cr_svuid; + kp->kp_rgid = p->p_ucred->cr_rgid; + kp->kp_svgid = p->p_ucred->cr_svgid; + + kp->kp_pid = p->p_pid; + if (p->p_oppid != 0) + kp->kp_ppid = p->p_oppid; + else + kp->kp_ppid = p->p_pptr != NULL ? p->p_pptr->p_pid : NO_PID; + kp->kp_pgid = p->p_pgrp->pg_id; + kp->kp_jobc = p->p_pgrp->pg_jobc; + kp->kp_sid = sess->s_sid; + bcopy(sess->s_login, kp->kp_login, MAXLOGNAME); + if (sess->s_ttyvp != NULL) + kp->kp_auxflags |= KI_CTTY; + if (SESS_LEADER(p)) + kp->kp_auxflags |= KI_SLEADER; + if ((p->p_flag & P_CONTROLT) != 0 && sess->s_ttyp != NULL) { + kp->kp_tdev = sess->s_ttyp->t_dev != NOCDEV ? + sess->s_ttyp->t_dev->si_udev : + NOUDEV; + if (sess->s_ttyp->t_pgrp != NULL) + kp->kp_tpgid = sess->s_ttyp->t_pgrp->pg_id; + else + kp->kp_tpgid = NO_PID; + kp->kp_tsid = sess->s_ttyp->t_session->s_sid; + } else { + kp->kp_tdev = NOUDEV; + } + kp->kp_exitstat = p->p_xstat; + kp->kp_nthreads = p->p_nthreads; + kp->kp_nice = p->p_nice; + + kp->kp_vm_map_size = p->p_vmspace->vm_map.size; + kp->kp_vm_rssize = p->p_vmspace->vm_rssize; + kp->kp_vm_swrss = p->p_vmspace->vm_swrss; + kp->kp_vm_tsize = p->p_vmspace->vm_tsize; + kp->kp_vm_dsize = p->p_vmspace->vm_dsize; + kp->kp_vm_ssize = p->p_vmspace->vm_ssize; + + if (jailed(p->p_ucred)) + kp->kp_jailid = p->p_ucred->cr_prison->pr_id; + + kp->kp_ru = p->p_ru; + kp->kp_ru = p->p_cru; +} + +/* + * Fill in a struct kinfo_lwp. + */ +void +fill_kinfo_lwp(struct lwp *lwp, struct kinfo_lwp *kl) +{ + bzero(kl, sizeof(*kl)); + + kl->kl_pid = lwp->lwp_proc->p_pid; + kl->kl_tid = lwp->lwp_tid; + +#ifdef notyet + kl->kl_flags = lwp->lwp_flag; + kl->kl_stat = lwp->lwp_stat; +#endif + kl->kl_tdflags = lwp->lwp_thread->td_flags; +#ifdef SMP + kl->kl_mpcount = lwp->lwp_thread->td_mpcount; +#else + kl->kl_mpcount = 0; +#endif + + kl->kl_prio = lwp->lwp_usdata.bsd4.priority; /* XXX TGEN dangerous assumption */ + kl->kl_tdprio = lwp->lwp_thread->td_pri; + kl->kl_rtprio = lwp->lwp_rtprio; + + kl->kl_uticks = lwp->lwp_thread->td_uticks; + kl->kl_sticks = lwp->lwp_thread->td_sticks; + kl->kl_iticks = lwp->lwp_thread->td_iticks; + kl->kl_cpticks = lwp->lwp_cpticks; + kl->kl_pctcpu = lwp->lwp_pctcpu; + kl->kl_swtime = lwp->lwp_swtime; + kl->kl_slptime = lwp->lwp_slptime; + kl->kl_origcpu = lwp->lwp_usdata.bsd4.origcpu; /* XXX TGEN same */ + kl->kl_estcpu = lwp->lwp_usdata.bsd4.estcpu; + kl->kl_cpuid = lwp->lwp_thread->td_gd->gd_cpuid; + + kl->kl_ru = lwp->lwp_ru; + + kl->kl_siglist = lwp->lwp_siglist; + kl->kl_sigmask = lwp->lwp_sigmask; + + kl->kl_wchan = (uintptr_t)lwp->lwp_thread->td_wchan; + if (lwp->lwp_thread->td_wmesg) { + strncpy(kl->kl_wmesg, lwp->lwp_thread->td_wmesg, WMESGLEN); + kl->kl_wmesg[WMESGLEN] = 0; + } +} diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c index edeebb7fca..437664ecb1 100644 --- a/sys/kern/kern_proc.c +++ b/sys/kern/kern_proc.c @@ -32,7 +32,7 @@ * * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95 * $FreeBSD: src/sys/kern/kern_proc.c,v 1.63.2.9 2003/05/08 07:47:16 kbyanc Exp $ - * $DragonFly: src/sys/kern/kern_proc.c,v 1.31 2007/01/01 22:51:17 corecode Exp $ + * $DragonFly: src/sys/kern/kern_proc.c,v 1.32 2007/02/01 10:33:25 corecode Exp $ */ #include @@ -596,6 +596,7 @@ DB_SHOW_COMMAND(pgrpdump, pgrpdump) } #endif /* DDB */ +#if 0 /* * Fill in an eproc structure for the specified thread. */ @@ -678,6 +679,7 @@ fill_eproc(struct proc *p, struct eproc *ep) if (p->p_ucred->cr_prison) ep->e_jailid = p->p_ucred->cr_prison->pr_id; } +#endif /* * Locate a process on the zombie list. Return a held process or NULL. @@ -694,52 +696,29 @@ zpfind(pid_t pid) } static int -sysctl_out_proc(struct proc *p, struct thread *td, struct sysctl_req *req, int doingzomb) +sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) { - struct eproc eproc; - struct proc xproc; + struct kinfo_proc ki; + struct lwp *lp; + int skp = 1, had_output = 0; int error; -#if 0 - pid_t pid = p->p_pid; -#endif - - if (p) { - td = p->p_thread; - fill_eproc(p, &eproc); - xproc = *p; - /* - * Aggregate rusage information - */ - calcru_proc(p, &xproc.p_ru); - - /* - * p_stat fixup. If we are in a thread sleep mark p_stat - * as sleeping if the thread is blocked. - */ - if (p->p_stat == SRUN && td && (td->td_flags & TDF_BLOCKED)) { - xproc.p_stat = SSLEEP; - } - /* - * If the process is being stopped but is in a normal tsleep, - * mark it as being SSTOP. - */ - if (p->p_stat == SSLEEP && (p->p_flag & P_STOPPED)) - xproc.p_stat = SSTOP; - if (p->p_flag & P_ZOMBIE) - xproc.p_stat = SZOMB; - } else if (td) { - fill_eproc_td(td, &eproc, &xproc); + fill_kinfo_proc(p, &ki); + if ((flags & KERN_PROC_FLAG_LWP) == 0) + skp = 1; + FOREACH_LWP_IN_PROC(lp, p) { + fill_kinfo_lwp(lp, &ki.kp_lwp); +output: + had_output = 1; + error = SYSCTL_OUT(req, &ki, sizeof(ki)); + if (error) + return error; + if (skp) + break; } - error = SYSCTL_OUT(req,(caddr_t)&xproc, sizeof(struct proc)); - if (error) - return (error); - error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc)); - if (error) - return (error); - error = SYSCTL_OUT(req,(caddr_t)td, sizeof(struct thread)); - if (error) - return (error); + /* We need to output at least the proc, even if there is no lwp. */ + if (!had_output) + goto output; #if 0 if (!doingzomb && pid && (pfind(pid) != p)) return EAGAIN; @@ -753,45 +732,44 @@ static int sysctl_kern_proc(SYSCTL_HANDLER_ARGS) { int *name = (int*) arg1; + int oid = oidp->oid_number; u_int namelen = arg2; - struct proc *p; - struct thread *td; - int doingzomb; + struct proc *p, *np; + struct proclist *plist; + int doingzomb, flags = 0; int error = 0; - int n; int origcpu; struct ucred *cr1 = curproc->p_ucred; - if (oidp->oid_number == KERN_PROC_PID) { - if (namelen != 1) - return (EINVAL); + flags = oid & KERN_PROC_FLAGMASK; + oid &= ~KERN_PROC_FLAGMASK; + + if ((oid == KERN_PROC_ALL && namelen != 0) || + (oid != KERN_PROC_ALL && namelen != 1)) + return (EINVAL); + + if (oid == KERN_PROC_PID) { p = pfind((pid_t)name[0]); if (!p) return (0); if (!PRISON_CHECK(cr1, p->p_ucred)) return (0); - error = sysctl_out_proc(p, NULL, req, 0); + error = sysctl_out_proc(p, req, flags); return (error); } - if (oidp->oid_number == KERN_PROC_ALL && !namelen) - ; - else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1) - ; - else - return (EINVAL); - + if (!req->oldptr) { /* overestimate by 5 procs */ error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); if (error) return (error); } - for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { - if (!doingzomb) - p = LIST_FIRST(&allproc); + for (doingzomb = 0; doingzomb <= 1; doingzomb++) { + if (doingzomb) + plist = &zombproc; else - p = LIST_FIRST(&zombproc); - for (; p != 0; p = LIST_NEXT(p, p_list)) { + plist = &allproc; + LIST_FOREACH_MUTABLE(p, plist, p_list, np) { /* * Show a user only their processes. */ @@ -806,7 +784,7 @@ sysctl_kern_proc(SYSCTL_HANDLER_ARGS) * TODO - make more efficient (see notes below). * do by session. */ - switch (oidp->oid_number) { + switch (oid) { case KERN_PROC_PGRP: /* could do this by traversing pgrp */ if (p->p_pgrp == NULL || @@ -839,7 +817,7 @@ sysctl_kern_proc(SYSCTL_HANDLER_ARGS) if (!PRISON_CHECK(cr1, p->p_ucred)) continue; PHOLD(p); - error = sysctl_out_proc(p, NULL, req, doingzomb); + error = sysctl_out_proc(p, req, flags); PRELE(p); if (error) return (error); @@ -856,6 +834,7 @@ sysctl_kern_proc(SYSCTL_HANDLER_ARGS) origcpu = mycpu->gd_cpuid; if (!ps_showallthreads || jailed(cr1)) goto post_threads; +#if 0 for (n = 1; n <= ncpus; ++n) { globaldata_t rgd; int nid; @@ -885,6 +864,7 @@ sysctl_kern_proc(SYSCTL_HANDLER_ARGS) return (error); } } +#endif post_threads: return (0); } @@ -962,5 +942,23 @@ SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, sysctl_kern_proc, "Process table"); +SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD, + sysctl_kern_proc, "Process table"); + +SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD, + sysctl_kern_proc, "Process table"); + +SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD, + sysctl_kern_proc, "Process table"); + +SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD, + sysctl_kern_proc, "Process table"); + +SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD, + sysctl_kern_proc, "Process table"); + +SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD, + sysctl_kern_proc, "Process table"); + SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY, sysctl_kern_proc_args, "Process argument list"); diff --git a/sys/sys/jail.h b/sys/sys/jail.h index 67718cc8af..6c3c240459 100644 --- a/sys/sys/jail.h +++ b/sys/sys/jail.h @@ -7,7 +7,7 @@ * ---------------------------------------------------------------------------- * * $FreeBSD: src/sys/sys/jail.h,v 1.8.2.2 2000/11/01 17:58:06 rwatson Exp $ - * $DragonFly: src/sys/sys/jail.h,v 1.10 2007/01/18 12:34:46 victor Exp $ + * $DragonFly: src/sys/sys/jail.h,v 1.11 2007/02/01 10:33:26 corecode Exp $ * */ @@ -50,7 +50,9 @@ struct jail_v0 { int jail(struct jail *); int jail_attach(int); -#else /* _KERNEL */ +#endif + +#ifdef _KERNEL #ifndef _SYS_NAMECACHE_H_ #include @@ -63,6 +65,10 @@ int jail_attach(int); MALLOC_DECLARE(M_PRISON); #endif +#endif /* _KERNEL */ + +#if defined(_KERNEL) || defined(_KERNEL_STRUCTURES) + #define JAIL_MAX 999999 /* Used to store the IPs of the jail */ @@ -119,5 +125,5 @@ jailed(struct ucred *cred) return(cred->cr_prison != NULL); } -#endif /* !_KERNEL */ +#endif /* _KERNEL || _KERNEL_STRUCTURES */ #endif /* !_SYS_JAIL_H_ */ diff --git a/sys/sys/kinfo.h b/sys/sys/kinfo.h index 0638c8e34a..a9cc3b9569 100644 --- a/sys/sys/kinfo.h +++ b/sys/sys/kinfo.h @@ -31,7 +31,7 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/sys/sys/kinfo.h,v 1.8 2006/12/29 18:02:56 victor Exp $ + * $DragonFly: src/sys/sys/kinfo.h,v 1.9 2007/02/01 10:33:26 corecode Exp $ */ #ifndef _SYS_KINFO_H_ @@ -43,6 +43,8 @@ #ifndef _SYS_PARAM_H_ #include #endif +#include +#include struct kinfo_file { size_t f_size; /* size of struct kinfo_file */ @@ -97,6 +99,118 @@ struct kinfo_clockinfo { int ci_profhz; /* profiling clock frequency */ }; +/* + * Structure definition for the KERN_LWP subtype op. + */ +struct kinfo_lwp { + pid_t kl_pid; /* PID of our associated proc */ + lwpid_t kl_tid; /* thread id */ + + int kl_flags; /* LWP_ flags */ + char kl_stat; /* S* lwp status */ + int kl_tdflags; /* thread flags */ + int kl_mpcount; /* MP lock held count */ + int kl_prio; /* scheduling priority */ + int kl_tdprio; /* lwkt sched priority */ + struct rtprio kl_rtprio; /* real-time scheduling prio */ + + /* accounting */ + uint64_t kl_uticks; /* time accounting */ + uint64_t kl_sticks; + uint64_t kl_iticks; + uint64_t kl_cpticks; /* sched quantums used */ + u_int kl_pctcpu; /* percentage cputime */ + u_int kl_swtime; /* time swapped in or out */ + u_int kl_slptime; /* time since last blocked */ + int kl_origcpu; /* originally scheduled on cpu */ + int kl_estcpu; + int kl_cpuid; /* CPU this lwp was last scheduled on */ + + struct rusage kl_ru; /* resource usage stats */ + + sigset_t kl_siglist; /* pending signals */ + sigset_t kl_sigmask; /* masked signals */ +#define WMESGLEN 7 + uintptr_t kl_wchan; /* waiting channel */ + char kl_wmesg[WMESGLEN+1]; /* waiting message */ +}; + +/* + * KERN_PROC subtype ops return arrays of normalized proc structures: + */ +struct kinfo_proc { + uintptr_t kp_paddr; /* address of this proc */ + + /* proc information */ + int kp_flags; + int kp_stat; + int kp_lock; + int kp_acflag; /* accounting flags */ + int kp_traceflag; + + uintptr_t kp_fd; /* address of the proc's files */ + + sigset_t kp_siglist; + sigset_t kp_sigignore; + sigset_t kp_sigcatch; + int kp_sigflag; /* from ps_flag */ + struct timeval kp_start; + + char kp_comm[MAXCOMLEN+1]; + + /* cred information */ + uid_t kp_uid; + short kp_ngroups; + gid_t kp_groups[NGROUPS]; + uid_t kp_ruid; + uid_t kp_svuid; + gid_t kp_rgid; + gid_t kp_svgid; + + pid_t kp_pid; /* process id */ + pid_t kp_ppid; /* parent process id */ + pid_t kp_pgid; /* process group id */ + int kp_jobc; /* job control counter */ + pid_t kp_sid; /* session id */ + char kp_login[roundup(MAXLOGNAME, sizeof(long))]; /* setlogin() name */ + dev_t kp_tdev; /* controlling tty dev */ + pid_t kp_tpgid; /* tty process group id */ + pid_t kp_tsid; /* tty session id */ + + u_short kp_exitstat; /* exit status information */ + int kp_nthreads; + int kp_nice; + + vm_size_t kp_vm_map_size; /* vmmap virtual size */ + segsz_t kp_vm_rssize; /* resident set size */ + segsz_t kp_vm_swrss; /* rss before last swap */ + segsz_t kp_vm_tsize; /* text size */ + segsz_t kp_vm_dsize; /* data size */ + segsz_t kp_vm_ssize; /* stack size */ + + int kp_jailid; + + struct rusage kp_ru; + struct rusage kp_cru; + + int kp_auxflags; /* generated flags */ +#define KI_CTTY 1 +#define KI_SLEADER 2 + + struct kinfo_lwp kp_lwp; + + int kp_spare[4]; +}; + +struct proc; +struct lwp; + +void fill_kinfo_proc(struct proc *, struct kinfo_proc *); +void fill_kinfo_lwp(struct lwp *, struct kinfo_lwp *); + +#define KINFO_NEXT(kp) ((union kinfo *)((uintptr_t)kp + kp->gen.len)) +#define KINFO_END(kp) (kp->gen.type == KINFO_TYPE_END) + #if defined(_KERNEL) #ifdef SMP #define cpu_time cputime_percpu[mycpuid] diff --git a/sys/sys/proc.h b/sys/sys/proc.h index 76cdfcfeec..fea4d7a160 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -37,7 +37,7 @@ * * @(#)proc.h 8.15 (Berkeley) 5/19/95 * $FreeBSD: src/sys/sys/proc.h,v 1.99.2.9 2003/06/06 20:21:32 tegge Exp $ - * $DragonFly: src/sys/sys/proc.h,v 1.90 2007/01/14 07:59:08 dillon Exp $ + * $DragonFly: src/sys/sys/proc.h,v 1.91 2007/02/01 10:33:26 corecode Exp $ */ #ifndef _SYS_PROC_H_ @@ -302,12 +302,10 @@ struct proc { struct spinlock p_spin; /* Spinlock for LWP access to proc */ }; -#if defined(_KERNEL) #define p_wchan p_thread->td_wchan #define p_wmesg p_thread->td_wmesg #define p_session p_pgrp->pg_session #define p_pgid p_pgrp->pg_id -#endif /* * Status values. Please note that p_stat doesn't actually take on @@ -369,6 +367,15 @@ struct proc { __func__, p, p->p_pid, p->p_comm), 1), \ FIRST_LWP_IN_PROC(p)) +/* + * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t, + * as it is used to represent "no process group". + */ +#define PID_MAX 99999 +#define NO_PID 100000 + +#define SESS_LEADER(p) ((p)->p_session->s_leader == (p)) + #ifdef _KERNEL #ifdef MALLOC_DECLARE @@ -386,15 +393,6 @@ MALLOC_DECLARE(M_PARGS); #define PRISON_CHECK(cr1, cr2) \ ((!(cr1)->cr_prison) || (cr1)->cr_prison == (cr2)->cr_prison) -/* - * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t, - * as it is used to represent "no process group". - */ -#define PID_MAX 99999 -#define NO_PID 100000 - -#define SESS_LEADER(p) ((p)->p_session->s_leader == (p)) - /* * STOPEVENT */ diff --git a/sys/sys/sysctl.h b/sys/sys/sysctl.h index 76764e6baa..8776b94c83 100644 --- a/sys/sys/sysctl.h +++ b/sys/sys/sysctl.h @@ -35,7 +35,7 @@ * * @(#)sysctl.h 8.1 (Berkeley) 6/2/93 * $FreeBSD: src/sys/sys/sysctl.h,v 1.81.2.10 2003/05/01 22:48:09 trhodes Exp $ - * $DragonFly: src/sys/sys/sysctl.h,v 1.21 2007/01/19 07:23:43 dillon Exp $ + * $DragonFly: src/sys/sys/sysctl.h,v 1.22 2007/02/01 10:33:26 corecode Exp $ */ #ifndef _SYS_SYSCTL_H_ @@ -431,6 +431,9 @@ TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry); #define KERN_PROC_RUID 6 /* by real uid */ #define KERN_PROC_ARGS 7 /* get/set arguments/proctitle */ +#define KERN_PROC_FLAGMASK 0x10 +#define KERN_PROC_FLAG_LWP 0x10 + /* * KERN_IPC identifiers */ diff --git a/sys/sys/user.h b/sys/sys/user.h index 697a292851..e0a1fec0c1 100644 --- a/sys/sys/user.h +++ b/sys/sys/user.h @@ -32,7 +32,7 @@ * * @(#)user.h 8.2 (Berkeley) 9/23/93 * $FreeBSD: src/sys/sys/user.h,v 1.24.2.1 2001/10/11 08:20:18 peter Exp $ - * $DragonFly: src/sys/sys/user.h,v 1.16 2007/01/01 22:51:18 corecode Exp $ + * $DragonFly: src/sys/sys/user.h,v 1.17 2007/02/01 10:33:26 corecode Exp $ */ #ifndef _SYS_USER_H_ @@ -99,53 +99,7 @@ #ifndef _MACHINE_COREDUMP_H_ #include #endif - -/* - * KERN_PROC subtype ops return arrays of augmented proc structures: - */ -struct kinfo_proc { - struct proc kp_proc; /* proc structure */ - struct eproc { - struct proc *e_paddr; /* address of proc */ - struct session *e_sess; /* session pointer */ - struct ucred e_ucred; /* current credentials */ - struct procsig e_procsig; /* shared signal structure */ - struct vmspace e_vm; /* address space */ -#ifdef old - struct pstats e_stats; /* process stats */ -#endif - u_int64_t e_uticks; /* time accouting */ - u_int64_t e_sticks; - u_int64_t e_iticks; - int e_cpuid; /* last scheduled on cpu */ - pid_t e_ppid; /* parent process id */ - pid_t e_pgid; /* process group id */ - short e_jobc; /* job control counter */ -#ifdef _KERNEL - udev_t e_tdev; /* controlling tty dev */ -#else - dev_t e_tdev; /* controlling tty dev */ -#endif - pid_t e_tpgid; /* tty process group id */ - struct session *e_tsess; /* tty session pointer */ -#define WMESGLEN 7 - char e_wmesg[WMESGLEN+1]; /* wchan message */ - segsz_t e_xsize; /* text size */ - short e_xrssize; /* text rss */ - short e_xccount; /* text references */ - short e_xswrss; - long e_flag; -#define EPROC_CTTY 0x01 /* controlling tty vnode active */ -#define EPROC_SLEADER 0x02 /* session leader */ - char e_login[roundup(MAXLOGNAME, sizeof(long))]; /* setlogin() name */ - int e_jailid; - long e_spare[1]; - } kp_eproc; - struct thread kp_thread; /* thread structure */ -}; -void fill_eproc_td (struct thread *, struct eproc *, struct proc *); -void fill_eproc (struct proc *, struct eproc *); - +#include /* * Per process structure containing data that isn't needed in core diff --git a/usr.bin/fstat/fstat.c b/usr.bin/fstat/fstat.c index bfe8fd0032..1a841d561c 100644 --- a/usr.bin/fstat/fstat.c +++ b/usr.bin/fstat/fstat.c @@ -33,7 +33,7 @@ * @(#) Copyright (c) 1988, 1993 The Regents of the University of California. All rights reserved. * @(#)fstat.c 8.3 (Berkeley) 5/2/95 * $FreeBSD: src/usr.bin/fstat/fstat.c,v 1.21.2.7 2001/11/21 10:49:37 dwmalone Exp $ - * $DragonFly: src/usr.bin/fstat/fstat.c,v 1.21 2006/10/27 05:04:35 dillon Exp $ + * $DragonFly: src/usr.bin/fstat/fstat.c,v 1.22 2007/02/01 10:33:26 corecode Exp $ */ #define _KERNEL_STRUCTURES @@ -136,8 +136,8 @@ int maxfiles; kvm_t *kd; -void dofiles(struct kinfo_proc *kp); -void dommap(struct kinfo_proc *kp); +void dofiles(struct kinfo_proc *kp, struct proc *p); +void dommap(struct proc *p); void vtrans(struct vnode *vp, struct nchandle *ncr, int i, int flag); int ufs_filestat(struct vnode *vp, struct filestat *fsp); int nfs_filestat(struct vnode *vp, struct filestat *fsp); @@ -154,6 +154,7 @@ main(int argc, char **argv) { struct passwd *passwd; struct kinfo_proc *p, *plast; + struct proc proc; int arg, ch, what; char *memf, *nlistf; char buf[_POSIX2_LINE_MAX]; @@ -259,11 +260,16 @@ main(int argc, char **argv) putchar('\n'); for (plast = &p[cnt]; p < plast; ++p) { - if (p->kp_proc.p_stat == SZOMB) + if (p->kp_stat == SZOMB) continue; - dofiles(p); + if (!kread((void *)p->kp_paddr, &proc, sizeof(proc))) { + dprintf(stderr, "can't read proc at %p for pid %d\n", + (void *)p->kp_paddr, Pid); + continue; + } + dofiles(p, &proc); if (mflg) - dommap(p); + dommap(&proc); } exit(0); } @@ -298,18 +304,16 @@ int Pid; * print open files attributed to this process */ void -dofiles(struct kinfo_proc *kp) +dofiles(struct kinfo_proc *kp, struct proc *p) { int i; struct file file; struct filedesc filed; struct ktrace_node ktrace_node; - struct proc *p = &kp->kp_proc; - struct eproc *ep = &kp->kp_eproc; - Uname = user_from_uid(ep->e_ucred.cr_uid, 0); - Pid = p->p_pid; - Comm = kp->kp_thread.td_comm; + Uname = user_from_uid(kp->kp_uid, 0); + Pid = kp->kp_pid; + Comm = kp->kp_comm; make_printable(Comm, strlen(Comm)); if (p->p_fd == NULL) @@ -389,9 +393,8 @@ dofiles(struct kinfo_proc *kp) } void -dommap(struct kinfo_proc *kp) +dommap(struct proc *p) { - struct proc *p = &kp->kp_proc; struct vmspace vmspace; vm_map_t map; struct vm_map_entry entry; diff --git a/usr.bin/gcore/gcore.c b/usr.bin/gcore/gcore.c index 3684054405..ebf41116f0 100644 --- a/usr.bin/gcore/gcore.c +++ b/usr.bin/gcore/gcore.c @@ -33,7 +33,7 @@ * @(#) Copyright (c) 1992, 1993 The Regents of the University of California. All rights reserved. * @(#)gcore.c 8.2 (Berkeley) 9/23/93 * $FreeBSD: src/usr.bin/gcore/gcore.c,v 1.15.2.2 2001/08/17 20:56:22 mikeh Exp $ - * $DragonFly: src/usr.bin/gcore/gcore.c,v 1.7 2005/04/10 20:55:38 drhodus Exp $ + * $DragonFly: src/usr.bin/gcore/gcore.c,v 1.8 2007/02/01 10:33:26 corecode Exp $ */ /* @@ -70,15 +70,13 @@ #include "extern.h" static void core(int, int, struct kinfo_proc *); -static void datadump(int, int, struct proc *, u_long, int); +static void datadump(int, int, pid_t, u_long, int); static void killed(int); static void restart_target(void); static void usage(void) __dead2; -static void userdump(int, struct proc *, u_long, int); +static void userdump(int, pid_t, u_long, int); kvm_t *kd; -/* XXX undocumented routine, should be in kvm.h? */ -ssize_t kvm_uread(kvm_t *, const struct proc *, u_long, char *, size_t); static int data_offset; static pid_t pid; @@ -86,7 +84,6 @@ static pid_t pid; int main(int argc, char **argv) { - struct proc *p; struct kinfo_proc *ki = NULL; struct exec exec; int ch, cnt, efd, fd, sflag, uid; @@ -152,23 +149,22 @@ main(int argc, char **argv) if (ki == NULL || cnt != 1) errx(1, "%d: not found", pid); - p = &ki->kp_proc; - if (ki->kp_eproc.e_ucred.cr_ruid != uid && uid != 0) + if (ki->kp_ruid != uid && uid != 0) errx(1, "%d: not owner", pid); - if (p->p_stat == SZOMB) + if (ki->kp_stat == SZOMB) errx(1, "%d: zombie", pid); - if (p->p_flag & P_WEXIT) + if (ki->kp_flags & P_WEXIT) errx(1, "%d: process exiting", pid); - if (p->p_flag & P_SYSTEM) /* Swapper or pagedaemon. */ + if (ki->kp_flags & P_SYSTEM) /* Swapper or pagedaemon. */ errx(1, "%d: system process", pid); - if (exec.a_text != ptoa(ki->kp_eproc.e_vm.vm_tsize)) + if (exec.a_text != ptoa(ki->kp_vm_tsize)) errx(1, "The executable %s does not belong to" " process %d!\n" "Text segment size (in bytes): executable %ld," " process %d", binfile, pid, exec.a_text, - ptoa(ki->kp_eproc.e_vm.vm_tsize)); + ptoa(ki->kp_vm_tsize)); data_offset = N_DATOFF(exec); } else if (IS_ELF(*(Elf_Ehdr *)&exec)) { is_aout = 0; @@ -213,14 +209,13 @@ core(int efd, int fd, struct kinfo_proc *ki) struct user user; char ubytes[ctob(UPAGES)]; } uarea; - struct proc *p = &ki->kp_proc; - int tsize = ki->kp_eproc.e_vm.vm_tsize; - int dsize = ki->kp_eproc.e_vm.vm_dsize; - int ssize = ki->kp_eproc.e_vm.vm_ssize; + int tsize = ki->kp_vm_tsize; + int dsize = ki->kp_vm_dsize; + int ssize = ki->kp_vm_ssize; int cnt; /* Read in user struct */ - cnt = kvm_read(kd, (u_long)p->p_addr, &uarea, sizeof(uarea)); + cnt = kvm_read(kd, ki->kp_pid, &uarea, sizeof(uarea)); if (cnt != sizeof(uarea)) errx(1, "read user structure: %s", cnt > 0 ? strerror(EIO) : strerror(errno)); @@ -238,17 +233,17 @@ core(int efd, int fd, struct kinfo_proc *ki) cnt > 0 ? strerror(EIO) : strerror(errno)); /* Dump data segment */ - datadump(efd, fd, p, USRTEXT + ctob(tsize), dsize); + datadump(efd, fd, ki->kp_pid, USRTEXT + ctob(tsize), dsize); /* Dump stack segment */ - userdump(fd, p, USRSTACK - ctob(ssize), ssize); + userdump(fd, ki->kp_pid, USRSTACK - ctob(ssize), ssize); /* Dump machine dependent portions of the core. */ md_core(kd, fd, ki); } void -datadump(int efd, int fd, struct proc *p, +datadump(int efd, int fd, pid_t pid, u_long addr, int npage) { int cc, delta; @@ -256,7 +251,7 @@ datadump(int efd, int fd, struct proc *p, delta = data_offset - addr; while (--npage >= 0) { - cc = kvm_uread(kd, p, addr, buffer, PAGE_SIZE); + cc = kvm_uread(kd, pid, addr, buffer, PAGE_SIZE); if (cc != PAGE_SIZE) { /* Try to read the page from the executable. */ if (lseek(efd, (off_t)addr + delta, SEEK_SET) == -1) @@ -292,14 +287,14 @@ restart_target(void) } void -userdump(int fd, struct proc *p, u_long addr, +userdump(int fd, pid_t pid, u_long addr, int npage) { int cc; char buffer[PAGE_SIZE]; while (--npage >= 0) { - cc = kvm_uread(kd, p, addr, buffer, PAGE_SIZE); + cc = kvm_uread(kd, pid, addr, buffer, PAGE_SIZE); if (cc != PAGE_SIZE) /* Could be an untouched fill-with-zero page. */ bzero(buffer, PAGE_SIZE); diff --git a/usr.bin/killall/killall.c b/usr.bin/killall/killall.c index b628bdd4ab..268c65da3c 100644 --- a/usr.bin/killall/killall.c +++ b/usr.bin/killall/killall.c @@ -25,7 +25,7 @@ * SUCH DAMAGE. * * $FreeBSD: src/usr.bin/killall/killall.c,v 1.5.2.4 2001/05/19 19:22:49 phk Exp $ - * $DragonFly: src/usr.bin/killall/killall.c,v 1.8 2006/03/27 09:02:07 joerg Exp $ + * $DragonFly: src/usr.bin/killall/killall.c,v 1.9 2007/02/01 10:33:26 corecode Exp $ */ #include @@ -304,16 +304,16 @@ main(int ac, char **av) mypid = getpid(); for (i = 0; i < nprocs; i++) { - thispid = procs[i].kp_proc.p_pid; - strncpy(thiscmd, procs[i].kp_thread.td_comm, MAXCOMLEN); + thispid = procs[i].kp_pid; + strncpy(thiscmd, procs[i].kp_comm, MAXCOMLEN); thiscmd[MAXCOMLEN] = '\0'; - thistdev = procs[i].kp_eproc.e_tdev; - thisuid = procs[i].kp_eproc.e_ucred.cr_ruid; /* real uid */ + thistdev = procs[i].kp_tdev; + thisuid = procs[i].kp_ruid; /* real uid */ if (thispid == mypid) continue; matched = 1; - if ((int)procs[i].kp_proc.p_pid < 0) + if ((int)procs[i].kp_pid < 0) matched = 0; if (user) { if (thisuid != uid) @@ -324,7 +324,7 @@ main(int ac, char **av) matched = 0; } if (jflag) { - if (procs[i].kp_eproc.e_jailid != jailid) + if (procs[i].kp_jailid != jailid) matched = 0; } if (cmd) { diff --git a/usr.bin/pkill/pkill.c b/usr.bin/pkill/pkill.c index 43064c9635..d567202591 100644 --- a/usr.bin/pkill/pkill.c +++ b/usr.bin/pkill/pkill.c @@ -1,5 +1,5 @@ /* $NetBSD: pkill.c,v 1.7 2004/02/15 17:03:30 soren Exp $ */ -/* $DragonFly: src/usr.bin/pkill/pkill.c,v 1.8 2005/08/02 16:27:48 cpressey Exp $ */ +/* $DragonFly: src/usr.bin/pkill/pkill.c,v 1.9 2007/02/01 10:33:26 corecode Exp $ */ /*- * Copyright (c) 2002 The NetBSD Foundation, Inc. @@ -263,7 +263,7 @@ main(int argc, char **argv) } for (i = 0, kp = plist; i < nproc; i++, kp++) { - if ((kp->kp_proc.p_flag & P_SYSTEM) != 0 || kp->kp_proc.p_pid == mypid) + if ((kp->kp_flags & P_SYSTEM) != 0 || kp->kp_pid == mypid) continue; if (matchargs) { @@ -280,7 +280,7 @@ main(int argc, char **argv) mstr = buf; } else - mstr = kp->kp_thread.td_comm; + mstr = kp->kp_comm; rv = regexec(®, mstr, 1, ®match, 0); if (rv == 0) { @@ -304,11 +304,11 @@ main(int argc, char **argv) * if it fails to meet the established criteria. */ for (i = 0, kp = plist; i < nproc; i++, kp++) { - if ((kp->kp_proc.p_flag & P_SYSTEM) != 0) + if ((kp->kp_flags & P_SYSTEM) != 0) continue; SLIST_FOREACH(li, &ruidlist, li_chain) { - if (kp->kp_eproc.e_ucred.cr_ruid == li->li_datum.ld_uid) + if (kp->kp_ruid == li->li_datum.ld_uid) break; } if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) { @@ -317,7 +317,7 @@ main(int argc, char **argv) } SLIST_FOREACH(li, &rgidlist, li_chain) { - if (kp->kp_eproc.e_ucred.cr_rgid == li->li_datum.ld_gid) + if (kp->kp_rgid == li->li_datum.ld_gid) break; } if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) { @@ -326,7 +326,7 @@ main(int argc, char **argv) } SLIST_FOREACH(li, &euidlist, li_chain) { - if (kp->kp_eproc.e_ucred.cr_uid == li->li_datum.ld_uid) + if (kp->kp_uid == li->li_datum.ld_uid) break; } if (SLIST_FIRST(&euidlist) != NULL && li == NULL) { @@ -335,7 +335,7 @@ main(int argc, char **argv) } SLIST_FOREACH(li, &ppidlist, li_chain) { - if (kp->kp_eproc.e_ppid == li->li_datum.ld_pid) + if (kp->kp_ppid == li->li_datum.ld_pid) break; } if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) { @@ -344,7 +344,7 @@ main(int argc, char **argv) } SLIST_FOREACH(li, &pgrplist, li_chain) { - if (kp->kp_eproc.e_pgid == li->li_datum.ld_pid) + if (kp->kp_pgid == li->li_datum.ld_pid) break; } if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) { @@ -354,9 +354,9 @@ main(int argc, char **argv) SLIST_FOREACH(li, &tdevlist, li_chain) { if (li->li_datum.ld_dev == NODEV && - (kp->kp_proc.p_flag & P_CONTROLT) == 0) + (kp->kp_flags & P_CONTROLT) == 0) break; - if (kp->kp_eproc.e_tdev == li->li_datum.ld_dev) + if (kp->kp_tdev == li->li_datum.ld_dev) break; } if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) { @@ -365,7 +365,7 @@ main(int argc, char **argv) } SLIST_FOREACH(li, &sidlist, li_chain) { - if (kp->kp_eproc.e_sess->s_sid == li->li_datum.ld_pid) + if (kp->kp_sid == li->li_datum.ld_pid) break; } if (SLIST_FIRST(&sidlist) != NULL && li == NULL) { @@ -386,11 +386,11 @@ main(int argc, char **argv) if (!selected[i]) continue; - if (kp->kp_thread.td_start.tv_sec > best.tv_sec || - (kp->kp_thread.td_start.tv_sec == best.tv_sec - && kp->kp_thread.td_start.tv_usec > best.tv_usec)) { - best.tv_sec = kp->kp_thread.td_start.tv_sec; - best.tv_usec = kp->kp_thread.td_start.tv_usec; + if (kp->kp_start.tv_sec > best.tv_sec || + (kp->kp_start.tv_sec == best.tv_sec + && kp->kp_start.tv_usec > best.tv_usec)) { + best.tv_sec = kp->kp_start.tv_sec; + best.tv_usec = kp->kp_start.tv_usec; bestidx = i; } } @@ -404,7 +404,7 @@ main(int argc, char **argv) * Take the appropriate action for each matched process, if any. */ for (i = 0, j = 0, rv = 0, kp = plist; i < nproc; i++, kp++) { - if (kp->kp_proc.p_pid == mypid) + if (kp->kp_pid == mypid) continue; if (selected[i]) { if (inverse) @@ -412,7 +412,7 @@ main(int argc, char **argv) } else if (!inverse) continue; - if ((kp->kp_proc.p_flag & P_SYSTEM) != 0) + if ((kp->kp_flags & P_SYSTEM) != 0) continue; rv = 1; @@ -449,8 +449,8 @@ usage(void) void killact(struct kinfo_proc *kp, int dummy __unused) { - if (kill(kp->kp_proc.p_pid, signum) == -1) - err(STATUS_ERROR, "signalling pid %d", (int)kp->kp_proc.p_pid); + if (kill(kp->kp_pid, signum) == -1) + err(STATUS_ERROR, "signalling pid %d", (int)kp->kp_pid); } /* @@ -468,16 +468,16 @@ grepact(struct kinfo_proc *kp, int printdelim) if ((argv = kvm_getargv(kd, kp, 0)) == NULL) return; - printf("%d ", (int)kp->kp_proc.p_pid); + printf("%d ", (int)kp->kp_pid); for (; *argv != NULL; argv++) { printf("%s", *argv); if (argv[1] != NULL) putchar(' '); } } else if (longfmt) - printf("%d %s", (int)kp->kp_proc.p_pid, kp->kp_thread.td_comm); + printf("%d %s", (int)kp->kp_pid, kp->kp_comm); else - printf("%d", (int)kp->kp_proc.p_pid); + printf("%d", (int)kp->kp_pid); } diff --git a/usr.bin/sockstat/sockstat.c b/usr.bin/sockstat/sockstat.c index 02ce253a8d..4c4b531ab0 100644 --- a/usr.bin/sockstat/sockstat.c +++ b/usr.bin/sockstat/sockstat.c @@ -27,7 +27,7 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD: src/usr.bin/sockstat/sockstat.c,v 1.12 2004/12/06 09:28:05 ru Exp $ - * $DragonFly: src/usr.bin/sockstat/sockstat.c,v 1.6 2006/04/14 16:48:27 dillon Exp $ + * $DragonFly: src/usr.bin/sockstat/sockstat.c,v 1.7 2007/02/01 10:33:26 corecode Exp $ */ #include @@ -428,7 +428,7 @@ getprocname(pid_t pid) warn("sysctl()"); return ("??"); } - return (proc.kp_thread.td_comm); + return (proc.kp_comm); } static int diff --git a/usr.bin/systat/pigs.c b/usr.bin/systat/pigs.c index 24bce44e81..998891c2d5 100644 --- a/usr.bin/systat/pigs.c +++ b/usr.bin/systat/pigs.c @@ -32,7 +32,7 @@ * * @(#)pigs.c 8.2 (Berkeley) 9/23/93 * - * $DragonFly: src/usr.bin/systat/pigs.c,v 1.11 2005/11/14 18:50:17 dillon Exp $ + * $DragonFly: src/usr.bin/systat/pigs.c,v 1.12 2007/02/01 10:33:26 corecode Exp $ */ /* @@ -88,7 +88,6 @@ void showpigs(void) { register int i, j, y, k; - struct eproc *ep; float total; int factor; char *uname, *pname, pidname[30]; @@ -115,7 +114,7 @@ showpigs(void) char buf[256]; if (pt[k].pt_pctcpu <= 0.01 && (pt[k].pt_kp == NULL || - pt[k].pt_kp->kp_proc.p_slptime > 1) + pt[k].pt_kp->kp_lwp.kl_slptime > 1) ) { --y; continue; @@ -124,9 +123,8 @@ showpigs(void) uname = ""; pname = ""; } else { - ep = &pt[k].pt_kp->kp_eproc; - uname = (char *)user_from_uid(ep->e_ucred.cr_uid, 0); - pname = pt[k].pt_kp->kp_thread.td_comm; + uname = (char *)user_from_uid(pt[k].pt_kp->kp_uid, 0); + pname = pt[k].pt_kp->kp_comm; } wmove(wnd, y, 0); wclrtoeol(wnd); @@ -179,9 +177,8 @@ fetchpigs(void) { int i; float time; - struct proc *pp; float *pctp; - struct kinfo_proc *kpp; + struct kinfo_proc *kpp, *pp; struct kinfo_cputime cp_time, diff_cp_time; double t; static int lastnproc = 0; @@ -208,13 +205,13 @@ fetchpigs(void) */ for (i = 0; i < nproc; i++) { pt[i].pt_kp = &kpp[i]; - pp = &kpp[i].kp_proc; + pp = &kpp[i]; pctp = &pt[i].pt_pctcpu; - time = pp->p_swtime; - if (time == 0 || (pp->p_flag & P_SWAPPEDOUT)) + time = pp->kp_lwp.kl_swtime; + if (time == 0 || (pp->kp_flags & P_SWAPPEDOUT)) *pctp = 0; else - *pctp = ((double) pp->p_pctcpu / + *pctp = ((double) pp->kp_lwp.kl_pctcpu / fscale) / (1.0 - exp(time * lccpu)); } /* @@ -271,18 +268,18 @@ compar(const void *a, const void *b) /* * Then check sleep times and run status. */ - if (pta->pt_kp->kp_proc.p_slptime < ptb->pt_kp->kp_proc.p_slptime) + if (pta->pt_kp->kp_lwp.kl_slptime < ptb->pt_kp->kp_lwp.kl_slptime) return(-1); - if (pta->pt_kp->kp_proc.p_slptime > ptb->pt_kp->kp_proc.p_slptime) + if (pta->pt_kp->kp_lwp.kl_slptime > ptb->pt_kp->kp_lwp.kl_slptime) return(1); /* * Runnability */ - if (pta->pt_kp->kp_proc.p_stat != ptb->pt_kp->kp_proc.p_stat) { - if (pta->pt_kp->kp_proc.p_stat == SRUN) + if (pta->pt_kp->kp_stat != ptb->pt_kp->kp_stat) { + if (pta->pt_kp->kp_stat == SRUN) return(-1); - if (ptb->pt_kp->kp_proc.p_stat == SRUN) + if (ptb->pt_kp->kp_stat == SRUN) return(1); } return(0); diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c index f960c2b32e..791f40e868 100644 --- a/usr.bin/top/machine.c +++ b/usr.bin/top/machine.c @@ -21,7 +21,7 @@ * Hiten Pandya * * $FreeBSD: src/usr.bin/top/machine.c,v 1.29.2.2 2001/07/31 20:27:05 tmm Exp $ - * $DragonFly: src/usr.bin/top/machine.c,v 1.19 2006/10/03 12:20:11 y0netan1 Exp $ + * $DragonFly: src/usr.bin/top/machine.c,v 1.20 2007/02/01 10:33:26 corecode Exp $ */ @@ -81,17 +81,16 @@ struct handle /* declarations for load_avg */ #include "loadavg.h" -#define PP(pp, field) ((pp)->kp_proc . field) -#define EP(pp, field) ((pp)->kp_eproc . field) -#define TP(pp, field) ((pp)->kp_thread . field) -#define VP(pp, field) ((pp)->kp_eproc.e_vm . field) +#define PP(pp, field) ((pp)->kp_ ## field) +#define LP(pp, field) ((pp)->kp_lwp.kl_ ## field) +#define VP(pp, field) ((pp)->kp_vm_ ## field) /* define what weighted cpu is. */ -#define weighted_cpu(pct, pp) (PP((pp), p_swtime) == 0 ? 0.0 : \ - ((pct) / (1.0 - exp(PP((pp), p_swtime) * logcpu)))) +#define weighted_cpu(pct, pp) (LP((pp), swtime) == 0 ? 0.0 : \ + ((pct) / (1.0 - exp(LP((pp), swtime) * logcpu)))) /* what we consider to be process size: */ -#define PROCSIZE(pp) (VP((pp), vm_map.size) / 1024) +#define PROCSIZE(pp) (VP((pp), map_size) / 1024) /* * These definitions control the format of the per-process area @@ -496,18 +495,18 @@ caddr_t get_process_info(struct system_info *si, struct process_select *sel, * status field. Processes with P_SYSTEM set are system * processes---these get ignored unless show_sysprocs is set. */ - if ((show_threads && (TP(pp, td_proc) == NULL)) || - (!show_only_threads && (PP(pp, p_stat) != 0 && - (show_self != PP(pp, p_pid)) && - (show_system || ((PP(pp, p_flag) & P_SYSTEM) == 0))))) + if ((show_threads && (LP(pp, pid) == 0)) || + (!show_only_threads && (PP(pp, stat) != 0 && + (show_self != PP(pp, pid)) && + (show_system || ((PP(pp, flags) & P_SYSTEM) == 0))))) { total_procs++; - process_states[(unsigned char) PP(pp, p_stat)]++; - if ((show_threads && (TP(pp, td_proc) == NULL)) || - (!show_only_threads && (PP(pp, p_stat) != SZOMB) && - (show_idle || (PP(pp, p_pctcpu) != 0) || - (PP(pp, p_stat) == SRUN)) && - (!show_uid || EP(pp, e_ucred.cr_ruid) == (uid_t)sel->uid))) + process_states[(unsigned char) PP(pp, stat)]++; + if ((show_threads && (LP(pp, pid) == 0)) || + (!show_only_threads && (PP(pp, stat) != SZOMB) && + (show_idle || (LP(pp, pctcpu) != 0) || + (PP(pp, stat) == SRUN)) && + (!show_uid || PP(pp, ruid) == (uid_t)sel->uid))) { *prefp++ = pp; active_procs++; @@ -550,19 +549,19 @@ char *format_next_process(caddr_t handle, char *(*get_userid)()) hp->remaining--; /* set the wrapper for the process/thread name */ - if ((PP(pp, p_flag) & P_SWAPPEDOUT)) + if ((PP(pp, flags) & P_SWAPPEDOUT)) wrapper = "[]"; /* swapped process [pname] */ - else if (((PP(pp, p_flag) & P_SYSTEM) != 0) && (TP(pp, td_proc) != NULL)) + else if (((PP(pp, flags) & P_SYSTEM) != 0) && (LP(pp, pid) != 0)) wrapper = "()"; /* system process (pname) */ - else if (show_threads && (TP(pp, td_proc) == NULL)) + else if (show_threads && (LP(pp, pid) == 0)) wrapper = "<>"; /* pure kernel threads */ else wrapper = NULL; /* get the process's command name */ if (wrapper != NULL) { - char *comm = TP(pp, td_comm); -#define COMSIZ sizeof(TP(pp, td_comm)) + char *comm = PP(pp, comm); +#define COMSIZ sizeof(PP(pp, comm)) char buf[COMSIZ]; (void) strncpy(buf, comm, COMSIZ); comm[0] = wrapper[0]; @@ -577,22 +576,22 @@ char *format_next_process(caddr_t handle, char *(*get_userid)()) * time includes the interrupt time although that is not wanted here. * ps(1) is similarly sloppy. */ - cputime = (EP(pp, e_uticks) + EP(pp, e_sticks)) / 1000000; + cputime = (LP(pp, uticks) + LP(pp, sticks)) / 1000000; /* calculate the base for cpu percentages */ - pct = pctdouble(PP(pp, p_pctcpu)); + pct = pctdouble(LP(pp, pctcpu)); /* generate "STATE" field */ - switch (state = PP(pp, p_stat)) { + switch (state = PP(pp, stat)) { case SRUN: - if (smpmode && TP(pp, td_flags) & TDF_RUNNING) - sprintf(status, "CPU%d", EP(pp, e_cpuid)); + if (smpmode && LP(pp, tdflags) & TDF_RUNNING) + sprintf(status, "CPU%d", LP(pp, cpuid)); else strcpy(status, "RUN"); break; case SSLEEP: - if (TP(pp, td_wmesg) != NULL) { - sprintf(status, "%.6s", EP(pp, e_wmesg)); + if (LP(pp, wmesg) != NULL) { + sprintf(status, "%.6s", LP(pp, wmesg)); break; } /* fall through */ @@ -612,18 +611,18 @@ char *format_next_process(caddr_t handle, char *(*get_userid)()) * real time 0 - 31 -> nice value -52 - -21 * thread 0 - 31 -> nice value -53 - */ - switch(PP(pp, p_rtprio.type)) { + switch(LP(pp, rtprio.type)) { case RTP_PRIO_REALTIME: - nice = PRIO_MIN - 1 - RTP_PRIO_MAX + PP(pp, p_rtprio.prio); + nice = PRIO_MIN - 1 - RTP_PRIO_MAX + LP(pp, rtprio.prio); break; case RTP_PRIO_IDLE: - nice = PRIO_MAX + 1 + PP(pp, p_rtprio.prio); + nice = PRIO_MAX + 1 + LP(pp, rtprio.prio); break; case RTP_PRIO_THREAD: - nice = PRIO_MIN - 1 - RTP_PRIO_MAX - PP(pp, p_rtprio.prio); + nice = PRIO_MIN - 1 - RTP_PRIO_MAX - LP(pp, rtprio.prio); break; default: - nice = PP(pp, p_nice); + nice = PP(pp, nice); break; } @@ -631,21 +630,21 @@ char *format_next_process(caddr_t handle, char *(*get_userid)()) /* format this entry */ snprintf(fmt, sizeof(fmt), smpmode ? smp_Proc_format : up_Proc_format, - PP(pp, p_pid), + PP(pp, pid), namelength, namelength, - (*get_userid)(EP(pp, e_ucred.cr_ruid)), - (show_threads && (TP(pp, td_proc) == NULL)) ? TP(pp, td_pri) : - PP(pp, p_usdata.bsd4.priority), + (*get_userid)(PP(pp, ruid)), + (show_threads && (LP(pp, pid) == 0)) ? LP(pp, tdprio) : + LP(pp, prio), nice, format_k2(PROCSIZE(pp)), - format_k2(pagetok(VP(pp, vm_rssize))), + format_k2(pagetok(VP(pp, rssize))), status, - smpmode ? EP(pp, e_cpuid) : 0, + smpmode ? LP(pp, cpuid) : 0, format_time(cputime), 100.0 * weighted_cpu(pct, pp), 100.0 * pct, cmdlength, - printable(TP(pp, td_comm))); + printable(PP(pp, comm))); /* return the result */ return(fmt); @@ -708,30 +707,30 @@ static unsigned char sorted_state[] = #define ORDERKEY_PCTCPU \ - if (lresult = (long) PP(p2, p_pctcpu) - (long) PP(p1, p_pctcpu), \ + if (lresult = (long) LP(p2, pctcpu) - (long) LP(p1, pctcpu), \ (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0) -#define CPTICKS(p) (EP(p, e_uticks) + EP(p, e_sticks)) +#define CPTICKS(p) (LP(p, uticks) + LP(p, sticks)) #define ORDERKEY_CPTICKS \ if ((result = CPTICKS(p2) > CPTICKS(p1) ? 1 : \ CPTICKS(p2) < CPTICKS(p1) ? -1 : 0) == 0) #define ORDERKEY_STATE \ - if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] - \ - sorted_state[(unsigned char) PP(p1, p_stat)]) == 0) + if ((result = sorted_state[(unsigned char) PP(p2, stat)] - \ + sorted_state[(unsigned char) PP(p1, stat)]) == 0) #define ORDERKEY_PRIO \ - if ((result = PP(p2, p_usdata.bsd4.priority) - PP(p1, p_usdata.bsd4.priority)) == 0) + if ((result = LP(p2, prio) - LP(p1, prio)) == 0) #define ORDERKEY_KTHREADS \ - if ((result = (TP(p1, td_proc) == NULL) - (TP(p2, td_proc) == NULL)) == 0) + if ((result = (LP(p1, pid) == 0) - (LP(p2, pid) == 0)) == 0) #define ORDERKEY_KTHREADS_PRIO \ - if ((result = TP(p2, td_pri) - TP(p1, td_pri)) == 0) + if ((result = LP(p2, tdprio) - LP(p1, tdprio)) == 0) #define ORDERKEY_RSSIZE \ - if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0) + if ((result = VP(p2, rssize) - VP(p1, rssize)) == 0) #define ORDERKEY_MEM \ if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 ) @@ -931,9 +930,9 @@ int proc_owner(int pid) while (--cnt >= 0) { pp = *prefp++; - if (PP(pp, p_pid) == (pid_t)pid) + if (PP(pp, pid) == (pid_t)pid) { - return((int)EP(pp, e_ucred.cr_ruid)); + return((int)PP(pp, ruid)); } } return(-1); diff --git a/usr.bin/w/extern.h b/usr.bin/w/extern.h index f50a6039d7..a7f46af1d3 100644 --- a/usr.bin/w/extern.h +++ b/usr.bin/w/extern.h @@ -32,12 +32,12 @@ * * @(#)extern.h 8.1 (Berkeley) 6/6/93 * $FreeBSD: src/usr.bin/w/extern.h,v 1.2.6.1 2002/03/12 19:51:51 phantom Exp $ - * $DragonFly: src/usr.bin/w/extern.h,v 1.3 2003/11/03 19:31:33 eirikn Exp $ + * $DragonFly: src/usr.bin/w/extern.h,v 1.4 2007/02/01 10:33:26 corecode Exp $ */ extern int use_ampm; -struct proc; +struct kinfo_proc; void pr_attime(time_t *, time_t *); int pr_idle(time_t); -int proc_compare(struct proc *, struct proc *); +int proc_compare(struct kinfo_proc *, struct kinfo_proc *); diff --git a/usr.bin/w/proc_compare.c b/usr.bin/w/proc_compare.c index 7fb0363419..3005ecd782 100644 --- a/usr.bin/w/proc_compare.c +++ b/usr.bin/w/proc_compare.c @@ -32,7 +32,7 @@ * * @(#)proc_compare.c 8.2 (Berkeley) 9/23/93 * - * $DragonFly: src/usr.bin/w/proc_compare.c,v 1.5 2005/06/27 18:38:03 dillon Exp $ + * $DragonFly: src/usr.bin/w/proc_compare.c,v 1.6 2007/02/01 10:33:26 corecode Exp $ */ #include @@ -60,14 +60,14 @@ * TODO - consider whether pctcpu should be used. */ -#define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL)) +#define ISRUN(p) (((p)->kp_stat == SRUN) || ((p)->kp_stat == SIDL)) #define TESTAB(a, b) ((a)<<1 | (b)) #define ONLYA 2 #define ONLYB 1 #define BOTH 3 int -proc_compare(register struct proc *p1, register struct proc *p2) +proc_compare(struct kinfo_proc *p1, struct kinfo_proc *p2) { if (p1 == NULL) @@ -84,36 +84,36 @@ proc_compare(register struct proc *p1, register struct proc *p2) /* * tie - favor one with highest recent cpu utilization */ - if (p2->p_usdata.bsd4.estcpu > p1->p_usdata.bsd4.estcpu) + if (p2->kp_lwp.kl_estcpu > p1->kp_lwp.kl_estcpu) return (1); - if (p1->p_usdata.bsd4.estcpu > p2->p_usdata.bsd4.estcpu) + if (p1->kp_lwp.kl_estcpu > p2->kp_lwp.kl_estcpu) return (0); - return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ + return (p2->kp_pid > p1->kp_pid); /* tie - return highest pid */ } /* * weed out zombies */ - switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) { + switch (TESTAB(p1->kp_stat == SZOMB, p2->kp_stat == SZOMB)) { case ONLYA: return (1); case ONLYB: return (0); case BOTH: - return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ + return (p2->kp_pid > p1->kp_pid); /* tie - return highest pid */ } /* * pick the one with the smallest sleep time */ - if (p2->p_slptime > p1->p_slptime) + if (p2->kp_lwp.kl_slptime > p1->kp_lwp.kl_slptime) return (0); - if (p1->p_slptime > p2->p_slptime) + if (p1->kp_lwp.kl_slptime > p2->kp_lwp.kl_slptime) return (1); /* * favor one sleeping in a non-interruptible sleep */ - if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0) + if (p1->kp_flags & P_SINTR && (p2->kp_flags & P_SINTR) == 0) return (1); - if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0) + if (p2->kp_flags & P_SINTR && (p1->kp_flags & P_SINTR) == 0) return (0); - return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ + return (p2->kp_pid > p1->kp_pid); /* tie - return highest pid */ } diff --git a/usr.bin/w/w.c b/usr.bin/w/w.c index 9b4a2c055f..22ab828e0b 100644 --- a/usr.bin/w/w.c +++ b/usr.bin/w/w.c @@ -33,7 +33,7 @@ * @(#) Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California. All rights reserved. * @(#)w.c 8.4 (Berkeley) 4/16/94 * $FreeBSD: src/usr.bin/w/w.c,v 1.38.2.6 2002/03/12 19:51:51 phantom Exp $ - * $DragonFly: src/usr.bin/w/w.c,v 1.7 2005/03/25 14:15:23 liamfoy Exp $ + * $DragonFly: src/usr.bin/w/w.c,v 1.8 2007/02/01 10:33:26 corecode Exp $ */ /* @@ -108,7 +108,7 @@ struct entry { struct kinfo_proc *dkp; /* debug option proc list */ } *ep, *ehead = NULL, **nextp = &ehead; -#define debugproc(p) *((struct kinfo_proc **)&(p)->kp_eproc.e_spare[0]) +#define debugproc(p) *((struct kinfo_proc **)&(p)->kp_spare[0]) static void pr_header(time_t *, int); static struct stat *ttystat(char *, int); @@ -275,22 +275,18 @@ main(int argc, char **argv) if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL) err(1, "%s", kvm_geterr(kd)); for (i = 0; i < nentries; i++, kp++) { - struct proc *pr = &kp->kp_proc; - struct eproc *e; - - if (pr->p_stat == SIDL || pr->p_stat == SZOMB) + if (kp->kp_stat == SIDL || kp->kp_stat == SZOMB) continue; - e = &kp->kp_eproc; for (ep = ehead; ep != NULL; ep = ep->next) { - if (ep->tdev == e->e_tdev) { + if (ep->tdev == kp->kp_tdev) { /* * proc is associated with this terminal */ - if (ep->kp == NULL && e->e_pgid == e->e_tpgid) { + if (ep->kp == NULL && kp->kp_pgid == kp->kp_tpgid) { /* * Proc is 'most interesting' */ - if (proc_compare(&ep->kp->kp_proc, pr)) + if (proc_compare(ep->kp, kp)) ep->kp = kp; } /* @@ -320,7 +316,7 @@ main(int argc, char **argv) continue; } ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth), - ep->kp->kp_thread.td_comm, MAXCOMLEN); + ep->kp->kp_comm, MAXCOMLEN); if (ep->args == NULL) err(1, NULL); } @@ -392,11 +388,11 @@ main(int argc, char **argv) char *ptr; ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth), - dkp->kp_thread.td_comm, MAXCOMLEN); + dkp->kp_comm, MAXCOMLEN); if (ptr == NULL) ptr = "-"; (void)printf("\t\t%-9d %s\n", - dkp->kp_proc.p_pid, ptr); + dkp->kp_pid, ptr); } } (void)printf("%-*.*s %-*.*s %-*.*s ", -- 2.41.0