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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 22*d2117003Sdp /* 23*d2117003Sdp * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*d2117003Sdp * Use is subject to license terms. 25*d2117003Sdp */ 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate 30*d2117003Sdp #pragma ident "%Z%%M% %I% %E% SMI" 31*d2117003Sdp 327c478bd9Sstevel@tonic-gate #include <sys/types.h> 337c478bd9Sstevel@tonic-gate #include <sys/times.h> 347c478bd9Sstevel@tonic-gate #include <sys/param.h> 35*d2117003Sdp #include <sys/wait.h> 36*d2117003Sdp #include <unistd.h> 377c478bd9Sstevel@tonic-gate #include <stdio.h> 38*d2117003Sdp #include <stdlib.h> 397c478bd9Sstevel@tonic-gate #include <signal.h> 40*d2117003Sdp #include <strings.h> 417c478bd9Sstevel@tonic-gate #include <time.h> 427c478bd9Sstevel@tonic-gate #include <errno.h> 437c478bd9Sstevel@tonic-gate #include <pwd.h> 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate char fname[20]; 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate /* 487c478bd9Sstevel@tonic-gate * Quant[0] will get set to HZ/10 later. 497c478bd9Sstevel@tonic-gate */ 507c478bd9Sstevel@tonic-gate char quant[] = { 10, 10, 10, 6, 10, 6, 10, 10, 10 }; 517c478bd9Sstevel@tonic-gate 52*d2117003Sdp void printt(char *, time_t); 53*d2117003Sdp void hmstime(char[]); 54*d2117003Sdp void diag(char *); 55*d2117003Sdp 56*d2117003Sdp int 57*d2117003Sdp main(int argc, char **argv) 587c478bd9Sstevel@tonic-gate { 597c478bd9Sstevel@tonic-gate struct tms buffer, obuffer; 607c478bd9Sstevel@tonic-gate int status; 617c478bd9Sstevel@tonic-gate register pid_t p; 627c478bd9Sstevel@tonic-gate int c; 637c478bd9Sstevel@tonic-gate time_t before, after; 647c478bd9Sstevel@tonic-gate char stime[9], etime[9]; 657c478bd9Sstevel@tonic-gate char cmd[80]; 667c478bd9Sstevel@tonic-gate extern char *optarg; 677c478bd9Sstevel@tonic-gate extern int optind; 687c478bd9Sstevel@tonic-gate int pflg = 0, sflg = 0, oflg = 0; 697c478bd9Sstevel@tonic-gate char aopt[25]; 70*d2117003Sdp FILE *pipin; 717c478bd9Sstevel@tonic-gate char ttyid[12], line[150]; 727c478bd9Sstevel@tonic-gate char eol; 737c478bd9Sstevel@tonic-gate char fld[20][12]; 747c478bd9Sstevel@tonic-gate int iline = 0, i, nfld; 757c478bd9Sstevel@tonic-gate int ichar, iblok; 767c478bd9Sstevel@tonic-gate long chars = 0, bloks = 0; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate /* initalize quant array using the sysconf() */ 797c478bd9Sstevel@tonic-gate quant[0] = ((int)sysconf(_SC_CLK_TCK))/10; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate aopt[0] = '\0'; /* terminate the string #1245107 */ 827c478bd9Sstevel@tonic-gate /* check options; */ 837c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "sopfhkmrt")) != EOF) 847c478bd9Sstevel@tonic-gate switch (c) { 857c478bd9Sstevel@tonic-gate case 's': sflg++; break; 867c478bd9Sstevel@tonic-gate case 'o': oflg++; break; 877c478bd9Sstevel@tonic-gate case 'p': pflg++; break; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate case 'f': strcat(aopt, "-f "); break; 907c478bd9Sstevel@tonic-gate case 'h': strcat(aopt, "-h "); break; 917c478bd9Sstevel@tonic-gate case 'k': strcat(aopt, "-k "); break; 927c478bd9Sstevel@tonic-gate case 'm': strcat(aopt, "-m "); break; 937c478bd9Sstevel@tonic-gate case 'r': strcat(aopt, "-r "); break; 947c478bd9Sstevel@tonic-gate case 't': strcat(aopt, "-t "); break; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate case '?': diag("Usage: timex [-s][-o][-p[-fhkmrt]] cmd"); 977c478bd9Sstevel@tonic-gate break; 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate if (optind >= argc) diag("Missing command"); 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * Check to see if accounting is installed and print a somewhat 1037c478bd9Sstevel@tonic-gate * meaninful message if not. 1047c478bd9Sstevel@tonic-gate */ 1057c478bd9Sstevel@tonic-gate if (((oflg+pflg) != 0) && (access("/usr/bin/acctcom", 01) == -1)) { 1067c478bd9Sstevel@tonic-gate oflg = 0; 1077c478bd9Sstevel@tonic-gate pflg = 0; 1087c478bd9Sstevel@tonic-gate fprintf(stderr, 1097c478bd9Sstevel@tonic-gate "Information from -p and -o options not available\n"); 1107c478bd9Sstevel@tonic-gate fprintf(stderr, 1117c478bd9Sstevel@tonic-gate " because process accounting is not operational.\n"); 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate if (sflg) { 1157c478bd9Sstevel@tonic-gate sprintf(fname, "/tmp/tmx%ld", getpid()); 1167c478bd9Sstevel@tonic-gate sprintf(cmd, "/usr/lib/sa/sadc 1 1 %s", fname); 1177c478bd9Sstevel@tonic-gate system(cmd); 1187c478bd9Sstevel@tonic-gate } 1197c478bd9Sstevel@tonic-gate if (pflg + oflg) hmstime(stime); 1207c478bd9Sstevel@tonic-gate before = times(&obuffer); 1217c478bd9Sstevel@tonic-gate if ((p = fork()) == (pid_t)-1) diag("Try again.\n"); 1227c478bd9Sstevel@tonic-gate if (p == 0) { 1237c478bd9Sstevel@tonic-gate setgid(getgid()); 1247c478bd9Sstevel@tonic-gate execvp(*(argv+optind), (argv+optind)); 125*d2117003Sdp fprintf(stderr, "%s: %s\n", *(argv+optind), strerror(errno)); 1267c478bd9Sstevel@tonic-gate exit(1); 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate signal(SIGINT, SIG_IGN); 1297c478bd9Sstevel@tonic-gate signal(SIGQUIT, SIG_IGN); 130*d2117003Sdp while (wait(&status) != p) 131*d2117003Sdp ; 1327c478bd9Sstevel@tonic-gate if ((status&0377) != 0) 1337c478bd9Sstevel@tonic-gate fprintf(stderr, "Command terminated abnormally.\n"); 1347c478bd9Sstevel@tonic-gate signal(SIGINT, SIG_DFL); 1357c478bd9Sstevel@tonic-gate signal(SIGQUIT, SIG_DFL); 1367c478bd9Sstevel@tonic-gate after = times(&buffer); 1377c478bd9Sstevel@tonic-gate if (pflg + oflg) hmstime(etime); 1387c478bd9Sstevel@tonic-gate if (sflg) system(cmd); 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate fprintf(stderr, "\n"); 1417c478bd9Sstevel@tonic-gate printt("real", (after-before)); 1427c478bd9Sstevel@tonic-gate printt("user", buffer.tms_cutime - obuffer.tms_cutime); 1437c478bd9Sstevel@tonic-gate printt("sys ", buffer.tms_cstime - obuffer.tms_cstime); 1447c478bd9Sstevel@tonic-gate fprintf(stderr, "\n"); 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate if (oflg+pflg) { 1477c478bd9Sstevel@tonic-gate if (isatty(0)) 1487c478bd9Sstevel@tonic-gate sprintf(ttyid, "-l %s", ttyname(0)+5); 1497c478bd9Sstevel@tonic-gate sprintf(cmd, "acctcom -S %s -E %s -u %s %s -i %s", 1507c478bd9Sstevel@tonic-gate stime, etime, getpwuid(getuid())->pw_name, ttyid, aopt); 1517c478bd9Sstevel@tonic-gate pipin = popen(cmd, "r"); 1527c478bd9Sstevel@tonic-gate while (fscanf(pipin, "%[^\n]%1c", line, &eol) > 1) { 1537c478bd9Sstevel@tonic-gate if (pflg) 1547c478bd9Sstevel@tonic-gate fprintf(stderr, "%s\n", line); 1557c478bd9Sstevel@tonic-gate if (oflg) { 1567c478bd9Sstevel@tonic-gate nfld = sscanf(line, 1577c478bd9Sstevel@tonic-gate "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", 1587c478bd9Sstevel@tonic-gate fld[0], fld[1], fld[2], fld[3], fld[4], 1597c478bd9Sstevel@tonic-gate fld[5], fld[6], fld[7], fld[8], fld[9], 1607c478bd9Sstevel@tonic-gate fld[10], fld[11], fld[12], fld[13], fld[14], 161*d2117003Sdp fld[15], fld[16], fld[17], fld[18], 162*d2117003Sdp fld[19]); 1637c478bd9Sstevel@tonic-gate if (++iline == 3) 1647c478bd9Sstevel@tonic-gate for (i = 0; i < nfld; i++) { 1657c478bd9Sstevel@tonic-gate if (strcmp(fld[i], "CHARS") 1667c478bd9Sstevel@tonic-gate == 0) 1677c478bd9Sstevel@tonic-gate ichar = i+2; 1687c478bd9Sstevel@tonic-gate if (strcmp(fld[i], "BLOCKS") 1697c478bd9Sstevel@tonic-gate == 0) 1707c478bd9Sstevel@tonic-gate iblok = i+2; 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate if (iline > 4) { 1737c478bd9Sstevel@tonic-gate chars += atol(fld[ichar]); 1747c478bd9Sstevel@tonic-gate bloks += atol(fld[iblok]); 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate pclose(pipin); 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate if (oflg) 1817c478bd9Sstevel@tonic-gate if (iline > 4) 1827c478bd9Sstevel@tonic-gate fprintf(stderr, 183*d2117003Sdp "\nCHARS TRNSFD = %ld\n" 184*d2117003Sdp "BLOCKS READ = %ld\n", chars, bloks); 1857c478bd9Sstevel@tonic-gate else 1867c478bd9Sstevel@tonic-gate fprintf(stderr, 1877c478bd9Sstevel@tonic-gate "\nNo process records found!\n"); 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate if (sflg) { 191*d2117003Sdp sprintf(cmd, "/usr/bin/sar -ubdycwaqvmpgrk -f %s 1>&2", fname); 1927c478bd9Sstevel@tonic-gate system(cmd); 1937c478bd9Sstevel@tonic-gate unlink(fname); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate exit(status>>8); 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate char *pad = "000 "; 1997c478bd9Sstevel@tonic-gate char *sep = "\0\0.\0:\0:\0\0"; 2007c478bd9Sstevel@tonic-gate char *nsep = "\0\0.\0 \0 \0\0"; 2017c478bd9Sstevel@tonic-gate 202*d2117003Sdp void 203*d2117003Sdp printt(char *s, time_t a) 2047c478bd9Sstevel@tonic-gate { 2057c478bd9Sstevel@tonic-gate char digit[9]; 206*d2117003Sdp int i; 2077c478bd9Sstevel@tonic-gate char c; 2087c478bd9Sstevel@tonic-gate int nonzero; 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate for (i = 0; i < 9; i++) { 2117c478bd9Sstevel@tonic-gate digit[i] = a % quant[i]; 2127c478bd9Sstevel@tonic-gate a /= quant[i]; 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate fprintf(stderr, s); 2157c478bd9Sstevel@tonic-gate nonzero = 0; 2167c478bd9Sstevel@tonic-gate while (--i > 0) { 2177c478bd9Sstevel@tonic-gate c = digit[i] != 0 ? digit[i] + '0': 2187c478bd9Sstevel@tonic-gate nonzero ? '0': 2197c478bd9Sstevel@tonic-gate pad[i]; 2207c478bd9Sstevel@tonic-gate if (c != '\0') putc(c, stderr); 2217c478bd9Sstevel@tonic-gate nonzero |= digit[i]; 2227c478bd9Sstevel@tonic-gate c = nonzero?sep[i]:nsep[i]; 2237c478bd9Sstevel@tonic-gate if (c != '\0') putc(c, stderr); 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate fprintf(stderr, "%c", digit[0] * 100/HZ + '0'); 2267c478bd9Sstevel@tonic-gate fprintf(stderr, "\n"); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate /* 230*d2117003Sdp * hmstime() sets current time in hh:mm:ss string format in stime; 2317c478bd9Sstevel@tonic-gate */ 2327c478bd9Sstevel@tonic-gate 233*d2117003Sdp void 234*d2117003Sdp hmstime(char stime[]) 2357c478bd9Sstevel@tonic-gate { 2367c478bd9Sstevel@tonic-gate char *ltime; 2377c478bd9Sstevel@tonic-gate time_t tme; 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate tme = time((time_t *)0); 2407c478bd9Sstevel@tonic-gate ltime = ctime(&tme); 2417c478bd9Sstevel@tonic-gate strncpy(stime, ltime+11, 8); 2427c478bd9Sstevel@tonic-gate stime[8] = '\0'; 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate 245*d2117003Sdp void 246*d2117003Sdp diag(char *s) 2477c478bd9Sstevel@tonic-gate { 2487c478bd9Sstevel@tonic-gate fprintf(stderr, "%s\n", s); 2497c478bd9Sstevel@tonic-gate unlink(fname); 2507c478bd9Sstevel@tonic-gate exit(1); 2517c478bd9Sstevel@tonic-gate } 252