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