18f67c5bcSEd Schouten /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
48f67c5bcSEd Schouten * Copyright (c) 1994 Christopher G. Demetriou
58f67c5bcSEd Schouten * Copyright (c) 1994 Simon J. Gerraty
68f67c5bcSEd Schouten * Copyright (c) 2012 Ed Schouten <ed@FreeBSD.org>
78f67c5bcSEd Schouten * All rights reserved.
8ff9c3a32SDavid Greenman *
98f67c5bcSEd Schouten * Redistribution and use in source and binary forms, with or without
108f67c5bcSEd Schouten * modification, are permitted provided that the following conditions
118f67c5bcSEd Schouten * are met:
128f67c5bcSEd Schouten * 1. Redistributions of source code must retain the above copyright
138f67c5bcSEd Schouten * notice, this list of conditions and the following disclaimer.
148f67c5bcSEd Schouten * 2. Redistributions in binary form must reproduce the above copyright
158f67c5bcSEd Schouten * notice, this list of conditions and the following disclaimer in the
168f67c5bcSEd Schouten * documentation and/or other materials provided with the distribution.
178f67c5bcSEd Schouten *
188f67c5bcSEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
198f67c5bcSEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
208f67c5bcSEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
218f67c5bcSEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
228f67c5bcSEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
238f67c5bcSEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
248f67c5bcSEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
258f67c5bcSEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
268f67c5bcSEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
278f67c5bcSEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
288f67c5bcSEd Schouten * SUCH DAMAGE.
29ff9c3a32SDavid Greenman */
30ff9c3a32SDavid Greenman
31b51547cfSPhilippe Charnier #include <sys/cdefs.h>
32b42c08d3SEd Schouten #include <sys/queue.h>
33ff9c3a32SDavid Greenman #include <sys/time.h>
34b42c08d3SEd Schouten
35ff9c3a32SDavid Greenman #include <err.h>
36ff9c3a32SDavid Greenman #include <errno.h>
37497c7558SAndrey A. Chernov #include <langinfo.h>
383efa2f58SPhilippe Charnier #include <locale.h>
39ff9c3a32SDavid Greenman #include <stdio.h>
40ff9c3a32SDavid Greenman #include <stdlib.h>
41ff9c3a32SDavid Greenman #include <string.h>
42db774d60SBruce Evans #include <timeconv.h>
43ff9c3a32SDavid Greenman #include <unistd.h>
44cf4d4e15SEd Schouten #include <utmpx.h>
45ff9c3a32SDavid Greenman
46ff9c3a32SDavid Greenman /*
47ff9c3a32SDavid Greenman * this is for our list of currently logged in sessions
48ff9c3a32SDavid Greenman */
49b42c08d3SEd Schouten struct utmpx_entry {
50b42c08d3SEd Schouten SLIST_ENTRY(utmpx_entry) next;
51b42c08d3SEd Schouten char user[sizeof(((struct utmpx *)0)->ut_user)];
52b42c08d3SEd Schouten char id[sizeof(((struct utmpx *)0)->ut_id)];
53b42c08d3SEd Schouten #ifdef CONSOLE_TTY
54b42c08d3SEd Schouten char line[sizeof(((struct utmpx *)0)->ut_line)];
55b42c08d3SEd Schouten #endif
56b42c08d3SEd Schouten struct timeval time;
57ff9c3a32SDavid Greenman };
58ff9c3a32SDavid Greenman
59ff9c3a32SDavid Greenman /*
60ff9c3a32SDavid Greenman * this is for our list of users that are accumulating time.
61ff9c3a32SDavid Greenman */
62b42c08d3SEd Schouten struct user_entry {
63b42c08d3SEd Schouten SLIST_ENTRY(user_entry) next;
64b42c08d3SEd Schouten char user[sizeof(((struct utmpx *)0)->ut_user)];
65b42c08d3SEd Schouten struct timeval time;
66ff9c3a32SDavid Greenman };
67ff9c3a32SDavid Greenman
68ff9c3a32SDavid Greenman /*
6910924c4fSGordon Bergling * this is for choosing whether to ignore a login
70ff9c3a32SDavid Greenman */
71b42c08d3SEd Schouten struct tty_entry {
72b42c08d3SEd Schouten SLIST_ENTRY(tty_entry) next;
73b42c08d3SEd Schouten char line[sizeof(((struct utmpx *)0)->ut_line) + 2];
74b51547cfSPhilippe Charnier size_t len;
75ff9c3a32SDavid Greenman int ret;
76ff9c3a32SDavid Greenman };
77ff9c3a32SDavid Greenman
78ff9c3a32SDavid Greenman /*
79ff9c3a32SDavid Greenman * globals - yes yuk
80ff9c3a32SDavid Greenman */
81ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY
82b42c08d3SEd Schouten static const char *Console = CONSOLE_TTY;
83ff9c3a32SDavid Greenman #endif
84b42c08d3SEd Schouten static struct timeval Total = { 0, 0 };
85b42c08d3SEd Schouten static struct timeval FirstTime = { 0, 0 };
86ff9c3a32SDavid Greenman static int Flags = 0;
87b42c08d3SEd Schouten static SLIST_HEAD(, utmpx_entry) CurUtmpx = SLIST_HEAD_INITIALIZER(CurUtmpx);
88b42c08d3SEd Schouten static SLIST_HEAD(, user_entry) Users = SLIST_HEAD_INITIALIZER(Users);
89b42c08d3SEd Schouten static SLIST_HEAD(, tty_entry) Ttys = SLIST_HEAD_INITIALIZER(Ttys);
90ff9c3a32SDavid Greenman
91ff9c3a32SDavid Greenman #define AC_W 1 /* not _PATH_WTMP */
92ff9c3a32SDavid Greenman #define AC_D 2 /* daily totals (ignore -p) */
93ff9c3a32SDavid Greenman #define AC_P 4 /* per-user totals */
94ff9c3a32SDavid Greenman #define AC_U 8 /* specified users only */
95ff9c3a32SDavid Greenman #define AC_T 16 /* specified ttys only */
96ff9c3a32SDavid Greenman
97b42c08d3SEd Schouten static void ac(const char *);
98b42c08d3SEd Schouten static void usage(void);
99ff9c3a32SDavid Greenman
100b42c08d3SEd Schouten static void
add_tty(const char * line)101b42c08d3SEd Schouten add_tty(const char *line)
102ff9c3a32SDavid Greenman {
103b42c08d3SEd Schouten struct tty_entry *tp;
1042adaffb0SGarance A Drosehn char *rcp;
105ff9c3a32SDavid Greenman
106ff9c3a32SDavid Greenman Flags |= AC_T;
107ff9c3a32SDavid Greenman
108b42c08d3SEd Schouten if ((tp = malloc(sizeof(*tp))) == NULL)
109c3737d34SPhilippe Charnier errx(1, "malloc failed");
110ff9c3a32SDavid Greenman tp->len = 0; /* full match */
111ff9c3a32SDavid Greenman tp->ret = 1; /* do if match */
112b42c08d3SEd Schouten if (*line == '!') { /* don't do if match */
113ff9c3a32SDavid Greenman tp->ret = 0;
114b42c08d3SEd Schouten line++;
115ff9c3a32SDavid Greenman }
116b42c08d3SEd Schouten strlcpy(tp->line, line, sizeof(tp->line));
117b42c08d3SEd Schouten /* Wildcard. */
118b42c08d3SEd Schouten if ((rcp = strchr(tp->line, '*')) != NULL) {
119ff9c3a32SDavid Greenman *rcp = '\0';
120b42c08d3SEd Schouten /* Match len bytes only. */
121b42c08d3SEd Schouten tp->len = strlen(tp->line);
122ff9c3a32SDavid Greenman }
123b42c08d3SEd Schouten SLIST_INSERT_HEAD(&Ttys, tp, next);
124ff9c3a32SDavid Greenman }
125ff9c3a32SDavid Greenman
126ff9c3a32SDavid Greenman /*
127ff9c3a32SDavid Greenman * should we process the named tty?
128ff9c3a32SDavid Greenman */
129b42c08d3SEd Schouten static int
do_tty(const char * line)130b42c08d3SEd Schouten do_tty(const char *line)
131ff9c3a32SDavid Greenman {
132b42c08d3SEd Schouten struct tty_entry *tp;
133ff9c3a32SDavid Greenman int def_ret = 0;
134ff9c3a32SDavid Greenman
135b42c08d3SEd Schouten SLIST_FOREACH(tp, &Ttys, next) {
136ff9c3a32SDavid Greenman if (tp->ret == 0) /* specific don't */
137ff9c3a32SDavid Greenman def_ret = 1; /* default do */
138ff9c3a32SDavid Greenman if (tp->len != 0) {
139b42c08d3SEd Schouten if (strncmp(line, tp->line, tp->len) == 0)
140ff9c3a32SDavid Greenman return tp->ret;
141ff9c3a32SDavid Greenman } else {
142b42c08d3SEd Schouten if (strncmp(line, tp->line, sizeof(tp->line)) == 0)
143ff9c3a32SDavid Greenman return tp->ret;
144ff9c3a32SDavid Greenman }
145ff9c3a32SDavid Greenman }
146b42c08d3SEd Schouten return (def_ret);
147ff9c3a32SDavid Greenman }
148ff9c3a32SDavid Greenman
149ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY
150ff9c3a32SDavid Greenman /*
151ff9c3a32SDavid Greenman * is someone logged in on Console?
152ff9c3a32SDavid Greenman */
153b42c08d3SEd Schouten static int
on_console(void)154b42c08d3SEd Schouten on_console(void)
155ff9c3a32SDavid Greenman {
156b42c08d3SEd Schouten struct utmpx_entry *up;
157ff9c3a32SDavid Greenman
158b42c08d3SEd Schouten SLIST_FOREACH(up, &CurUtmpx, next)
159b42c08d3SEd Schouten if (strcmp(up->line, Console) == 0)
160b42c08d3SEd Schouten return (1);
161b42c08d3SEd Schouten return (0);
162ff9c3a32SDavid Greenman }
163ff9c3a32SDavid Greenman #endif
164ff9c3a32SDavid Greenman
165ff9c3a32SDavid Greenman /*
166b42c08d3SEd Schouten * Update user's login time.
167b42c08d3SEd Schouten * If no entry for this user is found, a new entry is inserted into the
168b42c08d3SEd Schouten * list alphabetically.
169ff9c3a32SDavid Greenman */
170b42c08d3SEd Schouten static void
update_user(const char * user,struct timeval secs)171b42c08d3SEd Schouten update_user(const char *user, struct timeval secs)
172ff9c3a32SDavid Greenman {
173b42c08d3SEd Schouten struct user_entry *up, *aup;
174b42c08d3SEd Schouten int c;
175ff9c3a32SDavid Greenman
176b42c08d3SEd Schouten aup = NULL;
177b42c08d3SEd Schouten SLIST_FOREACH(up, &Users, next) {
178b42c08d3SEd Schouten c = strcmp(up->user, user);
179b42c08d3SEd Schouten if (c == 0) {
180b42c08d3SEd Schouten timeradd(&up->time, &secs, &up->time);
181b42c08d3SEd Schouten timeradd(&Total, &secs, &Total);
182b42c08d3SEd Schouten return;
183b42c08d3SEd Schouten } else if (c > 0)
184b42c08d3SEd Schouten break;
185b42c08d3SEd Schouten aup = up;
186ff9c3a32SDavid Greenman }
187ff9c3a32SDavid Greenman /*
188ff9c3a32SDavid Greenman * not found so add new user unless specified users only
189ff9c3a32SDavid Greenman */
190ff9c3a32SDavid Greenman if (Flags & AC_U)
191b42c08d3SEd Schouten return;
192ff9c3a32SDavid Greenman
193b42c08d3SEd Schouten if ((up = malloc(sizeof(*up))) == NULL)
194c3737d34SPhilippe Charnier errx(1, "malloc failed");
195b42c08d3SEd Schouten if (aup == NULL)
196b42c08d3SEd Schouten SLIST_INSERT_HEAD(&Users, up, next);
197b42c08d3SEd Schouten else
198b42c08d3SEd Schouten SLIST_INSERT_AFTER(aup, up, next);
199b42c08d3SEd Schouten strlcpy(up->user, user, sizeof(up->user));
200b42c08d3SEd Schouten up->time = secs;
201b42c08d3SEd Schouten timeradd(&Total, &secs, &Total);
202ff9c3a32SDavid Greenman }
203ff9c3a32SDavid Greenman
204ff9c3a32SDavid Greenman int
main(int argc,char * argv[])2057f761c52SGarance A Drosehn main(int argc, char *argv[])
206ff9c3a32SDavid Greenman {
207cf4d4e15SEd Schouten const char *wtmpf = NULL;
208ff9c3a32SDavid Greenman int c;
209ff9c3a32SDavid Greenman
2105877948cSAndrey A. Chernov (void) setlocale(LC_TIME, "");
2115877948cSAndrey A. Chernov
212b42c08d3SEd Schouten while ((c = getopt(argc, argv, "c:dpt:w:")) != -1) {
213ff9c3a32SDavid Greenman switch (c) {
214ff9c3a32SDavid Greenman case 'c':
215ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY
216ff9c3a32SDavid Greenman Console = optarg;
217ff9c3a32SDavid Greenman #else
218ff9c3a32SDavid Greenman usage(); /* XXX */
219ff9c3a32SDavid Greenman #endif
220ff9c3a32SDavid Greenman break;
221ff9c3a32SDavid Greenman case 'd':
222ff9c3a32SDavid Greenman Flags |= AC_D;
223ff9c3a32SDavid Greenman break;
224ff9c3a32SDavid Greenman case 'p':
225ff9c3a32SDavid Greenman Flags |= AC_P;
226ff9c3a32SDavid Greenman break;
227ff9c3a32SDavid Greenman case 't': /* only do specified ttys */
228ff9c3a32SDavid Greenman add_tty(optarg);
229ff9c3a32SDavid Greenman break;
230ff9c3a32SDavid Greenman case 'w':
231cf4d4e15SEd Schouten Flags |= AC_W;
232cf4d4e15SEd Schouten wtmpf = optarg;
233ff9c3a32SDavid Greenman break;
234ff9c3a32SDavid Greenman case '?':
235ff9c3a32SDavid Greenman default:
236ff9c3a32SDavid Greenman usage();
237ff9c3a32SDavid Greenman break;
238ff9c3a32SDavid Greenman }
239ff9c3a32SDavid Greenman }
240ff9c3a32SDavid Greenman if (optind < argc) {
241ff9c3a32SDavid Greenman /*
242ff9c3a32SDavid Greenman * initialize user list
243ff9c3a32SDavid Greenman */
244ff9c3a32SDavid Greenman for (; optind < argc; optind++) {
245b42c08d3SEd Schouten update_user(argv[optind], (struct timeval){ 0, 0 });
246ff9c3a32SDavid Greenman }
247ff9c3a32SDavid Greenman Flags |= AC_U; /* freeze user list */
248ff9c3a32SDavid Greenman }
249ff9c3a32SDavid Greenman if (Flags & AC_D)
250ff9c3a32SDavid Greenman Flags &= ~AC_P;
251cf4d4e15SEd Schouten ac(wtmpf);
252ff9c3a32SDavid Greenman
253b42c08d3SEd Schouten return (0);
254ff9c3a32SDavid Greenman }
255ff9c3a32SDavid Greenman
256ff9c3a32SDavid Greenman /*
257ff9c3a32SDavid Greenman * print login time in decimal hours
258ff9c3a32SDavid Greenman */
259b42c08d3SEd Schouten static void
show(const char * user,struct timeval secs)260b42c08d3SEd Schouten show(const char *user, struct timeval secs)
261ff9c3a32SDavid Greenman {
262cf4d4e15SEd Schouten (void)printf("\t%-*s %8.2f\n",
263b42c08d3SEd Schouten (int)sizeof(((struct user_entry *)0)->user), user,
264b42c08d3SEd Schouten (double)secs.tv_sec / 3600);
265ff9c3a32SDavid Greenman }
266ff9c3a32SDavid Greenman
267b42c08d3SEd Schouten static void
show_users(void)268b42c08d3SEd Schouten show_users(void)
269ff9c3a32SDavid Greenman {
270b42c08d3SEd Schouten struct user_entry *lp;
271ff9c3a32SDavid Greenman
272b42c08d3SEd Schouten SLIST_FOREACH(lp, &Users, next)
273b42c08d3SEd Schouten show(lp->user, lp->time);
274ff9c3a32SDavid Greenman }
275ff9c3a32SDavid Greenman
276ff9c3a32SDavid Greenman /*
277ff9c3a32SDavid Greenman * print total login time for 24hr period in decimal hours
278ff9c3a32SDavid Greenman */
279b42c08d3SEd Schouten static void
show_today(struct timeval today)280b42c08d3SEd Schouten show_today(struct timeval today)
281ff9c3a32SDavid Greenman {
282b42c08d3SEd Schouten struct user_entry *up;
283b42c08d3SEd Schouten struct utmpx_entry *lp;
284ff9c3a32SDavid Greenman char date[64];
28507df15ddSEd Schouten struct timeval diff, total = { 0, 0 }, usec = { 0, 1 }, yesterday;
286497c7558SAndrey A. Chernov static int d_first = -1;
287ff9c3a32SDavid Greenman
288497c7558SAndrey A. Chernov if (d_first < 0)
289497c7558SAndrey A. Chernov d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
290b42c08d3SEd Schouten timersub(&today, &usec, &yesterday);
291497c7558SAndrey A. Chernov (void)strftime(date, sizeof(date),
292497c7558SAndrey A. Chernov d_first ? "%e %b total" : "%b %e total",
293b42c08d3SEd Schouten localtime(&yesterday.tv_sec));
294ff9c3a32SDavid Greenman
295b42c08d3SEd Schouten SLIST_FOREACH(lp, &CurUtmpx, next) {
29607df15ddSEd Schouten timersub(&today, &lp->time, &diff);
29707df15ddSEd Schouten update_user(lp->user, diff);
298b42c08d3SEd Schouten /* As if they just logged in. */
29907df15ddSEd Schouten lp->time = today;
300ff9c3a32SDavid Greenman }
301b42c08d3SEd Schouten SLIST_FOREACH(up, &Users, next) {
30207df15ddSEd Schouten timeradd(&total, &up->time, &total);
303b42c08d3SEd Schouten /* For next day. */
304b42c08d3SEd Schouten timerclear(&up->time);
305ff9c3a32SDavid Greenman }
30607df15ddSEd Schouten if (timerisset(&total))
30707df15ddSEd Schouten (void)printf("%s %11.2f\n", date, (double)total.tv_sec / 3600);
308ff9c3a32SDavid Greenman }
309ff9c3a32SDavid Greenman
310ff9c3a32SDavid Greenman /*
311b42c08d3SEd Schouten * Log a user out and update their times.
312b42c08d3SEd Schouten * If ut_type is BOOT_TIME or SHUTDOWN_TIME, we log all users out as the
313b42c08d3SEd Schouten * system has been shut down.
314ff9c3a32SDavid Greenman */
315b42c08d3SEd Schouten static void
log_out(const struct utmpx * up)316b42c08d3SEd Schouten log_out(const struct utmpx *up)
317ff9c3a32SDavid Greenman {
318b42c08d3SEd Schouten struct utmpx_entry *lp, *lp2, *tlp;
319b42c08d3SEd Schouten struct timeval secs;
320ff9c3a32SDavid Greenman
321b42c08d3SEd Schouten for (lp = SLIST_FIRST(&CurUtmpx), lp2 = NULL; lp != NULL;)
322cf4d4e15SEd Schouten if (up->ut_type == BOOT_TIME || up->ut_type == SHUTDOWN_TIME ||
323cf4d4e15SEd Schouten (up->ut_type == DEAD_PROCESS &&
324b42c08d3SEd Schouten memcmp(lp->id, up->ut_id, sizeof(up->ut_id)) == 0)) {
325b42c08d3SEd Schouten timersub(&up->ut_tv, &lp->time, &secs);
326b42c08d3SEd Schouten update_user(lp->user, secs);
327ff9c3a32SDavid Greenman /*
328ff9c3a32SDavid Greenman * now lose it
329ff9c3a32SDavid Greenman */
330ff9c3a32SDavid Greenman tlp = lp;
331b42c08d3SEd Schouten lp = SLIST_NEXT(lp, next);
332b42c08d3SEd Schouten if (lp2 == NULL)
333b42c08d3SEd Schouten SLIST_REMOVE_HEAD(&CurUtmpx, next);
334b42c08d3SEd Schouten else
335b42c08d3SEd Schouten SLIST_REMOVE_AFTER(lp2, next);
336ff9c3a32SDavid Greenman free(tlp);
337ff9c3a32SDavid Greenman } else {
338ff9c3a32SDavid Greenman lp2 = lp;
339b42c08d3SEd Schouten lp = SLIST_NEXT(lp, next);
340ff9c3a32SDavid Greenman }
341ff9c3a32SDavid Greenman }
342ff9c3a32SDavid Greenman
343ff9c3a32SDavid Greenman /*
344ff9c3a32SDavid Greenman * if do_tty says ok, login a user
345ff9c3a32SDavid Greenman */
346b42c08d3SEd Schouten static void
log_in(struct utmpx * up)347b42c08d3SEd Schouten log_in(struct utmpx *up)
348ff9c3a32SDavid Greenman {
349b42c08d3SEd Schouten struct utmpx_entry *lp;
350ff9c3a32SDavid Greenman
351ff9c3a32SDavid Greenman /*
352ff9c3a32SDavid Greenman * this could be a login. if we're not dealing with
353ff9c3a32SDavid Greenman * the console name, say it is.
354ff9c3a32SDavid Greenman *
355ff9c3a32SDavid Greenman * If we are, and if ut_host==":0.0" we know that it
356ff9c3a32SDavid Greenman * isn't a real login. _But_ if we have not yet recorded
357ff9c3a32SDavid Greenman * someone being logged in on Console - due to the wtmp
358ff9c3a32SDavid Greenman * file starting after they logged in, we'll pretend they
359ff9c3a32SDavid Greenman * logged in, at the start of the wtmp file.
360ff9c3a32SDavid Greenman */
361ff9c3a32SDavid Greenman
362ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY
363ff9c3a32SDavid Greenman if (up->ut_host[0] == ':') {
364ff9c3a32SDavid Greenman /*
365ff9c3a32SDavid Greenman * SunOS 4.0.2 does not treat ":0.0" as special but we
366ff9c3a32SDavid Greenman * do.
367ff9c3a32SDavid Greenman */
368b42c08d3SEd Schouten if (on_console())
369b42c08d3SEd Schouten return;
370ff9c3a32SDavid Greenman /*
371ff9c3a32SDavid Greenman * ok, no recorded login, so they were here when wtmp
372ff9c3a32SDavid Greenman * started! Adjust ut_time!
373ff9c3a32SDavid Greenman */
374b42c08d3SEd Schouten up->ut_tv = FirstTime;
375ff9c3a32SDavid Greenman /*
376ff9c3a32SDavid Greenman * this allows us to pick the right logout
377ff9c3a32SDavid Greenman */
378fd96447aSKris Kennaway strlcpy(up->ut_line, Console, sizeof(up->ut_line));
379ff9c3a32SDavid Greenman }
380ff9c3a32SDavid Greenman #endif
381ff9c3a32SDavid Greenman /*
382ff9c3a32SDavid Greenman * If we are doing specified ttys only, we ignore
383ff9c3a32SDavid Greenman * anything else.
384ff9c3a32SDavid Greenman */
385b42c08d3SEd Schouten if (Flags & AC_T && !do_tty(up->ut_line))
386b42c08d3SEd Schouten return;
387ff9c3a32SDavid Greenman
388ff9c3a32SDavid Greenman /*
389ff9c3a32SDavid Greenman * go ahead and log them in
390ff9c3a32SDavid Greenman */
391b42c08d3SEd Schouten if ((lp = malloc(sizeof(*lp))) == NULL)
392c3737d34SPhilippe Charnier errx(1, "malloc failed");
393b42c08d3SEd Schouten SLIST_INSERT_HEAD(&CurUtmpx, lp, next);
394b42c08d3SEd Schouten strlcpy(lp->user, up->ut_user, sizeof(lp->user));
395b42c08d3SEd Schouten memcpy(lp->id, up->ut_id, sizeof(lp->id));
396b42c08d3SEd Schouten #ifdef CONSOLE_TTY
397b42c08d3SEd Schouten memcpy(lp->line, up->ut_line, sizeof(lp->line));
398ff9c3a32SDavid Greenman #endif
399b42c08d3SEd Schouten lp->time = up->ut_tv;
400ff9c3a32SDavid Greenman }
401ff9c3a32SDavid Greenman
402b42c08d3SEd Schouten static void
ac(const char * file)403cf4d4e15SEd Schouten ac(const char *file)
404ff9c3a32SDavid Greenman {
405b42c08d3SEd Schouten struct utmpx_entry *lp;
406cf4d4e15SEd Schouten struct utmpx *usr, usht;
407ff9c3a32SDavid Greenman struct tm *ltm;
408b42c08d3SEd Schouten struct timeval prev_secs, ut_timecopy, secs, clock_shift, now;
409*def653cdSJohn Baldwin int day;
410ff9c3a32SDavid Greenman
411e3b218cdSGarance A Drosehn day = -1;
412b42c08d3SEd Schouten timerclear(&prev_secs); /* Minimum acceptable date == 1970. */
413b42c08d3SEd Schouten timerclear(&secs);
414b42c08d3SEd Schouten timerclear(&clock_shift);
415cf4d4e15SEd Schouten if (setutxdb(UTXDB_LOG, file) != 0)
416cf4d4e15SEd Schouten err(1, "%s", file);
417cf4d4e15SEd Schouten while ((usr = getutxent()) != NULL) {
418b42c08d3SEd Schouten ut_timecopy = usr->ut_tv;
419b42c08d3SEd Schouten /* Don't let the time run backwards. */
420b42c08d3SEd Schouten if (timercmp(&ut_timecopy, &prev_secs, <))
421b42c08d3SEd Schouten ut_timecopy = prev_secs;
422e3b218cdSGarance A Drosehn prev_secs = ut_timecopy;
423e3b218cdSGarance A Drosehn
424b42c08d3SEd Schouten if (!timerisset(&FirstTime))
425e3b218cdSGarance A Drosehn FirstTime = ut_timecopy;
426ff9c3a32SDavid Greenman if (Flags & AC_D) {
427b42c08d3SEd Schouten ltm = localtime(&ut_timecopy.tv_sec);
428ff9c3a32SDavid Greenman if (day >= 0 && day != ltm->tm_yday) {
429ff9c3a32SDavid Greenman day = ltm->tm_yday;
430ff9c3a32SDavid Greenman /*
431ff9c3a32SDavid Greenman * print yesterday's total
432ff9c3a32SDavid Greenman */
433e3b218cdSGarance A Drosehn secs = ut_timecopy;
434b42c08d3SEd Schouten secs.tv_sec -= ltm->tm_sec;
435b42c08d3SEd Schouten secs.tv_sec -= 60 * ltm->tm_min;
436b42c08d3SEd Schouten secs.tv_sec -= 3600 * ltm->tm_hour;
437b42c08d3SEd Schouten secs.tv_usec = 0;
438b42c08d3SEd Schouten show_today(secs);
439ff9c3a32SDavid Greenman } else
440ff9c3a32SDavid Greenman day = ltm->tm_yday;
441ff9c3a32SDavid Greenman }
442cf4d4e15SEd Schouten switch(usr->ut_type) {
443cf4d4e15SEd Schouten case OLD_TIME:
444b42c08d3SEd Schouten clock_shift = ut_timecopy;
445ff9c3a32SDavid Greenman break;
446cf4d4e15SEd Schouten case NEW_TIME:
447b42c08d3SEd Schouten timersub(&clock_shift, &ut_timecopy, &clock_shift);
448ff9c3a32SDavid Greenman /*
449ff9c3a32SDavid Greenman * adjust time for those logged in
450ff9c3a32SDavid Greenman */
451b42c08d3SEd Schouten SLIST_FOREACH(lp, &CurUtmpx, next)
452b42c08d3SEd Schouten timersub(&lp->time, &clock_shift, &lp->time);
453ff9c3a32SDavid Greenman break;
454cf4d4e15SEd Schouten case BOOT_TIME:
455cf4d4e15SEd Schouten case SHUTDOWN_TIME:
456b42c08d3SEd Schouten log_out(usr);
457e3b218cdSGarance A Drosehn FirstTime = ut_timecopy; /* shouldn't be needed */
458ff9c3a32SDavid Greenman break;
459cf4d4e15SEd Schouten case USER_PROCESS:
460ff9c3a32SDavid Greenman /*
461b42c08d3SEd Schouten * If they came in on pts/..., then it is only
462b42c08d3SEd Schouten * a login session if the ut_host field is non-empty.
463ff9c3a32SDavid Greenman */
464b42c08d3SEd Schouten if (strncmp(usr->ut_line, "pts/", 4) != 0 ||
465cf4d4e15SEd Schouten *usr->ut_host != '\0')
466b42c08d3SEd Schouten log_in(usr);
467cf4d4e15SEd Schouten break;
468cf4d4e15SEd Schouten case DEAD_PROCESS:
469b42c08d3SEd Schouten log_out(usr);
470ff9c3a32SDavid Greenman break;
471ff9c3a32SDavid Greenman }
472ff9c3a32SDavid Greenman }
473cf4d4e15SEd Schouten endutxent();
474b42c08d3SEd Schouten (void)gettimeofday(&now, NULL);
475b42c08d3SEd Schouten if (Flags & AC_W)
476b42c08d3SEd Schouten usht.ut_tv = ut_timecopy;
4779c8d0b96SEd Schouten else
478b42c08d3SEd Schouten usht.ut_tv = now;
479cf4d4e15SEd Schouten usht.ut_type = SHUTDOWN_TIME;
480ff9c3a32SDavid Greenman
481ff9c3a32SDavid Greenman if (Flags & AC_D) {
482b42c08d3SEd Schouten ltm = localtime(&ut_timecopy.tv_sec);
483ff9c3a32SDavid Greenman if (day >= 0 && day != ltm->tm_yday) {
484ff9c3a32SDavid Greenman /*
485ff9c3a32SDavid Greenman * print yesterday's total
486ff9c3a32SDavid Greenman */
487e3b218cdSGarance A Drosehn secs = ut_timecopy;
488b42c08d3SEd Schouten secs.tv_sec -= ltm->tm_sec;
489b42c08d3SEd Schouten secs.tv_sec -= 60 * ltm->tm_min;
490b42c08d3SEd Schouten secs.tv_sec -= 3600 * ltm->tm_hour;
491b42c08d3SEd Schouten secs.tv_usec = 0;
492b42c08d3SEd Schouten show_today(secs);
493ff9c3a32SDavid Greenman }
494ff9c3a32SDavid Greenman }
495ff9c3a32SDavid Greenman /*
496ff9c3a32SDavid Greenman * anyone still logged in gets time up to now
497ff9c3a32SDavid Greenman */
498b42c08d3SEd Schouten log_out(&usht);
499ff9c3a32SDavid Greenman
500ff9c3a32SDavid Greenman if (Flags & AC_D)
501b42c08d3SEd Schouten show_today(now);
502ff9c3a32SDavid Greenman else {
503ff9c3a32SDavid Greenman if (Flags & AC_P)
504b42c08d3SEd Schouten show_users();
505ff9c3a32SDavid Greenman show("total", Total);
506ff9c3a32SDavid Greenman }
507ff9c3a32SDavid Greenman }
508ff9c3a32SDavid Greenman
509b42c08d3SEd Schouten static void
usage(void)5107f761c52SGarance A Drosehn usage(void)
511ff9c3a32SDavid Greenman {
512ff9c3a32SDavid Greenman (void)fprintf(stderr,
513ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY
514ff9c3a32SDavid Greenman "ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n");
515ff9c3a32SDavid Greenman #else
516ff9c3a32SDavid Greenman "ac [-dp] [-t tty] [-w wtmp] [users ...]\n");
517ff9c3a32SDavid Greenman #endif
518ff9c3a32SDavid Greenman exit(1);
519ff9c3a32SDavid Greenman }
520