$NetBSD: patch-ab,v 1.7 2000/09/03 13:31:00 wiz Exp $ --- machdep/bsd-44.c.orig Sun Feb 20 07:37:39 2000 +++ machdep/bsd-44.c Tue Jun 27 00:03:39 2000 @@ -23,11 +23,54 @@ #endif #endif +#if defined(BSD4_4) && (BSD < 199306) +#include +#include +#define _ALLPROCFLAG KINFO_PROC_ALL +#else +#include +#include +#define _ALLPROCFLAG KERN_PROC_ALL +#endif + #include +#include +#include #include -#include +#include #include -#include +#include + +#ifndef P_WEXIT /* backward compatibility */ +#define P_WEXIT SWEXIT +#endif + +static char *pidmap[] = { "swapper", "init", "pagedaemon" }; +static int pidmapsiz = sizeof(pidmap) / sizeof(pidmap[0]); + +#ifdef KERN_PROC2 +#define CMDLEN KI_MAXCOMLEN +#define PROCTYPE struct kinfo_proc2 +#define SYSCTL_TO_USE KERN_PROC2 +#define SYSCTL_MIBS 6 +#define PROC_COMM(kprocp) kprocp->p_comm +#define PROC_FLAG(kprocp) kprocp->p_flag +#define PROC_PID(kprocp) kprocp->p_pid +#define PROC_STAT(kprocp) kprocp->p_stat +#define PROC_TDEV(kprocp) kprocp->p_tdev +#define PROC_UID(kprocp) kprocp->p_uid +#else +#define PROCTYPE struct kinfo_proc +#define SYSCTL_TO_USE KERN_PROC +#define SYSCTL_MIBS 4 +#define PROC_COMM(kprocp) kprocp->kp_proc.p_comm +#define PROC_FLAG(kprocp) kprocp->kp_proc.p_flag +#define PROC_PID(kprocp) kprocp->kp_proc.p_pid +#define PROC_STAT(kprocp) kprocp->kp_proc.p_stat +#define PROC_TDEV(kprocp) kprocp->kp_eproc.e_tdev +#define PROC_UID(kprocp) kprocp->kp_eproc.e_ucred.cr_uid +#endif + /* * Define SigNames, NSig, and TtyDevDir here; they are used by other @@ -132,33 +175,6 @@ } /* - * Now, set up everything we need to write a GetProc() routine. - */ - -#include -#include - -#if defined(BSD4_4) && (BSD < 199306) -#include -#include -#define _ALLPROCFLAG KINFO_PROC_ALL -#else -#include -#include -#define _ALLPROCFLAG KERN_PROC_ALL -#endif - -#ifndef P_WEXIT /* backward compatibility */ -#define P_WEXIT SWEXIT -#endif - -static char *pidmap[] = { "swapper", "init", "pagedaemon" }; -static int pidmapsiz = sizeof(pidmap) / sizeof(pidmap[0]); - -#define PROC(kprocp) kprocp->kp_proc -#define EPROC(kprocp) kprocp->kp_eproc - -/* * GetProc() * * Fill in and return a `struct ProcInfo' with information about the @@ -169,54 +185,54 @@ { static struct ProcInfo procinfo; static int nproc = -1; - static struct kinfo_proc *aproc; - static kvm_t *kd = NULL; -#ifdef _POSIX2_LINE_MAX - char errbuf[_POSIX2_LINE_MAX]; -#else - char errbuf[2048]; -#endif + static PROCTYPE *aproc; + int mib[6]; + size_t size; /* * If this is our first time here, prepare to read procs from kernel. */ if (nproc == -1) { - kd = kvm_openfiles((char *)NULL, (char *)NULL, (char *)NULL, - O_RDONLY, errbuf); - if (kd == NULL) { - fprintf(stderr, "%s: %s\n", ProgName, errbuf); - exit(EX_SERR); - } + mib[0] = CTL_KERN; + mib[1] = SYSCTL_TO_USE; + mib[2] = _ALLPROCFLAG; + mib[3] = 0; +#ifdef KERN_PROC2 + mib[4] = sizeof(PROCTYPE); + mib[5] = 0; +#endif + if (sysctl(mib, SYSCTL_MIBS, NULL, &size, NULL, 0) < 0) + err(1, "sysctl kern.proc2"); + aproc = (PROCTYPE *)malloc(size); + if (aproc == NULL) + errx(1, "out of memory"); - if ((aproc=kvm_getprocs(kd, _ALLPROCFLAG, 0, &nproc)) == NULL) { - fprintf(stderr, "%s: %s\n", ProgName, kvm_geterr(kd)); - exit(EX_SERR); - } +#ifdef KERN_PROC2 + mib[5] = size / sizeof(PROCTYPE); +#endif + if (sysctl(mib, SYSCTL_MIBS, aproc, &size, NULL, 0) < 0) + err(1, "sysctl kern.proc2"); + nproc = size / sizeof(PROCTYPE); } - if (nproc == 0) { - if (kd != NULL) { - kvm_close(kd); - kd = NULL; - } - return((struct ProcInfo *)NULL); - } + if (nproc == 0) + return(NULL); do { - if (PROC(aproc).p_stat != 0) { + if (PROC_STAT(aproc) != 0) { /* * Make sure this isn't a "zombie" or "exiting" * process. If it is, fill in procinfo and return. */ procinfo.pi_flags = 0; - procinfo.pi_pid = (pid_T) PROC(aproc).p_pid; - procinfo.pi_uid = (uid_T) EPROC(aproc).e_ucred.cr_uid; + procinfo.pi_pid = (pid_T) PROC_PID(aproc); + procinfo.pi_uid = (uid_T) PROC_UID(aproc); - if (PROC(aproc).p_stat == SZOMB) { /* zombie */ + if (PROC_STAT(aproc) == SZOMB) { /* zombie */ static char *zombie = ""; procinfo.pi_flags |= PI_ZOMBIE; procinfo.pi_cmd = zombie; - } else if (PROC(aproc).p_flag & P_WEXIT) { /* exiting */ + } else if (PROC_FLAG(aproc) & P_WEXIT) { /* exiting */ static char *exiting = ""; procinfo.pi_flags |= PI_SWEXIT; procinfo.pi_cmd = exiting; @@ -228,22 +244,22 @@ return(&procinfo); } } - } while (PROC(aproc).p_stat == 0); + } while (PROC_STAT(aproc) == 0); /* * We now have a process (`aproc'). * Fill in the rest of `procinfo'. */ - if (EPROC(aproc).e_tdev != NODEV) { /* controlling tty */ + if (PROC_TDEV(aproc) != NODEV) { /* controlling tty */ procinfo.pi_flags |= PI_CTLTTY; - procinfo.pi_tty = (tty_T) EPROC(aproc).e_tdev; + procinfo.pi_tty = (tty_T) PROC_TDEV(aproc); } - if (PROC(aproc).p_pid < pidmapsiz) { /* special */ - procinfo.pi_cmd = pidmap[PROC(aproc).p_pid]; + if (PROC_PID(aproc) < pidmapsiz) { /* special */ + procinfo.pi_cmd = pidmap[PROC_PID(aproc)]; procinfo.pi_flags |= PI_ASKUSR; } else /* set path-stripped command name */ - SETCMD(procinfo.pi_cmd, PROC(aproc).p_comm, MAXCOMLEN) + SETCMD(procinfo.pi_cmd, PROC_COMM(aproc), MAXCOMLEN) nproc--; aproc++;