aboutsummaryrefslogtreecommitdiffstats
path: root/lib/boot-time-aux.h
blob: f1f940b12e48933aa70f9b482675460f4ce71fce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* Auxiliary functions for determining the time when the machine last booted.
   Copyright (C) 2023-2025 Free Software Foundation, Inc.

   This file is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   This file is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>.  */

#define SIZEOF(a) (sizeof(a)/sizeof(a[0]))

#if defined __linux__ || defined __ANDROID__

/* Store the uptime counter, as managed by the Linux kernel, in *P_UPTIME.
   Return 0 upon success, -1 upon failure.  */
_GL_ATTRIBUTE_MAYBE_UNUSED
static int
get_linux_uptime (struct timespec *p_uptime)
{
  /* The clock_gettime facility returns the uptime with a resolution of 1 µsec.
     It is available with glibc >= 2.14, Android, or musl libc.
     In glibc < 2.17 it required linking with librt.  */
# if !defined __GLIBC__ || 2 < __GLIBC__ + (17 <= __GLIBC_MINOR__)
  if (clock_gettime (CLOCK_BOOTTIME, p_uptime) >= 0)
    return 0;
# endif

  /* /proc/uptime contains the uptime with a resolution of 0.01 sec.
     But it does not have read permissions on Android.  */
# if !defined __ANDROID__
  FILE *fp = fopen ("/proc/uptime", "re");
  if (fp != NULL)
    {
      char buf[32 + 1];
      size_t n = fread (buf, 1, sizeof (buf) - 1, fp);
      fclose (fp);
      if (n > 0)
        {
          buf[n] = '\0';
          /* buf now contains two values: the uptime and the idle time.  */
          time_t s = 0;
          char *p;
          for (p = buf; '0' <= *p && *p <= '9'; p++)
            s = 10 * s + (*p - '0');
          if (buf < p)
            {
              long ns = 0;
              if (*p++ == '.')
                for (int i = 0; i < 9; i++)
                  ns = 10 * ns + ('0' <= *p && *p <= '9' ? *p++ - '0' : 0);
              p_uptime->tv_sec = s;
              p_uptime->tv_nsec = ns;
              return 0;
            }
        }
    }
# endif

# if HAVE_DECL_SYSINFO /* not available in Android API < 9 */
  /* The sysinfo call returns the uptime with a resolution of 1 sec only.  */
  struct sysinfo info;
  if (sysinfo (&info) >= 0)
    {
      p_uptime->tv_sec = info.uptime;
      p_uptime->tv_nsec = 0;
      return 0;
    }
# endif

  return -1;
}

#endif

#if defined __linux__ && !defined __ANDROID__

static int
get_linux_boot_time_fallback (struct timespec *p_boot_time)
{
  /* On Devuan with the 'runit' init system and on Artix with the 's6' init
     system, UTMP_FILE contains USER_PROCESS and other entries, but no
     BOOT_TIME entry.
     On Alpine Linux, UTMP_FILE is not filled.  It is always empty.
     So, in both cases, get the time stamp of a file that gets touched only
     during the boot process.  */

  const char * const boot_touched_files[] =
    {
      "/var/lib/systemd/random-seed", /* seen on distros with systemd */
      "/var/lib/urandom/random-seed", /* seen on Devuan with runit */
      "/var/lib/random-seed",         /* seen on Artix with s6 */
      /* This must come last, since on several distros /var/run/utmp is
         modified when a user logs in, i.e. long after boot.  */
      "/var/run/utmp"                 /* seen on Alpine Linux with OpenRC */
    };
  for (idx_t i = 0; i < SIZEOF (boot_touched_files); i++)
    {
      const char *filename = boot_touched_files[i];
      struct stat statbuf;
      if (stat (filename, &statbuf) >= 0)
        {
          struct timespec boot_time = get_stat_mtime (&statbuf);
          /* On Alpine 3.20.0_rc2 /var/run/utmp was observed with bogus
             timestamps of ~10 s.  Reject timestamps before
             2005-07-25 23:34:15 UTC (1122334455), as neither Alpine
             nor Devuan existed then.  */
          if (boot_time.tv_sec >= 1122334455)
            {
              *p_boot_time = boot_time;
              return 0;
            }
        }
    }
  return -1;
}

/* The following approach is only usable as a fallback, because it is of
   the form
     boot_time = (time now) - (kernel's ktime_get_boottime[_ts64] ())
   and therefore produces wrong values after the date has been bumped in the
   running system, which happens frequently if the system is running in a
   virtual machine and this VM has been put into "saved" or "sleep" state
   and then resumed.  */
static int
get_linux_boot_time_final_fallback (struct timespec *p_boot_time)
{
  struct timespec uptime;
  if (get_linux_uptime (&uptime) >= 0)
    {
      struct timespec result;
# if !defined __GLIBC__ || 2 < __GLIBC__ + (16 <= __GLIBC_MINOR__)
      /* Better than:
      if (0 <= clock_gettime (CLOCK_REALTIME, &result))
         because timespec_get does not need -lrt in glibc 2.16.
      */
      if (! timespec_get (&result, TIME_UTC))
        return -1;
#  else
      /* Fall back on lower-res approach that does not need -lrt.
         This is good enough; on these hosts UPTIME is even lower-res.  */
      struct timeval tv;
      int r = gettimeofday (&tv, NULL);
      if (r < 0)
        return r;
      result.tv_sec = tv.tv_sec;
      result.tv_nsec = tv.tv_usec * 1000;
#  endif

      if (result.tv_nsec < uptime.tv_nsec)
        {
          result.tv_nsec += 1000000000;
          result.tv_sec -= 1;
        }
      result.tv_sec -= uptime.tv_sec;
      result.tv_nsec -= uptime.tv_nsec;
      *p_boot_time = result;
      return 0;
    }
  return -1;
}

#endif

#if defined __ANDROID__

static int
get_android_boot_time (struct timespec *p_boot_time)
{
  /* On Android, there is no /var, and normal processes don't have access
     to system files.  Therefore use the kernel's uptime counter, although
     it produces wrong values after the date has been bumped in the running
     system.  */
  struct timespec uptime;
  if (get_linux_uptime (&uptime) >= 0)
    {
      struct timespec result;
      if (clock_gettime (CLOCK_REALTIME, &result) >= 0)
        {
          if (result.tv_nsec < uptime.tv_nsec)
            {
              result.tv_nsec += 1000000000;
              result.tv_sec -= 1;
            }
          result.tv_sec -= uptime.tv_sec;
          result.tv_nsec -= uptime.tv_nsec;
          *p_boot_time = result;
          return 0;
        }
    }
  return -1;
}

#endif

#if defined __OpenBSD__

static int
get_openbsd_boot_time (struct timespec *p_boot_time)
{
  /* On OpenBSD, UTMP_FILE is not filled.  It contains only dummy entries.
     So, get the time stamp of a file that gets touched only during the
     boot process.  */
  const char * const boot_touched_files[] =
    {
      "/var/db/host.random",
      "/var/run/utmp"
    };
  for (idx_t i = 0; i < SIZEOF (boot_touched_files); i++)
    {
      const char *filename = boot_touched_files[i];
      struct stat statbuf;
      if (stat (filename, &statbuf) >= 0)
        {
          *p_boot_time = get_stat_mtime (&statbuf);
          return 0;
        }
    }
  return -1;
}

#endif

#if HAVE_SYS_SYSCTL_H && HAVE_SYSCTL \
    && defined CTL_KERN && defined KERN_BOOTTIME \
    && !defined __minix
/* macOS, FreeBSD, GNU/kFreeBSD, NetBSD, OpenBSD */
/* On Minix 3.3 this sysctl produces garbage results.  Therefore avoid it.  */

/* The following approach is only usable as a fallback, because it produces
   wrong values after the date has been bumped in the running system, which
   happens frequently if the system is running in a virtual machine and this
   VM has been put into "saved" or "sleep" state and then resumed.  */
static int
get_bsd_boot_time_final_fallback (struct timespec *p_boot_time)
{
  static int request[2] = { CTL_KERN, KERN_BOOTTIME };
  struct timeval result;
  size_t result_len = sizeof result;

  if (sysctl (request, 2, &result, &result_len, NULL, 0) >= 0)
    {
      p_boot_time->tv_sec = result.tv_sec;
      p_boot_time->tv_nsec = result.tv_usec * 1000;
      return 0;
    }
  return -1;
}

#endif

#if defined __HAIKU__

static int
get_haiku_boot_time (struct timespec *p_boot_time)
{
  /* On Haiku, /etc/utmp does not exist.  During boot,
       1. the current time is restored, but possibly with a wrong time zone,
          that is, with an offset of a few hours,
       2. some symlinks and files get created,
       3. the various devices are brought up, in particular the network device,
       4. the correct date and time is set,
       5. some more device nodes get created.
     The boot time can be retrieved by looking at a directory created during
     phase 5, such as /dev/input.  */
  const char * const boot_touched_file = "/dev/input";
  struct stat statbuf;
  if (stat (boot_touched_file, &statbuf) >= 0)
    {
      *p_boot_time = get_stat_mtime (&statbuf);
      return 0;
    }
  return -1;
}

#endif

#if HAVE_OS_H /* BeOS, Haiku */

/* The following approach is only usable as a fallback, because it produces
   wrong values after the date has been bumped in the running system, which
   happens frequently if the system is running in a virtual machine and this
   VM has been put into "saved" or "sleep" state and then resumed.  */
static int
get_haiku_boot_time_final_fallback (struct timespec *p_boot_time)
{
  system_info si;

  get_system_info (&si);
  p_boot_time->tv_sec = si.boot_time / 1000000;
  p_boot_time->tv_nsec = (si.boot_time % 1000000) * 1000;
  return 0;
}

#endif

#if defined __CYGWIN__ || defined _WIN32

static int
get_windows_boot_time (struct timespec *p_boot_time)
{
  /* On Cygwin, /var/run/utmp is empty.
     On native Windows, <utmpx.h> and <utmp.h> don't exist.
     Instead, on Windows, the boot time can be retrieved by looking at the
     time stamp of a file that (normally) gets touched only during the boot
     process, namely C:\pagefile.sys.  */
  const char * const boot_touched_files[] =
    {
      #if defined __CYGWIN__ && !defined _WIN32
      /* It is more portable to use /proc/cygdrive/c than /cygdrive/c.  */
      "/proc/cygdrive/c/pagefile.sys",
      /* A fallback, working around a Cygwin 3.5.3 bug.  It has a modification
         time about 1.5 minutes after the last boot; but that's better than
         nothing.  */
      "/proc/cygdrive/c/ProgramData/Microsoft/Windows/DeviceMetadataCache/dmrc.idx"
      #else
      "C:\\pagefile.sys"
      #endif
    };
  for (idx_t i = 0; i < SIZEOF (boot_touched_files); i++)
    {
      const char *filename = boot_touched_files[i];
      struct stat statbuf;
      if (stat (filename, &statbuf) >= 0)
        {
# if defined __CYGWIN__ && !defined _WIN32
          /* Work around a Cygwin 3.5.3 bug.
             <https://cygwin.com/pipermail/cygwin/2024-May/255931.html>  */
          if (!S_ISDIR (statbuf.st_mode))
# endif
            {
              *p_boot_time = get_stat_mtime (&statbuf);
              return 0;
            }
        }
    }
  return -1;
}

# ifndef __CYGWIN__
#  if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)

/* Don't assume that UNICODE is not defined.  */
#   undef LoadLibrary
#   define LoadLibrary LoadLibraryA

/* Avoid warnings from gcc -Wcast-function-type.  */
#   define GetProcAddress \
     (void *) GetProcAddress

/* GetTickCount64 is only available on Windows Vista and later.  */
typedef ULONGLONG (WINAPI * GetTickCount64FuncType) (void);

static GetTickCount64FuncType GetTickCount64Func = NULL;
static BOOL initialized = FALSE;

static void
initialize (void)
{
  HMODULE kernel32 = LoadLibrary ("kernel32.dll");
  if (kernel32 != NULL)
    {
      GetTickCount64Func =
        (GetTickCount64FuncType) GetProcAddress (kernel32, "GetTickCount64");
    }
  initialized = TRUE;
}

#  else

#   define GetTickCount64Func GetTickCount64

#  endif

/* Fallback for Windows in the form:
     boot time = current time - uptime
   This uses the GetTickCount64 function which is only available on Windows
   Vista and later. See:
   <https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-gettickcount64>.  */
static int
get_windows_boot_time_fallback (struct timespec *p_boot_time)
{
#  if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)
  if (! initialized)
    initialize ();
#  endif
  if (GetTickCount64Func != NULL)
    {
      ULONGLONG uptime_ms = GetTickCount64Func ();
      struct timespec uptime;
      struct timespec result;
      struct timeval tv;
      if (gettimeofday (&tv, NULL) >= 0)
        {
          uptime.tv_sec = uptime_ms / 1000;
          uptime.tv_nsec = (uptime_ms % 1000) * 1000000;
          result.tv_sec = tv.tv_sec;
          result.tv_nsec = tv.tv_usec * 1000;
          if (result.tv_nsec < uptime.tv_nsec)
            {
              result.tv_nsec += 1000000000;
              result.tv_sec -= 1;
            }
          result.tv_sec -= uptime.tv_sec;
          result.tv_nsec -= uptime.tv_nsec;
          *p_boot_time = result;
          return 0;
        }
    }
  return -1;
}

# endif
#endif