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" |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 31 | #include "config.h" |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 32 | |
| 33 | struct log_handler { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 34 | struct handler handler; |
| 35 | struct console *console; |
| 36 | struct ringbuffer_consumer *rbc; |
| 37 | int fd; |
| 38 | size_t size; |
| 39 | size_t maxsize; |
| 40 | size_t pagesize; |
| 41 | char *log_filename; |
| 42 | char *rotate_filename; |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 43 | }; |
| 44 | |
Jeremy Kerr | e440a40 | 2016-03-17 16:34:14 +0800 | [diff] [blame] | 45 | static const char *default_filename = LOCALSTATEDIR "/log/obmc-console.log"; |
Andrew Jeffery | 5db8c79 | 2023-04-18 21:48:24 +0930 | [diff] [blame] | 46 | static const size_t default_logsize = 16ul * 1024ul; |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 47 | |
| 48 | static struct log_handler *to_log_handler(struct handler *handler) |
| 49 | { |
| 50 | return container_of(handler, struct log_handler, handler); |
| 51 | } |
| 52 | |
Andrew Jeffery | fd04832 | 2023-04-18 12:02:01 +0930 | [diff] [blame] | 53 | static int log_trim(struct log_handler *lh) |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 54 | { |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 55 | int rc; |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 56 | |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 57 | /* Move the log buffer file to the rotate file */ |
| 58 | close(lh->fd); |
| 59 | rc = rename(lh->log_filename, lh->rotate_filename); |
| 60 | if (rc) { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 61 | warn("Failed to rename %s to %s", lh->log_filename, |
| 62 | lh->rotate_filename); |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 63 | /* don't return, as we need to re-open the logfile */ |
| 64 | } |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 65 | |
John Wang | 46d9ef2 | 2023-01-16 11:04:51 +0800 | [diff] [blame] | 66 | lh->fd = open(lh->log_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 67 | if (lh->fd < 0) { |
| 68 | warn("Can't open log buffer file %s", lh->log_filename); |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 69 | return -1; |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 70 | } |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 71 | |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 72 | lh->size = 0; |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 73 | |
| 74 | return 0; |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 75 | } |
| 76 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 77 | 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] | 78 | { |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 79 | int rc; |
| 80 | |
| 81 | if (len > lh->maxsize) { |
| 82 | buf += len - lh->maxsize; |
| 83 | len = lh->maxsize; |
| 84 | } |
| 85 | |
| 86 | if (lh->size + len > lh->maxsize) { |
Andrew Jeffery | fd04832 | 2023-04-18 12:02:01 +0930 | [diff] [blame] | 87 | rc = log_trim(lh); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 88 | if (rc) { |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 89 | return rc; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 90 | } |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | rc = write_buf_to_fd(lh->fd, buf, len); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 94 | if (rc) { |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 95 | return rc; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 96 | } |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 97 | |
| 98 | lh->size += len; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 103 | static enum ringbuffer_poll_ret log_ringbuffer_poll(void *arg, size_t force_len |
| 104 | __attribute__((unused))) |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 105 | { |
| 106 | struct log_handler *lh = arg; |
| 107 | uint8_t *buf; |
| 108 | size_t len; |
| 109 | int rc; |
| 110 | |
| 111 | /* we log synchronously, so just dequeue everything we can, and |
| 112 | * commit straight away. */ |
| 113 | for (;;) { |
| 114 | len = ringbuffer_dequeue_peek(lh->rbc, 0, &buf); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 115 | if (!len) { |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 116 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 117 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 118 | |
| 119 | rc = log_data(lh, buf, len); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 120 | if (rc) { |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 121 | return RINGBUFFER_POLL_REMOVE; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 122 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 123 | |
| 124 | ringbuffer_dequeue_commit(lh->rbc, len); |
| 125 | } |
| 126 | |
| 127 | return RINGBUFFER_POLL_OK; |
| 128 | } |
| 129 | |
John Wang | 46d9ef2 | 2023-01-16 11:04:51 +0800 | [diff] [blame] | 130 | static int log_create(struct log_handler *lh) |
| 131 | { |
| 132 | off_t pos; |
| 133 | |
| 134 | lh->fd = open(lh->log_filename, O_WRONLY | O_CREAT | O_APPEND, 0644); |
| 135 | if (lh->fd < 0) { |
| 136 | warn("Can't open log buffer file %s", lh->log_filename); |
| 137 | return -1; |
| 138 | } |
| 139 | pos = lseek(lh->fd, 0, SEEK_CUR); |
| 140 | if (pos < 0) { |
| 141 | warn("Can't query log position for file %s", lh->log_filename); |
| 142 | close(lh->fd); |
| 143 | return -1; |
| 144 | } |
| 145 | lh->size = pos; |
| 146 | if ((size_t)pos >= lh->maxsize) { |
| 147 | return log_trim(lh); |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 153 | static struct handler *log_init(const struct handler_type *type |
| 154 | __attribute__((unused)), |
| 155 | struct console *console, struct config *config) |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 156 | { |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 157 | struct log_handler *lh; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 158 | const char *filename; |
| 159 | const char *logsize_str; |
Kun Yi | 1864435 | 2018-08-27 11:37:42 -0700 | [diff] [blame] | 160 | size_t logsize = default_logsize; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 161 | int rc; |
| 162 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 163 | lh = malloc(sizeof(*lh)); |
| 164 | if (!lh) { |
| 165 | return NULL; |
| 166 | } |
| 167 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 168 | lh->console = console; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 169 | lh->pagesize = 4096; |
| 170 | lh->size = 0; |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 171 | lh->log_filename = NULL; |
| 172 | lh->rotate_filename = NULL; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 173 | |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 174 | logsize_str = config_get_value(config, "logsize"); |
Medicine Yeh | d6e8b64 | 2024-03-18 01:49:17 -0700 | [diff] [blame] | 175 | rc = config_parse_bytesize(logsize_str, &logsize); |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 176 | if (logsize_str != NULL && rc) { |
| 177 | logsize = default_logsize; |
| 178 | warn("Invalid logsize. Default to %ukB", |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 179 | (unsigned int)(logsize >> 10)); |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 180 | } |
| 181 | lh->maxsize = logsize <= lh->pagesize ? lh->pagesize + 1 : logsize; |
| 182 | |
Alexander Hansen | a6b2910 | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 183 | filename = config_get_section_value(config, console->console_id, |
| 184 | "logfile"); |
| 185 | |
Alexander Hansen | 6498f9f | 2024-09-13 17:15:33 +0200 | [diff] [blame^] | 186 | if (!filename && config_count_sections(config) == 0) { |
Alexander Hansen | a6b2910 | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 187 | filename = config_get_value(config, "logfile"); |
| 188 | } |
| 189 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 190 | if (!filename) { |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 191 | filename = default_filename; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 192 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 193 | |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 194 | lh->log_filename = strdup(filename); |
| 195 | |
| 196 | rc = asprintf(&lh->rotate_filename, "%s.1", filename); |
| 197 | if (rc < 0) { |
| 198 | warn("Failed to construct rotate filename"); |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 199 | goto err_free; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 200 | } |
| 201 | |
John Wang | 46d9ef2 | 2023-01-16 11:04:51 +0800 | [diff] [blame] | 202 | rc = log_create(lh); |
| 203 | if (rc < 0) { |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 204 | goto err_free; |
John Wang | 46d9ef2 | 2023-01-16 11:04:51 +0800 | [diff] [blame] | 205 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 206 | lh->rbc = console_ringbuffer_consumer_register(console, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 207 | log_ringbuffer_poll, lh); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 208 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 209 | return &lh->handler; |
| 210 | |
| 211 | err_free: |
Andrew Jeffery | 48d1f53 | 2024-07-09 14:46:14 +0930 | [diff] [blame] | 212 | free(lh->rotate_filename); |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 213 | free(lh->log_filename); |
| 214 | free(lh); |
| 215 | return NULL; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 216 | } |
| 217 | |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 218 | static void log_fini(struct handler *handler) |
| 219 | { |
| 220 | struct log_handler *lh = to_log_handler(handler); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 221 | ringbuffer_consumer_unregister(lh->rbc); |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 222 | close(lh->fd); |
Lei YU | 93fd8a3 | 2022-03-30 10:15:25 +0800 | [diff] [blame] | 223 | free(lh->log_filename); |
| 224 | free(lh->rotate_filename); |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 225 | free(lh); |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 226 | } |
| 227 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 228 | static const struct handler_type log_handler = { |
| 229 | .name = "log", |
| 230 | .init = log_init, |
| 231 | .fini = log_fini, |
Jeremy Kerr | 3f54de4 | 2016-03-09 18:10:15 +0800 | [diff] [blame] | 232 | }; |
| 233 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 234 | console_handler_register(&log_handler); |