blob: eb5f80f9f06091f97894b2a487c327e35947b6be [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
36static int log_init(struct handler *handler, struct console *console)
37{
38 struct log_handler *lh = to_log_handler(handler);
39 int rc;
40
41 lh->console = console;
42 lh->maxsize = logsize;
43 lh->pagesize = 4096;
44 lh->size = 0;
45
46 lh->fd = open(filename, O_RDWR | O_CREAT, 0644);
47 if (lh->fd < 0) {
48 warn("Can't open log buffer file %s", filename);
49 return -1;
50 }
51 rc = ftruncate(lh->fd, 0);
52 if (rc) {
53 warn("Can't truncate file %s", filename);
54 close(lh->fd);
55 return -1;
56 }
57
58 return 0;
59}
60
Jeremy Kerr3f54de42016-03-09 18:10:15 +080061static int log_trim(struct log_handler *lh, size_t space)
62{
63 int rc, n_shift_pages, shift_len, shift_start;
64 off_t pos;
65 void *buf;
66
67 pos = lseek(lh->fd, 0, SEEK_CUR);
68
69 n_shift_pages = (space + lh->pagesize - 1) / lh->pagesize;
70 shift_start = n_shift_pages * lh->pagesize;
71 shift_len = pos - (n_shift_pages * lh->pagesize);
72
73 buf = mmap(NULL, pos, PROT_READ | PROT_WRITE, MAP_SHARED, lh->fd, 0);
74 if (buf == MAP_FAILED)
75 return -1;
76
77 memmove(buf, buf + shift_start, shift_len);
78
79 munmap(buf, pos);
80
81 lh->size = shift_len;
82 rc = ftruncate(lh->fd, lh->size);
83 if (rc)
84 warn("failed to truncate file");
85 lseek(lh->fd, 0, SEEK_END);
86
87 return 0;
88
89}
90
91static int log_data(struct handler *handler, uint8_t *buf, size_t len)
92{
93 struct log_handler *lh = to_log_handler(handler);
94 int rc;
95
96 if (len > lh->maxsize) {
97 buf += len - lh->maxsize;
98 len = lh->maxsize;
99 }
100
101 if (lh->size + len > lh->maxsize) {
102 rc = log_trim(lh, len);
103 if (rc)
104 return rc;
105 }
106
107 rc = write_buf_to_fd(lh->fd, buf, len);
108 if (rc)
109 return rc;
110
111 lh->size += len;
112
113 return 0;
114}
115
116static void log_fini(struct handler *handler)
117{
118 struct log_handler *lh = to_log_handler(handler);
119 close(lh->fd);
120}
121
122static struct log_handler log_handler = {
123 .handler = {
124 .name = "log",
125 .init = log_init,
Jeremy Kerr3f54de42016-03-09 18:10:15 +0800126 .data_in = log_data,
127 .fini = log_fini,
128 },
129};
130
131console_register_handler(&log_handler.handler);
132