From 4dcef2572018d4e8965a0c54a0b6040c0a5233f4 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 10 Aug 2023 13:08:19 +0200 Subject: [PATCH] boot-time: New module. * lib/boot-time.h: New file. * lib/boot-time.c: New file. * modules/boot-time: New file. --- ChangeLog | 7 +++++++ lib/boot-time.c | 43 +++++++++++++++++++++++++++++++++++++++++++ lib/boot-time.h | 38 ++++++++++++++++++++++++++++++++++++++ modules/boot-time | 27 +++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 lib/boot-time.c create mode 100644 lib/boot-time.h create mode 100644 modules/boot-time diff --git a/ChangeLog b/ChangeLog index b167189c03..cb3c937797 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2023-08-10 Bruno Haible + + boot-time: New module. + * lib/boot-time.h: New file. + * lib/boot-time.c: New file. + * modules/boot-time: New file. + 2023-08-10 Bruno Haible readutmp: Return a boot time also on Android. diff --git a/lib/boot-time.c b/lib/boot-time.c new file mode 100644 index 0000000000..fe1e13eccb --- /dev/null +++ b/lib/boot-time.c @@ -0,0 +1,43 @@ +/* Determine the time when the machine last booted. + Copyright (C) 2023 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 . */ + +/* Written by Bruno Haible . */ + +#include + +/* Specification. */ +#include "boot-time.h" + +#include + +#include "idx.h" +#include "readutmp.h" + +int +get_boot_time (struct timespec *p_boot_time) +{ + idx_t n_entries = 0; + STRUCT_UTMP *utmp = NULL; + read_utmp (UTMP_FILE, &n_entries, &utmp, READ_UTMP_BOOT_TIME); + if (n_entries > 0) + { + *p_boot_time = utmp[0].ut_ts; + free (utmp); + return 0; + } + free (utmp); + return -1; +} diff --git a/lib/boot-time.h b/lib/boot-time.h new file mode 100644 index 0000000000..159a0056f9 --- /dev/null +++ b/lib/boot-time.h @@ -0,0 +1,38 @@ +/* Determine the time when the machine last booted. + Copyright (C) 2023 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 . */ + +/* Written by Bruno Haible . */ + +#ifndef _BOOT_TIME_H +#define _BOOT_TIME_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/* Store the approximate time when the machine last booted in *P_BOOT_TIME, + and return 0. If it cannot be determined, return -1. */ +extern int get_boot_time (struct timespec *p_boot_time); + + +#ifdef __cplusplus +} +#endif + +#endif /* _BOOT_TIME_H */ diff --git a/modules/boot-time b/modules/boot-time new file mode 100644 index 0000000000..99ae5c9fe7 --- /dev/null +++ b/modules/boot-time @@ -0,0 +1,27 @@ +Description: +Determine the time when the machine last booted. + +Files: +lib/boot-time.h +lib/boot-time.c + +Depends-on: +idx +readutmp + +configure.ac: + +Makefile.am: +lib_SOURCES += boot-time.c + +Include: +"boot-time.h" + +Link: +$(READUTMP_LIB) + +License: +GPL + +Maintainer: +all -- 2.39.5