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