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