1994-05-27 12:33:43 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1980, 1991, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 4. Neither the name of the University 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 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.
|
|
|
|
*/
|
|
|
|
|
2001-12-11 22:18:47 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
1997-08-25 06:42:19 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
2001-12-11 22:18:47 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#ifndef lint
|
2001-12-11 22:18:47 +00:00
|
|
|
static const char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94";
|
1997-08-25 06:42:19 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* w - print system status (who and what)
|
|
|
|
*
|
|
|
|
* This program is similar to the systat command on Tenex/Tops 10/20
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/user.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/tty.h>
|
|
|
|
|
2013-07-28 18:44:17 +00:00
|
|
|
#include <machine/cpu.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2001-12-11 22:18:47 +00:00
|
|
|
#include <arpa/nameser.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <ctype.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <kvm.h>
|
2001-03-02 23:11:38 +00:00
|
|
|
#include <langinfo.h>
|
2014-10-08 05:04:31 +00:00
|
|
|
#include <libgen.h>
|
2001-07-26 19:20:13 +00:00
|
|
|
#include <libutil.h>
|
2001-05-26 00:02:13 +00:00
|
|
|
#include <limits.h>
|
2001-03-02 23:11:38 +00:00
|
|
|
#include <locale.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <nlist.h>
|
|
|
|
#include <paths.h>
|
2001-12-11 22:18:47 +00:00
|
|
|
#include <resolv.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <timeconv.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <unistd.h>
|
2010-01-13 18:09:21 +00:00
|
|
|
#include <utmpx.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <vis.h>
|
2014-11-05 23:54:33 +00:00
|
|
|
#include <libxo/xo.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include "extern.h"
|
|
|
|
|
2011-11-06 08:18:47 +00:00
|
|
|
static struct utmpx *utmp;
|
|
|
|
static struct winsize ws;
|
|
|
|
static kvm_t *kd;
|
|
|
|
static time_t now; /* the current time of day */
|
|
|
|
static int ttywidth; /* width of tty */
|
|
|
|
static int argwidth; /* width of tty */
|
|
|
|
static int header = 1; /* true if -h flag: don't print heading */
|
|
|
|
static int nflag; /* true if -n flag: don't convert addrs */
|
|
|
|
static int dflag; /* true if -d flag: output debug info */
|
|
|
|
static int sortidle; /* sort by idle time */
|
2001-03-02 23:11:38 +00:00
|
|
|
int use_ampm; /* use AM/PM time */
|
2011-11-06 08:18:47 +00:00
|
|
|
static int use_comma; /* use comma as floats separator */
|
|
|
|
static char **sel_users; /* login array of particular users selected */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* One of these per active utmp entry.
|
|
|
|
*/
|
2011-11-06 08:18:47 +00:00
|
|
|
static struct entry {
|
1994-05-27 12:33:43 +00:00
|
|
|
struct entry *next;
|
2009-12-06 01:10:30 +00:00
|
|
|
struct utmpx utmp;
|
1997-09-12 02:22:21 +00:00
|
|
|
dev_t tdev; /* dev_t of terminal */
|
|
|
|
time_t idle; /* idle time of terminal in seconds */
|
|
|
|
struct kinfo_proc *kp; /* `most interesting' proc */
|
|
|
|
char *args; /* arg list of interesting process */
|
|
|
|
struct kinfo_proc *dkp; /* debug option proc list */
|
1994-05-27 12:33:43 +00:00
|
|
|
} *ep, *ehead = NULL, **nextp = &ehead;
|
|
|
|
|
2010-01-05 21:00:23 +00:00
|
|
|
#define debugproc(p) *(&((struct kinfo_proc *)p)->ki_udata)
|
1999-07-28 19:29:46 +00:00
|
|
|
|
2009-12-06 01:10:30 +00:00
|
|
|
#define W_DISPUSERSIZE 10
|
|
|
|
#define W_DISPLINESIZE 8
|
|
|
|
#define W_DISPHOSTSIZE 24
|
2001-07-26 19:20:13 +00:00
|
|
|
|
2002-03-22 01:42:45 +00:00
|
|
|
static void pr_header(time_t *, int);
|
2009-12-06 01:10:30 +00:00
|
|
|
static struct stat *ttystat(char *);
|
2002-03-22 01:42:45 +00:00
|
|
|
static void usage(int);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2013-01-19 00:21:55 +00:00
|
|
|
char *fmt_argv(char **, char *, char *, size_t); /* ../../bin/ps/fmt.c */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int
|
2002-06-07 01:41:54 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
struct kinfo_proc *kp;
|
1997-09-12 02:22:21 +00:00
|
|
|
struct kinfo_proc *dkp;
|
1994-05-27 12:33:43 +00:00
|
|
|
struct stat *stp;
|
2000-12-21 01:30:47 +00:00
|
|
|
time_t touched;
|
2009-08-30 11:17:42 +00:00
|
|
|
int ch, i, nentries, nusers, wcmd, longidle, longattime, dropgid;
|
2014-11-05 23:54:33 +00:00
|
|
|
const char *memf, *nlistf, *p, *save_p;
|
2001-12-11 22:18:47 +00:00
|
|
|
char *x_suffix;
|
2001-05-26 00:02:13 +00:00
|
|
|
char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
|
2001-07-26 19:20:13 +00:00
|
|
|
char fn[MAXHOSTNAMELEN];
|
|
|
|
char *dot;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1999-07-28 19:29:46 +00:00
|
|
|
(void)setlocale(LC_ALL, "");
|
2001-03-02 23:11:38 +00:00
|
|
|
use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
|
2001-03-03 16:47:07 +00:00
|
|
|
use_comma = (*nl_langinfo(RADIXCHAR) != ',');
|
1995-10-24 03:06:09 +00:00
|
|
|
|
2014-11-05 23:54:33 +00:00
|
|
|
argc = xo_parse_args(argc, argv);
|
|
|
|
if (argc < 0)
|
|
|
|
exit(1);
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/* Are we w(1) or uptime(1)? */
|
2014-10-08 05:04:31 +00:00
|
|
|
if (strcmp(basename(argv[0]), "uptime") == 0) {
|
1994-05-27 12:33:43 +00:00
|
|
|
wcmd = 0;
|
|
|
|
p = "";
|
|
|
|
} else {
|
|
|
|
wcmd = 1;
|
1997-09-12 02:22:21 +00:00
|
|
|
p = "dhiflM:N:nsuw";
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
1999-11-17 15:28:45 +00:00
|
|
|
dropgid = 0;
|
2010-02-08 21:23:48 +00:00
|
|
|
memf = _PATH_DEVNULL;
|
|
|
|
nlistf = NULL;
|
1997-03-29 04:34:07 +00:00
|
|
|
while ((ch = getopt(argc, argv, p)) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch (ch) {
|
1997-09-12 02:22:21 +00:00
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'h':
|
|
|
|
header = 0;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
sortidle = 1;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
header = 0;
|
|
|
|
memf = optarg;
|
1999-11-17 15:28:45 +00:00
|
|
|
dropgid = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
nlistf = optarg;
|
1999-11-17 15:28:45 +00:00
|
|
|
dropgid = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nflag = 1;
|
|
|
|
break;
|
|
|
|
case 'f': case 'l': case 's': case 'u': case 'w':
|
|
|
|
warnx("[-flsuw] no longer supported");
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage(wcmd);
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
1995-07-18 05:07:02 +00:00
|
|
|
if (!(_res.options & RES_INIT))
|
|
|
|
res_init();
|
|
|
|
_res.retrans = 2; /* resolver timeout to 2 seconds per try */
|
|
|
|
_res.retry = 1; /* only try once.. */
|
|
|
|
|
1996-05-30 02:20:10 +00:00
|
|
|
/*
|
|
|
|
* Discard setgid privileges if not the running kernel so that bad
|
|
|
|
* guys can't print interesting stuff from kernel memory.
|
|
|
|
*/
|
1999-11-17 15:28:45 +00:00
|
|
|
if (dropgid)
|
1996-05-30 02:20:10 +00:00
|
|
|
setgid(getgid());
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
|
|
|
|
errx(1, "%s", errbuf);
|
|
|
|
|
|
|
|
(void)time(&now);
|
|
|
|
|
|
|
|
if (*argv)
|
1999-07-28 19:29:46 +00:00
|
|
|
sel_users = argv;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2009-12-06 01:10:30 +00:00
|
|
|
setutxent();
|
|
|
|
for (nusers = 0; (utmp = getutxent()) != NULL;) {
|
|
|
|
if (utmp->ut_type != USER_PROCESS)
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
2009-12-06 01:10:30 +00:00
|
|
|
if (!(stp = ttystat(utmp->ut_line)))
|
1999-03-31 21:01:39 +00:00
|
|
|
continue; /* corrupted record */
|
1994-05-27 12:33:43 +00:00
|
|
|
++nusers;
|
1999-07-28 19:29:46 +00:00
|
|
|
if (wcmd == 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
1999-07-28 19:29:46 +00:00
|
|
|
if (sel_users) {
|
|
|
|
int usermatch;
|
|
|
|
char **user;
|
|
|
|
|
|
|
|
usermatch = 0;
|
|
|
|
for (user = sel_users; !usermatch && *user; user++)
|
2009-12-06 01:10:30 +00:00
|
|
|
if (!strcmp(utmp->ut_user, *user))
|
1999-07-28 19:29:46 +00:00
|
|
|
usermatch = 1;
|
|
|
|
if (!usermatch)
|
|
|
|
continue;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
if ((ep = calloc(1, sizeof(struct entry))) == NULL)
|
1997-08-25 06:42:19 +00:00
|
|
|
errx(1, "calloc");
|
1994-05-27 12:33:43 +00:00
|
|
|
*nextp = ep;
|
1999-07-28 19:29:46 +00:00
|
|
|
nextp = &ep->next;
|
2009-12-06 01:10:30 +00:00
|
|
|
memmove(&ep->utmp, utmp, sizeof *utmp);
|
1994-05-27 12:33:43 +00:00
|
|
|
ep->tdev = stp->st_rdev;
|
|
|
|
/*
|
|
|
|
* If this is the console device, attempt to ascertain
|
|
|
|
* the true console device dev_t.
|
|
|
|
*/
|
|
|
|
if (ep->tdev == 0) {
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
size = sizeof(dev_t);
|
2006-05-11 17:25:36 +00:00
|
|
|
(void)sysctlbyname("machdep.consdev", &ep->tdev, &size, NULL, 0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2000-12-21 01:30:47 +00:00
|
|
|
touched = stp->st_atime;
|
2009-12-06 01:10:30 +00:00
|
|
|
if (touched < ep->utmp.ut_tv.tv_sec) {
|
2000-12-21 01:30:47 +00:00
|
|
|
/* tty untouched since before login */
|
2009-12-06 01:10:30 +00:00
|
|
|
touched = ep->utmp.ut_tv.tv_sec;
|
2000-12-21 01:30:47 +00:00
|
|
|
}
|
|
|
|
if ((ep->idle = now - touched) < 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
ep->idle = 0;
|
|
|
|
}
|
2009-12-06 01:10:30 +00:00
|
|
|
endutxent();
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_open_container("uptime-information");
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
if (header || wcmd == 0) {
|
|
|
|
pr_header(&now, nusers);
|
2001-05-28 21:30:31 +00:00
|
|
|
if (wcmd == 0) {
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_close_container("uptime-information");
|
2001-05-28 21:30:31 +00:00
|
|
|
(void)kvm_close(kd);
|
1999-07-28 19:29:46 +00:00
|
|
|
exit(0);
|
2001-05-28 21:30:31 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1998-05-21 08:46:48 +00:00
|
|
|
#define HEADER_USER "USER"
|
|
|
|
#define HEADER_TTY "TTY"
|
|
|
|
#define HEADER_FROM "FROM"
|
|
|
|
#define HEADER_LOGIN_IDLE "LOGIN@ IDLE "
|
|
|
|
#define HEADER_WHAT "WHAT\n"
|
2009-12-06 01:10:30 +00:00
|
|
|
#define WUSED (W_DISPUSERSIZE + W_DISPLINESIZE + W_DISPHOSTSIZE + \
|
1998-05-21 08:46:48 +00:00
|
|
|
sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%s}",
|
2009-12-06 01:10:30 +00:00
|
|
|
W_DISPUSERSIZE, W_DISPUSERSIZE, HEADER_USER,
|
|
|
|
W_DISPLINESIZE, W_DISPLINESIZE, HEADER_TTY,
|
2001-07-26 19:20:13 +00:00
|
|
|
W_DISPHOSTSIZE, W_DISPHOSTSIZE, HEADER_FROM,
|
1998-05-21 08:46:48 +00:00
|
|
|
HEADER_LOGIN_IDLE HEADER_WHAT);
|
1995-07-17 04:38:30 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
|
|
|
|
err(1, "%s", kvm_geterr(kd));
|
|
|
|
for (i = 0; i < nentries; i++, kp++) {
|
2010-01-13 18:09:21 +00:00
|
|
|
if (kp->ki_stat == SIDL || kp->ki_stat == SZOMB ||
|
|
|
|
kp->ki_tdev == NODEV)
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
|
|
|
for (ep = ehead; ep != NULL; ep = ep->next) {
|
Change the proc information returned from the kernel so that it
no longer contains kernel specific data structures, but rather
only scalar values and structures that are already part of the
kernel/user interface, specifically rusage and rtprio. It no
longer contains proc, session, pcred, ucred, procsig, vmspace,
pstats, mtx, sigiolst, klist, callout, pasleep, or mdproc. If
any of these changed in size, ps, w, fstat, gcore, systat, and
top would all stop working. The new structure has over 200 bytes
of unassigned space for future values to be added, yet is nearly
100 bytes smaller per entry than the structure that it replaced.
2000-12-12 07:25:57 +00:00
|
|
|
if (ep->tdev == kp->ki_tdev) {
|
1997-09-12 02:22:21 +00:00
|
|
|
/*
|
|
|
|
* proc is associated with this terminal
|
|
|
|
*/
|
Change the proc information returned from the kernel so that it
no longer contains kernel specific data structures, but rather
only scalar values and structures that are already part of the
kernel/user interface, specifically rusage and rtprio. It no
longer contains proc, session, pcred, ucred, procsig, vmspace,
pstats, mtx, sigiolst, klist, callout, pasleep, or mdproc. If
any of these changed in size, ps, w, fstat, gcore, systat, and
top would all stop working. The new structure has over 200 bytes
of unassigned space for future values to be added, yet is nearly
100 bytes smaller per entry than the structure that it replaced.
2000-12-12 07:25:57 +00:00
|
|
|
if (ep->kp == NULL && kp->ki_pgid == kp->ki_tpgid) {
|
1997-09-12 02:22:21 +00:00
|
|
|
/*
|
|
|
|
* Proc is 'most interesting'
|
|
|
|
*/
|
Change the proc information returned from the kernel so that it
no longer contains kernel specific data structures, but rather
only scalar values and structures that are already part of the
kernel/user interface, specifically rusage and rtprio. It no
longer contains proc, session, pcred, ucred, procsig, vmspace,
pstats, mtx, sigiolst, klist, callout, pasleep, or mdproc. If
any of these changed in size, ps, w, fstat, gcore, systat, and
top would all stop working. The new structure has over 200 bytes
of unassigned space for future values to be added, yet is nearly
100 bytes smaller per entry than the structure that it replaced.
2000-12-12 07:25:57 +00:00
|
|
|
if (proc_compare(ep->kp, kp))
|
1997-09-12 02:22:21 +00:00
|
|
|
ep->kp = kp;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
1997-09-12 02:22:21 +00:00
|
|
|
* Proc debug option info; add to debug
|
Change the proc information returned from the kernel so that it
no longer contains kernel specific data structures, but rather
only scalar values and structures that are already part of the
kernel/user interface, specifically rusage and rtprio. It no
longer contains proc, session, pcred, ucred, procsig, vmspace,
pstats, mtx, sigiolst, klist, callout, pasleep, or mdproc. If
any of these changed in size, ps, w, fstat, gcore, systat, and
top would all stop working. The new structure has over 200 bytes
of unassigned space for future values to be added, yet is nearly
100 bytes smaller per entry than the structure that it replaced.
2000-12-12 07:25:57 +00:00
|
|
|
* list using kinfo_proc ki_spare[0]
|
1997-09-12 02:22:21 +00:00
|
|
|
* as next pointer; ptr to ptr avoids the
|
|
|
|
* ptr = long assumption.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
1997-09-12 02:22:21 +00:00
|
|
|
dkp = ep->dkp;
|
|
|
|
ep->dkp = kp;
|
1999-07-28 19:29:46 +00:00
|
|
|
debugproc(kp) = dkp;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
|
|
|
|
ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
|
|
|
|
ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
|
|
|
|
ttywidth = 79;
|
|
|
|
else
|
|
|
|
ttywidth = ws.ws_col - 1;
|
|
|
|
argwidth = ttywidth - WUSED;
|
|
|
|
if (argwidth < 4)
|
|
|
|
argwidth = 8;
|
|
|
|
for (ep = ehead; ep != NULL; ep = ep->next) {
|
|
|
|
if (ep->kp == NULL) {
|
2001-12-11 22:18:47 +00:00
|
|
|
ep->args = strdup("-");
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth),
|
2013-01-19 00:21:55 +00:00
|
|
|
ep->kp->ki_comm, NULL, MAXCOMLEN);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (ep->args == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
}
|
|
|
|
/* sort by idle time */
|
|
|
|
if (sortidle && ehead != NULL) {
|
1999-07-28 19:29:46 +00:00
|
|
|
struct entry *from, *save;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1999-07-28 19:29:46 +00:00
|
|
|
from = ehead;
|
1994-05-27 12:33:43 +00:00
|
|
|
ehead = NULL;
|
|
|
|
while (from != NULL) {
|
|
|
|
for (nextp = &ehead;
|
|
|
|
(*nextp) && from->idle >= (*nextp)->idle;
|
|
|
|
nextp = &(*nextp)->next)
|
|
|
|
continue;
|
|
|
|
save = from;
|
|
|
|
from = from->next;
|
|
|
|
save->next = *nextp;
|
|
|
|
*nextp = save;
|
|
|
|
}
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_open_container("user-table");
|
|
|
|
xo_open_list("user-entry");
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
for (ep = ehead; ep != NULL; ep = ep->next) {
|
2009-11-22 16:51:44 +00:00
|
|
|
struct addrinfo hints, *res;
|
2001-07-26 19:20:13 +00:00
|
|
|
struct sockaddr_storage ss;
|
|
|
|
struct sockaddr *sa = (struct sockaddr *)&ss;
|
2001-12-11 22:18:47 +00:00
|
|
|
struct sockaddr_in *lsin = (struct sockaddr_in *)&ss;
|
|
|
|
struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)&ss;
|
2003-06-28 19:11:42 +00:00
|
|
|
time_t t;
|
2001-07-26 19:20:13 +00:00
|
|
|
int isaddr;
|
1999-04-22 23:40:56 +00:00
|
|
|
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_open_instance("user-entry");
|
|
|
|
|
|
|
|
save_p = p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
|
2001-07-26 19:20:13 +00:00
|
|
|
if ((x_suffix = strrchr(p, ':')) != NULL) {
|
|
|
|
if ((dot = strchr(x_suffix, '.')) != NULL &&
|
|
|
|
strchr(dot+1, '.') == NULL)
|
|
|
|
*x_suffix++ = '\0';
|
|
|
|
else
|
|
|
|
x_suffix = NULL;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2009-11-22 16:51:44 +00:00
|
|
|
|
|
|
|
isaddr = 0;
|
|
|
|
memset(&ss, '\0', sizeof(ss));
|
|
|
|
if (inet_pton(AF_INET6, p, &lsin6->sin6_addr) == 1) {
|
|
|
|
lsin6->sin6_len = sizeof(*lsin6);
|
|
|
|
lsin6->sin6_family = AF_INET6;
|
|
|
|
isaddr = 1;
|
|
|
|
} else if (inet_pton(AF_INET, p, &lsin->sin_addr) == 1) {
|
|
|
|
lsin->sin_len = sizeof(*lsin);
|
|
|
|
lsin->sin_family = AF_INET;
|
|
|
|
isaddr = 1;
|
|
|
|
}
|
2001-07-26 19:20:13 +00:00
|
|
|
if (!nflag) {
|
|
|
|
/* Attempt to change an IP address into a name */
|
|
|
|
if (isaddr && realhostname_sa(fn, sizeof(fn), sa,
|
|
|
|
sa->sa_len) == HOSTNAME_FOUND)
|
|
|
|
p = fn;
|
2009-11-22 16:51:44 +00:00
|
|
|
} else if (!isaddr) {
|
|
|
|
/*
|
|
|
|
* If a host has only one A/AAAA RR, change a
|
|
|
|
* name into an IP address
|
|
|
|
*/
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
if (getaddrinfo(p, NULL, &hints, &res) == 0) {
|
|
|
|
if (res->ai_next == NULL &&
|
|
|
|
getnameinfo(res->ai_addr, res->ai_addrlen,
|
|
|
|
fn, sizeof(fn), NULL, 0,
|
|
|
|
NI_NUMERICHOST) == 0)
|
|
|
|
p = fn;
|
|
|
|
freeaddrinfo(res);
|
|
|
|
}
|
1996-06-17 15:46:25 +00:00
|
|
|
}
|
2009-11-22 16:51:44 +00:00
|
|
|
|
2001-07-26 19:20:13 +00:00
|
|
|
if (x_suffix) {
|
|
|
|
(void)snprintf(buf, sizeof(buf), "%s:%s", p, x_suffix);
|
1994-05-27 12:33:43 +00:00
|
|
|
p = buf;
|
|
|
|
}
|
1999-01-10 15:28:37 +00:00
|
|
|
if (dflag) {
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_open_container("process-table");
|
|
|
|
xo_open_list("process-entry");
|
|
|
|
|
1999-07-28 19:29:46 +00:00
|
|
|
for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
|
2001-12-11 22:18:47 +00:00
|
|
|
const char *ptr;
|
1999-07-28 19:29:46 +00:00
|
|
|
|
1999-03-31 21:01:39 +00:00
|
|
|
ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth),
|
2013-01-19 00:21:55 +00:00
|
|
|
dkp->ki_comm, NULL, MAXCOMLEN);
|
1999-03-31 21:01:39 +00:00
|
|
|
if (ptr == NULL)
|
|
|
|
ptr = "-";
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_open_instance("process-entry");
|
|
|
|
xo_emit("\t\t{:process-id/%-9d/%d} {:command/%s}\n",
|
Change the proc information returned from the kernel so that it
no longer contains kernel specific data structures, but rather
only scalar values and structures that are already part of the
kernel/user interface, specifically rusage and rtprio. It no
longer contains proc, session, pcred, ucred, procsig, vmspace,
pstats, mtx, sigiolst, klist, callout, pasleep, or mdproc. If
any of these changed in size, ps, w, fstat, gcore, systat, and
top would all stop working. The new structure has over 200 bytes
of unassigned space for future values to be added, yet is nearly
100 bytes smaller per entry than the structure that it replaced.
2000-12-12 07:25:57 +00:00
|
|
|
dkp->ki_pid, ptr);
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_close_instance("process-entry");
|
1997-09-12 02:22:21 +00:00
|
|
|
}
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_close_list("process-entry");
|
|
|
|
xo_close_container("process-table");
|
1997-09-12 02:22:21 +00:00
|
|
|
}
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_emit("{:user/%-*.*s/%@**@s} {:tty/%-*.*s/%@**@s} ",
|
|
|
|
W_DISPUSERSIZE, W_DISPUSERSIZE, ep->utmp.ut_user,
|
|
|
|
W_DISPLINESIZE, W_DISPLINESIZE,
|
|
|
|
*ep->utmp.ut_line ?
|
|
|
|
(strncmp(ep->utmp.ut_line, "tty", 3) &&
|
|
|
|
strncmp(ep->utmp.ut_line, "cua", 3) ?
|
|
|
|
ep->utmp.ut_line : ep->utmp.ut_line + 3) : "-");
|
|
|
|
|
|
|
|
if (save_p && save_p != p)
|
|
|
|
xo_attr("address", "%s", save_p);
|
|
|
|
xo_emit("{:from/%-*.*s/%@**@s} ",
|
2001-07-26 19:20:13 +00:00
|
|
|
W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-");
|
2009-12-06 01:10:30 +00:00
|
|
|
t = ep->utmp.ut_tv.tv_sec;
|
2009-08-30 11:17:42 +00:00
|
|
|
longattime = pr_attime(&t, &now);
|
1999-01-10 15:28:37 +00:00
|
|
|
longidle = pr_idle(ep->idle);
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_emit("{:command/%.*s/%@*@s}\n",
|
|
|
|
argwidth - longidle - longattime,
|
2009-08-30 11:17:42 +00:00
|
|
|
ep->args);
|
2014-11-05 23:54:33 +00:00
|
|
|
|
|
|
|
xo_close_instance("user-entry");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2014-11-05 23:54:33 +00:00
|
|
|
|
|
|
|
xo_close_list("user-entry");
|
|
|
|
xo_close_container("user-table");
|
|
|
|
xo_close_container("uptime-information");
|
|
|
|
xo_finish();
|
|
|
|
|
2001-05-28 21:30:31 +00:00
|
|
|
(void)kvm_close(kd);
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-06-07 01:41:54 +00:00
|
|
|
pr_header(time_t *nowp, int nusers)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
double avenrun[3];
|
2002-03-07 22:36:56 +00:00
|
|
|
time_t uptime;
|
2005-10-17 15:37:22 +00:00
|
|
|
struct timespec tp;
|
1997-10-13 15:17:03 +00:00
|
|
|
int days, hrs, i, mins, secs;
|
1994-05-27 12:33:43 +00:00
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print time of day.
|
|
|
|
*/
|
2003-09-07 16:31:32 +00:00
|
|
|
if (strftime(buf, sizeof(buf),
|
|
|
|
use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)) != 0)
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_emit("{:time-of-day/%s} ", buf);
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Print how long system has been up.
|
|
|
|
*/
|
2012-10-12 15:03:28 +00:00
|
|
|
if (clock_gettime(CLOCK_UPTIME, &tp) != -1) {
|
2005-10-17 15:37:22 +00:00
|
|
|
uptime = tp.tv_sec;
|
2002-03-07 22:36:56 +00:00
|
|
|
if (uptime > 60)
|
|
|
|
uptime += 30;
|
|
|
|
days = uptime / 86400;
|
|
|
|
uptime %= 86400;
|
|
|
|
hrs = uptime / 3600;
|
|
|
|
uptime %= 3600;
|
|
|
|
mins = uptime / 60;
|
|
|
|
secs = uptime % 60;
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_emit(" up");
|
|
|
|
xo_attr("seconds", "%lu", (unsigned long) tp.tv_sec);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (days > 0)
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_emit(" {:uptime/%d day%s},",
|
|
|
|
days, days > 1 ? "s" : "");
|
1994-05-27 12:33:43 +00:00
|
|
|
if (hrs > 0 && mins > 0)
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_emit(" {:uptime/%2d:%02d},", hrs, mins);
|
1997-10-13 15:17:03 +00:00
|
|
|
else if (hrs > 0)
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_emit(" {:uptime/%d hr%s},",
|
|
|
|
hrs, hrs > 1 ? "s" : "");
|
1997-10-13 15:17:03 +00:00
|
|
|
else if (mins > 0)
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_emit(" {:uptime/%d min%s},",
|
|
|
|
mins, mins > 1 ? "s" : "");
|
1997-10-13 15:17:03 +00:00
|
|
|
else
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_emit(" {:uptime/%d sec%s},",
|
|
|
|
secs, secs > 1 ? "s" : "");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Print number of users logged in to system */
|
2014-11-11 21:52:10 +00:00
|
|
|
xo_emit(" {:users/%d} {N:user%s}", nusers, nusers == 1 ? "" : "s");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Print 1, 5, and 15 minute load averages.
|
|
|
|
*/
|
|
|
|
if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_emit(", no load average information available\n");
|
1994-05-27 12:33:43 +00:00
|
|
|
else {
|
2014-11-05 23:54:33 +00:00
|
|
|
static const char *format[] = {
|
|
|
|
" {:load-average-1/%.2f}",
|
|
|
|
" {:load-average-5/%.2f}",
|
|
|
|
" {:load-average-15/%.2f}",
|
|
|
|
};
|
|
|
|
xo_emit(", load averages:");
|
2001-12-11 22:18:47 +00:00
|
|
|
for (i = 0; i < (int)(sizeof(avenrun) / sizeof(avenrun[0])); i++) {
|
2001-03-03 16:47:07 +00:00
|
|
|
if (use_comma && i > 0)
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_emit(",");
|
|
|
|
xo_emit(format[i], avenrun[i]);
|
2001-03-03 16:47:07 +00:00
|
|
|
}
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_emit("\n");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct stat *
|
2009-12-06 01:10:30 +00:00
|
|
|
ttystat(char *line)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
static struct stat sb;
|
|
|
|
char ttybuf[MAXPATHLEN];
|
|
|
|
|
2009-12-06 01:10:30 +00:00
|
|
|
(void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
|
2011-07-05 14:12:48 +00:00
|
|
|
if (stat(ttybuf, &sb) == 0 && S_ISCHR(sb.st_mode)) {
|
2002-08-23 04:31:58 +00:00
|
|
|
return (&sb);
|
|
|
|
} else
|
1999-07-28 19:29:46 +00:00
|
|
|
return (NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-06-07 01:41:54 +00:00
|
|
|
usage(int wcmd)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
if (wcmd)
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_error("usage: w [-dhin] [-M core] [-N system] [user ...]\n");
|
1994-05-27 12:33:43 +00:00
|
|
|
else
|
2014-11-05 23:54:33 +00:00
|
|
|
xo_error("usage: uptime\n");
|
1999-07-28 19:29:46 +00:00
|
|
|
exit(1);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|