blob: 9f603e959caff3301f90c86db3d4abd59b7a407f [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);
Tom Cherry828db1a2019-11-14 10:39:40 -080030static int PmsgRead(struct logger_list* logger_list, struct android_log_transport_context* transp,
31 struct log_msg* log_msg);
32static void PmsgClose(struct logger_list* logger_list,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080033 struct android_log_transport_context* transp);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080034
Tom Cherry2d9779e2019-02-08 11:46:19 -080035struct android_log_transport_read pmsgLoggerRead = {
Tom Cherry71ba1642019-01-10 10:37:36 -080036 .name = "pmsg",
Tom Cherry828db1a2019-11-14 10:39:40 -080037 .available = PmsgAvailable,
Tom Cherry828db1a2019-11-14 10:39:40 -080038 .close = PmsgClose,
39 .read = PmsgRead,
Mark Salyzyn018a96d2016-03-01 13:45:42 -080040};
41
Tom Cherry828db1a2019-11-14 10:39:40 -080042static int PmsgAvailable(log_id_t logId) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080043 if (logId > LOG_ID_SECURITY) {
44 return -EINVAL;
45 }
46 if (access("/dev/pmsg0", W_OK) == 0) {
47 return 0;
48 }
49 return -EBADF;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080050}
51
Tom Cherry828db1a2019-11-14 10:39:40 -080052static int PmsgRead(struct logger_list* logger_list, struct android_log_transport_context* transp,
53 struct log_msg* log_msg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080054 ssize_t ret;
55 off_t current, next;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080056 struct __attribute__((__packed__)) {
57 android_pmsg_log_header_t p;
58 android_log_header_t l;
59 uint8_t prio;
60 } buf;
61 static uint8_t preread_count;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080062
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080063 memset(log_msg, 0, sizeof(*log_msg));
Mark Salyzyn018a96d2016-03-01 13:45:42 -080064
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080065 if (atomic_load(&transp->context.fd) <= 0) {
66 int i, fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080067
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080068 if (fd < 0) {
69 return -errno;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080070 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080071 if (fd == 0) { /* Argggg */
72 fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
73 close(0);
74 if (fd < 0) {
75 return -errno;
76 }
77 }
78 i = atomic_exchange(&transp->context.fd, fd);
79 if ((i > 0) && (i != fd)) {
80 close(i);
81 }
82 preread_count = 0;
83 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -080084
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080085 while (1) {
86 int fd;
Mark Salyzyndb8a2662016-10-10 07:27:42 -070087
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080088 if (preread_count < sizeof(buf)) {
89 fd = atomic_load(&transp->context.fd);
90 if (fd <= 0) {
91 return -EBADF;
92 }
Tom Cherry71ba1642019-01-10 10:37:36 -080093 ret = TEMP_FAILURE_RETRY(read(fd, &buf.p.magic + preread_count, sizeof(buf) - preread_count));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080094 if (ret < 0) {
95 return -errno;
96 }
97 preread_count += ret;
98 }
99 if (preread_count != sizeof(buf)) {
100 return preread_count ? -EIO : -EAGAIN;
101 }
102 if ((buf.p.magic != LOGGER_MAGIC) || (buf.p.len <= sizeof(buf)) ||
Tom Cherry71ba1642019-01-10 10:37:36 -0800103 (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) || (buf.l.id >= LOG_ID_MAX) ||
104 (buf.l.realtime.tv_nsec >= NS_PER_SEC) ||
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800105 ((buf.l.id != LOG_ID_EVENTS) && (buf.l.id != LOG_ID_SECURITY) &&
Tom Cherry71ba1642019-01-10 10:37:36 -0800106 ((buf.prio == ANDROID_LOG_UNKNOWN) || (buf.prio == ANDROID_LOG_DEFAULT) ||
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800107 (buf.prio >= ANDROID_LOG_SILENT)))) {
108 do {
109 memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
110 } while (preread_count && (buf.p.magic != LOGGER_MAGIC));
111 continue;
112 }
113 preread_count = 0;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800114
Tom Cherry9156c532019-11-14 08:56:39 -0800115 if ((logger_list->log_mask & (1 << buf.l.id)) &&
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800116 ((!logger_list->start.tv_sec && !logger_list->start.tv_nsec) ||
117 ((logger_list->start.tv_sec <= buf.l.realtime.tv_sec) &&
118 ((logger_list->start.tv_sec != buf.l.realtime.tv_sec) ||
119 (logger_list->start.tv_nsec <= buf.l.realtime.tv_nsec)))) &&
120 (!logger_list->pid || (logger_list->pid == buf.p.pid))) {
Tom Cherry441054a2019-10-15 16:53:11 -0700121 char* msg = log_msg->entry.msg;
Tom Cherry1e59dcc2019-10-15 16:06:06 -0700122 *msg = buf.prio;
123 fd = atomic_load(&transp->context.fd);
124 if (fd <= 0) {
125 return -EBADF;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800126 }
Tom Cherry1e59dcc2019-10-15 16:06:06 -0700127 ret = TEMP_FAILURE_RETRY(read(fd, msg + sizeof(buf.prio), buf.p.len - sizeof(buf)));
128 if (ret < 0) {
129 return -errno;
130 }
131 if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
132 return -EIO;
133 }
134
Tom Cherry441054a2019-10-15 16:53:11 -0700135 log_msg->entry.len = buf.p.len - sizeof(buf) + sizeof(buf.prio);
136 log_msg->entry.hdr_size = sizeof(log_msg->entry);
137 log_msg->entry.pid = buf.p.pid;
138 log_msg->entry.tid = buf.l.tid;
139 log_msg->entry.sec = buf.l.realtime.tv_sec;
140 log_msg->entry.nsec = buf.l.realtime.tv_nsec;
141 log_msg->entry.lid = buf.l.id;
142 log_msg->entry.uid = buf.p.uid;
Tom Cherry1e59dcc2019-10-15 16:06:06 -0700143
Tom Cherry441054a2019-10-15 16:53:11 -0700144 return ret + sizeof(buf.prio) + log_msg->entry.hdr_size;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800145 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800146
147 fd = atomic_load(&transp->context.fd);
148 if (fd <= 0) {
149 return -EBADF;
150 }
151 current = TEMP_FAILURE_RETRY(lseek(fd, (off_t)0, SEEK_CUR));
152 if (current < 0) {
153 return -errno;
154 }
155 fd = atomic_load(&transp->context.fd);
156 if (fd <= 0) {
157 return -EBADF;
158 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800159 next = TEMP_FAILURE_RETRY(lseek(fd, (off_t)(buf.p.len - sizeof(buf)), SEEK_CUR));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800160 if (next < 0) {
161 return -errno;
162 }
163 if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
164 return -EIO;
165 }
166 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800167}
168
Tom Cherry828db1a2019-11-14 10:39:40 -0800169static void PmsgClose(struct logger_list*, struct android_log_transport_context* transp) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800170 int fd = atomic_exchange(&transp->context.fd, 0);
171 if (fd > 0) {
172 close(fd);
173 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800174}
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700175
George Burgess IV559387d2018-10-03 14:10:00 -0700176static void* realloc_or_free(void* ptr, size_t new_size) {
177 void* result = realloc(ptr, new_size);
178 if (!result) {
179 free(ptr);
180 }
181 return result;
182}
183
Tom Cherry2d9779e2019-02-08 11:46:19 -0800184ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* prefix,
185 __android_log_pmsg_file_read_fn fn, void* arg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800186 ssize_t ret;
Tom Cherry828db1a2019-11-14 10:39:40 -0800187 struct logger_list logger_list;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800188 struct android_log_transport_context transp;
189 struct content {
190 struct listnode node;
Tom Cherry441054a2019-10-15 16:53:11 -0700191 struct logger_entry entry;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800192 } * content;
193 struct names {
194 struct listnode node;
195 struct listnode content;
196 log_id_t id;
197 char prio;
198 char name[];
199 } * names;
200 struct listnode name_list;
201 struct listnode *node, *n;
202 size_t len, prefix_len;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700203
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800204 if (!fn) {
205 return -EINVAL;
206 }
207
208 /* Add just enough clues in logger_list and transp to make API function */
209 memset(&logger_list, 0, sizeof(logger_list));
210 memset(&transp, 0, sizeof(transp));
211
Tom Cherry71ba1642019-01-10 10:37:36 -0800212 logger_list.mode = ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK | ANDROID_LOG_RDONLY;
Tom Cherry9156c532019-11-14 08:56:39 -0800213 logger_list.log_mask = (unsigned)-1;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800214 if (logId != LOG_ID_ANY) {
Tom Cherry9156c532019-11-14 08:56:39 -0800215 logger_list.log_mask = (1 << logId);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800216 }
Tom Cherry9156c532019-11-14 08:56:39 -0800217 logger_list.log_mask &= ~((1 << LOG_ID_KERNEL) | (1 << LOG_ID_EVENTS) | (1 << LOG_ID_SECURITY));
218 if (!logger_list.log_mask) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800219 return -EINVAL;
220 }
221
222 /* Initialize name list */
223 list_init(&name_list);
224
225 ret = SSIZE_MAX;
226
227 /* Validate incoming prefix, shift until it contains only 0 or 1 : or / */
228 prefix_len = 0;
229 if (prefix) {
230 const char *prev = NULL, *last = NULL, *cp = prefix;
231 while ((cp = strpbrk(cp, "/:"))) {
232 prev = last;
233 last = cp;
234 cp = cp + 1;
235 }
236 if (prev) {
237 prefix = prev + 1;
238 }
239 prefix_len = strlen(prefix);
240 }
241
242 /* Read the file content */
Tom Cherrye05b7842019-10-16 10:16:44 -0700243 log_msg log_msg;
Tom Cherry828db1a2019-11-14 10:39:40 -0800244 while (PmsgRead(&logger_list, &transp, &log_msg) > 0) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800245 const char* cp;
Tom Cherrye05b7842019-10-16 10:16:44 -0700246 size_t hdr_size = log_msg.entry.hdr_size;
Tom Cherry441054a2019-10-15 16:53:11 -0700247
Tom Cherrye05b7842019-10-16 10:16:44 -0700248 char* msg = (char*)&log_msg + hdr_size;
Tom Cherry71ba1642019-01-10 10:37:36 -0800249 const char* split = NULL;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800250
Tom Cherrye05b7842019-10-16 10:16:44 -0700251 if (hdr_size != sizeof(log_msg.entry)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800252 continue;
253 }
254 /* Check for invalid sequence number */
Tom Cherrye05b7842019-10-16 10:16:44 -0700255 if (log_msg.entry.nsec % ANDROID_LOG_PMSG_FILE_SEQUENCE ||
256 (log_msg.entry.nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
257 ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800258 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700259 }
260
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800261 /* Determine if it has <dirbase>:<filebase> format for tag */
Tom Cherrye05b7842019-10-16 10:16:44 -0700262 len = log_msg.entry.len - sizeof(prio);
Tom Cherry71ba1642019-01-10 10:37:36 -0800263 for (cp = msg + sizeof(prio); *cp && isprint(*cp) && !isspace(*cp) && --len; ++cp) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800264 if (*cp == ':') {
265 if (split) {
266 break;
267 }
268 split = cp;
269 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700270 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800271 if (*cp || !split) {
272 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700273 }
274
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800275 /* Filters */
276 if (prefix_len && strncmp(msg + sizeof(prio), prefix, prefix_len)) {
277 size_t offset;
278 /*
279 * Allow : to be a synonym for /
280 * Things we do dealing with const char * and do not alloc
281 */
282 split = strchr(prefix, ':');
283 if (split) {
284 continue;
285 }
286 split = strchr(prefix, '/');
287 if (!split) {
288 continue;
289 }
290 offset = split - prefix;
Tom Cherry71ba1642019-01-10 10:37:36 -0800291 if ((msg[offset + sizeof(prio)] != ':') || strncmp(msg + sizeof(prio), prefix, offset)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800292 continue;
293 }
294 ++offset;
Tom Cherry71ba1642019-01-10 10:37:36 -0800295 if ((prefix_len > offset) &&
296 strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800297 continue;
298 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700299 }
300
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800301 if ((prio != ANDROID_LOG_ANY) && (*msg < prio)) {
302 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700303 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700304
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800305 /* check if there is an existing entry */
306 list_for_each(node, &name_list) {
307 names = node_to_item(node, struct names, node);
Tom Cherrye05b7842019-10-16 10:16:44 -0700308 if (!strcmp(names->name, msg + sizeof(prio)) && names->id == log_msg.entry.lid &&
309 names->prio == *msg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800310 break;
311 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700312 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800313
314 /* We do not have an existing entry, create and add one */
315 if (node == &name_list) {
316 static const char numbers[] = "0123456789";
317 unsigned long long nl;
318
319 len = strlen(msg + sizeof(prio)) + 1;
Tom Cherry71ba1642019-01-10 10:37:36 -0800320 names = static_cast<struct names*>(calloc(1, sizeof(*names) + len));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800321 if (!names) {
322 ret = -ENOMEM;
323 break;
324 }
325 strcpy(names->name, msg + sizeof(prio));
Tom Cherrye05b7842019-10-16 10:16:44 -0700326 names->id = static_cast<log_id_t>(log_msg.entry.lid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800327 names->prio = *msg;
328 list_init(&names->content);
329 /*
330 * Insert in reverse numeric _then_ alpha sorted order as
331 * representative of log rotation:
332 *
333 * log.10
334 * klog.10
335 * . . .
336 * log.2
337 * klog.2
338 * log.1
339 * klog.1
340 * log
341 * klog
342 *
343 * thus when we present the content, we are provided the oldest
344 * first, which when 'refreshed' could spill off the end of the
345 * pmsg FIFO but retaining the newest data for last with best
346 * chances to survive.
347 */
348 nl = 0;
349 cp = strpbrk(names->name, numbers);
350 if (cp) {
351 nl = strtoull(cp, NULL, 10);
352 }
353 list_for_each_reverse(node, &name_list) {
354 struct names* a_name = node_to_item(node, struct names, node);
355 const char* r = a_name->name;
356 int compare = 0;
357
358 unsigned long long nr = 0;
359 cp = strpbrk(r, numbers);
360 if (cp) {
361 nr = strtoull(cp, NULL, 10);
362 }
363 if (nr != nl) {
364 compare = (nl > nr) ? 1 : -1;
365 }
366 if (compare == 0) {
367 compare = strcmp(names->name, r);
368 }
369 if (compare <= 0) {
370 break;
371 }
372 }
373 list_add_head(node, &names->node);
374 }
375
376 /* Remove any file fragments that match our sequence number */
377 list_for_each_safe(node, n, &names->content) {
378 content = node_to_item(node, struct content, node);
Tom Cherrye05b7842019-10-16 10:16:44 -0700379 if (log_msg.entry.nsec == content->entry.nsec) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800380 list_remove(&content->node);
381 free(content);
382 }
383 }
384
385 /* Add content */
Tom Cherry71ba1642019-01-10 10:37:36 -0800386 content = static_cast<struct content*>(
Tom Cherrye05b7842019-10-16 10:16:44 -0700387 calloc(1, sizeof(content->node) + hdr_size + log_msg.entry.len));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800388 if (!content) {
389 ret = -ENOMEM;
390 break;
391 }
Tom Cherrye05b7842019-10-16 10:16:44 -0700392 memcpy(&content->entry, &log_msg.entry, hdr_size + log_msg.entry.len);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800393
394 /* Insert in sequence number sorted order, to ease reconstruction */
395 list_for_each_reverse(node, &names->content) {
Tom Cherrye05b7842019-10-16 10:16:44 -0700396 if ((node_to_item(node, struct content, node))->entry.nsec < log_msg.entry.nsec) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800397 break;
398 }
399 }
400 list_add_head(node, &content->node);
401 }
Tom Cherry828db1a2019-11-14 10:39:40 -0800402 PmsgClose(&logger_list, &transp);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800403
404 /* Progress through all the collected files */
405 list_for_each_safe(node, n, &name_list) {
406 struct listnode *content_node, *m;
407 char* buf;
408 size_t sequence, tag_len;
409
410 names = node_to_item(node, struct names, node);
411
412 /* Construct content into a linear buffer */
413 buf = NULL;
414 len = 0;
415 sequence = 0;
416 tag_len = strlen(names->name) + sizeof(char); /* tag + nul */
417 list_for_each_safe(content_node, m, &names->content) {
418 ssize_t add_len;
419
420 content = node_to_item(content_node, struct content, node);
421 add_len = content->entry.len - tag_len - sizeof(prio);
422 if (add_len <= 0) {
423 list_remove(content_node);
424 free(content);
425 continue;
426 }
427
428 if (!buf) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800429 buf = static_cast<char*>(malloc(sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800430 if (!buf) {
431 ret = -ENOMEM;
432 list_remove(content_node);
433 free(content);
434 continue;
435 }
436 *buf = '\0';
437 }
438
439 /* Missing sequence numbers */
440 while (sequence < content->entry.nsec) {
441 /* plus space for enforced nul */
Tom Cherry71ba1642019-01-10 10:37:36 -0800442 buf = static_cast<char*>(realloc_or_free(buf, len + sizeof(char) + sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800443 if (!buf) {
444 break;
445 }
446 buf[len] = '\f'; /* Mark missing content with a form feed */
447 buf[++len] = '\0';
448 sequence += ANDROID_LOG_PMSG_FILE_SEQUENCE;
449 }
450 if (!buf) {
451 ret = -ENOMEM;
452 list_remove(content_node);
453 free(content);
454 continue;
455 }
456 /* plus space for enforced nul */
Tom Cherry71ba1642019-01-10 10:37:36 -0800457 buf = static_cast<char*>(realloc_or_free(buf, len + add_len + sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800458 if (!buf) {
459 ret = -ENOMEM;
460 list_remove(content_node);
461 free(content);
462 continue;
463 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800464 memcpy(buf + len, (char*)&content->entry + content->entry.hdr_size + tag_len + sizeof(prio),
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800465 add_len);
466 len += add_len;
467 buf[len] = '\0'; /* enforce trailing hidden nul */
468 sequence = content->entry.nsec + ANDROID_LOG_PMSG_FILE_SEQUENCE;
469
470 list_remove(content_node);
471 free(content);
472 }
473 if (buf) {
474 if (len) {
475 /* Buffer contains enforced trailing nul just beyond length */
476 ssize_t r;
477 *strchr(names->name, ':') = '/'; /* Convert back to filename */
478 r = (*fn)(names->id, names->prio, names->name, buf, len, arg);
479 if ((ret >= 0) && (r > 0)) {
480 if (ret == SSIZE_MAX) {
481 ret = r;
482 } else {
483 ret += r;
484 }
485 } else if (r < ret) {
486 ret = r;
487 }
488 }
489 free(buf);
490 }
491 list_remove(node);
492 free(names);
493 }
494 return (ret == SSIZE_MAX) ? -ENOENT : ret;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700495}