Subject: Re: skill package changes for NetBSD >= 1.5
To: None <hubert.feyrer@informatik.fh-regensburg.de>
From: Simon Burge <simonb@netbsd.org>
List: tech-pkg
Date: 06/23/2000 17:17:47
Hubert Feyrer wrote:

> On Fri, 23 Jun 2000, Simon Burge wrote:
> > No problems there - we've had user-available sysctls() to read process
> > info since Day 1 of NetBSD, so it should also work on NetBSD 0.8!  I'll
> > set things up so that if the newer process info sysctl() is available it
> > will be used - this means that skill won't need to be recompiled every
> > time some kernel internals (specifically 'struct proc') get changed.
> 
> That's cool! Can you back-port pkgsrc to 0.8 as well?
> ;-)

Sorry... :)


Can someone test the patch below on a NetBSD box prior to 1.4Z?  I don't
easy access to anything that old at the moment.  Note that there's
patches and a new patches/patch-ab file.  It should install skill mode
555 and should work as the old one did.  Once this is confirmed as
working I'll commit the changes and send them off to the author.  Bonus
points for anyone who can check these patches actually work on one of
the other *BSD's!

Ta,
Simon.
--

Index: Makefile
===================================================================
RCS file: /cvsroot/pkgsrc/sysutils/skill/Makefile,v
retrieving revision 1.8
diff -p -u -r1.8 Makefile
--- Makefile	2000/02/20 02:02:51	1.8
+++ Makefile	2000/06/23 07:03:42
@@ -12,13 +12,12 @@ EXTRACT_SUFX=	_tar.Z
 
 MAINTAINER=	kim@tac.nyc.ny.us
 
-BINMODE=	02555
-
 .include "../../mk/bsd.prefs.mk"
 
 .if ${OPSYS} == "NetBSD"
-MAKE_ENV+=	OSTYPE=bsd-44 BINGRP=kmem COPTS=-DUVM
+MAKE_ENV+=	OSTYPE=bsd-44
 .elif ${OPSYS} == "SunOS"
+BINMODE=	02555
 MAKE_ENV+=	OSTYPE=sys-5r4 BINGRP=sys CPPFLAGS=
 .endif
 
Index: files/patch-sum
===================================================================
RCS file: /cvsroot/pkgsrc/sysutils/skill/files/patch-sum,v
retrieving revision 1.4
diff -p -u -r1.4 patch-sum
--- patch-sum	2000/02/20 02:02:52	1.4
+++ patch-sum	2000/06/23 07:03:42
@@ -1,3 +1,4 @@
 $NetBSD: patch-sum,v 1.4 2000/02/20 02:02:52 wiz Exp $
 
-MD5 (patch-aa) = f923b45b898f5a9d5db09a45dceedabc
+MD5 (patch-aa) = c650e9348d22bb07487b0bf9b4e5c599
+MD5 (patch-ab) = 2884b29a7d91bc52d190fa669127f082
Index: patches/patch-aa
===================================================================
RCS file: /cvsroot/pkgsrc/sysutils/skill/patches/patch-aa,v
retrieving revision 1.7
diff -p -u -r1.7 patch-aa
--- patch-aa	2000/02/20 02:02:52	1.7
+++ patch-aa	2000/06/23 07:03:42
@@ -10,7 +10,8 @@ $NetBSD: patch-aa,v 1.7 2000/02/20 02:02
 -BINGRP=	kmem
 +BINDIR=	${PREFIX}/bin
 +#BINGRP=	kmem
- BINMOD=	2755
+-BINMOD=	2755
++#BINMOD=	${BINMODE}
  
  # CONFIGURE: Set location of man directory and man page suffix.
 -MANDIR=	/usr/man/manl
@@ -23,12 +24,10 @@ $NetBSD: patch-aa,v 1.7 2000/02/20 02:02
 +#OSTYPE=	bsd-44
  
  # CONFIGURE(Config): Add any OS-specific options here.
--COPTS=	
-+#COPTS=	-DUVM
+ COPTS=	
  
  # CONFIGURE(Config): Add any special libraries your system needs.
--LIBS=	
-+LIBS=	-lkvm
+ LIBS=	
  
  SRCS=	main.c argparse.c getproc.c
  OBJS=	main.o argparse.o getproc.o


------------------------------- new patch-ab -------------------------------
$NetBSD$

--- machdep/bsd-44.c.orig	Sun Feb 20 07:37:39 2000
+++ machdep/bsd-44.c	Fri Jun 23 13:33:09 2000
@@ -135,9 +135,6 @@
  * Now, set up everything we need to write a GetProc() routine.
  */
 
-#include <kvm.h>
-#include <fcntl.h>
-
 #if defined(BSD4_4) && (BSD < 199306)
 #include <sys/kinfo.h>
 #include <sys/kinfo_proc.h>
@@ -155,8 +152,28 @@
 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
+#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
 
 /*
  * GetProc()
@@ -169,54 +186,59 @@
 {
 	static struct ProcInfo procinfo;
 	static int nproc = -1;
-	static struct kinfo_proc *aproc;
-	static kvm_t *kd = NULL;
+	static PROCTYPE *aproc;
 #ifdef	_POSIX2_LINE_MAX
 	char errbuf[_POSIX2_LINE_MAX];
 #else
 	char errbuf[2048];
 #endif
+	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 = "<defunct>";
 				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 = "<exiting>";
 				procinfo.pi_flags |= PI_SWEXIT;
 				procinfo.pi_cmd = exiting;
@@ -228,22 +250,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++;