unix/fiss

wtmp utils (4050442466910e46411bf154d4c18e39a509be05)
Repositories | LICENSE

commit 4050442466910e46411bf154d4c18e39a509be05
parent 5552752b13d4577e3c5bbc5bad7d54487d1c5aaa
Author: Friedel Schon <[email protected]>
Date:   Mon, 24 Apr 2023 11:44:31 +0200

wtmp utils

Diffstat:
Ainclude/wtmp.h12++++++++++++
Asrc/wtmp.c41+++++++++++++++++++++++++++++++++++++++++
2 files changed, 53 insertions(+), 0 deletions(-)

diff --git a/include/wtmp.h b/include/wtmp.h @@ -0,0 +1,11 @@ +#pragma once + +#ifndef OUR_WTMP +# define OUR_WTMP "/var/log/wtmp" +#endif + +#ifndef OUR_UTMP +# define OUR_UTMP "/run/utmp" +#endif + +void write_wtmp(int boot); +\ No newline at end of file diff --git a/src/wtmp.c b/src/wtmp.c @@ -0,0 +1,41 @@ +#include "wtmp.h" + +#include <fcntl.h> +#include <string.h> +#include <sys/time.h> +#include <sys/utsname.h> +#include <unistd.h> +#include <utmp.h> + +void write_wtmp(int boot) { + int fd; + + if ((fd = open(OUR_WTMP, O_WRONLY | O_APPEND)) < 0) + return; + + struct utmp utmp = { 0 }; + struct utsname uname_buf; + struct timeval tv; + + gettimeofday(&tv, 0); + utmp.ut_tv.tv_sec = tv.tv_sec; + utmp.ut_tv.tv_usec = tv.tv_usec; + + utmp.ut_type = boot ? BOOT_TIME : RUN_LVL; + + strncpy(utmp.ut_name, boot ? "reboot" : "shutdown", sizeof utmp.ut_name); + strncpy(utmp.ut_id, "~~", sizeof utmp.ut_id); + strncpy(utmp.ut_line, boot ? "~" : "~~", sizeof utmp.ut_line); + if (uname(&uname_buf) == 0) + strncpy(utmp.ut_host, uname_buf.release, sizeof utmp.ut_host); + + write(fd, (char*) &utmp, sizeof utmp); + close(fd); + + if (boot) { + if ((fd = open(OUR_UTMP, O_WRONLY | O_APPEND)) < 0) + return; + write(fd, (char*) &utmp, sizeof utmp); + close(fd); + } +}