On Mon, Aug 25, 2025 at 04:04:13PM +0100, Andrew Bower wrote:
> On Sat, Aug 23, 2025 at 12:38:42AM +0200, [email protected] wrote:
[...]
> > As an alternative, wtmpdb could ship a wtmp-empty.db file (an empty sqlite
> > db with the correct schema), and replace the removed wtmp.db with that
> > file in the postrotate action of the logrotate snippet.
[...]
> I don't think I want to ship a generated datafile and unfortunately
> there is nothing in the wtmpdb interface that would initialise an empty
> file (even import of nothing cannot do it!) so I think our options are:
[...]
> P.S. When reviewing before sending I had a sudden change of mind that
> maybe I do like your generated file idea, which is like a static verion
> of (4)... it could be generated with the sqlite3 command at build time.
> But this'll have to stay as an addendum to my carefully crafted list!
> :-)

How does the attached patch, following your suggestion, look to you?

After rotation:

$ last
wtmpdb has no entries
>From bb19760ef0b1e88e94baeb054a9d9e1c84ad4d46 Mon Sep 17 00:00:00 2001
From: Andrew Bower <[email protected]>
Date: Mon, 25 Aug 2025 16:37:44 +0100
Subject: [PATCH] Rotate logs with logrotate

---
 debian/control                     |  2 ++
 debian/generate-empty-db           | 10 ++++++++++
 debian/libwtmpdb0.install          |  1 +
 debian/libwtmpdb0.wtmpdb.logrotate | 13 +++++++++++++
 debian/rules                       |  6 ++++++
 5 files changed, 32 insertions(+)
 create mode 100755 debian/generate-empty-db
 create mode 100644 debian/libwtmpdb0.wtmpdb.logrotate

diff --git a/debian/control b/debian/control
index c99b188..f770b8c 100644
--- a/debian/control
+++ b/debian/control
@@ -14,6 +14,7 @@ Build-Depends:
  pkgconf,
  systemd-dev [linux-any],
  xsltproc,
+ sqlite3,
 Priority: optional
 Maintainer: Andrew Bower <[email protected]>
 Section: admin
@@ -32,6 +33,7 @@ Breaks:
  util-linux (<< 2.40.1-3),
 Recommends:
  libpam-wtmpdb,
+ logrotate,
 Multi-Arch: foreign
 Description: utility to display login/logout/reboot information
  This package provides the program wtmpdb, which reads wtmpdb's
diff --git a/debian/generate-empty-db b/debian/generate-empty-db
new file mode 100755
index 0000000..ed52f19
--- /dev/null
+++ b/debian/generate-empty-db
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+set -e
+
+stmt=$(grep -Po '(?<=")CREATE TABLE.*(?=")' lib/sqlite.c)
+
+cd debian/tmp
+mkdir -p usr/share/wtmpdb
+sqlite3 -echo usr/share/wtmpdb/empty.db "$stmt"
+chmod 444 usr/share/wtmpdb/empty.db
diff --git a/debian/libwtmpdb0.install b/debian/libwtmpdb0.install
index 040b513..e1c228f 100644
--- a/debian/libwtmpdb0.install
+++ b/debian/libwtmpdb0.install
@@ -1 +1,2 @@
 usr/lib/${DEB_HOST_MULTIARCH}/libwtmpdb.so.0*
+usr/share/wtmpdb/empty.db
diff --git a/debian/libwtmpdb0.wtmpdb.logrotate b/debian/libwtmpdb0.wtmpdb.logrotate
new file mode 100644
index 0000000..c01ac80
--- /dev/null
+++ b/debian/libwtmpdb0.wtmpdb.logrotate
@@ -0,0 +1,13 @@
+/var/lib/wtmpdb/wtmp.db {
+    missingok
+    yearly
+    nocreate
+    nocompress
+    rotate 4
+    olddir /var/log
+    postrotate
+        cp /usr/share/wtmpdb/empty.db $1
+        chown --reference=$2 $1
+        chmod --reference=$2 $1
+    endscript
+}
diff --git a/debian/rules b/debian/rules
index caa006d..3e251fa 100755
--- a/debian/rules
+++ b/debian/rules
@@ -14,6 +14,9 @@ export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow
 override_dh_auto_configure:
 	dh_auto_configure -- $(CONFOPTS)
 
+execute_after_dh_auto_install:
+	debian/generate-empty-db
+
 # Notes:
 #  1. Do not add a boot entry on installation because most likely we are
 #     either not running in the target system or we have migrated the wtmp
@@ -27,3 +30,6 @@ override_dh_installsystemd:
 
 override_dh_installinit:
 	dh_installinit --name=wtmpdb-update-boot --no-start --no-stop-on-upgrade
+
+override_dh_installlogrotate:
+	dh_installlogrotate --name=wtmpdb
-- 
2.50.1

Reply via email to