Do not truncate log files after reboot

The log file will be truncated during the log_init process, so that the
logs will be lost after bmc(obmc-console) restarts.

Here we change the logic to loop through the sol logs instead of
truncating them when they already exist in the bmc system

Signed-off-by: John Wang <wangzhiqiang02@inspur.com>
Change-Id: I9a14867c7ec8bfb6f25edcc8f383afd44f8ee3ad
diff --git a/log-handler.c b/log-handler.c
index a545505..d93b679 100644
--- a/log-handler.c
+++ b/log-handler.c
@@ -62,7 +62,7 @@
 		/* don't return, as we need to re-open the logfile */
 	}
 
-	lh->fd = open(lh->log_filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
+	lh->fd = open(lh->log_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
 	if (lh->fd < 0) {
 		warn("Can't open log buffer file %s", lh->log_filename);
 		return -1;
@@ -126,6 +126,29 @@
 	return RINGBUFFER_POLL_OK;
 }
 
+static int log_create(struct log_handler *lh)
+{
+	off_t pos;
+
+	lh->fd = open(lh->log_filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
+	if (lh->fd < 0) {
+		warn("Can't open log buffer file %s", lh->log_filename);
+		return -1;
+	}
+	pos = lseek(lh->fd, 0, SEEK_CUR);
+	if (pos < 0) {
+		warn("Can't query log position for file %s", lh->log_filename);
+		close(lh->fd);
+		return -1;
+	}
+	lh->size = pos;
+	if ((size_t)pos >= lh->maxsize) {
+		return log_trim(lh);
+	}
+
+	return 0;
+}
+
 static int log_init(struct handler *handler, struct console *console,
 		    struct config *config)
 {
@@ -155,12 +178,6 @@
 		filename = default_filename;
 	}
 
-	lh->fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
-	if (lh->fd < 0) {
-		warn("Can't open log buffer file %s", filename);
-		return -1;
-	}
-
 	lh->log_filename = strdup(filename);
 
 	rc = asprintf(&lh->rotate_filename, "%s.1", filename);
@@ -169,6 +186,10 @@
 		return -1;
 	}
 
+	rc = log_create(lh);
+	if (rc < 0) {
+		return -1;
+	}
 	lh->rbc = console_ringbuffer_consumer_register(console,
 						       log_ringbuffer_poll, lh);