log-handler: fix console logs
server->n_consoles was not incremented until after console_init.
But the log handler already made use of that value to decide which
filename to use for logging, in the case of config with no sections.
This fix avoids use of server->n_consoles in favor of looking at the
number of sections in the config.
Change-Id: Ic19802808197557cd1f632c63a0123604a619039
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/log-handler.c b/log-handler.c
index 79b0f99..571fba6 100644
--- a/log-handler.c
+++ b/log-handler.c
@@ -183,7 +183,7 @@
filename = config_get_section_value(config, console->console_id,
"logfile");
- if (!filename && console->server->n_consoles == 1) {
+ if (!filename && config_count_sections(config) == 0) {
filename = config_get_value(config, "logfile");
}
diff --git a/test/meson.build b/test/meson.build
index d8bcb60..d36d15c 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -37,6 +37,7 @@
server_tests = [
'test-console-logs-to-file',
+ 'test-console-logs-to-file-no-sections',
'test-console-socket-read',
'test-console-socket-write',
'test-multiple-consoles',
diff --git a/test/test-console-logs-to-file-no-sections b/test/test-console-logs-to-file-no-sections
new file mode 100755
index 0000000..8d4e052
--- /dev/null
+++ b/test/test-console-logs-to-file-no-sections
@@ -0,0 +1,45 @@
+#!/usr/bin/sh
+
+set -eux
+
+SOCAT="$1"
+SERVER="$2"
+
+# Meet DBus bus and path name constraints, append own PID for parallel runs
+TEST_NAME="$(basename "$0" | tr '-' '_')"_${$}
+TEST_DIR="$(mktemp --tmpdir --directory "${TEST_NAME}.XXXXXX")"
+PTYS_PID=""
+SERVER_PID=""
+
+cd "$TEST_DIR"
+
+cleanup()
+{
+ [ -z "$SERVER_PID" ] || kill -s INT "$SERVER_PID"
+ [ -z "$PTYS_PID" ] || kill "$PTYS_PID"
+ wait
+ cd -
+ rm -rf "$TEST_DIR"
+}
+
+trap cleanup EXIT
+
+TEST_CONF="${TEST_NAME}.conf"
+TEST_LOG="${TEST_NAME}.log"
+
+cat <<EOF > "$TEST_CONF"
+logfile = $TEST_LOG
+console-id = $TEST_NAME
+EOF
+
+"$SOCAT" -u PTY,raw,echo=0,link=remote PTY,raw,echo=0,wait-slave,link=local &
+PTYS_PID="$!"
+while ! [ -e remote ] || ! [ -e local ]; do sleep 1; done
+
+"$SERVER" --config "$TEST_CONF" "$(realpath local)" &
+SERVER_PID="$!"
+while ! busctl status --user xyz.openbmc_project.Console."${TEST_NAME}"; do sleep 1; done
+
+echo console-should-log-to-file-legacy-syntax > remote
+sleep 1
+grep -LF console-should-log-to-file-legacy-syntax "$TEST_LOG"