Imported Upstream version 1.8.6p8
[debian/sudo] / plugins / sudoers / boottime.c
1 /*
2  * Copyright (c) 2009-2011 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/param.h>
20 #include <sys/types.h>
21 #include <sys/time.h>
22
23 #include <stdio.h>
24 #ifdef STDC_HEADERS
25 # include <stdlib.h>
26 # include <stddef.h>
27 #else
28 # ifdef HAVE_STDLIB_H
29 #  include <stdlib.h>
30 # endif
31 #endif /* STDC_HEADERS */
32 #ifdef HAVE_STRING_H
33 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
34 #  include <memory.h>
35 # endif
36 # include <string.h>
37 #endif /* HAVE_STRING_H */
38 #ifdef HAVE_STRINGS_H
39 # include <strings.h>
40 #endif /* HAVE_STRINGS_H */
41 #include <limits.h>
42 #if TIME_WITH_SYS_TIME
43 # include <time.h>
44 #endif
45 #ifndef __linux__
46 # if defined(HAVE_SYSCTL) && defined(KERN_BOOTTIME)
47 #  include <sys/sysctl.h>
48 # elif defined(HAVE_GETUTXID)
49 #  include <utmpx.h>
50 # elif defined(HAVE_GETUTID)
51 #  include <utmp.h>
52 # endif
53 #endif /* !__linux__ */
54
55 #include "missing.h"
56 #include "sudo_debug.h"
57
58 /*
59  * Fill in a struct timeval with the time the system booted.
60  * Returns 1 on success and 0 on failure.
61  */
62
63 #if defined(__linux__)
64 int
65 get_boottime(struct timeval *tv)
66 {
67     char *line = NULL;
68     size_t linesize = 0;
69     ssize_t len;
70     FILE * fp;
71     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
72
73     /* read btime from /proc/stat */
74     fp = fopen("/proc/stat", "r");
75     if (fp != NULL) {
76         while ((len = getline(&line, &linesize, fp)) != -1) {
77             if (strncmp(line, "btime ", 6) == 0) {
78                 tv->tv_sec = atoi(line + 6);
79                 tv->tv_usec = 0;
80                 debug_return_bool(1);
81             }
82         }
83         fclose(fp);
84         free(line);
85     }
86
87     debug_return_bool(0);
88 }
89
90 #elif defined(HAVE_SYSCTL) && defined(KERN_BOOTTIME)
91
92 int
93 get_boottime(struct timeval *tv)
94 {
95     size_t size;
96     int mib[2];
97     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
98
99     mib[0] = CTL_KERN;
100     mib[1] = KERN_BOOTTIME;
101     size = sizeof(*tv);
102     if (sysctl(mib, 2, tv, &size, NULL, 0) != -1)
103         debug_return_bool(1);
104
105     debug_return_bool(0);
106 }
107
108 #elif defined(HAVE_GETUTXID)
109
110 int
111 get_boottime(struct timeval *tv)
112 {
113     struct utmpx *ut, key;
114     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
115
116     memset(&key, 0, sizeof(key));
117     key.ut_type = BOOT_TIME;
118     setutxent();
119     if ((ut = getutxid(&key)) != NULL) {
120         tv->tv_sec = ut->ut_tv.tv_sec;
121         tv->tv_usec = ut->ut_tv.tv_usec;
122     }
123     endutxent();
124     debug_return_bool(ut != NULL);
125 }
126
127 #elif defined(HAVE_GETUTID)
128
129 int
130 get_boottime(struct timeval *tv)
131 {
132     struct utmp *ut, key;
133     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
134
135     memset(&key, 0, sizeof(key));
136     key.ut_type = BOOT_TIME;
137     setutent();
138     if ((ut = getutid(&key)) != NULL) {
139         tv->tv_sec = ut->ut_time;
140         tv->tv_usec = 0;
141     }
142     endutent();
143     debug_return_bool(ut != NULL);
144 }
145
146 #else
147
148 int
149 get_boottime(struct timeval *tv)
150 {
151     debug_decl(get_boottime, SUDO_DEBUG_UTIL)
152     debug_return_bool(0);
153 }
154 #endif