w.c revision 1.10       1   1.1      cgd /*-
      2   1.1      cgd  * Copyright (c) 1980, 1991 The Regents of the University of California.
      3   1.1      cgd  * All rights reserved.
      4   1.1      cgd  *
      5   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6   1.1      cgd  * modification, are permitted provided that the following conditions
      7   1.1      cgd  * are met:
      8   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14   1.1      cgd  *    must display the following acknowledgement:
     15   1.1      cgd  *	This product includes software developed by the University of
     16   1.1      cgd  *	California, Berkeley and its contributors.
     17   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18   1.1      cgd  *    may be used to endorse or promote products derived from this software
     19   1.1      cgd  *    without specific prior written permission.
     20   1.1      cgd  *
     21   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31   1.1      cgd  * SUCH DAMAGE.
     32   1.1      cgd  */
     33   1.1      cgd 
     34   1.1      cgd #ifndef lint
     35   1.1      cgd char copyright[] =
     36   1.1      cgd "@(#) Copyright (c) 1980, 1991 The Regents of the University of California.\n\
     37   1.1      cgd  All rights reserved.\n";
     38   1.1      cgd #endif /* not lint */
     39   1.1      cgd 
     40   1.1      cgd #ifndef lint
     41   1.8  mycroft /*static char sccsid[] = "from: @(#)w.c	5.29 (Berkeley) 4/23/91";*/
     42  1.10      cgd static char rcsid[] = "$Id: w.c,v 1.10 1994/05/05 02:08:58 cgd Exp $";
     43   1.1      cgd #endif /* not lint */
     44   1.1      cgd 
     45   1.1      cgd /*
     46   1.1      cgd  * w - print system status (who and what)
     47   1.1      cgd  *
     48   1.1      cgd  * This program is similar to the systat command on Tenex/Tops 10/20
     49   1.1      cgd  *
     50   1.1      cgd  */
     51   1.1      cgd #include <sys/param.h>
     52   1.1      cgd #include <utmp.h>
     53   1.1      cgd #include <sys/time.h>
     54   1.1      cgd #include <sys/stat.h>
     55   1.1      cgd #include <sys/proc.h>
     56   1.1      cgd #include <sys/user.h>
     57   1.1      cgd #include <sys/ioctl.h>
     58   1.1      cgd #include <sys/tty.h>
     59   1.1      cgd #include <nlist.h>
     60   1.1      cgd #include <kvm.h>
     61   1.1      cgd #include <ctype.h>
     62   1.1      cgd #include <paths.h>
     63   1.1      cgd #include <string.h>
     64   1.1      cgd #include <stdio.h>
     65   1.7      cgd #include <vis.h>
     66   1.1      cgd 
     67  1.10      cgd #ifdef P_PPWAIT
     68   1.1      cgd #define NEWVM
     69   1.1      cgd #endif
     70   1.1      cgd #ifndef NEWVM
     71   1.1      cgd #include <machine/pte.h>
     72   1.1      cgd #include <sys/vm.h>
     73   1.1      cgd #endif
     74   1.1      cgd 
     75   1.1      cgd char	*program;
     76   1.1      cgd int	ttywidth;		/* width of tty */
     77   1.1      cgd int	argwidth;		/* width of tty */
     78   1.1      cgd int	header = 1;		/* true if -h flag: don't print heading */
     79   1.1      cgd int	wcmd = 1;		/* true if this is w(1), and not uptime(1) */
     80   1.1      cgd int	nusers;			/* number of users logged in now */
     81   1.1      cgd char *	sel_user;		/* login of particular user selected */
     82   1.1      cgd time_t	now;			/* the current time of day */
     83   1.1      cgd struct	timeval boottime;
     84   1.1      cgd time_t	uptime;			/* time of last reboot & elapsed time since */
     85   1.1      cgd struct	utmp utmp;
     86   1.1      cgd struct	winsize ws;
     87   1.1      cgd int	sortidle;		/* sort bu idle time */
     88   1.1      cgd 
     89   1.1      cgd 
     90   1.1      cgd /*
     91   1.1      cgd  * One of these per active utmp entry.
     92   1.1      cgd  */
     93   1.1      cgd struct	entry {
     94   1.1      cgd 	struct	entry *next;
     95   1.1      cgd 	struct	utmp utmp;
     96   1.1      cgd 	dev_t	tdev;		/* dev_t of terminal */
     97   1.1      cgd 	int	idle;		/* idle time of terminal in minutes */
     98   1.1      cgd 	struct	proc *proc;	/* list of procs in foreground */
     99   1.1      cgd 	char	*args;		/* arg list of interesting process */
    100   1.1      cgd } *ep, *ehead = NULL, **nextp = &ehead;
    101   1.1      cgd 
    102   1.1      cgd struct nlist nl[] = {
    103   1.1      cgd 	{ "_boottime" },
    104   1.1      cgd #define X_BOOTTIME	0
    105   1.1      cgd 	{ "" },
    106   1.1      cgd };
    107   1.1      cgd 
    108   1.1      cgd #define USAGE "[ -hi ] [ user ]"
    109   1.1      cgd #define usage()	fprintf(stderr, "usage: %s: %s\n", program, USAGE)
    110   1.1      cgd 
    111   1.1      cgd main(argc, argv)
    112   1.1      cgd 	int argc;
    113   1.1      cgd 	char **argv;
    114   1.1      cgd {
    115   1.1      cgd 	register int i;
    116   1.1      cgd 	struct winsize win;
    117   1.1      cgd 	register struct proc *p;
    118   1.1      cgd 	struct eproc *e;
    119   1.1      cgd 	struct stat *stp, *ttystat();
    120   1.1      cgd 	FILE *ut;
    121   1.1      cgd 	char *cp;
    122   1.7      cgd 	char *vis_args;
    123   1.1      cgd 	int ch;
    124   1.1      cgd 	extern char *optarg;
    125   1.1      cgd 	extern int optind;
    126   1.1      cgd 	char *attime();
    127   1.1      cgd 
    128   1.1      cgd 	program = argv[0];
    129   1.1      cgd 	/*
    130   1.1      cgd 	 * are we w(1) or uptime(1)
    131   1.1      cgd 	 */
    132   1.1      cgd 	if ((cp = rindex(program, '/')) || *(cp = program) == '-')
    133   1.1      cgd 		cp++;
    134   1.1      cgd 	if (*cp == 'u')
    135   1.1      cgd 		wcmd = 0;
    136   1.1      cgd 
    137   1.1      cgd 	while ((ch = getopt(argc, argv, "hiflsuw")) != EOF)
    138   1.1      cgd 		switch((char)ch) {
    139   1.1      cgd 		case 'h':
    140   1.1      cgd 			header = 0;
    141   1.1      cgd 			break;
    142   1.1      cgd 		case 'i':
    143   1.1      cgd 			sortidle++;
    144   1.1      cgd 			break;
    145   1.1      cgd 		case 'f': case 'l': case 's': case 'u': case 'w':
    146   1.1      cgd 			error("[-flsuw] no longer supported");
    147   1.1      cgd 			usage();
    148   1.1      cgd 			exit(1);
    149   1.1      cgd 		case '?':
    150   1.1      cgd 		default:
    151   1.1      cgd 			usage();
    152   1.1      cgd 			exit(1);
    153   1.1      cgd 		}
    154   1.1      cgd 	argc -= optind;
    155   1.1      cgd 	argv += optind;
    156   1.1      cgd 
    157   1.1      cgd 	if (*argv)
    158   1.1      cgd 		sel_user = *argv;
    159   1.1      cgd 
    160   1.1      cgd 	if (header && kvm_nlist(nl) != 0) {
    161   1.1      cgd 		error("can't get namelist");
    162   1.1      cgd 		exit (1);
    163   1.1      cgd 	}
    164   1.1      cgd 	time(&now);
    165   1.1      cgd 	ut = fopen(_PATH_UTMP, "r");
    166   1.1      cgd 	while (fread(&utmp, sizeof(utmp), 1, ut)) {
    167   1.1      cgd 		if (utmp.ut_name[0] == '\0')
    168   1.1      cgd 			continue;
    169   1.1      cgd 		nusers++;
    170   1.1      cgd 		if (wcmd == 0 || (sel_user &&
    171   1.1      cgd 		    strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0))
    172   1.1      cgd 			continue;
    173   1.1      cgd 		if ((ep = (struct entry *)
    174   1.1      cgd 		     calloc(1, sizeof (struct entry))) == NULL) {
    175   1.1      cgd 			error("out of memory");
    176   1.1      cgd 			exit(1);
    177   1.1      cgd 		}
    178   1.1      cgd 		*nextp = ep;
    179   1.1      cgd 		nextp = &(ep->next);
    180   1.1      cgd 		bcopy(&utmp, &(ep->utmp), sizeof (struct utmp));
    181   1.1      cgd 		stp = ttystat(ep->utmp.ut_line);
    182   1.1      cgd 		ep->tdev = stp->st_rdev;
    183   1.1      cgd 		ep->idle = ((now - stp->st_atime) + 30) / 60; /* secs->mins */
    184   1.1      cgd 		if (ep->idle < 0)
    185   1.1      cgd 			ep->idle = 0;
    186   1.1      cgd 	}
    187   1.1      cgd 	fclose(ut);
    188   1.1      cgd 
    189   1.1      cgd 	if (header || wcmd == 0) {
    190   1.1      cgd 		double	avenrun[3];
    191   1.1      cgd 		int days, hrs, mins;
    192   1.1      cgd 
    193   1.1      cgd 		/*
    194   1.1      cgd 		 * Print time of day
    195   1.1      cgd 		 */
    196   1.1      cgd 		fputs(attime(&now), stdout);
    197   1.1      cgd 		/*
    198   1.1      cgd 		 * Print how long system has been up.
    199   1.1      cgd 		 * (Found by looking for "boottime" in kernel)
    200   1.1      cgd 		 */
    201   1.1      cgd 		(void)kvm_read((void *)nl[X_BOOTTIME].n_value, &boottime,
    202   1.1      cgd 		    sizeof (boottime));
    203   1.1      cgd 		uptime = now - boottime.tv_sec;
    204   1.1      cgd 		uptime += 30;
    205   1.1      cgd 		days = uptime / (60*60*24);
    206   1.1      cgd 		uptime %= (60*60*24);
    207   1.1      cgd 		hrs = uptime / (60*60);
    208   1.1      cgd 		uptime %= (60*60);
    209   1.1      cgd 		mins = uptime / 60;
    210   1.1      cgd 
    211   1.1      cgd 		printf("  up");
    212   1.1      cgd 		if (days > 0)
    213   1.1      cgd 			printf(" %d day%s,", days, days>1?"s":"");
    214   1.1      cgd 		if (hrs > 0 && mins > 0) {
    215   1.1      cgd 			printf(" %2d:%02d,", hrs, mins);
    216   1.1      cgd 		} else {
    217   1.1      cgd 			if (hrs > 0)
    218   1.1      cgd 				printf(" %d hr%s,", hrs, hrs>1?"s":"");
    219   1.1      cgd 			if (mins > 0)
    220   1.1      cgd 				printf(" %d min%s,", mins, mins>1?"s":"");
    221   1.1      cgd 		}
    222   1.1      cgd 
    223   1.1      cgd 		/* Print number of users logged in to system */
    224   1.1      cgd 		printf("  %d user%s", nusers, nusers>1?"s":"");
    225   1.1      cgd 
    226   1.1      cgd 		/*
    227   1.1      cgd 		 * Print 1, 5, and 15 minute load averages.
    228   1.1      cgd 		 */
    229   1.1      cgd 		printf(",  load average:");
    230   1.1      cgd 		(void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
    231   1.1      cgd 		for (i = 0; i < (sizeof(avenrun)/sizeof(avenrun[0])); i++) {
    232   1.1      cgd 			if (i > 0)
    233   1.1      cgd 				printf(",");
    234   1.1      cgd 			printf(" %.2f", avenrun[i]);
    235   1.1      cgd 		}
    236   1.1      cgd 		printf("\n");
    237   1.1      cgd 		if (wcmd == 0)		/* if uptime(1) then done */
    238   1.1      cgd 			exit(0);
    239   1.1      cgd #define HEADER	"USER    TTY FROM              LOGIN@  IDLE WHAT\n"
    240   1.1      cgd #define WUSED	(sizeof (HEADER) - sizeof ("WHAT\n"))
    241   1.1      cgd 		printf(HEADER);
    242   1.1      cgd 	}
    243   1.1      cgd 
    244   1.1      cgd 	while ((p = kvm_nextproc()) != NULL) {
    245  1.10      cgd 		if (p->p_stat == SZOMB || (p->p_flag & P_CONTROLT) == 0)
    246   1.1      cgd 			continue;
    247   1.1      cgd 		e = kvm_geteproc(p);
    248   1.1      cgd 		for (ep = ehead; ep != NULL; ep = ep->next) {
    249   1.1      cgd 			if (ep->tdev == e->e_tdev && e->e_pgid == e->e_tpgid) {
    250   1.1      cgd 				/*
    251   1.1      cgd 				 * Proc is in foreground of this terminal
    252   1.1      cgd 				 */
    253   1.1      cgd 				if (proc_compare(ep->proc, p))
    254   1.1      cgd 					ep->proc = p;
    255   1.1      cgd 				break;
    256   1.1      cgd 			}
    257   1.1      cgd 		}
    258   1.1      cgd 	}
    259   1.1      cgd 	if ((ioctl(1, TIOCGWINSZ, &ws) == -1 &&
    260   1.1      cgd 	     ioctl(2, TIOCGWINSZ, &ws) == -1 &&
    261   1.1      cgd 	     ioctl(0, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
    262   1.1      cgd 	       ttywidth = 79;
    263   1.1      cgd         else
    264   1.1      cgd 	       ttywidth = ws.ws_col - 1;
    265   1.1      cgd 	argwidth = ttywidth - WUSED;
    266   1.1      cgd 	if (argwidth < 4)
    267   1.1      cgd 		argwidth = 8;
    268   1.1      cgd 	for (ep = ehead; ep != NULL; ep = ep->next) {
    269   1.3  mycroft 		if (!ep->proc) {
    270   1.3  mycroft 			ep->args = NULL;
    271   1.3  mycroft 			continue;
    272   1.3  mycroft 		}
    273   1.1      cgd 		ep->args = strdup(kvm_getargs(ep->proc, kvm_getu(ep->proc)));
    274   1.1      cgd 		if (ep->args == NULL) {
    275   1.1      cgd 			error("out of memory");
    276   1.1      cgd 			exit(1);
    277   1.1      cgd 		}
    278   1.1      cgd 	}
    279   1.1      cgd 	/* sort by idle time */
    280   1.1      cgd 	if (sortidle && ehead != NULL) {
    281   1.1      cgd 		struct entry *from = ehead, *save;
    282   1.1      cgd 
    283   1.1      cgd 		ehead = NULL;
    284   1.1      cgd 		while (from != NULL) {
    285   1.1      cgd 			for (nextp = &ehead;
    286   1.1      cgd 			    (*nextp) && from->idle >= (*nextp)->idle;
    287   1.1      cgd 			    nextp = &(*nextp)->next)
    288   1.1      cgd 				;
    289   1.1      cgd 			save = from;
    290   1.1      cgd 			from = from->next;
    291   1.1      cgd 			save->next = *nextp;
    292   1.1      cgd 			*nextp = save;
    293   1.1      cgd 		}
    294   1.1      cgd 	}
    295   1.7      cgd 
    296   1.7      cgd 	if ((vis_args = (char *)malloc(argwidth * 4 + 1)) == NULL) {
    297   1.7      cgd 		error("out of memory");
    298   1.7      cgd 		exit(1);
    299   1.7      cgd 	}
    300   1.1      cgd 	for (ep = ehead; ep != NULL; ep = ep->next) {
    301   1.1      cgd 		printf("%-*.*s %-2.2s %-*.*s %s",
    302   1.1      cgd 			UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
    303   1.1      cgd 			strncmp(ep->utmp.ut_line, "tty", 3) == 0 ?
    304   1.1      cgd 				ep->utmp.ut_line+3 : ep->utmp.ut_line,
    305   1.1      cgd 			UT_HOSTSIZE, UT_HOSTSIZE, *ep->utmp.ut_host ?
    306   1.1      cgd 				ep->utmp.ut_host : "-",
    307   1.1      cgd 			attime(&ep->utmp.ut_time));
    308   1.1      cgd 		if (ep->idle >= 36 * 60)
    309   1.1      cgd 			printf(" %ddays ", (ep->idle + 12 * 60) / (24 * 60));
    310   1.2      sef 		else if (ep->idle == 0)
    311   1.9  mycroft 			printf("     - ");
    312   1.1      cgd 		else
    313   1.1      cgd 			prttime(ep->idle, " ");
    314   1.7      cgd 		if (ep->args)
    315   1.7      cgd 	        	strvisx(vis_args, ep->args,
    316   1.7      cgd 				(strlen(ep->args) > argwidth) ?
    317   1.7      cgd 					argwidth : strlen(ep->args),
    318   1.7      cgd 				VIS_TAB|VIS_NL|VIS_NOSLASH);
    319   1.7      cgd 		printf("%.*s\n", argwidth, ep->args ? vis_args : "-");
    320   1.1      cgd 	}
    321   1.7      cgd 	free(vis_args);
    322   1.7      cgd 
    323   1.1      cgd 	exit(0);
    324   1.1      cgd }
    325   1.1      cgd 
    326   1.1      cgd struct stat *
    327   1.1      cgd ttystat(line)
    328   1.1      cgd {
    329   1.1      cgd 	static struct stat statbuf;
    330   1.1      cgd 	char ttybuf[sizeof (_PATH_DEV) + UT_LINESIZE + 1];
    331   1.1      cgd 
    332   1.1      cgd 	sprintf(ttybuf, "%s/%.*s", _PATH_DEV, UT_LINESIZE, line);
    333   1.1      cgd 	(void) stat(ttybuf, &statbuf);
    334   1.1      cgd 
    335   1.1      cgd 	return (&statbuf);
    336   1.1      cgd }
    337   1.1      cgd 
    338   1.1      cgd /*
    339   1.1      cgd  * prttime prints a time in hours and minutes or minutes and seconds.
    340   1.1      cgd  * The character string tail is printed at the end, obvious
    341   1.1      cgd  * strings to pass are "", " ", or "am".
    342   1.1      cgd  */
    343   1.1      cgd prttime(tim, tail)
    344   1.1      cgd 	time_t tim;
    345   1.1      cgd 	char *tail;
    346   1.1      cgd {
    347   1.1      cgd 
    348   1.9  mycroft 	if (tim >= 60)
    349   1.9  mycroft 		printf(" %2d:%02d", tim/60, tim%60);
    350   1.9  mycroft 	else if (tim >= 0)
    351   1.1      cgd 		printf("    %2d", tim);
    352   1.1      cgd 	printf("%s", tail);
    353   1.1      cgd }
    354   1.1      cgd 
    355   1.1      cgd #include <varargs.h>
    356   1.1      cgd 
    357   1.1      cgd error(va_alist)
    358   1.1      cgd 	va_dcl
    359   1.1      cgd {
    360   1.1      cgd 	char *fmt;
    361   1.1      cgd 	va_list ap;
    362   1.1      cgd 
    363   1.1      cgd 	fprintf(stderr, "%s: ", program);
    364   1.1      cgd 	va_start(ap);
    365   1.1      cgd 	fmt = va_arg(ap, char *);
    366   1.1      cgd 	(void) vfprintf(stderr, fmt, ap);
    367   1.1      cgd 	va_end(ap);
    368   1.1      cgd 	fprintf(stderr, "\n");
    369   1.1      cgd }
    370