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 51337fffaSsr161167 * Common Development and Distribution License (the "License"). 61337fffaSsr161167 * 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 */ 21*9ba7e32fSdduvall 22d2117003Sdp /* 231337fffaSsr161167 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24d2117003Sdp * Use is subject to license terms. 25d2117003Sdp */ 261337fffaSsr161167 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 30*9ba7e32fSdduvall 31d2117003Sdp #pragma ident "%Z%%M% %I% %E% SMI" 32d2117003Sdp 337c478bd9Sstevel@tonic-gate #include <sys/types.h> 347c478bd9Sstevel@tonic-gate #include <sys/times.h> 357c478bd9Sstevel@tonic-gate #include <sys/param.h> 36d2117003Sdp #include <sys/wait.h> 37d2117003Sdp #include <unistd.h> 387c478bd9Sstevel@tonic-gate #include <stdio.h> 39d2117003Sdp #include <stdlib.h> 407c478bd9Sstevel@tonic-gate #include <signal.h> 41d2117003Sdp #include <strings.h> 427c478bd9Sstevel@tonic-gate #include <time.h> 437c478bd9Sstevel@tonic-gate #include <errno.h> 447c478bd9Sstevel@tonic-gate #include <pwd.h> 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate char fname[20]; 477c478bd9Sstevel@tonic-gate 48*9ba7e32fSdduvall /* 49*9ba7e32fSdduvall * Quant[0] will get set to HZ/10 later. 50*9ba7e32fSdduvall */ 51*9ba7e32fSdduvall int quant[] = { 10, 10, 10, 6, 10, 6, 10, 10, 10, 10, 10 }; 52*9ba7e32fSdduvall 53*9ba7e32fSdduvall void printt(char *, time_t); 54d2117003Sdp void hmstime(char[]); 55d2117003Sdp void diag(char *); 56d2117003Sdp 57d2117003Sdp int 58d2117003Sdp main(int argc, char **argv) 597c478bd9Sstevel@tonic-gate { 607c478bd9Sstevel@tonic-gate struct tms buffer, obuffer; 617c478bd9Sstevel@tonic-gate int status; 627c478bd9Sstevel@tonic-gate register pid_t p; 637c478bd9Sstevel@tonic-gate int c; 64*9ba7e32fSdduvall time_t before, after; 657c478bd9Sstevel@tonic-gate char stime[9], etime[9]; 667c478bd9Sstevel@tonic-gate char cmd[80]; 677c478bd9Sstevel@tonic-gate int pflg = 0, sflg = 0, oflg = 0; 687c478bd9Sstevel@tonic-gate char aopt[25]; 69d2117003Sdp FILE *pipin; 707c478bd9Sstevel@tonic-gate char ttyid[12], line[150]; 717c478bd9Sstevel@tonic-gate char eol; 727c478bd9Sstevel@tonic-gate char fld[20][12]; 737c478bd9Sstevel@tonic-gate int iline = 0, i, nfld; 747c478bd9Sstevel@tonic-gate int ichar, iblok; 757c478bd9Sstevel@tonic-gate long chars = 0, bloks = 0; 767c478bd9Sstevel@tonic-gate 77*9ba7e32fSdduvall /* initalize quant array using the sysconf() */ 78*9ba7e32fSdduvall quant[0] = ((int)sysconf(_SC_CLK_TCK))/10; 79*9ba7e32fSdduvall 807c478bd9Sstevel@tonic-gate aopt[0] = '\0'; /* terminate the string #1245107 */ 817c478bd9Sstevel@tonic-gate /* check options; */ 827c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "sopfhkmrt")) != EOF) 837c478bd9Sstevel@tonic-gate switch (c) { 847c478bd9Sstevel@tonic-gate case 's': sflg++; break; 857c478bd9Sstevel@tonic-gate case 'o': oflg++; break; 867c478bd9Sstevel@tonic-gate case 'p': pflg++; break; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate case 'f': strcat(aopt, "-f "); break; 897c478bd9Sstevel@tonic-gate case 'h': strcat(aopt, "-h "); break; 907c478bd9Sstevel@tonic-gate case 'k': strcat(aopt, "-k "); break; 917c478bd9Sstevel@tonic-gate case 'm': strcat(aopt, "-m "); break; 927c478bd9Sstevel@tonic-gate case 'r': strcat(aopt, "-r "); break; 937c478bd9Sstevel@tonic-gate case 't': strcat(aopt, "-t "); break; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate case '?': diag("Usage: timex [-s][-o][-p[-fhkmrt]] cmd"); 967c478bd9Sstevel@tonic-gate break; 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate if (optind >= argc) diag("Missing command"); 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* 1017c478bd9Sstevel@tonic-gate * Check to see if accounting is installed and print a somewhat 1027c478bd9Sstevel@tonic-gate * meaninful message if not. 1037c478bd9Sstevel@tonic-gate */ 1047c478bd9Sstevel@tonic-gate if (((oflg+pflg) != 0) && (access("/usr/bin/acctcom", 01) == -1)) { 1057c478bd9Sstevel@tonic-gate oflg = 0; 1067c478bd9Sstevel@tonic-gate pflg = 0; 1077c478bd9Sstevel@tonic-gate fprintf(stderr, 1087c478bd9Sstevel@tonic-gate "Information from -p and -o options not available\n"); 1097c478bd9Sstevel@tonic-gate fprintf(stderr, 1107c478bd9Sstevel@tonic-gate " because process accounting is not operational.\n"); 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate if (sflg) { 1147c478bd9Sstevel@tonic-gate sprintf(fname, "/tmp/tmx%ld", getpid()); 1157c478bd9Sstevel@tonic-gate sprintf(cmd, "/usr/lib/sa/sadc 1 1 %s", fname); 1167c478bd9Sstevel@tonic-gate system(cmd); 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate if (pflg + oflg) hmstime(stime); 119*9ba7e32fSdduvall before = times(&obuffer); 1207c478bd9Sstevel@tonic-gate if ((p = fork()) == (pid_t)-1) diag("Try again.\n"); 1217c478bd9Sstevel@tonic-gate if (p == 0) { 1227c478bd9Sstevel@tonic-gate setgid(getgid()); 1237c478bd9Sstevel@tonic-gate execvp(*(argv+optind), (argv+optind)); 124d2117003Sdp fprintf(stderr, "%s: %s\n", *(argv+optind), strerror(errno)); 1257c478bd9Sstevel@tonic-gate exit(1); 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate signal(SIGINT, SIG_IGN); 1287c478bd9Sstevel@tonic-gate signal(SIGQUIT, SIG_IGN); 129d2117003Sdp while (wait(&status) != p) 130d2117003Sdp ; 1317c478bd9Sstevel@tonic-gate if ((status&0377) != 0) 1327c478bd9Sstevel@tonic-gate fprintf(stderr, "Command terminated abnormally.\n"); 1337c478bd9Sstevel@tonic-gate signal(SIGINT, SIG_DFL); 1347c478bd9Sstevel@tonic-gate signal(SIGQUIT, SIG_DFL); 135*9ba7e32fSdduvall after = times(&buffer); 1367c478bd9Sstevel@tonic-gate if (pflg + oflg) hmstime(etime); 1377c478bd9Sstevel@tonic-gate if (sflg) system(cmd); 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate fprintf(stderr, "\n"); 140*9ba7e32fSdduvall printt("real", (after-before)); 141*9ba7e32fSdduvall printt("user", buffer.tms_cutime - obuffer.tms_cutime); 142*9ba7e32fSdduvall printt("sys ", buffer.tms_cstime - obuffer.tms_cstime); 1437c478bd9Sstevel@tonic-gate fprintf(stderr, "\n"); 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate if (oflg+pflg) { 1467c478bd9Sstevel@tonic-gate if (isatty(0)) 1477c478bd9Sstevel@tonic-gate sprintf(ttyid, "-l %s", ttyname(0)+5); 1487c478bd9Sstevel@tonic-gate sprintf(cmd, "acctcom -S %s -E %s -u %s %s -i %s", 1497c478bd9Sstevel@tonic-gate stime, etime, getpwuid(getuid())->pw_name, ttyid, aopt); 1507c478bd9Sstevel@tonic-gate pipin = popen(cmd, "r"); 1517c478bd9Sstevel@tonic-gate while (fscanf(pipin, "%[^\n]%1c", line, &eol) > 1) { 1527c478bd9Sstevel@tonic-gate if (pflg) 1537c478bd9Sstevel@tonic-gate fprintf(stderr, "%s\n", line); 1547c478bd9Sstevel@tonic-gate if (oflg) { 1557c478bd9Sstevel@tonic-gate nfld = sscanf(line, 1567c478bd9Sstevel@tonic-gate "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", 1577c478bd9Sstevel@tonic-gate fld[0], fld[1], fld[2], fld[3], fld[4], 1587c478bd9Sstevel@tonic-gate fld[5], fld[6], fld[7], fld[8], fld[9], 1597c478bd9Sstevel@tonic-gate fld[10], fld[11], fld[12], fld[13], fld[14], 160d2117003Sdp fld[15], fld[16], fld[17], fld[18], 161d2117003Sdp fld[19]); 1627c478bd9Sstevel@tonic-gate if (++iline == 3) 1637c478bd9Sstevel@tonic-gate for (i = 0; i < nfld; i++) { 1647c478bd9Sstevel@tonic-gate if (strcmp(fld[i], "CHARS") 1657c478bd9Sstevel@tonic-gate == 0) 1667c478bd9Sstevel@tonic-gate ichar = i+2; 1677c478bd9Sstevel@tonic-gate if (strcmp(fld[i], "BLOCKS") 1687c478bd9Sstevel@tonic-gate == 0) 1697c478bd9Sstevel@tonic-gate iblok = i+2; 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate if (iline > 4) { 1727c478bd9Sstevel@tonic-gate chars += atol(fld[ichar]); 1737c478bd9Sstevel@tonic-gate bloks += atol(fld[iblok]); 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate pclose(pipin); 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate if (oflg) 1807c478bd9Sstevel@tonic-gate if (iline > 4) 1817c478bd9Sstevel@tonic-gate fprintf(stderr, 182d2117003Sdp "\nCHARS TRNSFD = %ld\n" 183d2117003Sdp "BLOCKS READ = %ld\n", chars, bloks); 1847c478bd9Sstevel@tonic-gate else 1857c478bd9Sstevel@tonic-gate fprintf(stderr, 1867c478bd9Sstevel@tonic-gate "\nNo process records found!\n"); 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate if (sflg) { 190d2117003Sdp sprintf(cmd, "/usr/bin/sar -ubdycwaqvmpgrk -f %s 1>&2", fname); 1917c478bd9Sstevel@tonic-gate system(cmd); 1927c478bd9Sstevel@tonic-gate unlink(fname); 1937c478bd9Sstevel@tonic-gate } 194*9ba7e32fSdduvall exit(status>>8); 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 197*9ba7e32fSdduvall char *pad = "000 "; 198*9ba7e32fSdduvall char *sep = "\0\0.\0:\0:\0\0"; 199*9ba7e32fSdduvall char *nsep = "\0\0.\0 \0 \0\0"; 200*9ba7e32fSdduvall 201d2117003Sdp void 202*9ba7e32fSdduvall printt(char *s, time_t a) 203*9ba7e32fSdduvall { 204*9ba7e32fSdduvall int digit[11]; 205*9ba7e32fSdduvall int i; 206*9ba7e32fSdduvall char c; 207*9ba7e32fSdduvall int nonzero; 2087c478bd9Sstevel@tonic-gate 209*9ba7e32fSdduvall for (i = 0; i < 11; i++) { 210*9ba7e32fSdduvall digit[i] = a % quant[i]; 211*9ba7e32fSdduvall a /= quant[i]; 2127c478bd9Sstevel@tonic-gate } 213*9ba7e32fSdduvall fprintf(stderr, s); 214*9ba7e32fSdduvall nonzero = 0; 215*9ba7e32fSdduvall while (--i > 0) { 216*9ba7e32fSdduvall c = digit[i] != 0 ? digit[i] + '0': 217*9ba7e32fSdduvall nonzero ? '0': 218*9ba7e32fSdduvall pad[i]; 219*9ba7e32fSdduvall if (c != '\0') putc(c, stderr); 220*9ba7e32fSdduvall nonzero |= digit[i]; 221*9ba7e32fSdduvall c = nonzero?sep[i]:nsep[i]; 222*9ba7e32fSdduvall if (c != '\0') putc(c, stderr); 2237c478bd9Sstevel@tonic-gate } 224*9ba7e32fSdduvall fprintf(stderr, "%c", digit[0] * 100/HZ + '0'); 2257c478bd9Sstevel@tonic-gate fprintf(stderr, "\n"); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate /* 229d2117003Sdp * hmstime() sets current time in hh:mm:ss string format in stime; 2307c478bd9Sstevel@tonic-gate */ 2317c478bd9Sstevel@tonic-gate 232d2117003Sdp void 233d2117003Sdp hmstime(char stime[]) 2347c478bd9Sstevel@tonic-gate { 2357c478bd9Sstevel@tonic-gate char *ltime; 2367c478bd9Sstevel@tonic-gate time_t tme; 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate tme = time((time_t *)0); 2397c478bd9Sstevel@tonic-gate ltime = ctime(&tme); 2407c478bd9Sstevel@tonic-gate strncpy(stime, ltime+11, 8); 2417c478bd9Sstevel@tonic-gate stime[8] = '\0'; 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate 244d2117003Sdp void 245d2117003Sdp diag(char *s) 2467c478bd9Sstevel@tonic-gate { 2477c478bd9Sstevel@tonic-gate fprintf(stderr, "%s\n", s); 2487c478bd9Sstevel@tonic-gate unlink(fname); 2497c478bd9Sstevel@tonic-gate exit(1); 2507c478bd9Sstevel@tonic-gate } 251