File: get_uptime.c

package info (click to toggle)
ngetty 1.1-3.1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 392 kB
  • sloc: ansic: 1,588; makefile: 206; sh: 193; asm: 100
file content (34 lines) | stat: -rw-r--r-- 661 bytes parent folder | download | duplicates (5)
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
#include "sysinfo_defs.h"
#include "boottime_defs.h"

#include <time.h>
#include <sys/types.h>

#ifdef HAVE_SYSTEM_SYSINFO
#include <sys/sysinfo.h>

int get_uptime(time_t *uptime) /*EXTRACT_INCL*/{
  struct sysinfo info;
  if (sysinfo(&info)) return -1;
  *uptime = info.uptime;
  return 0;
}

#else

#ifdef HAVE_SYSTEM_BOOTTIME
#include <sys/sysctl.h>
#include <sys/time.h>

int get_uptime(time_t *uptime) { 
  struct timeval res = { 0, 0 };
  int req[2] = { CTL_KERN, KERN_BOOTTIME };
  size_t res_len = sizeof res;
  if (sysctl(req, 2, &res, &res_len, 0, 0) >= 0 && res.tv_sec > 0) {
    *uptime = res.tv_sec;
    return 0;
  } 
  return -1;
}
#endif
#endif