1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate /* 27*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 28*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 29*7c478bd9Sstevel@tonic-gate */ 30*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.11 */ 31*7c478bd9Sstevel@tonic-gate 32*7c478bd9Sstevel@tonic-gate /* 33*7c478bd9Sstevel@tonic-gate * acctprc1 [ctmpfile] 34*7c478bd9Sstevel@tonic-gate * reads std. input (acct.h format), adds login names 35*7c478bd9Sstevel@tonic-gate * writes std. output (ptmp.h/ascii format) 36*7c478bd9Sstevel@tonic-gate * if ctmpfile is given, it is expected have ctmp.h/ascii data, 37*7c478bd9Sstevel@tonic-gate * sorted by uid/name; it is used to make better guesses at login names 38*7c478bd9Sstevel@tonic-gate */ 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 42*7c478bd9Sstevel@tonic-gate #include "acctdef.h" 43*7c478bd9Sstevel@tonic-gate #include <stdio.h> 44*7c478bd9Sstevel@tonic-gate #include <errno.h> 45*7c478bd9Sstevel@tonic-gate #include <sys/acct.h> 46*7c478bd9Sstevel@tonic-gate #define MYKIND(flag) ((flag & ACCTF) == 0) 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate struct acct ab; 49*7c478bd9Sstevel@tonic-gate struct ctmp cb; 50*7c478bd9Sstevel@tonic-gate struct ptmp pb; 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate int a_usize = A_USIZE; 53*7c478bd9Sstevel@tonic-gate struct urec { /* 1 for each distinct uid/name */ 54*7c478bd9Sstevel@tonic-gate uid_t ur_uid; /* sorted by uid/name */ 55*7c478bd9Sstevel@tonic-gate char ur_name[NSZ]; 56*7c478bd9Sstevel@tonic-gate struct srec *ur_srec; /* ptr to first session */ 57*7c478bd9Sstevel@tonic-gate short ur_cnt; /* # sessions */ 58*7c478bd9Sstevel@tonic-gate } * ur; 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate struct urec *urlast; 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate int a_ssize = A_SSIZE; 63*7c478bd9Sstevel@tonic-gate int ssize; 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate struct srec { /* 1 for each distinct session */ 66*7c478bd9Sstevel@tonic-gate dev_t sr_tty; /* dev, used to connect with process*/ 67*7c478bd9Sstevel@tonic-gate time_t sr_start; /* start time of session */ 68*7c478bd9Sstevel@tonic-gate time_t sr_end; /* end time of session */ 69*7c478bd9Sstevel@tonic-gate } * sr; 70*7c478bd9Sstevel@tonic-gate 71*7c478bd9Sstevel@tonic-gate char *getname(uid_t, dev_t, time_t); 72*7c478bd9Sstevel@tonic-gate void readctmp(char *); 73*7c478bd9Sstevel@tonic-gate char *getnamc(uid_t, dev_t, time_t); 74*7c478bd9Sstevel@tonic-gate int aread(int); 75*7c478bd9Sstevel@tonic-gate 76*7c478bd9Sstevel@tonic-gate char *uidtonam(); 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate int 79*7c478bd9Sstevel@tonic-gate main(int argc, char **argv) 80*7c478bd9Sstevel@tonic-gate { 81*7c478bd9Sstevel@tonic-gate long elaps[2]; 82*7c478bd9Sstevel@tonic-gate ulong_t etime, stime; 83*7c478bd9Sstevel@tonic-gate unsigned long mem; 84*7c478bd9Sstevel@tonic-gate ulong_t expand(); 85*7c478bd9Sstevel@tonic-gate int ver; /* version of acct struct */ 86*7c478bd9Sstevel@tonic-gate int aread(); 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate if ((ur = (struct urec *) calloc(a_usize, 89*7c478bd9Sstevel@tonic-gate sizeof (struct urec))) == NULL) { 90*7c478bd9Sstevel@tonic-gate fprintf(stderr, "acctpr1: Cannot allocate memory\n"); 91*7c478bd9Sstevel@tonic-gate exit(3); 92*7c478bd9Sstevel@tonic-gate } 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate urlast = ur; 95*7c478bd9Sstevel@tonic-gate if ((sr = (struct srec *) calloc(a_ssize, 96*7c478bd9Sstevel@tonic-gate sizeof (struct srec))) == NULL) { 97*7c478bd9Sstevel@tonic-gate fprintf(stderr, "acctpr1: Cannot allocate memory\n"); 98*7c478bd9Sstevel@tonic-gate exit(3); 99*7c478bd9Sstevel@tonic-gate } 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate while (--argc > 0) { 102*7c478bd9Sstevel@tonic-gate if (**++argv == '-') 103*7c478bd9Sstevel@tonic-gate switch(*++*argv) { 104*7c478bd9Sstevel@tonic-gate } 105*7c478bd9Sstevel@tonic-gate else { 106*7c478bd9Sstevel@tonic-gate readctmp(*argv); 107*7c478bd9Sstevel@tonic-gate } 108*7c478bd9Sstevel@tonic-gate } 109*7c478bd9Sstevel@tonic-gate 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate if (fread((char *)&ab, sizeof(struct acct), 1, stdin) != 1) 112*7c478bd9Sstevel@tonic-gate return; 113*7c478bd9Sstevel@tonic-gate else if (ab.ac_flag & AEXPND) 114*7c478bd9Sstevel@tonic-gate ver = 2; /* 4.0 acct structure */ 115*7c478bd9Sstevel@tonic-gate else 116*7c478bd9Sstevel@tonic-gate ver = 1; /* 3.x acct structure */ 117*7c478bd9Sstevel@tonic-gate 118*7c478bd9Sstevel@tonic-gate rewind(stdin); 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate while (aread(ver) == 1) { 121*7c478bd9Sstevel@tonic-gate if (!MYKIND(ab.ac_flag)) 122*7c478bd9Sstevel@tonic-gate continue; 123*7c478bd9Sstevel@tonic-gate pb.pt_uid = ab.ac_uid; 124*7c478bd9Sstevel@tonic-gate CPYN(pb.pt_name, getname(ab.ac_uid, ab.ac_tty, ab.ac_btime)); 125*7c478bd9Sstevel@tonic-gate /* 126*7c478bd9Sstevel@tonic-gate * approximate cpu P/NP split as same as elapsed time 127*7c478bd9Sstevel@tonic-gate */ 128*7c478bd9Sstevel@tonic-gate if ((etime = SECS(expand(ab.ac_etime))) == 0) 129*7c478bd9Sstevel@tonic-gate etime = 1; 130*7c478bd9Sstevel@tonic-gate stime = expand(ab.ac_stime) + expand(ab.ac_utime); 131*7c478bd9Sstevel@tonic-gate mem = expand(ab.ac_mem); 132*7c478bd9Sstevel@tonic-gate if(pnpsplit(ab.ac_btime, etime, elaps) == 0) { 133*7c478bd9Sstevel@tonic-gate fprintf(stderr, "acctprc1: could not calculate prime/non-prime hours\n"); 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate exit(1); 136*7c478bd9Sstevel@tonic-gate } 137*7c478bd9Sstevel@tonic-gate pb.pt_cpu[0] = (double)stime * (double)elaps[0] / etime; 138*7c478bd9Sstevel@tonic-gate pb.pt_cpu[1] = (stime > pb.pt_cpu[0])? stime - pb.pt_cpu[0] : 0; 139*7c478bd9Sstevel@tonic-gate pb.pt_cpu[1] = stime - pb.pt_cpu[0]; 140*7c478bd9Sstevel@tonic-gate if (stime) 141*7c478bd9Sstevel@tonic-gate pb.pt_mem = (mem + stime - 1) / stime; 142*7c478bd9Sstevel@tonic-gate else 143*7c478bd9Sstevel@tonic-gate pb.pt_mem = 0; /* unlikely */ 144*7c478bd9Sstevel@tonic-gate printf("%ld\t%.*s\t%lu\t%lu\t%u\n", 145*7c478bd9Sstevel@tonic-gate pb.pt_uid, 146*7c478bd9Sstevel@tonic-gate OUTPUT_NSZ, 147*7c478bd9Sstevel@tonic-gate pb.pt_name, 148*7c478bd9Sstevel@tonic-gate pb.pt_cpu[0], pb.pt_cpu[1], 149*7c478bd9Sstevel@tonic-gate pb.pt_mem); 150*7c478bd9Sstevel@tonic-gate } 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate exit(0); 153*7c478bd9Sstevel@tonic-gate } 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate /* 156*7c478bd9Sstevel@tonic-gate * return ptr to name corresponding to uid 157*7c478bd9Sstevel@tonic-gate * try ctmp first, then use uidtonam (internal list or passwd file) 158*7c478bd9Sstevel@tonic-gate */ 159*7c478bd9Sstevel@tonic-gate char * 160*7c478bd9Sstevel@tonic-gate getname(uid_t uid, dev_t tty, time_t start) 161*7c478bd9Sstevel@tonic-gate { 162*7c478bd9Sstevel@tonic-gate char *p; 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate if ((p = getnamc(uid, tty, start)) != NULL) 165*7c478bd9Sstevel@tonic-gate return (p); 166*7c478bd9Sstevel@tonic-gate return (uidtonam(uid)); 167*7c478bd9Sstevel@tonic-gate } 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate /* 170*7c478bd9Sstevel@tonic-gate * read ctmp file, build up urec-srec data structures for 171*7c478bd9Sstevel@tonic-gate * later use by getnamc 172*7c478bd9Sstevel@tonic-gate */ 173*7c478bd9Sstevel@tonic-gate void 174*7c478bd9Sstevel@tonic-gate readctmp(char *fname) 175*7c478bd9Sstevel@tonic-gate { 176*7c478bd9Sstevel@tonic-gate FILE *fp; 177*7c478bd9Sstevel@tonic-gate struct urec *up; 178*7c478bd9Sstevel@tonic-gate struct srec *sp; 179*7c478bd9Sstevel@tonic-gate int i = 0, j = 0, k=0; 180*7c478bd9Sstevel@tonic-gate 181*7c478bd9Sstevel@tonic-gate if ((fp = fopen(fname, "r")) == NULL) { 182*7c478bd9Sstevel@tonic-gate fprintf(stderr, "acctprc1: can't open %s\n", fname); 183*7c478bd9Sstevel@tonic-gate return; 184*7c478bd9Sstevel@tonic-gate } 185*7c478bd9Sstevel@tonic-gate 186*7c478bd9Sstevel@tonic-gate up = NULL; 187*7c478bd9Sstevel@tonic-gate sp = sr; 188*7c478bd9Sstevel@tonic-gate 189*7c478bd9Sstevel@tonic-gate while (fscanf(fp, "%hd\t%ld\t%s\t%lu\t%lu\t%lu\t%*[^\n]", 190*7c478bd9Sstevel@tonic-gate &cb.ct_tty, 191*7c478bd9Sstevel@tonic-gate &cb.ct_uid, 192*7c478bd9Sstevel@tonic-gate cb.ct_name, 193*7c478bd9Sstevel@tonic-gate &cb.ct_con[0], 194*7c478bd9Sstevel@tonic-gate &cb.ct_con[1], 195*7c478bd9Sstevel@tonic-gate &cb.ct_start) != EOF) { 196*7c478bd9Sstevel@tonic-gate if (up == NULL || cb.ct_uid != up->ur_uid || 197*7c478bd9Sstevel@tonic-gate !EQN(cb.ct_name, up->ur_name)) { 198*7c478bd9Sstevel@tonic-gate if (up == NULL) 199*7c478bd9Sstevel@tonic-gate up = ur; 200*7c478bd9Sstevel@tonic-gate if (++up >= &ur[a_usize]) { 201*7c478bd9Sstevel@tonic-gate a_usize = a_usize + A_USIZE; 202*7c478bd9Sstevel@tonic-gate if ((ur = (struct urec *) realloc(ur, a_usize * 203*7c478bd9Sstevel@tonic-gate sizeof (struct urec))) == NULL) { 204*7c478bd9Sstevel@tonic-gate fprintf(stderr, "acctprc1: 1 Cannot reallocate memory\n"); 205*7c478bd9Sstevel@tonic-gate exit(2); 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate up = &ur[a_usize - A_USIZE]; 208*7c478bd9Sstevel@tonic-gate } 209*7c478bd9Sstevel@tonic-gate up->ur_uid = cb.ct_uid; 210*7c478bd9Sstevel@tonic-gate CPYN(up->ur_name, cb.ct_name); 211*7c478bd9Sstevel@tonic-gate up->ur_srec = sp; 212*7c478bd9Sstevel@tonic-gate up->ur_cnt = 0; 213*7c478bd9Sstevel@tonic-gate } 214*7c478bd9Sstevel@tonic-gate 215*7c478bd9Sstevel@tonic-gate if (sp >= &sr[a_ssize-1]) { 216*7c478bd9Sstevel@tonic-gate a_ssize = a_ssize + A_SSIZE; 217*7c478bd9Sstevel@tonic-gate if ((sr = (struct srec *) realloc(sr, a_ssize * 218*7c478bd9Sstevel@tonic-gate sizeof (struct srec))) == NULL) { 219*7c478bd9Sstevel@tonic-gate fprintf(stderr, "acctprc1: 2 Cannot reallocate memory\n"); 220*7c478bd9Sstevel@tonic-gate printf("errno=%d\n", errno); 221*7c478bd9Sstevel@tonic-gate exit(2); 222*7c478bd9Sstevel@tonic-gate } 223*7c478bd9Sstevel@tonic-gate sp = &sr[a_ssize - A_SSIZE]; 224*7c478bd9Sstevel@tonic-gate } 225*7c478bd9Sstevel@tonic-gate 226*7c478bd9Sstevel@tonic-gate sp->sr_tty = cb.ct_tty; 227*7c478bd9Sstevel@tonic-gate sp->sr_start = cb.ct_start; 228*7c478bd9Sstevel@tonic-gate sp->sr_end = cb.ct_start + cb.ct_con[0] + cb.ct_con[1]; 229*7c478bd9Sstevel@tonic-gate sp++; 230*7c478bd9Sstevel@tonic-gate up->ur_cnt++; 231*7c478bd9Sstevel@tonic-gate } 232*7c478bd9Sstevel@tonic-gate if (up != NULL) 233*7c478bd9Sstevel@tonic-gate urlast = ++up; 234*7c478bd9Sstevel@tonic-gate fclose(fp); 235*7c478bd9Sstevel@tonic-gate } 236*7c478bd9Sstevel@tonic-gate 237*7c478bd9Sstevel@tonic-gate /* 238*7c478bd9Sstevel@tonic-gate * using urec-srec data (if any), make best guess at login name 239*7c478bd9Sstevel@tonic-gate * corresponding to uid, return ptr to the name. 240*7c478bd9Sstevel@tonic-gate * must match on tty; use start time to help guess 241*7c478bd9Sstevel@tonic-gate * for any urec having same uid as uid, search array of associated 242*7c478bd9Sstevel@tonic-gate * srecs for those having same tty 243*7c478bd9Sstevel@tonic-gate * if start time of process is within range of session, that's it 244*7c478bd9Sstevel@tonic-gate * if none can be found within range, give it to person of same uid 245*7c478bd9Sstevel@tonic-gate * who last logged off on that terminal 246*7c478bd9Sstevel@tonic-gate */ 247*7c478bd9Sstevel@tonic-gate char * 248*7c478bd9Sstevel@tonic-gate getnamc(uid_t uid, dev_t tty, time_t start) 249*7c478bd9Sstevel@tonic-gate { 250*7c478bd9Sstevel@tonic-gate struct urec *up; 251*7c478bd9Sstevel@tonic-gate struct srec *sp; 252*7c478bd9Sstevel@tonic-gate struct srec *splast; 253*7c478bd9Sstevel@tonic-gate long latest; 254*7c478bd9Sstevel@tonic-gate char *guess; 255*7c478bd9Sstevel@tonic-gate 256*7c478bd9Sstevel@tonic-gate latest = 0; 257*7c478bd9Sstevel@tonic-gate guess = NULL; 258*7c478bd9Sstevel@tonic-gate for (up = ur; up < urlast && uid >= up->ur_uid; up++) 259*7c478bd9Sstevel@tonic-gate if (uid == up->ur_uid) { 260*7c478bd9Sstevel@tonic-gate sp = up->ur_srec; 261*7c478bd9Sstevel@tonic-gate splast = sp+up->ur_cnt; 262*7c478bd9Sstevel@tonic-gate for (; sp < splast; sp++) 263*7c478bd9Sstevel@tonic-gate if (tty == sp->sr_tty) { 264*7c478bd9Sstevel@tonic-gate if (start >= sp->sr_start && 265*7c478bd9Sstevel@tonic-gate start <= sp->sr_end) 266*7c478bd9Sstevel@tonic-gate return(up->ur_name); 267*7c478bd9Sstevel@tonic-gate if (start >= sp->sr_start && 268*7c478bd9Sstevel@tonic-gate sp->sr_end > latest) { 269*7c478bd9Sstevel@tonic-gate latest = sp->sr_end; 270*7c478bd9Sstevel@tonic-gate guess = up->ur_name; 271*7c478bd9Sstevel@tonic-gate } 272*7c478bd9Sstevel@tonic-gate } 273*7c478bd9Sstevel@tonic-gate } 274*7c478bd9Sstevel@tonic-gate 275*7c478bd9Sstevel@tonic-gate return(guess); 276*7c478bd9Sstevel@tonic-gate } 277*7c478bd9Sstevel@tonic-gate int 278*7c478bd9Sstevel@tonic-gate aread(int ver) 279*7c478bd9Sstevel@tonic-gate { 280*7c478bd9Sstevel@tonic-gate struct o_acct oab; 281*7c478bd9Sstevel@tonic-gate int ret; 282*7c478bd9Sstevel@tonic-gate 283*7c478bd9Sstevel@tonic-gate if (ver != 2) { 284*7c478bd9Sstevel@tonic-gate if ((ret = fread((char *)&oab, sizeof(struct o_acct), 1, stdin)) == 1){ 285*7c478bd9Sstevel@tonic-gate /* copy SVR3 acct struct to SVR4 acct struct */ 286*7c478bd9Sstevel@tonic-gate ab.ac_flag = oab.ac_flag | AEXPND; 287*7c478bd9Sstevel@tonic-gate ab.ac_stat = oab.ac_stat; 288*7c478bd9Sstevel@tonic-gate ab.ac_uid = (uid_t) oab.ac_uid; 289*7c478bd9Sstevel@tonic-gate ab.ac_gid = (gid_t) oab.ac_gid; 290*7c478bd9Sstevel@tonic-gate ab.ac_tty = (dev_t) oab.ac_tty; 291*7c478bd9Sstevel@tonic-gate ab.ac_btime = oab.ac_btime; 292*7c478bd9Sstevel@tonic-gate ab.ac_utime = oab.ac_utime; 293*7c478bd9Sstevel@tonic-gate ab.ac_stime = oab.ac_stime; 294*7c478bd9Sstevel@tonic-gate ab.ac_mem = oab.ac_mem; 295*7c478bd9Sstevel@tonic-gate ab.ac_io = oab.ac_io; 296*7c478bd9Sstevel@tonic-gate ab.ac_rw = oab.ac_rw; 297*7c478bd9Sstevel@tonic-gate strcpy(ab.ac_comm, oab.ac_comm); 298*7c478bd9Sstevel@tonic-gate } 299*7c478bd9Sstevel@tonic-gate } else 300*7c478bd9Sstevel@tonic-gate ret = fread((char *)&ab, sizeof(struct acct), 1, stdin); 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate 303*7c478bd9Sstevel@tonic-gate return(ret != 1 ? 0 : 1); 304*7c478bd9Sstevel@tonic-gate } 305