2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1994 Christopher G. Demetriou
5 * Copyright (c) 1994 Simon J. Gerraty
6 * Copyright (c) 2012 Ed Schouten <ed@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/queue.h>
49 * this is for our list of currently logged in sessions
52 SLIST_ENTRY(utmpx_entry) next;
53 char user[sizeof(((struct utmpx *)0)->ut_user)];
54 char id[sizeof(((struct utmpx *)0)->ut_id)];
56 char line[sizeof(((struct utmpx *)0)->ut_line)];
62 * this is for our list of users that are accumulating time.
65 SLIST_ENTRY(user_entry) next;
66 char user[sizeof(((struct utmpx *)0)->ut_user)];
71 * this is for chosing whether to ignore a login
74 SLIST_ENTRY(tty_entry) next;
75 char line[sizeof(((struct utmpx *)0)->ut_line) + 2];
84 static const char *Console = CONSOLE_TTY;
86 static struct timeval Total = { 0, 0 };
87 static struct timeval FirstTime = { 0, 0 };
89 static SLIST_HEAD(, utmpx_entry) CurUtmpx = SLIST_HEAD_INITIALIZER(CurUtmpx);
90 static SLIST_HEAD(, user_entry) Users = SLIST_HEAD_INITIALIZER(Users);
91 static SLIST_HEAD(, tty_entry) Ttys = SLIST_HEAD_INITIALIZER(Ttys);
93 #define AC_W 1 /* not _PATH_WTMP */
94 #define AC_D 2 /* daily totals (ignore -p) */
95 #define AC_P 4 /* per-user totals */
96 #define AC_U 8 /* specified users only */
97 #define AC_T 16 /* specified ttys only */
99 static void ac(const char *);
100 static void usage(void);
103 add_tty(const char *line)
105 struct tty_entry *tp;
110 if ((tp = malloc(sizeof(*tp))) == NULL)
111 errx(1, "malloc failed");
112 tp->len = 0; /* full match */
113 tp->ret = 1; /* do if match */
114 if (*line == '!') { /* don't do if match */
118 strlcpy(tp->line, line, sizeof(tp->line));
120 if ((rcp = strchr(tp->line, '*')) != NULL) {
122 /* Match len bytes only. */
123 tp->len = strlen(tp->line);
125 SLIST_INSERT_HEAD(&Ttys, tp, next);
129 * should we process the named tty?
132 do_tty(const char *line)
134 struct tty_entry *tp;
137 SLIST_FOREACH(tp, &Ttys, next) {
138 if (tp->ret == 0) /* specific don't */
139 def_ret = 1; /* default do */
141 if (strncmp(line, tp->line, tp->len) == 0)
144 if (strncmp(line, tp->line, sizeof(tp->line)) == 0)
153 * is someone logged in on Console?
158 struct utmpx_entry *up;
160 SLIST_FOREACH(up, &CurUtmpx, next)
161 if (strcmp(up->line, Console) == 0)
168 * Update user's login time.
169 * If no entry for this user is found, a new entry is inserted into the
170 * list alphabetically.
173 update_user(const char *user, struct timeval secs)
175 struct user_entry *up, *aup;
179 SLIST_FOREACH(up, &Users, next) {
180 c = strcmp(up->user, user);
182 timeradd(&up->time, &secs, &up->time);
183 timeradd(&Total, &secs, &Total);
190 * not found so add new user unless specified users only
195 if ((up = malloc(sizeof(*up))) == NULL)
196 errx(1, "malloc failed");
198 SLIST_INSERT_HEAD(&Users, up, next);
200 SLIST_INSERT_AFTER(aup, up, next);
201 strlcpy(up->user, user, sizeof(up->user));
203 timeradd(&Total, &secs, &Total);
207 main(int argc, char *argv[])
209 const char *wtmpf = NULL;
212 (void) setlocale(LC_TIME, "");
214 while ((c = getopt(argc, argv, "c:dpt:w:")) != -1) {
229 case 't': /* only do specified ttys */
244 * initialize user list
246 for (; optind < argc; optind++) {
247 update_user(argv[optind], (struct timeval){ 0, 0 });
249 Flags |= AC_U; /* freeze user list */
259 * print login time in decimal hours
262 show(const char *user, struct timeval secs)
264 (void)printf("\t%-*s %8.2f\n",
265 (int)sizeof(((struct user_entry *)0)->user), user,
266 (double)secs.tv_sec / 3600);
272 struct user_entry *lp;
274 SLIST_FOREACH(lp, &Users, next)
275 show(lp->user, lp->time);
279 * print total login time for 24hr period in decimal hours
282 show_today(struct timeval today)
284 struct user_entry *up;
285 struct utmpx_entry *lp;
287 struct timeval diff, total = { 0, 0 }, usec = { 0, 1 }, yesterday;
288 static int d_first = -1;
291 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
292 timersub(&today, &usec, &yesterday);
293 (void)strftime(date, sizeof(date),
294 d_first ? "%e %b total" : "%b %e total",
295 localtime(&yesterday.tv_sec));
297 SLIST_FOREACH(lp, &CurUtmpx, next) {
298 timersub(&today, &lp->time, &diff);
299 update_user(lp->user, diff);
300 /* As if they just logged in. */
303 SLIST_FOREACH(up, &Users, next) {
304 timeradd(&total, &up->time, &total);
306 timerclear(&up->time);
308 if (timerisset(&total))
309 (void)printf("%s %11.2f\n", date, (double)total.tv_sec / 3600);
313 * Log a user out and update their times.
314 * If ut_type is BOOT_TIME or SHUTDOWN_TIME, we log all users out as the
315 * system has been shut down.
318 log_out(const struct utmpx *up)
320 struct utmpx_entry *lp, *lp2, *tlp;
323 for (lp = SLIST_FIRST(&CurUtmpx), lp2 = NULL; lp != NULL;)
324 if (up->ut_type == BOOT_TIME || up->ut_type == SHUTDOWN_TIME ||
325 (up->ut_type == DEAD_PROCESS &&
326 memcmp(lp->id, up->ut_id, sizeof(up->ut_id)) == 0)) {
327 timersub(&up->ut_tv, &lp->time, &secs);
328 update_user(lp->user, secs);
333 lp = SLIST_NEXT(lp, next);
335 SLIST_REMOVE_HEAD(&CurUtmpx, next);
337 SLIST_REMOVE_AFTER(lp2, next);
341 lp = SLIST_NEXT(lp, next);
346 * if do_tty says ok, login a user
349 log_in(struct utmpx *up)
351 struct utmpx_entry *lp;
354 * this could be a login. if we're not dealing with
355 * the console name, say it is.
357 * If we are, and if ut_host==":0.0" we know that it
358 * isn't a real login. _But_ if we have not yet recorded
359 * someone being logged in on Console - due to the wtmp
360 * file starting after they logged in, we'll pretend they
361 * logged in, at the start of the wtmp file.
365 if (up->ut_host[0] == ':') {
367 * SunOS 4.0.2 does not treat ":0.0" as special but we
373 * ok, no recorded login, so they were here when wtmp
374 * started! Adjust ut_time!
376 up->ut_tv = FirstTime;
378 * this allows us to pick the right logout
380 strlcpy(up->ut_line, Console, sizeof(up->ut_line));
384 * If we are doing specified ttys only, we ignore
387 if (Flags & AC_T && !do_tty(up->ut_line))
391 * go ahead and log them in
393 if ((lp = malloc(sizeof(*lp))) == NULL)
394 errx(1, "malloc failed");
395 SLIST_INSERT_HEAD(&CurUtmpx, lp, next);
396 strlcpy(lp->user, up->ut_user, sizeof(lp->user));
397 memcpy(lp->id, up->ut_id, sizeof(lp->id));
399 memcpy(lp->line, up->ut_line, sizeof(lp->line));
401 lp->time = up->ut_tv;
407 struct utmpx_entry *lp;
408 struct utmpx *usr, usht;
410 struct timeval prev_secs, ut_timecopy, secs, clock_shift, now;
414 timerclear(&prev_secs); /* Minimum acceptable date == 1970. */
416 timerclear(&clock_shift);
418 if (setutxdb(UTXDB_LOG, file) != 0)
420 while ((usr = getutxent()) != NULL) {
422 ut_timecopy = usr->ut_tv;
423 /* Don't let the time run backwards. */
424 if (timercmp(&ut_timecopy, &prev_secs, <))
425 ut_timecopy = prev_secs;
426 prev_secs = ut_timecopy;
428 if (!timerisset(&FirstTime))
429 FirstTime = ut_timecopy;
431 ltm = localtime(&ut_timecopy.tv_sec);
432 if (day >= 0 && day != ltm->tm_yday) {
435 * print yesterday's total
438 secs.tv_sec -= ltm->tm_sec;
439 secs.tv_sec -= 60 * ltm->tm_min;
440 secs.tv_sec -= 3600 * ltm->tm_hour;
446 switch(usr->ut_type) {
448 clock_shift = ut_timecopy;
451 timersub(&clock_shift, &ut_timecopy, &clock_shift);
453 * adjust time for those logged in
455 SLIST_FOREACH(lp, &CurUtmpx, next)
456 timersub(&lp->time, &clock_shift, &lp->time);
461 FirstTime = ut_timecopy; /* shouldn't be needed */
465 * If they came in on pts/..., then it is only
466 * a login session if the ut_host field is non-empty.
468 if (strncmp(usr->ut_line, "pts/", 4) != 0 ||
469 *usr->ut_host != '\0')
478 (void)gettimeofday(&now, NULL);
480 usht.ut_tv = ut_timecopy;
483 usht.ut_type = SHUTDOWN_TIME;
486 ltm = localtime(&ut_timecopy.tv_sec);
487 if (day >= 0 && day != ltm->tm_yday) {
489 * print yesterday's total
492 secs.tv_sec -= ltm->tm_sec;
493 secs.tv_sec -= 60 * ltm->tm_min;
494 secs.tv_sec -= 3600 * ltm->tm_hour;
500 * anyone still logged in gets time up to now
509 show("total", Total);
516 (void)fprintf(stderr,
518 "ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n");
520 "ac [-dp] [-t tty] [-w wtmp] [users ...]\n");