wtmp.c (931B) download
1#include "wtmp.h"
2
3#include <fcntl.h>
4#include <string.h>
5#include <sys/time.h>
6#include <sys/utsname.h>
7#include <unistd.h>
8#include <utmp.h>
9
10void write_wtmp(int boot) {
11 int fd;
12
13 struct utmp utmp = { 0 };
14 struct utsname uname_buf;
15 struct timeval tv;
16
17 if ((fd = open(OUR_WTMP, O_WRONLY | O_APPEND)) < 0)
18 return;
19
20 gettimeofday(&tv, 0);
21 utmp.ut_tv.tv_sec = tv.tv_sec;
22 utmp.ut_tv.tv_usec = tv.tv_usec;
23
24 utmp.ut_type = boot ? BOOT_TIME : RUN_LVL;
25
26 strncpy(utmp.ut_name, boot ? "reboot" : "shutdown", sizeof utmp.ut_name);
27 strncpy(utmp.ut_id, "~~", sizeof utmp.ut_id);
28 strncpy(utmp.ut_line, boot ? "~" : "~~", sizeof utmp.ut_line);
29 if (uname(&uname_buf) == 0)
30 strncpy(utmp.ut_host, uname_buf.release, sizeof utmp.ut_host);
31
32 write(fd, (char*) &utmp, sizeof utmp);
33 close(fd);
34
35 if (boot) {
36 if ((fd = open(OUR_UTMP, O_WRONLY | O_APPEND)) < 0)
37 return;
38 write(fd, (char*) &utmp, sizeof utmp);
39 close(fd);
40 }
41}