blob: cd83161f80ca0f3ffa48452f3e0f0e7a77087882 [file] [log] [blame]
Mark Salyzyn018a96d2016-03-01 13:45:42 -08001/*
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 Salyzyn864e8e82016-03-14 14:15:50 -070017#include <ctype.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080018#include <errno.h>
19#include <fcntl.h>
Mark Salyzyn864e8e82016-03-14 14:15:50 -070020#include <stdlib.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080021#include <string.h>
22#include <sys/types.h>
23
24#include <private/android_filesystem_config.h>
25#include <private/android_logger.h>
26
Mark Salyzyn018a96d2016-03-01 13:45:42 -080027#include "logger.h"
28
Tom Cherry828db1a2019-11-14 10:39:40 -080029static int PmsgAvailable(log_id_t logId);
30static int PmsgVersion(struct logger* logger, struct android_log_transport_context* transp);
31static int PmsgRead(struct logger_list* logger_list, struct android_log_transport_context* transp,
32 struct log_msg* log_msg);
33static void PmsgClose(struct logger_list* logger_list,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080034 struct android_log_transport_context* transp);
Tom Cherry828db1a2019-11-14 10:39:40 -080035static int PmsgClear(struct logger* logger, struct android_log_transport_context* transp);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080036
Tom Cherry2d9779e2019-02-08 11:46:19 -080037struct android_log_transport_read pmsgLoggerRead = {
Tom Cherry71ba1642019-01-10 10:37:36 -080038 .name = "pmsg",
Tom Cherry828db1a2019-11-14 10:39:40 -080039 .available = PmsgAvailable,
40 .version = PmsgVersion,
41 .close = PmsgClose,
42 .read = PmsgRead,
Tom Cherry71ba1642019-01-10 10:37:36 -080043 .poll = NULL,
Tom Cherry828db1a2019-11-14 10:39:40 -080044 .clear = PmsgClear,
Tom Cherry71ba1642019-01-10 10:37:36 -080045 .setSize = NULL,
46 .getSize = NULL,
47 .getReadableSize = NULL,
48 .getPrune = NULL,
49 .setPrune = NULL,
50 .getStats = NULL,
Mark Salyzyn018a96d2016-03-01 13:45:42 -080051};
52
Tom Cherry828db1a2019-11-14 10:39:40 -080053static int PmsgAvailable(log_id_t logId) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080054 if (logId > LOG_ID_SECURITY) {
55 return -EINVAL;
56 }
57 if (access("/dev/pmsg0", W_OK) == 0) {
58 return 0;
59 }
60 return -EBADF;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080061}
62
Tom Cherry828db1a2019-11-14 10:39:40 -080063static int PmsgClear(struct logger*, struct android_log_transport_context*) {
Tom Cherry1e59dcc2019-10-15 16:06:06 -070064 return unlink("/sys/fs/pstore/pmsg-ramoops-0");
Mark Salyzyn018a96d2016-03-01 13:45:42 -080065}
66
67/*
68 * returns the logger version
69 */
Tom Cherry828db1a2019-11-14 10:39:40 -080070static int PmsgVersion(struct logger*, struct android_log_transport_context*) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080071 return 4;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080072}
73
Tom Cherry828db1a2019-11-14 10:39:40 -080074static int PmsgRead(struct logger_list* logger_list, struct android_log_transport_context* transp,
75 struct log_msg* log_msg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080076 ssize_t ret;
77 off_t current, next;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080078 struct __attribute__((__packed__)) {
79 android_pmsg_log_header_t p;
80 android_log_header_t l;
81 uint8_t prio;
82 } buf;
83 static uint8_t preread_count;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080084
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080085 memset(log_msg, 0, sizeof(*log_msg));
Mark Salyzyn018a96d2016-03-01 13:45:42 -080086
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080087 if (atomic_load(&transp->context.fd) <= 0) {
88 int i, fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080089
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080090 if (fd < 0) {
91 return -errno;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080092 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080093 if (fd == 0) { /* Argggg */
94 fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
95 close(0);
96 if (fd < 0) {
97 return -errno;
98 }
99 }
100 i = atomic_exchange(&transp->context.fd, fd);
101 if ((i > 0) && (i != fd)) {
102 close(i);
103 }
104 preread_count = 0;
105 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800106
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800107 while (1) {
108 int fd;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700109
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800110 if (preread_count < sizeof(buf)) {
111 fd = atomic_load(&transp->context.fd);
112 if (fd <= 0) {
113 return -EBADF;
114 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800115 ret = TEMP_FAILURE_RETRY(read(fd, &buf.p.magic + preread_count, sizeof(buf) - preread_count));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800116 if (ret < 0) {
117 return -errno;
118 }
119 preread_count += ret;
120 }
121 if (preread_count != sizeof(buf)) {
122 return preread_count ? -EIO : -EAGAIN;
123 }
124 if ((buf.p.magic != LOGGER_MAGIC) || (buf.p.len <= sizeof(buf)) ||
Tom Cherry71ba1642019-01-10 10:37:36 -0800125 (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) || (buf.l.id >= LOG_ID_MAX) ||
126 (buf.l.realtime.tv_nsec >= NS_PER_SEC) ||
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800127 ((buf.l.id != LOG_ID_EVENTS) && (buf.l.id != LOG_ID_SECURITY) &&
Tom Cherry71ba1642019-01-10 10:37:36 -0800128 ((buf.prio == ANDROID_LOG_UNKNOWN) || (buf.prio == ANDROID_LOG_DEFAULT) ||
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800129 (buf.prio >= ANDROID_LOG_SILENT)))) {
130 do {
131 memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
132 } while (preread_count && (buf.p.magic != LOGGER_MAGIC));
133 continue;
134 }
135 preread_count = 0;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800136
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800137 if ((transp->logMask & (1 << buf.l.id)) &&
138 ((!logger_list->start.tv_sec && !logger_list->start.tv_nsec) ||
139 ((logger_list->start.tv_sec <= buf.l.realtime.tv_sec) &&
140 ((logger_list->start.tv_sec != buf.l.realtime.tv_sec) ||
141 (logger_list->start.tv_nsec <= buf.l.realtime.tv_nsec)))) &&
142 (!logger_list->pid || (logger_list->pid == buf.p.pid))) {
Tom Cherry441054a2019-10-15 16:53:11 -0700143 char* msg = log_msg->entry.msg;
Tom Cherry1e59dcc2019-10-15 16:06:06 -0700144 *msg = buf.prio;
145 fd = atomic_load(&transp->context.fd);
146 if (fd <= 0) {
147 return -EBADF;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800148 }
Tom Cherry1e59dcc2019-10-15 16:06:06 -0700149 ret = TEMP_FAILURE_RETRY(read(fd, msg + sizeof(buf.prio), buf.p.len - sizeof(buf)));
150 if (ret < 0) {
151 return -errno;
152 }
153 if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
154 return -EIO;
155 }
156
Tom Cherry441054a2019-10-15 16:53:11 -0700157 log_msg->entry.len = buf.p.len - sizeof(buf) + sizeof(buf.prio);
158 log_msg->entry.hdr_size = sizeof(log_msg->entry);
159 log_msg->entry.pid = buf.p.pid;
160 log_msg->entry.tid = buf.l.tid;
161 log_msg->entry.sec = buf.l.realtime.tv_sec;
162 log_msg->entry.nsec = buf.l.realtime.tv_nsec;
163 log_msg->entry.lid = buf.l.id;
164 log_msg->entry.uid = buf.p.uid;
Tom Cherry1e59dcc2019-10-15 16:06:06 -0700165
Tom Cherry441054a2019-10-15 16:53:11 -0700166 return ret + sizeof(buf.prio) + log_msg->entry.hdr_size;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800167 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800168
169 fd = atomic_load(&transp->context.fd);
170 if (fd <= 0) {
171 return -EBADF;
172 }
173 current = TEMP_FAILURE_RETRY(lseek(fd, (off_t)0, SEEK_CUR));
174 if (current < 0) {
175 return -errno;
176 }
177 fd = atomic_load(&transp->context.fd);
178 if (fd <= 0) {
179 return -EBADF;
180 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800181 next = TEMP_FAILURE_RETRY(lseek(fd, (off_t)(buf.p.len - sizeof(buf)), SEEK_CUR));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800182 if (next < 0) {
183 return -errno;
184 }
185 if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
186 return -EIO;
187 }
188 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800189}
190
Tom Cherry828db1a2019-11-14 10:39:40 -0800191static void PmsgClose(struct logger_list*, struct android_log_transport_context* transp) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800192 int fd = atomic_exchange(&transp->context.fd, 0);
193 if (fd > 0) {
194 close(fd);
195 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800196}
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700197
George Burgess IV559387d2018-10-03 14:10:00 -0700198static void* realloc_or_free(void* ptr, size_t new_size) {
199 void* result = realloc(ptr, new_size);
200 if (!result) {
201 free(ptr);
202 }
203 return result;
204}
205
Tom Cherry2d9779e2019-02-08 11:46:19 -0800206ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* prefix,
207 __android_log_pmsg_file_read_fn fn, void* arg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800208 ssize_t ret;
Tom Cherry828db1a2019-11-14 10:39:40 -0800209 struct logger_list logger_list;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800210 struct android_log_transport_context transp;
211 struct content {
212 struct listnode node;
Tom Cherry441054a2019-10-15 16:53:11 -0700213 struct logger_entry entry;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800214 } * content;
215 struct names {
216 struct listnode node;
217 struct listnode content;
218 log_id_t id;
219 char prio;
220 char name[];
221 } * names;
222 struct listnode name_list;
223 struct listnode *node, *n;
224 size_t len, prefix_len;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700225
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800226 if (!fn) {
227 return -EINVAL;
228 }
229
230 /* Add just enough clues in logger_list and transp to make API function */
231 memset(&logger_list, 0, sizeof(logger_list));
232 memset(&transp, 0, sizeof(transp));
233
Tom Cherry71ba1642019-01-10 10:37:36 -0800234 logger_list.mode = ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK | ANDROID_LOG_RDONLY;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800235 transp.logMask = (unsigned)-1;
236 if (logId != LOG_ID_ANY) {
237 transp.logMask = (1 << logId);
238 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800239 transp.logMask &= ~((1 << LOG_ID_KERNEL) | (1 << LOG_ID_EVENTS) | (1 << LOG_ID_SECURITY));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800240 if (!transp.logMask) {
241 return -EINVAL;
242 }
243
244 /* Initialize name list */
245 list_init(&name_list);
246
247 ret = SSIZE_MAX;
248
249 /* Validate incoming prefix, shift until it contains only 0 or 1 : or / */
250 prefix_len = 0;
251 if (prefix) {
252 const char *prev = NULL, *last = NULL, *cp = prefix;
253 while ((cp = strpbrk(cp, "/:"))) {
254 prev = last;
255 last = cp;
256 cp = cp + 1;
257 }
258 if (prev) {
259 prefix = prev + 1;
260 }
261 prefix_len = strlen(prefix);
262 }
263
264 /* Read the file content */
Tom Cherrye05b7842019-10-16 10:16:44 -0700265 log_msg log_msg;
Tom Cherry828db1a2019-11-14 10:39:40 -0800266 while (PmsgRead(&logger_list, &transp, &log_msg) > 0) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800267 const char* cp;
Tom Cherrye05b7842019-10-16 10:16:44 -0700268 size_t hdr_size = log_msg.entry.hdr_size;
Tom Cherry441054a2019-10-15 16:53:11 -0700269
Tom Cherrye05b7842019-10-16 10:16:44 -0700270 char* msg = (char*)&log_msg + hdr_size;
Tom Cherry71ba1642019-01-10 10:37:36 -0800271 const char* split = NULL;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800272
Tom Cherrye05b7842019-10-16 10:16:44 -0700273 if (hdr_size != sizeof(log_msg.entry)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800274 continue;
275 }
276 /* Check for invalid sequence number */
Tom Cherrye05b7842019-10-16 10:16:44 -0700277 if (log_msg.entry.nsec % ANDROID_LOG_PMSG_FILE_SEQUENCE ||
278 (log_msg.entry.nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
279 ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800280 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700281 }
282
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800283 /* Determine if it has <dirbase>:<filebase> format for tag */
Tom Cherrye05b7842019-10-16 10:16:44 -0700284 len = log_msg.entry.len - sizeof(prio);
Tom Cherry71ba1642019-01-10 10:37:36 -0800285 for (cp = msg + sizeof(prio); *cp && isprint(*cp) && !isspace(*cp) && --len; ++cp) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800286 if (*cp == ':') {
287 if (split) {
288 break;
289 }
290 split = cp;
291 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700292 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800293 if (*cp || !split) {
294 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700295 }
296
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800297 /* Filters */
298 if (prefix_len && strncmp(msg + sizeof(prio), prefix, prefix_len)) {
299 size_t offset;
300 /*
301 * Allow : to be a synonym for /
302 * Things we do dealing with const char * and do not alloc
303 */
304 split = strchr(prefix, ':');
305 if (split) {
306 continue;
307 }
308 split = strchr(prefix, '/');
309 if (!split) {
310 continue;
311 }
312 offset = split - prefix;
Tom Cherry71ba1642019-01-10 10:37:36 -0800313 if ((msg[offset + sizeof(prio)] != ':') || strncmp(msg + sizeof(prio), prefix, offset)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800314 continue;
315 }
316 ++offset;
Tom Cherry71ba1642019-01-10 10:37:36 -0800317 if ((prefix_len > offset) &&
318 strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800319 continue;
320 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700321 }
322
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800323 if ((prio != ANDROID_LOG_ANY) && (*msg < prio)) {
324 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700325 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700326
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800327 /* check if there is an existing entry */
328 list_for_each(node, &name_list) {
329 names = node_to_item(node, struct names, node);
Tom Cherrye05b7842019-10-16 10:16:44 -0700330 if (!strcmp(names->name, msg + sizeof(prio)) && names->id == log_msg.entry.lid &&
331 names->prio == *msg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800332 break;
333 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700334 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800335
336 /* We do not have an existing entry, create and add one */
337 if (node == &name_list) {
338 static const char numbers[] = "0123456789";
339 unsigned long long nl;
340
341 len = strlen(msg + sizeof(prio)) + 1;
Tom Cherry71ba1642019-01-10 10:37:36 -0800342 names = static_cast<struct names*>(calloc(1, sizeof(*names) + len));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800343 if (!names) {
344 ret = -ENOMEM;
345 break;
346 }
347 strcpy(names->name, msg + sizeof(prio));
Tom Cherrye05b7842019-10-16 10:16:44 -0700348 names->id = static_cast<log_id_t>(log_msg.entry.lid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800349 names->prio = *msg;
350 list_init(&names->content);
351 /*
352 * Insert in reverse numeric _then_ alpha sorted order as
353 * representative of log rotation:
354 *
355 * log.10
356 * klog.10
357 * . . .
358 * log.2
359 * klog.2
360 * log.1
361 * klog.1
362 * log
363 * klog
364 *
365 * thus when we present the content, we are provided the oldest
366 * first, which when 'refreshed' could spill off the end of the
367 * pmsg FIFO but retaining the newest data for last with best
368 * chances to survive.
369 */
370 nl = 0;
371 cp = strpbrk(names->name, numbers);
372 if (cp) {
373 nl = strtoull(cp, NULL, 10);
374 }
375 list_for_each_reverse(node, &name_list) {
376 struct names* a_name = node_to_item(node, struct names, node);
377 const char* r = a_name->name;
378 int compare = 0;
379
380 unsigned long long nr = 0;
381 cp = strpbrk(r, numbers);
382 if (cp) {
383 nr = strtoull(cp, NULL, 10);
384 }
385 if (nr != nl) {
386 compare = (nl > nr) ? 1 : -1;
387 }
388 if (compare == 0) {
389 compare = strcmp(names->name, r);
390 }
391 if (compare <= 0) {
392 break;
393 }
394 }
395 list_add_head(node, &names->node);
396 }
397
398 /* Remove any file fragments that match our sequence number */
399 list_for_each_safe(node, n, &names->content) {
400 content = node_to_item(node, struct content, node);
Tom Cherrye05b7842019-10-16 10:16:44 -0700401 if (log_msg.entry.nsec == content->entry.nsec) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800402 list_remove(&content->node);
403 free(content);
404 }
405 }
406
407 /* Add content */
Tom Cherry71ba1642019-01-10 10:37:36 -0800408 content = static_cast<struct content*>(
Tom Cherrye05b7842019-10-16 10:16:44 -0700409 calloc(1, sizeof(content->node) + hdr_size + log_msg.entry.len));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800410 if (!content) {
411 ret = -ENOMEM;
412 break;
413 }
Tom Cherrye05b7842019-10-16 10:16:44 -0700414 memcpy(&content->entry, &log_msg.entry, hdr_size + log_msg.entry.len);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800415
416 /* Insert in sequence number sorted order, to ease reconstruction */
417 list_for_each_reverse(node, &names->content) {
Tom Cherrye05b7842019-10-16 10:16:44 -0700418 if ((node_to_item(node, struct content, node))->entry.nsec < log_msg.entry.nsec) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800419 break;
420 }
421 }
422 list_add_head(node, &content->node);
423 }
Tom Cherry828db1a2019-11-14 10:39:40 -0800424 PmsgClose(&logger_list, &transp);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800425
426 /* Progress through all the collected files */
427 list_for_each_safe(node, n, &name_list) {
428 struct listnode *content_node, *m;
429 char* buf;
430 size_t sequence, tag_len;
431
432 names = node_to_item(node, struct names, node);
433
434 /* Construct content into a linear buffer */
435 buf = NULL;
436 len = 0;
437 sequence = 0;
438 tag_len = strlen(names->name) + sizeof(char); /* tag + nul */
439 list_for_each_safe(content_node, m, &names->content) {
440 ssize_t add_len;
441
442 content = node_to_item(content_node, struct content, node);
443 add_len = content->entry.len - tag_len - sizeof(prio);
444 if (add_len <= 0) {
445 list_remove(content_node);
446 free(content);
447 continue;
448 }
449
450 if (!buf) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800451 buf = static_cast<char*>(malloc(sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800452 if (!buf) {
453 ret = -ENOMEM;
454 list_remove(content_node);
455 free(content);
456 continue;
457 }
458 *buf = '\0';
459 }
460
461 /* Missing sequence numbers */
462 while (sequence < content->entry.nsec) {
463 /* plus space for enforced nul */
Tom Cherry71ba1642019-01-10 10:37:36 -0800464 buf = static_cast<char*>(realloc_or_free(buf, len + sizeof(char) + sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800465 if (!buf) {
466 break;
467 }
468 buf[len] = '\f'; /* Mark missing content with a form feed */
469 buf[++len] = '\0';
470 sequence += ANDROID_LOG_PMSG_FILE_SEQUENCE;
471 }
472 if (!buf) {
473 ret = -ENOMEM;
474 list_remove(content_node);
475 free(content);
476 continue;
477 }
478 /* plus space for enforced nul */
Tom Cherry71ba1642019-01-10 10:37:36 -0800479 buf = static_cast<char*>(realloc_or_free(buf, len + add_len + sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800480 if (!buf) {
481 ret = -ENOMEM;
482 list_remove(content_node);
483 free(content);
484 continue;
485 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800486 memcpy(buf + len, (char*)&content->entry + content->entry.hdr_size + tag_len + sizeof(prio),
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800487 add_len);
488 len += add_len;
489 buf[len] = '\0'; /* enforce trailing hidden nul */
490 sequence = content->entry.nsec + ANDROID_LOG_PMSG_FILE_SEQUENCE;
491
492 list_remove(content_node);
493 free(content);
494 }
495 if (buf) {
496 if (len) {
497 /* Buffer contains enforced trailing nul just beyond length */
498 ssize_t r;
499 *strchr(names->name, ':') = '/'; /* Convert back to filename */
500 r = (*fn)(names->id, names->prio, names->name, buf, len, arg);
501 if ((ret >= 0) && (r > 0)) {
502 if (ret == SSIZE_MAX) {
503 ret = r;
504 } else {
505 ret += r;
506 }
507 } else if (r < ret) {
508 ret = r;
509 }
510 }
511 free(buf);
512 }
513 list_remove(node);
514 free(names);
515 }
516 return (ret == SSIZE_MAX) ? -ENOENT : ret;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700517}