xref: /onnv-gate/usr/src/cmd/acct/acctprc1.c (revision 428:a261479c3dbd)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
230Sstevel@tonic-gate /*	  All Rights Reserved  	*/
240Sstevel@tonic-gate 
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
27*428Ssl108498  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
280Sstevel@tonic-gate  * Use is subject to license terms.
290Sstevel@tonic-gate  */
30*428Ssl108498 #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  *	acctprc1 [ctmpfile]
340Sstevel@tonic-gate  *	reads std. input (acct.h format), adds login names
350Sstevel@tonic-gate  *	writes std. output (ptmp.h/ascii format)
360Sstevel@tonic-gate  *	if ctmpfile is given, it is expected have ctmp.h/ascii data,
370Sstevel@tonic-gate  *	sorted by uid/name; it is used to make better guesses at login names
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/param.h>
420Sstevel@tonic-gate #include "acctdef.h"
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <errno.h>
450Sstevel@tonic-gate #include <sys/acct.h>
46*428Ssl108498 #include <stdlib.h>
47*428Ssl108498 
480Sstevel@tonic-gate #define MYKIND(flag)	((flag & ACCTF) == 0)
490Sstevel@tonic-gate 
500Sstevel@tonic-gate struct	acct	ab;
510Sstevel@tonic-gate struct	ctmp	cb;
520Sstevel@tonic-gate struct	ptmp	pb;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate int	a_usize = A_USIZE;
550Sstevel@tonic-gate struct urec {				/* 1 for each distinct uid/name */
560Sstevel@tonic-gate 	uid_t	ur_uid;			/* sorted by uid/name */
570Sstevel@tonic-gate 	char	ur_name[NSZ];
580Sstevel@tonic-gate 	struct srec	*ur_srec;		/* ptr to first session */
590Sstevel@tonic-gate 	short	ur_cnt;			/* # sessions */
600Sstevel@tonic-gate } * ur;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate struct urec *urlast;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate int	a_ssize = A_SSIZE;
650Sstevel@tonic-gate int	ssize;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate struct srec {				/* 1 for each distinct session */
680Sstevel@tonic-gate 	dev_t	sr_tty;			/* dev, used to connect with process*/
690Sstevel@tonic-gate 	time_t	sr_start;		/* start time of session */
700Sstevel@tonic-gate 	time_t	sr_end;			/* end time of session */
710Sstevel@tonic-gate } * sr;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate char *getname(uid_t, dev_t, time_t);
740Sstevel@tonic-gate void readctmp(char *);
750Sstevel@tonic-gate char *getnamc(uid_t, dev_t, time_t);
760Sstevel@tonic-gate int aread(int);
770Sstevel@tonic-gate 
780Sstevel@tonic-gate char	*uidtonam();
790Sstevel@tonic-gate 
800Sstevel@tonic-gate int
main(int argc,char ** argv)810Sstevel@tonic-gate main(int argc, char **argv)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	long		elaps[2];
840Sstevel@tonic-gate 	ulong_t		etime, stime;
850Sstevel@tonic-gate 	unsigned long	mem;
860Sstevel@tonic-gate 	ulong_t		expand();
870Sstevel@tonic-gate 	int 		ver;	/* version of acct struct */
880Sstevel@tonic-gate 	int 		aread();
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	if ((ur = (struct urec *) calloc(a_usize,
910Sstevel@tonic-gate 		sizeof (struct urec))) == NULL) {
920Sstevel@tonic-gate 		fprintf(stderr, "acctpr1: Cannot allocate memory\n");
930Sstevel@tonic-gate 		exit(3);
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	urlast = ur;
970Sstevel@tonic-gate 	if ((sr = (struct srec *) calloc(a_ssize,
980Sstevel@tonic-gate 		sizeof (struct srec))) == NULL) {
990Sstevel@tonic-gate 		fprintf(stderr, "acctpr1: Cannot allocate memory\n");
1000Sstevel@tonic-gate 		exit(3);
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	while (--argc > 0) {
1040Sstevel@tonic-gate 		if (**++argv == '-')
1050Sstevel@tonic-gate 			switch(*++*argv) {
1060Sstevel@tonic-gate 			}
1070Sstevel@tonic-gate 		else {
1080Sstevel@tonic-gate 			readctmp(*argv);
1090Sstevel@tonic-gate 		}
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	if (fread((char *)&ab, sizeof(struct acct), 1, stdin) != 1)
114*428Ssl108498 		exit(1);
1150Sstevel@tonic-gate 	else if (ab.ac_flag & AEXPND)
1160Sstevel@tonic-gate 		ver = 2;	/* 4.0 acct structure */
1170Sstevel@tonic-gate 	else
1180Sstevel@tonic-gate 		ver = 1;	/* 3.x acct structure */
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	rewind(stdin);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	while (aread(ver) == 1) {
1230Sstevel@tonic-gate 		if (!MYKIND(ab.ac_flag))
1240Sstevel@tonic-gate 			continue;
1250Sstevel@tonic-gate 		pb.pt_uid = ab.ac_uid;
1260Sstevel@tonic-gate 		CPYN(pb.pt_name, getname(ab.ac_uid, ab.ac_tty, ab.ac_btime));
1270Sstevel@tonic-gate 		/*
1280Sstevel@tonic-gate 		 * approximate cpu P/NP split as same as elapsed time
1290Sstevel@tonic-gate 		 */
1300Sstevel@tonic-gate 		if ((etime = SECS(expand(ab.ac_etime))) == 0)
1310Sstevel@tonic-gate 			etime = 1;
1320Sstevel@tonic-gate 		stime = expand(ab.ac_stime) + expand(ab.ac_utime);
1330Sstevel@tonic-gate 		mem = expand(ab.ac_mem);
1340Sstevel@tonic-gate 		if(pnpsplit(ab.ac_btime, etime, elaps) == 0) {
1350Sstevel@tonic-gate 			fprintf(stderr, "acctprc1: could not calculate prime/non-prime hours\n");
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 			exit(1);
1380Sstevel@tonic-gate 		}
1390Sstevel@tonic-gate 		pb.pt_cpu[0] = (double)stime * (double)elaps[0] / etime;
1400Sstevel@tonic-gate 		pb.pt_cpu[1] = (stime > pb.pt_cpu[0])? stime - pb.pt_cpu[0] : 0;
1410Sstevel@tonic-gate 		pb.pt_cpu[1] = stime - pb.pt_cpu[0];
1420Sstevel@tonic-gate 		if (stime)
1430Sstevel@tonic-gate 			pb.pt_mem = (mem + stime - 1) / stime;
1440Sstevel@tonic-gate 		else
1450Sstevel@tonic-gate 			pb.pt_mem = 0;	/* unlikely */
1460Sstevel@tonic-gate 		printf("%ld\t%.*s\t%lu\t%lu\t%u\n",
1470Sstevel@tonic-gate 		    pb.pt_uid,
1480Sstevel@tonic-gate 		    OUTPUT_NSZ,
1490Sstevel@tonic-gate 		    pb.pt_name,
1500Sstevel@tonic-gate 		    pb.pt_cpu[0], pb.pt_cpu[1],
1510Sstevel@tonic-gate 		    pb.pt_mem);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	exit(0);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  *	return ptr to name corresponding to uid
1590Sstevel@tonic-gate  *	try ctmp first, then use uidtonam (internal list or passwd file)
1600Sstevel@tonic-gate  */
1610Sstevel@tonic-gate char *
getname(uid_t uid,dev_t tty,time_t start)1620Sstevel@tonic-gate getname(uid_t uid, dev_t tty, time_t start)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	char *p;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	if ((p = getnamc(uid, tty, start)) != NULL)
1670Sstevel@tonic-gate 		return (p);
1680Sstevel@tonic-gate 	return (uidtonam(uid));
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  *	read ctmp file, build up urec-srec data structures for
1730Sstevel@tonic-gate  *	later use by getnamc
1740Sstevel@tonic-gate  */
1750Sstevel@tonic-gate void
readctmp(char * fname)1760Sstevel@tonic-gate readctmp(char *fname)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	FILE *fp;
1790Sstevel@tonic-gate 	struct urec *up;
1800Sstevel@tonic-gate 	struct srec *sp;
1810Sstevel@tonic-gate 	int i = 0, j = 0, k=0;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if ((fp = fopen(fname, "r")) == NULL) {
1840Sstevel@tonic-gate 		fprintf(stderr, "acctprc1: can't open %s\n", fname);
1850Sstevel@tonic-gate 		return;
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	up = NULL;
1890Sstevel@tonic-gate 	sp = sr;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	while (fscanf(fp, "%hd\t%ld\t%s\t%lu\t%lu\t%lu\t%*[^\n]",
1920Sstevel@tonic-gate 		&cb.ct_tty,
1930Sstevel@tonic-gate 		&cb.ct_uid,
1940Sstevel@tonic-gate 		cb.ct_name,
1950Sstevel@tonic-gate 		&cb.ct_con[0],
1960Sstevel@tonic-gate 		&cb.ct_con[1],
1970Sstevel@tonic-gate 		&cb.ct_start) != EOF) {
1980Sstevel@tonic-gate 		if (up == NULL || cb.ct_uid != up->ur_uid ||
1990Sstevel@tonic-gate 			!EQN(cb.ct_name, up->ur_name)) {
2000Sstevel@tonic-gate 			if (up == NULL)
2010Sstevel@tonic-gate 				up = ur;
2020Sstevel@tonic-gate 			if (++up >= &ur[a_usize]) {
2030Sstevel@tonic-gate 				a_usize = a_usize + A_USIZE;
2040Sstevel@tonic-gate                 		if ((ur = (struct urec *) realloc(ur, a_usize *
2050Sstevel@tonic-gate 					sizeof (struct urec))) == NULL) {
2060Sstevel@tonic-gate                         		fprintf(stderr, "acctprc1: 1 Cannot reallocate memory\n");
2070Sstevel@tonic-gate 					exit(2);
2080Sstevel@tonic-gate 				}
2090Sstevel@tonic-gate 				up = &ur[a_usize - A_USIZE];
2100Sstevel@tonic-gate 			}
2110Sstevel@tonic-gate 			up->ur_uid = cb.ct_uid;
2120Sstevel@tonic-gate 			CPYN(up->ur_name, cb.ct_name);
2130Sstevel@tonic-gate 			up->ur_srec = sp;
2140Sstevel@tonic-gate 			up->ur_cnt = 0;
2150Sstevel@tonic-gate 		}
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 		if (sp >= &sr[a_ssize-1]) {
2180Sstevel@tonic-gate 			a_ssize = a_ssize + A_SSIZE;
2190Sstevel@tonic-gate 			if ((sr = (struct srec *) realloc(sr, a_ssize *
2200Sstevel@tonic-gate 				sizeof (struct srec))) == NULL) {
2210Sstevel@tonic-gate 				fprintf(stderr, "acctprc1: 2 Cannot reallocate memory\n");
2220Sstevel@tonic-gate 				printf("errno=%d\n", errno);
2230Sstevel@tonic-gate 				exit(2);
2240Sstevel@tonic-gate 			}
2250Sstevel@tonic-gate 			sp = &sr[a_ssize - A_SSIZE];
2260Sstevel@tonic-gate 		}
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 		sp->sr_tty = cb.ct_tty;
2290Sstevel@tonic-gate 		sp->sr_start = cb.ct_start;
2300Sstevel@tonic-gate 		sp->sr_end = cb.ct_start + cb.ct_con[0] + cb.ct_con[1];
2310Sstevel@tonic-gate 		sp++;
2320Sstevel@tonic-gate 		up->ur_cnt++;
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 	if (up != NULL)
2350Sstevel@tonic-gate 		urlast = ++up;
2360Sstevel@tonic-gate 	fclose(fp);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate  *	using urec-srec data (if any), make best guess at login name
2410Sstevel@tonic-gate  *	corresponding to uid, return ptr to the name.
2420Sstevel@tonic-gate  *	must match on tty; use start time to help guess
2430Sstevel@tonic-gate  *	for any urec having same uid as uid, search array of associated
2440Sstevel@tonic-gate  *	srecs for those having same tty
2450Sstevel@tonic-gate  *	if start time of process is within range of session, that's it
2460Sstevel@tonic-gate  *	if none can be found within range, give it to person of same uid
2470Sstevel@tonic-gate  *	who last logged off on that terminal
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate char *
getnamc(uid_t uid,dev_t tty,time_t start)2500Sstevel@tonic-gate getnamc(uid_t uid, dev_t tty, time_t start)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	struct urec *up;
2530Sstevel@tonic-gate 	struct srec *sp;
2540Sstevel@tonic-gate 	struct srec *splast;
2550Sstevel@tonic-gate 	long latest;
2560Sstevel@tonic-gate 	char *guess;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	latest = 0;
2590Sstevel@tonic-gate 	guess = NULL;
2600Sstevel@tonic-gate 	for (up = ur; up < urlast && uid >= up->ur_uid; up++)
2610Sstevel@tonic-gate 		if (uid == up->ur_uid) {
2620Sstevel@tonic-gate 			sp = up->ur_srec;
2630Sstevel@tonic-gate 			splast = sp+up->ur_cnt;
2640Sstevel@tonic-gate 			for (; sp < splast; sp++)
2650Sstevel@tonic-gate 				if (tty == sp->sr_tty) {
2660Sstevel@tonic-gate 					if (start >= sp->sr_start &&
2670Sstevel@tonic-gate 						start <= sp->sr_end)
2680Sstevel@tonic-gate 						return(up->ur_name);
2690Sstevel@tonic-gate 					if (start >= sp->sr_start &&
2700Sstevel@tonic-gate 						sp->sr_end > latest) {
2710Sstevel@tonic-gate 						latest = sp->sr_end;
2720Sstevel@tonic-gate 						guess = up->ur_name;
2730Sstevel@tonic-gate 					}
2740Sstevel@tonic-gate 				}
2750Sstevel@tonic-gate 		}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	return(guess);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate int
aread(int ver)2800Sstevel@tonic-gate aread(int ver)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	struct o_acct oab;
2830Sstevel@tonic-gate 	int ret;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	if (ver != 2) {
2860Sstevel@tonic-gate 		if ((ret = fread((char *)&oab, sizeof(struct o_acct), 1, stdin)) == 1){
2870Sstevel@tonic-gate 			/* copy SVR3 acct struct to SVR4 acct struct */
2880Sstevel@tonic-gate 			ab.ac_flag = oab.ac_flag | AEXPND;
2890Sstevel@tonic-gate 			ab.ac_stat = oab.ac_stat;
2900Sstevel@tonic-gate 			ab.ac_uid = (uid_t) oab.ac_uid;
2910Sstevel@tonic-gate 			ab.ac_gid = (gid_t) oab.ac_gid;
2920Sstevel@tonic-gate 			ab.ac_tty = (dev_t) oab.ac_tty;
2930Sstevel@tonic-gate 			ab.ac_btime = oab.ac_btime;
2940Sstevel@tonic-gate 			ab.ac_utime = oab.ac_utime;
2950Sstevel@tonic-gate 			ab.ac_stime = oab.ac_stime;
2960Sstevel@tonic-gate 			ab.ac_mem = oab.ac_mem;
2970Sstevel@tonic-gate 			ab.ac_io = oab.ac_io;
2980Sstevel@tonic-gate 			ab.ac_rw = oab.ac_rw;
2990Sstevel@tonic-gate 			strcpy(ab.ac_comm, oab.ac_comm);
3000Sstevel@tonic-gate 		}
3010Sstevel@tonic-gate 	} else
3020Sstevel@tonic-gate 		ret = fread((char *)&ab, sizeof(struct acct), 1, stdin);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	return(ret != 1 ? 0 : 1);
3060Sstevel@tonic-gate }
307