Jeremy Kerr | 9326d77 | 2016-03-17 17:15:02 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 16 | |
| 17 | #include <endian.h> |
| 18 | #include <err.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <stdbool.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <sys/mman.h> |
| 27 | |
| 28 | #include <linux/types.h> |
| 29 | |
| 30 | #include "console-server.h" |
| 31 | |
| 32 | struct log_handler { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 33 | struct handler handler; |
| 34 | struct console *console; |
| 35 | struct ringbuffer_consumer *rbc; |
| 36 | int fd; |
| 37 | size_t size; |
| 38 | size_t maxsize; |
| 39 | size_t pagesize; |
| 40 | char *log_filename; |
| 41 | char *rotate_filename; |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 42 | }; |
| 43 | |
Jeremy Kerr | e440a40 | 2016-03-17 16:34:14 +0800 | [diff] [blame] | 44 | static const char *default_filename = LOCALSTATEDIR "/log/obmc-console.log"; |
Andrew Jeffery | 5db8c79 | 2023-04-18 21:48:24 +0930 | [diff] [blame] | 45 | static const size_t default_logsize = 16ul * 1024ul; |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 46 | |
| 47 | static struct log_handler *to_log_handler(struct handler *handler) |
| 48 | { |
| 49 | return container_of(handler, struct log_handler, handler); |
| 50 | } |
| 51 | |
Andrew Jeffery | fd04832 | 2023-04-18 12:02:01 +0930 | [diff] [blame] | 52 | static int log_trim(struct log_handler *lh) |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 53 | { |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 54 | int rc; |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 55 | |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 56 | /* Move the log buffer file to the rotate file */ |
| 57 | close(lh->fd); |
| 58 | rc = rename(lh->log_filename, lh->rotate_filename); |
| 59 | if (rc) { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 60 | warn("Failed to rename %s to %s", lh->log_filename, |
| 61 | lh->rotate_filename); |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 62 | /* don't return, as we need to re-open the logfile */ |
| 63 | } |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 64 | |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 65 | lh->fd = open(lh->log_filename, O_RDWR | O_CREAT | O_TRUNC, 0644); |
| 66 | if (lh->fd < 0) { |
| 67 | warn("Can't open log buffer file %s", lh->log_filename); |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 68 | return -1; |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 69 | } |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 70 | |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 71 | lh->size = 0; |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 72 | |
| 73 | return 0; |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 76 | static int log_data(struct log_handler *lh, uint8_t *buf, size_t len) |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 77 | { |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 78 | int rc; |
| 79 | |
| 80 | if (len > lh->maxsize) { |
| 81 | buf += len - lh->maxsize; |
| 82 | len = lh->maxsize; |
| 83 | } |
| 84 | |
| 85 | if (lh->size + len > lh->maxsize) { |
Andrew Jeffery | fd04832 | 2023-04-18 12:02:01 +0930 | [diff] [blame] | 86 | rc = log_trim(lh); |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 87 | if (rc) |
| 88 | return rc; |
| 89 | } |
| 90 | |
| 91 | rc = write_buf_to_fd(lh->fd, buf, len); |
| 92 | if (rc) |
| 93 | return rc; |
| 94 | |
| 95 | lh->size += len; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 100 | static enum ringbuffer_poll_ret log_ringbuffer_poll(void *arg, size_t force_len |
| 101 | __attribute__((unused))) |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 102 | { |
| 103 | struct log_handler *lh = arg; |
| 104 | uint8_t *buf; |
| 105 | size_t len; |
| 106 | int rc; |
| 107 | |
| 108 | /* we log synchronously, so just dequeue everything we can, and |
| 109 | * commit straight away. */ |
| 110 | for (;;) { |
| 111 | len = ringbuffer_dequeue_peek(lh->rbc, 0, &buf); |
| 112 | if (!len) |
| 113 | break; |
| 114 | |
| 115 | rc = log_data(lh, buf, len); |
| 116 | if (rc) |
| 117 | return RINGBUFFER_POLL_REMOVE; |
| 118 | |
| 119 | ringbuffer_dequeue_commit(lh->rbc, len); |
| 120 | } |
| 121 | |
| 122 | return RINGBUFFER_POLL_OK; |
| 123 | } |
| 124 | |
| 125 | static int log_init(struct handler *handler, struct console *console, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 126 | struct config *config) |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 127 | { |
| 128 | struct log_handler *lh = to_log_handler(handler); |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 129 | const char *filename, *logsize_str; |
Kun Yi | 1864435 | 2018-08-27 11:37:42 -0700 | [diff] [blame] | 130 | size_t logsize = default_logsize; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 131 | int rc; |
| 132 | |
| 133 | lh->console = console; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 134 | lh->pagesize = 4096; |
| 135 | lh->size = 0; |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 136 | lh->log_filename = NULL; |
| 137 | lh->rotate_filename = NULL; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 138 | |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 139 | logsize_str = config_get_value(config, "logsize"); |
| 140 | rc = config_parse_logsize(logsize_str, &logsize); |
| 141 | if (logsize_str != NULL && rc) { |
| 142 | logsize = default_logsize; |
| 143 | warn("Invalid logsize. Default to %ukB", |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 144 | (unsigned int)(logsize >> 10)); |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 145 | } |
| 146 | lh->maxsize = logsize <= lh->pagesize ? lh->pagesize + 1 : logsize; |
| 147 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 148 | filename = config_get_value(config, "logfile"); |
| 149 | if (!filename) |
| 150 | filename = default_filename; |
| 151 | |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 152 | lh->fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 153 | if (lh->fd < 0) { |
| 154 | warn("Can't open log buffer file %s", filename); |
| 155 | return -1; |
| 156 | } |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 157 | |
| 158 | lh->log_filename = strdup(filename); |
| 159 | |
| 160 | rc = asprintf(&lh->rotate_filename, "%s.1", filename); |
| 161 | if (rc < 0) { |
| 162 | warn("Failed to construct rotate filename"); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | lh->rbc = console_ringbuffer_consumer_register(console, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 167 | log_ringbuffer_poll, lh); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 172 | static void log_fini(struct handler *handler) |
| 173 | { |
| 174 | struct log_handler *lh = to_log_handler(handler); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 175 | ringbuffer_consumer_unregister(lh->rbc); |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 176 | close(lh->fd); |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 177 | free(lh->log_filename); |
| 178 | free(lh->rotate_filename); |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static struct log_handler log_handler = { |
| 182 | .handler = { |
| 183 | .name = "log", |
| 184 | .init = log_init, |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 185 | .fini = log_fini, |
| 186 | }, |
| 187 | }; |
| 188 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 189 | console_handler_register(&log_handler.handler); |