blob: f43ce3ac11cbd7f78e09309a4666ac18692df02b [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 Cherry441054a2019-10-15 16:53:11 -0700147 char* msg = log_msg->entry.msg;
Tom Cherry1e59dcc2019-10-15 16:06:06 -0700148 *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
Tom Cherry441054a2019-10-15 16:53:11 -0700161 log_msg->entry.len = buf.p.len - sizeof(buf) + sizeof(buf.prio);
162 log_msg->entry.hdr_size = sizeof(log_msg->entry);
163 log_msg->entry.pid = buf.p.pid;
164 log_msg->entry.tid = buf.l.tid;
165 log_msg->entry.sec = buf.l.realtime.tv_sec;
166 log_msg->entry.nsec = buf.l.realtime.tv_nsec;
167 log_msg->entry.lid = buf.l.id;
168 log_msg->entry.uid = buf.p.uid;
Tom Cherry1e59dcc2019-10-15 16:06:06 -0700169
Tom Cherry441054a2019-10-15 16:53:11 -0700170 return ret + sizeof(buf.prio) + log_msg->entry.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 Cherry441054a2019-10-15 16:53:11 -0700218 struct logger_entry 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 */
Tom Cherrye05b7842019-10-16 10:16:44 -0700270 log_msg log_msg;
271 while (pmsgRead(&logger_list, &transp, &log_msg) > 0) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800272 const char* cp;
Tom Cherrye05b7842019-10-16 10:16:44 -0700273 size_t hdr_size = log_msg.entry.hdr_size;
Tom Cherry441054a2019-10-15 16:53:11 -0700274
Tom Cherrye05b7842019-10-16 10:16:44 -0700275 char* msg = (char*)&log_msg + hdr_size;
Tom Cherry71ba1642019-01-10 10:37:36 -0800276 const char* split = NULL;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800277
Tom Cherrye05b7842019-10-16 10:16:44 -0700278 if (hdr_size != sizeof(log_msg.entry)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800279 continue;
280 }
281 /* Check for invalid sequence number */
Tom Cherrye05b7842019-10-16 10:16:44 -0700282 if (log_msg.entry.nsec % ANDROID_LOG_PMSG_FILE_SEQUENCE ||
283 (log_msg.entry.nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
284 ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800285 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700286 }
287
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800288 /* Determine if it has <dirbase>:<filebase> format for tag */
Tom Cherrye05b7842019-10-16 10:16:44 -0700289 len = log_msg.entry.len - sizeof(prio);
Tom Cherry71ba1642019-01-10 10:37:36 -0800290 for (cp = msg + sizeof(prio); *cp && isprint(*cp) && !isspace(*cp) && --len; ++cp) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800291 if (*cp == ':') {
292 if (split) {
293 break;
294 }
295 split = cp;
296 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700297 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800298 if (*cp || !split) {
299 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700300 }
301
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800302 /* Filters */
303 if (prefix_len && strncmp(msg + sizeof(prio), prefix, prefix_len)) {
304 size_t offset;
305 /*
306 * Allow : to be a synonym for /
307 * Things we do dealing with const char * and do not alloc
308 */
309 split = strchr(prefix, ':');
310 if (split) {
311 continue;
312 }
313 split = strchr(prefix, '/');
314 if (!split) {
315 continue;
316 }
317 offset = split - prefix;
Tom Cherry71ba1642019-01-10 10:37:36 -0800318 if ((msg[offset + sizeof(prio)] != ':') || strncmp(msg + sizeof(prio), prefix, offset)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800319 continue;
320 }
321 ++offset;
Tom Cherry71ba1642019-01-10 10:37:36 -0800322 if ((prefix_len > offset) &&
323 strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800324 continue;
325 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700326 }
327
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800328 if ((prio != ANDROID_LOG_ANY) && (*msg < prio)) {
329 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700330 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700331
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800332 /* check if there is an existing entry */
333 list_for_each(node, &name_list) {
334 names = node_to_item(node, struct names, node);
Tom Cherrye05b7842019-10-16 10:16:44 -0700335 if (!strcmp(names->name, msg + sizeof(prio)) && names->id == log_msg.entry.lid &&
336 names->prio == *msg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800337 break;
338 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700339 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800340
341 /* We do not have an existing entry, create and add one */
342 if (node == &name_list) {
343 static const char numbers[] = "0123456789";
344 unsigned long long nl;
345
346 len = strlen(msg + sizeof(prio)) + 1;
Tom Cherry71ba1642019-01-10 10:37:36 -0800347 names = static_cast<struct names*>(calloc(1, sizeof(*names) + len));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800348 if (!names) {
349 ret = -ENOMEM;
350 break;
351 }
352 strcpy(names->name, msg + sizeof(prio));
Tom Cherrye05b7842019-10-16 10:16:44 -0700353 names->id = static_cast<log_id_t>(log_msg.entry.lid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800354 names->prio = *msg;
355 list_init(&names->content);
356 /*
357 * Insert in reverse numeric _then_ alpha sorted order as
358 * representative of log rotation:
359 *
360 * log.10
361 * klog.10
362 * . . .
363 * log.2
364 * klog.2
365 * log.1
366 * klog.1
367 * log
368 * klog
369 *
370 * thus when we present the content, we are provided the oldest
371 * first, which when 'refreshed' could spill off the end of the
372 * pmsg FIFO but retaining the newest data for last with best
373 * chances to survive.
374 */
375 nl = 0;
376 cp = strpbrk(names->name, numbers);
377 if (cp) {
378 nl = strtoull(cp, NULL, 10);
379 }
380 list_for_each_reverse(node, &name_list) {
381 struct names* a_name = node_to_item(node, struct names, node);
382 const char* r = a_name->name;
383 int compare = 0;
384
385 unsigned long long nr = 0;
386 cp = strpbrk(r, numbers);
387 if (cp) {
388 nr = strtoull(cp, NULL, 10);
389 }
390 if (nr != nl) {
391 compare = (nl > nr) ? 1 : -1;
392 }
393 if (compare == 0) {
394 compare = strcmp(names->name, r);
395 }
396 if (compare <= 0) {
397 break;
398 }
399 }
400 list_add_head(node, &names->node);
401 }
402
403 /* Remove any file fragments that match our sequence number */
404 list_for_each_safe(node, n, &names->content) {
405 content = node_to_item(node, struct content, node);
Tom Cherrye05b7842019-10-16 10:16:44 -0700406 if (log_msg.entry.nsec == content->entry.nsec) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800407 list_remove(&content->node);
408 free(content);
409 }
410 }
411
412 /* Add content */
Tom Cherry71ba1642019-01-10 10:37:36 -0800413 content = static_cast<struct content*>(
Tom Cherrye05b7842019-10-16 10:16:44 -0700414 calloc(1, sizeof(content->node) + hdr_size + log_msg.entry.len));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800415 if (!content) {
416 ret = -ENOMEM;
417 break;
418 }
Tom Cherrye05b7842019-10-16 10:16:44 -0700419 memcpy(&content->entry, &log_msg.entry, hdr_size + log_msg.entry.len);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800420
421 /* Insert in sequence number sorted order, to ease reconstruction */
422 list_for_each_reverse(node, &names->content) {
Tom Cherrye05b7842019-10-16 10:16:44 -0700423 if ((node_to_item(node, struct content, node))->entry.nsec < log_msg.entry.nsec) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800424 break;
425 }
426 }
427 list_add_head(node, &content->node);
428 }
429 pmsgClose(&logger_list, &transp);
430
431 /* Progress through all the collected files */
432 list_for_each_safe(node, n, &name_list) {
433 struct listnode *content_node, *m;
434 char* buf;
435 size_t sequence, tag_len;
436
437 names = node_to_item(node, struct names, node);
438
439 /* Construct content into a linear buffer */
440 buf = NULL;
441 len = 0;
442 sequence = 0;
443 tag_len = strlen(names->name) + sizeof(char); /* tag + nul */
444 list_for_each_safe(content_node, m, &names->content) {
445 ssize_t add_len;
446
447 content = node_to_item(content_node, struct content, node);
448 add_len = content->entry.len - tag_len - sizeof(prio);
449 if (add_len <= 0) {
450 list_remove(content_node);
451 free(content);
452 continue;
453 }
454
455 if (!buf) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800456 buf = static_cast<char*>(malloc(sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800457 if (!buf) {
458 ret = -ENOMEM;
459 list_remove(content_node);
460 free(content);
461 continue;
462 }
463 *buf = '\0';
464 }
465
466 /* Missing sequence numbers */
467 while (sequence < content->entry.nsec) {
468 /* plus space for enforced nul */
Tom Cherry71ba1642019-01-10 10:37:36 -0800469 buf = static_cast<char*>(realloc_or_free(buf, len + sizeof(char) + sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800470 if (!buf) {
471 break;
472 }
473 buf[len] = '\f'; /* Mark missing content with a form feed */
474 buf[++len] = '\0';
475 sequence += ANDROID_LOG_PMSG_FILE_SEQUENCE;
476 }
477 if (!buf) {
478 ret = -ENOMEM;
479 list_remove(content_node);
480 free(content);
481 continue;
482 }
483 /* plus space for enforced nul */
Tom Cherry71ba1642019-01-10 10:37:36 -0800484 buf = static_cast<char*>(realloc_or_free(buf, len + add_len + sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800485 if (!buf) {
486 ret = -ENOMEM;
487 list_remove(content_node);
488 free(content);
489 continue;
490 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800491 memcpy(buf + len, (char*)&content->entry + content->entry.hdr_size + tag_len + sizeof(prio),
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800492 add_len);
493 len += add_len;
494 buf[len] = '\0'; /* enforce trailing hidden nul */
495 sequence = content->entry.nsec + ANDROID_LOG_PMSG_FILE_SEQUENCE;
496
497 list_remove(content_node);
498 free(content);
499 }
500 if (buf) {
501 if (len) {
502 /* Buffer contains enforced trailing nul just beyond length */
503 ssize_t r;
504 *strchr(names->name, ':') = '/'; /* Convert back to filename */
505 r = (*fn)(names->id, names->prio, names->name, buf, len, arg);
506 if ((ret >= 0) && (r > 0)) {
507 if (ret == SSIZE_MAX) {
508 ret = r;
509 } else {
510 ret += r;
511 }
512 } else if (r < ret) {
513 ret = r;
514 }
515 }
516 free(buf);
517 }
518 list_remove(node);
519 free(names);
520 }
521 return (ret == SSIZE_MAX) ? -ENOENT : ret;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700522}