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