+2024-05-24 Collin Funk <collin.funk1@gmail.com>
+
+ boot-time, readutmp: Add a Native Windows boot time fallback.
+ * lib/boot-time-aux.h (initialize, get_windows_boot_time_fallback): New
+ functions.
+ * lib/boot-time.c [_WIN32 && !__CYGWIN__]: Include Windows headers and
+ <sys/time.h>.
+ (get_boot_time_uncached): Use the fallback.
+ * lib/readutmp.c [_WIN32 && !__CYGWIN__]: Include Windows headers and
+ <sys/time.h>.
+ (read_utmp_from_file): Use the fallback.
+ * modules/boot-time (Depends-on): Add gettimeofday.
+ * modules/readutmp (Depends-on): Add gettimeofday.
+
2024-05-23 Bruno Haible <bruno@clisp.org>
mbrtoc32: Work around bug in Cygwin 3.5.3.
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
# include <OS.h>
#endif
+#if defined _WIN32 && ! defined __CYGWIN__
+# define WIN32_LEAN_AND_MEAN
+# include <windows.h>
+# include <sysinfoapi.h>
+# include <sys/time.h>
+#endif
+
#include "idx.h"
#include "readutmp.h"
#include "stat-time.h"
{
/* Workaround for Windows: */
get_windows_boot_time (&found_boot_time);
+# ifndef __CYGWIN__
+ if (found_boot_time.tv_sec == 0)
+ get_windows_boot_time_fallback (&found_boot_time);
+# endif
}
# endif
# include <OS.h>
#endif
+#if defined _WIN32 && ! defined __CYGWIN__
+# define WIN32_LEAN_AND_MEAN
+# include <windows.h>
+# include <sysinfoapi.h>
+# include <sys/time.h>
+#endif
+
#include "stat-time.h"
#include "xalloc.h"
}
# endif
+# if defined _WIN32 && ! defined __CYGWIN__
+ if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0
+ && strcmp (file, UTMP_FILE) == 0
+ && !have_boot_time (a))
+ {
+ struct timespec boot_time;
+ if (get_windows_boot_time_fallback (&boot_time) >= 0)
+ a = add_utmp (a, options,
+ "reboot", strlen ("reboot"),
+ "", 0,
+ "", 0,
+ "", 0,
+ 0, BOOT_TIME, boot_time, 0, 0, 0);
+ }
+# endif
+
a = finish_utmp (a);
*n_entries = a.filled;
extensions
idx
stat-time
+gettimeofday
stdbool
time-h
unlocked-io-internal
extensions
idx
stat-time
+gettimeofday
stdbool
stdint
strnlen