From: Todd C. Miller Date: Thu, 3 Jun 2010 11:41:04 +0000 (-0400) Subject: get_boottime() now fills in a timeval struct X-Git-Tag: SUDO_1_7_3~133 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7967f60fb64cc682f92bd856a4fdd4ceb0eab2db;p=sudo get_boottime() now fills in a timeval struct --HG-- branch : 1.7 --- diff --git a/boottime.c b/boottime.c index 6789b9a13..d78b6d6f4 100644 --- a/boottime.c +++ b/boottime.c @@ -44,11 +44,16 @@ #include "compat.h" #include "missing.h" +/* + * Fill in a struct timeval with the time the system booted. + * Returns TRUE on success and FALSE on failure. + */ + #if defined(__linux__) -time_t -get_boottime() +int +get_boottime(tv) + struct timeval *tv; { - time_t boottime = 0; char *line = NULL; size_t linesize = 0; ssize_t len; @@ -59,77 +64,80 @@ get_boottime() if (fp != NULL) { while ((len = getline(&line, &linesize, fp)) != -1) { if (strncmp(line, "btime ", 6) == 0) { - boottime = atoi(line + 6); - break; + tv->tv_sec = atoi(line + 6); + tv->tv_usec = 0; + return TRUE; } } fclose(fp); free(line); } - return(boottime); + return FALSE; } #elif defined(HAVE_SYSCTL) && defined(KERN_BOOTTIME) -time_t -get_boottime() +int +get_boottime(tv) + struct timeval *tv; { - struct timeval tv; - time_t boottime = 0; size_t size; int mib[2]; mib[0] = CTL_KERN; mib[1] = KERN_BOOTTIME; - size = sizeof(tv); - if (sysctl(mib, 2, &tv, &size, NULL, 0) != -1) - boottime = tv.tv_sec; + size = sizeof(*tv); + if (sysctl(mib, 2, tv, &size, NULL, 0) != -1) + return TRUE; - return(boottime); + return FALSE; } #elif defined(HAVE_GETUTXID) #include -time_t -get_boottime() +int +get_boottime(tv) + struct timeval *tv; { - time_t boottime = 0; struct utmpx *ut, key; zero_bytes(&key, sizeof(key)); - key.ut_type = BOOT_TIME; + key.ut_type = BOOT_TIME; if ((ut = getutxid(&key)) != NULL) { - boottime = ut->ut_tv.tv_sec; + tv->tv_sec = ut->ut_tv.tv_sec; + tv->tv_usec = ut->ut_tv.tv_usec; endutxent(); } - return(boottime); + return ut != NULL; } #elif defined(HAVE_GETUTID) #include -time_t -get_boottime() +int +get_boottime(tv) + struct timeval *tv; { - time_t boottime = 0; struct utmp *ut, key; zero_bytes(&key, sizeof(key)); - key.ut_type = BOOT_TIME; + key.ut_type = BOOT_TIME; if ((ut = getutid(&key)) != NULL) { - boottime = ut->ut_time; + tv->tv_sec = ut->ut_time; + tv->tv_usec = 0; endutent(); } - return(boottime); + return ut != NULL; } #else -time_t -get_boottime() +int +get_boottime(tv) + struct timeval *tv { - return(0); + return FALSE; } #endif diff --git a/check.c b/check.c index 568b9aac3..b9d4be7b8 100644 --- a/check.c +++ b/check.c @@ -392,7 +392,8 @@ timestamp_status(timestampdir, timestampfile, user, flags) int flags; { struct stat sb; - time_t boottime, now; + struct timeval boottime, mtime; + time_t now; char *dirparent = def_timestampdir; int status = TS_ERROR; /* assume the worst */ @@ -530,29 +531,29 @@ timestamp_status(timestampdir, timestampfile, user, flags) * If the file/dir exists and we are not removing it, check its mtime. */ if (status == TS_OLD && !ISSET(flags, TS_REMOVE)) { + mtim_get(&sb, &mtime); /* Negative timeouts only expire manually (sudo -k). */ - if (def_timestamp_timeout < 0 && sb.st_mtime != 0) + if (def_timestamp_timeout < 0 && mtime.tv_sec != 0) status = TS_CURRENT; else { - /* XXX - should use timeval here */ now = time(NULL); - boottime = get_boottime(); if (def_timestamp_timeout && - now - sb.st_mtime < 60 * def_timestamp_timeout) { + now - mtime.tv_sec < 60 * def_timestamp_timeout) { /* * Check for bogus time on the stampfile. The clock may * have been set back or someone could be trying to spoof us. */ - if (sb.st_mtime > now + 60 * def_timestamp_timeout * 2) { + if (mtime.tv_sec > now + 60 * def_timestamp_timeout * 2) { + time_t tv_sec = (time_t)mtime.tv_sec; log_error(NO_EXIT, "timestamp too far in the future: %20.20s", - 4 + ctime(&sb.st_mtime)); + 4 + ctime(&tv_sec)); if (timestampfile) (void) unlink(timestampfile); else (void) rmdir(timestampdir); status = TS_MISSING; - } else if (sb.st_mtime < boottime) { + } else if (get_boottime(&boottime) && timercmp(&mtime, &boottime, <)) { status = TS_OLD; } else { status = TS_CURRENT; diff --git a/sudo.h b/sudo.h index 4db6d1adf..d5802292f 100644 --- a/sudo.h +++ b/sudo.h @@ -284,7 +284,7 @@ int term_noecho __P((int)); int term_raw __P((int, int, int)); int term_restore __P((int, int)); char *get_timestr __P((time_t, int)); -time_t get_boottime __P((void)); +int get_boottime __P((struct timeval *)); int user_in_group __P((struct passwd *, const char *)); YY_DECL;