summaryrefslogtreecommitdiffstats
path: root/login-utils/last.c
diff options
context:
space:
mode:
authorRuediger Meier2015-03-24 12:22:13 +0100
committerRuediger Meier2015-03-25 10:36:52 +0100
commit2e81d99870920a8f9a79a00221839ce8c8cb3400 (patch)
treec4808b6ebecac8a311d280f94959267d3dd3538d /login-utils/last.c
parentlast: fix first (current) runlevel line (diff)
downloadkernel-qcow2-util-linux-2e81d99870920a8f9a79a00221839ce8c8cb3400.tar.gz
kernel-qcow2-util-linux-2e81d99870920a8f9a79a00221839ce8c8cb3400.tar.xz
kernel-qcow2-util-linux-2e81d99870920a8f9a79a00221839ce8c8cb3400.zip
last, fix race when comparing time stamps
It is just luck if two time() calls happen within the same second. Introduced in 31d28e09. Actually I don't like adding another global variable but this way we avoid bigger refactoring. IMO it's questionable why lastdate, lastdown, etc. are initialized with current time() at all. It looks unsafe to print "still running" always when logout_time = now. Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
Diffstat (limited to 'login-utils/last.c')
-rw-r--r--login-utils/last.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/login-utils/last.c b/login-utils/last.c
index 4a165c2c1..8d82c106e 100644
--- a/login-utils/last.c
+++ b/login-utils/last.c
@@ -138,6 +138,7 @@ static struct last_timefmt timefmts[] = {
/* Global variables */
static unsigned int recsdone; /* Number of records listed */
static time_t lastdate; /* Last date we've seen */
+static time_t currentdate; /* date when we started processing the file */
/* --time-format=option parser */
static int which_time_format(const char *optarg)
@@ -372,7 +373,7 @@ static void trim_trailing_spaces(char *s)
*/
static int list(const struct last_control *ctl, struct utmp *p, time_t logout_time, int what)
{
- time_t secs, utmp_time, now;
+ time_t secs, utmp_time;
char logintime[LAST_TIMESTAMP_LEN];
char logouttime[LAST_TIMESTAMP_LEN];
char length[LAST_TIMESTAMP_LEN];
@@ -428,8 +429,7 @@ static int list(const struct last_control *ctl, struct utmp *p, time_t logout_ti
hours = (secs / 3600) % 24;
days = secs / 86400;
- now = time(NULL);
- if (logout_time == now) {
+ if (logout_time == currentdate) {
if (ctl->time_fmt > LAST_TIMEFTM_SHORT_CTIME) {
sprintf(logouttime, " still running");
length[0] = 0;
@@ -638,7 +638,7 @@ static void process_wtmp_file(const struct last_control *ctl,
/*
* Fill in 'lastdate'
*/
- lastdate = lastrch = lastdown;
+ lastdate = currentdate = lastrch = lastdown;
/*
* Install signal handlers