prepare to upload
[debian/sudo] / boottime.c
1 /*
2  * Copyright (c) 2009-2010 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
46 #ifdef HAVE_SYSCTL
47 # include <sys/sysctl.h>
48 #endif
49
50 #include "compat.h"
51 #include "missing.h"
52
53 /*
54  * Fill in a struct timeval with the time the system booted.
55  * Returns 1 on success and 0 on failure.
56  */
57
58 #if defined(__linux__)
59 int
60 get_boottime(tv)
61     struct timeval *tv;
62 {
63     char *line = NULL;
64     size_t linesize = 0;
65     ssize_t len;
66     FILE * fp;
67
68     /* read btime from /proc/stat */
69     fp = fopen("/proc/stat", "r");
70     if (fp != NULL) {
71         while ((len = getline(&line, &linesize, fp)) != -1) {
72             if (strncmp(line, "btime ", 6) == 0) {
73                 tv->tv_sec = atoi(line + 6);
74                 tv->tv_usec = 0;
75                 return 1;
76             }
77         }
78         fclose(fp);
79         free(line);
80     }
81
82     return 0;
83 }
84
85 #elif defined(HAVE_SYSCTL) && defined(KERN_BOOTTIME)
86
87 int
88 get_boottime(tv)
89     struct timeval *tv;
90 {
91     size_t size;
92     int mib[2];
93
94     mib[0] = CTL_KERN;
95     mib[1] = KERN_BOOTTIME;
96     size = sizeof(*tv);
97     if (sysctl(mib, 2, tv, &size, NULL, 0) != -1)
98         return 1;
99
100     return 0;
101 }
102
103 #elif defined(HAVE_GETUTXID)
104
105 #include <utmpx.h>
106 int
107 get_boottime(tv)
108     struct timeval *tv;
109 {
110     struct utmpx *ut, key;
111
112     memset(&key, 0, sizeof(key));
113     key.ut_type = BOOT_TIME;
114     if ((ut = getutxid(&key)) != NULL) {
115         tv->tv_sec = ut->ut_tv.tv_sec;
116         tv->tv_usec = ut->ut_tv.tv_usec;
117         endutxent();
118     }
119     return ut != NULL;
120 }
121
122 #elif defined(HAVE_GETUTID)
123
124 #include <utmp.h>
125 int
126 get_boottime(tv)
127     struct timeval *tv;
128 {
129     struct utmp *ut, key;
130
131     memset(&key, 0, sizeof(key));
132     key.ut_type = BOOT_TIME;
133     if ((ut = getutid(&key)) != NULL) {
134         tv->tv_sec = ut->ut_time;
135         tv->tv_usec = 0;
136         endutent();
137     }
138     return ut != NULL;
139 }
140
141 #else
142
143 int
144 get_boottime(tv)
145     struct timeval *tv;
146 {
147     return 0;
148 }
149 #endif