xref: /illumos-gate/usr/src/cmd/w/w.c (revision 174b8e45)
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
576e222fdSSumanth Naropanth  * Common Development and Distribution License (the "License").
676e222fdSSumanth 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 /*
220a1278f2SGary Mills  * Copyright (c) 2013 Gary Mills
230a1278f2SGary Mills  *
2476e222fdSSumanth Naropanth  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
266a79a301SJason King  *
276a79a301SJason King  * Copyright 2020 Joyent, Inc.
287c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
31352d7aedSToomas Soome /*	  All Rights Reserved	*/
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
357c478bd9Sstevel@tonic-gate  * The Regents of the University of California
367c478bd9Sstevel@tonic-gate  * All Rights Reserved
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
397c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
407c478bd9Sstevel@tonic-gate  * contributors.
417c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * This is the new w command which takes advantage of
457c478bd9Sstevel@tonic-gate  * the /proc interface to gain access to the information
467c478bd9Sstevel@tonic-gate  * of all the processes currently on the system.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * This program also implements 'uptime'.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * Maintenance note:
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * Much of this code is replicated in whodo.c.  If you're
537c478bd9Sstevel@tonic-gate  * fixing bugs here, then you should probably fix 'em there too.
547c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate #include <sys/types.h>
573f764e12SYuri Pankov #include <sys/loadavg.h>
583f764e12SYuri Pankov #include <sys/queue.h>
597c478bd9Sstevel@tonic-gate #include <sys/stat.h>
603f764e12SYuri Pankov #include <sys/sysmacros.h>
613f764e12SYuri Pankov 
623f764e12SYuri Pankov #include <ctype.h>
637c478bd9Sstevel@tonic-gate #include <dirent.h>
643f764e12SYuri Pankov #include <err.h>
653f764e12SYuri Pankov #include <errno.h>
663f764e12SYuri Pankov #include <fcntl.h>
677c478bd9Sstevel@tonic-gate #include <limits.h>
683f764e12SYuri Pankov #include <locale.h>
697c478bd9Sstevel@tonic-gate #include <priv_utils.h>
703f764e12SYuri Pankov #include <procfs.h>		/* /proc header file */
713f764e12SYuri Pankov #include <stdarg.h>
723f764e12SYuri Pankov #include <stdio.h>
733f764e12SYuri Pankov #include <stdlib.h>
743f764e12SYuri Pankov #include <string.h>
753f764e12SYuri Pankov #include <time.h>
763f764e12SYuri Pankov #include <unistd.h>
773f764e12SYuri Pankov #include <utmpx.h>
797c478bd9Sstevel@tonic-gate /*
800a1278f2SGary Mills  * Use the full lengths from utmpx for user and line.
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate static struct utmpx dummy;
837c478bd9Sstevel@tonic-gate #define	NMAX		(sizeof (dummy.ut_user))
847c478bd9Sstevel@tonic-gate #define	LMAX		(sizeof (dummy.ut_line))
850a1278f2SGary Mills 
860a1278f2SGary Mills /* Print minimum field widths. */
870a1278f2SGary Mills #define	LOGIN_WIDTH	8
88f0540631SGary Mills #define	LINE_WIDTH	8
907c478bd9Sstevel@tonic-gate #define	DIV60(t)	((t+30)/60)	/* x/60 rounded */
927c478bd9Sstevel@tonic-gate #define	PROCDIR		"/proc"
937c478bd9Sstevel@tonic-gate #define	PRINTF(a)	if (printf a < 0) { \
947c478bd9Sstevel@tonic-gate 		perror((gettext("%s: printf failed"), prog)); \
957c478bd9Sstevel@tonic-gate 		exit(1); }
977c478bd9Sstevel@tonic-gate struct uproc {
987c478bd9Sstevel@tonic-gate 	pid_t	p_upid;			/* process id */
997c478bd9Sstevel@tonic-gate 	dev_t   p_ttyd;			/* controlling tty of process */
1007c478bd9Sstevel@tonic-gate 	time_t  p_time;			/* seconds of user & system time */
1017c478bd9Sstevel@tonic-gate 	time_t	p_ctime;		/* seconds of child user & sys time */
1027c478bd9Sstevel@tonic-gate 	int	p_igintr;		/* 1 = ignores SIGQUIT and SIGINT */
1037c478bd9Sstevel@tonic-gate 	char    p_comm[PRARGSZ+1];	/* command */
1047c478bd9Sstevel@tonic-gate 	char    p_args[PRARGSZ+1];	/* command line arguments */
1053f764e12SYuri Pankov 	STAILQ_ENTRY(uproc) uprocs;
1067c478bd9Sstevel@tonic-gate };
1073f764e12SYuri Pankov STAILQ_HEAD(uprochead, uproc) uphead;
109352d7aedSToomas Soome static time_t	findidle(char *);
1107c478bd9Sstevel@tonic-gate static void	clnarglist(char *);
111f0540631SGary Mills static void	prttime(time_t, int);
1127c478bd9Sstevel@tonic-gate static void	prtat(time_t *time);
1146a79a301SJason King static int	priv_proc_open(const char *, int);
1156a79a301SJason King static int	priv_proc_openat(int, const char *, int);
1166a79a301SJason King static boolean_t do_proc_read(int, void *, size_t);
1176a79a301SJason King 
1187c478bd9Sstevel@tonic-gate static char	*prog;		/* pointer to invocation name */
1197c478bd9Sstevel@tonic-gate static int	header = 1;	/* true if -h flag: don't print heading */
1207c478bd9Sstevel@tonic-gate static int	lflag = 1;	/* set if -l flag; 0 for -s flag: short form */
1217c478bd9Sstevel@tonic-gate static char	*sel_user;	/* login of particular user selected */
122352d7aedSToomas Soome static char	firstchar;	/* first char of name of prog invoked as */
1237c478bd9Sstevel@tonic-gate static int	login;		/* true if invoked as login shell */
1247c478bd9Sstevel@tonic-gate static time_t	now;		/* current time of day */
1257c478bd9Sstevel@tonic-gate static time_t	uptime;		/* time of last reboot & elapsed time since */
1267c478bd9Sstevel@tonic-gate static int	nusers;		/* number of users logged in now */
1286a79a301SJason King /*
1296a79a301SJason King  * Basic privs we never need and can drop. This is likely not exhaustive,
1306a79a301SJason King  * but should significantly reduce any potential attack surfaces.
1316a79a301SJason King  */
1326a79a301SJason King static const char *drop_privs[] = {
1336a79a301SJason King 	PRIV_FILE_WRITE,
1346a79a301SJason King 	PRIV_NET_ACCESS,
1356a79a301SJason King 	PRIV_PROC_EXEC,
1366a79a301SJason King 	PRIV_PROC_FORK,
1376a79a301SJason King 	PRIV_FILE_LINK_ANY
1386a79a301SJason King };
1396a79a301SJason King 
1407c478bd9Sstevel@tonic-gate #if SIGQUIT > SIGINT
1417c478bd9Sstevel@tonic-gate #define	ACTSIZE	SIGQUIT
1427c478bd9Sstevel@tonic-gate #else
1437c478bd9Sstevel@tonic-gate #define	ACTSIZE	SIGINT
1447c478bd9Sstevel@tonic-gate #endif
1467c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1477c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	struct utmpx	*ut;
1507c478bd9Sstevel@tonic-gate 	struct utmpx	*utmpbegin;
1517c478bd9Sstevel@tonic-gate 	struct utmpx	*utmpend;
1527c478bd9Sstevel@tonic-gate 	struct utmpx	*utp;
1533f764e12SYuri Pankov 	struct uproc	*up;
1547c478bd9Sstevel@tonic-gate 	struct psinfo	info;
1557c478bd9Sstevel@tonic-gate 	struct sigaction actinfo[ACTSIZE];
1567c478bd9Sstevel@tonic-gate 	struct pstatus	statinfo;
1577c478bd9Sstevel@tonic-gate 	struct stat	sbuf;
158352d7aedSToomas Soome 	DIR		*dirp;
1593f764e12SYuri Pankov 	struct dirent	*dp;
1606a79a301SJason King 	char		pname[PATH_MAX];
1617c478bd9Sstevel@tonic-gate 	int		procfd;
1626a79a301SJason King 	int		dirfd;
1637c478bd9Sstevel@tonic-gate 	char		*cp;
1647c478bd9Sstevel@tonic-gate 	int		i;
1657c478bd9Sstevel@tonic-gate 	int		days, hrs, mins;
1667c478bd9Sstevel@tonic-gate 	int		entries;
1677c478bd9Sstevel@tonic-gate 	double		loadavg[3];
1686a79a301SJason King 	priv_set_t	*pset;
1696a79a301SJason King 
1706a79a301SJason King 	if (__init_suid_priv(PU_CLEARLIMITSET, PRIV_PROC_OWNER, NULL) != 0) {
1716a79a301SJason King 		err(EXIT_FAILURE, "failed to enable privilege bracketing");
1726a79a301SJason King 	}
1736a79a301SJason King 
1746a79a301SJason King 	/*
1756a79a301SJason King 	 * After setting up privilege bracketing, we can further reduce the
1766a79a301SJason King 	 * privileges in use. The effective set is set to the basic set minus
1776a79a301SJason King 	 * the privs in drop_privs. The permitted set is the effective set
1786a79a301SJason King 	 * plus PRIV_PROC_OWNER (i.e. the privilege being bracketed).
1796a79a301SJason King 	 */
1806a79a301SJason King 	pset = priv_allocset();
1816a79a301SJason King 	if (pset == NULL)
1826a79a301SJason King 		err(EXIT_FAILURE, "priv_allocset failed");
1836a79a301SJason King 
1846a79a301SJason King 	priv_basicset(pset);
1856a79a301SJason King 	for (i = 0; i < ARRAY_SIZE(drop_privs); i++) {
1866a79a301SJason King 		if (priv_delset(pset, drop_privs[i]) != 0) {
1876a79a301SJason King 			err(EXIT_FAILURE,
1886a79a301SJason King 			    "failed to remove %s privilege from privilege set",
1896a79a301SJason King 			    drop_privs[i]);
1906a79a301SJason King 		}
1916a79a301SJason King 	}
1926a79a301SJason King 
1936a79a301SJason King 	if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pset) < 0)
1946a79a301SJason King 		err(EXIT_FAILURE, "failed setting effective privilege set");
1956a79a301SJason King 
1966a79a301SJason King 	if (priv_addset(pset, PRIV_PROC_OWNER) != 0) {
1976a79a301SJason King 		err(EXIT_FAILURE,
1986a79a301SJason King 		    "failed to add PRIV_PROC_OWNER privilege to privilege set");
1996a79a301SJason King 	}
2006a79a301SJason King 
2016a79a301SJason King 	if (setppriv(PRIV_SET, PRIV_PERMITTED, pset) < 0)
2026a79a301SJason King 		err(EXIT_FAILURE, "failed to set permitted privilege set");
2047c478bd9Sstevel@tonic-gate 	/*
2056a79a301SJason King 	 * Unfortunately, when run as root, privilege bracketing is a no-op,
2066a79a301SJason King 	 * so we have to add PRIV_PROC_OWNER into our effective set for things
2076a79a301SJason King 	 * to work.
2087c478bd9Sstevel@tonic-gate 	 */
2096a79a301SJason King 	if (getuid() == 0 && setppriv(PRIV_SET, PRIV_EFFECTIVE, pset) < 0) {
2106a79a301SJason King 		err(EXIT_FAILURE, "failed to set effective privilege set");
2116a79a301SJason King 	}
2126a79a301SJason King 
2136a79a301SJason King 	priv_freeset(pset);
2146a79a301SJason King 	pset = NULL;
2167c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2177c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2187c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
2197c478bd9Sstevel@tonic-gate #endif
2207c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2227c478bd9Sstevel@tonic-gate 	login = (argv[0][0] == '-');
2237c478bd9Sstevel@tonic-gate 	cp = strrchr(argv[0], '/');
2247c478bd9Sstevel@tonic-gate 	firstchar = login ? argv[0][1] : (cp == 0) ? argv[0][0] : cp[1];
2257c478bd9Sstevel@tonic-gate 	prog = argv[0];
2277c478bd9Sstevel@tonic-gate 	while (argc > 1) {
2287c478bd9Sstevel@tonic-gate 		if (argv[1][0] == '-') {
2297c478bd9Sstevel@tonic-gate 			for (i = 1; argv[1][i]; i++) {
2307c478bd9Sstevel@tonic-gate 				switch (argv[1][i]) {
2327c478bd9Sstevel@tonic-gate 				case 'h':
2337c478bd9Sstevel@tonic-gate 					header = 0;
2347c478bd9Sstevel@tonic-gate 					break;
2367c478bd9Sstevel@tonic-gate 				case 'l':
2377c478bd9Sstevel@tonic-gate 					lflag++;
2387c478bd9Sstevel@tonic-gate 					break;
2397c478bd9Sstevel@tonic-gate 				case 's':
2407c478bd9Sstevel@tonic-gate 					lflag = 0;
2417c478bd9Sstevel@tonic-gate 					break;
2437c478bd9Sstevel@tonic-gate 				case 'u':
2447c478bd9Sstevel@tonic-gate 				case 'w':
2457c478bd9Sstevel@tonic-gate 					firstchar = argv[1][i];
2467c478bd9Sstevel@tonic-gate 					break;
2487c478bd9Sstevel@tonic-gate 				default:
2497c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2507c478bd9Sstevel@tonic-gate 					    "%s: bad flag %s\n"),
2517c478bd9Sstevel@tonic-gate 					    prog, argv[1]);
2527c478bd9Sstevel@tonic-gate 					exit(1);
2537c478bd9Sstevel@tonic-gate 				}
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 		} else {
2567c478bd9Sstevel@tonic-gate 			if (!isalnum(argv[1][0]) || argc > 2) {
2577c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
2587c478bd9Sstevel@tonic-gate 				    "usage: %s [ -hlsuw ] [ user ]\n"), prog);
2597c478bd9Sstevel@tonic-gate 				exit(1);
2607c478bd9Sstevel@tonic-gate 			} else
2617c478bd9Sstevel@tonic-gate 				sel_user = argv[1];
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 		argc--; argv++;
2647c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	/*
2671eabc4beSSachidananda Urs 	 * read the UTMPX_FILE (contains information about each logged in user)
2687c478bd9Sstevel@tonic-gate 	 */
2696a79a301SJason King 	if (stat(UTMPX_FILE, &sbuf) < 0)
2706a79a301SJason King 		err(EXIT_FAILURE, gettext("stat error of %s"), UTMPX_FILE);
2716a79a301SJason King 
2727c478bd9Sstevel@tonic-gate 	entries = sbuf.st_size / sizeof (struct futmpx);
2736a79a301SJason King 	if ((ut = calloc(entries, sizeof (struct utmpx))) == NULL)
2746a79a301SJason King 		err(EXIT_FAILURE, gettext("calloc error of %s"), UTMPX_FILE);
2767c478bd9Sstevel@tonic-gate 	(void) utmpxname(UTMPX_FILE);
2787c478bd9Sstevel@tonic-gate 	utmpbegin = ut;
2796a79a301SJason King 	utmpend = utmpbegin + entries;
2817c478bd9Sstevel@tonic-gate 	setutxent();
28276e222fdSSumanth Naropanth 	while ((ut < utmpend) && ((utp = getutxent()) != NULL))
2837c478bd9Sstevel@tonic-gate 		(void) memcpy(ut++, utp, sizeof (*ut));
2847c478bd9Sstevel@tonic-gate 	endutxent();
2867c478bd9Sstevel@tonic-gate 	(void) time(&now);	/* get current time */
2887c478bd9Sstevel@tonic-gate 	if (header) {	/* print a header */
2897c478bd9Sstevel@tonic-gate 		prtat(&now);
2907c478bd9Sstevel@tonic-gate 		for (ut = utmpbegin; ut < utmpend; ut++) {
2917c478bd9Sstevel@tonic-gate 			if (ut->ut_type == USER_PROCESS) {
2921eabc4beSSachidananda Urs 				if (!nonuserx(*ut))
2937c478bd9Sstevel@tonic-gate 					nusers++;
2947c478bd9Sstevel@tonic-gate 			} else if (ut->ut_type == BOOT_TIME) {
2957c478bd9Sstevel@tonic-gate 				uptime = now - ut->ut_xtime;
2967c478bd9Sstevel@tonic-gate 				uptime += 30;
2977c478bd9Sstevel@tonic-gate 				days = uptime / (60*60*24);
2987c478bd9Sstevel@tonic-gate 				uptime %= (60*60*24);
2997c478bd9Sstevel@tonic-gate 				hrs = uptime / (60*60);
3007c478bd9Sstevel@tonic-gate 				uptime %= (60*60);
3017c478bd9Sstevel@tonic-gate 				mins = uptime / 60;
303f0540631SGary Mills 				PRINTF((gettext("up")));
3047c478bd9Sstevel@tonic-gate 				if (days > 0)
3057c478bd9Sstevel@tonic-gate 					PRINTF((gettext(
3067c478bd9Sstevel@tonic-gate 					    " %d day(s),"), days));
3077c478bd9Sstevel@tonic-gate 				if (hrs > 0 && mins > 0) {
3087c478bd9Sstevel@tonic-gate 					PRINTF((" %2d:%02d,", hrs, mins));
3097c478bd9Sstevel@tonic-gate 				} else {
310*174b8e45SRick V 					if (hrs > 0) {
3117c478bd9Sstevel@tonic-gate 						PRINTF((gettext(
3127c478bd9Sstevel@tonic-gate 						    " %d hr(s),"), hrs));
313*174b8e45SRick V 					} else { /* mins can be zero */
3147c478bd9Sstevel@tonic-gate 						PRINTF((gettext(
3157c478bd9Sstevel@tonic-gate 						    " %d min(s),"), mins));
316*174b8e45SRick V 					}
3177c478bd9Sstevel@tonic-gate 				}
3187c478bd9Sstevel@tonic-gate 			}
3197c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 		ut = utmpbegin;	/* rewind utmp data */
3227c478bd9Sstevel@tonic-gate 		PRINTF((((nusers == 1) ?
3237c478bd9Sstevel@tonic-gate 		    gettext("  %d user") : gettext("  %d users")), nusers));
3247c478bd9Sstevel@tonic-gate 		/*
3257c478bd9Sstevel@tonic-gate 		 * Print 1, 5, and 15 minute load averages.
3267c478bd9Sstevel@tonic-gate 		 */
3277c478bd9Sstevel@tonic-gate 		(void) getloadavg(loadavg, 3);
3287c478bd9Sstevel@tonic-gate 		PRINTF((gettext(",  load average: %.2f, %.2f, %.2f\n"),
3297c478bd9Sstevel@tonic-gate 		    loadavg[LOADAVG_1MIN], loadavg[LOADAVG_5MIN],
3307c478bd9Sstevel@tonic-gate 		    loadavg[LOADAVG_15MIN]));
3327c478bd9Sstevel@tonic-gate 		if (firstchar == 'u')	/* uptime command */
3337c478bd9Sstevel@tonic-gate 			exit(0);
3357c478bd9Sstevel@tonic-gate 		if (lflag) {
336f0540631SGary Mills 			PRINTF((dcgettext(NULL, "User     tty      "
337f0540631SGary Mills 			    "login@         idle    JCPU    PCPU what\n",
338f0540631SGary Mills 			    LC_TIME)));
3397c478bd9Sstevel@tonic-gate 		} else {
3407c478bd9Sstevel@tonic-gate 			PRINTF((dcgettext(NULL,
341f0540631SGary Mills 			    "User     tty         idle what\n",
342f0540631SGary Mills 			    LC_TIME)));
3437c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 		if (fflush(stdout) == EOF) {
3466a79a301SJason King 			err(EXIT_FAILURE, "fflush failed");
3477c478bd9Sstevel@tonic-gate 		}
3487c478bd9Sstevel@tonic-gate 	}
3503f764e12SYuri Pankov 	/* Loop through /proc, reading info about each process */
3516a79a301SJason King 	if ((dirp = opendir(PROCDIR)) == NULL)
3526a79a301SJason King 		err(EXIT_FAILURE, gettext("could not open %s"), PROCDIR);
3543f764e12SYuri Pankov 	STAILQ_INIT(&uphead);
3557c478bd9Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != NULL) {
3567c478bd9Sstevel@tonic-gate 		if (dp->d_name[0] == '.')
3577c478bd9Sstevel@tonic-gate 			continue;
3586a79a301SJason King 
3596a79a301SJason King 		if (snprintf(pname, sizeof (pname), "%s/%s", PROCDIR,
3606a79a301SJason King 		    dp->d_name) > sizeof (pname))
3617c478bd9Sstevel@tonic-gate 			continue;
3626a79a301SJason King 
3636a79a301SJason King 		dirfd = priv_proc_open(pname, O_RDONLY | O_DIRECTORY);
36445de8795SJason King 		if (dirfd < 0)
3656a79a301SJason King 			continue;
3666a79a301SJason King 
3676a79a301SJason King 		procfd = priv_proc_openat(dirfd, "psinfo", O_RDONLY);
3686a79a301SJason King 		if (procfd < 0) {
3696a79a301SJason King 			(void) close(dirfd);
3706a79a301SJason King 			continue;
3716a79a301SJason King 		}
3726a79a301SJason King 		if (!do_proc_read(procfd, &info, sizeof (info))) {
3733f764e12SYuri Pankov 			warn(gettext("failed to read %s"), pname);
3746a79a301SJason King 			(void) close(dirfd);
3757c478bd9Sstevel@tonic-gate 			continue;
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 		(void) close(procfd);
3793f764e12SYuri Pankov 		/* Not interested in zombies */
3803f764e12SYuri Pankov 		if (info.pr_nlwp == 0)
3813f764e12SYuri Pankov 			continue;
3823f764e12SYuri Pankov 		/* Not interested in processes without a terminal */
3833f764e12SYuri Pankov 		if (info.pr_ttydev == NODEV)
3843f764e12SYuri Pankov 			continue;
3863f764e12SYuri Pankov 		procfd = priv_proc_openat(dirfd, "status", O_RDONLY);
3873f764e12SYuri Pankov 		if (procfd < 0) {
3883f764e12SYuri Pankov 			(void) close(dirfd);
3893f764e12SYuri Pankov 			continue;
3903f764e12SYuri Pankov 		}
3913f764e12SYuri Pankov 		if (!do_proc_read(procfd, &statinfo, sizeof (statinfo))) {
3923f764e12SYuri Pankov 			warn(gettext("failed to read %s/status"), pname);
3937c478bd9Sstevel@tonic-gate 			(void) close(procfd);
3946a79a301SJason King 			(void) close(dirfd);
3953f764e12SYuri Pankov 			continue;
3967c478bd9Sstevel@tonic-gate 		}
3973f764e12SYuri Pankov 		(void) close(procfd);
3993f764e12SYuri Pankov 		procfd = priv_proc_openat(dirfd, "sigact", O_RDONLY);
4003f764e12SYuri Pankov 		if (procfd < 0) {
4013f764e12SYuri Pankov 			(void) close(dirfd);
4023f764e12SYuri Pankov 			continue;
4033f764e12SYuri Pankov 		}
4043f764e12SYuri Pankov 		if (!do_proc_read(procfd, actinfo, sizeof (actinfo))) {
4053f764e12SYuri Pankov 			warn(gettext("failed to read %s/sigact"), pname);
4063f764e12SYuri Pankov 			(void) close(procfd);
4073f764e12SYuri Pankov 			(void) close(dirfd);
4083f764e12SYuri Pankov 			continue;
4097c478bd9Sstevel@tonic-gate 		}
4103f764e12SYuri Pankov 		(void) close(procfd);
4113f764e12SYuri Pankov 		(void) close(dirfd);
4133f764e12SYuri Pankov 		up = calloc(1, sizeof (*up));
4143f764e12SYuri Pankov 		if (up == NULL)
4153f764e12SYuri Pankov 			err(EXIT_FAILURE, "calloc");
4163f764e12SYuri Pankov 		up->p_upid = info.pr_pid;
4173f764e12SYuri Pankov 		up->p_ttyd = info.pr_ttydev;
4183f764e12SYuri Pankov 		up->p_time =
4193f764e12SYuri Pankov 		    statinfo.pr_utime.tv_sec +
4203f764e12SYuri Pankov 		    statinfo.pr_stime.tv_sec;
4213f764e12SYuri Pankov 		up->p_ctime =
4223f764e12SYuri Pankov 		    statinfo.pr_cutime.tv_sec +
4233f764e12SYuri Pankov 		    statinfo.pr_cstime.tv_sec;
4243f764e12SYuri Pankov 		up->p_igintr =
4253f764e12SYuri Pankov 		    actinfo[SIGINT-1].sa_handler == SIG_IGN &&
4263f764e12SYuri Pankov 		    actinfo[SIGQUIT-1].sa_handler == SIG_IGN;
4273f764e12SYuri Pankov 		(void) strlcpy(up->p_comm, info.pr_fname, sizeof (up->p_comm));
4283f764e12SYuri Pankov 		/* Process args */
4293f764e12SYuri Pankov 		clnarglist(info.pr_psargs);
4303f764e12SYuri Pankov 		(void) strlcpy(up->p_args, info.pr_psargs, sizeof (up->p_args));
4313f764e12SYuri Pankov 		if (up->p_args[0] == 0 || up->p_args[0] == '?' ||
4323f764e12SYuri Pankov 		    (up->p_args[0] == '-' && up->p_args[1] <= ' ')) {
4333f764e12SYuri Pankov 			(void) strlcat(up->p_args, " (", sizeof (up->p_args));
4343f764e12SYuri Pankov 			(void) strlcat(up->p_args, up->p_comm,
4353f764e12SYuri Pankov 			    sizeof (up->p_args));
4363f764e12SYuri Pankov 			(void) strlcat(up->p_args, ")", sizeof (up->p_args));
4377c478bd9Sstevel@tonic-gate 		}
4383f764e12SYuri Pankov 		STAILQ_INSERT_TAIL(&uphead, up, uprocs);
4397c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 	/* revert to non-privileged user after opening */
4426a79a301SJason King 	__priv_relinquish();
4436a79a301SJason King 	if (getuid() == 0) {
4446a79a301SJason King 		/*
4456a79a301SJason King 		 * Since the privilege bracketing functions are effectively
4466a79a301SJason King 		 * no-ops when running as root, we must explicitly
4476a79a301SJason King 		 * relinquish PRIV_PROC_OWNER ourselves.
4486a79a301SJason King 		 */
4496a79a301SJason King 		pset = priv_allocset();
4506a79a301SJason King 		if (pset == NULL) {
4516a79a301SJason King 			err(EXIT_FAILURE,
4526a79a301SJason King 			    gettext("failed to allocate privilege set"));
4536a79a301SJason King 		}
4546a79a301SJason King 
4556a79a301SJason King 		priv_emptyset(pset);
4566a79a301SJason King 
4576a79a301SJason King 		if (priv_addset(pset, PRIV_PROC_OWNER) != 0) {
4586a79a301SJason King 			err(EXIT_FAILURE, gettext("failed to add "
4596a79a301SJason King 			    "PRIV_PROC_OWNER to privilege set"));
4606a79a301SJason King 		}
4616a79a301SJason King 
4626a79a301SJason King 		if (setppriv(PRIV_OFF, PRIV_PERMITTED, pset) != 0) {
4636a79a301SJason King 			err(EXIT_FAILURE,
4646a79a301SJason King 			    gettext("failed to set permitted privilege set"));
4656a79a301SJason King 		}
4666a79a301SJason King 
4676a79a301SJason King 		priv_freeset(pset);
4686a79a301SJason King 		pset = NULL;
4696a79a301SJason King 	}
4717c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
4727c478bd9Sstevel@tonic-gate 	(void) time(&now);	/* get current time */
4747c478bd9Sstevel@tonic-gate 	/*
4757c478bd9Sstevel@tonic-gate 	 * loop through utmpx file, printing process info
4767c478bd9Sstevel@tonic-gate 	 * about each logged in user
4777c478bd9Sstevel@tonic-gate 	 */
4787c478bd9Sstevel@tonic-gate 	for (ut = utmpbegin; ut < utmpend; ut++) {
4793f764e12SYuri Pankov 		struct uproc *upt;
4803f764e12SYuri Pankov 		char linedev[PATH_MAX];
4813f764e12SYuri Pankov 		char what[1024];
4823f764e12SYuri Pankov 		time_t idle, jobtime, proctime;
4833f764e12SYuri Pankov 		pid_t curpid;
4843f764e12SYuri Pankov 
4857c478bd9Sstevel@tonic-gate 		if (ut->ut_type != USER_PROCESS)
4867c478bd9Sstevel@tonic-gate 			continue;
4873f764e12SYuri Pankov 		if (sel_user != NULL &&
4883f764e12SYuri Pankov 		    strncmp(ut->ut_name, sel_user, NMAX) != 0)
4893f764e12SYuri Pankov 			continue;
4917c478bd9Sstevel@tonic-gate 		/* print login name of the user */
4920a1278f2SGary Mills 		PRINTF(("%-*.*s ", LOGIN_WIDTH, NMAX, ut->ut_name));
4947c478bd9Sstevel@tonic-gate 		/* print tty user is on */
4957c478bd9Sstevel@tonic-gate 		if (lflag) {
496f0540631SGary Mills 			PRINTF(("%-*.*s ", LINE_WIDTH, LMAX, ut->ut_line));
4977c478bd9Sstevel@tonic-gate 		} else {
4983f764e12SYuri Pankov 			if (strncmp(ut->ut_line, "pts/", strlen("pts/")) == 0) {
499f0540631SGary Mills 				PRINTF(("%-*.*s ", LINE_WIDTH, LMAX,
500f0540631SGary Mills 				    &ut->ut_line[4]));
5017c478bd9Sstevel@tonic-gate 			} else {
502f0540631SGary Mills 				PRINTF(("%-*.*s ", LINE_WIDTH, LMAX,
5030a1278f2SGary Mills 				    ut->ut_line));
5047c478bd9Sstevel@tonic-gate 			}
5057c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 		/* print when the user logged in */
5087c478bd9Sstevel@tonic-gate 		if (lflag) {
5097c478bd9Sstevel@tonic-gate 			time_t tim = ut->ut_xtime;
5107c478bd9Sstevel@tonic-gate 			prtat(&tim);
5117c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 		/* print idle time */
5147c478bd9Sstevel@tonic-gate 		idle = findidle(ut->ut_line);
515f0540631SGary Mills 		prttime(idle, 8);
5173f764e12SYuri Pankov 		/*
5183f764e12SYuri Pankov 		 * Go through the list of processes for this terminal,
5193f764e12SYuri Pankov 		 * calculating job/process times, and look for the
5203f764e12SYuri Pankov 		 * "most interesting" process.
5213f764e12SYuri Pankov 		 */
5223f764e12SYuri Pankov 		jobtime = 0;
5233f764e12SYuri Pankov 		proctime = 0;
5247c478bd9Sstevel@tonic-gate 		curpid = -1;
5253f764e12SYuri Pankov 		(void) strlcpy(what, "-", sizeof (what));
5273f764e12SYuri Pankov 		(void) snprintf(linedev, sizeof (linedev), "/dev/%s",
5283f764e12SYuri Pankov 		    ut->ut_line);
5293f764e12SYuri Pankov 		if (stat(linedev, &sbuf) == -1 ||
5303f764e12SYuri Pankov 		    (sbuf.st_mode & S_IFMT) != S_IFCHR ||
5313f764e12SYuri Pankov 		    sbuf.st_rdev == NODEV)
5323f764e12SYuri Pankov 			goto skip;
5343f764e12SYuri Pankov 		STAILQ_FOREACH_SAFE(up, &uphead, uprocs, upt) {
5353f764e12SYuri Pankov 			if (up->p_ttyd != sbuf.st_rdev)
5363f764e12SYuri Pankov 				continue;
5373f764e12SYuri Pankov 			jobtime += up->p_time + up->p_ctime;
5383f764e12SYuri Pankov 			proctime += up->p_time;
5393f764e12SYuri Pankov 			/*
5403f764e12SYuri Pankov 			 * Check for "most interesting" process, currently
5413f764e12SYuri Pankov 			 * the one having the highest PID.
5423f764e12SYuri Pankov 			 */
5433f764e12SYuri Pankov 			if (up->p_upid > curpid && !up->p_igintr) {
5443f764e12SYuri Pankov 				curpid = up->p_upid;
5453f764e12SYuri Pankov 				if (lflag) {
5463f764e12SYuri Pankov 					(void) strlcpy(what, up->p_args,
5473f764e12SYuri Pankov 					    sizeof (what));
5483f764e12SYuri Pankov 				} else {
5493f764e12SYuri Pankov 					(void) strlcpy(what, up->p_comm,
5503f764e12SYuri Pankov 					    sizeof (what));
5513f764e12SYuri Pankov 				}
5523f764e12SYuri Pankov 			}
5533f764e12SYuri Pankov 			STAILQ_REMOVE(&uphead, up, uproc, uprocs);
5543f764e12SYuri Pankov 			free(up);
5553f764e12SYuri Pankov 		}
5573f764e12SYuri Pankov skip:
5583f764e12SYuri Pankov 		if (lflag) {
5593f764e12SYuri Pankov 			/* Print CPU time for all processes & children */
5603f764e12SYuri Pankov 			prttime(jobtime, 8);
5613f764e12SYuri Pankov 			/* Print cpu time for interesting process */
5623f764e12SYuri Pankov 			prttime(proctime, 8);
5633f764e12SYuri Pankov 		}
5643f764e12SYuri Pankov 		/* "Most interesting" process */
5653f764e12SYuri Pankov 		PRINTF(("%-.32s\n", what));
5667c478bd9Sstevel@tonic-gate 	}
5683f764e12SYuri Pankov 	if (fclose(stdout) == EOF)
5693f764e12SYuri Pankov 		err(EXIT_FAILURE, gettext("fclose failed"));
5703f764e12SYuri Pankov 
5713f764e12SYuri Pankov 	return (0);
5727c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate #define	HR	(60 * 60)
5757c478bd9Sstevel@tonic-gate #define	DAY	(24 * HR)
5767c478bd9Sstevel@tonic-gate #define	MON	(30 * DAY)
5787c478bd9Sstevel@tonic-gate /*
579f0540631SGary Mills  * Prttime prints an elapsed time in hours, minutes, or seconds,
580f0540631SGary Mills  * right-justified with the rightmost column always blank.
581f0540631SGary Mills  * The second argument is the minimum field width.
5827c478bd9Sstevel@tonic-gate  */
5837c478bd9Sstevel@tonic-gate static void
prttime(time_t tim,int width)584f0540631SGary Mills prttime(time_t tim, int width)
5857c478bd9Sstevel@tonic-gate {
586f0540631SGary Mills 	char value[36];
587f0540631SGary Mills 
588f0540631SGary Mills 	if (tim >= 36 * 60) {
589f0540631SGary Mills 		(void) snprintf(value, sizeof (value), "%d:%02d:%02d",
590f0540631SGary Mills 		    (int)tim / HR, (int)(tim % HR) / 60, (int)tim % 60);
591f0540631SGary Mills 	} else if (tim >= 60) {
592f0540631SGary Mills 		(void) snprintf(value, sizeof (value), "%d:%02d",
593f0540631SGary Mills 		    (int)tim / 60, (int)tim % 60);
5947c478bd9Sstevel@tonic-gate 	} else if (tim > 0) {
595f0540631SGary Mills 		(void) snprintf(value, sizeof (value), "%d", (int)tim);
5967c478bd9Sstevel@tonic-gate 	} else {
5976a79a301SJason King 		(void) strlcpy(value, "0", sizeof (value));
5987c478bd9Sstevel@tonic-gate 	}
599f0540631SGary Mills 	width = (width > 2) ? width - 1 : 1;
600f0540631SGary Mills 	PRINTF(("%*s ", width, value));
6017c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate /*
604f0540631SGary Mills  * Prints the ISO date or time given a pointer to a time of day,
605f0540631SGary Mills  * left-justfied in a 12-character expanding field with the
606f0540631SGary Mills  * rightmost column always blank.
607f0540631SGary Mills  * Includes a dcgettext() override in case a message catalog is needed.
6087c478bd9Sstevel@tonic-gate  */
6097c478bd9Sstevel@tonic-gate static void
prtat(time_t * time)6107c478bd9Sstevel@tonic-gate prtat(time_t *time)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 	struct tm	*p;
6147c478bd9Sstevel@tonic-gate 	p = localtime(time);
6157c478bd9Sstevel@tonic-gate 	if (now - *time <= 18 * HR) {
6167c478bd9Sstevel@tonic-gate 		char timestr[50];
617f0540631SGary Mills 
6187c478bd9Sstevel@tonic-gate 		(void) strftime(timestr, sizeof (timestr),
619f0540631SGary Mills 		    dcgettext(NULL, "%T", LC_TIME), p);
620f0540631SGary Mills 		PRINTF(("%-11s ", timestr));
6217c478bd9Sstevel@tonic-gate 	} else if (now - *time <= 7 * DAY) {
6227c478bd9Sstevel@tonic-gate 		char weekdaytime[20];
6247c478bd9Sstevel@tonic-gate 		(void) strftime(weekdaytime, sizeof (weekdaytime),
625f0540631SGary Mills 		    dcgettext(NULL, "%a %H:%M", LC_TIME), p);
626f0540631SGary Mills 		PRINTF(("%-11s ", weekdaytime));
6277c478bd9Sstevel@tonic-gate 	} else {
6287c478bd9Sstevel@tonic-gate 		char monthtime[20];
6307c478bd9Sstevel@tonic-gate 		(void) strftime(monthtime, sizeof (monthtime),
631f0540631SGary Mills 		    dcgettext(NULL, "%F", LC_TIME), p);
632f0540631SGary Mills 		PRINTF(("%-11s ", monthtime));
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate  * find & return number of minutes current tty has been idle
6387c478bd9Sstevel@tonic-gate  */
6397c478bd9Sstevel@tonic-gate static time_t
findidle(char * devname)6407c478bd9Sstevel@tonic-gate findidle(char *devname)
6417c478bd9Sstevel@tonic-gate {
6427c478bd9Sstevel@tonic-gate 	struct stat stbuf;
6437c478bd9Sstevel@tonic-gate 	time_t lastaction, diff;
6447c478bd9Sstevel@tonic-gate 	char ttyname[64];
6466a79a301SJason King 	(void) strlcpy(ttyname, "/dev/", sizeof (ttyname));
6476a79a301SJason King 	(void) strlcat(ttyname, devname, sizeof (ttyname));
6487c478bd9Sstevel@tonic-gate 	if (stat(ttyname, &stbuf) != -1) {
6497c478bd9Sstevel@tonic-gate 		lastaction = stbuf.st_atime;
6507c478bd9Sstevel@tonic-gate 		diff = now - lastaction;
6517c478bd9Sstevel@tonic-gate 		diff = DIV60(diff);
6527c478bd9Sstevel@tonic-gate 		if (diff < 0)
6537c478bd9Sstevel@tonic-gate 			diff = 0;
6547c478bd9Sstevel@tonic-gate 	} else
6557c478bd9Sstevel@tonic-gate 		diff = 0;
6567c478bd9Sstevel@tonic-gate 	return (diff);
6577c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate  * given a pointer to the argument string get rid of unsavory characters.
6617c478bd9Sstevel@tonic-gate  */
6627c478bd9Sstevel@tonic-gate static void
clnarglist(char * arglist)6637c478bd9Sstevel@tonic-gate clnarglist(char *arglist)
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate 	char	*c;
666352d7aedSToomas Soome 	int	err = 0;
6687c478bd9Sstevel@tonic-gate 	/* get rid of unsavory characters */
669352d7aedSToomas Soome 	for (c = arglist; *c != '\0'; c++) {
6707c478bd9Sstevel@tonic-gate 		if ((*c < ' ') || (*c > 0176)) {
6717c478bd9Sstevel@tonic-gate 			if (err++ > 5) {
672352d7aedSToomas Soome 				*arglist = '\0';
6737c478bd9Sstevel@tonic-gate 				break;
6747c478bd9Sstevel@tonic-gate 			}
6757c478bd9Sstevel@tonic-gate 			*c = '?';
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate }
6796a79a301SJason King 
6806a79a301SJason King static int
priv_proc_open(const char * path,int oflag)6816a79a301SJason King priv_proc_open(const char *path, int oflag)
6826a79a301SJason King {
6836a79a301SJason King 	int fd, errsave = 0;
6846a79a301SJason King 
6856a79a301SJason King 	if (__priv_bracket(PRIV_ON) != 0)
6866a79a301SJason King 		err(EXIT_FAILURE, gettext("privilege bracketing failed"));
6876a79a301SJason King 
6886a79a301SJason King 	do {
6896a79a301SJason King 		fd = open(path, oflag);
6906a79a301SJason King 		if (fd < 0)
6916a79a301SJason King 			errsave = errno;
6926a79a301SJason King 	} while (fd < 0 && errno == EAGAIN);
6936a79a301SJason King 
6946a79a301SJason King 	if (__priv_bracket(PRIV_OFF) != 0)
6956a79a301SJason King 		err(EXIT_FAILURE, gettext("privilege bracketing failed"));
6966a79a301SJason King 
6976a79a301SJason King 	if (fd < 0)
6986a79a301SJason King 		errno = errsave;
6996a79a301SJason King 
7006a79a301SJason King 	return (fd);
7016a79a301SJason King }
7026a79a301SJason King 
7036a79a301SJason King static int
priv_proc_openat(int dfd,const char * path,int mode)7046a79a301SJason King priv_proc_openat(int dfd, const char *path, int mode)
7056a79a301SJason King {
7066a79a301SJason King 	int fd, errsave = 0;
7076a79a301SJason King 
7086a79a301SJason King 	if (__priv_bracket(PRIV_ON) != 0)
7096a79a301SJason King 		err(EXIT_FAILURE, gettext("privilege bracketing failed"));
7106a79a301SJason King 
7116a79a301SJason King 	do {
7126a79a301SJason King 		fd = openat(dfd, path, mode);
7136a79a301SJason King 		if (fd < 0)
7146a79a301SJason King 			errsave = errno;
7156a79a301SJason King 	} while (fd < 0 && errno == EAGAIN);
7166a79a301SJason King 
7176a79a301SJason King 	if (__priv_bracket(PRIV_OFF) != 0)
7186a79a301SJason King 		err(EXIT_FAILURE, gettext("privilege bracketing failed"));
7196a79a301SJason King 
7206a79a301SJason King 	if (fd < 0)
7216a79a301SJason King 		errno = errsave;
7226a79a301SJason King 
7236a79a301SJason King 	return (fd);
7246a79a301SJason King }
7256a79a301SJason King 
7266a79a301SJason King static boolean_t
do_proc_read(int fd,void * buf,size_t bufsize)7276a79a301SJason King do_proc_read(int fd, void *buf, size_t bufsize)
7286a79a301SJason King {
7296a79a301SJason King 	ssize_t n;
7306a79a301SJason King 
7316a79a301SJason King 	do {
7326a79a301SJason King 		n = pread(fd, buf, bufsize, 0);
7336a79a301SJason King 		if (n == bufsize)
7346a79a301SJason King 			return (B_TRUE);
7356a79a301SJason King 		/*
7366a79a301SJason King 		 * Retry on a partial read or EAGAIN, otherwise fail
7376a79a301SJason King 		 */
7386a79a301SJason King 	} while (n >= 0 || errno == EAGAIN);
7396a79a301SJason King 
7406a79a301SJason King 	return (B_FALSE);
7416a79a301SJason King }