blob: 9a387fd4118c6bba127c50d1c9a6b79f36e6b6e4 [file] [log] [blame]
Jeremy Kerr3f54de42016-03-09 18:10:15 +08001
2#include <endian.h>
3#include <err.h>
4#include <fcntl.h>
5#include <stdbool.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <unistd.h>
10
11#include <sys/mman.h>
12
13#include <linux/types.h>
14
15#include "console-server.h"
16
17struct log_handler {
18 struct handler handler;
19 struct console *console;
20 int fd;
21 size_t size;
22 size_t maxsize;
23 int pagesize;
24};
25
26
Jeremy Kerrdf94dc12016-03-16 12:19:49 +080027static const char *filename = LOCALSTATEDIR "/log/openbmc-console.log";
28
Jeremy Kerr3f54de42016-03-09 18:10:15 +080029static const size_t logsize = 16 * 1024;
30
31static struct log_handler *to_log_handler(struct handler *handler)
32{
33 return container_of(handler, struct log_handler, handler);
34}
35
Jeremy Kerrd47963e2016-03-16 17:29:55 +080036static int log_init(struct handler *handler, struct console *console,
37 struct config *config __attribute__((unused)))
Jeremy Kerr3f54de42016-03-09 18:10:15 +080038{
39 struct log_handler *lh = to_log_handler(handler);
40 int rc;
41
42 lh->console = console;
43 lh->maxsize = logsize;
44 lh->pagesize = 4096;
45 lh->size = 0;
46
47 lh->fd = open(filename, O_RDWR | O_CREAT, 0644);
48 if (lh->fd < 0) {
49 warn("Can't open log buffer file %s", filename);
50 return -1;
51 }
52 rc = ftruncate(lh->fd, 0);
53 if (rc) {
54 warn("Can't truncate file %s", filename);
55 close(lh->fd);
56 return -1;
57 }
58
59 return 0;
60}
61
Jeremy Kerr3f54de42016-03-09 18:10:15 +080062static int log_trim(struct log_handler *lh, size_t space)
63{
64 int rc, n_shift_pages, shift_len, shift_start;
65 off_t pos;
66 void *buf;
67
68 pos = lseek(lh->fd, 0, SEEK_CUR);
69
70 n_shift_pages = (space + lh->pagesize - 1) / lh->pagesize;
71 shift_start = n_shift_pages * lh->pagesize;
72 shift_len = pos - (n_shift_pages * lh->pagesize);
73
74 buf = mmap(NULL, pos, PROT_READ | PROT_WRITE, MAP_SHARED, lh->fd, 0);
75 if (buf == MAP_FAILED)
76 return -1;
77
78 memmove(buf, buf + shift_start, shift_len);
79
80 munmap(buf, pos);
81
82 lh->size = shift_len;
83 rc = ftruncate(lh->fd, lh->size);
84 if (rc)
85 warn("failed to truncate file");
86 lseek(lh->fd, 0, SEEK_END);
87
88 return 0;
89
90}
91
92static int log_data(struct handler *handler, uint8_t *buf, size_t len)
93{
94 struct log_handler *lh = to_log_handler(handler);
95 int rc;
96
97 if (len > lh->maxsize) {
98 buf += len - lh->maxsize;
99 len = lh->maxsize;
100 }
101
102 if (lh->size + len > lh->maxsize) {
103 rc = log_trim(lh, len);
104 if (rc)
105 return rc;
106 }
107
108 rc = write_buf_to_fd(lh->fd, buf, len);
109 if (rc)
110 return rc;
111
112 lh->size += len;
113
114 return 0;
115}
116
117static void log_fini(struct handler *handler)
118{
119 struct log_handler *lh = to_log_handler(handler);
120 close(lh->fd);
121}
122
123static struct log_handler log_handler = {
124 .handler = {
125 .name = "log",
126 .init = log_init,
Jeremy Kerr3f54de42016-03-09 18:10:15 +0800127 .data_in = log_data,
128 .fini = log_fini,
129 },
130};
131
132console_register_handler(&log_handler.handler);
133