Hi Corey,

When running ipmi_sim to provide a BMC for a VM (qemu with
ipmi-bmc-extern), the SOL backup file is only written when the simulator
exits, which makes the console history file useless for debugging a
machine while it runs.

So I patched openipmi! :)

This patch adds a flush=<seconds> history option to the sol command:
when set, new SOL history data is appended to the backup file every
<seconds> while it is received.  The file is only ever appended to, so
the startup restore (reading the last history_size bytes) stays
consistent and the file doubles as a complete console log.  Without the
option, the behavior is unchanged.  As a side effect, the backup file is
now also flushed on clean shutdowns even when the SOL device is
disconnected, and SIGUSR1 flushes it on demand.

I hope you like the patch. Let me know if you think it needs refinements.

Cheers,

Thomas Goirand (zigo)
From 668ac11cb81a1a9e7ecad5d650c3dd248ee4de14 Mon Sep 17 00:00:00 2001
From: Thomas Goirand <[email protected]>
Date: Fri, 20 Nov 2026 15:11:52 +0100
Subject: [PATCH] lanserv: Add a flush= option to append SOL history
 periodically

The SOL backup file was only written when ipmi_sim received a
catchable signal or exited normally, so the console history file
stayed stale while the simulator ran.

Add a flush=<seconds> history option to the sol command.  When
given, a tick handler appends the history data that has not been
flushed yet to the backup file every <seconds> while new data is
received.  The file is only ever appended to, which keeps the
startup restore (reading the last history_size bytes of the file)
consistent and lets the file serve as a complete console log.
Without the option, the periodic flushing is not enabled and the
behavior is unchanged.

The clean-shutdown flush now also runs when the SOL device is
disconnected, and SIGUSR1 flushes the backup file on demand
without shutting the simulator down.

Signed-off-by: Thomas Goirand <[email protected]>
---
 lanserv/ipmi_lan.5 |  12 +++--
 lanserv/ipmi_sim.c |  14 +++++-
 lanserv/sol.c      | 111 ++++++++++++++++++++++++++++++++++++++-------
 lanserv/sol.h      |   7 +++
 4 files changed, 123 insertions(+), 21 deletions(-)

diff --git a/lanserv/ipmi_lan.5 b/lanserv/ipmi_lan.5
index 9de2c240..258eb663 100644
--- a/lanserv/ipmi_lan.5
+++ b/lanserv/ipmi_lan.5
@@ -172,9 +172,15 @@ the buffer.  Data from the device is stored in the history buffer all
 the time.  Connecting to SOL interface 2 will cause the full history
 buffer to be dumped.  If
 .I backupfile
-is specified, then the history is made persistent.  However, it is
-only stored when a catchable signal or normal shutdown is done, so a
-poweroff or fatal signal will cause the data to be lost.
+is specified, then the history is made persistent.  It is appended to
+when a catchable signal or normal shutdown is done, and on receipt of
+a SIGUSR1 signal, so only a poweroff or fatal signal will cause the
+most recent data to be lost.  If
+.B flush=\fIseconds\fP
+is given as an history option, new data is also appended to the file
+every \fIseconds\fP while it is received, so the file tracks the
+console contents while the simulator runs.  The file is read back at
+simulator startup to restore the history buffer.
 
 .I historyfru
 makes the history available via the given FRU number on the MC.
diff --git a/lanserv/ipmi_sim.c b/lanserv/ipmi_sim.c
index 3a433845..40867aa0 100644
--- a/lanserv/ipmi_sim.c
+++ b/lanserv/ipmi_sim.c
@@ -1444,7 +1444,7 @@ shutdown_handler(int sig)
 	h->handler(h->info, sig);
 	h = h->next;
     }
-    if (sig)
+    if (sig && sig != SIGUSR1)
 	raise(sig);
 }
 
@@ -1795,6 +1795,18 @@ main(int argc, const char *argv[])
 	}
     }
 
+    /*
+     * SIGUSR1 flushes the SOL history backup file without shutting
+     * down the simulator, so it must stay re-armable (no
+     * SA_RESETHAND).
+     */
+    act.sa_flags = 0;
+    err = sigaction(SIGUSR1, &act, NULL);
+    if (err) {
+	fprintf(stderr, "Unable to register SIGUSR1: %s\n",
+		strerror(errno));
+    }
+
     tv.tv_sec = 1;
     tv.tv_usec = 0;
     err = data.os_hnd->start_timer(data.os_hnd, data.timer, &tv, tick, &data);
diff --git a/lanserv/sol.c b/lanserv/sol.c
index a40036bb..5282f049 100644
--- a/lanserv/sol.c
+++ b/lanserv/sol.c
@@ -134,6 +134,15 @@ struct soldata_s {
      */
     ipmi_shutdown_t backupfilehandler;
 
+    /*
+     * History data not yet appended to the backup file, and the
+     * number of seconds waited since the last append.  Used to keep
+     * the backup file updated while the simulator runs.
+     */
+    unsigned int history_unflushed;
+    unsigned int history_flush_wait;
+    ipmi_tick_handler_t tick_handler;
+
     /*
      * The amount of actual history to store when the copy request happens.
      * If zero, this is ignored.  This is guaranteed to be <= history_size.
@@ -1526,6 +1535,7 @@ add_to_history(ipmi_sol_t *sol, unsigned char *buf, unsigned int len)
 	sd->history_start++;
     }
     sd->history_end += len;
+    sd->history_unflushed += len;
 }
 
 static int
@@ -1705,6 +1715,14 @@ is_sol_read_config(char **tokptr, sys_data_t *sys, const char **err)
 
 		if (strncmp(opt, "backupfile=", 11) == 0) {
 		    sol->backupfile = sys_strdup(sys, opt + 11);
+		} else if (strncmp(opt, "flush=", 6) == 0) {
+		    char *fend;
+
+		    sol->history_flush_secs = strtoul(opt + 6, &fend, 0);
+		    if (*fend != '\0' || sol->history_flush_secs == 0) {
+			*err = "Invalid flush value";
+			return -1;
+		    }
 		} else {
 		    *err = "Unknown history option";
 		    return -1;
@@ -1842,39 +1860,93 @@ sol_init(emu_data_t *emu)
 				 handle_sol_payload);
 }
 
+/*
+ * Append the history data that has not been written to the backup
+ * file yet.  The file is only appended to, never truncated, so the
+ * startup restore (which reads the last history_size bytes of the
+ * file) stays consistent and the file doubles as a console log.
+ */
 static void
-handle_sol_shutdown(void *info, int sig)
+sol_history_flush(ipmi_sol_t *sol)
 {
-    ipmi_sol_t *sol = info;
     soldata_t *sd = sol->soldata;
+    unsigned int pos;
     FILE *f;
 
-    if (sol->configured < 2 || !sd)
+    if (!sol->backupfile || !sd || !sd->history)
+	return;
+    if (sd->history_unflushed == 0)
+	return;
+    if (sd->history_start == sd->history_end)
 	return;
 
-    sol->configured--;
-    sd->shutdown(sol);
+    if (sd->history_unflushed > sol->history_size)
+	sd->history_unflushed = sol->history_size;
 
-    if (!sol->backupfile || (sd->history_start == sd->history_end))
+    f = fopen(sol->backupfile, "a");
+    if (!f)
 	return;
 
     /*
-     * Write the current history to the backup file.
+     * The unflushed data ends at history_end, write the last
+     * history_unflushed bytes, handling the buffer wrap.
      */
-    f = fopen(sol->backupfile, "w");
-    if (!f)
-	return;
-    if (sd->history_end >= sd->history_start) {
-	/* History is fully contained between start and end. */
-	fwrite(sd->history + sd->history_start, 1,
-	       sd->history_end - sd->history_start + 1, f);
+    pos = ((unsigned int) sd->history_end) + 1 + sol->history_size
+	- sd->history_unflushed;
+    pos %= sol->history_size;
+    if (pos <= (unsigned int) sd->history_end) {
+	fwrite(sd->history + pos, 1, sd->history_end - pos + 1, f);
     } else {
-	/* History laps over end, need two writes. */
-	fwrite(sd->history + sd->history_start, 1,
-	       sol->history_size - sd->history_start, f);
+	fwrite(sd->history + pos, 1, sol->history_size - pos, f);
 	fwrite(sd->history, 1, sd->history_end + 1, f);
     }
     fclose(f);
+
+    sd->history_unflushed = 0;
+}
+
+static void
+handle_sol_shutdown(void *info, int sig)
+{
+    ipmi_sol_t *sol = info;
+    soldata_t *sd = sol->soldata;
+
+    if (sig == SIGUSR1) {
+	/* Just flush the history to the backup file. */
+	sol_history_flush(sol);
+	return;
+    }
+
+    /*
+     * Flush the history even if the SOL device is not currently
+     * connected, the data in the buffer is still valid.
+     */
+    sol_history_flush(sol);
+
+    if (sol->configured < 2 || !sd)
+	return;
+
+    sol->configured--;
+    sd->shutdown(sol);
+}
+
+static void
+sol_tick_handler(void *info, unsigned int seconds)
+{
+    ipmi_sol_t *sol = info;
+    soldata_t *sd = sol->soldata;
+
+    if (!sol->backupfile || !sol->history_flush_secs)
+	return;
+    if (!sd->history_unflushed)
+	return;
+
+    sd->history_flush_wait += seconds;
+    if (sd->history_flush_wait < sol->history_flush_secs)
+	return;
+
+    sd->history_flush_wait = 0;
+    sol_history_flush(sol);
 }
 
 static int
@@ -1931,6 +2003,11 @@ sol_init_mc(sys_data_t *sys, lmc_data_t *mc)
     sd->backupfilehandler.info = sol;
     ipmi_register_shutdown_handler(&sd->backupfilehandler);
 
+    sd->tick_handler.info = sol;
+    sd->tick_handler.handler = sol_tick_handler;
+    if (sol->history_flush_secs)
+	sys->register_tick_handler(&sd->tick_handler);
+
     sd->fd = -1;
     sd->curr_packet_seq = 1;
     sd->history_curr_packet_seq = 1;
diff --git a/lanserv/sol.h b/lanserv/sol.h
index 57fd1258..e521a0d3 100644
--- a/lanserv/sol.h
+++ b/lanserv/sol.h
@@ -94,6 +94,13 @@ typedef struct ipmi_sol_s {
     /* History is stored in this file is the program fails. */
     char *backupfile;
 
+    /*
+     * If non-zero, append new history data to the backup file every
+     * this many seconds while it is received.  Zero (the default)
+     * only flushes the file on clean shutdowns and on SIGUSR1.
+     */
+    unsigned int history_flush_secs;
+
     int use_rtscts;
     int readclear;
 
-- 
2.47.3

_______________________________________________
Openipmi-developer mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openipmi-developer

Reply via email to