2 * Copyright (c) 1994 Christopher G. Demetriou
3 * Copyright (c) 1994 Simon J. Gerraty
4 * Copyright (c) 2012 Ed Schouten <ed@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/queue.h>
47 * this is for our list of currently logged in sessions
50 SLIST_ENTRY(utmpx_entry) next;
51 char user[sizeof(((struct utmpx *)0)->ut_user)];
52 char id[sizeof(((struct utmpx *)0)->ut_id)];
54 char line[sizeof(((struct utmpx *)0)->ut_line)];
60 * this is for our list of users that are accumulating time.
63 SLIST_ENTRY(user_entry) next;
64 char user[sizeof(((struct utmpx *)0)->ut_user)];
69 * this is for chosing whether to ignore a login
72 SLIST_ENTRY(tty_entry) next;
73 char line[sizeof(((struct utmpx *)0)->ut_line) + 2];
82 static const char *Console = CONSOLE_TTY;
84 static struct timeval Total = { 0, 0 };
85 static struct timeval FirstTime = { 0, 0 };
87 static SLIST_HEAD(, utmpx_entry) CurUtmpx = SLIST_HEAD_INITIALIZER(CurUtmpx);
88 static SLIST_HEAD(, user_entry) Users = SLIST_HEAD_INITIALIZER(Users);
89 static SLIST_HEAD(, tty_entry) Ttys = SLIST_HEAD_INITIALIZER(Ttys);
91 #define AC_W 1 /* not _PATH_WTMP */
92 #define AC_D 2 /* daily totals (ignore -p) */
93 #define AC_P 4 /* per-user totals */
94 #define AC_U 8 /* specified users only */
95 #define AC_T 16 /* specified ttys only */
97 static void ac(const char *);
98 static void usage(void);
101 add_tty(const char *line)
103 struct tty_entry *tp;
108 if ((tp = malloc(sizeof(*tp))) == NULL)
109 errx(1, "malloc failed");
110 tp->len = 0; /* full match */
111 tp->ret = 1; /* do if match */
112 if (*line == '!') { /* don't do if match */
116 strlcpy(tp->line, line, sizeof(tp->line));
118 if ((rcp = strchr(tp->line, '*')) != NULL) {
120 /* Match len bytes only. */
121 tp->len = strlen(tp->line);
123 SLIST_INSERT_HEAD(&Ttys, tp, next);
127 * should we process the named tty?
130 do_tty(const char *line)
132 struct tty_entry *tp;
135 SLIST_FOREACH(tp, &Ttys, next) {
136 if (tp->ret == 0) /* specific don't */
137 def_ret = 1; /* default do */
139 if (strncmp(line, tp->line, tp->len) == 0)
142 if (strncmp(line, tp->line, sizeof(tp->line)) == 0)
151 * is someone logged in on Console?
156 struct utmpx_entry *up;
158 SLIST_FOREACH(up, &CurUtmpx, next)
159 if (strcmp(up->line, Console) == 0)
166 * Update user's login time.
167 * If no entry for this user is found, a new entry is inserted into the
168 * list alphabetically.
171 update_user(const char *user, struct timeval secs)
173 struct user_entry *up, *aup;
177 SLIST_FOREACH(up, &Users, next) {
178 c = strcmp(up->user, user);
180 timeradd(&up->time, &secs, &up->time);
181 timeradd(&Total, &secs, &Total);
188 * not found so add new user unless specified users only
193 if ((up = malloc(sizeof(*up))) == NULL)
194 errx(1, "malloc failed");
196 SLIST_INSERT_HEAD(&Users, up, next);
198 SLIST_INSERT_AFTER(aup, up, next);
199 strlcpy(up->user, user, sizeof(up->user));
201 timeradd(&Total, &secs, &Total);
205 main(int argc, char *argv[])
207 const char *wtmpf = NULL;
210 (void) setlocale(LC_TIME, "");
212 while ((c = getopt(argc, argv, "c:dpt:w:")) != -1) {
227 case 't': /* only do specified ttys */
242 * initialize user list
244 for (; optind < argc; optind++) {
245 update_user(argv[optind], (struct timeval){ 0, 0 });
247 Flags |= AC_U; /* freeze user list */
257 * print login time in decimal hours
260 show(const char *user, struct timeval secs)
262 (void)printf("\t%-*s %8.2f\n",
263 (int)sizeof(((struct user_entry *)0)->user), user,
264 (double)secs.tv_sec / 3600);
270 struct user_entry *lp;
272 SLIST_FOREACH(lp, &Users, next)
273 show(lp->user, lp->time);
277 * print total login time for 24hr period in decimal hours
280 show_today(struct timeval today)
282 struct user_entry *up;
283 struct utmpx_entry *lp;
285 struct timeval diff, total = { 0, 0 }, usec = { 0, 1 }, yesterday;
286 static int d_first = -1;
289 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
290 timersub(&today, &usec, &yesterday);
291 (void)strftime(date, sizeof(date),
292 d_first ? "%e %b total" : "%b %e total",
293 localtime(&yesterday.tv_sec));
295 SLIST_FOREACH(lp, &CurUtmpx, next) {
296 timersub(&today, &lp->time, &diff);
297 update_user(lp->user, diff);
298 /* As if they just logged in. */
301 SLIST_FOREACH(up, &Users, next) {
302 timeradd(&total, &up->time, &total);
304 timerclear(&up->time);
306 if (timerisset(&total))
307 (void)printf("%s %11.2f\n", date, (double)total.tv_sec / 3600);
311 * Log a user out and update their times.
312 * If ut_type is BOOT_TIME or SHUTDOWN_TIME, we log all users out as the
313 * system has been shut down.
316 log_out(const struct utmpx *up)
318 struct utmpx_entry *lp, *lp2, *tlp;
321 for (lp = SLIST_FIRST(&CurUtmpx), lp2 = NULL; lp != NULL;)
322 if (up->ut_type == BOOT_TIME || up->ut_type == SHUTDOWN_TIME ||
323 (up->ut_type == DEAD_PROCESS &&
324 memcmp(lp->id, up->ut_id, sizeof(up->ut_id)) == 0)) {
325 timersub(&up->ut_tv, &lp->time, &secs);
326 update_user(lp->user, secs);
331 lp = SLIST_NEXT(lp, next);
333 SLIST_REMOVE_HEAD(&CurUtmpx, next);
335 SLIST_REMOVE_AFTER(lp2, next);
339 lp = SLIST_NEXT(lp, next);
344 * if do_tty says ok, login a user
347 log_in(struct utmpx *up)
349 struct utmpx_entry *lp;
352 * this could be a login. if we're not dealing with
353 * the console name, say it is.
355 * If we are, and if ut_host==":0.0" we know that it
356 * isn't a real login. _But_ if we have not yet recorded
357 * someone being logged in on Console - due to the wtmp
358 * file starting after they logged in, we'll pretend they
359 * logged in, at the start of the wtmp file.
363 if (up->ut_host[0] == ':') {
365 * SunOS 4.0.2 does not treat ":0.0" as special but we
371 * ok, no recorded login, so they were here when wtmp
372 * started! Adjust ut_time!
374 up->ut_tv = FirstTime;
376 * this allows us to pick the right logout
378 strlcpy(up->ut_line, Console, sizeof(up->ut_line));
382 * If we are doing specified ttys only, we ignore
385 if (Flags & AC_T && !do_tty(up->ut_line))
389 * go ahead and log them in
391 if ((lp = malloc(sizeof(*lp))) == NULL)
392 errx(1, "malloc failed");
393 SLIST_INSERT_HEAD(&CurUtmpx, lp, next);
394 strlcpy(lp->user, up->ut_user, sizeof(lp->user));
395 memcpy(lp->id, up->ut_id, sizeof(lp->id));
397 memcpy(lp->line, up->ut_line, sizeof(lp->line));
399 lp->time = up->ut_tv;
405 struct utmpx_entry *lp;
406 struct utmpx *usr, usht;
408 struct timeval prev_secs, ut_timecopy, secs, clock_shift, now;
412 timerclear(&prev_secs); /* Minimum acceptable date == 1970. */
414 timerclear(&clock_shift);
416 if (setutxdb(UTXDB_LOG, file) != 0)
418 while ((usr = getutxent()) != NULL) {
420 ut_timecopy = usr->ut_tv;
421 /* Don't let the time run backwards. */
422 if (timercmp(&ut_timecopy, &prev_secs, <))
423 ut_timecopy = prev_secs;
424 prev_secs = ut_timecopy;
426 if (!timerisset(&FirstTime))
427 FirstTime = ut_timecopy;
429 ltm = localtime(&ut_timecopy.tv_sec);
430 if (day >= 0 && day != ltm->tm_yday) {
433 * print yesterday's total
436 secs.tv_sec -= ltm->tm_sec;
437 secs.tv_sec -= 60 * ltm->tm_min;
438 secs.tv_sec -= 3600 * ltm->tm_hour;
444 switch(usr->ut_type) {
446 clock_shift = ut_timecopy;
449 timersub(&clock_shift, &ut_timecopy, &clock_shift);
451 * adjust time for those logged in
453 SLIST_FOREACH(lp, &CurUtmpx, next)
454 timersub(&lp->time, &clock_shift, &lp->time);
459 FirstTime = ut_timecopy; /* shouldn't be needed */
463 * If they came in on pts/..., then it is only
464 * a login session if the ut_host field is non-empty.
466 if (strncmp(usr->ut_line, "pts/", 4) != 0 ||
467 *usr->ut_host != '\0')
476 (void)gettimeofday(&now, NULL);
478 usht.ut_tv = ut_timecopy;
481 usht.ut_type = SHUTDOWN_TIME;
484 ltm = localtime(&ut_timecopy.tv_sec);
485 if (day >= 0 && day != ltm->tm_yday) {
487 * print yesterday's total
490 secs.tv_sec -= ltm->tm_sec;
491 secs.tv_sec -= 60 * ltm->tm_min;
492 secs.tv_sec -= 3600 * ltm->tm_hour;
498 * anyone still logged in gets time up to now
507 show("total", Total);
514 (void)fprintf(stderr,
516 "ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n");
518 "ac [-dp] [-t tty] [-w wtmp] [users ...]\n");