Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2016 The Android Open Source Project |
| 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 | */ |
| 16 | |
Mark Salyzyn | 5b2ce40 | 2016-03-14 14:15:50 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <stdbool.h> |
Mark Salyzyn | 5b2ce40 | 2016-03-14 14:15:50 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 22 | #include <string.h> |
| 23 | #include <sys/types.h> |
| 24 | |
| 25 | #include <private/android_filesystem_config.h> |
| 26 | #include <private/android_logger.h> |
| 27 | |
| 28 | #include "config_read.h" |
| 29 | #include "logger.h" |
| 30 | |
| 31 | static int pmsgAvailable(log_id_t logId); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 32 | static int pmsgVersion(struct android_log_logger* logger, |
| 33 | struct android_log_transport_context* transp); |
| 34 | static int pmsgRead(struct android_log_logger_list* logger_list, |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 35 | struct android_log_transport_context* transp, struct log_msg* log_msg); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 36 | static void pmsgClose(struct android_log_logger_list* logger_list, |
| 37 | struct android_log_transport_context* transp); |
| 38 | static int pmsgClear(struct android_log_logger* logger, |
| 39 | struct android_log_transport_context* transp); |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 40 | |
| 41 | LIBLOG_HIDDEN struct android_log_transport_read pmsgLoggerRead = { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 42 | .node = {&pmsgLoggerRead.node, &pmsgLoggerRead.node}, |
| 43 | .name = "pmsg", |
| 44 | .available = pmsgAvailable, |
| 45 | .version = pmsgVersion, |
| 46 | .read = pmsgRead, |
| 47 | .poll = NULL, |
| 48 | .close = pmsgClose, |
| 49 | .clear = pmsgClear, |
| 50 | .setSize = NULL, |
| 51 | .getSize = NULL, |
| 52 | .getReadableSize = NULL, |
| 53 | .getPrune = NULL, |
| 54 | .setPrune = NULL, |
| 55 | .getStats = NULL, |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 58 | static int pmsgAvailable(log_id_t logId) { |
| 59 | if (logId > LOG_ID_SECURITY) { |
| 60 | return -EINVAL; |
| 61 | } |
| 62 | if (access("/dev/pmsg0", W_OK) == 0) { |
| 63 | return 0; |
| 64 | } |
| 65 | return -EBADF; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* Determine the credentials of the caller */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 69 | static bool uid_has_log_permission(uid_t uid) { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 70 | return (uid == AID_SYSTEM) || (uid == AID_LOG) || (uid == AID_ROOT) || (uid == AID_LOGD); |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 73 | static uid_t get_best_effective_uid() { |
| 74 | uid_t euid; |
| 75 | uid_t uid; |
| 76 | gid_t gid; |
| 77 | ssize_t i; |
| 78 | static uid_t last_uid = (uid_t)-1; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 79 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 80 | if (last_uid != (uid_t)-1) { |
| 81 | return last_uid; |
| 82 | } |
| 83 | uid = __android_log_uid(); |
| 84 | if (uid_has_log_permission(uid)) { |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 85 | return last_uid = uid; |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 86 | } |
| 87 | euid = geteuid(); |
| 88 | if (uid_has_log_permission(euid)) { |
| 89 | return last_uid = euid; |
| 90 | } |
| 91 | gid = getgid(); |
| 92 | if (uid_has_log_permission(gid)) { |
| 93 | return last_uid = gid; |
| 94 | } |
| 95 | gid = getegid(); |
| 96 | if (uid_has_log_permission(gid)) { |
| 97 | return last_uid = gid; |
| 98 | } |
| 99 | i = getgroups((size_t)0, NULL); |
| 100 | if (i > 0) { |
| 101 | gid_t list[i]; |
| 102 | |
| 103 | getgroups(i, list); |
| 104 | while (--i >= 0) { |
| 105 | if (uid_has_log_permission(list[i])) { |
| 106 | return last_uid = list[i]; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | return last_uid = uid; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 113 | static int pmsgClear(struct android_log_logger* logger __unused, |
| 114 | struct android_log_transport_context* transp __unused) { |
| 115 | if (uid_has_log_permission(get_best_effective_uid())) { |
| 116 | return unlink("/sys/fs/pstore/pmsg-ramoops-0"); |
| 117 | } |
| 118 | errno = EPERM; |
| 119 | return -1; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
| 123 | * returns the logger version |
| 124 | */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 125 | static int pmsgVersion(struct android_log_logger* logger __unused, |
| 126 | struct android_log_transport_context* transp __unused) { |
| 127 | return 4; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 130 | static int pmsgRead(struct android_log_logger_list* logger_list, |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 131 | struct android_log_transport_context* transp, struct log_msg* log_msg) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 132 | ssize_t ret; |
| 133 | off_t current, next; |
| 134 | uid_t uid; |
| 135 | struct android_log_logger* logger; |
| 136 | struct __attribute__((__packed__)) { |
| 137 | android_pmsg_log_header_t p; |
| 138 | android_log_header_t l; |
| 139 | uint8_t prio; |
| 140 | } buf; |
| 141 | static uint8_t preread_count; |
| 142 | bool is_system; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 143 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 144 | memset(log_msg, 0, sizeof(*log_msg)); |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 145 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 146 | if (atomic_load(&transp->context.fd) <= 0) { |
| 147 | int i, fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC); |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 148 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 149 | if (fd < 0) { |
| 150 | return -errno; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 151 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 152 | if (fd == 0) { /* Argggg */ |
| 153 | fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC); |
| 154 | close(0); |
| 155 | if (fd < 0) { |
| 156 | return -errno; |
| 157 | } |
| 158 | } |
| 159 | i = atomic_exchange(&transp->context.fd, fd); |
| 160 | if ((i > 0) && (i != fd)) { |
| 161 | close(i); |
| 162 | } |
| 163 | preread_count = 0; |
| 164 | } |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 165 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 166 | while (1) { |
| 167 | int fd; |
Mark Salyzyn | ceee2f5 | 2016-10-10 07:27:42 -0700 | [diff] [blame] | 168 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 169 | if (preread_count < sizeof(buf)) { |
| 170 | fd = atomic_load(&transp->context.fd); |
| 171 | if (fd <= 0) { |
| 172 | return -EBADF; |
| 173 | } |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 174 | ret = TEMP_FAILURE_RETRY(read(fd, &buf.p.magic + preread_count, sizeof(buf) - preread_count)); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 175 | if (ret < 0) { |
| 176 | return -errno; |
| 177 | } |
| 178 | preread_count += ret; |
| 179 | } |
| 180 | if (preread_count != sizeof(buf)) { |
| 181 | return preread_count ? -EIO : -EAGAIN; |
| 182 | } |
| 183 | if ((buf.p.magic != LOGGER_MAGIC) || (buf.p.len <= sizeof(buf)) || |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 184 | (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) || (buf.l.id >= LOG_ID_MAX) || |
| 185 | (buf.l.realtime.tv_nsec >= NS_PER_SEC) || |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 186 | ((buf.l.id != LOG_ID_EVENTS) && (buf.l.id != LOG_ID_SECURITY) && |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 187 | ((buf.prio == ANDROID_LOG_UNKNOWN) || (buf.prio == ANDROID_LOG_DEFAULT) || |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 188 | (buf.prio >= ANDROID_LOG_SILENT)))) { |
| 189 | do { |
| 190 | memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count); |
| 191 | } while (preread_count && (buf.p.magic != LOGGER_MAGIC)); |
| 192 | continue; |
| 193 | } |
| 194 | preread_count = 0; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 195 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 196 | if ((transp->logMask & (1 << buf.l.id)) && |
| 197 | ((!logger_list->start.tv_sec && !logger_list->start.tv_nsec) || |
| 198 | ((logger_list->start.tv_sec <= buf.l.realtime.tv_sec) && |
| 199 | ((logger_list->start.tv_sec != buf.l.realtime.tv_sec) || |
| 200 | (logger_list->start.tv_nsec <= buf.l.realtime.tv_nsec)))) && |
| 201 | (!logger_list->pid || (logger_list->pid == buf.p.pid))) { |
| 202 | uid = get_best_effective_uid(); |
| 203 | is_system = uid_has_log_permission(uid); |
| 204 | if (is_system || (uid == buf.p.uid)) { |
| 205 | char* msg = is_system ? log_msg->entry_v4.msg : log_msg->entry_v3.msg; |
| 206 | *msg = buf.prio; |
Mark Salyzyn | ceee2f5 | 2016-10-10 07:27:42 -0700 | [diff] [blame] | 207 | fd = atomic_load(&transp->context.fd); |
| 208 | if (fd <= 0) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 209 | return -EBADF; |
Mark Salyzyn | ceee2f5 | 2016-10-10 07:27:42 -0700 | [diff] [blame] | 210 | } |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 211 | ret = TEMP_FAILURE_RETRY(read(fd, msg + sizeof(buf.prio), buf.p.len - sizeof(buf))); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 212 | if (ret < 0) { |
| 213 | return -errno; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 214 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 215 | if (ret != (ssize_t)(buf.p.len - sizeof(buf))) { |
| 216 | return -EIO; |
Mark Salyzyn | ceee2f5 | 2016-10-10 07:27:42 -0700 | [diff] [blame] | 217 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 218 | |
| 219 | log_msg->entry_v4.len = buf.p.len - sizeof(buf) + sizeof(buf.prio); |
| 220 | log_msg->entry_v4.hdr_size = |
| 221 | is_system ? sizeof(log_msg->entry_v4) : sizeof(log_msg->entry_v3); |
| 222 | log_msg->entry_v4.pid = buf.p.pid; |
| 223 | log_msg->entry_v4.tid = buf.l.tid; |
| 224 | log_msg->entry_v4.sec = buf.l.realtime.tv_sec; |
| 225 | log_msg->entry_v4.nsec = buf.l.realtime.tv_nsec; |
| 226 | log_msg->entry_v4.lid = buf.l.id; |
| 227 | if (is_system) { |
| 228 | log_msg->entry_v4.uid = buf.p.uid; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 229 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 230 | |
| 231 | return ret + sizeof(buf.prio) + log_msg->entry_v4.hdr_size; |
| 232 | } |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 233 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 234 | |
| 235 | fd = atomic_load(&transp->context.fd); |
| 236 | if (fd <= 0) { |
| 237 | return -EBADF; |
| 238 | } |
| 239 | current = TEMP_FAILURE_RETRY(lseek(fd, (off_t)0, SEEK_CUR)); |
| 240 | if (current < 0) { |
| 241 | return -errno; |
| 242 | } |
| 243 | fd = atomic_load(&transp->context.fd); |
| 244 | if (fd <= 0) { |
| 245 | return -EBADF; |
| 246 | } |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 247 | next = TEMP_FAILURE_RETRY(lseek(fd, (off_t)(buf.p.len - sizeof(buf)), SEEK_CUR)); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 248 | if (next < 0) { |
| 249 | return -errno; |
| 250 | } |
| 251 | if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) { |
| 252 | return -EIO; |
| 253 | } |
| 254 | } |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 257 | static void pmsgClose(struct android_log_logger_list* logger_list __unused, |
| 258 | struct android_log_transport_context* transp) { |
| 259 | int fd = atomic_exchange(&transp->context.fd, 0); |
| 260 | if (fd > 0) { |
| 261 | close(fd); |
| 262 | } |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 263 | } |
Mark Salyzyn | 5b2ce40 | 2016-03-14 14:15:50 -0700 | [diff] [blame] | 264 | |
George Burgess IV | 32851b2 | 2018-10-03 14:10:00 -0700 | [diff] [blame] | 265 | static void* realloc_or_free(void* ptr, size_t new_size) { |
| 266 | void* result = realloc(ptr, new_size); |
| 267 | if (!result) { |
| 268 | free(ptr); |
| 269 | } |
| 270 | return result; |
| 271 | } |
| 272 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 273 | LIBLOG_ABI_PRIVATE ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, |
| 274 | const char* prefix, |
| 275 | __android_log_pmsg_file_read_fn fn, |
| 276 | void* arg) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 277 | ssize_t ret; |
| 278 | struct android_log_logger_list logger_list; |
| 279 | struct android_log_transport_context transp; |
| 280 | struct content { |
| 281 | struct listnode node; |
| 282 | union { |
| 283 | struct logger_entry_v4 entry; |
| 284 | struct logger_entry_v4 entry_v4; |
| 285 | struct logger_entry_v3 entry_v3; |
| 286 | struct logger_entry_v2 entry_v2; |
| 287 | struct logger_entry entry_v1; |
| 288 | }; |
| 289 | } * content; |
| 290 | struct names { |
| 291 | struct listnode node; |
| 292 | struct listnode content; |
| 293 | log_id_t id; |
| 294 | char prio; |
| 295 | char name[]; |
| 296 | } * names; |
| 297 | struct listnode name_list; |
| 298 | struct listnode *node, *n; |
| 299 | size_t len, prefix_len; |
Mark Salyzyn | 5b2ce40 | 2016-03-14 14:15:50 -0700 | [diff] [blame] | 300 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 301 | if (!fn) { |
| 302 | return -EINVAL; |
| 303 | } |
| 304 | |
| 305 | /* Add just enough clues in logger_list and transp to make API function */ |
| 306 | memset(&logger_list, 0, sizeof(logger_list)); |
| 307 | memset(&transp, 0, sizeof(transp)); |
| 308 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 309 | logger_list.mode = ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK | ANDROID_LOG_RDONLY; |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 310 | transp.logMask = (unsigned)-1; |
| 311 | if (logId != LOG_ID_ANY) { |
| 312 | transp.logMask = (1 << logId); |
| 313 | } |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 314 | transp.logMask &= ~((1 << LOG_ID_KERNEL) | (1 << LOG_ID_EVENTS) | (1 << LOG_ID_SECURITY)); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 315 | if (!transp.logMask) { |
| 316 | return -EINVAL; |
| 317 | } |
| 318 | |
| 319 | /* Initialize name list */ |
| 320 | list_init(&name_list); |
| 321 | |
| 322 | ret = SSIZE_MAX; |
| 323 | |
| 324 | /* Validate incoming prefix, shift until it contains only 0 or 1 : or / */ |
| 325 | prefix_len = 0; |
| 326 | if (prefix) { |
| 327 | const char *prev = NULL, *last = NULL, *cp = prefix; |
| 328 | while ((cp = strpbrk(cp, "/:"))) { |
| 329 | prev = last; |
| 330 | last = cp; |
| 331 | cp = cp + 1; |
| 332 | } |
| 333 | if (prev) { |
| 334 | prefix = prev + 1; |
| 335 | } |
| 336 | prefix_len = strlen(prefix); |
| 337 | } |
| 338 | |
| 339 | /* Read the file content */ |
| 340 | while (pmsgRead(&logger_list, &transp, &transp.logMsg) > 0) { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 341 | const char* cp; |
| 342 | size_t hdr_size = transp.logMsg.entry.hdr_size ? transp.logMsg.entry.hdr_size |
| 343 | : sizeof(transp.logMsg.entry_v1); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 344 | char* msg = (char*)&transp.logMsg + hdr_size; |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 345 | const char* split = NULL; |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 346 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 347 | if ((hdr_size < sizeof(transp.logMsg.entry_v1)) || (hdr_size > sizeof(transp.logMsg.entry))) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 348 | continue; |
| 349 | } |
| 350 | /* Check for invalid sequence number */ |
| 351 | if ((transp.logMsg.entry.nsec % ANDROID_LOG_PMSG_FILE_SEQUENCE) || |
| 352 | ((transp.logMsg.entry.nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >= |
| 353 | ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE)) { |
| 354 | continue; |
Mark Salyzyn | 5b2ce40 | 2016-03-14 14:15:50 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 357 | /* Determine if it has <dirbase>:<filebase> format for tag */ |
| 358 | len = transp.logMsg.entry.len - sizeof(prio); |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 359 | for (cp = msg + sizeof(prio); *cp && isprint(*cp) && !isspace(*cp) && --len; ++cp) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 360 | if (*cp == ':') { |
| 361 | if (split) { |
| 362 | break; |
| 363 | } |
| 364 | split = cp; |
| 365 | } |
Mark Salyzyn | 5b2ce40 | 2016-03-14 14:15:50 -0700 | [diff] [blame] | 366 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 367 | if (*cp || !split) { |
| 368 | continue; |
Mark Salyzyn | 5b2ce40 | 2016-03-14 14:15:50 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 371 | /* Filters */ |
| 372 | if (prefix_len && strncmp(msg + sizeof(prio), prefix, prefix_len)) { |
| 373 | size_t offset; |
| 374 | /* |
| 375 | * Allow : to be a synonym for / |
| 376 | * Things we do dealing with const char * and do not alloc |
| 377 | */ |
| 378 | split = strchr(prefix, ':'); |
| 379 | if (split) { |
| 380 | continue; |
| 381 | } |
| 382 | split = strchr(prefix, '/'); |
| 383 | if (!split) { |
| 384 | continue; |
| 385 | } |
| 386 | offset = split - prefix; |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 387 | if ((msg[offset + sizeof(prio)] != ':') || strncmp(msg + sizeof(prio), prefix, offset)) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 388 | continue; |
| 389 | } |
| 390 | ++offset; |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 391 | if ((prefix_len > offset) && |
| 392 | strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 393 | continue; |
| 394 | } |
Mark Salyzyn | 5b2ce40 | 2016-03-14 14:15:50 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 397 | if ((prio != ANDROID_LOG_ANY) && (*msg < prio)) { |
| 398 | continue; |
Mark Salyzyn | 5b2ce40 | 2016-03-14 14:15:50 -0700 | [diff] [blame] | 399 | } |
Mark Salyzyn | 5b2ce40 | 2016-03-14 14:15:50 -0700 | [diff] [blame] | 400 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 401 | /* check if there is an existing entry */ |
| 402 | list_for_each(node, &name_list) { |
| 403 | names = node_to_item(node, struct names, node); |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 404 | if (!strcmp(names->name, msg + sizeof(prio)) && (names->id == transp.logMsg.entry.lid) && |
| 405 | (names->prio == *msg)) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 406 | break; |
| 407 | } |
Mark Salyzyn | 5b2ce40 | 2016-03-14 14:15:50 -0700 | [diff] [blame] | 408 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 409 | |
| 410 | /* We do not have an existing entry, create and add one */ |
| 411 | if (node == &name_list) { |
| 412 | static const char numbers[] = "0123456789"; |
| 413 | unsigned long long nl; |
| 414 | |
| 415 | len = strlen(msg + sizeof(prio)) + 1; |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 416 | names = static_cast<struct names*>(calloc(1, sizeof(*names) + len)); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 417 | if (!names) { |
| 418 | ret = -ENOMEM; |
| 419 | break; |
| 420 | } |
| 421 | strcpy(names->name, msg + sizeof(prio)); |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 422 | names->id = static_cast<log_id_t>(transp.logMsg.entry.lid); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 423 | names->prio = *msg; |
| 424 | list_init(&names->content); |
| 425 | /* |
| 426 | * Insert in reverse numeric _then_ alpha sorted order as |
| 427 | * representative of log rotation: |
| 428 | * |
| 429 | * log.10 |
| 430 | * klog.10 |
| 431 | * . . . |
| 432 | * log.2 |
| 433 | * klog.2 |
| 434 | * log.1 |
| 435 | * klog.1 |
| 436 | * log |
| 437 | * klog |
| 438 | * |
| 439 | * thus when we present the content, we are provided the oldest |
| 440 | * first, which when 'refreshed' could spill off the end of the |
| 441 | * pmsg FIFO but retaining the newest data for last with best |
| 442 | * chances to survive. |
| 443 | */ |
| 444 | nl = 0; |
| 445 | cp = strpbrk(names->name, numbers); |
| 446 | if (cp) { |
| 447 | nl = strtoull(cp, NULL, 10); |
| 448 | } |
| 449 | list_for_each_reverse(node, &name_list) { |
| 450 | struct names* a_name = node_to_item(node, struct names, node); |
| 451 | const char* r = a_name->name; |
| 452 | int compare = 0; |
| 453 | |
| 454 | unsigned long long nr = 0; |
| 455 | cp = strpbrk(r, numbers); |
| 456 | if (cp) { |
| 457 | nr = strtoull(cp, NULL, 10); |
| 458 | } |
| 459 | if (nr != nl) { |
| 460 | compare = (nl > nr) ? 1 : -1; |
| 461 | } |
| 462 | if (compare == 0) { |
| 463 | compare = strcmp(names->name, r); |
| 464 | } |
| 465 | if (compare <= 0) { |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | list_add_head(node, &names->node); |
| 470 | } |
| 471 | |
| 472 | /* Remove any file fragments that match our sequence number */ |
| 473 | list_for_each_safe(node, n, &names->content) { |
| 474 | content = node_to_item(node, struct content, node); |
| 475 | if (transp.logMsg.entry.nsec == content->entry.nsec) { |
| 476 | list_remove(&content->node); |
| 477 | free(content); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | /* Add content */ |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 482 | content = static_cast<struct content*>( |
| 483 | calloc(1, sizeof(content->node) + hdr_size + transp.logMsg.entry.len)); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 484 | if (!content) { |
| 485 | ret = -ENOMEM; |
| 486 | break; |
| 487 | } |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 488 | memcpy(&content->entry, &transp.logMsg.entry, hdr_size + transp.logMsg.entry.len); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 489 | |
| 490 | /* Insert in sequence number sorted order, to ease reconstruction */ |
| 491 | list_for_each_reverse(node, &names->content) { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 492 | if ((node_to_item(node, struct content, node))->entry.nsec < transp.logMsg.entry.nsec) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 493 | break; |
| 494 | } |
| 495 | } |
| 496 | list_add_head(node, &content->node); |
| 497 | } |
| 498 | pmsgClose(&logger_list, &transp); |
| 499 | |
| 500 | /* Progress through all the collected files */ |
| 501 | list_for_each_safe(node, n, &name_list) { |
| 502 | struct listnode *content_node, *m; |
| 503 | char* buf; |
| 504 | size_t sequence, tag_len; |
| 505 | |
| 506 | names = node_to_item(node, struct names, node); |
| 507 | |
| 508 | /* Construct content into a linear buffer */ |
| 509 | buf = NULL; |
| 510 | len = 0; |
| 511 | sequence = 0; |
| 512 | tag_len = strlen(names->name) + sizeof(char); /* tag + nul */ |
| 513 | list_for_each_safe(content_node, m, &names->content) { |
| 514 | ssize_t add_len; |
| 515 | |
| 516 | content = node_to_item(content_node, struct content, node); |
| 517 | add_len = content->entry.len - tag_len - sizeof(prio); |
| 518 | if (add_len <= 0) { |
| 519 | list_remove(content_node); |
| 520 | free(content); |
| 521 | continue; |
| 522 | } |
| 523 | |
| 524 | if (!buf) { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 525 | buf = static_cast<char*>(malloc(sizeof(char))); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 526 | if (!buf) { |
| 527 | ret = -ENOMEM; |
| 528 | list_remove(content_node); |
| 529 | free(content); |
| 530 | continue; |
| 531 | } |
| 532 | *buf = '\0'; |
| 533 | } |
| 534 | |
| 535 | /* Missing sequence numbers */ |
| 536 | while (sequence < content->entry.nsec) { |
| 537 | /* plus space for enforced nul */ |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 538 | buf = static_cast<char*>(realloc_or_free(buf, len + sizeof(char) + sizeof(char))); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 539 | if (!buf) { |
| 540 | break; |
| 541 | } |
| 542 | buf[len] = '\f'; /* Mark missing content with a form feed */ |
| 543 | buf[++len] = '\0'; |
| 544 | sequence += ANDROID_LOG_PMSG_FILE_SEQUENCE; |
| 545 | } |
| 546 | if (!buf) { |
| 547 | ret = -ENOMEM; |
| 548 | list_remove(content_node); |
| 549 | free(content); |
| 550 | continue; |
| 551 | } |
| 552 | /* plus space for enforced nul */ |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 553 | buf = static_cast<char*>(realloc_or_free(buf, len + add_len + sizeof(char))); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 554 | if (!buf) { |
| 555 | ret = -ENOMEM; |
| 556 | list_remove(content_node); |
| 557 | free(content); |
| 558 | continue; |
| 559 | } |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 560 | memcpy(buf + len, (char*)&content->entry + content->entry.hdr_size + tag_len + sizeof(prio), |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 561 | add_len); |
| 562 | len += add_len; |
| 563 | buf[len] = '\0'; /* enforce trailing hidden nul */ |
| 564 | sequence = content->entry.nsec + ANDROID_LOG_PMSG_FILE_SEQUENCE; |
| 565 | |
| 566 | list_remove(content_node); |
| 567 | free(content); |
| 568 | } |
| 569 | if (buf) { |
| 570 | if (len) { |
| 571 | /* Buffer contains enforced trailing nul just beyond length */ |
| 572 | ssize_t r; |
| 573 | *strchr(names->name, ':') = '/'; /* Convert back to filename */ |
| 574 | r = (*fn)(names->id, names->prio, names->name, buf, len, arg); |
| 575 | if ((ret >= 0) && (r > 0)) { |
| 576 | if (ret == SSIZE_MAX) { |
| 577 | ret = r; |
| 578 | } else { |
| 579 | ret += r; |
| 580 | } |
| 581 | } else if (r < ret) { |
| 582 | ret = r; |
| 583 | } |
| 584 | } |
| 585 | free(buf); |
| 586 | } |
| 587 | list_remove(node); |
| 588 | free(names); |
| 589 | } |
| 590 | return (ret == SSIZE_MAX) ? -ENOENT : ret; |
Mark Salyzyn | 5b2ce40 | 2016-03-14 14:15:50 -0700 | [diff] [blame] | 591 | } |