2006-06-22 21:57:31 +04:00
|
|
|
/* $NetBSD: w.c,v 1.72 2006/06/22 17:57:31 christos Exp $ */
|
1996-06-07 05:36:39 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1994-05-09 07:35:55 +04:00
|
|
|
* Copyright (c) 1980, 1991, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* 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.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
|
|
|
*/
|
|
|
|
|
1997-10-20 06:49:14 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-10-20 06:49:14 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1996-06-07 05:36:39 +04:00
|
|
|
#if 0
|
1997-01-09 14:55:19 +03:00
|
|
|
static char sccsid[] = "@(#)w.c 8.6 (Berkeley) 6/30/94";
|
1996-06-07 05:36:39 +04:00
|
|
|
#else
|
2006-06-22 21:57:31 +04:00
|
|
|
__RCSID("$NetBSD: w.c,v 1.72 2006/06/22 17:57:31 christos Exp $");
|
1996-06-07 05:36:39 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* w - print system status (who and what)
|
|
|
|
*
|
|
|
|
* This program is similar to the systat command on Tenex/Tops 10/20
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
2000-09-28 22:20:29 +04:00
|
|
|
#include <sys/types.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/stat.h>
|
1994-05-09 07:35:55 +04:00
|
|
|
#include <sys/sysctl.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/user.h>
|
|
|
|
#include <sys/ioctl.h>
|
1994-05-09 07:35:55 +04:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <ctype.h>
|
1994-05-09 07:35:55 +04:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <kvm.h>
|
1997-03-04 01:12:19 +03:00
|
|
|
#include <limits.h>
|
1994-05-09 07:35:55 +04:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <nlist.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <paths.h>
|
|
|
|
#include <stdio.h>
|
1994-05-09 07:35:55 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1998-04-02 15:39:40 +04:00
|
|
|
#include <time.h>
|
1994-05-09 07:35:55 +04:00
|
|
|
#include <tzfile.h>
|
|
|
|
#include <unistd.h>
|
2002-07-28 03:58:40 +04:00
|
|
|
#ifdef SUPPORT_UTMP
|
1994-05-09 07:35:55 +04:00
|
|
|
#include <utmp.h>
|
2002-07-28 03:58:40 +04:00
|
|
|
#endif
|
|
|
|
#ifdef SUPPORT_UTMPX
|
|
|
|
#include <utmpx.h>
|
|
|
|
#endif
|
1993-07-15 21:55:48 +04:00
|
|
|
#include <vis.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-09 07:35:55 +04:00
|
|
|
#include "extern.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-09 07:35:55 +04:00
|
|
|
struct timeval boottime;
|
|
|
|
struct winsize ws;
|
|
|
|
kvm_t *kd;
|
|
|
|
time_t now; /* the current time of day */
|
|
|
|
time_t uptime; /* time of last reboot & elapsed time since */
|
|
|
|
int ttywidth; /* width of tty */
|
2000-07-16 02:45:14 +04:00
|
|
|
int argwidth; /* width of tty left to print process args */
|
1994-05-09 07:35:55 +04:00
|
|
|
int header = 1; /* true if -h flag: don't print heading */
|
|
|
|
int nflag; /* true if -n flag: don't convert addrs */
|
2004-12-22 20:16:07 +03:00
|
|
|
int wflag; /* true if -w flag: wide printout */
|
1994-05-09 07:35:55 +04:00
|
|
|
int sortidle; /* sort bu idle time */
|
|
|
|
char *sel_user; /* login of particular user selected */
|
1998-07-06 10:56:06 +04:00
|
|
|
char domain[MAXHOSTNAMELEN + 1];
|
2002-07-28 03:58:40 +04:00
|
|
|
int maxname = 8, maxline = 3, maxhost = 16;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
1994-05-09 07:35:55 +04:00
|
|
|
* One of these per active utmp entry.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
struct entry {
|
|
|
|
struct entry *next;
|
2006-06-22 21:57:31 +04:00
|
|
|
char name[UTX_USERSIZE + 1];
|
|
|
|
char line[UTX_LINESIZE + 1];
|
|
|
|
char host[UTX_HOSTSIZE + 1];
|
2003-02-26 18:04:10 +03:00
|
|
|
char type[2];
|
2002-07-28 03:58:40 +04:00
|
|
|
struct timeval tv;
|
2000-05-26 07:10:31 +04:00
|
|
|
dev_t tdev; /* dev_t of terminal */
|
|
|
|
time_t idle; /* idle time of terminal in seconds */
|
2003-02-26 22:10:28 +03:00
|
|
|
struct kinfo_proc2 *tp; /* `most interesting' tty proc */
|
|
|
|
struct kinfo_proc2 *pp; /* pid proc */
|
2003-02-26 18:01:09 +03:00
|
|
|
pid_t pid; /* pid or ~0 if not known */
|
1993-03-21 12:45:37 +03:00
|
|
|
} *ep, *ehead = NULL, **nextp = &ehead;
|
|
|
|
|
2004-11-19 16:17:06 +03:00
|
|
|
static void pr_args(struct kinfo_proc2 *);
|
|
|
|
static void pr_header(time_t *, int);
|
2002-07-28 03:58:40 +04:00
|
|
|
#if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
|
2004-11-19 16:17:06 +03:00
|
|
|
static int ttystat(const char *, struct stat *);
|
2002-07-28 03:58:40 +04:00
|
|
|
static void process(struct entry *);
|
|
|
|
#endif
|
2004-11-19 16:17:06 +03:00
|
|
|
static void usage(int);
|
|
|
|
|
2001-01-05 07:59:21 +03:00
|
|
|
int main(int, char **);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-09 07:35:55 +04:00
|
|
|
int
|
2001-01-05 07:59:21 +03:00
|
|
|
main(int argc, char **argv)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2000-05-26 07:10:31 +04:00
|
|
|
struct kinfo_proc2 *kp;
|
1994-05-09 07:35:55 +04:00
|
|
|
struct hostent *hp;
|
1997-05-19 07:44:43 +04:00
|
|
|
struct in_addr l;
|
2005-09-15 04:58:49 +04:00
|
|
|
int ch, i, nentries, nusers, wcmd, curtain, use_sysctl;
|
|
|
|
char *memf, *nlistf, *p, *x, *usrnp;
|
2002-07-28 03:58:40 +04:00
|
|
|
time_t then;
|
2005-09-15 04:58:49 +04:00
|
|
|
size_t len;
|
2002-07-28 03:58:40 +04:00
|
|
|
#ifdef SUPPORT_UTMP
|
|
|
|
struct utmp *ut;
|
|
|
|
#endif
|
|
|
|
#ifdef SUPPORT_UTMPX
|
|
|
|
struct utmpx *utx;
|
|
|
|
#endif
|
2001-02-20 02:03:42 +03:00
|
|
|
const char *progname;
|
1997-03-04 01:12:19 +03:00
|
|
|
char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2004-12-22 20:20:30 +03:00
|
|
|
setprogname(argv[0]);
|
|
|
|
|
1994-05-09 07:35:55 +04:00
|
|
|
/* Are we w(1) or uptime(1)? */
|
2001-02-20 02:03:42 +03:00
|
|
|
progname = getprogname();
|
|
|
|
if (*progname == '-')
|
|
|
|
progname++;
|
|
|
|
if (*progname == 'u') {
|
1993-03-21 12:45:37 +03:00
|
|
|
wcmd = 0;
|
1994-05-09 07:35:55 +04:00
|
|
|
p = "";
|
|
|
|
} else {
|
|
|
|
wcmd = 1;
|
2004-12-22 20:16:07 +03:00
|
|
|
p = "hiM:N:nw";
|
1994-05-09 07:35:55 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-09 07:35:55 +04:00
|
|
|
memf = nlistf = NULL;
|
1997-10-20 07:08:44 +04:00
|
|
|
while ((ch = getopt(argc, argv, p)) != -1)
|
1994-05-09 07:35:55 +04:00
|
|
|
switch (ch) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'h':
|
|
|
|
header = 0;
|
|
|
|
break;
|
|
|
|
case 'i':
|
1994-05-09 07:35:55 +04:00
|
|
|
sortidle = 1;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
header = 0;
|
|
|
|
memf = optarg;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
nlistf = optarg;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nflag = 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
2004-12-22 20:16:07 +03:00
|
|
|
case 'w':
|
|
|
|
wflag = 1;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case '?':
|
|
|
|
default:
|
1994-05-09 07:35:55 +04:00
|
|
|
usage(wcmd);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2005-09-15 04:58:49 +04:00
|
|
|
use_sysctl = (memf == NULL && nlistf == NULL);
|
|
|
|
|
2005-09-05 01:18:33 +04:00
|
|
|
if ((kd = kvm_openfiles(nlistf, memf, NULL,
|
|
|
|
memf == NULL ? KVM_NO_FILES : O_RDONLY, errbuf)) == NULL)
|
|
|
|
errx(1, "%s", errbuf);
|
1994-05-09 07:35:55 +04:00
|
|
|
|
|
|
|
(void)time(&now);
|
2002-09-16 05:49:06 +04:00
|
|
|
|
2005-09-15 04:58:49 +04:00
|
|
|
if (use_sysctl) {
|
|
|
|
len = sizeof(curtain);
|
|
|
|
if (sysctlbyname("security.curtain", &curtain, &len,
|
|
|
|
NULL, 0) == -1)
|
|
|
|
curtain = 0;
|
|
|
|
}
|
|
|
|
|
2002-09-16 05:49:06 +04:00
|
|
|
#ifdef SUPPORT_UTMPX
|
|
|
|
setutxent();
|
2002-07-28 03:58:40 +04:00
|
|
|
#endif
|
|
|
|
#ifdef SUPPORT_UTMP
|
2002-09-16 05:49:06 +04:00
|
|
|
setutent();
|
2002-07-28 03:58:40 +04:00
|
|
|
#endif
|
1994-05-09 07:35:55 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*argv)
|
|
|
|
sel_user = *argv;
|
|
|
|
|
2002-07-28 03:58:40 +04:00
|
|
|
nusers = 0;
|
|
|
|
#ifdef SUPPORT_UTMPX
|
|
|
|
while ((utx = getutxent()) != NULL) {
|
|
|
|
if (utx->ut_type != USER_PROCESS)
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
1994-05-09 07:35:55 +04:00
|
|
|
++nusers;
|
2002-09-16 08:02:21 +04:00
|
|
|
if (sel_user &&
|
2004-11-11 03:09:07 +03:00
|
|
|
strncmp(utx->ut_name, sel_user, sizeof(utx->ut_name)) != 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
1994-05-09 07:35:55 +04:00
|
|
|
if ((ep = calloc(1, sizeof(struct entry))) == NULL)
|
1999-11-09 18:06:30 +03:00
|
|
|
err(1, NULL);
|
2002-07-28 03:58:40 +04:00
|
|
|
(void)memcpy(ep->name, utx->ut_name, sizeof(utx->ut_name));
|
|
|
|
(void)memcpy(ep->line, utx->ut_line, sizeof(utx->ut_line));
|
|
|
|
ep->name[sizeof(utx->ut_name)] = '\0';
|
|
|
|
ep->line[sizeof(utx->ut_line)] = '\0';
|
2002-10-21 14:10:26 +04:00
|
|
|
if (!nflag || getnameinfo((struct sockaddr *)&utx->ut_ss,
|
|
|
|
utx->ut_ss.ss_len, ep->host, sizeof(ep->host), NULL, 0,
|
|
|
|
NI_NUMERICHOST) != 0) {
|
|
|
|
(void)memcpy(ep->host, utx->ut_host,
|
|
|
|
sizeof(utx->ut_host));
|
|
|
|
ep->host[sizeof(utx->ut_host)] = '\0';
|
|
|
|
}
|
2003-02-26 18:04:10 +03:00
|
|
|
ep->type[0] = 'x';
|
2002-07-28 03:58:40 +04:00
|
|
|
ep->tv = utx->ut_tv;
|
2003-02-26 18:01:09 +03:00
|
|
|
ep->pid = utx->ut_pid;
|
1993-03-21 12:45:37 +03:00
|
|
|
*nextp = ep;
|
|
|
|
nextp = &(ep->next);
|
2002-09-16 08:02:21 +04:00
|
|
|
if (wcmd != 0)
|
|
|
|
process(ep);
|
2002-07-28 03:58:40 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SUPPORT_UTMP
|
|
|
|
while ((ut = getutent()) != NULL) {
|
2002-07-29 00:47:57 +04:00
|
|
|
if (ut->ut_name[0] == '\0')
|
|
|
|
continue;
|
2002-09-16 08:02:21 +04:00
|
|
|
|
|
|
|
if (sel_user &&
|
2004-11-11 03:09:07 +03:00
|
|
|
strncmp(ut->ut_name, sel_user, sizeof(ut->ut_name)) != 0)
|
1994-06-30 12:08:08 +04:00
|
|
|
continue;
|
2001-01-05 07:54:53 +03:00
|
|
|
|
2002-07-28 03:58:40 +04:00
|
|
|
/* Don't process entries that we have utmpx for */
|
|
|
|
for (ep = ehead; ep != NULL; ep = ep->next) {
|
|
|
|
if (strncmp(ep->line, ut->ut_line,
|
|
|
|
sizeof(ut->ut_line)) == 0)
|
|
|
|
break;
|
2001-11-05 06:35:49 +03:00
|
|
|
}
|
2002-07-28 03:58:40 +04:00
|
|
|
if (ep != NULL)
|
|
|
|
continue;
|
|
|
|
|
2002-08-22 18:52:55 +04:00
|
|
|
++nusers;
|
2002-07-28 03:58:40 +04:00
|
|
|
if ((ep = calloc(1, sizeof(struct entry))) == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
(void)memcpy(ep->name, ut->ut_name, sizeof(ut->ut_name));
|
|
|
|
(void)memcpy(ep->line, ut->ut_line, sizeof(ut->ut_line));
|
|
|
|
(void)memcpy(ep->host, ut->ut_host, sizeof(ut->ut_host));
|
|
|
|
ep->name[sizeof(ut->ut_name)] = '\0';
|
|
|
|
ep->line[sizeof(ut->ut_line)] = '\0';
|
|
|
|
ep->host[sizeof(ut->ut_host)] = '\0';
|
|
|
|
ep->tv.tv_sec = ut->ut_time;
|
|
|
|
*nextp = ep;
|
|
|
|
nextp = &(ep->next);
|
2002-09-16 08:02:21 +04:00
|
|
|
if (wcmd != 0)
|
|
|
|
process(ep);
|
1994-05-09 07:35:55 +04:00
|
|
|
}
|
2002-07-28 03:58:40 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SUPPORT_UTMPX
|
2002-09-16 05:49:06 +04:00
|
|
|
endutxent();
|
2002-07-28 03:58:40 +04:00
|
|
|
#endif
|
|
|
|
#ifdef SUPPORT_UTMP
|
2002-09-16 05:49:06 +04:00
|
|
|
endutent();
|
2002-07-28 03:58:40 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-09 07:35:55 +04:00
|
|
|
if (header || wcmd == 0) {
|
|
|
|
pr_header(&now, nusers);
|
|
|
|
if (wcmd == 0)
|
|
|
|
exit (0);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-09-05 01:18:33 +04:00
|
|
|
if ((kp = kvm_getproc2(kd, KERN_PROC_ALL, 0,
|
|
|
|
sizeof(struct kinfo_proc2), &nentries)) == NULL)
|
|
|
|
errx(1, "%s", kvm_geterr(kd));
|
2000-07-22 07:14:06 +04:00
|
|
|
|
|
|
|
/* Include trailing space because TTY header starts one column early. */
|
1994-05-09 07:35:55 +04:00
|
|
|
for (i = 0; i < nentries; i++, kp++) {
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2000-05-26 07:10:31 +04:00
|
|
|
if (kp->p_stat == SIDL || kp->p_stat == SZOMB)
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
2000-09-28 22:20:29 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
for (ep = ehead; ep != NULL; ep = ep->next) {
|
2003-02-26 22:10:28 +03:00
|
|
|
if (ep->tdev != 0 && ep->tdev == kp->p_tdev &&
|
|
|
|
kp->p__pgid == kp->p_tpgid) {
|
|
|
|
/*
|
|
|
|
* Proc is in foreground of this
|
|
|
|
* terminal
|
|
|
|
*/
|
|
|
|
if (proc_compare(ep->tp, kp))
|
|
|
|
ep->tp = kp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ep->pid != 0 && ep->pid == kp->p_pid) {
|
|
|
|
ep->pp = kp;
|
2003-02-26 18:01:09 +03:00
|
|
|
break;
|
2000-09-28 22:20:29 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
2000-06-25 17:41:12 +04:00
|
|
|
|
1994-05-09 07:35:55 +04:00
|
|
|
if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
|
2002-10-21 14:18:23 +04:00
|
|
|
ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
|
|
|
|
ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
|
2001-11-05 06:35:49 +03:00
|
|
|
ttywidth = 79;
|
|
|
|
else
|
|
|
|
ttywidth = ws.ws_col - 1;
|
2005-01-08 08:08:53 +03:00
|
|
|
|
2005-03-13 07:08:16 +03:00
|
|
|
if (!wflag && maxhost > (ttywidth / 3))
|
|
|
|
maxhost = ttywidth / 3;
|
2005-01-08 08:08:53 +03:00
|
|
|
|
|
|
|
argwidth = printf("%-*s TTY %-*s %*s IDLE WHAT\n",
|
|
|
|
maxname, "USER", maxhost, "FROM",
|
|
|
|
7 /* "dddhhXm" */, "LOGIN@");
|
|
|
|
argwidth -= sizeof("WHAT\n") - 1 /* NUL */;
|
2000-06-25 17:41:12 +04:00
|
|
|
argwidth = ttywidth - argwidth;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (argwidth < 4)
|
|
|
|
argwidth = 8;
|
2005-01-08 08:08:53 +03:00
|
|
|
if (wflag)
|
|
|
|
argwidth = -1;
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/* sort by idle time */
|
|
|
|
if (sortidle && ehead != NULL) {
|
|
|
|
struct entry *from = ehead, *save;
|
2002-10-21 14:18:23 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
ehead = NULL;
|
|
|
|
while (from != NULL) {
|
1994-05-09 07:35:55 +04:00
|
|
|
for (nextp = &ehead;
|
1993-03-21 12:45:37 +03:00
|
|
|
(*nextp) && from->idle >= (*nextp)->idle;
|
|
|
|
nextp = &(*nextp)->next)
|
1994-05-09 07:35:55 +04:00
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
save = from;
|
|
|
|
from = from->next;
|
|
|
|
save->next = *nextp;
|
|
|
|
*nextp = save;
|
|
|
|
}
|
|
|
|
}
|
2002-07-28 03:58:40 +04:00
|
|
|
#if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
|
|
|
|
else if (ehead != NULL) {
|
|
|
|
struct entry *from = ehead, *save;
|
2002-10-21 14:18:23 +04:00
|
|
|
|
2002-07-28 03:58:40 +04:00
|
|
|
ehead = NULL;
|
|
|
|
while (from != NULL) {
|
|
|
|
for (nextp = &ehead;
|
|
|
|
(*nextp) && strcmp(from->line, (*nextp)->line) > 0;
|
|
|
|
nextp = &(*nextp)->next)
|
|
|
|
continue;
|
|
|
|
save = from;
|
|
|
|
from = from->next;
|
|
|
|
save->next = *nextp;
|
|
|
|
*nextp = save;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2002-10-21 14:18:23 +04:00
|
|
|
|
1998-07-06 10:56:06 +04:00
|
|
|
if (!nflag) {
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = gethostname(domain, sizeof(domain));
|
|
|
|
domain[sizeof(domain) - 1] = '\0';
|
|
|
|
if (rv < 0 || (p = strchr(domain, '.')) == 0)
|
1994-05-09 07:35:55 +04:00
|
|
|
domain[0] = '\0';
|
1998-07-06 10:56:06 +04:00
|
|
|
else
|
1994-05-09 07:35:55 +04:00
|
|
|
memmove(domain, p, strlen(p) + 1);
|
1998-07-06 10:56:06 +04:00
|
|
|
}
|
1994-05-09 07:35:55 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
for (ep = ehead; ep != NULL; ep = ep->next) {
|
2002-07-28 03:58:40 +04:00
|
|
|
char host_buf[MAXHOSTNAMELEN + 1];
|
2001-01-05 07:54:53 +03:00
|
|
|
|
2003-07-12 17:57:49 +04:00
|
|
|
strlcpy(host_buf, ep->host, sizeof(host_buf));
|
2001-01-05 07:54:53 +03:00
|
|
|
p = *host_buf ? host_buf : "-";
|
|
|
|
|
2002-07-28 03:58:40 +04:00
|
|
|
for (x = p; x < p + MAXHOSTNAMELEN; x++)
|
1997-03-31 08:13:15 +04:00
|
|
|
if (*x == '\0' || *x == ':')
|
1997-03-30 03:21:37 +04:00
|
|
|
break;
|
2002-07-28 03:58:40 +04:00
|
|
|
if (x == p + MAXHOSTNAMELEN || *x != ':')
|
1997-03-30 03:21:37 +04:00
|
|
|
x = NULL;
|
1997-03-31 08:13:15 +04:00
|
|
|
else
|
|
|
|
*x++ = '\0';
|
|
|
|
|
1997-05-19 07:44:43 +04:00
|
|
|
if (!nflag && inet_aton(p, &l) &&
|
1994-05-09 07:35:55 +04:00
|
|
|
(hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
|
|
|
|
if (domain[0] != '\0') {
|
|
|
|
p = hp->h_name;
|
|
|
|
p += strlen(hp->h_name);
|
|
|
|
p -= strlen(domain);
|
2001-11-05 06:35:49 +03:00
|
|
|
if (p > hp->h_name &&
|
|
|
|
strcasecmp(p, domain) == 0)
|
1994-05-09 07:35:55 +04:00
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
p = hp->h_name;
|
|
|
|
}
|
|
|
|
if (x) {
|
2001-01-05 07:54:53 +03:00
|
|
|
(void)snprintf(buf, sizeof(buf), "%s:%s", p, x);
|
1994-05-09 07:35:55 +04:00
|
|
|
p = buf;
|
|
|
|
}
|
2005-09-15 04:58:49 +04:00
|
|
|
|
2003-02-26 22:10:28 +03:00
|
|
|
if (ep->tp != NULL)
|
|
|
|
kp = ep->tp;
|
|
|
|
else if (ep->pp != NULL)
|
|
|
|
kp = ep->pp;
|
2006-06-22 21:57:31 +04:00
|
|
|
else if (ep->pid != 0) {
|
2005-09-15 04:58:49 +04:00
|
|
|
if (curtain)
|
|
|
|
kp = NULL;
|
|
|
|
else {
|
|
|
|
warnx("Stale utmp%s entry: %s %s %s",
|
|
|
|
ep->type, ep->name, ep->line, ep->host);
|
|
|
|
continue;
|
|
|
|
}
|
2000-08-21 04:19:06 +04:00
|
|
|
}
|
2005-09-15 04:58:49 +04:00
|
|
|
usrnp = (kp == NULL) ? ep->name : kp->p_login;
|
2004-11-10 19:58:10 +03:00
|
|
|
(void)printf("%-*s %-7.7s %-*.*s ",
|
2005-09-15 04:58:49 +04:00
|
|
|
maxname, usrnp, ep->line,
|
2005-01-08 08:08:53 +03:00
|
|
|
maxhost, maxhost, *p ? p : "-");
|
2002-07-28 03:58:40 +04:00
|
|
|
then = (time_t)ep->tv.tv_sec;
|
|
|
|
pr_attime(&then, &now);
|
1994-05-09 07:35:55 +04:00
|
|
|
pr_idle(ep->idle);
|
2003-02-26 22:10:28 +03:00
|
|
|
pr_args(kp);
|
2000-06-25 17:41:12 +04:00
|
|
|
(void)printf("\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1995-05-21 17:42:48 +04:00
|
|
|
static void
|
2001-01-05 07:59:21 +03:00
|
|
|
pr_args(struct kinfo_proc2 *kp)
|
1995-05-21 17:42:48 +04:00
|
|
|
{
|
|
|
|
char **argv;
|
|
|
|
int left;
|
|
|
|
|
1995-05-25 08:02:19 +04:00
|
|
|
if (kp == 0)
|
|
|
|
goto nothing;
|
1995-05-21 17:42:48 +04:00
|
|
|
left = argwidth;
|
2005-09-05 01:18:33 +04:00
|
|
|
argv = kvm_getargv2(kd, kp, (argwidth < 0) ? 0 : argwidth);
|
2003-02-26 22:01:54 +03:00
|
|
|
if (argv == 0) {
|
|
|
|
if (kp->p_comm == 0) {
|
|
|
|
goto nothing;
|
|
|
|
} else {
|
|
|
|
fmt_putc('(', &left);
|
|
|
|
fmt_puts((char *)kp->p_comm, &left);
|
|
|
|
fmt_putc(')', &left);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2005-09-05 01:18:33 +04:00
|
|
|
while (*argv) {
|
|
|
|
fmt_puts(*argv, &left);
|
|
|
|
argv++;
|
|
|
|
fmt_putc(' ', &left);
|
1995-05-21 17:42:48 +04:00
|
|
|
}
|
1995-05-25 08:02:19 +04:00
|
|
|
return;
|
|
|
|
nothing:
|
|
|
|
putchar('-');
|
1995-05-21 17:42:48 +04:00
|
|
|
}
|
|
|
|
|
1994-05-09 07:35:55 +04:00
|
|
|
static void
|
2001-01-05 07:59:21 +03:00
|
|
|
pr_header(time_t *nowp, int nusers)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-05-09 07:35:55 +04:00
|
|
|
double avenrun[3];
|
|
|
|
time_t uptime;
|
|
|
|
int days, hrs, i, mins;
|
|
|
|
int mib[2];
|
|
|
|
size_t size;
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print time of day.
|
|
|
|
*
|
|
|
|
* SCCS forces the string manipulation below, as it replaces
|
|
|
|
* %, M, and % in a character string with the file name.
|
|
|
|
*/
|
2000-12-20 04:17:49 +03:00
|
|
|
(void)strftime(buf, sizeof(buf), "%l:%" "M%p", localtime(nowp));
|
1997-04-13 17:44:13 +04:00
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
1994-05-09 07:35:55 +04:00
|
|
|
(void)printf("%s ", buf);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-09 07:35:55 +04:00
|
|
|
/*
|
|
|
|
* Print how long system has been up.
|
|
|
|
* (Found by looking getting "boottime" from the kernel)
|
|
|
|
*/
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_BOOTTIME;
|
|
|
|
size = sizeof(boottime);
|
|
|
|
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
|
|
|
|
boottime.tv_sec != 0) {
|
|
|
|
uptime = now - boottime.tv_sec;
|
|
|
|
uptime += 30;
|
1997-07-08 00:30:08 +04:00
|
|
|
if (uptime > SECSPERMIN) {
|
|
|
|
days = uptime / SECSPERDAY;
|
|
|
|
uptime %= SECSPERDAY;
|
|
|
|
hrs = uptime / SECSPERHOUR;
|
|
|
|
uptime %= SECSPERHOUR;
|
|
|
|
mins = uptime / SECSPERMIN;
|
|
|
|
(void)printf(" up");
|
|
|
|
if (days > 0)
|
|
|
|
(void)printf(" %d day%s,", days,
|
|
|
|
days > 1 ? "s" : "");
|
|
|
|
if (hrs > 0 && mins > 0)
|
|
|
|
(void)printf(" %2d:%02d,", hrs, mins);
|
|
|
|
else {
|
|
|
|
if (hrs > 0)
|
|
|
|
(void)printf(" %d hr%s,",
|
|
|
|
hrs, hrs > 1 ? "s" : "");
|
|
|
|
if (mins > 0)
|
|
|
|
(void)printf(" %d min%s,",
|
|
|
|
mins, mins > 1 ? "s" : "");
|
|
|
|
}
|
1994-05-09 07:35:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print number of users logged in to system */
|
1995-03-28 16:48:45 +04:00
|
|
|
(void)printf(" %d user%s", nusers, nusers != 1 ? "s" : "");
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-09 07:35:55 +04:00
|
|
|
/*
|
|
|
|
* Print 1, 5, and 15 minute load averages.
|
|
|
|
*/
|
|
|
|
if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
|
|
|
|
(void)printf(", no load average information available\n");
|
|
|
|
else {
|
|
|
|
(void)printf(", load averages:");
|
|
|
|
for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
|
|
|
|
if (i > 0)
|
|
|
|
(void)printf(",");
|
|
|
|
(void)printf(" %.2f", avenrun[i]);
|
|
|
|
}
|
|
|
|
(void)printf("\n");
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2002-07-28 03:58:40 +04:00
|
|
|
#if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
|
2004-11-19 16:17:06 +03:00
|
|
|
static int
|
|
|
|
ttystat(const char *line, struct stat *st)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-05-09 07:35:55 +04:00
|
|
|
char ttybuf[MAXPATHLEN];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-07-28 03:58:40 +04:00
|
|
|
(void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
|
2004-11-19 16:17:06 +03:00
|
|
|
return stat(ttybuf, st);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2002-07-28 03:58:40 +04:00
|
|
|
static void
|
|
|
|
process(struct entry *ep)
|
|
|
|
{
|
2004-11-19 16:17:06 +03:00
|
|
|
struct stat st;
|
2002-07-28 03:58:40 +04:00
|
|
|
time_t touched;
|
|
|
|
int max;
|
|
|
|
|
|
|
|
if ((max = strlen(ep->name)) > maxname)
|
|
|
|
maxname = max;
|
|
|
|
if ((max = strlen(ep->line)) > maxline)
|
|
|
|
maxline = max;
|
|
|
|
if ((max = strlen(ep->host)) > maxhost)
|
|
|
|
maxhost = max;
|
|
|
|
|
2004-11-19 16:17:06 +03:00
|
|
|
ep->tdev = 0;
|
|
|
|
ep->idle = (time_t)-1;
|
2002-07-28 03:58:40 +04:00
|
|
|
|
2003-02-26 18:01:09 +03:00
|
|
|
#ifdef SUPPORT_UTMP
|
2002-07-28 03:58:40 +04:00
|
|
|
/*
|
|
|
|
* Hack to recognize and correctly parse
|
|
|
|
* ut entry made by ftpd. The "tty" used
|
|
|
|
* by ftpd is not a real tty, just identifier in
|
2004-11-19 16:17:06 +03:00
|
|
|
* form ftpPID. Pid parsed from the "tty name"
|
2002-07-28 03:58:40 +04:00
|
|
|
* is used later to match corresponding process.
|
2003-02-26 18:01:09 +03:00
|
|
|
* NB: This is only used for utmp entries. For utmpx,
|
|
|
|
* we already have the pid.
|
2002-07-28 03:58:40 +04:00
|
|
|
*/
|
2003-02-26 18:01:09 +03:00
|
|
|
if (ep->pid == 0 && strncmp(ep->line, "ftp", 3) == 0) {
|
|
|
|
ep->pid = strtol(ep->line + 3, NULL, 10);
|
2002-07-28 03:58:40 +04:00
|
|
|
return;
|
|
|
|
}
|
2003-02-26 18:01:09 +03:00
|
|
|
#endif
|
2004-11-19 16:17:06 +03:00
|
|
|
if (ttystat(ep->line, &st) == -1)
|
2003-02-26 18:01:09 +03:00
|
|
|
return;
|
2002-07-28 03:58:40 +04:00
|
|
|
|
2004-11-19 16:17:06 +03:00
|
|
|
ep->tdev = st.st_rdev;
|
2002-07-28 03:58:40 +04:00
|
|
|
/*
|
|
|
|
* If this is the console device, attempt to ascertain
|
|
|
|
* the true console device dev_t.
|
|
|
|
*/
|
|
|
|
if (ep->tdev == 0) {
|
|
|
|
int mib[2];
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_CONSDEV;
|
|
|
|
size = sizeof(dev_t);
|
|
|
|
(void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2004-11-19 16:17:06 +03:00
|
|
|
touched = st.st_atime;
|
2002-07-28 03:58:40 +04:00
|
|
|
if (touched < ep->tv.tv_sec) {
|
|
|
|
/* tty untouched since before login */
|
|
|
|
touched = ep->tv.tv_sec;
|
|
|
|
}
|
|
|
|
if ((ep->idle = now - touched) < 0)
|
|
|
|
ep->idle = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1994-05-09 07:35:55 +04:00
|
|
|
static void
|
2001-01-05 07:59:21 +03:00
|
|
|
usage(int wcmd)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-11-05 06:35:49 +03:00
|
|
|
|
1994-05-09 07:35:55 +04:00
|
|
|
if (wcmd)
|
|
|
|
(void)fprintf(stderr,
|
2004-12-22 20:20:30 +03:00
|
|
|
"Usage: %s [-hinw] [-M core] [-N system] [user]\n",
|
|
|
|
getprogname());
|
1994-05-09 07:35:55 +04:00
|
|
|
else
|
2004-12-22 20:20:30 +03:00
|
|
|
(void)fprintf(stderr, "Usage: %s\n", getprogname());
|
1998-07-06 10:56:06 +04:00
|
|
|
exit(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|