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 */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 31352d7aedSToomas Soome /* All Rights Reserved */ 327c478bd9Sstevel@tonic-gate 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 */ 427c478bd9Sstevel@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 */ 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate #include <sys/types.h> 57*3f764e12SYuri Pankov #include <sys/loadavg.h> 58*3f764e12SYuri Pankov #include <sys/queue.h> 597c478bd9Sstevel@tonic-gate #include <sys/stat.h> 60*3f764e12SYuri Pankov #include <sys/sysmacros.h> 61*3f764e12SYuri Pankov 62*3f764e12SYuri Pankov #include <ctype.h> 637c478bd9Sstevel@tonic-gate #include <dirent.h> 64*3f764e12SYuri Pankov #include <err.h> 65*3f764e12SYuri Pankov #include <errno.h> 66*3f764e12SYuri Pankov #include <fcntl.h> 677c478bd9Sstevel@tonic-gate #include <limits.h> 68*3f764e12SYuri Pankov #include <locale.h> 697c478bd9Sstevel@tonic-gate #include <priv_utils.h> 70*3f764e12SYuri Pankov #include <procfs.h> /* /proc header file */ 71*3f764e12SYuri Pankov #include <stdarg.h> 72*3f764e12SYuri Pankov #include <stdio.h> 73*3f764e12SYuri Pankov #include <stdlib.h> 74*3f764e12SYuri Pankov #include <string.h> 75*3f764e12SYuri Pankov #include <time.h> 76*3f764e12SYuri Pankov #include <unistd.h> 77*3f764e12SYuri Pankov #include <utmpx.h> 787c478bd9Sstevel@tonic-gate 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 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate #define DIV60(t) ((t+30)/60) /* x/60 rounded */ 917c478bd9Sstevel@tonic-gate 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); } 967c478bd9Sstevel@tonic-gate 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 */ 105*3f764e12SYuri Pankov STAILQ_ENTRY(uproc) uprocs; 1067c478bd9Sstevel@tonic-gate }; 107*3f764e12SYuri Pankov STAILQ_HEAD(uprochead, uproc) uphead; 1087c478bd9Sstevel@tonic-gate 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); 1137c478bd9Sstevel@tonic-gate 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 */ 1277c478bd9Sstevel@tonic-gate 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 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate int 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; 153*3f764e12SYuri 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; 159*3f764e12SYuri 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"); 2037c478bd9Sstevel@tonic-gate 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; 2157c478bd9Sstevel@tonic-gate 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); 2217c478bd9Sstevel@tonic-gate 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]; 2267c478bd9Sstevel@tonic-gate 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]) { 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate case 'h': 2337c478bd9Sstevel@tonic-gate header = 0; 2347c478bd9Sstevel@tonic-gate break; 2357c478bd9Sstevel@tonic-gate 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; 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate case 'u': 2447c478bd9Sstevel@tonic-gate case 'w': 2457c478bd9Sstevel@tonic-gate firstchar = argv[1][i]; 2467c478bd9Sstevel@tonic-gate break; 2477c478bd9Sstevel@tonic-gate 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 } 2657c478bd9Sstevel@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); 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate (void) utmpxname(UTMPX_FILE); 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate utmpbegin = ut; 2796a79a301SJason King utmpend = utmpbegin + entries; 2807c478bd9Sstevel@tonic-gate 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(); 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate (void) time(&now); /* get current time */ 2877c478bd9Sstevel@tonic-gate 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; 3027c478bd9Sstevel@tonic-gate 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 { 3107c478bd9Sstevel@tonic-gate if (hrs > 0) 3117c478bd9Sstevel@tonic-gate PRINTF((gettext( 3127c478bd9Sstevel@tonic-gate " %d hr(s),"), hrs)); 3137c478bd9Sstevel@tonic-gate if (mins > 0) 3147c478bd9Sstevel@tonic-gate PRINTF((gettext( 3157c478bd9Sstevel@tonic-gate " %d min(s),"), mins)); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate ut = utmpbegin; /* rewind utmp data */ 3217c478bd9Sstevel@tonic-gate PRINTF((((nusers == 1) ? 3227c478bd9Sstevel@tonic-gate gettext(" %d user") : gettext(" %d users")), nusers)); 3237c478bd9Sstevel@tonic-gate /* 3247c478bd9Sstevel@tonic-gate * Print 1, 5, and 15 minute load averages. 3257c478bd9Sstevel@tonic-gate */ 3267c478bd9Sstevel@tonic-gate (void) getloadavg(loadavg, 3); 3277c478bd9Sstevel@tonic-gate PRINTF((gettext(", load average: %.2f, %.2f, %.2f\n"), 3287c478bd9Sstevel@tonic-gate loadavg[LOADAVG_1MIN], loadavg[LOADAVG_5MIN], 3297c478bd9Sstevel@tonic-gate loadavg[LOADAVG_15MIN])); 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate if (firstchar == 'u') /* uptime command */ 3327c478bd9Sstevel@tonic-gate exit(0); 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate if (lflag) { 335f0540631SGary Mills PRINTF((dcgettext(NULL, "User tty " 336f0540631SGary Mills "login@ idle JCPU PCPU what\n", 337f0540631SGary Mills LC_TIME))); 3387c478bd9Sstevel@tonic-gate } else { 3397c478bd9Sstevel@tonic-gate PRINTF((dcgettext(NULL, 340f0540631SGary Mills "User tty idle what\n", 341f0540631SGary Mills LC_TIME))); 3427c478bd9Sstevel@tonic-gate } 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate if (fflush(stdout) == EOF) { 3456a79a301SJason King err(EXIT_FAILURE, "fflush failed"); 3467c478bd9Sstevel@tonic-gate } 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate 349*3f764e12SYuri Pankov /* Loop through /proc, reading info about each process */ 3506a79a301SJason King if ((dirp = opendir(PROCDIR)) == NULL) 3516a79a301SJason King err(EXIT_FAILURE, gettext("could not open %s"), PROCDIR); 3527c478bd9Sstevel@tonic-gate 353*3f764e12SYuri Pankov STAILQ_INIT(&uphead); 3547c478bd9Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) { 3557c478bd9Sstevel@tonic-gate if (dp->d_name[0] == '.') 3567c478bd9Sstevel@tonic-gate continue; 3576a79a301SJason King 3586a79a301SJason King if (snprintf(pname, sizeof (pname), "%s/%s", PROCDIR, 3596a79a301SJason King dp->d_name) > sizeof (pname)) 3607c478bd9Sstevel@tonic-gate continue; 3616a79a301SJason King 3626a79a301SJason King dirfd = priv_proc_open(pname, O_RDONLY | O_DIRECTORY); 36345de8795SJason King if (dirfd < 0) 3646a79a301SJason King continue; 3656a79a301SJason King 3666a79a301SJason King procfd = priv_proc_openat(dirfd, "psinfo", O_RDONLY); 3676a79a301SJason King if (procfd < 0) { 3686a79a301SJason King (void) close(dirfd); 3696a79a301SJason King continue; 3706a79a301SJason King } 3716a79a301SJason King if (!do_proc_read(procfd, &info, sizeof (info))) { 372*3f764e12SYuri Pankov warn(gettext("failed to read %s"), pname); 3736a79a301SJason King (void) close(dirfd); 3747c478bd9Sstevel@tonic-gate continue; 3757c478bd9Sstevel@tonic-gate } 3767c478bd9Sstevel@tonic-gate (void) close(procfd); 3777c478bd9Sstevel@tonic-gate 378*3f764e12SYuri Pankov /* Not interested in zombies */ 379*3f764e12SYuri Pankov if (info.pr_nlwp == 0) 380*3f764e12SYuri Pankov continue; 381*3f764e12SYuri Pankov /* Not interested in processes without a terminal */ 382*3f764e12SYuri Pankov if (info.pr_ttydev == NODEV) 383*3f764e12SYuri Pankov continue; 3847c478bd9Sstevel@tonic-gate 385*3f764e12SYuri Pankov procfd = priv_proc_openat(dirfd, "status", O_RDONLY); 386*3f764e12SYuri Pankov if (procfd < 0) { 387*3f764e12SYuri Pankov (void) close(dirfd); 388*3f764e12SYuri Pankov continue; 389*3f764e12SYuri Pankov } 390*3f764e12SYuri Pankov if (!do_proc_read(procfd, &statinfo, sizeof (statinfo))) { 391*3f764e12SYuri Pankov warn(gettext("failed to read %s/status"), pname); 3927c478bd9Sstevel@tonic-gate (void) close(procfd); 3936a79a301SJason King (void) close(dirfd); 394*3f764e12SYuri Pankov continue; 3957c478bd9Sstevel@tonic-gate } 396*3f764e12SYuri Pankov (void) close(procfd); 3977c478bd9Sstevel@tonic-gate 398*3f764e12SYuri Pankov procfd = priv_proc_openat(dirfd, "sigact", O_RDONLY); 399*3f764e12SYuri Pankov if (procfd < 0) { 400*3f764e12SYuri Pankov (void) close(dirfd); 401*3f764e12SYuri Pankov continue; 402*3f764e12SYuri Pankov } 403*3f764e12SYuri Pankov if (!do_proc_read(procfd, actinfo, sizeof (actinfo))) { 404*3f764e12SYuri Pankov warn(gettext("failed to read %s/sigact"), pname); 405*3f764e12SYuri Pankov (void) close(procfd); 406*3f764e12SYuri Pankov (void) close(dirfd); 407*3f764e12SYuri Pankov continue; 4087c478bd9Sstevel@tonic-gate } 409*3f764e12SYuri Pankov (void) close(procfd); 410*3f764e12SYuri Pankov (void) close(dirfd); 4117c478bd9Sstevel@tonic-gate 412*3f764e12SYuri Pankov up = calloc(1, sizeof (*up)); 413*3f764e12SYuri Pankov if (up == NULL) 414*3f764e12SYuri Pankov err(EXIT_FAILURE, "calloc"); 415*3f764e12SYuri Pankov up->p_upid = info.pr_pid; 416*3f764e12SYuri Pankov up->p_ttyd = info.pr_ttydev; 417*3f764e12SYuri Pankov up->p_time = 418*3f764e12SYuri Pankov statinfo.pr_utime.tv_sec + 419*3f764e12SYuri Pankov statinfo.pr_stime.tv_sec; 420*3f764e12SYuri Pankov up->p_ctime = 421*3f764e12SYuri Pankov statinfo.pr_cutime.tv_sec + 422*3f764e12SYuri Pankov statinfo.pr_cstime.tv_sec; 423*3f764e12SYuri Pankov up->p_igintr = 424*3f764e12SYuri Pankov actinfo[SIGINT-1].sa_handler == SIG_IGN && 425*3f764e12SYuri Pankov actinfo[SIGQUIT-1].sa_handler == SIG_IGN; 426*3f764e12SYuri Pankov (void) strlcpy(up->p_comm, info.pr_fname, sizeof (up->p_comm)); 427*3f764e12SYuri Pankov /* Process args */ 428*3f764e12SYuri Pankov clnarglist(info.pr_psargs); 429*3f764e12SYuri Pankov (void) strlcpy(up->p_args, info.pr_psargs, sizeof (up->p_args)); 430*3f764e12SYuri Pankov if (up->p_args[0] == 0 || up->p_args[0] == '?' || 431*3f764e12SYuri Pankov (up->p_args[0] == '-' && up->p_args[1] <= ' ')) { 432*3f764e12SYuri Pankov (void) strlcat(up->p_args, " (", sizeof (up->p_args)); 433*3f764e12SYuri Pankov (void) strlcat(up->p_args, up->p_comm, 434*3f764e12SYuri Pankov sizeof (up->p_args)); 435*3f764e12SYuri Pankov (void) strlcat(up->p_args, ")", sizeof (up->p_args)); 4367c478bd9Sstevel@tonic-gate } 437*3f764e12SYuri Pankov STAILQ_INSERT_TAIL(&uphead, up, uprocs); 4387c478bd9Sstevel@tonic-gate } 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate /* revert to non-privileged user after opening */ 4416a79a301SJason King __priv_relinquish(); 4426a79a301SJason King if (getuid() == 0) { 4436a79a301SJason King /* 4446a79a301SJason King * Since the privilege bracketing functions are effectively 4456a79a301SJason King * no-ops when running as root, we must explicitly 4466a79a301SJason King * relinquish PRIV_PROC_OWNER ourselves. 4476a79a301SJason King */ 4486a79a301SJason King pset = priv_allocset(); 4496a79a301SJason King if (pset == NULL) { 4506a79a301SJason King err(EXIT_FAILURE, 4516a79a301SJason King gettext("failed to allocate privilege set")); 4526a79a301SJason King } 4536a79a301SJason King 4546a79a301SJason King priv_emptyset(pset); 4556a79a301SJason King 4566a79a301SJason King if (priv_addset(pset, PRIV_PROC_OWNER) != 0) { 4576a79a301SJason King err(EXIT_FAILURE, gettext("failed to add " 4586a79a301SJason King "PRIV_PROC_OWNER to privilege set")); 4596a79a301SJason King } 4606a79a301SJason King 4616a79a301SJason King if (setppriv(PRIV_OFF, PRIV_PERMITTED, pset) != 0) { 4626a79a301SJason King err(EXIT_FAILURE, 4636a79a301SJason King gettext("failed to set permitted privilege set")); 4646a79a301SJason King } 4656a79a301SJason King 4666a79a301SJason King priv_freeset(pset); 4676a79a301SJason King pset = NULL; 4686a79a301SJason King } 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate (void) closedir(dirp); 4717c478bd9Sstevel@tonic-gate (void) time(&now); /* get current time */ 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate /* 4747c478bd9Sstevel@tonic-gate * loop through utmpx file, printing process info 4757c478bd9Sstevel@tonic-gate * about each logged in user 4767c478bd9Sstevel@tonic-gate */ 4777c478bd9Sstevel@tonic-gate for (ut = utmpbegin; ut < utmpend; ut++) { 478*3f764e12SYuri Pankov struct uproc *upt; 479*3f764e12SYuri Pankov char linedev[PATH_MAX]; 480*3f764e12SYuri Pankov char what[1024]; 481*3f764e12SYuri Pankov time_t idle, jobtime, proctime; 482*3f764e12SYuri Pankov pid_t curpid; 483*3f764e12SYuri Pankov 4847c478bd9Sstevel@tonic-gate if (ut->ut_type != USER_PROCESS) 4857c478bd9Sstevel@tonic-gate continue; 486*3f764e12SYuri Pankov if (sel_user != NULL && 487*3f764e12SYuri Pankov strncmp(ut->ut_name, sel_user, NMAX) != 0) 488*3f764e12SYuri Pankov continue; 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate /* print login name of the user */ 4910a1278f2SGary Mills PRINTF(("%-*.*s ", LOGIN_WIDTH, NMAX, ut->ut_name)); 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate /* print tty user is on */ 4947c478bd9Sstevel@tonic-gate if (lflag) { 495f0540631SGary Mills PRINTF(("%-*.*s ", LINE_WIDTH, LMAX, ut->ut_line)); 4967c478bd9Sstevel@tonic-gate } else { 497*3f764e12SYuri Pankov if (strncmp(ut->ut_line, "pts/", strlen("pts/")) == 0) { 498f0540631SGary Mills PRINTF(("%-*.*s ", LINE_WIDTH, LMAX, 499f0540631SGary Mills &ut->ut_line[4])); 5007c478bd9Sstevel@tonic-gate } else { 501f0540631SGary Mills PRINTF(("%-*.*s ", LINE_WIDTH, LMAX, 5020a1278f2SGary Mills ut->ut_line)); 5037c478bd9Sstevel@tonic-gate } 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate /* print when the user logged in */ 5077c478bd9Sstevel@tonic-gate if (lflag) { 5087c478bd9Sstevel@tonic-gate time_t tim = ut->ut_xtime; 5097c478bd9Sstevel@tonic-gate prtat(&tim); 5107c478bd9Sstevel@tonic-gate } 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate /* print idle time */ 5137c478bd9Sstevel@tonic-gate idle = findidle(ut->ut_line); 514f0540631SGary Mills prttime(idle, 8); 5157c478bd9Sstevel@tonic-gate 516*3f764e12SYuri Pankov /* 517*3f764e12SYuri Pankov * Go through the list of processes for this terminal, 518*3f764e12SYuri Pankov * calculating job/process times, and look for the 519*3f764e12SYuri Pankov * "most interesting" process. 520*3f764e12SYuri Pankov */ 521*3f764e12SYuri Pankov jobtime = 0; 522*3f764e12SYuri Pankov proctime = 0; 5237c478bd9Sstevel@tonic-gate curpid = -1; 524*3f764e12SYuri Pankov (void) strlcpy(what, "-", sizeof (what)); 5257c478bd9Sstevel@tonic-gate 526*3f764e12SYuri Pankov (void) snprintf(linedev, sizeof (linedev), "/dev/%s", 527*3f764e12SYuri Pankov ut->ut_line); 528*3f764e12SYuri Pankov if (stat(linedev, &sbuf) == -1 || 529*3f764e12SYuri Pankov (sbuf.st_mode & S_IFMT) != S_IFCHR || 530*3f764e12SYuri Pankov sbuf.st_rdev == NODEV) 531*3f764e12SYuri Pankov goto skip; 5327c478bd9Sstevel@tonic-gate 533*3f764e12SYuri Pankov STAILQ_FOREACH_SAFE(up, &uphead, uprocs, upt) { 534*3f764e12SYuri Pankov if (up->p_ttyd != sbuf.st_rdev) 535*3f764e12SYuri Pankov continue; 536*3f764e12SYuri Pankov jobtime += up->p_time + up->p_ctime; 537*3f764e12SYuri Pankov proctime += up->p_time; 538*3f764e12SYuri Pankov /* 539*3f764e12SYuri Pankov * Check for "most interesting" process, currently 540*3f764e12SYuri Pankov * the one having the highest PID. 541*3f764e12SYuri Pankov */ 542*3f764e12SYuri Pankov if (up->p_upid > curpid && !up->p_igintr) { 543*3f764e12SYuri Pankov curpid = up->p_upid; 544*3f764e12SYuri Pankov if (lflag) { 545*3f764e12SYuri Pankov (void) strlcpy(what, up->p_args, 546*3f764e12SYuri Pankov sizeof (what)); 547*3f764e12SYuri Pankov } else { 548*3f764e12SYuri Pankov (void) strlcpy(what, up->p_comm, 549*3f764e12SYuri Pankov sizeof (what)); 550*3f764e12SYuri Pankov } 551*3f764e12SYuri Pankov } 552*3f764e12SYuri Pankov STAILQ_REMOVE(&uphead, up, uproc, uprocs); 553*3f764e12SYuri Pankov free(up); 554*3f764e12SYuri Pankov } 5557c478bd9Sstevel@tonic-gate 556*3f764e12SYuri Pankov skip: 557*3f764e12SYuri Pankov if (lflag) { 558*3f764e12SYuri Pankov /* Print CPU time for all processes & children */ 559*3f764e12SYuri Pankov prttime(jobtime, 8); 560*3f764e12SYuri Pankov /* Print cpu time for interesting process */ 561*3f764e12SYuri Pankov prttime(proctime, 8); 562*3f764e12SYuri Pankov } 563*3f764e12SYuri Pankov /* "Most interesting" process */ 564*3f764e12SYuri Pankov PRINTF(("%-.32s\n", what)); 5657c478bd9Sstevel@tonic-gate } 5667c478bd9Sstevel@tonic-gate 567*3f764e12SYuri Pankov if (fclose(stdout) == EOF) 568*3f764e12SYuri Pankov err(EXIT_FAILURE, gettext("fclose failed")); 569*3f764e12SYuri Pankov 570*3f764e12SYuri Pankov return (0); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate #define HR (60 * 60) 5747c478bd9Sstevel@tonic-gate #define DAY (24 * HR) 5757c478bd9Sstevel@tonic-gate #define MON (30 * DAY) 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate /* 578f0540631SGary Mills * Prttime prints an elapsed time in hours, minutes, or seconds, 579f0540631SGary Mills * right-justified with the rightmost column always blank. 580f0540631SGary Mills * The second argument is the minimum field width. 5817c478bd9Sstevel@tonic-gate */ 5827c478bd9Sstevel@tonic-gate static void 583f0540631SGary Mills prttime(time_t tim, int width) 5847c478bd9Sstevel@tonic-gate { 585f0540631SGary Mills char value[36]; 586f0540631SGary Mills 587f0540631SGary Mills if (tim >= 36 * 60) { 588f0540631SGary Mills (void) snprintf(value, sizeof (value), "%d:%02d:%02d", 589f0540631SGary Mills (int)tim / HR, (int)(tim % HR) / 60, (int)tim % 60); 590f0540631SGary Mills } else if (tim >= 60) { 591f0540631SGary Mills (void) snprintf(value, sizeof (value), "%d:%02d", 592f0540631SGary Mills (int)tim / 60, (int)tim % 60); 5937c478bd9Sstevel@tonic-gate } else if (tim > 0) { 594f0540631SGary Mills (void) snprintf(value, sizeof (value), "%d", (int)tim); 5957c478bd9Sstevel@tonic-gate } else { 5966a79a301SJason King (void) strlcpy(value, "0", sizeof (value)); 5977c478bd9Sstevel@tonic-gate } 598f0540631SGary Mills width = (width > 2) ? width - 1 : 1; 599f0540631SGary Mills PRINTF(("%*s ", width, value)); 6007c478bd9Sstevel@tonic-gate } 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate /* 603f0540631SGary Mills * Prints the ISO date or time given a pointer to a time of day, 604f0540631SGary Mills * left-justfied in a 12-character expanding field with the 605f0540631SGary Mills * rightmost column always blank. 606f0540631SGary Mills * Includes a dcgettext() override in case a message catalog is needed. 6077c478bd9Sstevel@tonic-gate */ 6087c478bd9Sstevel@tonic-gate static void 6097c478bd9Sstevel@tonic-gate prtat(time_t *time) 6107c478bd9Sstevel@tonic-gate { 6117c478bd9Sstevel@tonic-gate struct tm *p; 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate p = localtime(time); 6147c478bd9Sstevel@tonic-gate if (now - *time <= 18 * HR) { 6157c478bd9Sstevel@tonic-gate char timestr[50]; 616f0540631SGary Mills 6177c478bd9Sstevel@tonic-gate (void) strftime(timestr, sizeof (timestr), 618f0540631SGary Mills dcgettext(NULL, "%T", LC_TIME), p); 619f0540631SGary Mills PRINTF(("%-11s ", timestr)); 6207c478bd9Sstevel@tonic-gate } else if (now - *time <= 7 * DAY) { 6217c478bd9Sstevel@tonic-gate char weekdaytime[20]; 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate (void) strftime(weekdaytime, sizeof (weekdaytime), 624f0540631SGary Mills dcgettext(NULL, "%a %H:%M", LC_TIME), p); 625f0540631SGary Mills PRINTF(("%-11s ", weekdaytime)); 6267c478bd9Sstevel@tonic-gate } else { 6277c478bd9Sstevel@tonic-gate char monthtime[20]; 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate (void) strftime(monthtime, sizeof (monthtime), 630f0540631SGary Mills dcgettext(NULL, "%F", LC_TIME), p); 631f0540631SGary Mills PRINTF(("%-11s ", monthtime)); 6327c478bd9Sstevel@tonic-gate } 6337c478bd9Sstevel@tonic-gate } 6347c478bd9Sstevel@tonic-gate 6357c478bd9Sstevel@tonic-gate /* 6367c478bd9Sstevel@tonic-gate * find & return number of minutes current tty has been idle 6377c478bd9Sstevel@tonic-gate */ 6387c478bd9Sstevel@tonic-gate static time_t 6397c478bd9Sstevel@tonic-gate findidle(char *devname) 6407c478bd9Sstevel@tonic-gate { 6417c478bd9Sstevel@tonic-gate struct stat stbuf; 6427c478bd9Sstevel@tonic-gate time_t lastaction, diff; 6437c478bd9Sstevel@tonic-gate char ttyname[64]; 6447c478bd9Sstevel@tonic-gate 6456a79a301SJason King (void) strlcpy(ttyname, "/dev/", sizeof (ttyname)); 6466a79a301SJason King (void) strlcat(ttyname, devname, sizeof (ttyname)); 6477c478bd9Sstevel@tonic-gate if (stat(ttyname, &stbuf) != -1) { 6487c478bd9Sstevel@tonic-gate lastaction = stbuf.st_atime; 6497c478bd9Sstevel@tonic-gate diff = now - lastaction; 6507c478bd9Sstevel@tonic-gate diff = DIV60(diff); 6517c478bd9Sstevel@tonic-gate if (diff < 0) 6527c478bd9Sstevel@tonic-gate diff = 0; 6537c478bd9Sstevel@tonic-gate } else 6547c478bd9Sstevel@tonic-gate diff = 0; 6557c478bd9Sstevel@tonic-gate return (diff); 6567c478bd9Sstevel@tonic-gate } 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate /* 6597c478bd9Sstevel@tonic-gate * given a pointer to the argument string get rid of unsavory characters. 6607c478bd9Sstevel@tonic-gate */ 6617c478bd9Sstevel@tonic-gate static void 6627c478bd9Sstevel@tonic-gate clnarglist(char *arglist) 6637c478bd9Sstevel@tonic-gate { 6647c478bd9Sstevel@tonic-gate char *c; 665352d7aedSToomas Soome int err = 0; 6667c478bd9Sstevel@tonic-gate 6677c478bd9Sstevel@tonic-gate /* get rid of unsavory characters */ 668352d7aedSToomas Soome for (c = arglist; *c != '\0'; c++) { 6697c478bd9Sstevel@tonic-gate if ((*c < ' ') || (*c > 0176)) { 6707c478bd9Sstevel@tonic-gate if (err++ > 5) { 671352d7aedSToomas Soome *arglist = '\0'; 6727c478bd9Sstevel@tonic-gate break; 6737c478bd9Sstevel@tonic-gate } 6747c478bd9Sstevel@tonic-gate *c = '?'; 6757c478bd9Sstevel@tonic-gate } 6767c478bd9Sstevel@tonic-gate } 6777c478bd9Sstevel@tonic-gate } 6786a79a301SJason King 6796a79a301SJason King static int 6806a79a301SJason King priv_proc_open(const char *path, int oflag) 6816a79a301SJason King { 6826a79a301SJason King int fd, errsave = 0; 6836a79a301SJason King 6846a79a301SJason King if (__priv_bracket(PRIV_ON) != 0) 6856a79a301SJason King err(EXIT_FAILURE, gettext("privilege bracketing failed")); 6866a79a301SJason King 6876a79a301SJason King do { 6886a79a301SJason King fd = open(path, oflag); 6896a79a301SJason King if (fd < 0) 6906a79a301SJason King errsave = errno; 6916a79a301SJason King } while (fd < 0 && errno == EAGAIN); 6926a79a301SJason King 6936a79a301SJason King if (__priv_bracket(PRIV_OFF) != 0) 6946a79a301SJason King err(EXIT_FAILURE, gettext("privilege bracketing failed")); 6956a79a301SJason King 6966a79a301SJason King if (fd < 0) 6976a79a301SJason King errno = errsave; 6986a79a301SJason King 6996a79a301SJason King return (fd); 7006a79a301SJason King } 7016a79a301SJason King 7026a79a301SJason King static int 7036a79a301SJason King priv_proc_openat(int dfd, const char *path, int mode) 7046a79a301SJason King { 7056a79a301SJason King int fd, errsave = 0; 7066a79a301SJason King 7076a79a301SJason King if (__priv_bracket(PRIV_ON) != 0) 7086a79a301SJason King err(EXIT_FAILURE, gettext("privilege bracketing failed")); 7096a79a301SJason King 7106a79a301SJason King do { 7116a79a301SJason King fd = openat(dfd, path, mode); 7126a79a301SJason King if (fd < 0) 7136a79a301SJason King errsave = errno; 7146a79a301SJason King } while (fd < 0 && errno == EAGAIN); 7156a79a301SJason King 7166a79a301SJason King if (__priv_bracket(PRIV_OFF) != 0) 7176a79a301SJason King err(EXIT_FAILURE, gettext("privilege bracketing failed")); 7186a79a301SJason King 7196a79a301SJason King if (fd < 0) 7206a79a301SJason King errno = errsave; 7216a79a301SJason King 7226a79a301SJason King return (fd); 7236a79a301SJason King } 7246a79a301SJason King 7256a79a301SJason King static boolean_t 7266a79a301SJason King do_proc_read(int fd, void *buf, size_t bufsize) 7276a79a301SJason King { 7286a79a301SJason King ssize_t n; 7296a79a301SJason King 7306a79a301SJason King do { 7316a79a301SJason King n = pread(fd, buf, bufsize, 0); 7326a79a301SJason King if (n == bufsize) 7336a79a301SJason King return (B_TRUE); 7346a79a301SJason King /* 7356a79a301SJason King * Retry on a partial read or EAGAIN, otherwise fail 7366a79a301SJason King */ 7376a79a301SJason King } while (n >= 0 || errno == EAGAIN); 7386a79a301SJason King 7396a79a301SJason King return (B_FALSE); 7406a79a301SJason King } 741