File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / sudo / plugins / sudoers / boottime.c
Revision 1.1.1.4 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Sun Jun 15 16:12:54 2014 UTC (10 years, 3 months ago) by misho
Branches: sudo, MAIN
CVS tags: v1_8_10p3_0, v1_8_10p3, HEAD
sudo v 1.8.10p3

    1: /*
    2:  * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com>
    3:  *
    4:  * Permission to use, copy, modify, and distribute this software for any
    5:  * purpose with or without fee is hereby granted, provided that the above
    6:  * copyright notice and this permission notice appear in all copies.
    7:  *
    8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   15:  */
   16: 
   17: #include <config.h>
   18: 
   19: #include <sys/types.h>
   20: #include <sys/time.h>
   21: 
   22: #include <stdio.h>
   23: #ifdef STDC_HEADERS
   24: # include <stdlib.h>
   25: # include <stddef.h>
   26: #else
   27: # ifdef HAVE_STDLIB_H
   28: #  include <stdlib.h>
   29: # endif
   30: #endif /* STDC_HEADERS */
   31: #ifdef HAVE_STDBOOL_H
   32: # include <stdbool.h>
   33: #else
   34: # include "compat/stdbool.h"
   35: #endif /* HAVE_STDBOOL_H */
   36: #ifdef HAVE_STRING_H
   37: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
   38: #  include <memory.h>
   39: # endif
   40: # include <string.h>
   41: #endif /* HAVE_STRING_H */
   42: #ifdef HAVE_STRINGS_H
   43: # include <strings.h>
   44: #endif /* HAVE_STRINGS_H */
   45: #include <limits.h>
   46: #ifdef TIME_WITH_SYS_TIME
   47: # include <time.h>
   48: #endif
   49: #ifndef __linux__
   50: # if defined(HAVE_SYSCTL) && defined(KERN_BOOTTIME)
   51: #  include <sys/sysctl.h>
   52: # elif defined(HAVE_GETUTXID)
   53: #  include <utmpx.h>
   54: # elif defined(HAVE_GETUTID)
   55: #  include <utmp.h>
   56: # endif
   57: #endif /* !__linux__ */
   58: 
   59: #include "missing.h"
   60: #include "sudo_debug.h"
   61: 
   62: /*
   63:  * Fill in a struct timeval with the time the system booted.
   64:  * Returns 1 on success and 0 on failure.
   65:  */
   66: 
   67: #if defined(__linux__)
   68: bool
   69: get_boottime(struct timeval *tv)
   70: {
   71:     char *ep, *line = NULL;
   72:     size_t linesize = 0;
   73:     bool found = false;
   74:     ssize_t len;
   75:     FILE *fp;
   76:     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
   77: 
   78:     /* read btime from /proc/stat */
   79:     fp = fopen("/proc/stat", "r");
   80:     if (fp != NULL) {
   81: 	while ((len = getline(&line, &linesize, fp)) != -1) {
   82: 	    if (strncmp(line, "btime ", 6) == 0) {
   83: 		long long llval = strtonum(line + 6, 1, LLONG_MAX, NULL);
   84: 		if (llval > 0) {
   85: 		    tv->tv_sec = (time_t)llval;
   86: 		    tv->tv_usec = 0;
   87: 		    found = true;
   88: 		    break;
   89: 		}
   90: 	    }
   91: 	}
   92: 	fclose(fp);
   93: 	free(line);
   94:     }
   95: 
   96:     debug_return_bool(found);
   97: }
   98: 
   99: #elif defined(HAVE_SYSCTL) && defined(KERN_BOOTTIME)
  100: 
  101: bool
  102: get_boottime(struct timeval *tv)
  103: {
  104:     size_t size;
  105:     int mib[2];
  106:     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
  107: 
  108:     mib[0] = CTL_KERN;
  109:     mib[1] = KERN_BOOTTIME;
  110:     size = sizeof(*tv);
  111:     if (sysctl(mib, 2, tv, &size, NULL, 0) != -1)
  112: 	debug_return_bool(true);
  113: 
  114:     debug_return_bool(false);
  115: }
  116: 
  117: #elif defined(HAVE_GETUTXID)
  118: 
  119: int
  120: get_boottime(struct timeval *tv)
  121: {
  122:     struct utmpx *ut, key;
  123:     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
  124: 
  125:     memset(&key, 0, sizeof(key));
  126:     key.ut_type = BOOT_TIME;
  127:     setutxent();
  128:     if ((ut = getutxid(&key)) != NULL) {
  129: 	tv->tv_sec = ut->ut_tv.tv_sec;
  130: 	tv->tv_usec = ut->ut_tv.tv_usec;
  131:     }
  132:     endutxent();
  133:     debug_return_bool(ut != NULL);
  134: }
  135: 
  136: #elif defined(HAVE_GETUTID)
  137: 
  138: int
  139: get_boottime(struct timeval *tv)
  140: {
  141:     struct utmp *ut, key;
  142:     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
  143: 
  144:     memset(&key, 0, sizeof(key));
  145:     key.ut_type = BOOT_TIME;
  146:     setutent();
  147:     if ((ut = getutid(&key)) != NULL) {
  148: 	tv->tv_sec = ut->ut_time;
  149: 	tv->tv_usec = 0;
  150:     }
  151:     endutent();
  152:     debug_return_bool(ut != NULL);
  153: }
  154: 
  155: #else
  156: 
  157: int
  158: get_boottime(struct timeval *tv)
  159: {
  160:     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
  161:     debug_return_bool(false);
  162: }
  163: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>