xref: /titanic_53/usr/src/cmd/whodo/whodo.c (revision 76e222fdcb4e5cd81e03f2d48b91c94b86526844)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*76e222fdSSumanth Naropanth  * Common Development and Distribution License (the "License").
6*76e222fdSSumanth Naropanth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*76e222fdSSumanth Naropanth  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * This is the new whodo command which takes advantage of
417c478bd9Sstevel@tonic-gate  * the /proc interface to gain access to the information
427c478bd9Sstevel@tonic-gate  * of all the processes currently on the system.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * Maintenance note:
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * Much of this code is replicated in w.c.  If you're
477c478bd9Sstevel@tonic-gate  * fixing bugs here, then you should probably fix 'em there too.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <stdio.h>
517c478bd9Sstevel@tonic-gate #include <string.h>
527c478bd9Sstevel@tonic-gate #include <stdlib.h>
537c478bd9Sstevel@tonic-gate #include <ctype.h>
547c478bd9Sstevel@tonic-gate #include <fcntl.h>
557c478bd9Sstevel@tonic-gate #include <time.h>
567c478bd9Sstevel@tonic-gate #include <errno.h>
577c478bd9Sstevel@tonic-gate #include <sys/types.h>
587c478bd9Sstevel@tonic-gate #include <utmpx.h>
597c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
607c478bd9Sstevel@tonic-gate #include <sys/stat.h>
617c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
627c478bd9Sstevel@tonic-gate #include <dirent.h>
637c478bd9Sstevel@tonic-gate #include <procfs.h>		/* /proc header file */
647c478bd9Sstevel@tonic-gate #include <sys/wait.h>
657c478bd9Sstevel@tonic-gate #include <locale.h>
667c478bd9Sstevel@tonic-gate #include <unistd.h>
677c478bd9Sstevel@tonic-gate #include <limits.h>
687c478bd9Sstevel@tonic-gate #include <priv_utils.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * utmpx defines wider fields for user and line.  For compatibility of output,
727c478bd9Sstevel@tonic-gate  * we are limiting these to the old maximums in utmp. Define UTMPX_NAMELEN
737c478bd9Sstevel@tonic-gate  * to use the full lengths.
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate #ifndef UTMPX_NAMELEN
767c478bd9Sstevel@tonic-gate /* XXX - utmp - fix name length */
777c478bd9Sstevel@tonic-gate #define	NMAX		(_POSIX_LOGIN_NAME_MAX - 1)
787c478bd9Sstevel@tonic-gate #define	LMAX		12
797c478bd9Sstevel@tonic-gate #else /* UTMPX_NAMELEN */
807c478bd9Sstevel@tonic-gate static struct utmpx dummy;
817c478bd9Sstevel@tonic-gate #define	NMAX	(sizeof (dummy.ut_user))
827c478bd9Sstevel@tonic-gate #define	LMAX	(sizeof (dummy.ut_line))
837c478bd9Sstevel@tonic-gate #endif /* UTMPX_NAMELEN */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #define	DIV60(t)	((t+30)/60)    /* x/60 rounded */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #ifdef ERR
887c478bd9Sstevel@tonic-gate #undef ERR
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate #define	ERR		(-1)
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #define	DEVNAMELEN	14
937c478bd9Sstevel@tonic-gate #define	HSIZE		256		/* size of process hash table */
947c478bd9Sstevel@tonic-gate #define	PROCDIR		"/proc"
957c478bd9Sstevel@tonic-gate #define	INITPROCESS	(pid_t)1	/* init process pid */
967c478bd9Sstevel@tonic-gate #define	NONE		'n'		/* no state */
977c478bd9Sstevel@tonic-gate #define	RUNNING		'r'		/* runnable process */
987c478bd9Sstevel@tonic-gate #define	ZOMBIE		'z'		/* zombie process */
997c478bd9Sstevel@tonic-gate #define	VISITED		'v'		/* marked node as visited */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static int	ndevs;			/* number of configured devices */
1027c478bd9Sstevel@tonic-gate static int	maxdev;			/* slots for configured devices */
1037c478bd9Sstevel@tonic-gate #define	DNINCR	100
1047c478bd9Sstevel@tonic-gate static struct devl {			/* device list   */
1057c478bd9Sstevel@tonic-gate 	char	dname[DEVNAMELEN];	/* device name   */
1067c478bd9Sstevel@tonic-gate 	dev_t	ddev;			/* device number */
1077c478bd9Sstevel@tonic-gate } *devl;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate struct uproc {
1107c478bd9Sstevel@tonic-gate 	pid_t	p_upid;			/* user process id */
1117c478bd9Sstevel@tonic-gate 	char	p_state;		/* numeric value of process state */
1127c478bd9Sstevel@tonic-gate 	dev_t	p_ttyd;			/* controlling tty of process */
1137c478bd9Sstevel@tonic-gate 	time_t	p_time;			/* ticks of user & system time */
1147c478bd9Sstevel@tonic-gate 	time_t	p_ctime;		/* ticks of child user & system time */
1157c478bd9Sstevel@tonic-gate 	int	p_igintr;		/* 1=ignores SIGQUIT and SIGINT */
1167c478bd9Sstevel@tonic-gate 	char	p_comm[PRARGSZ+1];	/* command */
1177c478bd9Sstevel@tonic-gate 	char	p_args[PRARGSZ+1];	/* command line arguments */
1187c478bd9Sstevel@tonic-gate 	struct uproc	*p_child,	/* first child pointer */
1197c478bd9Sstevel@tonic-gate 			*p_sibling,	/* sibling pointer */
1207c478bd9Sstevel@tonic-gate 			*p_pgrplink,	/* pgrp link */
1217c478bd9Sstevel@tonic-gate 			*p_link;	/* hash table chain pointer */
1227c478bd9Sstevel@tonic-gate };
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  *	define	hash table for struct uproc
1267c478bd9Sstevel@tonic-gate  *	Hash function uses process id
1277c478bd9Sstevel@tonic-gate  *	and the size of the hash table(HSIZE)
1287c478bd9Sstevel@tonic-gate  *	to determine process index into the table.
1297c478bd9Sstevel@tonic-gate  */
1307c478bd9Sstevel@tonic-gate static struct uproc	pr_htbl[HSIZE];
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate static struct	uproc	*findhash(pid_t);
1337c478bd9Sstevel@tonic-gate static time_t	findidle(char *);
1347c478bd9Sstevel@tonic-gate static void	clnarglist(char *);
1357c478bd9Sstevel@tonic-gate static void	showproc(struct uproc *);
1367c478bd9Sstevel@tonic-gate static void	showtotals(struct uproc *);
1377c478bd9Sstevel@tonic-gate static void	calctotals(struct uproc *);
1387c478bd9Sstevel@tonic-gate static char	*getty(dev_t);
1397c478bd9Sstevel@tonic-gate static void	prttime(time_t, char *);
1407c478bd9Sstevel@tonic-gate static void	prtat(time_t *);
1417c478bd9Sstevel@tonic-gate static void	checkampm(char *);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static char	*prog;
1447c478bd9Sstevel@tonic-gate static int	header = 1;	/* true if -h flag: don't print heading */
1457c478bd9Sstevel@tonic-gate static int	lflag = 0;	/* true if -l flag: w command format */
1467c478bd9Sstevel@tonic-gate static char 	*sel_user;	/* login of particular user selected */
1477c478bd9Sstevel@tonic-gate static time_t	now;		/* current time of day */
1487c478bd9Sstevel@tonic-gate static time_t	uptime;		/* time of last reboot & elapsed time since */
1497c478bd9Sstevel@tonic-gate static int	nusers;		/* number of users logged in now */
1507c478bd9Sstevel@tonic-gate static time_t	idle;		/* number of minutes user is idle */
1517c478bd9Sstevel@tonic-gate static time_t	jobtime;	/* total cpu time visible */
1527c478bd9Sstevel@tonic-gate static char	doing[520];	/* process attached to terminal */
1537c478bd9Sstevel@tonic-gate static time_t	proctime;	/* cpu time of process in doing */
1547c478bd9Sstevel@tonic-gate static int	empty;
1557c478bd9Sstevel@tonic-gate static pid_t	curpid;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #if SIGQUIT > SIGINT
1587c478bd9Sstevel@tonic-gate #define	ACTSIZE	SIGQUIT
1597c478bd9Sstevel@tonic-gate #else
1607c478bd9Sstevel@tonic-gate #define	ACTSIZE	SIGINT
1617c478bd9Sstevel@tonic-gate #endif
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate int
1647c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	struct utmpx	*ut;
1677c478bd9Sstevel@tonic-gate 	struct utmpx	*utmpbegin;
1687c478bd9Sstevel@tonic-gate 	struct utmpx	*utmpend;
1697c478bd9Sstevel@tonic-gate 	struct utmpx 	*utp;
1707c478bd9Sstevel@tonic-gate 	struct tm		*tm;
1717c478bd9Sstevel@tonic-gate 	struct uproc	*up, *parent, *pgrp;
1727c478bd9Sstevel@tonic-gate 	struct psinfo	info;
1737c478bd9Sstevel@tonic-gate 	struct sigaction actinfo[ACTSIZE];
1747c478bd9Sstevel@tonic-gate 	struct pstatus	statinfo;
1757c478bd9Sstevel@tonic-gate 	size_t		size;
1767c478bd9Sstevel@tonic-gate 	struct stat	sbuf;
1777c478bd9Sstevel@tonic-gate 	struct utsname	uts;
1787c478bd9Sstevel@tonic-gate 	DIR		*dirp;
1797c478bd9Sstevel@tonic-gate 	struct	dirent	*dp;
1807c478bd9Sstevel@tonic-gate 	char 		pname[64];
1817c478bd9Sstevel@tonic-gate 	char 		*fname;
1827c478bd9Sstevel@tonic-gate 	int		procfd;
1837c478bd9Sstevel@tonic-gate 	int		i;
1847c478bd9Sstevel@tonic-gate 	int		days, hrs, mins;
1857c478bd9Sstevel@tonic-gate 	int		entries;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/*
1887c478bd9Sstevel@tonic-gate 	 * This program needs the proc_owner privilege
1897c478bd9Sstevel@tonic-gate 	 */
1907c478bd9Sstevel@tonic-gate 	(void) __init_suid_priv(PU_CLEARLIMITSET, PRIV_PROC_OWNER,
1917c478bd9Sstevel@tonic-gate 	    (char *)NULL);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1947c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1957c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1967c478bd9Sstevel@tonic-gate #endif
1977c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	prog = argv[0];
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	while (argc > 1) {
2027c478bd9Sstevel@tonic-gate 		if (argv[1][0] == '-') {
2037c478bd9Sstevel@tonic-gate 			for (i = 1; argv[1][i]; i++) {
2047c478bd9Sstevel@tonic-gate 				switch (argv[1][i]) {
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 				case 'h':
2077c478bd9Sstevel@tonic-gate 					header = 0;
2087c478bd9Sstevel@tonic-gate 					break;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 				case 'l':
2117c478bd9Sstevel@tonic-gate 					lflag++;
2127c478bd9Sstevel@tonic-gate 					break;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 				default:
2157c478bd9Sstevel@tonic-gate 					(void) printf(gettext(
2167c478bd9Sstevel@tonic-gate 					    "usage: %s [ -hl ] [ user ]\n"),
2177c478bd9Sstevel@tonic-gate 					    prog);
2187c478bd9Sstevel@tonic-gate 					exit(1);
2197c478bd9Sstevel@tonic-gate 				}
2207c478bd9Sstevel@tonic-gate 			}
2217c478bd9Sstevel@tonic-gate 		} else {
2227c478bd9Sstevel@tonic-gate 			if (!isalnum(argv[1][0]) || argc > 2) {
2237c478bd9Sstevel@tonic-gate 				(void) printf(gettext(
2247c478bd9Sstevel@tonic-gate 				    "usage: %s [ -hl ] [ user ]\n"), prog);
2257c478bd9Sstevel@tonic-gate 				exit(1);
2267c478bd9Sstevel@tonic-gate 			} else
2277c478bd9Sstevel@tonic-gate 				sel_user = argv[1];
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 		argc--; argv++;
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	 * read the UTMPX_FILE (contains information about
2347c478bd9Sstevel@tonic-gate 	 * each logged in user)
2357c478bd9Sstevel@tonic-gate 	 */
2367c478bd9Sstevel@tonic-gate 	if (stat(UTMPX_FILE, &sbuf) == ERR) {
2377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: stat error of %s: %s\n"),
2387c478bd9Sstevel@tonic-gate 		    prog, UTMPX_FILE, strerror(errno));
2397c478bd9Sstevel@tonic-gate 		exit(1);
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 	entries = sbuf.st_size / sizeof (struct futmpx);
2427c478bd9Sstevel@tonic-gate 	size = sizeof (struct utmpx) * entries;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if ((ut = malloc(size)) == NULL) {
2457c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: malloc error of %s: %s\n"),
2467c478bd9Sstevel@tonic-gate 		    prog, UTMPX_FILE, strerror(errno));
2477c478bd9Sstevel@tonic-gate 		exit(1);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	(void) utmpxname(UTMPX_FILE);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	utmpbegin = ut;
2537c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
2547c478bd9Sstevel@tonic-gate 	utmpend = (struct utmpx *)((char *)utmpbegin + size);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	setutxent();
257*76e222fdSSumanth Naropanth 	while ((ut < utmpend) && ((utp = getutxent()) != NULL))
2587c478bd9Sstevel@tonic-gate 		(void) memcpy(ut++, utp, sizeof (*ut));
2597c478bd9Sstevel@tonic-gate 	endutxent();
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	(void) time(&now);	/* get current time */
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (header) {	/* print a header */
2647c478bd9Sstevel@tonic-gate 		if (lflag) {	/* w command format header */
2657c478bd9Sstevel@tonic-gate 			prtat(&now);
2667c478bd9Sstevel@tonic-gate 			for (ut = utmpbegin; ut < utmpend; ut++) {
2677c478bd9Sstevel@tonic-gate 				if (ut->ut_type == USER_PROCESS) {
2687c478bd9Sstevel@tonic-gate 					nusers++;
2697c478bd9Sstevel@tonic-gate 				} else if (ut->ut_type == BOOT_TIME) {
2707c478bd9Sstevel@tonic-gate 					uptime = now - ut->ut_xtime;
2717c478bd9Sstevel@tonic-gate 					uptime += 30;
2727c478bd9Sstevel@tonic-gate 					days = uptime / (60*60*24);
2737c478bd9Sstevel@tonic-gate 					uptime %= (60*60*24);
2747c478bd9Sstevel@tonic-gate 					hrs = uptime / (60*60);
2757c478bd9Sstevel@tonic-gate 					uptime %= (60*60);
2767c478bd9Sstevel@tonic-gate 					mins = uptime / 60;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 					(void) printf(dcgettext(NULL,
2797c478bd9Sstevel@tonic-gate 					    "  up %d day(s), %d hr(s), "
2807c478bd9Sstevel@tonic-gate 					    "%d min(s)", LC_TIME),
2817c478bd9Sstevel@tonic-gate 					    days, hrs, mins);
2827c478bd9Sstevel@tonic-gate 				}
2837c478bd9Sstevel@tonic-gate 			}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 			ut = utmpbegin; /* rewind utmp data */
2867c478bd9Sstevel@tonic-gate 			(void) printf(dcgettext(NULL,
2877c478bd9Sstevel@tonic-gate 			    "  %d user(s)\n", LC_TIME), nusers);
2887c478bd9Sstevel@tonic-gate 			(void) printf(dcgettext(NULL, "User     tty           "
2897c478bd9Sstevel@tonic-gate 			    "login@  idle   JCPU   PCPU  what\n", LC_TIME));
2907c478bd9Sstevel@tonic-gate 		} else {	/* standard whodo header */
2917c478bd9Sstevel@tonic-gate 			char date_buf[100];
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 			/*
2947c478bd9Sstevel@tonic-gate 			 * print current time and date
2957c478bd9Sstevel@tonic-gate 			 */
2967c478bd9Sstevel@tonic-gate 			(void) strftime(date_buf, sizeof (date_buf),
2977c478bd9Sstevel@tonic-gate 			    dcgettext(NULL, "%C", LC_TIME), localtime(&now));
2987c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", date_buf);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 			/*
3017c478bd9Sstevel@tonic-gate 			 * print system name
3027c478bd9Sstevel@tonic-gate 			 */
3037c478bd9Sstevel@tonic-gate 			(void) uname(&uts);
3047c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", uts.nodename);
3057c478bd9Sstevel@tonic-gate 		}
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/*
3097c478bd9Sstevel@tonic-gate 	 * loop through /proc, reading info about each process
3107c478bd9Sstevel@tonic-gate 	 * and build the parent/child tree
3117c478bd9Sstevel@tonic-gate 	 */
3127c478bd9Sstevel@tonic-gate 	if (!(dirp = opendir(PROCDIR))) {
3137c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could not open %s: %s\n"),
3147c478bd9Sstevel@tonic-gate 		    prog, PROCDIR, strerror(errno));
3157c478bd9Sstevel@tonic-gate 		exit(1);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != NULL) {
3197c478bd9Sstevel@tonic-gate 		if (dp->d_name[0] == '.')
3207c478bd9Sstevel@tonic-gate 			continue;
3217c478bd9Sstevel@tonic-gate retry:
3227c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname),
3237c478bd9Sstevel@tonic-gate 		    "%s/%s/", PROCDIR, dp->d_name);
3247c478bd9Sstevel@tonic-gate 		fname = pname + strlen(pname);
3257c478bd9Sstevel@tonic-gate 		(void) strcpy(fname, "psinfo");
3267c478bd9Sstevel@tonic-gate 		if ((procfd = open(pname, O_RDONLY)) < 0)
3277c478bd9Sstevel@tonic-gate 			continue;
3287c478bd9Sstevel@tonic-gate 		if (read(procfd, &info, sizeof (info)) != sizeof (info)) {
3297c478bd9Sstevel@tonic-gate 			int err = errno;
3307c478bd9Sstevel@tonic-gate 			(void) close(procfd);
3317c478bd9Sstevel@tonic-gate 			if (err == EAGAIN)
3327c478bd9Sstevel@tonic-gate 				goto retry;
3337c478bd9Sstevel@tonic-gate 			if (err != ENOENT)
3347c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3357c478bd9Sstevel@tonic-gate 				    "%s: read() failed on %s: %s\n"),
3367c478bd9Sstevel@tonic-gate 				    prog, pname, strerror(err));
3377c478bd9Sstevel@tonic-gate 			continue;
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 		(void) close(procfd);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		up = findhash(info.pr_pid);
3427c478bd9Sstevel@tonic-gate 		up->p_ttyd = info.pr_ttydev;
3437c478bd9Sstevel@tonic-gate 		up->p_state = (info.pr_nlwp == 0? ZOMBIE : RUNNING);
3447c478bd9Sstevel@tonic-gate 		up->p_time = 0;
3457c478bd9Sstevel@tonic-gate 		up->p_ctime = 0;
3467c478bd9Sstevel@tonic-gate 		up->p_igintr = 0;
3477c478bd9Sstevel@tonic-gate 		(void) strncpy(up->p_comm, info.pr_fname,
3487c478bd9Sstevel@tonic-gate 		    sizeof (info.pr_fname));
3497c478bd9Sstevel@tonic-gate 		up->p_args[0] = 0;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		if (up->p_state != NONE && up->p_state != ZOMBIE) {
3527c478bd9Sstevel@tonic-gate 			(void) strcpy(fname, "status");
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 			/* now we need the proc_owner privilege */
3557c478bd9Sstevel@tonic-gate 			(void) __priv_bracket(PRIV_ON);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 			procfd = open(pname, O_RDONLY);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 			/* drop proc_owner privilege after open */
3607c478bd9Sstevel@tonic-gate 			(void) __priv_bracket(PRIV_OFF);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 			if (procfd  < 0)
3637c478bd9Sstevel@tonic-gate 				continue;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 			if (read(procfd, &statinfo, sizeof (statinfo))
3667c478bd9Sstevel@tonic-gate 			    != sizeof (statinfo)) {
3677c478bd9Sstevel@tonic-gate 				int err = errno;
3687c478bd9Sstevel@tonic-gate 				(void) close(procfd);
3697c478bd9Sstevel@tonic-gate 				if (err == EAGAIN)
3707c478bd9Sstevel@tonic-gate 					goto retry;
3717c478bd9Sstevel@tonic-gate 				if (err != ENOENT)
3727c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
3737c478bd9Sstevel@tonic-gate 					    "%s: read() failed on %s: %s \n"),
3747c478bd9Sstevel@tonic-gate 					    prog, pname, strerror(err));
3757c478bd9Sstevel@tonic-gate 				continue;
3767c478bd9Sstevel@tonic-gate 			}
3777c478bd9Sstevel@tonic-gate 			(void) close(procfd);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 			up->p_time = statinfo.pr_utime.tv_sec +
3807c478bd9Sstevel@tonic-gate 			    statinfo.pr_stime.tv_sec;
3817c478bd9Sstevel@tonic-gate 			up->p_ctime = statinfo.pr_cutime.tv_sec +
3827c478bd9Sstevel@tonic-gate 			    statinfo.pr_cstime.tv_sec;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 			(void) strcpy(fname, "sigact");
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 			/* now we need the proc_owner privilege */
3877c478bd9Sstevel@tonic-gate 			(void) __priv_bracket(PRIV_ON);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 			procfd = open(pname, O_RDONLY);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 			/* drop proc_owner privilege after open */
3927c478bd9Sstevel@tonic-gate 			(void) __priv_bracket(PRIV_OFF);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 			if (procfd  < 0)
3957c478bd9Sstevel@tonic-gate 				continue;
3967c478bd9Sstevel@tonic-gate 			if (read(procfd, actinfo, sizeof (actinfo))
3977c478bd9Sstevel@tonic-gate 			    != sizeof (actinfo)) {
3987c478bd9Sstevel@tonic-gate 				int err = errno;
3997c478bd9Sstevel@tonic-gate 				(void) close(procfd);
4007c478bd9Sstevel@tonic-gate 				if (err == EAGAIN)
4017c478bd9Sstevel@tonic-gate 					goto retry;
4027c478bd9Sstevel@tonic-gate 				if (err != ENOENT)
4037c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
4047c478bd9Sstevel@tonic-gate 					    "%s: read() failed on %s: %s \n"),
4057c478bd9Sstevel@tonic-gate 					    prog, pname, strerror(err));
4067c478bd9Sstevel@tonic-gate 				continue;
4077c478bd9Sstevel@tonic-gate 			}
4087c478bd9Sstevel@tonic-gate 			(void) close(procfd);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 			up->p_igintr =
4117c478bd9Sstevel@tonic-gate 			    actinfo[SIGINT-1].sa_handler == SIG_IGN &&
4127c478bd9Sstevel@tonic-gate 			    actinfo[SIGQUIT-1].sa_handler == SIG_IGN;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 			up->p_args[0] = 0;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 			/*
4177c478bd9Sstevel@tonic-gate 			 * Process args if there's a chance we'll print it.
4187c478bd9Sstevel@tonic-gate 			 */
4197c478bd9Sstevel@tonic-gate 			if (lflag) { /* w command needs args */
4207c478bd9Sstevel@tonic-gate 				clnarglist(info.pr_psargs);
4217c478bd9Sstevel@tonic-gate 				(void) strcpy(up->p_args, info.pr_psargs);
4227c478bd9Sstevel@tonic-gate 				if (up->p_args[0] == 0 ||
4237c478bd9Sstevel@tonic-gate 				    up->p_args[0] == '-' &&
4247c478bd9Sstevel@tonic-gate 				    up->p_args[1] <= ' ' ||
4257c478bd9Sstevel@tonic-gate 				    up->p_args[0] == '?') {
4267c478bd9Sstevel@tonic-gate 					(void) strcat(up->p_args, " (");
4277c478bd9Sstevel@tonic-gate 					(void) strcat(up->p_args, up->p_comm);
4287c478bd9Sstevel@tonic-gate 					(void) strcat(up->p_args, ")");
4297c478bd9Sstevel@tonic-gate 				}
4307c478bd9Sstevel@tonic-gate 			}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 		}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		/*
4357c478bd9Sstevel@tonic-gate 		 * link pgrp together in case parents go away
4367c478bd9Sstevel@tonic-gate 		 * Pgrp chain is a single linked list originating
4377c478bd9Sstevel@tonic-gate 		 * from the pgrp leader to its group member.
4387c478bd9Sstevel@tonic-gate 		 */
4397c478bd9Sstevel@tonic-gate 		if (info.pr_pgid != info.pr_pid) {	/* not pgrp leader */
4407c478bd9Sstevel@tonic-gate 			pgrp = findhash(info.pr_pgid);
4417c478bd9Sstevel@tonic-gate 			up->p_pgrplink = pgrp->p_pgrplink;
4427c478bd9Sstevel@tonic-gate 			pgrp->p_pgrplink = up;
4437c478bd9Sstevel@tonic-gate 		}
4447c478bd9Sstevel@tonic-gate 		parent = findhash(info.pr_ppid);
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		/* if this is the new member, link it in */
4477c478bd9Sstevel@tonic-gate 		if (parent->p_upid != INITPROCESS) {
4487c478bd9Sstevel@tonic-gate 			if (parent->p_child) {
4497c478bd9Sstevel@tonic-gate 				up->p_sibling = parent->p_child;
4507c478bd9Sstevel@tonic-gate 				up->p_child = 0;
4517c478bd9Sstevel@tonic-gate 			}
4527c478bd9Sstevel@tonic-gate 			parent->p_child = up;
4537c478bd9Sstevel@tonic-gate 		}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/* revert to non-privileged user */
4587c478bd9Sstevel@tonic-gate 	(void) __priv_relinquish();
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
4617c478bd9Sstevel@tonic-gate 	(void) time(&now);	/* get current time */
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/*
4647c478bd9Sstevel@tonic-gate 	 * loop through utmpx file, printing process info
4657c478bd9Sstevel@tonic-gate 	 * about each logged in user
4667c478bd9Sstevel@tonic-gate 	 */
4677c478bd9Sstevel@tonic-gate 	for (ut = utmpbegin; ut < utmpend; ut++) {
4687c478bd9Sstevel@tonic-gate 		time_t tim;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		if (ut->ut_type != USER_PROCESS)
4717c478bd9Sstevel@tonic-gate 			continue;
4727c478bd9Sstevel@tonic-gate 		if (sel_user && strncmp(ut->ut_name, sel_user, NMAX) != 0)
4737c478bd9Sstevel@tonic-gate 			continue;	/* we're looking for somebody else */
4747c478bd9Sstevel@tonic-gate 		if (lflag) {	/* -l flag format (w command) */
4757c478bd9Sstevel@tonic-gate 			/* print login name of the user */
4767c478bd9Sstevel@tonic-gate 			(void) printf("%-*.*s ", NMAX, NMAX, ut->ut_name);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 			/* print tty user is on */
4797c478bd9Sstevel@tonic-gate 			(void) printf("%-*.*s", LMAX, LMAX, ut->ut_line);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 			/* print when the user logged in */
4827c478bd9Sstevel@tonic-gate 			tim = ut->ut_xtime;
4837c478bd9Sstevel@tonic-gate 			(void) prtat(&tim);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 			/* print idle time */
4867c478bd9Sstevel@tonic-gate 			idle = findidle(ut->ut_line);
4877c478bd9Sstevel@tonic-gate 			if (idle >= 36 * 60)
4887c478bd9Sstevel@tonic-gate 				(void) printf(dcgettext(NULL, "%2ddays ",
4897c478bd9Sstevel@tonic-gate 				    LC_TIME), (idle + 12 * 60) / (24 * 60));
4907c478bd9Sstevel@tonic-gate 			else
4917c478bd9Sstevel@tonic-gate 				prttime(idle, " ");
4927c478bd9Sstevel@tonic-gate 			showtotals(findhash((pid_t)ut->ut_pid));
4937c478bd9Sstevel@tonic-gate 		} else {	/* standard whodo format */
4947c478bd9Sstevel@tonic-gate 			tim = ut->ut_xtime;
4957c478bd9Sstevel@tonic-gate 			tm = localtime(&tim);
4967c478bd9Sstevel@tonic-gate 			(void) printf("\n%-*.*s %-*.*s %2.1d:%2.2d\n",
4977c478bd9Sstevel@tonic-gate 			    LMAX, LMAX, ut->ut_line,
4987c478bd9Sstevel@tonic-gate 			    NMAX, NMAX, ut->ut_name, tm->tm_hour, tm->tm_min);
4997c478bd9Sstevel@tonic-gate 			showproc(findhash((pid_t)ut->ut_pid));
5007c478bd9Sstevel@tonic-gate 		}
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	return (0);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate /*
5077c478bd9Sstevel@tonic-gate  * Used for standard whodo format.
5087c478bd9Sstevel@tonic-gate  * This is the recursive routine descending the process
5097c478bd9Sstevel@tonic-gate  * tree starting from the given process pointer(up).
5107c478bd9Sstevel@tonic-gate  * It used depth-first search strategy and also marked
5117c478bd9Sstevel@tonic-gate  * each node as printed as it traversed down the tree.
5127c478bd9Sstevel@tonic-gate  */
5137c478bd9Sstevel@tonic-gate static void
5147c478bd9Sstevel@tonic-gate showproc(struct uproc *up)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate 	struct	uproc	*zp;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	if (up->p_state == VISITED) /* we already been here */
5197c478bd9Sstevel@tonic-gate 		return;
5207c478bd9Sstevel@tonic-gate 	/* print the data for this process */
5217c478bd9Sstevel@tonic-gate 	if (up->p_state == ZOMBIE)
5227c478bd9Sstevel@tonic-gate 		(void) printf("    %-*.*s %5d %4.1ld:%2.2ld %s\n",
5237c478bd9Sstevel@tonic-gate 		    LMAX, LMAX, "  ?", (int)up->p_upid, 0L, 0L, "<defunct>");
5247c478bd9Sstevel@tonic-gate 	else if (up->p_state != NONE) {
5257c478bd9Sstevel@tonic-gate 		(void) printf("    %-*.*s %5d %4.1ld:%2.2ld %s\n",
5267c478bd9Sstevel@tonic-gate 		    LMAX, LMAX, getty(up->p_ttyd), (int)up->p_upid,
5277c478bd9Sstevel@tonic-gate 		    up->p_time / 60L, up->p_time % 60L,
5287c478bd9Sstevel@tonic-gate 		    up->p_comm);
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 	up->p_state = VISITED;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/* descend for its children */
5337c478bd9Sstevel@tonic-gate 	if (up->p_child) {
5347c478bd9Sstevel@tonic-gate 		showproc(up->p_child);
5357c478bd9Sstevel@tonic-gate 		for (zp = up->p_child->p_sibling; zp; zp = zp->p_sibling) {
5367c478bd9Sstevel@tonic-gate 			showproc(zp);
5377c478bd9Sstevel@tonic-gate 		}
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/* print the pgrp relation */
5417c478bd9Sstevel@tonic-gate 	if (up->p_pgrplink)
5427c478bd9Sstevel@tonic-gate 		showproc(up->p_pgrplink);
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate  * Used for -l flag (w command) format.
5487c478bd9Sstevel@tonic-gate  * Prints the CPU time for all processes & children,
5497c478bd9Sstevel@tonic-gate  * and the cpu time for interesting process,
5507c478bd9Sstevel@tonic-gate  * and what the user is doing.
5517c478bd9Sstevel@tonic-gate  */
5527c478bd9Sstevel@tonic-gate static void
5537c478bd9Sstevel@tonic-gate showtotals(struct uproc *up)
5547c478bd9Sstevel@tonic-gate {
5557c478bd9Sstevel@tonic-gate 	jobtime = 0;
5567c478bd9Sstevel@tonic-gate 	proctime = 0;
5577c478bd9Sstevel@tonic-gate 	empty = 1;
5587c478bd9Sstevel@tonic-gate 	curpid = -1;
5597c478bd9Sstevel@tonic-gate 	(void) strcpy(doing, "-"); /* default act: normally never prints */
5607c478bd9Sstevel@tonic-gate 	calctotals(up);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	/* print CPU time for all processes & children */
5637c478bd9Sstevel@tonic-gate 	/* and need to convert clock ticks to seconds first */
5647c478bd9Sstevel@tonic-gate 	prttime((time_t)jobtime, " ");
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	/* print cpu time for interesting process */
5677c478bd9Sstevel@tonic-gate 	/* and need to convert clock ticks to seconds first */
5687c478bd9Sstevel@tonic-gate 	prttime((time_t)proctime, " ");
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	/* what user is doing, current process */
5717c478bd9Sstevel@tonic-gate 	(void) printf(" %-.32s\n", doing);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate /*
5757c478bd9Sstevel@tonic-gate  *  Used for -l flag (w command) format.
5767c478bd9Sstevel@tonic-gate  *  This recursive routine descends the process
5777c478bd9Sstevel@tonic-gate  *  tree starting from the given process pointer(up).
5787c478bd9Sstevel@tonic-gate  *  It used depth-first search strategy and also marked
5797c478bd9Sstevel@tonic-gate  *  each node as visited as it traversed down the tree.
5807c478bd9Sstevel@tonic-gate  *  It calculates the process time for all processes &
5817c478bd9Sstevel@tonic-gate  *  children.  It also finds the "interesting" process
5827c478bd9Sstevel@tonic-gate  *  and determines its cpu time and command.
5837c478bd9Sstevel@tonic-gate  */
5847c478bd9Sstevel@tonic-gate static void
5857c478bd9Sstevel@tonic-gate calctotals(struct uproc *up)
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate 	struct uproc	*zp;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if (up->p_state == VISITED)
5907c478bd9Sstevel@tonic-gate 		return;
5917c478bd9Sstevel@tonic-gate 	up->p_state = VISITED;
5927c478bd9Sstevel@tonic-gate 	if (up->p_state == NONE || up->p_state == ZOMBIE)
5937c478bd9Sstevel@tonic-gate 		return;
5947c478bd9Sstevel@tonic-gate 	jobtime += up->p_time + up->p_ctime;
5957c478bd9Sstevel@tonic-gate 	proctime += up->p_time;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	if (empty && !up->p_igintr) {
5987c478bd9Sstevel@tonic-gate 		empty = 0;
5997c478bd9Sstevel@tonic-gate 		curpid = -1;
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	if (up->p_upid > curpid && (!up->p_igintr || empty)) {
6037c478bd9Sstevel@tonic-gate 		curpid = up->p_upid;
6047c478bd9Sstevel@tonic-gate 		(void) strcpy(doing, up->p_args);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	/* descend for its children */
6087c478bd9Sstevel@tonic-gate 	if (up->p_child) {
6097c478bd9Sstevel@tonic-gate 		calctotals(up->p_child);
6107c478bd9Sstevel@tonic-gate 		for (zp = up->p_child->p_sibling; zp; zp = zp->p_sibling)
6117c478bd9Sstevel@tonic-gate 			calctotals(zp);
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate static char *
6167c478bd9Sstevel@tonic-gate devadd(char *name, dev_t ddev)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	struct devl *dp;
6197c478bd9Sstevel@tonic-gate 	int leng, start, i;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	if (ndevs == maxdev) {
6227c478bd9Sstevel@tonic-gate 		maxdev += DNINCR;
6237c478bd9Sstevel@tonic-gate 		dp = realloc(devl, maxdev * sizeof (struct devl));
6247c478bd9Sstevel@tonic-gate 		if (!dp) {
6257c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6267c478bd9Sstevel@tonic-gate 			    gettext("%s: out of memory!: %s\n"),
6277c478bd9Sstevel@tonic-gate 			    prog, strerror(errno));
6287c478bd9Sstevel@tonic-gate 			exit(1);
6297c478bd9Sstevel@tonic-gate 		}
6307c478bd9Sstevel@tonic-gate 		devl = dp;
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 	dp = &devl[ndevs++];
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	dp->ddev = ddev;
6357c478bd9Sstevel@tonic-gate 	if (name == NULL) {
6367c478bd9Sstevel@tonic-gate 		(void) strcpy(dp->dname, "  ?  ");
6377c478bd9Sstevel@tonic-gate 		return (dp->dname);
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	leng = strlen(name);
6417c478bd9Sstevel@tonic-gate 	if (leng < DEVNAMELEN + 4) {
6427c478bd9Sstevel@tonic-gate 		/* strip off "/dev/" */
6437c478bd9Sstevel@tonic-gate 		(void) strcpy(dp->dname, &name[5]);
6447c478bd9Sstevel@tonic-gate 	} else {
6457c478bd9Sstevel@tonic-gate 		/* strip enough off the front to fit */
6467c478bd9Sstevel@tonic-gate 		start = leng - DEVNAMELEN - 1;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		for (i = start; i < leng && name[i] != '/'; i++)
6497c478bd9Sstevel@tonic-gate 				;
6507c478bd9Sstevel@tonic-gate 		if (i == leng)
6517c478bd9Sstevel@tonic-gate 			(void) strncpy(dp->dname, &name[start], DEVNAMELEN);
6527c478bd9Sstevel@tonic-gate 		else
6537c478bd9Sstevel@tonic-gate 			(void) strncpy(dp->dname, &name[i+1], DEVNAMELEN);
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 	return (dp->dname);
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate static char *
6597c478bd9Sstevel@tonic-gate devlookup(dev_t ddev)
6607c478bd9Sstevel@tonic-gate {
6617c478bd9Sstevel@tonic-gate 	struct devl *dp;
6627c478bd9Sstevel@tonic-gate 	int i;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	for (dp = devl, i = 0; i < ndevs; dp++, i++) {
6657c478bd9Sstevel@tonic-gate 		if (dp->ddev == ddev)
6667c478bd9Sstevel@tonic-gate 			return (dp->dname);
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate 	return (NULL);
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate /*
6727c478bd9Sstevel@tonic-gate  * This routine gives back a corresponding device name
6737c478bd9Sstevel@tonic-gate  * from the device number given.
6747c478bd9Sstevel@tonic-gate  */
6757c478bd9Sstevel@tonic-gate static char *
6767c478bd9Sstevel@tonic-gate getty(dev_t dev)
6777c478bd9Sstevel@tonic-gate {
6787c478bd9Sstevel@tonic-gate 	extern char *_ttyname_dev(dev_t, char *, size_t);
6797c478bd9Sstevel@tonic-gate 	char devname[TTYNAME_MAX];
6807c478bd9Sstevel@tonic-gate 	char *retval;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	if (dev == PRNODEV)
6837c478bd9Sstevel@tonic-gate 		return ("  ?  ");
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	if ((retval = devlookup(dev)) != NULL)
6867c478bd9Sstevel@tonic-gate 		return (retval);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	retval = _ttyname_dev(dev, devname, sizeof (devname));
6897c478bd9Sstevel@tonic-gate 	return (devadd(retval, dev));
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate /*
6937c478bd9Sstevel@tonic-gate  * Findhash  finds the appropriate entry in the process
6947c478bd9Sstevel@tonic-gate  * hash table (pr_htbl) for the given pid in case that
6957c478bd9Sstevel@tonic-gate  * pid exists on the hash chain. It returns back a pointer
6967c478bd9Sstevel@tonic-gate  * to that uproc structure. If this is a new pid, it allocates
6977c478bd9Sstevel@tonic-gate  * a new node, initializes it, links it into the chain (after
6987c478bd9Sstevel@tonic-gate  * head) and returns a structure pointer.
6997c478bd9Sstevel@tonic-gate  */
7007c478bd9Sstevel@tonic-gate static struct uproc *
7017c478bd9Sstevel@tonic-gate findhash(pid_t pid)
7027c478bd9Sstevel@tonic-gate {
7037c478bd9Sstevel@tonic-gate 	struct uproc *up, *tp;
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	tp = up = &pr_htbl[(int)pid % HSIZE];
7067c478bd9Sstevel@tonic-gate 	if (up->p_upid == 0) {			/* empty slot */
7077c478bd9Sstevel@tonic-gate 		up->p_upid = pid;
7087c478bd9Sstevel@tonic-gate 		up->p_state = NONE;
7097c478bd9Sstevel@tonic-gate 		up->p_child = up->p_sibling = up->p_pgrplink = up->p_link = 0;
7107c478bd9Sstevel@tonic-gate 		return (up);
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 	if (up->p_upid == pid) {		/* found in hash table */
7137c478bd9Sstevel@tonic-gate 		return (up);
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 	for (tp = up->p_link; tp; tp = tp->p_link) {	/* follow chain */
7167c478bd9Sstevel@tonic-gate 		if (tp->p_upid == pid) {
7177c478bd9Sstevel@tonic-gate 			return (tp);
7187c478bd9Sstevel@tonic-gate 		}
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 	tp = malloc(sizeof (*tp));		/* add new node */
7217c478bd9Sstevel@tonic-gate 	if (!tp) {
7227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: out of memory!: %s\n"),
7237c478bd9Sstevel@tonic-gate 		    prog, strerror(errno));
7247c478bd9Sstevel@tonic-gate 		exit(1);
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 	(void) memset((char *)tp, 0, sizeof (*tp));
7277c478bd9Sstevel@tonic-gate 	tp->p_upid = pid;
7287c478bd9Sstevel@tonic-gate 	tp->p_state = NONE;
7297c478bd9Sstevel@tonic-gate 	tp->p_child = tp->p_sibling = tp->p_pgrplink = (pid_t)0;
7307c478bd9Sstevel@tonic-gate 	tp->p_link = up->p_link;		/* insert after head */
7317c478bd9Sstevel@tonic-gate 	up->p_link = tp;
7327c478bd9Sstevel@tonic-gate 	return (tp);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate #define	HR	(60 * 60)
7367c478bd9Sstevel@tonic-gate #define	DAY	(24 * HR)
7377c478bd9Sstevel@tonic-gate #define	MON	(30 * DAY)
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate /*
7407c478bd9Sstevel@tonic-gate  * prints a time in hours and minutes or minutes and seconds.
7417c478bd9Sstevel@tonic-gate  * The character string 'tail' is printed at the end, obvious
7427c478bd9Sstevel@tonic-gate  * strings to pass are "", " ", or "am".
7437c478bd9Sstevel@tonic-gate  */
7447c478bd9Sstevel@tonic-gate static void
7457c478bd9Sstevel@tonic-gate prttime(time_t tim, char *tail)
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate 	if (tim >= 60)
7487c478bd9Sstevel@tonic-gate 		(void) printf(dcgettext(NULL, "%3d:%02d", LC_TIME),
7497c478bd9Sstevel@tonic-gate 		    (int)tim/60, (int)tim%60);
7507c478bd9Sstevel@tonic-gate 	else if (tim > 0)
7517c478bd9Sstevel@tonic-gate 		(void) printf(dcgettext(NULL, "    %2d", LC_TIME), (int)tim);
7527c478bd9Sstevel@tonic-gate 	else
7537c478bd9Sstevel@tonic-gate 		(void) printf("      ");
7547c478bd9Sstevel@tonic-gate 	(void) printf("%s", tail);
7557c478bd9Sstevel@tonic-gate }
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate /*
7597c478bd9Sstevel@tonic-gate  * prints a 12 hour time given a pointer to a time of day
7607c478bd9Sstevel@tonic-gate  */
7617c478bd9Sstevel@tonic-gate static void
7627c478bd9Sstevel@tonic-gate prtat(time_t *time)
7637c478bd9Sstevel@tonic-gate {
7647c478bd9Sstevel@tonic-gate 	struct tm *p;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	p = localtime(time);
7677c478bd9Sstevel@tonic-gate 	if (now - *time <= 18 * HR) {
7687c478bd9Sstevel@tonic-gate 		char timestr[50];
7697c478bd9Sstevel@tonic-gate 		(void) strftime(timestr, sizeof (timestr),
7707c478bd9Sstevel@tonic-gate 		    dcgettext(NULL, " %l:%M""%p", LC_TIME), p);
7717c478bd9Sstevel@tonic-gate 		checkampm(timestr);
7727c478bd9Sstevel@tonic-gate 		(void) printf("%s", timestr);
7737c478bd9Sstevel@tonic-gate 	} else if (now - *time <= 7 * DAY) {
7747c478bd9Sstevel@tonic-gate 		char weekdaytime[20];
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		(void) strftime(weekdaytime, sizeof (weekdaytime),
7777c478bd9Sstevel@tonic-gate 		    dcgettext(NULL, "%a%l%p", LC_TIME), p);
7787c478bd9Sstevel@tonic-gate 		checkampm(weekdaytime);
7797c478bd9Sstevel@tonic-gate 		(void) printf(" %s", weekdaytime);
7807c478bd9Sstevel@tonic-gate 	} else {
7817c478bd9Sstevel@tonic-gate 		char monthtime[20];
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 		(void) strftime(monthtime, sizeof (monthtime),
7847c478bd9Sstevel@tonic-gate 		    dcgettext(NULL, "%e%b%y", LC_TIME), p);
7857c478bd9Sstevel@tonic-gate 		(void) printf(" %s", monthtime);
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate  * find & return number of minutes current tty has been idle
7917c478bd9Sstevel@tonic-gate  */
7927c478bd9Sstevel@tonic-gate static time_t
7937c478bd9Sstevel@tonic-gate findidle(char *devname)
7947c478bd9Sstevel@tonic-gate {
7957c478bd9Sstevel@tonic-gate 	struct stat stbuf;
7967c478bd9Sstevel@tonic-gate 	time_t lastaction, diff;
7977c478bd9Sstevel@tonic-gate 	char ttyname[64];
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	(void) strcpy(ttyname, "/dev/");
8007c478bd9Sstevel@tonic-gate 	(void) strcat(ttyname, devname);
8017c478bd9Sstevel@tonic-gate 	if (stat(ttyname, &stbuf) != -1) {
8027c478bd9Sstevel@tonic-gate 		lastaction = stbuf.st_atime;
8037c478bd9Sstevel@tonic-gate 		diff = now - lastaction;
8047c478bd9Sstevel@tonic-gate 		diff = DIV60(diff);
8057c478bd9Sstevel@tonic-gate 		if (diff < 0)
8067c478bd9Sstevel@tonic-gate 			diff = 0;
8077c478bd9Sstevel@tonic-gate 	} else
8087c478bd9Sstevel@tonic-gate 		diff = 0;
8097c478bd9Sstevel@tonic-gate 	return (diff);
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate /*
8137c478bd9Sstevel@tonic-gate  * given a pointer to the argument string clean out unsavory characters.
8147c478bd9Sstevel@tonic-gate  */
8157c478bd9Sstevel@tonic-gate static void
8167c478bd9Sstevel@tonic-gate clnarglist(char *arglist)
8177c478bd9Sstevel@tonic-gate {
8187c478bd9Sstevel@tonic-gate 	char	*c;
8197c478bd9Sstevel@tonic-gate 	int	err = 0;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	/* get rid of unsavory characters */
8227c478bd9Sstevel@tonic-gate 	for (c = arglist; *c == NULL; c++) {
8237c478bd9Sstevel@tonic-gate 		if ((*c < ' ') || (*c > 0176)) {
8247c478bd9Sstevel@tonic-gate 			if (err++ > 5) {
8257c478bd9Sstevel@tonic-gate 				*arglist = NULL;
8267c478bd9Sstevel@tonic-gate 				break;
8277c478bd9Sstevel@tonic-gate 			}
8287c478bd9Sstevel@tonic-gate 			*c = '?';
8297c478bd9Sstevel@tonic-gate 		}
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate /* replaces all occurences of AM/PM with am/pm */
8347c478bd9Sstevel@tonic-gate static void
8357c478bd9Sstevel@tonic-gate checkampm(char *str)
8367c478bd9Sstevel@tonic-gate {
8377c478bd9Sstevel@tonic-gate 	char *ampm;
8387c478bd9Sstevel@tonic-gate 	while ((ampm = strstr(str, "AM")) != NULL ||
8397c478bd9Sstevel@tonic-gate 	    (ampm = strstr(str, "PM")) != NULL) {
8407c478bd9Sstevel@tonic-gate 		*ampm = tolower(*ampm);
8417c478bd9Sstevel@tonic-gate 		*(ampm+1) = tolower(*(ampm+1));
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate }
844