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 */ 227c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 237c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 27*414388d7Ssl108498 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 287c478bd9Sstevel@tonic-gate * Use is subject to license terms. 297c478bd9Sstevel@tonic-gate */ 30*414388d7Ssl108498 #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate /* 337c478bd9Sstevel@tonic-gate * acctprc1 [ctmpfile] 347c478bd9Sstevel@tonic-gate * reads std. input (acct.h format), adds login names 357c478bd9Sstevel@tonic-gate * writes std. output (ptmp.h/ascii format) 367c478bd9Sstevel@tonic-gate * if ctmpfile is given, it is expected have ctmp.h/ascii data, 377c478bd9Sstevel@tonic-gate * sorted by uid/name; it is used to make better guesses at login names 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #include <sys/types.h> 417c478bd9Sstevel@tonic-gate #include <sys/param.h> 427c478bd9Sstevel@tonic-gate #include "acctdef.h" 437c478bd9Sstevel@tonic-gate #include <stdio.h> 447c478bd9Sstevel@tonic-gate #include <errno.h> 457c478bd9Sstevel@tonic-gate #include <sys/acct.h> 46*414388d7Ssl108498 #include <stdlib.h> 47*414388d7Ssl108498 487c478bd9Sstevel@tonic-gate #define MYKIND(flag) ((flag & ACCTF) == 0) 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate struct acct ab; 517c478bd9Sstevel@tonic-gate struct ctmp cb; 527c478bd9Sstevel@tonic-gate struct ptmp pb; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate int a_usize = A_USIZE; 557c478bd9Sstevel@tonic-gate struct urec { /* 1 for each distinct uid/name */ 567c478bd9Sstevel@tonic-gate uid_t ur_uid; /* sorted by uid/name */ 577c478bd9Sstevel@tonic-gate char ur_name[NSZ]; 587c478bd9Sstevel@tonic-gate struct srec *ur_srec; /* ptr to first session */ 597c478bd9Sstevel@tonic-gate short ur_cnt; /* # sessions */ 607c478bd9Sstevel@tonic-gate } * ur; 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate struct urec *urlast; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate int a_ssize = A_SSIZE; 657c478bd9Sstevel@tonic-gate int ssize; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate struct srec { /* 1 for each distinct session */ 687c478bd9Sstevel@tonic-gate dev_t sr_tty; /* dev, used to connect with process*/ 697c478bd9Sstevel@tonic-gate time_t sr_start; /* start time of session */ 707c478bd9Sstevel@tonic-gate time_t sr_end; /* end time of session */ 717c478bd9Sstevel@tonic-gate } * sr; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate char *getname(uid_t, dev_t, time_t); 747c478bd9Sstevel@tonic-gate void readctmp(char *); 757c478bd9Sstevel@tonic-gate char *getnamc(uid_t, dev_t, time_t); 767c478bd9Sstevel@tonic-gate int aread(int); 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate char *uidtonam(); 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate int 817c478bd9Sstevel@tonic-gate main(int argc, char **argv) 827c478bd9Sstevel@tonic-gate { 837c478bd9Sstevel@tonic-gate long elaps[2]; 847c478bd9Sstevel@tonic-gate ulong_t etime, stime; 857c478bd9Sstevel@tonic-gate unsigned long mem; 867c478bd9Sstevel@tonic-gate ulong_t expand(); 877c478bd9Sstevel@tonic-gate int ver; /* version of acct struct */ 887c478bd9Sstevel@tonic-gate int aread(); 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate if ((ur = (struct urec *) calloc(a_usize, 917c478bd9Sstevel@tonic-gate sizeof (struct urec))) == NULL) { 927c478bd9Sstevel@tonic-gate fprintf(stderr, "acctpr1: Cannot allocate memory\n"); 937c478bd9Sstevel@tonic-gate exit(3); 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate urlast = ur; 977c478bd9Sstevel@tonic-gate if ((sr = (struct srec *) calloc(a_ssize, 987c478bd9Sstevel@tonic-gate sizeof (struct srec))) == NULL) { 997c478bd9Sstevel@tonic-gate fprintf(stderr, "acctpr1: Cannot allocate memory\n"); 1007c478bd9Sstevel@tonic-gate exit(3); 1017c478bd9Sstevel@tonic-gate } 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate while (--argc > 0) { 1047c478bd9Sstevel@tonic-gate if (**++argv == '-') 1057c478bd9Sstevel@tonic-gate switch(*++*argv) { 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate else { 1087c478bd9Sstevel@tonic-gate readctmp(*argv); 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate if (fread((char *)&ab, sizeof(struct acct), 1, stdin) != 1) 114*414388d7Ssl108498 exit(1); 1157c478bd9Sstevel@tonic-gate else if (ab.ac_flag & AEXPND) 1167c478bd9Sstevel@tonic-gate ver = 2; /* 4.0 acct structure */ 1177c478bd9Sstevel@tonic-gate else 1187c478bd9Sstevel@tonic-gate ver = 1; /* 3.x acct structure */ 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate rewind(stdin); 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate while (aread(ver) == 1) { 1237c478bd9Sstevel@tonic-gate if (!MYKIND(ab.ac_flag)) 1247c478bd9Sstevel@tonic-gate continue; 1257c478bd9Sstevel@tonic-gate pb.pt_uid = ab.ac_uid; 1267c478bd9Sstevel@tonic-gate CPYN(pb.pt_name, getname(ab.ac_uid, ab.ac_tty, ab.ac_btime)); 1277c478bd9Sstevel@tonic-gate /* 1287c478bd9Sstevel@tonic-gate * approximate cpu P/NP split as same as elapsed time 1297c478bd9Sstevel@tonic-gate */ 1307c478bd9Sstevel@tonic-gate if ((etime = SECS(expand(ab.ac_etime))) == 0) 1317c478bd9Sstevel@tonic-gate etime = 1; 1327c478bd9Sstevel@tonic-gate stime = expand(ab.ac_stime) + expand(ab.ac_utime); 1337c478bd9Sstevel@tonic-gate mem = expand(ab.ac_mem); 1347c478bd9Sstevel@tonic-gate if(pnpsplit(ab.ac_btime, etime, elaps) == 0) { 1357c478bd9Sstevel@tonic-gate fprintf(stderr, "acctprc1: could not calculate prime/non-prime hours\n"); 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate exit(1); 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate pb.pt_cpu[0] = (double)stime * (double)elaps[0] / etime; 1407c478bd9Sstevel@tonic-gate pb.pt_cpu[1] = (stime > pb.pt_cpu[0])? stime - pb.pt_cpu[0] : 0; 1417c478bd9Sstevel@tonic-gate pb.pt_cpu[1] = stime - pb.pt_cpu[0]; 1427c478bd9Sstevel@tonic-gate if (stime) 1437c478bd9Sstevel@tonic-gate pb.pt_mem = (mem + stime - 1) / stime; 1447c478bd9Sstevel@tonic-gate else 1457c478bd9Sstevel@tonic-gate pb.pt_mem = 0; /* unlikely */ 1467c478bd9Sstevel@tonic-gate printf("%ld\t%.*s\t%lu\t%lu\t%u\n", 1477c478bd9Sstevel@tonic-gate pb.pt_uid, 1487c478bd9Sstevel@tonic-gate OUTPUT_NSZ, 1497c478bd9Sstevel@tonic-gate pb.pt_name, 1507c478bd9Sstevel@tonic-gate pb.pt_cpu[0], pb.pt_cpu[1], 1517c478bd9Sstevel@tonic-gate pb.pt_mem); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate exit(0); 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate /* 1587c478bd9Sstevel@tonic-gate * return ptr to name corresponding to uid 1597c478bd9Sstevel@tonic-gate * try ctmp first, then use uidtonam (internal list or passwd file) 1607c478bd9Sstevel@tonic-gate */ 1617c478bd9Sstevel@tonic-gate char * 1627c478bd9Sstevel@tonic-gate getname(uid_t uid, dev_t tty, time_t start) 1637c478bd9Sstevel@tonic-gate { 1647c478bd9Sstevel@tonic-gate char *p; 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate if ((p = getnamc(uid, tty, start)) != NULL) 1677c478bd9Sstevel@tonic-gate return (p); 1687c478bd9Sstevel@tonic-gate return (uidtonam(uid)); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate /* 1727c478bd9Sstevel@tonic-gate * read ctmp file, build up urec-srec data structures for 1737c478bd9Sstevel@tonic-gate * later use by getnamc 1747c478bd9Sstevel@tonic-gate */ 1757c478bd9Sstevel@tonic-gate void 1767c478bd9Sstevel@tonic-gate readctmp(char *fname) 1777c478bd9Sstevel@tonic-gate { 1787c478bd9Sstevel@tonic-gate FILE *fp; 1797c478bd9Sstevel@tonic-gate struct urec *up; 1807c478bd9Sstevel@tonic-gate struct srec *sp; 1817c478bd9Sstevel@tonic-gate int i = 0, j = 0, k=0; 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate if ((fp = fopen(fname, "r")) == NULL) { 1847c478bd9Sstevel@tonic-gate fprintf(stderr, "acctprc1: can't open %s\n", fname); 1857c478bd9Sstevel@tonic-gate return; 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate up = NULL; 1897c478bd9Sstevel@tonic-gate sp = sr; 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate while (fscanf(fp, "%hd\t%ld\t%s\t%lu\t%lu\t%lu\t%*[^\n]", 1927c478bd9Sstevel@tonic-gate &cb.ct_tty, 1937c478bd9Sstevel@tonic-gate &cb.ct_uid, 1947c478bd9Sstevel@tonic-gate cb.ct_name, 1957c478bd9Sstevel@tonic-gate &cb.ct_con[0], 1967c478bd9Sstevel@tonic-gate &cb.ct_con[1], 1977c478bd9Sstevel@tonic-gate &cb.ct_start) != EOF) { 1987c478bd9Sstevel@tonic-gate if (up == NULL || cb.ct_uid != up->ur_uid || 1997c478bd9Sstevel@tonic-gate !EQN(cb.ct_name, up->ur_name)) { 2007c478bd9Sstevel@tonic-gate if (up == NULL) 2017c478bd9Sstevel@tonic-gate up = ur; 2027c478bd9Sstevel@tonic-gate if (++up >= &ur[a_usize]) { 2037c478bd9Sstevel@tonic-gate a_usize = a_usize + A_USIZE; 2047c478bd9Sstevel@tonic-gate if ((ur = (struct urec *) realloc(ur, a_usize * 2057c478bd9Sstevel@tonic-gate sizeof (struct urec))) == NULL) { 2067c478bd9Sstevel@tonic-gate fprintf(stderr, "acctprc1: 1 Cannot reallocate memory\n"); 2077c478bd9Sstevel@tonic-gate exit(2); 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate up = &ur[a_usize - A_USIZE]; 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate up->ur_uid = cb.ct_uid; 2127c478bd9Sstevel@tonic-gate CPYN(up->ur_name, cb.ct_name); 2137c478bd9Sstevel@tonic-gate up->ur_srec = sp; 2147c478bd9Sstevel@tonic-gate up->ur_cnt = 0; 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate if (sp >= &sr[a_ssize-1]) { 2187c478bd9Sstevel@tonic-gate a_ssize = a_ssize + A_SSIZE; 2197c478bd9Sstevel@tonic-gate if ((sr = (struct srec *) realloc(sr, a_ssize * 2207c478bd9Sstevel@tonic-gate sizeof (struct srec))) == NULL) { 2217c478bd9Sstevel@tonic-gate fprintf(stderr, "acctprc1: 2 Cannot reallocate memory\n"); 2227c478bd9Sstevel@tonic-gate printf("errno=%d\n", errno); 2237c478bd9Sstevel@tonic-gate exit(2); 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate sp = &sr[a_ssize - A_SSIZE]; 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate sp->sr_tty = cb.ct_tty; 2297c478bd9Sstevel@tonic-gate sp->sr_start = cb.ct_start; 2307c478bd9Sstevel@tonic-gate sp->sr_end = cb.ct_start + cb.ct_con[0] + cb.ct_con[1]; 2317c478bd9Sstevel@tonic-gate sp++; 2327c478bd9Sstevel@tonic-gate up->ur_cnt++; 2337c478bd9Sstevel@tonic-gate } 2347c478bd9Sstevel@tonic-gate if (up != NULL) 2357c478bd9Sstevel@tonic-gate urlast = ++up; 2367c478bd9Sstevel@tonic-gate fclose(fp); 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate /* 2407c478bd9Sstevel@tonic-gate * using urec-srec data (if any), make best guess at login name 2417c478bd9Sstevel@tonic-gate * corresponding to uid, return ptr to the name. 2427c478bd9Sstevel@tonic-gate * must match on tty; use start time to help guess 2437c478bd9Sstevel@tonic-gate * for any urec having same uid as uid, search array of associated 2447c478bd9Sstevel@tonic-gate * srecs for those having same tty 2457c478bd9Sstevel@tonic-gate * if start time of process is within range of session, that's it 2467c478bd9Sstevel@tonic-gate * if none can be found within range, give it to person of same uid 2477c478bd9Sstevel@tonic-gate * who last logged off on that terminal 2487c478bd9Sstevel@tonic-gate */ 2497c478bd9Sstevel@tonic-gate char * 2507c478bd9Sstevel@tonic-gate getnamc(uid_t uid, dev_t tty, time_t start) 2517c478bd9Sstevel@tonic-gate { 2527c478bd9Sstevel@tonic-gate struct urec *up; 2537c478bd9Sstevel@tonic-gate struct srec *sp; 2547c478bd9Sstevel@tonic-gate struct srec *splast; 2557c478bd9Sstevel@tonic-gate long latest; 2567c478bd9Sstevel@tonic-gate char *guess; 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate latest = 0; 2597c478bd9Sstevel@tonic-gate guess = NULL; 2607c478bd9Sstevel@tonic-gate for (up = ur; up < urlast && uid >= up->ur_uid; up++) 2617c478bd9Sstevel@tonic-gate if (uid == up->ur_uid) { 2627c478bd9Sstevel@tonic-gate sp = up->ur_srec; 2637c478bd9Sstevel@tonic-gate splast = sp+up->ur_cnt; 2647c478bd9Sstevel@tonic-gate for (; sp < splast; sp++) 2657c478bd9Sstevel@tonic-gate if (tty == sp->sr_tty) { 2667c478bd9Sstevel@tonic-gate if (start >= sp->sr_start && 2677c478bd9Sstevel@tonic-gate start <= sp->sr_end) 2687c478bd9Sstevel@tonic-gate return(up->ur_name); 2697c478bd9Sstevel@tonic-gate if (start >= sp->sr_start && 2707c478bd9Sstevel@tonic-gate sp->sr_end > latest) { 2717c478bd9Sstevel@tonic-gate latest = sp->sr_end; 2727c478bd9Sstevel@tonic-gate guess = up->ur_name; 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate return(guess); 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate int 2807c478bd9Sstevel@tonic-gate aread(int ver) 2817c478bd9Sstevel@tonic-gate { 2827c478bd9Sstevel@tonic-gate struct o_acct oab; 2837c478bd9Sstevel@tonic-gate int ret; 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate if (ver != 2) { 2867c478bd9Sstevel@tonic-gate if ((ret = fread((char *)&oab, sizeof(struct o_acct), 1, stdin)) == 1){ 2877c478bd9Sstevel@tonic-gate /* copy SVR3 acct struct to SVR4 acct struct */ 2887c478bd9Sstevel@tonic-gate ab.ac_flag = oab.ac_flag | AEXPND; 2897c478bd9Sstevel@tonic-gate ab.ac_stat = oab.ac_stat; 2907c478bd9Sstevel@tonic-gate ab.ac_uid = (uid_t) oab.ac_uid; 2917c478bd9Sstevel@tonic-gate ab.ac_gid = (gid_t) oab.ac_gid; 2927c478bd9Sstevel@tonic-gate ab.ac_tty = (dev_t) oab.ac_tty; 2937c478bd9Sstevel@tonic-gate ab.ac_btime = oab.ac_btime; 2947c478bd9Sstevel@tonic-gate ab.ac_utime = oab.ac_utime; 2957c478bd9Sstevel@tonic-gate ab.ac_stime = oab.ac_stime; 2967c478bd9Sstevel@tonic-gate ab.ac_mem = oab.ac_mem; 2977c478bd9Sstevel@tonic-gate ab.ac_io = oab.ac_io; 2987c478bd9Sstevel@tonic-gate ab.ac_rw = oab.ac_rw; 2997c478bd9Sstevel@tonic-gate strcpy(ab.ac_comm, oab.ac_comm); 3007c478bd9Sstevel@tonic-gate } 3017c478bd9Sstevel@tonic-gate } else 3027c478bd9Sstevel@tonic-gate ret = fread((char *)&ab, sizeof(struct acct), 1, stdin); 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate return(ret != 1 ? 0 : 1); 3067c478bd9Sstevel@tonic-gate } 307