blob: ce923f332404678ac8c2176ec42fdbac449e8094 [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
29static int pmsgAvailable(log_id_t logId);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080030static int pmsgVersion(struct android_log_logger* logger,
31 struct android_log_transport_context* transp);
32static int pmsgRead(struct android_log_logger_list* logger_list,
Tom Cherry71ba1642019-01-10 10:37:36 -080033 struct android_log_transport_context* transp, struct log_msg* log_msg);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080034static void pmsgClose(struct android_log_logger_list* logger_list,
35 struct android_log_transport_context* transp);
36static int pmsgClear(struct android_log_logger* logger,
37 struct android_log_transport_context* transp);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080038
Tom Cherry2d9779e2019-02-08 11:46:19 -080039struct android_log_transport_read pmsgLoggerRead = {
Tom Cherry71ba1642019-01-10 10:37:36 -080040 .name = "pmsg",
41 .available = pmsgAvailable,
42 .version = pmsgVersion,
Nick Desaulniers34282df2019-10-07 17:31:18 -070043 .close = pmsgClose,
Tom Cherry71ba1642019-01-10 10:37:36 -080044 .read = pmsgRead,
45 .poll = NULL,
Tom Cherry71ba1642019-01-10 10:37:36 -080046 .clear = pmsgClear,
47 .setSize = NULL,
48 .getSize = NULL,
49 .getReadableSize = NULL,
50 .getPrune = NULL,
51 .setPrune = NULL,
52 .getStats = NULL,
Mark Salyzyn018a96d2016-03-01 13:45:42 -080053};
54
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080055static int pmsgAvailable(log_id_t logId) {
56 if (logId > LOG_ID_SECURITY) {
57 return -EINVAL;
58 }
59 if (access("/dev/pmsg0", W_OK) == 0) {
60 return 0;
61 }
62 return -EBADF;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080063}
64
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080065static int pmsgClear(struct android_log_logger* logger __unused,
66 struct android_log_transport_context* transp __unused) {
Tom Cherry1e59dcc2019-10-15 16:06:06 -070067 return unlink("/sys/fs/pstore/pmsg-ramoops-0");
Mark Salyzyn018a96d2016-03-01 13:45:42 -080068}
69
70/*
71 * returns the logger version
72 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080073static int pmsgVersion(struct android_log_logger* logger __unused,
74 struct android_log_transport_context* transp __unused) {
75 return 4;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080076}
77
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080078static int pmsgRead(struct android_log_logger_list* logger_list,
Tom Cherry71ba1642019-01-10 10:37:36 -080079 struct android_log_transport_context* transp, struct log_msg* log_msg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080080 ssize_t ret;
81 off_t current, next;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080082 struct __attribute__((__packed__)) {
83 android_pmsg_log_header_t p;
84 android_log_header_t l;
85 uint8_t prio;
86 } buf;
87 static uint8_t preread_count;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080088
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080089 memset(log_msg, 0, sizeof(*log_msg));
Mark Salyzyn018a96d2016-03-01 13:45:42 -080090
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080091 if (atomic_load(&transp->context.fd) <= 0) {
92 int i, fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080093
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080094 if (fd < 0) {
95 return -errno;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080096 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080097 if (fd == 0) { /* Argggg */
98 fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
99 close(0);
100 if (fd < 0) {
101 return -errno;
102 }
103 }
104 i = atomic_exchange(&transp->context.fd, fd);
105 if ((i > 0) && (i != fd)) {
106 close(i);
107 }
108 preread_count = 0;
109 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800110
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800111 while (1) {
112 int fd;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700113
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800114 if (preread_count < sizeof(buf)) {
115 fd = atomic_load(&transp->context.fd);
116 if (fd <= 0) {
117 return -EBADF;
118 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800119 ret = TEMP_FAILURE_RETRY(read(fd, &buf.p.magic + preread_count, sizeof(buf) - preread_count));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800120 if (ret < 0) {
121 return -errno;
122 }
123 preread_count += ret;
124 }
125 if (preread_count != sizeof(buf)) {
126 return preread_count ? -EIO : -EAGAIN;
127 }
128 if ((buf.p.magic != LOGGER_MAGIC) || (buf.p.len <= sizeof(buf)) ||
Tom Cherry71ba1642019-01-10 10:37:36 -0800129 (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) || (buf.l.id >= LOG_ID_MAX) ||
130 (buf.l.realtime.tv_nsec >= NS_PER_SEC) ||
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800131 ((buf.l.id != LOG_ID_EVENTS) && (buf.l.id != LOG_ID_SECURITY) &&
Tom Cherry71ba1642019-01-10 10:37:36 -0800132 ((buf.prio == ANDROID_LOG_UNKNOWN) || (buf.prio == ANDROID_LOG_DEFAULT) ||
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800133 (buf.prio >= ANDROID_LOG_SILENT)))) {
134 do {
135 memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
136 } while (preread_count && (buf.p.magic != LOGGER_MAGIC));
137 continue;
138 }
139 preread_count = 0;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800140
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800141 if ((transp->logMask & (1 << buf.l.id)) &&
142 ((!logger_list->start.tv_sec && !logger_list->start.tv_nsec) ||
143 ((logger_list->start.tv_sec <= buf.l.realtime.tv_sec) &&
144 ((logger_list->start.tv_sec != buf.l.realtime.tv_sec) ||
145 (logger_list->start.tv_nsec <= buf.l.realtime.tv_nsec)))) &&
146 (!logger_list->pid || (logger_list->pid == buf.p.pid))) {
Tom Cherry1e59dcc2019-10-15 16:06:06 -0700147 char* msg = log_msg->entry_v4.msg;
148 *msg = buf.prio;
149 fd = atomic_load(&transp->context.fd);
150 if (fd <= 0) {
151 return -EBADF;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800152 }
Tom Cherry1e59dcc2019-10-15 16:06:06 -0700153 ret = TEMP_FAILURE_RETRY(read(fd, msg + sizeof(buf.prio), buf.p.len - sizeof(buf)));
154 if (ret < 0) {
155 return -errno;
156 }
157 if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
158 return -EIO;
159 }
160
161 log_msg->entry_v4.len = buf.p.len - sizeof(buf) + sizeof(buf.prio);
162 log_msg->entry_v4.hdr_size = sizeof(log_msg->entry_v4);
163 log_msg->entry_v4.pid = buf.p.pid;
164 log_msg->entry_v4.tid = buf.l.tid;
165 log_msg->entry_v4.sec = buf.l.realtime.tv_sec;
166 log_msg->entry_v4.nsec = buf.l.realtime.tv_nsec;
167 log_msg->entry_v4.lid = buf.l.id;
168 log_msg->entry_v4.uid = buf.p.uid;
169
170 return ret + sizeof(buf.prio) + log_msg->entry_v4.hdr_size;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800171 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800172
173 fd = atomic_load(&transp->context.fd);
174 if (fd <= 0) {
175 return -EBADF;
176 }
177 current = TEMP_FAILURE_RETRY(lseek(fd, (off_t)0, SEEK_CUR));
178 if (current < 0) {
179 return -errno;
180 }
181 fd = atomic_load(&transp->context.fd);
182 if (fd <= 0) {
183 return -EBADF;
184 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800185 next = TEMP_FAILURE_RETRY(lseek(fd, (off_t)(buf.p.len - sizeof(buf)), SEEK_CUR));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800186 if (next < 0) {
187 return -errno;
188 }
189 if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
190 return -EIO;
191 }
192 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800193}
194
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800195static void pmsgClose(struct android_log_logger_list* logger_list __unused,
196 struct android_log_transport_context* transp) {
197 int fd = atomic_exchange(&transp->context.fd, 0);
198 if (fd > 0) {
199 close(fd);
200 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800201}
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700202
George Burgess IV559387d2018-10-03 14:10:00 -0700203static void* realloc_or_free(void* ptr, size_t new_size) {
204 void* result = realloc(ptr, new_size);
205 if (!result) {
206 free(ptr);
207 }
208 return result;
209}
210
Tom Cherry2d9779e2019-02-08 11:46:19 -0800211ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* prefix,
212 __android_log_pmsg_file_read_fn fn, void* arg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800213 ssize_t ret;
214 struct android_log_logger_list logger_list;
215 struct android_log_transport_context transp;
216 struct content {
217 struct listnode node;
Tom Cherry1e59dcc2019-10-15 16:06:06 -0700218 struct logger_entry_v4 entry;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800219 } * content;
220 struct names {
221 struct listnode node;
222 struct listnode content;
223 log_id_t id;
224 char prio;
225 char name[];
226 } * names;
227 struct listnode name_list;
228 struct listnode *node, *n;
229 size_t len, prefix_len;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700230
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800231 if (!fn) {
232 return -EINVAL;
233 }
234
235 /* Add just enough clues in logger_list and transp to make API function */
236 memset(&logger_list, 0, sizeof(logger_list));
237 memset(&transp, 0, sizeof(transp));
238
Tom Cherry71ba1642019-01-10 10:37:36 -0800239 logger_list.mode = ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK | ANDROID_LOG_RDONLY;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800240 transp.logMask = (unsigned)-1;
241 if (logId != LOG_ID_ANY) {
242 transp.logMask = (1 << logId);
243 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800244 transp.logMask &= ~((1 << LOG_ID_KERNEL) | (1 << LOG_ID_EVENTS) | (1 << LOG_ID_SECURITY));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800245 if (!transp.logMask) {
246 return -EINVAL;
247 }
248
249 /* Initialize name list */
250 list_init(&name_list);
251
252 ret = SSIZE_MAX;
253
254 /* Validate incoming prefix, shift until it contains only 0 or 1 : or / */
255 prefix_len = 0;
256 if (prefix) {
257 const char *prev = NULL, *last = NULL, *cp = prefix;
258 while ((cp = strpbrk(cp, "/:"))) {
259 prev = last;
260 last = cp;
261 cp = cp + 1;
262 }
263 if (prev) {
264 prefix = prev + 1;
265 }
266 prefix_len = strlen(prefix);
267 }
268
269 /* Read the file content */
270 while (pmsgRead(&logger_list, &transp, &transp.logMsg) > 0) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800271 const char* cp;
272 size_t hdr_size = transp.logMsg.entry.hdr_size ? transp.logMsg.entry.hdr_size
273 : sizeof(transp.logMsg.entry_v1);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800274 char* msg = (char*)&transp.logMsg + hdr_size;
Tom Cherry71ba1642019-01-10 10:37:36 -0800275 const char* split = NULL;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800276
Tom Cherry71ba1642019-01-10 10:37:36 -0800277 if ((hdr_size < sizeof(transp.logMsg.entry_v1)) || (hdr_size > sizeof(transp.logMsg.entry))) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800278 continue;
279 }
280 /* Check for invalid sequence number */
281 if ((transp.logMsg.entry.nsec % ANDROID_LOG_PMSG_FILE_SEQUENCE) ||
282 ((transp.logMsg.entry.nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
283 ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE)) {
284 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700285 }
286
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800287 /* Determine if it has <dirbase>:<filebase> format for tag */
288 len = transp.logMsg.entry.len - sizeof(prio);
Tom Cherry71ba1642019-01-10 10:37:36 -0800289 for (cp = msg + sizeof(prio); *cp && isprint(*cp) && !isspace(*cp) && --len; ++cp) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800290 if (*cp == ':') {
291 if (split) {
292 break;
293 }
294 split = cp;
295 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700296 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800297 if (*cp || !split) {
298 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700299 }
300
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800301 /* Filters */
302 if (prefix_len && strncmp(msg + sizeof(prio), prefix, prefix_len)) {
303 size_t offset;
304 /*
305 * Allow : to be a synonym for /
306 * Things we do dealing with const char * and do not alloc
307 */
308 split = strchr(prefix, ':');
309 if (split) {
310 continue;
311 }
312 split = strchr(prefix, '/');
313 if (!split) {
314 continue;
315 }
316 offset = split - prefix;
Tom Cherry71ba1642019-01-10 10:37:36 -0800317 if ((msg[offset + sizeof(prio)] != ':') || strncmp(msg + sizeof(prio), prefix, offset)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800318 continue;
319 }
320 ++offset;
Tom Cherry71ba1642019-01-10 10:37:36 -0800321 if ((prefix_len > offset) &&
322 strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800323 continue;
324 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700325 }
326
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800327 if ((prio != ANDROID_LOG_ANY) && (*msg < prio)) {
328 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700329 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700330
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800331 /* check if there is an existing entry */
332 list_for_each(node, &name_list) {
333 names = node_to_item(node, struct names, node);
Tom Cherry71ba1642019-01-10 10:37:36 -0800334 if (!strcmp(names->name, msg + sizeof(prio)) && (names->id == transp.logMsg.entry.lid) &&
335 (names->prio == *msg)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800336 break;
337 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700338 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800339
340 /* We do not have an existing entry, create and add one */
341 if (node == &name_list) {
342 static const char numbers[] = "0123456789";
343 unsigned long long nl;
344
345 len = strlen(msg + sizeof(prio)) + 1;
Tom Cherry71ba1642019-01-10 10:37:36 -0800346 names = static_cast<struct names*>(calloc(1, sizeof(*names) + len));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800347 if (!names) {
348 ret = -ENOMEM;
349 break;
350 }
351 strcpy(names->name, msg + sizeof(prio));
Tom Cherry71ba1642019-01-10 10:37:36 -0800352 names->id = static_cast<log_id_t>(transp.logMsg.entry.lid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800353 names->prio = *msg;
354 list_init(&names->content);
355 /*
356 * Insert in reverse numeric _then_ alpha sorted order as
357 * representative of log rotation:
358 *
359 * log.10
360 * klog.10
361 * . . .
362 * log.2
363 * klog.2
364 * log.1
365 * klog.1
366 * log
367 * klog
368 *
369 * thus when we present the content, we are provided the oldest
370 * first, which when 'refreshed' could spill off the end of the
371 * pmsg FIFO but retaining the newest data for last with best
372 * chances to survive.
373 */
374 nl = 0;
375 cp = strpbrk(names->name, numbers);
376 if (cp) {
377 nl = strtoull(cp, NULL, 10);
378 }
379 list_for_each_reverse(node, &name_list) {
380 struct names* a_name = node_to_item(node, struct names, node);
381 const char* r = a_name->name;
382 int compare = 0;
383
384 unsigned long long nr = 0;
385 cp = strpbrk(r, numbers);
386 if (cp) {
387 nr = strtoull(cp, NULL, 10);
388 }
389 if (nr != nl) {
390 compare = (nl > nr) ? 1 : -1;
391 }
392 if (compare == 0) {
393 compare = strcmp(names->name, r);
394 }
395 if (compare <= 0) {
396 break;
397 }
398 }
399 list_add_head(node, &names->node);
400 }
401
402 /* Remove any file fragments that match our sequence number */
403 list_for_each_safe(node, n, &names->content) {
404 content = node_to_item(node, struct content, node);
405 if (transp.logMsg.entry.nsec == content->entry.nsec) {
406 list_remove(&content->node);
407 free(content);
408 }
409 }
410
411 /* Add content */
Tom Cherry71ba1642019-01-10 10:37:36 -0800412 content = static_cast<struct content*>(
413 calloc(1, sizeof(content->node) + hdr_size + transp.logMsg.entry.len));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800414 if (!content) {
415 ret = -ENOMEM;
416 break;
417 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800418 memcpy(&content->entry, &transp.logMsg.entry, hdr_size + transp.logMsg.entry.len);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800419
420 /* Insert in sequence number sorted order, to ease reconstruction */
421 list_for_each_reverse(node, &names->content) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800422 if ((node_to_item(node, struct content, node))->entry.nsec < transp.logMsg.entry.nsec) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800423 break;
424 }
425 }
426 list_add_head(node, &content->node);
427 }
428 pmsgClose(&logger_list, &transp);
429
430 /* Progress through all the collected files */
431 list_for_each_safe(node, n, &name_list) {
432 struct listnode *content_node, *m;
433 char* buf;
434 size_t sequence, tag_len;
435
436 names = node_to_item(node, struct names, node);
437
438 /* Construct content into a linear buffer */
439 buf = NULL;
440 len = 0;
441 sequence = 0;
442 tag_len = strlen(names->name) + sizeof(char); /* tag + nul */
443 list_for_each_safe(content_node, m, &names->content) {
444 ssize_t add_len;
445
446 content = node_to_item(content_node, struct content, node);
447 add_len = content->entry.len - tag_len - sizeof(prio);
448 if (add_len <= 0) {
449 list_remove(content_node);
450 free(content);
451 continue;
452 }
453
454 if (!buf) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800455 buf = static_cast<char*>(malloc(sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800456 if (!buf) {
457 ret = -ENOMEM;
458 list_remove(content_node);
459 free(content);
460 continue;
461 }
462 *buf = '\0';
463 }
464
465 /* Missing sequence numbers */
466 while (sequence < content->entry.nsec) {
467 /* plus space for enforced nul */
Tom Cherry71ba1642019-01-10 10:37:36 -0800468 buf = static_cast<char*>(realloc_or_free(buf, len + sizeof(char) + sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800469 if (!buf) {
470 break;
471 }
472 buf[len] = '\f'; /* Mark missing content with a form feed */
473 buf[++len] = '\0';
474 sequence += ANDROID_LOG_PMSG_FILE_SEQUENCE;
475 }
476 if (!buf) {
477 ret = -ENOMEM;
478 list_remove(content_node);
479 free(content);
480 continue;
481 }
482 /* plus space for enforced nul */
Tom Cherry71ba1642019-01-10 10:37:36 -0800483 buf = static_cast<char*>(realloc_or_free(buf, len + add_len + sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800484 if (!buf) {
485 ret = -ENOMEM;
486 list_remove(content_node);
487 free(content);
488 continue;
489 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800490 memcpy(buf + len, (char*)&content->entry + content->entry.hdr_size + tag_len + sizeof(prio),
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800491 add_len);
492 len += add_len;
493 buf[len] = '\0'; /* enforce trailing hidden nul */
494 sequence = content->entry.nsec + ANDROID_LOG_PMSG_FILE_SEQUENCE;
495
496 list_remove(content_node);
497 free(content);
498 }
499 if (buf) {
500 if (len) {
501 /* Buffer contains enforced trailing nul just beyond length */
502 ssize_t r;
503 *strchr(names->name, ':') = '/'; /* Convert back to filename */
504 r = (*fn)(names->id, names->prio, names->name, buf, len, arg);
505 if ((ret >= 0) && (r > 0)) {
506 if (ret == SSIZE_MAX) {
507 ret = r;
508 } else {
509 ret += r;
510 }
511 } else if (r < ret) {
512 ret = r;
513 }
514 }
515 free(buf);
516 }
517 list_remove(node);
518 free(names);
519 }
520 return (ret == SSIZE_MAX) ? -ENOENT : ret;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700521}