Annotation of embedaddon/sudo/plugins/sudoers/boottime.c, revision 1.1.1.3
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 */
31: #ifdef HAVE_STRING_H
32: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
33: # include <memory.h>
34: # endif
35: # include <string.h>
36: #endif /* HAVE_STRING_H */
37: #ifdef HAVE_STRINGS_H
38: # include <strings.h>
39: #endif /* HAVE_STRINGS_H */
40: #include <limits.h>
41: #if TIME_WITH_SYS_TIME
42: # include <time.h>
43: #endif
44: #ifndef __linux__
45: # if defined(HAVE_SYSCTL) && defined(KERN_BOOTTIME)
46: # include <sys/sysctl.h>
47: # elif defined(HAVE_GETUTXID)
48: # include <utmpx.h>
49: # elif defined(HAVE_GETUTID)
50: # include <utmp.h>
51: # endif
52: #endif /* !__linux__ */
53:
54: #include "missing.h"
1.1.1.2 misho 55: #include "sudo_debug.h"
1.1 misho 56:
57: /*
58: * Fill in a struct timeval with the time the system booted.
59: * Returns 1 on success and 0 on failure.
60: */
61:
62: #if defined(__linux__)
63: int
64: get_boottime(struct timeval *tv)
65: {
1.1.1.3 ! misho 66: char *ep, *line = NULL;
1.1 misho 67: size_t linesize = 0;
68: ssize_t len;
69: FILE * fp;
1.1.1.2 misho 70: debug_decl(get_boottime, SUDO_DEBUG_UTIL)
1.1 misho 71:
72: /* read btime from /proc/stat */
73: fp = fopen("/proc/stat", "r");
74: if (fp != NULL) {
75: while ((len = getline(&line, &linesize, fp)) != -1) {
76: if (strncmp(line, "btime ", 6) == 0) {
1.1.1.3 ! misho 77: #ifdef HAVE_STRTOLL
! 78: long long llval = strtoll(line + 6, &ep, 10);
! 79: if (line[6] != '\0' && *ep == '\0' && (time_t)llval == llval) {
! 80: tv->tv_sec = (time_t)llval;
! 81: tv->tv_usec = 0;
! 82: debug_return_bool(1);
! 83: }
! 84: #else
! 85: long lval = strtol(line + 6, &ep, 10);
! 86: if (line[6] != '\0' && *ep == '\0' && (time_t)lval == lval) {
! 87: tv->tv_sec = (time_t)llval;
! 88: tv->tv_usec = 0;
! 89: debug_return_bool(1);
! 90: }
! 91: #endif
1.1 misho 92: }
93: }
94: fclose(fp);
95: free(line);
96: }
97:
1.1.1.2 misho 98: debug_return_bool(0);
1.1 misho 99: }
100:
101: #elif defined(HAVE_SYSCTL) && defined(KERN_BOOTTIME)
102:
103: int
104: get_boottime(struct timeval *tv)
105: {
106: size_t size;
107: int mib[2];
1.1.1.2 misho 108: debug_decl(get_boottime, SUDO_DEBUG_UTIL)
1.1 misho 109:
110: mib[0] = CTL_KERN;
111: mib[1] = KERN_BOOTTIME;
112: size = sizeof(*tv);
113: if (sysctl(mib, 2, tv, &size, NULL, 0) != -1)
1.1.1.2 misho 114: debug_return_bool(1);
1.1 misho 115:
1.1.1.2 misho 116: debug_return_bool(0);
1.1 misho 117: }
118:
119: #elif defined(HAVE_GETUTXID)
120:
121: int
122: get_boottime(struct timeval *tv)
123: {
124: struct utmpx *ut, key;
1.1.1.2 misho 125: debug_decl(get_boottime, SUDO_DEBUG_UTIL)
1.1 misho 126:
127: memset(&key, 0, sizeof(key));
128: key.ut_type = BOOT_TIME;
129: setutxent();
130: if ((ut = getutxid(&key)) != NULL) {
131: tv->tv_sec = ut->ut_tv.tv_sec;
132: tv->tv_usec = ut->ut_tv.tv_usec;
133: }
134: endutxent();
1.1.1.2 misho 135: debug_return_bool(ut != NULL);
1.1 misho 136: }
137:
138: #elif defined(HAVE_GETUTID)
139:
140: int
141: get_boottime(struct timeval *tv)
142: {
143: struct utmp *ut, key;
1.1.1.2 misho 144: debug_decl(get_boottime, SUDO_DEBUG_UTIL)
1.1 misho 145:
146: memset(&key, 0, sizeof(key));
147: key.ut_type = BOOT_TIME;
148: setutent();
149: if ((ut = getutid(&key)) != NULL) {
150: tv->tv_sec = ut->ut_time;
151: tv->tv_usec = 0;
152: }
153: endutent();
1.1.1.2 misho 154: debug_return_bool(ut != NULL);
1.1 misho 155: }
156:
157: #else
158:
159: int
160: get_boottime(struct timeval *tv)
161: {
1.1.1.2 misho 162: debug_decl(get_boottime, SUDO_DEBUG_UTIL)
163: debug_return_bool(0);
1.1 misho 164: }
165: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>