ac.c revision 21673
1/*
2 *      Copyright (c) 1994 Christopher G. Demetriou.
3 *      @(#)Copyright (c) 1994, Simon J. Gerraty.
4 *
5 *      This is free software.  It comes with NO WARRANTY.
6 *      Permission to use, modify and distribute this source code
7 *      is granted subject to the following conditions.
8 *      1/ that the above copyright notice and this notice
9 *      are preserved in all copies and that due credit be given
10 *      to the author.
11 *      2/ that any changes to this code are clearly commented
12 *      as such so that the author does not get blamed for bugs
13 *      other than his own.
14 */
15
16#ifndef lint
17static char rcsid[] = "$FreeBSD: head/usr.sbin/ac/ac.c 21673 1997-01-14 07:20:47Z jkh $";
18#endif
19
20#include <sys/types.h>
21#include <sys/file.h>
22#include <sys/time.h>
23#include <err.h>
24#include <errno.h>
25#include <pwd.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <utmp.h>
30#include <unistd.h>
31#include <locale.h>
32
33/*
34 * this is for our list of currently logged in sessions
35 */
36struct utmp_list {
37	struct utmp_list *next;
38	struct utmp usr;
39};
40
41/*
42 * this is for our list of users that are accumulating time.
43 */
44struct user_list {
45	struct user_list *next;
46	char	name[UT_NAMESIZE+1];
47	time_t	secs;
48};
49
50/*
51 * this is for chosing whether to ignore a login
52 */
53struct tty_list {
54	struct tty_list *next;
55	char	name[UT_LINESIZE+3];
56	int	len;
57	int	ret;
58};
59
60/*
61 * globals - yes yuk
62 */
63#ifdef CONSOLE_TTY
64static char 	*Console = CONSOLE_TTY;
65#endif
66static time_t	Total = 0;
67static time_t	FirstTime = 0;
68static int	Flags = 0;
69static struct user_list *Users = NULL;
70static struct tty_list *Ttys = NULL;
71
72#define NEW(type) (type *)malloc(sizeof (type))
73
74#define	AC_W	1				/* not _PATH_WTMP */
75#define	AC_D	2				/* daily totals (ignore -p) */
76#define	AC_P	4				/* per-user totals */
77#define	AC_U	8				/* specified users only */
78#define	AC_T	16				/* specified ttys only */
79
80#ifdef DEBUG
81static int Debug = 0;
82#endif
83
84int			main __P((int, char **));
85int			ac __P((FILE *));
86struct tty_list		*add_tty __P((char *));
87int			do_tty __P((char *));
88FILE			*file __P((char *));
89struct utmp_list	*log_in __P((struct utmp_list *, struct utmp *));
90struct utmp_list	*log_out __P((struct utmp_list *, struct utmp *));
91int			on_console __P((struct utmp_list *));
92void			show __P((char *, time_t));
93void			show_today __P((struct user_list *, struct utmp_list *,
94			    time_t));
95void			show_users __P((struct user_list *));
96struct user_list	*update_user __P((struct user_list *, char *, time_t));
97void			usage __P((void));
98
99/*
100 * open wtmp or die
101 */
102FILE *
103file(name)
104	char *name;
105{
106	FILE *fp;
107
108	if ((fp = fopen(name, "r")) == NULL)
109		err(1, "%s", name);
110	/* in case we want to discriminate */
111	if (strcmp(_PATH_WTMP, name))
112		Flags |= AC_W;
113	return fp;
114}
115
116struct tty_list *
117add_tty(name)
118	char *name;
119{
120	struct tty_list *tp;
121	register char *rcp;
122
123	Flags |= AC_T;
124
125	if ((tp = NEW(struct tty_list)) == NULL)
126		err(1, "malloc");
127	tp->len = 0;				/* full match */
128	tp->ret = 1;				/* do if match */
129	if (*name == '!') {			/* don't do if match */
130		tp->ret = 0;
131		name++;
132	}
133	(void)strncpy(tp->name, name, sizeof (tp->name) - 1);
134	tp->name[sizeof (tp->name) - 1] = '\0';
135	if ((rcp = strchr(tp->name, '*')) != NULL) {	/* wild card */
136		*rcp = '\0';
137		tp->len = strlen(tp->name);	/* match len bytes only */
138	}
139	tp->next = Ttys;
140	Ttys = tp;
141	return Ttys;
142}
143
144/*
145 * should we process the named tty?
146 */
147int
148do_tty(name)
149	char *name;
150{
151	struct tty_list *tp;
152	int def_ret = 0;
153
154	for (tp = Ttys; tp != NULL; tp = tp->next) {
155		if (tp->ret == 0)		/* specific don't */
156			def_ret = 1;		/* default do */
157		if (tp->len != 0) {
158			if (strncmp(name, tp->name, tp->len) == 0)
159				return tp->ret;
160		} else {
161			if (strncmp(name, tp->name, sizeof (tp->name)) == 0)
162				return tp->ret;
163		}
164	}
165	return def_ret;
166}
167
168#ifdef CONSOLE_TTY
169/*
170 * is someone logged in on Console?
171 */
172int
173on_console(head)
174	struct utmp_list *head;
175{
176	struct utmp_list *up;
177
178	for (up = head; up; up = up->next) {
179		if (strncmp(up->usr.ut_line, Console,
180		    sizeof (up->usr.ut_line)) == 0)
181			return 1;
182	}
183	return 0;
184}
185#endif
186
187/*
188 * update user's login time
189 */
190struct user_list *
191update_user(head, name, secs)
192	struct user_list *head;
193	char	*name;
194	time_t	secs;
195{
196	struct user_list *up;
197
198	for (up = head; up != NULL; up = up->next) {
199		if (strncmp(up->name, name, UT_NAMESIZE) == 0) {
200			up->secs += secs;
201			Total += secs;
202			return head;
203		}
204	}
205	/*
206	 * not found so add new user unless specified users only
207	 */
208	if (Flags & AC_U)
209		return head;
210
211	if ((up = NEW(struct user_list)) == NULL)
212		err(1, "malloc");
213	up->next = head;
214	(void)strncpy(up->name, name, sizeof (up->name) - 1);
215	up->name[sizeof (up->name) - 1] = '\0';	/* paranoid! */
216	up->secs = secs;
217	Total += secs;
218	return up;
219}
220
221int
222main(argc, argv)
223	int	argc;
224	char	**argv;
225{
226	FILE *fp;
227	int c;
228
229	(void) setlocale(LC_TIME, "");
230
231	fp = NULL;
232	while ((c = getopt(argc, argv, "Dc:dpt:w:")) != EOF) {
233		switch (c) {
234#ifdef DEBUG
235		case 'D':
236			Debug++;
237			break;
238#endif
239		case 'c':
240#ifdef CONSOLE_TTY
241			Console = optarg;
242#else
243			usage();		/* XXX */
244#endif
245			break;
246		case 'd':
247			Flags |= AC_D;
248			break;
249		case 'p':
250			Flags |= AC_P;
251			break;
252		case 't':			/* only do specified ttys */
253			add_tty(optarg);
254			break;
255		case 'w':
256			fp = file(optarg);
257			break;
258		case '?':
259		default:
260			usage();
261			break;
262		}
263	}
264	if (optind < argc) {
265		/*
266		 * initialize user list
267		 */
268		for (; optind < argc; optind++) {
269			Users = update_user(Users, argv[optind], 0L);
270		}
271		Flags |= AC_U;			/* freeze user list */
272	}
273	if (Flags & AC_D)
274		Flags &= ~AC_P;
275	if (fp == NULL) {
276		/*
277		 * if _PATH_WTMP does not exist, exit quietly
278		 */
279		if (access(_PATH_WTMP, 0) != 0 && errno == ENOENT)
280			return 0;
281
282		fp = file(_PATH_WTMP);
283	}
284	ac(fp);
285
286	return 0;
287}
288
289/*
290 * print login time in decimal hours
291 */
292void
293show(name, secs)
294	char *name;
295	time_t secs;
296{
297	(void)printf("\t%-*s %8.2f\n", UT_NAMESIZE, name,
298	    ((double)secs / 3600));
299}
300
301void
302show_users(list)
303	struct user_list *list;
304{
305	struct user_list *lp;
306
307	for (lp = list; lp; lp = lp->next)
308		show(lp->name, lp->secs);
309}
310
311/*
312 * print total login time for 24hr period in decimal hours
313 */
314void
315show_today(users, logins, secs)
316	struct user_list *users;
317	struct utmp_list *logins;
318	time_t secs;
319{
320	struct user_list *up;
321	struct utmp_list *lp;
322	char date[64];
323	time_t yesterday = secs - 1;
324
325	(void)strftime(date, sizeof (date), "%b %e  total",
326	    localtime(&yesterday));
327
328	/* restore the missing second */
329	yesterday++;
330
331	for (lp = logins; lp != NULL; lp = lp->next) {
332		secs = yesterday - lp->usr.ut_time;
333		Users = update_user(Users, lp->usr.ut_name, secs);
334		lp->usr.ut_time = yesterday;	/* as if they just logged in */
335	}
336	secs = 0;
337	for (up = users; up != NULL; up = up->next) {
338		secs += up->secs;
339		up->secs = 0;			/* for next day */
340	}
341 	if (secs)
342		(void)printf("%s %11.2f\n", date, ((double)secs / 3600));
343}
344
345/*
346 * log a user out and update their times.
347 * if ut_line is "~", we log all users out as the system has
348 * been shut down.
349 */
350struct utmp_list *
351log_out(head, up)
352	struct utmp_list *head;
353	struct utmp *up;
354{
355	struct utmp_list *lp, *lp2, *tlp;
356	time_t secs;
357
358	for (lp = head, lp2 = NULL; lp != NULL; )
359		if (*up->ut_line == '~' || strncmp(lp->usr.ut_line, up->ut_line,
360		    sizeof (up->ut_line)) == 0) {
361			secs = up->ut_time - lp->usr.ut_time;
362			Users = update_user(Users, lp->usr.ut_name, secs);
363#ifdef DEBUG
364			if (Debug)
365				printf("%-.*s %-.*s: %-.*s logged out (%2d:%02d:%02d)\n",
366				    19, ctime(&up->ut_time),
367				    sizeof (lp->usr.ut_line), lp->usr.ut_line,
368				    sizeof (lp->usr.ut_name), lp->usr.ut_name,
369				    secs / 3600, (secs % 3600) / 60, secs % 60);
370#endif
371			/*
372			 * now lose it
373			 */
374			tlp = lp;
375			lp = lp->next;
376			if (tlp == head)
377				head = lp;
378			else if (lp2 != NULL)
379				lp2->next = lp;
380			free(tlp);
381		} else {
382			lp2 = lp;
383			lp = lp->next;
384		}
385	return head;
386}
387
388
389/*
390 * if do_tty says ok, login a user
391 */
392struct utmp_list *
393log_in(head, up)
394	struct utmp_list *head;
395	struct utmp *up;
396{
397	struct utmp_list *lp;
398
399	/*
400	 * this could be a login. if we're not dealing with
401	 * the console name, say it is.
402	 *
403	 * If we are, and if ut_host==":0.0" we know that it
404	 * isn't a real login. _But_ if we have not yet recorded
405	 * someone being logged in on Console - due to the wtmp
406	 * file starting after they logged in, we'll pretend they
407	 * logged in, at the start of the wtmp file.
408	 */
409
410#ifdef CONSOLE_TTY
411	if (up->ut_host[0] == ':') {
412		/*
413		 * SunOS 4.0.2 does not treat ":0.0" as special but we
414		 * do.
415		 */
416		if (on_console(head))
417			return head;
418		/*
419		 * ok, no recorded login, so they were here when wtmp
420		 * started!  Adjust ut_time!
421		 */
422		up->ut_time = FirstTime;
423		/*
424		 * this allows us to pick the right logout
425		 */
426		(void)strncpy(up->ut_line, Console, sizeof (up->ut_line) - 1);
427		up->ut_line[sizeof (up->ut_line) - 1] = '\0'; /* paranoid! */
428	}
429#endif
430	/*
431	 * If we are doing specified ttys only, we ignore
432	 * anything else.
433	 */
434	if (Flags & AC_T)
435		if (!do_tty(up->ut_line))
436			return head;
437
438	/*
439	 * go ahead and log them in
440	 */
441	if ((lp = NEW(struct utmp_list)) == NULL)
442		err(1, "malloc");
443	lp->next = head;
444	head = lp;
445	memmove((char *)&lp->usr, (char *)up, sizeof (struct utmp));
446#ifdef DEBUG
447	if (Debug) {
448		printf("%-.*s %-.*s: %-.*s logged in", 19,
449		    ctime(&lp->usr.ut_time), sizeof (up->ut_line),
450		       up->ut_line, sizeof (up->ut_name), up->ut_name);
451		if (*up->ut_host)
452			printf(" (%-.*s)", sizeof (up->ut_host), up->ut_host);
453		putchar('\n');
454	}
455#endif
456	return head;
457}
458
459int
460ac(fp)
461	FILE	*fp;
462{
463	struct utmp_list *lp, *head = NULL;
464	struct utmp usr;
465	struct tm *ltm;
466	time_t secs;
467	int day = -1;
468
469	while (fread((char *)&usr, sizeof(usr), 1, fp) == 1) {
470		if (!FirstTime)
471			FirstTime = usr.ut_time;
472		if (Flags & AC_D) {
473			ltm = localtime(&usr.ut_time);
474			if (day >= 0 && day != ltm->tm_yday) {
475				day = ltm->tm_yday;
476				/*
477				 * print yesterday's total
478				 */
479				secs = usr.ut_time;
480				secs -= ltm->tm_sec;
481				secs -= 60 * ltm->tm_min;
482				secs -= 3600 * ltm->tm_hour;
483				show_today(Users, head, secs);
484			} else
485				day = ltm->tm_yday;
486		}
487		switch(*usr.ut_line) {
488		case '|':
489			secs = usr.ut_time;
490			break;
491		case '{':
492			secs -= usr.ut_time;
493			/*
494			 * adjust time for those logged in
495			 */
496			for (lp = head; lp != NULL; lp = lp->next)
497				lp->usr.ut_time -= secs;
498			break;
499		case '~':			/* reboot or shutdown */
500			head = log_out(head, &usr);
501			FirstTime = usr.ut_time; /* shouldn't be needed */
502			break;
503		default:
504			/*
505			 * if they came in on tty[p-y]*, then it is only
506			 * a login session if the ut_host field is non-empty
507			 */
508			if (*usr.ut_name) {
509				if (strncmp(usr.ut_line, "tty", 3) != 0 ||
510				    strchr("pqrstuvwxy", usr.ut_line[3]) == 0 ||
511				    *usr.ut_host != '\0')
512					head = log_in(head, &usr);
513			} else
514				head = log_out(head, &usr);
515			break;
516		}
517	}
518	(void)fclose(fp);
519	usr.ut_time = time((time_t *)0);
520	(void)strcpy(usr.ut_line, "~");
521
522	if (Flags & AC_D) {
523		ltm = localtime(&usr.ut_time);
524		if (day >= 0 && day != ltm->tm_yday) {
525			/*
526			 * print yesterday's total
527			 */
528			secs = usr.ut_time;
529			secs -= ltm->tm_sec;
530			secs -= 60 * ltm->tm_min;
531			secs -= 3600 * ltm->tm_hour;
532			show_today(Users, head, secs);
533		}
534	}
535	/*
536	 * anyone still logged in gets time up to now
537	 */
538	head = log_out(head, &usr);
539
540	if (Flags & AC_D)
541		show_today(Users, head, time((time_t *)0));
542	else {
543		if (Flags & AC_P)
544			show_users(Users);
545		show("total", Total);
546	}
547	return 0;
548}
549
550void
551usage()
552{
553	(void)fprintf(stderr,
554#ifdef CONSOLE_TTY
555	    "ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n");
556#else
557	    "ac [-dp] [-t tty] [-w wtmp] [users ...]\n");
558#endif
559	exit(1);
560}
561