1994-09-26 22:12:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1994 Christopher G. Demetriou.
|
|
|
|
* @(#)Copyright (c) 1994, Simon J. Gerraty.
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1994-09-26 22:12:27 +00:00
|
|
|
* This is free software. It comes with NO WARRANTY.
|
1995-05-30 03:57:47 +00:00
|
|
|
* Permission to use, modify and distribute this source code
|
1994-09-26 22:12:27 +00:00
|
|
|
* is granted subject to the following conditions.
|
1995-05-30 03:57:47 +00:00
|
|
|
* 1/ that the above copyright notice and this notice
|
|
|
|
* are preserved in all copies and that due credit be given
|
|
|
|
* to the author.
|
|
|
|
* 2/ that any changes to this code are clearly commented
|
|
|
|
* as such so that the author does not get blamed for bugs
|
1994-09-26 22:12:27 +00:00
|
|
|
* other than his own.
|
|
|
|
*/
|
|
|
|
|
2002-10-17 13:19:47 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-09-26 22:12:27 +00:00
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
#include <sys/queue.h>
|
1994-09-26 22:12:27 +00:00
|
|
|
#include <sys/time.h>
|
2012-08-30 16:45:27 +00:00
|
|
|
|
1994-09-26 22:12:27 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2001-03-21 13:41:03 +00:00
|
|
|
#include <langinfo.h>
|
1997-09-01 06:11:40 +00:00
|
|
|
#include <locale.h>
|
1994-09-26 22:12:27 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2002-07-08 20:13:07 +00:00
|
|
|
#include <timeconv.h>
|
1994-09-26 22:12:27 +00:00
|
|
|
#include <unistd.h>
|
2010-01-13 18:14:59 +00:00
|
|
|
#include <utmpx.h>
|
1994-09-26 22:12:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* this is for our list of currently logged in sessions
|
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
struct utmpx_entry {
|
|
|
|
SLIST_ENTRY(utmpx_entry) next;
|
|
|
|
char user[sizeof(((struct utmpx *)0)->ut_user)];
|
|
|
|
char id[sizeof(((struct utmpx *)0)->ut_id)];
|
|
|
|
#ifdef CONSOLE_TTY
|
|
|
|
char line[sizeof(((struct utmpx *)0)->ut_line)];
|
|
|
|
#endif
|
|
|
|
struct timeval time;
|
1994-09-26 22:12:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is for our list of users that are accumulating time.
|
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
struct user_entry {
|
|
|
|
SLIST_ENTRY(user_entry) next;
|
|
|
|
char user[sizeof(((struct utmpx *)0)->ut_user)];
|
|
|
|
struct timeval time;
|
1994-09-26 22:12:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is for chosing whether to ignore a login
|
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
struct tty_entry {
|
|
|
|
SLIST_ENTRY(tty_entry) next;
|
|
|
|
char line[sizeof(((struct utmpx *)0)->ut_line) + 2];
|
|
|
|
size_t len;
|
|
|
|
int ret;
|
1994-09-26 22:12:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* globals - yes yuk
|
|
|
|
*/
|
|
|
|
#ifdef CONSOLE_TTY
|
2012-08-30 16:45:27 +00:00
|
|
|
static const char *Console = CONSOLE_TTY;
|
1994-09-26 22:12:27 +00:00
|
|
|
#endif
|
2012-08-30 16:45:27 +00:00
|
|
|
static struct timeval Total = { 0, 0 };
|
|
|
|
static struct timeval FirstTime = { 0, 0 };
|
1994-09-26 22:12:27 +00:00
|
|
|
static int Flags = 0;
|
2012-08-30 16:45:27 +00:00
|
|
|
static SLIST_HEAD(, utmpx_entry) CurUtmpx = SLIST_HEAD_INITIALIZER(CurUtmpx);
|
|
|
|
static SLIST_HEAD(, user_entry) Users = SLIST_HEAD_INITIALIZER(Users);
|
|
|
|
static SLIST_HEAD(, tty_entry) Ttys = SLIST_HEAD_INITIALIZER(Ttys);
|
1994-09-26 22:12:27 +00:00
|
|
|
|
|
|
|
#define AC_W 1 /* not _PATH_WTMP */
|
|
|
|
#define AC_D 2 /* daily totals (ignore -p) */
|
|
|
|
#define AC_P 4 /* per-user totals */
|
|
|
|
#define AC_U 8 /* specified users only */
|
|
|
|
#define AC_T 16 /* specified ttys only */
|
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
static void ac(const char *);
|
|
|
|
static void usage(void);
|
1994-09-26 22:12:27 +00:00
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
static void
|
|
|
|
add_tty(const char *line)
|
1994-09-26 22:12:27 +00:00
|
|
|
{
|
2012-08-30 16:45:27 +00:00
|
|
|
struct tty_entry *tp;
|
2004-03-03 02:44:52 +00:00
|
|
|
char *rcp;
|
1994-09-26 22:12:27 +00:00
|
|
|
|
|
|
|
Flags |= AC_T;
|
1995-05-30 03:57:47 +00:00
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
if ((tp = malloc(sizeof(*tp))) == NULL)
|
1999-10-12 19:27:11 +00:00
|
|
|
errx(1, "malloc failed");
|
1994-09-26 22:12:27 +00:00
|
|
|
tp->len = 0; /* full match */
|
|
|
|
tp->ret = 1; /* do if match */
|
2012-08-30 16:45:27 +00:00
|
|
|
if (*line == '!') { /* don't do if match */
|
1994-09-26 22:12:27 +00:00
|
|
|
tp->ret = 0;
|
2012-08-30 16:45:27 +00:00
|
|
|
line++;
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
2012-08-30 16:45:27 +00:00
|
|
|
strlcpy(tp->line, line, sizeof(tp->line));
|
|
|
|
/* Wildcard. */
|
|
|
|
if ((rcp = strchr(tp->line, '*')) != NULL) {
|
1994-09-26 22:12:27 +00:00
|
|
|
*rcp = '\0';
|
2012-08-30 16:45:27 +00:00
|
|
|
/* Match len bytes only. */
|
|
|
|
tp->len = strlen(tp->line);
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
2012-08-30 16:45:27 +00:00
|
|
|
SLIST_INSERT_HEAD(&Ttys, tp, next);
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* should we process the named tty?
|
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
static int
|
|
|
|
do_tty(const char *line)
|
1994-09-26 22:12:27 +00:00
|
|
|
{
|
2012-08-30 16:45:27 +00:00
|
|
|
struct tty_entry *tp;
|
1994-09-26 22:12:27 +00:00
|
|
|
int def_ret = 0;
|
1995-05-30 03:57:47 +00:00
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
SLIST_FOREACH(tp, &Ttys, next) {
|
1994-09-26 22:12:27 +00:00
|
|
|
if (tp->ret == 0) /* specific don't */
|
|
|
|
def_ret = 1; /* default do */
|
|
|
|
if (tp->len != 0) {
|
2012-08-30 16:45:27 +00:00
|
|
|
if (strncmp(line, tp->line, tp->len) == 0)
|
1994-09-26 22:12:27 +00:00
|
|
|
return tp->ret;
|
|
|
|
} else {
|
2012-08-30 16:45:27 +00:00
|
|
|
if (strncmp(line, tp->line, sizeof(tp->line)) == 0)
|
1994-09-26 22:12:27 +00:00
|
|
|
return tp->ret;
|
|
|
|
}
|
|
|
|
}
|
2012-08-30 16:45:27 +00:00
|
|
|
return (def_ret);
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONSOLE_TTY
|
|
|
|
/*
|
|
|
|
* is someone logged in on Console?
|
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
static int
|
|
|
|
on_console(void)
|
1994-09-26 22:12:27 +00:00
|
|
|
{
|
2012-08-30 16:45:27 +00:00
|
|
|
struct utmpx_entry *up;
|
1994-09-26 22:12:27 +00:00
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
SLIST_FOREACH(up, &CurUtmpx, next)
|
|
|
|
if (strcmp(up->line, Console) == 0)
|
|
|
|
return (1);
|
|
|
|
return (0);
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2012-08-30 16:45:27 +00:00
|
|
|
* Update user's login time.
|
|
|
|
* If no entry for this user is found, a new entry is inserted into the
|
|
|
|
* list alphabetically.
|
1994-09-26 22:12:27 +00:00
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
static void
|
|
|
|
update_user(const char *user, struct timeval secs)
|
1994-09-26 22:12:27 +00:00
|
|
|
{
|
2012-08-30 16:45:27 +00:00
|
|
|
struct user_entry *up, *aup;
|
|
|
|
int c;
|
1994-09-26 22:12:27 +00:00
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
aup = NULL;
|
|
|
|
SLIST_FOREACH(up, &Users, next) {
|
|
|
|
c = strcmp(up->user, user);
|
|
|
|
if (c == 0) {
|
|
|
|
timeradd(&up->time, &secs, &up->time);
|
|
|
|
timeradd(&Total, &secs, &Total);
|
|
|
|
return;
|
|
|
|
} else if (c > 0)
|
|
|
|
break;
|
|
|
|
aup = up;
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* not found so add new user unless specified users only
|
|
|
|
*/
|
|
|
|
if (Flags & AC_U)
|
2012-08-30 16:45:27 +00:00
|
|
|
return;
|
1995-05-30 03:57:47 +00:00
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
if ((up = malloc(sizeof(*up))) == NULL)
|
1999-10-12 19:27:11 +00:00
|
|
|
errx(1, "malloc failed");
|
2012-08-30 16:45:27 +00:00
|
|
|
if (aup == NULL)
|
|
|
|
SLIST_INSERT_HEAD(&Users, up, next);
|
|
|
|
else
|
|
|
|
SLIST_INSERT_AFTER(aup, up, next);
|
|
|
|
strlcpy(up->user, user, sizeof(up->user));
|
|
|
|
up->time = secs;
|
|
|
|
timeradd(&Total, &secs, &Total);
|
2004-03-08 19:20:06 +00:00
|
|
|
}
|
|
|
|
|
1994-09-26 22:12:27 +00:00
|
|
|
int
|
2004-03-03 02:41:21 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-09-26 22:12:27 +00:00
|
|
|
{
|
2010-01-13 18:14:59 +00:00
|
|
|
const char *wtmpf = NULL;
|
1994-09-26 22:12:27 +00:00
|
|
|
int c;
|
|
|
|
|
1995-10-26 23:10:10 +00:00
|
|
|
(void) setlocale(LC_TIME, "");
|
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
while ((c = getopt(argc, argv, "c:dpt:w:")) != -1) {
|
1994-09-26 22:12:27 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
|
|
|
#ifdef CONSOLE_TTY
|
|
|
|
Console = optarg;
|
|
|
|
#else
|
|
|
|
usage(); /* XXX */
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
Flags |= AC_D;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
Flags |= AC_P;
|
|
|
|
break;
|
|
|
|
case 't': /* only do specified ttys */
|
|
|
|
add_tty(optarg);
|
|
|
|
break;
|
|
|
|
case 'w':
|
2010-01-13 18:14:59 +00:00
|
|
|
Flags |= AC_W;
|
|
|
|
wtmpf = optarg;
|
1994-09-26 22:12:27 +00:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (optind < argc) {
|
|
|
|
/*
|
|
|
|
* initialize user list
|
|
|
|
*/
|
|
|
|
for (; optind < argc; optind++) {
|
2012-08-30 16:45:27 +00:00
|
|
|
update_user(argv[optind], (struct timeval){ 0, 0 });
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
|
|
|
Flags |= AC_U; /* freeze user list */
|
|
|
|
}
|
|
|
|
if (Flags & AC_D)
|
|
|
|
Flags &= ~AC_P;
|
2010-01-13 18:14:59 +00:00
|
|
|
ac(wtmpf);
|
1995-05-30 03:57:47 +00:00
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
return (0);
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* print login time in decimal hours
|
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
static void
|
|
|
|
show(const char *user, struct timeval secs)
|
1994-09-26 22:12:27 +00:00
|
|
|
{
|
2010-01-13 18:14:59 +00:00
|
|
|
(void)printf("\t%-*s %8.2f\n",
|
2012-08-30 16:45:27 +00:00
|
|
|
(int)sizeof(((struct user_entry *)0)->user), user,
|
|
|
|
(double)secs.tv_sec / 3600);
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
static void
|
|
|
|
show_users(void)
|
1994-09-26 22:12:27 +00:00
|
|
|
{
|
2012-08-30 16:45:27 +00:00
|
|
|
struct user_entry *lp;
|
1994-09-26 22:12:27 +00:00
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
SLIST_FOREACH(lp, &Users, next)
|
|
|
|
show(lp->user, lp->time);
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* print total login time for 24hr period in decimal hours
|
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
static void
|
|
|
|
show_today(struct timeval today)
|
1994-09-26 22:12:27 +00:00
|
|
|
{
|
2012-08-30 16:45:27 +00:00
|
|
|
struct user_entry *up;
|
|
|
|
struct utmpx_entry *lp;
|
1994-09-26 22:12:27 +00:00
|
|
|
char date[64];
|
2012-08-30 16:45:27 +00:00
|
|
|
struct timeval usec = { 0, 1 };
|
|
|
|
struct timeval yesterday;
|
2001-03-21 13:41:03 +00:00
|
|
|
static int d_first = -1;
|
1994-09-26 22:12:27 +00:00
|
|
|
|
2001-03-21 13:41:03 +00:00
|
|
|
if (d_first < 0)
|
|
|
|
d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
|
2012-08-30 16:45:27 +00:00
|
|
|
timersub(&today, &usec, &yesterday);
|
|
|
|
(void)strftime(date, sizeof(date),
|
2001-03-21 13:41:03 +00:00
|
|
|
d_first ? "%e %b total" : "%b %e total",
|
2012-08-30 16:45:27 +00:00
|
|
|
localtime(&yesterday.tv_sec));
|
1994-09-26 22:12:27 +00:00
|
|
|
|
|
|
|
/* restore the missing second */
|
2012-08-30 16:45:27 +00:00
|
|
|
timeradd(&today, &usec, &yesterday);
|
1995-05-30 03:57:47 +00:00
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
SLIST_FOREACH(lp, &CurUtmpx, next) {
|
|
|
|
timersub(&yesterday, &lp->time, &today);
|
|
|
|
update_user(lp->user, today);
|
|
|
|
/* As if they just logged in. */
|
|
|
|
lp->time = yesterday;
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
2012-08-30 16:45:27 +00:00
|
|
|
timerclear(&today);
|
|
|
|
SLIST_FOREACH(up, &Users, next) {
|
|
|
|
timeradd(&today, &up->time, &today);
|
|
|
|
/* For next day. */
|
|
|
|
timerclear(&up->time);
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
2012-08-30 16:45:27 +00:00
|
|
|
if (timerisset(&today))
|
|
|
|
(void)printf("%s %11.2f\n", date, (double)today.tv_sec / 3600);
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-08-30 16:45:27 +00:00
|
|
|
* Log a user out and update their times.
|
|
|
|
* If ut_type is BOOT_TIME or SHUTDOWN_TIME, we log all users out as the
|
|
|
|
* system has been shut down.
|
1994-09-26 22:12:27 +00:00
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
static void
|
|
|
|
log_out(const struct utmpx *up)
|
1994-09-26 22:12:27 +00:00
|
|
|
{
|
2012-08-30 16:45:27 +00:00
|
|
|
struct utmpx_entry *lp, *lp2, *tlp;
|
|
|
|
struct timeval secs;
|
1995-05-30 03:57:47 +00:00
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
for (lp = SLIST_FIRST(&CurUtmpx), lp2 = NULL; lp != NULL;)
|
2010-01-13 18:14:59 +00:00
|
|
|
if (up->ut_type == BOOT_TIME || up->ut_type == SHUTDOWN_TIME ||
|
|
|
|
(up->ut_type == DEAD_PROCESS &&
|
2012-08-30 16:45:27 +00:00
|
|
|
memcmp(lp->id, up->ut_id, sizeof(up->ut_id)) == 0)) {
|
|
|
|
timersub(&up->ut_tv, &lp->time, &secs);
|
|
|
|
update_user(lp->user, secs);
|
1994-09-26 22:12:27 +00:00
|
|
|
/*
|
|
|
|
* now lose it
|
|
|
|
*/
|
|
|
|
tlp = lp;
|
2012-08-30 16:45:27 +00:00
|
|
|
lp = SLIST_NEXT(lp, next);
|
|
|
|
if (lp2 == NULL)
|
|
|
|
SLIST_REMOVE_HEAD(&CurUtmpx, next);
|
|
|
|
else
|
|
|
|
SLIST_REMOVE_AFTER(lp2, next);
|
1994-09-26 22:12:27 +00:00
|
|
|
free(tlp);
|
|
|
|
} else {
|
|
|
|
lp2 = lp;
|
2012-08-30 16:45:27 +00:00
|
|
|
lp = SLIST_NEXT(lp, next);
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if do_tty says ok, login a user
|
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
static void
|
|
|
|
log_in(struct utmpx *up)
|
1994-09-26 22:12:27 +00:00
|
|
|
{
|
2012-08-30 16:45:27 +00:00
|
|
|
struct utmpx_entry *lp;
|
1994-09-26 22:12:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* this could be a login. if we're not dealing with
|
|
|
|
* the console name, say it is.
|
|
|
|
*
|
|
|
|
* If we are, and if ut_host==":0.0" we know that it
|
|
|
|
* isn't a real login. _But_ if we have not yet recorded
|
|
|
|
* someone being logged in on Console - due to the wtmp
|
|
|
|
* file starting after they logged in, we'll pretend they
|
|
|
|
* logged in, at the start of the wtmp file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONSOLE_TTY
|
|
|
|
if (up->ut_host[0] == ':') {
|
|
|
|
/*
|
|
|
|
* SunOS 4.0.2 does not treat ":0.0" as special but we
|
1995-05-30 03:57:47 +00:00
|
|
|
* do.
|
1994-09-26 22:12:27 +00:00
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
if (on_console())
|
|
|
|
return;
|
1994-09-26 22:12:27 +00:00
|
|
|
/*
|
|
|
|
* ok, no recorded login, so they were here when wtmp
|
1995-05-30 03:57:47 +00:00
|
|
|
* started! Adjust ut_time!
|
1994-09-26 22:12:27 +00:00
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
up->ut_tv = FirstTime;
|
1994-09-26 22:12:27 +00:00
|
|
|
/*
|
|
|
|
* this allows us to pick the right logout
|
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
strlcpy(up->ut_line, Console, sizeof(up->ut_line));
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* If we are doing specified ttys only, we ignore
|
|
|
|
* anything else.
|
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
if (Flags & AC_T && !do_tty(up->ut_line))
|
|
|
|
return;
|
1994-09-26 22:12:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* go ahead and log them in
|
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
if ((lp = malloc(sizeof(*lp))) == NULL)
|
1999-10-12 19:27:11 +00:00
|
|
|
errx(1, "malloc failed");
|
2012-08-30 16:45:27 +00:00
|
|
|
SLIST_INSERT_HEAD(&CurUtmpx, lp, next);
|
|
|
|
strlcpy(lp->user, up->ut_user, sizeof(lp->user));
|
|
|
|
memcpy(lp->id, up->ut_id, sizeof(lp->id));
|
|
|
|
#ifdef CONSOLE_TTY
|
|
|
|
memcpy(lp->line, up->ut_line, sizeof(lp->line));
|
1994-09-26 22:12:27 +00:00
|
|
|
#endif
|
2012-08-30 16:45:27 +00:00
|
|
|
lp->time = up->ut_tv;
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
static void
|
2010-01-13 18:14:59 +00:00
|
|
|
ac(const char *file)
|
1994-09-26 22:12:27 +00:00
|
|
|
{
|
2012-08-30 16:45:27 +00:00
|
|
|
struct utmpx_entry *lp;
|
2010-01-13 18:14:59 +00:00
|
|
|
struct utmpx *usr, usht;
|
1994-09-26 22:12:27 +00:00
|
|
|
struct tm *ltm;
|
2012-08-30 16:45:27 +00:00
|
|
|
struct timeval prev_secs, ut_timecopy, secs, clock_shift, now;
|
|
|
|
int day, rfound;
|
1995-05-30 03:57:47 +00:00
|
|
|
|
2004-03-08 20:02:23 +00:00
|
|
|
day = -1;
|
2012-08-30 16:45:27 +00:00
|
|
|
timerclear(&prev_secs); /* Minimum acceptable date == 1970. */
|
|
|
|
timerclear(&secs);
|
|
|
|
timerclear(&clock_shift);
|
|
|
|
rfound = 0;
|
2010-01-13 18:14:59 +00:00
|
|
|
if (setutxdb(UTXDB_LOG, file) != 0)
|
|
|
|
err(1, "%s", file);
|
|
|
|
while ((usr = getutxent()) != NULL) {
|
2004-03-08 20:02:23 +00:00
|
|
|
rfound++;
|
2012-08-30 16:45:27 +00:00
|
|
|
ut_timecopy = usr->ut_tv;
|
|
|
|
/* Don't let the time run backwards. */
|
|
|
|
if (timercmp(&ut_timecopy, &prev_secs, <))
|
|
|
|
ut_timecopy = prev_secs;
|
2004-03-08 20:02:23 +00:00
|
|
|
prev_secs = ut_timecopy;
|
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
if (!timerisset(&FirstTime))
|
2004-03-08 20:02:23 +00:00
|
|
|
FirstTime = ut_timecopy;
|
1994-09-26 22:12:27 +00:00
|
|
|
if (Flags & AC_D) {
|
2012-08-30 16:45:27 +00:00
|
|
|
ltm = localtime(&ut_timecopy.tv_sec);
|
1994-09-26 22:12:27 +00:00
|
|
|
if (day >= 0 && day != ltm->tm_yday) {
|
|
|
|
day = ltm->tm_yday;
|
|
|
|
/*
|
|
|
|
* print yesterday's total
|
|
|
|
*/
|
2004-03-08 20:02:23 +00:00
|
|
|
secs = ut_timecopy;
|
2012-08-30 16:45:27 +00:00
|
|
|
secs.tv_sec -= ltm->tm_sec;
|
|
|
|
secs.tv_sec -= 60 * ltm->tm_min;
|
|
|
|
secs.tv_sec -= 3600 * ltm->tm_hour;
|
|
|
|
secs.tv_usec = 0;
|
|
|
|
show_today(secs);
|
1994-09-26 22:12:27 +00:00
|
|
|
} else
|
|
|
|
day = ltm->tm_yday;
|
|
|
|
}
|
2010-01-13 18:14:59 +00:00
|
|
|
switch(usr->ut_type) {
|
|
|
|
case OLD_TIME:
|
2012-08-30 16:45:27 +00:00
|
|
|
clock_shift = ut_timecopy;
|
1994-09-26 22:12:27 +00:00
|
|
|
break;
|
2010-01-13 18:14:59 +00:00
|
|
|
case NEW_TIME:
|
2012-08-30 16:45:27 +00:00
|
|
|
timersub(&clock_shift, &ut_timecopy, &clock_shift);
|
1994-09-26 22:12:27 +00:00
|
|
|
/*
|
|
|
|
* adjust time for those logged in
|
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
SLIST_FOREACH(lp, &CurUtmpx, next)
|
|
|
|
timersub(&lp->time, &clock_shift, &lp->time);
|
1994-09-26 22:12:27 +00:00
|
|
|
break;
|
2010-01-13 18:14:59 +00:00
|
|
|
case BOOT_TIME:
|
|
|
|
case SHUTDOWN_TIME:
|
2012-08-30 16:45:27 +00:00
|
|
|
log_out(usr);
|
2004-03-08 20:02:23 +00:00
|
|
|
FirstTime = ut_timecopy; /* shouldn't be needed */
|
1994-09-26 22:12:27 +00:00
|
|
|
break;
|
2010-01-13 18:14:59 +00:00
|
|
|
case USER_PROCESS:
|
1994-09-26 22:12:27 +00:00
|
|
|
/*
|
2012-08-30 16:45:27 +00:00
|
|
|
* If they came in on pts/..., then it is only
|
|
|
|
* a login session if the ut_host field is non-empty.
|
1994-09-26 22:12:27 +00:00
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
if (strncmp(usr->ut_line, "pts/", 4) != 0 ||
|
2010-01-13 18:14:59 +00:00
|
|
|
*usr->ut_host != '\0')
|
2012-08-30 16:45:27 +00:00
|
|
|
log_in(usr);
|
2010-01-13 18:14:59 +00:00
|
|
|
break;
|
|
|
|
case DEAD_PROCESS:
|
2012-08-30 16:45:27 +00:00
|
|
|
log_out(usr);
|
1994-09-26 22:12:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-01-13 18:14:59 +00:00
|
|
|
endutxent();
|
2012-08-30 16:45:27 +00:00
|
|
|
(void)gettimeofday(&now, NULL);
|
|
|
|
if (Flags & AC_W)
|
|
|
|
usht.ut_tv = ut_timecopy;
|
2010-04-02 14:30:56 +00:00
|
|
|
else
|
2012-08-30 16:45:27 +00:00
|
|
|
usht.ut_tv = now;
|
2010-01-13 18:14:59 +00:00
|
|
|
usht.ut_type = SHUTDOWN_TIME;
|
1995-05-30 03:57:47 +00:00
|
|
|
|
1994-09-26 22:12:27 +00:00
|
|
|
if (Flags & AC_D) {
|
2012-08-30 16:45:27 +00:00
|
|
|
ltm = localtime(&ut_timecopy.tv_sec);
|
1994-09-26 22:12:27 +00:00
|
|
|
if (day >= 0 && day != ltm->tm_yday) {
|
|
|
|
/*
|
|
|
|
* print yesterday's total
|
|
|
|
*/
|
2004-03-08 20:02:23 +00:00
|
|
|
secs = ut_timecopy;
|
2012-08-30 16:45:27 +00:00
|
|
|
secs.tv_sec -= ltm->tm_sec;
|
|
|
|
secs.tv_sec -= 60 * ltm->tm_min;
|
|
|
|
secs.tv_sec -= 3600 * ltm->tm_hour;
|
|
|
|
secs.tv_usec = 0;
|
|
|
|
show_today(secs);
|
1994-09-26 22:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* anyone still logged in gets time up to now
|
|
|
|
*/
|
2012-08-30 16:45:27 +00:00
|
|
|
log_out(&usht);
|
1994-09-26 22:12:27 +00:00
|
|
|
|
|
|
|
if (Flags & AC_D)
|
2012-08-30 16:45:27 +00:00
|
|
|
show_today(now);
|
1994-09-26 22:12:27 +00:00
|
|
|
else {
|
|
|
|
if (Flags & AC_P)
|
2012-08-30 16:45:27 +00:00
|
|
|
show_users();
|
1994-09-26 22:12:27 +00:00
|
|
|
show("total", Total);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-30 16:45:27 +00:00
|
|
|
static void
|
2004-03-03 02:41:21 +00:00
|
|
|
usage(void)
|
1994-09-26 22:12:27 +00:00
|
|
|
{
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
#ifdef CONSOLE_TTY
|
|
|
|
"ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n");
|
|
|
|
#else
|
|
|
|
"ac [-dp] [-t tty] [-w wtmp] [users ...]\n");
|
|
|
|
#endif
|
|
|
|
exit(1);
|
|
|
|
}
|