log-handler: Set the end of the file as the file size

The file descriptor is lost after system AC cycle.
The log file will grow indefinitely if the log recorded does not exceed
the maximum log size after each AC cycle.

Change-Id: If23d68b08817d54731dd6eff8a42e9eb476b6437
Fixes: 46d9ef298f2e ("Do not truncate log files after reboot")
Signed-off-by: Marshall Zhan <marshall.zhan.wiwynn@gmail.com>
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 947a5e6..fde0075 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -80,3 +80,7 @@
 ### Fixed
 
 1. obmc-console: Consolidate handling of default socket-id
+
+### Fixed
+
+1. log-handler: Set the end of the file as the file size
diff --git a/log-handler.c b/log-handler.c
index 571fba6..1ea1cdf 100644
--- a/log-handler.c
+++ b/log-handler.c
@@ -136,7 +136,7 @@
 		warn("Can't open log buffer file %s", lh->log_filename);
 		return -1;
 	}
-	pos = lseek(lh->fd, 0, SEEK_CUR);
+	pos = lseek(lh->fd, 0, SEEK_END);
 	if (pos < 0) {
 		warn("Can't query log position for file %s", lh->log_filename);
 		close(lh->fd);
diff --git a/test/test-console-logs-to-file b/test/test-console-logs-to-file
index ffedbd9..252ccf6 100755
--- a/test/test-console-logs-to-file
+++ b/test/test-console-logs-to-file
@@ -26,14 +26,19 @@
 
 TEST_CONF="${TEST_NAME}.conf"
 TEST_LOG="${TEST_NAME}.log"
+BLOCK_SIZE=1024
+LOG_MAX_SIZE=$((5 * $BLOCK_SIZE))
 
 cat <<EOF > "$TEST_CONF"
 active-console = $TEST_NAME
+logsize = $LOG_MAX_SIZE
 [$TEST_NAME]
 logfile = $TEST_LOG
 console-id = $TEST_NAME
 EOF
 
+dd if=/dev/zero bs=$BLOCK_SIZE count=$(($LOG_MAX_SIZE / $BLOCK_SIZE)) >> "$TEST_LOG"
+
 "$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
@@ -45,3 +50,5 @@
 echo console-should-log-to-file > remote
 sleep 1
 grep -LF console-should-log-to-file "$TEST_LOG"
+
+[ -e "$TEST_LOG" ] && [ $(stat -c%s "$TEST_LOG") -le $LOG_MAX_SIZE ]
diff --git a/test/test-console-logs-to-file-no-sections b/test/test-console-logs-to-file-no-sections
index 8d4e052..1dbfb21 100755
--- a/test/test-console-logs-to-file-no-sections
+++ b/test/test-console-logs-to-file-no-sections
@@ -26,12 +26,17 @@
 
 TEST_CONF="${TEST_NAME}.conf"
 TEST_LOG="${TEST_NAME}.log"
+BLOCK_SIZE=1024
+LOG_MAX_SIZE=$((5 * $BLOCK_SIZE))
 
 cat <<EOF > "$TEST_CONF"
 logfile = $TEST_LOG
 console-id = $TEST_NAME
+logsize = $LOG_MAX_SIZE
 EOF
 
+dd if=/dev/zero bs=$BLOCK_SIZE count=$(($LOG_MAX_SIZE / $BLOCK_SIZE)) >> "$TEST_LOG"
+
 "$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
@@ -43,3 +48,5 @@
 echo console-should-log-to-file-legacy-syntax > remote
 sleep 1
 grep -LF console-should-log-to-file-legacy-syntax "$TEST_LOG"
+
+[ -e "$TEST_LOG" ] && [ $(stat -c%s "$TEST_LOG") -le $LOG_MAX_SIZE ]