From cb65a7fe02e5f4f60980f6634bbc5aabffa4e33b Mon Sep 17 00:00:00 2001 From: ed Date: Thu, 27 Oct 2011 17:05:18 +0000 Subject: Make our utmpx more like System V. When booting the system, truncate the utx.active file, but do write the BOOT_TIME record into it afterwards. This allows one to obtain the boot time of the system as follows: struct utmpx u1 = { .ut_type = BOOT_TIME }, *u2; setutxent(); u2 = getutxid(&u1); Now, the boot time is stored in u2->ut_tv, just like on Linux and other systems. We don't open the utx.active file with O_EXLOCK. It's rather unlikely that other applications use this database at the same time and I want to prevent the possibility of deadlocks in init(8). Discussed with: pluknet --- lib/libc/gen/getutxent.3 | 3 +-- lib/libc/gen/pututxline.c | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/libc/gen/getutxent.3 b/lib/libc/gen/getutxent.3 index 1fe64f1..f3d7e73 100644 --- a/lib/libc/gen/getutxent.3 +++ b/lib/libc/gen/getutxent.3 @@ -301,7 +301,6 @@ The value of determines which databases are modified. .Pp Entries of type -.Dv BOOT_TIME , .Dv SHUTDOWN_TIME , .Dv OLD_TIME and @@ -335,7 +334,7 @@ In addition, entries of type .Dv BOOT_TIME and .Dv SHUTDOWN_TIME -will cause all entries in +will cause all existing entries in .Pa /var/run/utx.active to be discarded. .Pp diff --git a/lib/libc/gen/pututxline.c b/lib/libc/gen/pututxline.c index 0cc7a01..98addee 100644 --- a/lib/libc/gen/pututxline.c +++ b/lib/libc/gen/pututxline.c @@ -86,6 +86,9 @@ utx_active_add(const struct futx *fu) return (-1); while (fread(&fe, sizeof(fe), 1, fp) == 1) { switch (fe.fu_type) { + case BOOT_TIME: + /* Leave these intact. */ + break; case USER_PROCESS: case INIT_PROCESS: case LOGIN_PROCESS: @@ -171,6 +174,19 @@ utx_active_remove(struct futx *fu) } static void +utx_active_init(const struct futx *fu) +{ + int fd; + + /* Initialize utx.active with a single BOOT_TIME record. */ + fd = _open(_PATH_UTX_ACTIVE, O_CREAT|O_RDWR|O_TRUNC, 0644); + if (fd < 0) + return; + _write(fd, fu, sizeof(*fu)); + _close(fd); +} + +static void utx_active_purge(void) { @@ -277,9 +293,11 @@ pututxline(const struct utmpx *utmpx) switch (fu.fu_type) { case BOOT_TIME: + utx_active_init(&fu); + utx_lastlogin_upgrade(); + break; case SHUTDOWN_TIME: utx_active_purge(); - utx_lastlogin_upgrade(); break; case OLD_TIME: case NEW_TIME: -- cgit v1.1