1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate /* 31*0Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 32*0Sstevel@tonic-gate * The Regents of the University of California 33*0Sstevel@tonic-gate * All Rights Reserved 34*0Sstevel@tonic-gate * 35*0Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 36*0Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 37*0Sstevel@tonic-gate * contributors. 38*0Sstevel@tonic-gate */ 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate /* 43*0Sstevel@tonic-gate * This is the new w command which takes advantage of 44*0Sstevel@tonic-gate * the /proc interface to gain access to the information 45*0Sstevel@tonic-gate * of all the processes currently on the system. 46*0Sstevel@tonic-gate * 47*0Sstevel@tonic-gate * This program also implements 'uptime'. 48*0Sstevel@tonic-gate * 49*0Sstevel@tonic-gate * Maintenance note: 50*0Sstevel@tonic-gate * 51*0Sstevel@tonic-gate * Much of this code is replicated in whodo.c. If you're 52*0Sstevel@tonic-gate * fixing bugs here, then you should probably fix 'em there too. 53*0Sstevel@tonic-gate */ 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate #include <stdio.h> 56*0Sstevel@tonic-gate #include <string.h> 57*0Sstevel@tonic-gate #include <stdarg.h> 58*0Sstevel@tonic-gate #include <stdlib.h> 59*0Sstevel@tonic-gate #include <ctype.h> 60*0Sstevel@tonic-gate #include <fcntl.h> 61*0Sstevel@tonic-gate #include <time.h> 62*0Sstevel@tonic-gate #include <errno.h> 63*0Sstevel@tonic-gate #include <sys/types.h> 64*0Sstevel@tonic-gate #include <utmpx.h> 65*0Sstevel@tonic-gate #include <sys/stat.h> 66*0Sstevel@tonic-gate #include <dirent.h> 67*0Sstevel@tonic-gate #include <procfs.h> /* /proc header file */ 68*0Sstevel@tonic-gate #include <locale.h> 69*0Sstevel@tonic-gate #include <unistd.h> 70*0Sstevel@tonic-gate #include <sys/loadavg.h> 71*0Sstevel@tonic-gate #include <limits.h> 72*0Sstevel@tonic-gate #include <priv_utils.h> 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate /* 75*0Sstevel@tonic-gate * utmpx defines wider fields for user and line. For compatibility of output, 76*0Sstevel@tonic-gate * we are limiting these to the old maximums in utmp. Define UTMPX_NAMELEN 77*0Sstevel@tonic-gate * to use the full lengths. 78*0Sstevel@tonic-gate */ 79*0Sstevel@tonic-gate #ifndef UTMPX_NAMELEN 80*0Sstevel@tonic-gate /* XXX - utmp - fix name length */ 81*0Sstevel@tonic-gate #define NMAX (_POSIX_LOGIN_NAME_MAX - 1) 82*0Sstevel@tonic-gate #define LMAX 12 83*0Sstevel@tonic-gate #else /* UTMPX_NAMELEN */ 84*0Sstevel@tonic-gate static struct utmpx dummy; 85*0Sstevel@tonic-gate #define NMAX (sizeof (dummy.ut_user)) 86*0Sstevel@tonic-gate #define LMAX (sizeof (dummy.ut_line)) 87*0Sstevel@tonic-gate #endif /* UTMPX_NAMELEN */ 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate #define DIV60(t) ((t+30)/60) /* x/60 rounded */ 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate #ifdef ERR 92*0Sstevel@tonic-gate #undef ERR 93*0Sstevel@tonic-gate #endif 94*0Sstevel@tonic-gate #define ERR (-1) 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate #define HSIZE 256 /* size of process hash table */ 97*0Sstevel@tonic-gate #define PROCDIR "/proc" 98*0Sstevel@tonic-gate #define INITPROCESS (pid_t)1 /* init process pid */ 99*0Sstevel@tonic-gate #define NONE 'n' /* no state */ 100*0Sstevel@tonic-gate #define RUNNING 'r' /* runnable process */ 101*0Sstevel@tonic-gate #define ZOMBIE 'z' /* zombie process */ 102*0Sstevel@tonic-gate #define VISITED 'v' /* marked node as visited */ 103*0Sstevel@tonic-gate #define PRINTF(a) if (printf a < 0) { \ 104*0Sstevel@tonic-gate perror((gettext("%s: printf failed"), prog)); \ 105*0Sstevel@tonic-gate exit(1); } 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate struct uproc { 108*0Sstevel@tonic-gate pid_t p_upid; /* process id */ 109*0Sstevel@tonic-gate char p_state; /* numeric value of process state */ 110*0Sstevel@tonic-gate dev_t p_ttyd; /* controlling tty of process */ 111*0Sstevel@tonic-gate time_t p_time; /* seconds of user & system time */ 112*0Sstevel@tonic-gate time_t p_ctime; /* seconds of child user & sys time */ 113*0Sstevel@tonic-gate int p_igintr; /* 1 = ignores SIGQUIT and SIGINT */ 114*0Sstevel@tonic-gate char p_comm[PRARGSZ+1]; /* command */ 115*0Sstevel@tonic-gate char p_args[PRARGSZ+1]; /* command line arguments */ 116*0Sstevel@tonic-gate struct uproc *p_child, /* first child pointer */ 117*0Sstevel@tonic-gate *p_sibling, /* sibling pointer */ 118*0Sstevel@tonic-gate *p_pgrpl, /* pgrp link */ 119*0Sstevel@tonic-gate *p_link; /* hash table chain pointer */ 120*0Sstevel@tonic-gate }; 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate /* 123*0Sstevel@tonic-gate * define hash table for struct uproc 124*0Sstevel@tonic-gate * Hash function uses process id 125*0Sstevel@tonic-gate * and the size of the hash table(HSIZE) 126*0Sstevel@tonic-gate * to determine process index into the table. 127*0Sstevel@tonic-gate */ 128*0Sstevel@tonic-gate static struct uproc pr_htbl[HSIZE]; 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate static struct uproc *findhash(pid_t); 131*0Sstevel@tonic-gate static time_t findidle(char *); 132*0Sstevel@tonic-gate static void clnarglist(char *); 133*0Sstevel@tonic-gate static void showtotals(struct uproc *); 134*0Sstevel@tonic-gate static void calctotals(struct uproc *); 135*0Sstevel@tonic-gate static void prttime(time_t, char *); 136*0Sstevel@tonic-gate static void prtat(time_t *time); 137*0Sstevel@tonic-gate static void checkampm(char *str); 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate static char *prog; /* pointer to invocation name */ 140*0Sstevel@tonic-gate static int header = 1; /* true if -h flag: don't print heading */ 141*0Sstevel@tonic-gate static int lflag = 1; /* set if -l flag; 0 for -s flag: short form */ 142*0Sstevel@tonic-gate static char *sel_user; /* login of particular user selected */ 143*0Sstevel@tonic-gate static char firstchar; /* first char of name of prog invoked as */ 144*0Sstevel@tonic-gate static int login; /* true if invoked as login shell */ 145*0Sstevel@tonic-gate static time_t now; /* current time of day */ 146*0Sstevel@tonic-gate static time_t uptime; /* time of last reboot & elapsed time since */ 147*0Sstevel@tonic-gate static int nusers; /* number of users logged in now */ 148*0Sstevel@tonic-gate static time_t idle; /* number of minutes user is idle */ 149*0Sstevel@tonic-gate static time_t jobtime; /* total cpu time visible */ 150*0Sstevel@tonic-gate static char doing[520]; /* process attached to terminal */ 151*0Sstevel@tonic-gate static time_t proctime; /* cpu time of process in doing */ 152*0Sstevel@tonic-gate static pid_t curpid, empty; 153*0Sstevel@tonic-gate static int add_times; /* boolean: add the cpu times or not */ 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate #if SIGQUIT > SIGINT 156*0Sstevel@tonic-gate #define ACTSIZE SIGQUIT 157*0Sstevel@tonic-gate #else 158*0Sstevel@tonic-gate #define ACTSIZE SIGINT 159*0Sstevel@tonic-gate #endif 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate int 162*0Sstevel@tonic-gate main(int argc, char *argv[]) 163*0Sstevel@tonic-gate { 164*0Sstevel@tonic-gate struct utmpx *ut; 165*0Sstevel@tonic-gate struct utmpx *utmpbegin; 166*0Sstevel@tonic-gate struct utmpx *utmpend; 167*0Sstevel@tonic-gate struct utmpx *utp; 168*0Sstevel@tonic-gate struct uproc *up, *parent, *pgrp; 169*0Sstevel@tonic-gate struct psinfo info; 170*0Sstevel@tonic-gate struct sigaction actinfo[ACTSIZE]; 171*0Sstevel@tonic-gate struct pstatus statinfo; 172*0Sstevel@tonic-gate size_t size; 173*0Sstevel@tonic-gate struct stat sbuf; 174*0Sstevel@tonic-gate DIR *dirp; 175*0Sstevel@tonic-gate struct dirent *dp; 176*0Sstevel@tonic-gate char pname[64]; 177*0Sstevel@tonic-gate char *fname; 178*0Sstevel@tonic-gate int procfd; 179*0Sstevel@tonic-gate char *cp; 180*0Sstevel@tonic-gate int i; 181*0Sstevel@tonic-gate int days, hrs, mins; 182*0Sstevel@tonic-gate int entries; 183*0Sstevel@tonic-gate double loadavg[3]; 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate /* 186*0Sstevel@tonic-gate * This program needs the proc_owner privilege 187*0Sstevel@tonic-gate */ 188*0Sstevel@tonic-gate (void) __init_suid_priv(PU_CLEARLIMITSET, PRIV_PROC_OWNER, 189*0Sstevel@tonic-gate (char *)NULL); 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 192*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 193*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 194*0Sstevel@tonic-gate #endif 195*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate login = (argv[0][0] == '-'); 198*0Sstevel@tonic-gate cp = strrchr(argv[0], '/'); 199*0Sstevel@tonic-gate firstchar = login ? argv[0][1] : (cp == 0) ? argv[0][0] : cp[1]; 200*0Sstevel@tonic-gate prog = argv[0]; 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate while (argc > 1) { 203*0Sstevel@tonic-gate if (argv[1][0] == '-') { 204*0Sstevel@tonic-gate for (i = 1; argv[1][i]; i++) { 205*0Sstevel@tonic-gate switch (argv[1][i]) { 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gate case 'h': 208*0Sstevel@tonic-gate header = 0; 209*0Sstevel@tonic-gate break; 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate case 'l': 212*0Sstevel@tonic-gate lflag++; 213*0Sstevel@tonic-gate break; 214*0Sstevel@tonic-gate case 's': 215*0Sstevel@tonic-gate lflag = 0; 216*0Sstevel@tonic-gate break; 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate case 'u': 219*0Sstevel@tonic-gate case 'w': 220*0Sstevel@tonic-gate firstchar = argv[1][i]; 221*0Sstevel@tonic-gate break; 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate default: 224*0Sstevel@tonic-gate (void) fprintf(stderr, gettext( 225*0Sstevel@tonic-gate "%s: bad flag %s\n"), 226*0Sstevel@tonic-gate prog, argv[1]); 227*0Sstevel@tonic-gate exit(1); 228*0Sstevel@tonic-gate } 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate } else { 231*0Sstevel@tonic-gate if (!isalnum(argv[1][0]) || argc > 2) { 232*0Sstevel@tonic-gate (void) fprintf(stderr, gettext( 233*0Sstevel@tonic-gate "usage: %s [ -hlsuw ] [ user ]\n"), prog); 234*0Sstevel@tonic-gate exit(1); 235*0Sstevel@tonic-gate } else 236*0Sstevel@tonic-gate sel_user = argv[1]; 237*0Sstevel@tonic-gate } 238*0Sstevel@tonic-gate argc--; argv++; 239*0Sstevel@tonic-gate } 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate /* 242*0Sstevel@tonic-gate * read the UTMP_FILE (contains information about each logged in user) 243*0Sstevel@tonic-gate */ 244*0Sstevel@tonic-gate if (stat(UTMPX_FILE, &sbuf) == ERR) { 245*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: stat error of %s: %s\n"), 246*0Sstevel@tonic-gate prog, UTMPX_FILE, strerror(errno)); 247*0Sstevel@tonic-gate exit(1); 248*0Sstevel@tonic-gate } 249*0Sstevel@tonic-gate entries = sbuf.st_size / sizeof (struct futmpx); 250*0Sstevel@tonic-gate size = sizeof (struct utmpx) * entries; 251*0Sstevel@tonic-gate if ((ut = malloc(size)) == NULL) { 252*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: malloc error of %s: %s\n"), 253*0Sstevel@tonic-gate prog, UTMPX_FILE, strerror(errno)); 254*0Sstevel@tonic-gate exit(1); 255*0Sstevel@tonic-gate } 256*0Sstevel@tonic-gate 257*0Sstevel@tonic-gate (void) utmpxname(UTMPX_FILE); 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate utmpbegin = ut; 260*0Sstevel@tonic-gate utmpend = (struct utmpx *)((char *)utmpbegin + size); 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate setutxent(); 263*0Sstevel@tonic-gate while ((utp = getutxent()) != NULL) 264*0Sstevel@tonic-gate (void) memcpy(ut++, utp, sizeof (*ut)); 265*0Sstevel@tonic-gate endutxent(); 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate (void) time(&now); /* get current time */ 268*0Sstevel@tonic-gate 269*0Sstevel@tonic-gate if (header) { /* print a header */ 270*0Sstevel@tonic-gate prtat(&now); 271*0Sstevel@tonic-gate for (ut = utmpbegin; ut < utmpend; ut++) { 272*0Sstevel@tonic-gate if (ut->ut_type == USER_PROCESS) { 273*0Sstevel@tonic-gate if (!nonuser(*ut)) 274*0Sstevel@tonic-gate nusers++; 275*0Sstevel@tonic-gate } else if (ut->ut_type == BOOT_TIME) { 276*0Sstevel@tonic-gate uptime = now - ut->ut_xtime; 277*0Sstevel@tonic-gate uptime += 30; 278*0Sstevel@tonic-gate days = uptime / (60*60*24); 279*0Sstevel@tonic-gate uptime %= (60*60*24); 280*0Sstevel@tonic-gate hrs = uptime / (60*60); 281*0Sstevel@tonic-gate uptime %= (60*60); 282*0Sstevel@tonic-gate mins = uptime / 60; 283*0Sstevel@tonic-gate 284*0Sstevel@tonic-gate PRINTF((gettext(" up"))); 285*0Sstevel@tonic-gate if (days > 0) 286*0Sstevel@tonic-gate PRINTF((gettext( 287*0Sstevel@tonic-gate " %d day(s),"), days)); 288*0Sstevel@tonic-gate if (hrs > 0 && mins > 0) { 289*0Sstevel@tonic-gate PRINTF((" %2d:%02d,", hrs, mins)); 290*0Sstevel@tonic-gate } else { 291*0Sstevel@tonic-gate if (hrs > 0) 292*0Sstevel@tonic-gate PRINTF((gettext( 293*0Sstevel@tonic-gate " %d hr(s),"), hrs)); 294*0Sstevel@tonic-gate if (mins > 0) 295*0Sstevel@tonic-gate PRINTF((gettext( 296*0Sstevel@tonic-gate " %d min(s),"), mins)); 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate } 299*0Sstevel@tonic-gate } 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate ut = utmpbegin; /* rewind utmp data */ 302*0Sstevel@tonic-gate PRINTF((((nusers == 1) ? 303*0Sstevel@tonic-gate gettext(" %d user") : gettext(" %d users")), nusers)); 304*0Sstevel@tonic-gate /* 305*0Sstevel@tonic-gate * Print 1, 5, and 15 minute load averages. 306*0Sstevel@tonic-gate */ 307*0Sstevel@tonic-gate (void) getloadavg(loadavg, 3); 308*0Sstevel@tonic-gate PRINTF((gettext(", load average: %.2f, %.2f, %.2f\n"), 309*0Sstevel@tonic-gate loadavg[LOADAVG_1MIN], loadavg[LOADAVG_5MIN], 310*0Sstevel@tonic-gate loadavg[LOADAVG_15MIN])); 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate if (firstchar == 'u') /* uptime command */ 313*0Sstevel@tonic-gate exit(0); 314*0Sstevel@tonic-gate 315*0Sstevel@tonic-gate if (lflag) { 316*0Sstevel@tonic-gate PRINTF((dcgettext(NULL, "User tty " 317*0Sstevel@tonic-gate "login@ idle JCPU PCPU what\n", LC_TIME))); 318*0Sstevel@tonic-gate } else { 319*0Sstevel@tonic-gate PRINTF((dcgettext(NULL, 320*0Sstevel@tonic-gate "User tty idle what\n", LC_TIME))); 321*0Sstevel@tonic-gate } 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate if (fflush(stdout) == EOF) { 324*0Sstevel@tonic-gate perror((gettext("%s: fflush failed\n"), prog)); 325*0Sstevel@tonic-gate exit(1); 326*0Sstevel@tonic-gate } 327*0Sstevel@tonic-gate } 328*0Sstevel@tonic-gate 329*0Sstevel@tonic-gate /* 330*0Sstevel@tonic-gate * loop through /proc, reading info about each process 331*0Sstevel@tonic-gate * and build the parent/child tree 332*0Sstevel@tonic-gate */ 333*0Sstevel@tonic-gate if (!(dirp = opendir(PROCDIR))) { 334*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: could not open %s: %s\n"), 335*0Sstevel@tonic-gate prog, PROCDIR, strerror(errno)); 336*0Sstevel@tonic-gate exit(1); 337*0Sstevel@tonic-gate } 338*0Sstevel@tonic-gate 339*0Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) { 340*0Sstevel@tonic-gate if (dp->d_name[0] == '.') 341*0Sstevel@tonic-gate continue; 342*0Sstevel@tonic-gate retry: 343*0Sstevel@tonic-gate (void) sprintf(pname, "%s/%s/", PROCDIR, dp->d_name); 344*0Sstevel@tonic-gate fname = pname + strlen(pname); 345*0Sstevel@tonic-gate (void) strcpy(fname, "psinfo"); 346*0Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) < 0) 347*0Sstevel@tonic-gate continue; 348*0Sstevel@tonic-gate if (read(procfd, &info, sizeof (info)) != sizeof (info)) { 349*0Sstevel@tonic-gate int err = errno; 350*0Sstevel@tonic-gate (void) close(procfd); 351*0Sstevel@tonic-gate if (err == EAGAIN) 352*0Sstevel@tonic-gate goto retry; 353*0Sstevel@tonic-gate if (err != ENOENT) 354*0Sstevel@tonic-gate (void) fprintf(stderr, gettext( 355*0Sstevel@tonic-gate "%s: read() failed on %s: %s \n"), 356*0Sstevel@tonic-gate prog, pname, strerror(err)); 357*0Sstevel@tonic-gate continue; 358*0Sstevel@tonic-gate } 359*0Sstevel@tonic-gate (void) close(procfd); 360*0Sstevel@tonic-gate 361*0Sstevel@tonic-gate up = findhash(info.pr_pid); 362*0Sstevel@tonic-gate up->p_ttyd = info.pr_ttydev; 363*0Sstevel@tonic-gate up->p_state = (info.pr_nlwp == 0? ZOMBIE : RUNNING); 364*0Sstevel@tonic-gate up->p_time = 0; 365*0Sstevel@tonic-gate up->p_ctime = 0; 366*0Sstevel@tonic-gate up->p_igintr = 0; 367*0Sstevel@tonic-gate (void) strncpy(up->p_comm, info.pr_fname, 368*0Sstevel@tonic-gate sizeof (info.pr_fname)); 369*0Sstevel@tonic-gate up->p_args[0] = 0; 370*0Sstevel@tonic-gate 371*0Sstevel@tonic-gate if (up->p_state != NONE && up->p_state != ZOMBIE) { 372*0Sstevel@tonic-gate (void) strcpy(fname, "status"); 373*0Sstevel@tonic-gate 374*0Sstevel@tonic-gate /* now we need the proc_owner privilege */ 375*0Sstevel@tonic-gate (void) __priv_bracket(PRIV_ON); 376*0Sstevel@tonic-gate 377*0Sstevel@tonic-gate procfd = open(pname, O_RDONLY); 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gate /* drop proc_owner privilege after open */ 380*0Sstevel@tonic-gate (void) __priv_bracket(PRIV_OFF); 381*0Sstevel@tonic-gate 382*0Sstevel@tonic-gate if (procfd < 0) 383*0Sstevel@tonic-gate continue; 384*0Sstevel@tonic-gate 385*0Sstevel@tonic-gate if (read(procfd, &statinfo, sizeof (statinfo)) 386*0Sstevel@tonic-gate != sizeof (statinfo)) { 387*0Sstevel@tonic-gate int err = errno; 388*0Sstevel@tonic-gate (void) close(procfd); 389*0Sstevel@tonic-gate if (err == EAGAIN) 390*0Sstevel@tonic-gate goto retry; 391*0Sstevel@tonic-gate if (err != ENOENT) 392*0Sstevel@tonic-gate (void) fprintf(stderr, gettext( 393*0Sstevel@tonic-gate "%s: read() failed on %s: %s \n"), 394*0Sstevel@tonic-gate prog, pname, strerror(err)); 395*0Sstevel@tonic-gate continue; 396*0Sstevel@tonic-gate } 397*0Sstevel@tonic-gate (void) close(procfd); 398*0Sstevel@tonic-gate 399*0Sstevel@tonic-gate up->p_time = statinfo.pr_utime.tv_sec + 400*0Sstevel@tonic-gate statinfo.pr_stime.tv_sec; /* seconds */ 401*0Sstevel@tonic-gate up->p_ctime = statinfo.pr_cutime.tv_sec + 402*0Sstevel@tonic-gate statinfo.pr_cstime.tv_sec; 403*0Sstevel@tonic-gate 404*0Sstevel@tonic-gate (void) strcpy(fname, "sigact"); 405*0Sstevel@tonic-gate 406*0Sstevel@tonic-gate /* now we need the proc_owner privilege */ 407*0Sstevel@tonic-gate (void) __priv_bracket(PRIV_ON); 408*0Sstevel@tonic-gate 409*0Sstevel@tonic-gate procfd = open(pname, O_RDONLY); 410*0Sstevel@tonic-gate 411*0Sstevel@tonic-gate /* drop proc_owner privilege after open */ 412*0Sstevel@tonic-gate (void) __priv_bracket(PRIV_OFF); 413*0Sstevel@tonic-gate 414*0Sstevel@tonic-gate if (procfd < 0) 415*0Sstevel@tonic-gate continue; 416*0Sstevel@tonic-gate 417*0Sstevel@tonic-gate if (read(procfd, actinfo, sizeof (actinfo)) 418*0Sstevel@tonic-gate != sizeof (actinfo)) { 419*0Sstevel@tonic-gate int err = errno; 420*0Sstevel@tonic-gate (void) close(procfd); 421*0Sstevel@tonic-gate if (err == EAGAIN) 422*0Sstevel@tonic-gate goto retry; 423*0Sstevel@tonic-gate if (err != ENOENT) 424*0Sstevel@tonic-gate (void) fprintf(stderr, gettext( 425*0Sstevel@tonic-gate "%s: read() failed on %s: %s \n"), 426*0Sstevel@tonic-gate prog, pname, strerror(err)); 427*0Sstevel@tonic-gate continue; 428*0Sstevel@tonic-gate } 429*0Sstevel@tonic-gate (void) close(procfd); 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate up->p_igintr = 432*0Sstevel@tonic-gate actinfo[SIGINT-1].sa_handler == SIG_IGN && 433*0Sstevel@tonic-gate actinfo[SIGQUIT-1].sa_handler == SIG_IGN; 434*0Sstevel@tonic-gate 435*0Sstevel@tonic-gate /* 436*0Sstevel@tonic-gate * Process args. 437*0Sstevel@tonic-gate */ 438*0Sstevel@tonic-gate up->p_args[0] = 0; 439*0Sstevel@tonic-gate clnarglist(info.pr_psargs); 440*0Sstevel@tonic-gate (void) strcat(up->p_args, info.pr_psargs); 441*0Sstevel@tonic-gate if (up->p_args[0] == 0 || 442*0Sstevel@tonic-gate up->p_args[0] == '-' && up->p_args[1] <= ' ' || 443*0Sstevel@tonic-gate up->p_args[0] == '?') { 444*0Sstevel@tonic-gate (void) strcat(up->p_args, " ("); 445*0Sstevel@tonic-gate (void) strcat(up->p_args, up->p_comm); 446*0Sstevel@tonic-gate (void) strcat(up->p_args, ")"); 447*0Sstevel@tonic-gate } 448*0Sstevel@tonic-gate } 449*0Sstevel@tonic-gate 450*0Sstevel@tonic-gate /* 451*0Sstevel@tonic-gate * link pgrp together in case parents go away 452*0Sstevel@tonic-gate * Pgrp chain is a single linked list originating 453*0Sstevel@tonic-gate * from the pgrp leader to its group member. 454*0Sstevel@tonic-gate */ 455*0Sstevel@tonic-gate if (info.pr_pgid != info.pr_pid) { /* not pgrp leader */ 456*0Sstevel@tonic-gate pgrp = findhash(info.pr_pgid); 457*0Sstevel@tonic-gate up->p_pgrpl = pgrp->p_pgrpl; 458*0Sstevel@tonic-gate pgrp->p_pgrpl = up; 459*0Sstevel@tonic-gate } 460*0Sstevel@tonic-gate parent = findhash(info.pr_ppid); 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gate /* if this is the new member, link it in */ 463*0Sstevel@tonic-gate if (parent->p_upid != INITPROCESS) { 464*0Sstevel@tonic-gate if (parent->p_child) { 465*0Sstevel@tonic-gate up->p_sibling = parent->p_child; 466*0Sstevel@tonic-gate up->p_child = 0; 467*0Sstevel@tonic-gate } 468*0Sstevel@tonic-gate parent->p_child = up; 469*0Sstevel@tonic-gate } 470*0Sstevel@tonic-gate } 471*0Sstevel@tonic-gate 472*0Sstevel@tonic-gate /* revert to non-privileged user after opening */ 473*0Sstevel@tonic-gate (void) __priv_relinquish(); 474*0Sstevel@tonic-gate 475*0Sstevel@tonic-gate (void) closedir(dirp); 476*0Sstevel@tonic-gate (void) time(&now); /* get current time */ 477*0Sstevel@tonic-gate 478*0Sstevel@tonic-gate /* 479*0Sstevel@tonic-gate * loop through utmpx file, printing process info 480*0Sstevel@tonic-gate * about each logged in user 481*0Sstevel@tonic-gate */ 482*0Sstevel@tonic-gate for (ut = utmpbegin; ut < utmpend; ut++) { 483*0Sstevel@tonic-gate if (ut->ut_type != USER_PROCESS) 484*0Sstevel@tonic-gate continue; 485*0Sstevel@tonic-gate if (sel_user && strncmp(ut->ut_name, sel_user, NMAX) != 0) 486*0Sstevel@tonic-gate continue; /* we're looking for somebody else */ 487*0Sstevel@tonic-gate 488*0Sstevel@tonic-gate /* print login name of the user */ 489*0Sstevel@tonic-gate PRINTF(("%-*.*s ", NMAX, NMAX, ut->ut_name)); 490*0Sstevel@tonic-gate 491*0Sstevel@tonic-gate /* print tty user is on */ 492*0Sstevel@tonic-gate if (lflag) { 493*0Sstevel@tonic-gate PRINTF(("%-*.*s", LMAX, LMAX, ut->ut_line)); 494*0Sstevel@tonic-gate } else { 495*0Sstevel@tonic-gate if (ut->ut_line[0] == 'p' && ut->ut_line[1] == 't' && 496*0Sstevel@tonic-gate ut->ut_line[2] == 's' && ut->ut_line[3] == '/') { 497*0Sstevel@tonic-gate PRINTF(("%-*.3s", LMAX, &ut->ut_line[4])); 498*0Sstevel@tonic-gate } else { 499*0Sstevel@tonic-gate PRINTF(("%-*.*s", LMAX, LMAX, ut->ut_line)); 500*0Sstevel@tonic-gate } 501*0Sstevel@tonic-gate } 502*0Sstevel@tonic-gate 503*0Sstevel@tonic-gate /* print when the user logged in */ 504*0Sstevel@tonic-gate if (lflag) { 505*0Sstevel@tonic-gate time_t tim = ut->ut_xtime; 506*0Sstevel@tonic-gate prtat(&tim); 507*0Sstevel@tonic-gate } 508*0Sstevel@tonic-gate 509*0Sstevel@tonic-gate /* print idle time */ 510*0Sstevel@tonic-gate idle = findidle(ut->ut_line); 511*0Sstevel@tonic-gate if (idle >= 36 * 60) { 512*0Sstevel@tonic-gate PRINTF((dcgettext(NULL, "%2ddays ", LC_TIME), 513*0Sstevel@tonic-gate (idle + 12 * 60) / (24 * 60))); 514*0Sstevel@tonic-gate } else 515*0Sstevel@tonic-gate prttime(idle, " "); 516*0Sstevel@tonic-gate showtotals(findhash(ut->ut_pid)); 517*0Sstevel@tonic-gate } 518*0Sstevel@tonic-gate if (fclose(stdout) == EOF) { 519*0Sstevel@tonic-gate perror((gettext("%s: fclose failed"), prog)); 520*0Sstevel@tonic-gate exit(1); 521*0Sstevel@tonic-gate } 522*0Sstevel@tonic-gate return (0); 523*0Sstevel@tonic-gate } 524*0Sstevel@tonic-gate 525*0Sstevel@tonic-gate /* 526*0Sstevel@tonic-gate * Prints the CPU time for all processes & children, 527*0Sstevel@tonic-gate * and the cpu time for interesting process, 528*0Sstevel@tonic-gate * and what the user is doing. 529*0Sstevel@tonic-gate */ 530*0Sstevel@tonic-gate static void 531*0Sstevel@tonic-gate showtotals(struct uproc *up) 532*0Sstevel@tonic-gate { 533*0Sstevel@tonic-gate jobtime = 0; 534*0Sstevel@tonic-gate proctime = 0; 535*0Sstevel@tonic-gate empty = 1; 536*0Sstevel@tonic-gate curpid = -1; 537*0Sstevel@tonic-gate add_times = 1; 538*0Sstevel@tonic-gate 539*0Sstevel@tonic-gate calctotals(up); 540*0Sstevel@tonic-gate 541*0Sstevel@tonic-gate if (lflag) { 542*0Sstevel@tonic-gate /* print CPU time for all processes & children */ 543*0Sstevel@tonic-gate /* and need to convert clock ticks to seconds first */ 544*0Sstevel@tonic-gate prttime((time_t)jobtime, " "); 545*0Sstevel@tonic-gate 546*0Sstevel@tonic-gate /* print cpu time for interesting process */ 547*0Sstevel@tonic-gate /* and need to convert clock ticks to seconds first */ 548*0Sstevel@tonic-gate prttime((time_t)proctime, " "); 549*0Sstevel@tonic-gate } 550*0Sstevel@tonic-gate /* what user is doing, current process */ 551*0Sstevel@tonic-gate PRINTF((" %-.32s\n", doing)); 552*0Sstevel@tonic-gate } 553*0Sstevel@tonic-gate 554*0Sstevel@tonic-gate /* 555*0Sstevel@tonic-gate * This recursive routine descends the process 556*0Sstevel@tonic-gate * tree starting from the given process pointer(up). 557*0Sstevel@tonic-gate * It used depth-first search strategy and also marked 558*0Sstevel@tonic-gate * each node as visited as it traversed down the tree. 559*0Sstevel@tonic-gate * It calculates the process time for all processes & 560*0Sstevel@tonic-gate * children. It also finds the interesting process 561*0Sstevel@tonic-gate * and determines its cpu time and command. 562*0Sstevel@tonic-gate */ 563*0Sstevel@tonic-gate static void 564*0Sstevel@tonic-gate calctotals(struct uproc *up) 565*0Sstevel@tonic-gate { 566*0Sstevel@tonic-gate struct uproc *zp; 567*0Sstevel@tonic-gate 568*0Sstevel@tonic-gate /* 569*0Sstevel@tonic-gate * Once a node has been visited, stop adding cpu times 570*0Sstevel@tonic-gate * for its children so they don't get totalled twice. 571*0Sstevel@tonic-gate * Still look for the interesting job for this utmp 572*0Sstevel@tonic-gate * entry, however. 573*0Sstevel@tonic-gate */ 574*0Sstevel@tonic-gate if (up->p_state == VISITED) 575*0Sstevel@tonic-gate add_times = 0; 576*0Sstevel@tonic-gate up->p_state = VISITED; 577*0Sstevel@tonic-gate if (up->p_state == NONE || up->p_state == ZOMBIE) 578*0Sstevel@tonic-gate return; 579*0Sstevel@tonic-gate 580*0Sstevel@tonic-gate if (empty && !up->p_igintr) { 581*0Sstevel@tonic-gate empty = 0; 582*0Sstevel@tonic-gate curpid = -1; 583*0Sstevel@tonic-gate } 584*0Sstevel@tonic-gate 585*0Sstevel@tonic-gate if (up->p_upid > curpid && (!up->p_igintr || empty)) { 586*0Sstevel@tonic-gate curpid = up->p_upid; 587*0Sstevel@tonic-gate if (lflag) 588*0Sstevel@tonic-gate (void) strcpy(doing, up->p_args); 589*0Sstevel@tonic-gate else 590*0Sstevel@tonic-gate (void) strcpy(doing, up->p_comm); 591*0Sstevel@tonic-gate } 592*0Sstevel@tonic-gate 593*0Sstevel@tonic-gate if (add_times == 1) { 594*0Sstevel@tonic-gate jobtime += up->p_time + up->p_ctime; 595*0Sstevel@tonic-gate proctime += up->p_time; 596*0Sstevel@tonic-gate } 597*0Sstevel@tonic-gate 598*0Sstevel@tonic-gate /* descend for its children */ 599*0Sstevel@tonic-gate if (up->p_child) { 600*0Sstevel@tonic-gate calctotals(up->p_child); 601*0Sstevel@tonic-gate for (zp = up->p_child->p_sibling; zp; zp = zp->p_sibling) 602*0Sstevel@tonic-gate calctotals(zp); 603*0Sstevel@tonic-gate } 604*0Sstevel@tonic-gate } 605*0Sstevel@tonic-gate 606*0Sstevel@tonic-gate /* 607*0Sstevel@tonic-gate * Findhash finds the appropriate entry in the process 608*0Sstevel@tonic-gate * hash table (pr_htbl) for the given pid in case that 609*0Sstevel@tonic-gate * pid exists on the hash chain. It returns back a pointer 610*0Sstevel@tonic-gate * to that uproc structure. If this is a new pid, it allocates 611*0Sstevel@tonic-gate * a new node, initializes it, links it into the chain (after 612*0Sstevel@tonic-gate * head) and returns a structure pointer. 613*0Sstevel@tonic-gate */ 614*0Sstevel@tonic-gate static struct uproc * 615*0Sstevel@tonic-gate findhash(pid_t pid) 616*0Sstevel@tonic-gate { 617*0Sstevel@tonic-gate struct uproc *up, *tp; 618*0Sstevel@tonic-gate 619*0Sstevel@tonic-gate tp = up = &pr_htbl[pid % HSIZE]; 620*0Sstevel@tonic-gate if (up->p_upid == 0) { /* empty slot */ 621*0Sstevel@tonic-gate up->p_upid = pid; 622*0Sstevel@tonic-gate up->p_state = NONE; 623*0Sstevel@tonic-gate up->p_child = up->p_sibling = up->p_pgrpl = up->p_link = 0; 624*0Sstevel@tonic-gate return (up); 625*0Sstevel@tonic-gate } 626*0Sstevel@tonic-gate if (up->p_upid == pid) { /* found in hash table */ 627*0Sstevel@tonic-gate return (up); 628*0Sstevel@tonic-gate } 629*0Sstevel@tonic-gate for (tp = up->p_link; tp; tp = tp->p_link) { /* follow chain */ 630*0Sstevel@tonic-gate if (tp->p_upid == pid) 631*0Sstevel@tonic-gate return (tp); 632*0Sstevel@tonic-gate } 633*0Sstevel@tonic-gate tp = malloc(sizeof (*tp)); /* add new node */ 634*0Sstevel@tonic-gate if (!tp) { 635*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: out of memory!: %s\n"), 636*0Sstevel@tonic-gate prog, strerror(errno)); 637*0Sstevel@tonic-gate exit(1); 638*0Sstevel@tonic-gate } 639*0Sstevel@tonic-gate (void) memset(tp, 0, sizeof (*tp)); 640*0Sstevel@tonic-gate tp->p_upid = pid; 641*0Sstevel@tonic-gate tp->p_state = NONE; 642*0Sstevel@tonic-gate tp->p_child = tp->p_sibling = tp->p_pgrpl = 0; 643*0Sstevel@tonic-gate tp->p_link = up->p_link; /* insert after head */ 644*0Sstevel@tonic-gate up->p_link = tp; 645*0Sstevel@tonic-gate return (tp); 646*0Sstevel@tonic-gate } 647*0Sstevel@tonic-gate 648*0Sstevel@tonic-gate #define HR (60 * 60) 649*0Sstevel@tonic-gate #define DAY (24 * HR) 650*0Sstevel@tonic-gate #define MON (30 * DAY) 651*0Sstevel@tonic-gate 652*0Sstevel@tonic-gate /* 653*0Sstevel@tonic-gate * prttime prints a time in hours and minutes or minutes and seconds. 654*0Sstevel@tonic-gate * The character string tail is printed at the end, obvious 655*0Sstevel@tonic-gate * strings to pass are "", " ", or "am". 656*0Sstevel@tonic-gate */ 657*0Sstevel@tonic-gate static void 658*0Sstevel@tonic-gate prttime(time_t tim, char *tail) 659*0Sstevel@tonic-gate { 660*0Sstevel@tonic-gate if (tim >= 60) { 661*0Sstevel@tonic-gate PRINTF((dcgettext(NULL, "%3d:%02d", LC_TIME), 662*0Sstevel@tonic-gate (int)tim/60, (int)tim%60)); 663*0Sstevel@tonic-gate } else if (tim > 0) { 664*0Sstevel@tonic-gate PRINTF((dcgettext(NULL, " %2d", LC_TIME), (int)tim)); 665*0Sstevel@tonic-gate } else { 666*0Sstevel@tonic-gate PRINTF((" ")); 667*0Sstevel@tonic-gate } 668*0Sstevel@tonic-gate PRINTF(("%s", tail)); 669*0Sstevel@tonic-gate } 670*0Sstevel@tonic-gate 671*0Sstevel@tonic-gate /* 672*0Sstevel@tonic-gate * prints a 12 hour time given a pointer to a time of day 673*0Sstevel@tonic-gate */ 674*0Sstevel@tonic-gate static void 675*0Sstevel@tonic-gate prtat(time_t *time) 676*0Sstevel@tonic-gate { 677*0Sstevel@tonic-gate struct tm *p; 678*0Sstevel@tonic-gate 679*0Sstevel@tonic-gate p = localtime(time); 680*0Sstevel@tonic-gate if (now - *time <= 18 * HR) { 681*0Sstevel@tonic-gate char timestr[50]; 682*0Sstevel@tonic-gate (void) strftime(timestr, sizeof (timestr), 683*0Sstevel@tonic-gate dcgettext(NULL, "%l:%M""%p", LC_TIME), p); 684*0Sstevel@tonic-gate checkampm(timestr); 685*0Sstevel@tonic-gate PRINTF((" %s", timestr)); 686*0Sstevel@tonic-gate } else if (now - *time <= 7 * DAY) { 687*0Sstevel@tonic-gate char weekdaytime[20]; 688*0Sstevel@tonic-gate 689*0Sstevel@tonic-gate (void) strftime(weekdaytime, sizeof (weekdaytime), 690*0Sstevel@tonic-gate dcgettext(NULL, "%a%l%p", LC_TIME), p); 691*0Sstevel@tonic-gate checkampm(weekdaytime); 692*0Sstevel@tonic-gate PRINTF((" %s", weekdaytime)); 693*0Sstevel@tonic-gate } else { 694*0Sstevel@tonic-gate char monthtime[20]; 695*0Sstevel@tonic-gate 696*0Sstevel@tonic-gate (void) strftime(monthtime, sizeof (monthtime), 697*0Sstevel@tonic-gate dcgettext(NULL, "%e%b%y", LC_TIME), p); 698*0Sstevel@tonic-gate PRINTF((" %s", monthtime)); 699*0Sstevel@tonic-gate } 700*0Sstevel@tonic-gate } 701*0Sstevel@tonic-gate 702*0Sstevel@tonic-gate /* 703*0Sstevel@tonic-gate * find & return number of minutes current tty has been idle 704*0Sstevel@tonic-gate */ 705*0Sstevel@tonic-gate static time_t 706*0Sstevel@tonic-gate findidle(char *devname) 707*0Sstevel@tonic-gate { 708*0Sstevel@tonic-gate struct stat stbuf; 709*0Sstevel@tonic-gate time_t lastaction, diff; 710*0Sstevel@tonic-gate char ttyname[64]; 711*0Sstevel@tonic-gate 712*0Sstevel@tonic-gate (void) strcpy(ttyname, "/dev/"); 713*0Sstevel@tonic-gate (void) strcat(ttyname, devname); 714*0Sstevel@tonic-gate if (stat(ttyname, &stbuf) != -1) { 715*0Sstevel@tonic-gate lastaction = stbuf.st_atime; 716*0Sstevel@tonic-gate diff = now - lastaction; 717*0Sstevel@tonic-gate diff = DIV60(diff); 718*0Sstevel@tonic-gate if (diff < 0) 719*0Sstevel@tonic-gate diff = 0; 720*0Sstevel@tonic-gate } else 721*0Sstevel@tonic-gate diff = 0; 722*0Sstevel@tonic-gate return (diff); 723*0Sstevel@tonic-gate } 724*0Sstevel@tonic-gate 725*0Sstevel@tonic-gate /* 726*0Sstevel@tonic-gate * given a pointer to the argument string get rid of unsavory characters. 727*0Sstevel@tonic-gate */ 728*0Sstevel@tonic-gate static void 729*0Sstevel@tonic-gate clnarglist(char *arglist) 730*0Sstevel@tonic-gate { 731*0Sstevel@tonic-gate char *c; 732*0Sstevel@tonic-gate int err = 0; 733*0Sstevel@tonic-gate 734*0Sstevel@tonic-gate /* get rid of unsavory characters */ 735*0Sstevel@tonic-gate for (c = arglist; *c != NULL; c++) { 736*0Sstevel@tonic-gate if ((*c < ' ') || (*c > 0176)) { 737*0Sstevel@tonic-gate if (err++ > 5) { 738*0Sstevel@tonic-gate *arglist = NULL; 739*0Sstevel@tonic-gate break; 740*0Sstevel@tonic-gate } 741*0Sstevel@tonic-gate *c = '?'; 742*0Sstevel@tonic-gate } 743*0Sstevel@tonic-gate } 744*0Sstevel@tonic-gate } 745*0Sstevel@tonic-gate 746*0Sstevel@tonic-gate /* replaces all occurences of AM/PM with am/pm */ 747*0Sstevel@tonic-gate static void 748*0Sstevel@tonic-gate checkampm(char *str) 749*0Sstevel@tonic-gate { 750*0Sstevel@tonic-gate char *ampm; 751*0Sstevel@tonic-gate while ((ampm = strstr(str, "AM")) != NULL || 752*0Sstevel@tonic-gate (ampm = strstr(str, "PM")) != NULL) { 753*0Sstevel@tonic-gate *ampm = tolower(*ampm); 754*0Sstevel@tonic-gate *(ampm+1) = tolower(*(ampm+1)); 755*0Sstevel@tonic-gate } 756*0Sstevel@tonic-gate } 757