Annotation of embedaddon/sudo/plugins/sudoers/boottime.c, revision 1.1.1.4

1.1       misho       1: /*
1.1.1.3   misho       2:  * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com>
1.1       misho       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 */
1.1.1.4 ! misho      31: #ifdef HAVE_STDBOOL_H
        !            32: # include <stdbool.h>
        !            33: #else
        !            34: # include "compat/stdbool.h"
        !            35: #endif /* HAVE_STDBOOL_H */
1.1       misho      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>
1.1.1.4 ! misho      46: #ifdef TIME_WITH_SYS_TIME
1.1       misho      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"
1.1.1.2   misho      60: #include "sudo_debug.h"
1.1       misho      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__)
1.1.1.4 ! misho      68: bool
1.1       misho      69: get_boottime(struct timeval *tv)
                     70: {
1.1.1.3   misho      71:     char *ep, *line = NULL;
1.1       misho      72:     size_t linesize = 0;
1.1.1.4 ! misho      73:     bool found = false;
1.1       misho      74:     ssize_t len;
1.1.1.4 ! misho      75:     FILE *fp;
1.1.1.2   misho      76:     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
1.1       misho      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) {
1.1.1.4 ! misho      83:                long long llval = strtonum(line + 6, 1, LLONG_MAX, NULL);
        !            84:                if (llval > 0) {
1.1.1.3   misho      85:                    tv->tv_sec = (time_t)llval;
                     86:                    tv->tv_usec = 0;
1.1.1.4 ! misho      87:                    found = true;
        !            88:                    break;
1.1.1.3   misho      89:                }
1.1       misho      90:            }
                     91:        }
                     92:        fclose(fp);
                     93:        free(line);
                     94:     }
                     95: 
1.1.1.4 ! misho      96:     debug_return_bool(found);
1.1       misho      97: }
                     98: 
                     99: #elif defined(HAVE_SYSCTL) && defined(KERN_BOOTTIME)
                    100: 
1.1.1.4 ! misho     101: bool
1.1       misho     102: get_boottime(struct timeval *tv)
                    103: {
                    104:     size_t size;
                    105:     int mib[2];
1.1.1.2   misho     106:     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
1.1       misho     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)
1.1.1.4 ! misho     112:        debug_return_bool(true);
1.1       misho     113: 
1.1.1.4 ! misho     114:     debug_return_bool(false);
1.1       misho     115: }
                    116: 
                    117: #elif defined(HAVE_GETUTXID)
                    118: 
                    119: int
                    120: get_boottime(struct timeval *tv)
                    121: {
                    122:     struct utmpx *ut, key;
1.1.1.2   misho     123:     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
1.1       misho     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();
1.1.1.2   misho     133:     debug_return_bool(ut != NULL);
1.1       misho     134: }
                    135: 
                    136: #elif defined(HAVE_GETUTID)
                    137: 
                    138: int
                    139: get_boottime(struct timeval *tv)
                    140: {
                    141:     struct utmp *ut, key;
1.1.1.2   misho     142:     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
1.1       misho     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();
1.1.1.2   misho     152:     debug_return_bool(ut != NULL);
1.1       misho     153: }
                    154: 
                    155: #else
                    156: 
                    157: int
                    158: get_boottime(struct timeval *tv)
                    159: {
1.1.1.2   misho     160:     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
1.1.1.4 ! misho     161:     debug_return_bool(false);
1.1       misho     162: }
                    163: #endif

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