xref: /titanic_41/usr/src/cmd/acct/acctcon.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  *	acctcon [-l file] [-o file] <wtmpx-file
35*7c478bd9Sstevel@tonic-gate  *	-l file	causes output of line usage summary
36*7c478bd9Sstevel@tonic-gate  *	-o file	causes first/last/reboots report to be written to file
37*7c478bd9Sstevel@tonic-gate  *	reads input (normally /var/adm/wtmpx), produces
38*7c478bd9Sstevel@tonic-gate  *	list of sessions, sorted by ending time in tacct.h format
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include <stdio.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
44*7c478bd9Sstevel@tonic-gate #include "acctdef.h"
45*7c478bd9Sstevel@tonic-gate #include <ctype.h>
46*7c478bd9Sstevel@tonic-gate #include <time.h>
47*7c478bd9Sstevel@tonic-gate #include <utmpx.h>
48*7c478bd9Sstevel@tonic-gate #include <locale.h>
49*7c478bd9Sstevel@tonic-gate #include <string.h>
50*7c478bd9Sstevel@tonic-gate #include <search.h>
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate int   a_tsize = A_TSIZE;
53*7c478bd9Sstevel@tonic-gate int	tsize	= -1;	/* highest index of used slot in tbuf table */
54*7c478bd9Sstevel@tonic-gate static	csize;
55*7c478bd9Sstevel@tonic-gate struct  utmpx	wb;	/* record structure read into */
56*7c478bd9Sstevel@tonic-gate struct	ctmp	cb;	/* record structure written out of */
57*7c478bd9Sstevel@tonic-gate struct	tacct	tb;
58*7c478bd9Sstevel@tonic-gate double	timet, timei;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate struct tbuf {
61*7c478bd9Sstevel@tonic-gate 	char	tline[LSZ];	/* /dev/...  */
62*7c478bd9Sstevel@tonic-gate 	char	tname[NSZ];	/* user name */
63*7c478bd9Sstevel@tonic-gate 	time_t	ttime;		/* start time */
64*7c478bd9Sstevel@tonic-gate 	dev_t	tdev;		/* device */
65*7c478bd9Sstevel@tonic-gate 	int	tlsess;		/* # complete sessions */
66*7c478bd9Sstevel@tonic-gate 	int	tlon;		/* # times on (ut_type of 7) */
67*7c478bd9Sstevel@tonic-gate 	int	tloff;		/* # times off (ut_type != 7) */
68*7c478bd9Sstevel@tonic-gate 	long	ttotal;		/* total time used on this line */
69*7c478bd9Sstevel@tonic-gate } *tbuf;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate struct ctab {
72*7c478bd9Sstevel@tonic-gate 	uid_t		ct_uid;
73*7c478bd9Sstevel@tonic-gate 	char		ct_name[NSZ];
74*7c478bd9Sstevel@tonic-gate 	long 		ct_con[2];
75*7c478bd9Sstevel@tonic-gate 	ushort_t	ct_sess;
76*7c478bd9Sstevel@tonic-gate } *pctab;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate int	nsys;
79*7c478bd9Sstevel@tonic-gate struct sys {
80*7c478bd9Sstevel@tonic-gate 	char	sname[LSZ];	/* reasons for ACCOUNTING records */
81*7c478bd9Sstevel@tonic-gate 	char	snum;		/* number of times encountered */
82*7c478bd9Sstevel@tonic-gate } sy[NSYS];
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate static char time_buf[50];
85*7c478bd9Sstevel@tonic-gate time_t	datetime;	/* old time if date changed, otherwise 0 */
86*7c478bd9Sstevel@tonic-gate time_t	firstime;
87*7c478bd9Sstevel@tonic-gate time_t	lastime;
88*7c478bd9Sstevel@tonic-gate int	ndates;		/* number of times date changed */
89*7c478bd9Sstevel@tonic-gate int	exitcode;
90*7c478bd9Sstevel@tonic-gate char	*report	= NULL;
91*7c478bd9Sstevel@tonic-gate char	*replin = NULL;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate uid_t	namtouid();
94*7c478bd9Sstevel@tonic-gate dev_t	lintodev();
95*7c478bd9Sstevel@tonic-gate static int valid(void);
96*7c478bd9Sstevel@tonic-gate static void fixup(FILE *);
97*7c478bd9Sstevel@tonic-gate static void loop(void);
98*7c478bd9Sstevel@tonic-gate static void bootshut(void);
99*7c478bd9Sstevel@tonic-gate static int iline(void);
100*7c478bd9Sstevel@tonic-gate static void upall(void);
101*7c478bd9Sstevel@tonic-gate static void update(struct tbuf *);
102*7c478bd9Sstevel@tonic-gate static void printrep(void);
103*7c478bd9Sstevel@tonic-gate static void printlin(void);
104*7c478bd9Sstevel@tonic-gate static int tcmp(struct tbuf *, struct tbuf *);
105*7c478bd9Sstevel@tonic-gate static int node_compare(const void *, const void *);
106*7c478bd9Sstevel@tonic-gate static void enter(struct ctmp *);
107*7c478bd9Sstevel@tonic-gate static void print_node(const void *, VISIT, int);
108*7c478bd9Sstevel@tonic-gate static void output(void);
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate extern char 	*optarg;
111*7c478bd9Sstevel@tonic-gate extern int	optind;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate void **root = NULL;
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate int
116*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate 	int c;
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
121*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "l:o:")) != EOF)
122*7c478bd9Sstevel@tonic-gate 		switch (c) {
123*7c478bd9Sstevel@tonic-gate 		case 'l':
124*7c478bd9Sstevel@tonic-gate 			replin = optarg;
125*7c478bd9Sstevel@tonic-gate 			break;
126*7c478bd9Sstevel@tonic-gate 		case 'o':
127*7c478bd9Sstevel@tonic-gate 			report = optarg;
128*7c478bd9Sstevel@tonic-gate 			break;
129*7c478bd9Sstevel@tonic-gate 		case '?':
130*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "usage: %s [-l lineuse] "
131*7c478bd9Sstevel@tonic-gate 			    "[-o reboot]\n", argv[0]);
132*7c478bd9Sstevel@tonic-gate 			exit(1);
133*7c478bd9Sstevel@tonic-gate 		}
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	if ((tbuf = (struct tbuf *)calloc(a_tsize,
136*7c478bd9Sstevel@tonic-gate 		sizeof (struct tbuf))) == NULL) {
137*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "acctcon: Cannot allocate memory\n");
138*7c478bd9Sstevel@tonic-gate 		exit(3);
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	/*
142*7c478bd9Sstevel@tonic-gate 	 * XXX - fixme - need a good way of getting the fd that getutxent would
143*7c478bd9Sstevel@tonic-gate 	 * use to access wtmpx, so we can convert this read of stdin to use
144*7c478bd9Sstevel@tonic-gate 	 * the APIs and remove the dependence on the existence of the file.
145*7c478bd9Sstevel@tonic-gate 	 */
146*7c478bd9Sstevel@tonic-gate 	while (fread(&wb, sizeof (wb), 1, stdin) == 1) {
147*7c478bd9Sstevel@tonic-gate 		if (firstime == 0)
148*7c478bd9Sstevel@tonic-gate 			firstime = wb.ut_xtime;
149*7c478bd9Sstevel@tonic-gate 		if (valid())
150*7c478bd9Sstevel@tonic-gate 			loop();
151*7c478bd9Sstevel@tonic-gate 		else
152*7c478bd9Sstevel@tonic-gate 			fixup(stderr);
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 	wb.ut_name[0] = '\0';
155*7c478bd9Sstevel@tonic-gate 	strcpy(wb.ut_line, "acctcon");
156*7c478bd9Sstevel@tonic-gate 	wb.ut_type = ACCOUNTING;
157*7c478bd9Sstevel@tonic-gate 	wb.ut_xtime = lastime;
158*7c478bd9Sstevel@tonic-gate 	loop();
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	output();
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	if (report != NULL)
163*7c478bd9Sstevel@tonic-gate 		printrep();
164*7c478bd9Sstevel@tonic-gate 	if (replin != NULL)
165*7c478bd9Sstevel@tonic-gate 		printlin();
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	exit(exitcode);
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate /*
172*7c478bd9Sstevel@tonic-gate  * valid: check input wtmpx record, return 1 if looks OK
173*7c478bd9Sstevel@tonic-gate  */
174*7c478bd9Sstevel@tonic-gate static int
175*7c478bd9Sstevel@tonic-gate valid()
176*7c478bd9Sstevel@tonic-gate {
177*7c478bd9Sstevel@tonic-gate 	int i, c;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	/* XPG say that user names should not start with a "-" */
180*7c478bd9Sstevel@tonic-gate 	if ((c = wb.ut_name[0]) == '-')
181*7c478bd9Sstevel@tonic-gate 		return (0);
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < NSZ; i++) {
184*7c478bd9Sstevel@tonic-gate 		c = wb.ut_name[i];
185*7c478bd9Sstevel@tonic-gate 		if (isalnum(c) || c == '$' || c == ' ' || c == '.' ||
186*7c478bd9Sstevel@tonic-gate 			c == '_' || c == '-')
187*7c478bd9Sstevel@tonic-gate 			continue;
188*7c478bd9Sstevel@tonic-gate 		else if (c == '\0')
189*7c478bd9Sstevel@tonic-gate 			break;
190*7c478bd9Sstevel@tonic-gate 		else
191*7c478bd9Sstevel@tonic-gate 			return (0);
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	if ((wb.ut_type >= EMPTY) && (wb.ut_type <= UTMAXTYPE))
195*7c478bd9Sstevel@tonic-gate 		return (1);
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	return (0);
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate static void
201*7c478bd9Sstevel@tonic-gate fixup(FILE *stream)
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate 	fprintf(stream, "bad wtmpx: offset %lu.\n", ftell(stdin)-sizeof (wb));
204*7c478bd9Sstevel@tonic-gate 	fprintf(stream, "bad record is:  %.*s\t%.*s\t%lu",
205*7c478bd9Sstevel@tonic-gate 	    sizeof (wb.ut_line),
206*7c478bd9Sstevel@tonic-gate 	    wb.ut_line,
207*7c478bd9Sstevel@tonic-gate 	    sizeof (wb.ut_name),
208*7c478bd9Sstevel@tonic-gate 	    wb.ut_name,
209*7c478bd9Sstevel@tonic-gate 	    wb.ut_xtime);
210*7c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &wb.ut_xtime);
211*7c478bd9Sstevel@tonic-gate 	fprintf(stream, "\t%s", time_buf);
212*7c478bd9Sstevel@tonic-gate 	exitcode = 1;
213*7c478bd9Sstevel@tonic-gate }
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate static void
216*7c478bd9Sstevel@tonic-gate loop()
217*7c478bd9Sstevel@tonic-gate {
218*7c478bd9Sstevel@tonic-gate 	int timediff;
219*7c478bd9Sstevel@tonic-gate 	struct tbuf *tp;
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	if (wb.ut_line[0] == '\0')	/* It's an init admin process */
222*7c478bd9Sstevel@tonic-gate 		return;			/* no connect accounting data here */
223*7c478bd9Sstevel@tonic-gate 	switch (wb.ut_type) {
224*7c478bd9Sstevel@tonic-gate 	case OLD_TIME:
225*7c478bd9Sstevel@tonic-gate 		datetime = wb.ut_xtime;
226*7c478bd9Sstevel@tonic-gate 		return;
227*7c478bd9Sstevel@tonic-gate 	case NEW_TIME:
228*7c478bd9Sstevel@tonic-gate 		if (datetime == 0)
229*7c478bd9Sstevel@tonic-gate 			return;
230*7c478bd9Sstevel@tonic-gate 		timediff = wb.ut_xtime - datetime;
231*7c478bd9Sstevel@tonic-gate 		for (tp = tbuf; tp <= &tbuf[tsize]; tp++)
232*7c478bd9Sstevel@tonic-gate 			tp->ttime += timediff;
233*7c478bd9Sstevel@tonic-gate 		datetime = 0;
234*7c478bd9Sstevel@tonic-gate 		ndates++;
235*7c478bd9Sstevel@tonic-gate 		return;
236*7c478bd9Sstevel@tonic-gate 	case BOOT_TIME:
237*7c478bd9Sstevel@tonic-gate 		upall();
238*7c478bd9Sstevel@tonic-gate 	case ACCOUNTING:
239*7c478bd9Sstevel@tonic-gate 	case RUN_LVL:
240*7c478bd9Sstevel@tonic-gate 		lastime = wb.ut_xtime;
241*7c478bd9Sstevel@tonic-gate 		bootshut();
242*7c478bd9Sstevel@tonic-gate 		return;
243*7c478bd9Sstevel@tonic-gate 	case USER_PROCESS:
244*7c478bd9Sstevel@tonic-gate 	case LOGIN_PROCESS:
245*7c478bd9Sstevel@tonic-gate 	case INIT_PROCESS:
246*7c478bd9Sstevel@tonic-gate 	case DEAD_PROCESS:	/* WHCC mod 3/86  */
247*7c478bd9Sstevel@tonic-gate 		update(&tbuf[iline()]);
248*7c478bd9Sstevel@tonic-gate 		return;
249*7c478bd9Sstevel@tonic-gate 	case EMPTY:
250*7c478bd9Sstevel@tonic-gate 		return;
251*7c478bd9Sstevel@tonic-gate 	default:
252*7c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &wb.ut_xtime);
253*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "acctcon: invalid type %d for %s %s %s",
254*7c478bd9Sstevel@tonic-gate 			wb.ut_type,
255*7c478bd9Sstevel@tonic-gate 			wb.ut_name,
256*7c478bd9Sstevel@tonic-gate 			wb.ut_line,
257*7c478bd9Sstevel@tonic-gate 			time_buf);
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate }
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate /*
262*7c478bd9Sstevel@tonic-gate  * bootshut: record reboot (or shutdown)
263*7c478bd9Sstevel@tonic-gate  * bump count, looking up wb.ut_line in sy table
264*7c478bd9Sstevel@tonic-gate  */
265*7c478bd9Sstevel@tonic-gate static void
266*7c478bd9Sstevel@tonic-gate bootshut()
267*7c478bd9Sstevel@tonic-gate {
268*7c478bd9Sstevel@tonic-gate 	int i;
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsys && !EQN(wb.ut_line, sy[i].sname); i++)
271*7c478bd9Sstevel@tonic-gate 		;
272*7c478bd9Sstevel@tonic-gate 	if (i >= nsys) {
273*7c478bd9Sstevel@tonic-gate 		if (++nsys > NSYS) {
274*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
275*7c478bd9Sstevel@tonic-gate 				"acctcon: recompile with larger NSYS\n");
276*7c478bd9Sstevel@tonic-gate 			nsys = NSYS;
277*7c478bd9Sstevel@tonic-gate 			return;
278*7c478bd9Sstevel@tonic-gate 		}
279*7c478bd9Sstevel@tonic-gate 		CPYN(sy[i].sname, wb.ut_line);
280*7c478bd9Sstevel@tonic-gate 	}
281*7c478bd9Sstevel@tonic-gate 	sy[i].snum++;
282*7c478bd9Sstevel@tonic-gate }
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate /*
285*7c478bd9Sstevel@tonic-gate  * iline: look up/enter current line name in tbuf, return index
286*7c478bd9Sstevel@tonic-gate  * (used to avoid system dependencies on naming)
287*7c478bd9Sstevel@tonic-gate  */
288*7c478bd9Sstevel@tonic-gate static int
289*7c478bd9Sstevel@tonic-gate iline()
290*7c478bd9Sstevel@tonic-gate {
291*7c478bd9Sstevel@tonic-gate 	int i;
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	for (i = 0; i <= tsize; i++)
294*7c478bd9Sstevel@tonic-gate 		if (EQN(wb.ut_line, tbuf[i].tline))
295*7c478bd9Sstevel@tonic-gate 			return (i);
296*7c478bd9Sstevel@tonic-gate 	if (++tsize >= a_tsize) {
297*7c478bd9Sstevel@tonic-gate 		a_tsize = a_tsize + A_TSIZE;
298*7c478bd9Sstevel@tonic-gate 		if ((tbuf = (struct tbuf *)realloc(tbuf, a_tsize *
299*7c478bd9Sstevel@tonic-gate 			sizeof (struct tbuf))) == NULL) {
300*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "acctcon: Cannot reallocate memory\n");
301*7c478bd9Sstevel@tonic-gate 			exit(2);
302*7c478bd9Sstevel@tonic-gate 		}
303*7c478bd9Sstevel@tonic-gate 	}
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 	CPYN(tbuf[tsize].tline, wb.ut_line);
306*7c478bd9Sstevel@tonic-gate 	tbuf[tsize].tdev = lintodev(wb.ut_line);
307*7c478bd9Sstevel@tonic-gate 	return (tsize);
308*7c478bd9Sstevel@tonic-gate }
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate static void
311*7c478bd9Sstevel@tonic-gate upall()
312*7c478bd9Sstevel@tonic-gate {
313*7c478bd9Sstevel@tonic-gate 	struct tbuf *tp;
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate 	wb.ut_type = DEAD_PROCESS;	/* fudge a logoff for reboot record. */
316*7c478bd9Sstevel@tonic-gate 	for (tp = tbuf; tp <= &tbuf[tsize]; tp++)
317*7c478bd9Sstevel@tonic-gate 		update(tp);
318*7c478bd9Sstevel@tonic-gate }
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate /*
321*7c478bd9Sstevel@tonic-gate  * update tbuf with new time, write ctmp record for end of session
322*7c478bd9Sstevel@tonic-gate  */
323*7c478bd9Sstevel@tonic-gate static void
324*7c478bd9Sstevel@tonic-gate update(struct tbuf *tp)
325*7c478bd9Sstevel@tonic-gate {
326*7c478bd9Sstevel@tonic-gate 	time_t	told,	/* last time for tbuf record */
327*7c478bd9Sstevel@tonic-gate 		tnew;	/* time of this record */
328*7c478bd9Sstevel@tonic-gate 			/* Difference is connect time */
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate 	told = tp->ttime;
331*7c478bd9Sstevel@tonic-gate 	tnew = wb.ut_xtime;
332*7c478bd9Sstevel@tonic-gate 	if (told > tnew) {
333*7c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &told);
334*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "acctcon: bad times: old: %s", time_buf);
335*7c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &tnew);
336*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "new: %s", time_buf);
337*7c478bd9Sstevel@tonic-gate 		exitcode = 1;
338*7c478bd9Sstevel@tonic-gate 		tp->ttime = tnew;
339*7c478bd9Sstevel@tonic-gate 		return;
340*7c478bd9Sstevel@tonic-gate 	}
341*7c478bd9Sstevel@tonic-gate 	tp->ttime = tnew;
342*7c478bd9Sstevel@tonic-gate 	switch (wb.ut_type) {
343*7c478bd9Sstevel@tonic-gate 	case USER_PROCESS:
344*7c478bd9Sstevel@tonic-gate 		tp->tlsess++;
345*7c478bd9Sstevel@tonic-gate 		/*
346*7c478bd9Sstevel@tonic-gate 		 * Someone logged in without logging off. Put out record.
347*7c478bd9Sstevel@tonic-gate 		 */
348*7c478bd9Sstevel@tonic-gate 		if (tp->tname[0] != '\0') {
349*7c478bd9Sstevel@tonic-gate 			cb.ct_tty = tp->tdev;
350*7c478bd9Sstevel@tonic-gate 			CPYN(cb.ct_name, tp->tname);
351*7c478bd9Sstevel@tonic-gate 			cb.ct_uid = namtouid(cb.ct_name);
352*7c478bd9Sstevel@tonic-gate 			cb.ct_start = told;
353*7c478bd9Sstevel@tonic-gate 			if (pnpsplit(cb.ct_start, (ulong_t)(tnew-told),
354*7c478bd9Sstevel@tonic-gate 			    cb.ct_con) == 0) {
355*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, "acctcon: could not calculate "
356*7c478bd9Sstevel@tonic-gate 				    "prime/non-prime hours\n");
357*7c478bd9Sstevel@tonic-gate 				exit(1);
358*7c478bd9Sstevel@tonic-gate 			}
359*7c478bd9Sstevel@tonic-gate 			enter(&cb);
360*7c478bd9Sstevel@tonic-gate 			tp->ttotal += tnew-told;
361*7c478bd9Sstevel@tonic-gate 		} else	/* Someone just logged in */
362*7c478bd9Sstevel@tonic-gate 			tp->tlon++;
363*7c478bd9Sstevel@tonic-gate 		CPYN(tp->tname, wb.ut_name);
364*7c478bd9Sstevel@tonic-gate 		break;
365*7c478bd9Sstevel@tonic-gate 	case DEAD_PROCESS:
366*7c478bd9Sstevel@tonic-gate 		tp->tloff++;
367*7c478bd9Sstevel@tonic-gate 		if (tp->tname[0] != '\0') { /* Someone logged off */
368*7c478bd9Sstevel@tonic-gate 			/* Set up and print ctmp record */
369*7c478bd9Sstevel@tonic-gate 			cb.ct_tty = tp->tdev;
370*7c478bd9Sstevel@tonic-gate 			CPYN(cb.ct_name, tp->tname);
371*7c478bd9Sstevel@tonic-gate 			cb.ct_uid = namtouid(cb.ct_name);
372*7c478bd9Sstevel@tonic-gate 			cb.ct_start = told;
373*7c478bd9Sstevel@tonic-gate 			if (pnpsplit(cb.ct_start, (ulong_t)(tnew-told),
374*7c478bd9Sstevel@tonic-gate 			    cb.ct_con) == 0) {
375*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, "acctcon: could not calculate "
376*7c478bd9Sstevel@tonic-gate 				    "prime/non-prime hours\n");
377*7c478bd9Sstevel@tonic-gate 				exit(1);
378*7c478bd9Sstevel@tonic-gate 			}
379*7c478bd9Sstevel@tonic-gate 			enter(&cb);
380*7c478bd9Sstevel@tonic-gate 			tp->ttotal += tnew-told;
381*7c478bd9Sstevel@tonic-gate 			tp->tname[0] = '\0';
382*7c478bd9Sstevel@tonic-gate 		}
383*7c478bd9Sstevel@tonic-gate 	}
384*7c478bd9Sstevel@tonic-gate }
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate static void
387*7c478bd9Sstevel@tonic-gate printrep()
388*7c478bd9Sstevel@tonic-gate {
389*7c478bd9Sstevel@tonic-gate 	int i;
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate 	freopen(report, "w", stdout);
392*7c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &firstime);
393*7c478bd9Sstevel@tonic-gate 	printf("from %s", time_buf);
394*7c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT, &lastime);
395*7c478bd9Sstevel@tonic-gate 	printf("to   %s", time_buf);
396*7c478bd9Sstevel@tonic-gate 	if (ndates)
397*7c478bd9Sstevel@tonic-gate 		printf("%d\tdate change%c\n", ndates, (ndates > 1 ? 's' :
398*7c478bd9Sstevel@tonic-gate 		    '\0'));
399*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsys; i++)
400*7c478bd9Sstevel@tonic-gate 		printf("%d\t%.*s\n", sy[i].snum,
401*7c478bd9Sstevel@tonic-gate 		    sizeof (sy[i].sname), sy[i].sname);
402*7c478bd9Sstevel@tonic-gate }
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate /*
406*7c478bd9Sstevel@tonic-gate  *	print summary of line usage
407*7c478bd9Sstevel@tonic-gate  *	accuracy only guaranteed for wtmpx file started fresh
408*7c478bd9Sstevel@tonic-gate  */
409*7c478bd9Sstevel@tonic-gate static void
410*7c478bd9Sstevel@tonic-gate printlin()
411*7c478bd9Sstevel@tonic-gate {
412*7c478bd9Sstevel@tonic-gate 	struct tbuf *tp;
413*7c478bd9Sstevel@tonic-gate 	double ttime;
414*7c478bd9Sstevel@tonic-gate 	int tsess, ton, toff;
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	freopen(replin, "w", stdout);
417*7c478bd9Sstevel@tonic-gate 	ttime = 0.0;
418*7c478bd9Sstevel@tonic-gate 	tsess = ton = toff = 0;
419*7c478bd9Sstevel@tonic-gate 	timet = MINS(lastime-firstime);
420*7c478bd9Sstevel@tonic-gate 	printf("TOTAL DURATION IS %.0f MINUTES\n", timet);
421*7c478bd9Sstevel@tonic-gate 	printf("LINE         MINUTES  PERCENT  # SESS  # ON  # OFF\n");
422*7c478bd9Sstevel@tonic-gate 	qsort((char *)tbuf, tsize + 1, sizeof (tbuf[0]), tcmp);
423*7c478bd9Sstevel@tonic-gate 	for (tp = tbuf; tp <= &tbuf[tsize]; tp++) {
424*7c478bd9Sstevel@tonic-gate 		timei = MINS(tp->ttotal);
425*7c478bd9Sstevel@tonic-gate 		ttime += timei;
426*7c478bd9Sstevel@tonic-gate 		tsess += tp->tlsess;
427*7c478bd9Sstevel@tonic-gate 		ton += tp->tlon;
428*7c478bd9Sstevel@tonic-gate 		toff += tp->tloff;
429*7c478bd9Sstevel@tonic-gate 		printf("%-*.*s %-7.0f  %-7.0f  %-6d  %-4d  %-5d\n",
430*7c478bd9Sstevel@tonic-gate 		    OUTPUT_LSZ,
431*7c478bd9Sstevel@tonic-gate 		    OUTPUT_LSZ,
432*7c478bd9Sstevel@tonic-gate 		    tp->tline,
433*7c478bd9Sstevel@tonic-gate 		    timei,
434*7c478bd9Sstevel@tonic-gate 		    (timet > 0.)? 100*timei/timet : 0.,
435*7c478bd9Sstevel@tonic-gate 		    tp->tlsess,
436*7c478bd9Sstevel@tonic-gate 		    tp->tlon,
437*7c478bd9Sstevel@tonic-gate 		    tp->tloff);
438*7c478bd9Sstevel@tonic-gate 	}
439*7c478bd9Sstevel@tonic-gate 	printf("TOTALS       %-7.0f  --       %-6d  %-4d  %-5d\n",
440*7c478bd9Sstevel@tonic-gate 	    ttime, tsess, ton, toff);
441*7c478bd9Sstevel@tonic-gate }
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate static int
444*7c478bd9Sstevel@tonic-gate tcmp(struct tbuf *t1, struct tbuf *t2)
445*7c478bd9Sstevel@tonic-gate {
446*7c478bd9Sstevel@tonic-gate 	return (strncmp(t1->tline, t2->tline, LSZ));
447*7c478bd9Sstevel@tonic-gate }
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate static int
450*7c478bd9Sstevel@tonic-gate node_compare(const void *node1, const void *node2)
451*7c478bd9Sstevel@tonic-gate {
452*7c478bd9Sstevel@tonic-gate 	if (((const struct ctab *)node1)->ct_uid >
453*7c478bd9Sstevel@tonic-gate 	    ((const struct ctab *)node2)->ct_uid)
454*7c478bd9Sstevel@tonic-gate 		return (1);
455*7c478bd9Sstevel@tonic-gate 	else if (((const struct ctab *)node1)->ct_uid <
456*7c478bd9Sstevel@tonic-gate 	    ((const struct ctab *)node2)->ct_uid)
457*7c478bd9Sstevel@tonic-gate 		return (-1);
458*7c478bd9Sstevel@tonic-gate 	else
459*7c478bd9Sstevel@tonic-gate 		return (0);
460*7c478bd9Sstevel@tonic-gate }
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate static void
463*7c478bd9Sstevel@tonic-gate enter(struct ctmp *c)
464*7c478bd9Sstevel@tonic-gate {
465*7c478bd9Sstevel@tonic-gate 	unsigned i;
466*7c478bd9Sstevel@tonic-gate 	int j;
467*7c478bd9Sstevel@tonic-gate 	struct ctab **pt;
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate 	if ((pctab = (struct ctab *)malloc(sizeof (struct ctab))) == NULL) {
470*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "acctcon: malloc fail!\n");
471*7c478bd9Sstevel@tonic-gate 		exit(2);
472*7c478bd9Sstevel@tonic-gate 	}
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 	pctab->ct_uid = c->ct_uid;
475*7c478bd9Sstevel@tonic-gate 	CPYN(pctab->ct_name, c->ct_name);
476*7c478bd9Sstevel@tonic-gate 	pctab->ct_con[0] = c->ct_con[0];
477*7c478bd9Sstevel@tonic-gate 	pctab->ct_con[1] = c->ct_con[1];
478*7c478bd9Sstevel@tonic-gate 	pctab->ct_sess = 1;
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate 	if (*(pt = (struct ctab **)tsearch((void *)pctab, (void **)&root,  \
481*7c478bd9Sstevel@tonic-gate 		node_compare)) == NULL) {
482*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Not enough space available to build tree\n");
483*7c478bd9Sstevel@tonic-gate 		exit(1);
484*7c478bd9Sstevel@tonic-gate 	}
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	if (*pt != pctab) {
487*7c478bd9Sstevel@tonic-gate 		(*pt)->ct_con[0] += c->ct_con[0];
488*7c478bd9Sstevel@tonic-gate 		(*pt)->ct_con[1] += c->ct_con[1];
489*7c478bd9Sstevel@tonic-gate 		(*pt)->ct_sess++;
490*7c478bd9Sstevel@tonic-gate 		free(pctab);
491*7c478bd9Sstevel@tonic-gate 	}
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate }
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate static void
496*7c478bd9Sstevel@tonic-gate print_node(const void *node, VISIT order, int level)
497*7c478bd9Sstevel@tonic-gate {
498*7c478bd9Sstevel@tonic-gate 	if (order == postorder || order == leaf) {
499*7c478bd9Sstevel@tonic-gate 		tb.ta_uid = (*(struct ctab **)node)->ct_uid;
500*7c478bd9Sstevel@tonic-gate 		CPYN(tb.ta_name, (*(struct ctab **)node)->ct_name);
501*7c478bd9Sstevel@tonic-gate 		tb.ta_con[0] = ((*(struct ctab **)node)->ct_con[0]) / 60.0;
502*7c478bd9Sstevel@tonic-gate 		tb.ta_con[1] = ((*(struct ctab **)node)->ct_con[1]) / 60.0;
503*7c478bd9Sstevel@tonic-gate 		tb.ta_sc = (*(struct ctab **)node)->ct_sess;
504*7c478bd9Sstevel@tonic-gate 		fwrite(&tb, sizeof (tb), 1, stdout);
505*7c478bd9Sstevel@tonic-gate 	}
506*7c478bd9Sstevel@tonic-gate }
507*7c478bd9Sstevel@tonic-gate 
508*7c478bd9Sstevel@tonic-gate static void
509*7c478bd9Sstevel@tonic-gate output()
510*7c478bd9Sstevel@tonic-gate {
511*7c478bd9Sstevel@tonic-gate 	twalk((struct ctab *)root, print_node);
512*7c478bd9Sstevel@tonic-gate }
513