Use new sysctl/kvm interfaces and remove sgid bit.

This commit is contained in:
simonb 2000-05-26 03:10:31 +00:00
parent fd521aefe9
commit ea74487218
4 changed files with 25 additions and 45 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.14 1999/09/12 01:19:26 chs Exp $
# $NetBSD: Makefile,v 1.15 2000/05/26 03:10:31 simonb Exp $
# @(#)Makefile 8.1 (Berkeley) 6/6/93
PROG= w
@ -6,8 +6,6 @@ SRCS= fmt.c pr_time.c proc_compare.c w.c
MAN= w.1 uptime.1
DPADD= ${LIBKVM}
LDADD= -lkvm
BINGRP= kmem
BINMODE=2555
LINKS= ${BINDIR}/w ${BINDIR}/uptime
.PATH: ${.CURDIR}/../../bin/ps

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.3 1996/06/07 01:39:16 thorpej Exp $ */
/* $NetBSD: extern.h,v 1.4 2000/05/26 03:10:31 simonb Exp $ */
/*-
* Copyright (c) 1993
@ -35,9 +35,9 @@
* @(#)extern.h 8.1 (Berkeley) 6/6/93
*/
struct proc;
struct kinfo_proc2;
void fmt_puts __P((char *, int *));
void fmt_putc __P((int, int *));
void pr_attime __P((time_t *, time_t *));
void pr_idle __P((time_t));
int proc_compare __P((struct proc *, struct proc *));
int proc_compare __P((struct kinfo_proc2 *, struct kinfo_proc2 *));

View File

@ -1,4 +1,4 @@
/* $NetBSD: proc_compare.c,v 1.8 2000/05/26 00:44:27 thorpej Exp $ */
/* $NetBSD: proc_compare.c,v 1.9 2000/05/26 03:10:31 simonb Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -38,13 +38,13 @@
#if 0
static char sccsid[] = "@(#)proc_compare.c 8.2 (Berkeley) 9/23/93";
#else
__RCSID("$NetBSD: proc_compare.c,v 1.8 2000/05/26 00:44:27 thorpej Exp $");
__RCSID("$NetBSD: proc_compare.c,v 1.9 2000/05/26 03:10:31 simonb Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/time.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include "extern.h"
@ -76,7 +76,7 @@ __RCSID("$NetBSD: proc_compare.c,v 1.8 2000/05/26 00:44:27 thorpej Exp $");
int
proc_compare(p1, p2)
struct proc *p1, *p2;
struct kinfo_proc2 *p1, *p2;
{
if (p1 == NULL)

View File

@ -1,4 +1,4 @@
/* $NetBSD: w.c,v 1.34 1999/11/29 11:15:13 pk Exp $ */
/* $NetBSD: w.c,v 1.35 2000/05/26 03:10:31 simonb Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993, 1994
@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)w.c 8.6 (Berkeley) 6/30/94";
#else
__RCSID("$NetBSD: w.c,v 1.34 1999/11/29 11:15:13 pk Exp $");
__RCSID("$NetBSD: w.c,v 1.35 2000/05/26 03:10:31 simonb Exp $");
#endif
#endif /* not lint */
@ -105,12 +105,12 @@ char domain[MAXHOSTNAMELEN + 1];
struct entry {
struct entry *next;
struct utmp utmp;
dev_t tdev; /* dev_t of terminal */
time_t idle; /* idle time of terminal in seconds */
struct kinfo_proc *kp; /* `most interesting' proc */
dev_t tdev; /* dev_t of terminal */
time_t idle; /* idle time of terminal in seconds */
struct kinfo_proc2 *kp; /* `most interesting' proc */
} *ep, *ehead = NULL, **nextp = &ehead;
static void pr_args __P((struct kinfo_proc *));
static void pr_args __P((struct kinfo_proc2 *));
static void pr_header __P((time_t *, int));
static struct stat
*ttystat __P((char *));
@ -123,18 +123,15 @@ main(argc, argv)
char **argv;
{
extern char *__progname;
struct kinfo_proc *kp;
struct kinfo_proc2 *kp;
struct hostent *hp;
struct stat *stp;
FILE *ut;
struct in_addr l;
int ch, i, nentries, nusers, wcmd;
gid_t egid = getegid();
char *memf, *nlistf, *p, *x;
char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
(void)setegid(getgid());
/* Are we w(1) or uptime(1)? */
p = __progname;
if (*p == '-')
@ -176,24 +173,10 @@ main(argc, argv)
argc -= optind;
argv += optind;
/*
* Discard setgid privileges. If not the running kernel, we toss
* them away totally so that bad guys can't print interesting stuff
* from kernel memory, otherwise switch back to kmem for the
* duration of the kvm_openfiles() call.
*/
if (nlistf != NULL || memf != NULL)
(void)setgid(getgid());
else
(void)setegid(egid);
if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
if ((kd = kvm_openfiles(nlistf, memf, NULL,
memf == NULL ? KVM_NO_FILES : O_RDONLY, errbuf)) == NULL)
errx(1, "%s", errbuf);
/* get rid of it now anyway */
if (nlistf == NULL && memf == NULL)
(void)setgid(getgid());
(void)time(&now);
if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
err(1, "%s", _PATH_UTMP);
@ -246,21 +229,20 @@ main(argc, argv)
#define WUSED (sizeof (HEADER) - sizeof ("WHAT\n"))
(void)printf(HEADER);
if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
if ((kp = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc),
&nentries)) == NULL)
errx(1, "%s", kvm_geterr(kd));
for (i = 0; i < nentries; i++, kp++) {
struct proc *p = &kp->kp_proc;
struct eproc *e;
if (p->p_stat == SIDL || p->p_stat == SZOMB)
if (kp->p_stat == SIDL || kp->p_stat == SZOMB)
continue;
e = &kp->kp_eproc;
for (ep = ehead; ep != NULL; ep = ep->next) {
if (ep->tdev == e->e_tdev && e->e_pgid == e->e_tpgid) {
if (ep->tdev == kp->p_tdev &&
kp->p__pgid == kp->p_tpgid) {
/*
* Proc is in foreground of this terminal
*/
if (proc_compare(&ep->kp->kp_proc, p))
if (proc_compare(ep->kp, kp))
ep->kp = kp;
break;
}
@ -345,7 +327,7 @@ main(argc, argv)
static void
pr_args(kp)
struct kinfo_proc *kp;
struct kinfo_proc2 *kp;
{
char **argv;
int left;
@ -353,7 +335,7 @@ pr_args(kp)
if (kp == 0)
goto nothing;
left = argwidth;
argv = kvm_getargv(kd, kp, argwidth);
argv = kvm_getargv2(kd, kp, argwidth);
if (argv == 0)
goto nothing;
while (*argv) {