blob: eaac97f938e6befdabf64dd981ca939b6c256675 [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
27#include "config_read.h"
28#include "logger.h"
29
30static int pmsgAvailable(log_id_t logId);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080031static int pmsgVersion(struct android_log_logger* logger,
32 struct android_log_transport_context* transp);
33static int pmsgRead(struct android_log_logger_list* logger_list,
Tom Cherry71ba1642019-01-10 10:37:36 -080034 struct android_log_transport_context* transp, struct log_msg* log_msg);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080035static void pmsgClose(struct android_log_logger_list* logger_list,
36 struct android_log_transport_context* transp);
37static int pmsgClear(struct android_log_logger* logger,
38 struct android_log_transport_context* transp);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080039
Tom Cherry2d9779e2019-02-08 11:46:19 -080040struct android_log_transport_read pmsgLoggerRead = {
Tom Cherry71ba1642019-01-10 10:37:36 -080041 .node = {&pmsgLoggerRead.node, &pmsgLoggerRead.node},
42 .name = "pmsg",
43 .available = pmsgAvailable,
44 .version = pmsgVersion,
45 .read = pmsgRead,
46 .poll = NULL,
47 .close = pmsgClose,
48 .clear = pmsgClear,
49 .setSize = NULL,
50 .getSize = NULL,
51 .getReadableSize = NULL,
52 .getPrune = NULL,
53 .setPrune = NULL,
54 .getStats = NULL,
Mark Salyzyn018a96d2016-03-01 13:45:42 -080055};
56
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080057static int pmsgAvailable(log_id_t logId) {
58 if (logId > LOG_ID_SECURITY) {
59 return -EINVAL;
60 }
61 if (access("/dev/pmsg0", W_OK) == 0) {
62 return 0;
63 }
64 return -EBADF;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080065}
66
67/* Determine the credentials of the caller */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080068static bool uid_has_log_permission(uid_t uid) {
Tom Cherry71ba1642019-01-10 10:37:36 -080069 return (uid == AID_SYSTEM) || (uid == AID_LOG) || (uid == AID_ROOT) || (uid == AID_LOGD);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080070}
71
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080072static uid_t get_best_effective_uid() {
73 uid_t euid;
74 uid_t uid;
75 gid_t gid;
76 ssize_t i;
77 static uid_t last_uid = (uid_t)-1;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080078
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080079 if (last_uid != (uid_t)-1) {
80 return last_uid;
81 }
82 uid = __android_log_uid();
83 if (uid_has_log_permission(uid)) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -080084 return last_uid = uid;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080085 }
86 euid = geteuid();
87 if (uid_has_log_permission(euid)) {
88 return last_uid = euid;
89 }
90 gid = getgid();
91 if (uid_has_log_permission(gid)) {
92 return last_uid = gid;
93 }
94 gid = getegid();
95 if (uid_has_log_permission(gid)) {
96 return last_uid = gid;
97 }
98 i = getgroups((size_t)0, NULL);
99 if (i > 0) {
100 gid_t list[i];
101
102 getgroups(i, list);
103 while (--i >= 0) {
104 if (uid_has_log_permission(list[i])) {
105 return last_uid = list[i];
106 }
107 }
108 }
109 return last_uid = uid;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800110}
111
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800112static int pmsgClear(struct android_log_logger* logger __unused,
113 struct android_log_transport_context* transp __unused) {
114 if (uid_has_log_permission(get_best_effective_uid())) {
115 return unlink("/sys/fs/pstore/pmsg-ramoops-0");
116 }
117 errno = EPERM;
118 return -1;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800119}
120
121/*
122 * returns the logger version
123 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800124static int pmsgVersion(struct android_log_logger* logger __unused,
125 struct android_log_transport_context* transp __unused) {
126 return 4;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800127}
128
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800129static int pmsgRead(struct android_log_logger_list* logger_list,
Tom Cherry71ba1642019-01-10 10:37:36 -0800130 struct android_log_transport_context* transp, struct log_msg* log_msg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800131 ssize_t ret;
132 off_t current, next;
133 uid_t uid;
134 struct android_log_logger* logger;
135 struct __attribute__((__packed__)) {
136 android_pmsg_log_header_t p;
137 android_log_header_t l;
138 uint8_t prio;
139 } buf;
140 static uint8_t preread_count;
141 bool is_system;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800142
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800143 memset(log_msg, 0, sizeof(*log_msg));
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800144
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800145 if (atomic_load(&transp->context.fd) <= 0) {
146 int i, fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800147
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800148 if (fd < 0) {
149 return -errno;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800150 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800151 if (fd == 0) { /* Argggg */
152 fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
153 close(0);
154 if (fd < 0) {
155 return -errno;
156 }
157 }
158 i = atomic_exchange(&transp->context.fd, fd);
159 if ((i > 0) && (i != fd)) {
160 close(i);
161 }
162 preread_count = 0;
163 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800164
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800165 while (1) {
166 int fd;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700167
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800168 if (preread_count < sizeof(buf)) {
169 fd = atomic_load(&transp->context.fd);
170 if (fd <= 0) {
171 return -EBADF;
172 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800173 ret = TEMP_FAILURE_RETRY(read(fd, &buf.p.magic + preread_count, sizeof(buf) - preread_count));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800174 if (ret < 0) {
175 return -errno;
176 }
177 preread_count += ret;
178 }
179 if (preread_count != sizeof(buf)) {
180 return preread_count ? -EIO : -EAGAIN;
181 }
182 if ((buf.p.magic != LOGGER_MAGIC) || (buf.p.len <= sizeof(buf)) ||
Tom Cherry71ba1642019-01-10 10:37:36 -0800183 (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) || (buf.l.id >= LOG_ID_MAX) ||
184 (buf.l.realtime.tv_nsec >= NS_PER_SEC) ||
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800185 ((buf.l.id != LOG_ID_EVENTS) && (buf.l.id != LOG_ID_SECURITY) &&
Tom Cherry71ba1642019-01-10 10:37:36 -0800186 ((buf.prio == ANDROID_LOG_UNKNOWN) || (buf.prio == ANDROID_LOG_DEFAULT) ||
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800187 (buf.prio >= ANDROID_LOG_SILENT)))) {
188 do {
189 memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
190 } while (preread_count && (buf.p.magic != LOGGER_MAGIC));
191 continue;
192 }
193 preread_count = 0;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800194
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800195 if ((transp->logMask & (1 << buf.l.id)) &&
196 ((!logger_list->start.tv_sec && !logger_list->start.tv_nsec) ||
197 ((logger_list->start.tv_sec <= buf.l.realtime.tv_sec) &&
198 ((logger_list->start.tv_sec != buf.l.realtime.tv_sec) ||
199 (logger_list->start.tv_nsec <= buf.l.realtime.tv_nsec)))) &&
200 (!logger_list->pid || (logger_list->pid == buf.p.pid))) {
201 uid = get_best_effective_uid();
202 is_system = uid_has_log_permission(uid);
203 if (is_system || (uid == buf.p.uid)) {
204 char* msg = is_system ? log_msg->entry_v4.msg : log_msg->entry_v3.msg;
205 *msg = buf.prio;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700206 fd = atomic_load(&transp->context.fd);
207 if (fd <= 0) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800208 return -EBADF;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700209 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800210 ret = TEMP_FAILURE_RETRY(read(fd, msg + sizeof(buf.prio), buf.p.len - sizeof(buf)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800211 if (ret < 0) {
212 return -errno;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800213 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800214 if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
215 return -EIO;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700216 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800217
218 log_msg->entry_v4.len = buf.p.len - sizeof(buf) + sizeof(buf.prio);
219 log_msg->entry_v4.hdr_size =
220 is_system ? sizeof(log_msg->entry_v4) : sizeof(log_msg->entry_v3);
221 log_msg->entry_v4.pid = buf.p.pid;
222 log_msg->entry_v4.tid = buf.l.tid;
223 log_msg->entry_v4.sec = buf.l.realtime.tv_sec;
224 log_msg->entry_v4.nsec = buf.l.realtime.tv_nsec;
225 log_msg->entry_v4.lid = buf.l.id;
226 if (is_system) {
227 log_msg->entry_v4.uid = buf.p.uid;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800228 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800229
230 return ret + sizeof(buf.prio) + log_msg->entry_v4.hdr_size;
231 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800232 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800233
234 fd = atomic_load(&transp->context.fd);
235 if (fd <= 0) {
236 return -EBADF;
237 }
238 current = TEMP_FAILURE_RETRY(lseek(fd, (off_t)0, SEEK_CUR));
239 if (current < 0) {
240 return -errno;
241 }
242 fd = atomic_load(&transp->context.fd);
243 if (fd <= 0) {
244 return -EBADF;
245 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800246 next = TEMP_FAILURE_RETRY(lseek(fd, (off_t)(buf.p.len - sizeof(buf)), SEEK_CUR));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800247 if (next < 0) {
248 return -errno;
249 }
250 if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
251 return -EIO;
252 }
253 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800254}
255
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800256static void pmsgClose(struct android_log_logger_list* logger_list __unused,
257 struct android_log_transport_context* transp) {
258 int fd = atomic_exchange(&transp->context.fd, 0);
259 if (fd > 0) {
260 close(fd);
261 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800262}
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700263
George Burgess IV559387d2018-10-03 14:10:00 -0700264static void* realloc_or_free(void* ptr, size_t new_size) {
265 void* result = realloc(ptr, new_size);
266 if (!result) {
267 free(ptr);
268 }
269 return result;
270}
271
Tom Cherry2d9779e2019-02-08 11:46:19 -0800272ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* prefix,
273 __android_log_pmsg_file_read_fn fn, void* arg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800274 ssize_t ret;
275 struct android_log_logger_list logger_list;
276 struct android_log_transport_context transp;
277 struct content {
278 struct listnode node;
279 union {
280 struct logger_entry_v4 entry;
281 struct logger_entry_v4 entry_v4;
282 struct logger_entry_v3 entry_v3;
283 struct logger_entry_v2 entry_v2;
284 struct logger_entry entry_v1;
285 };
286 } * content;
287 struct names {
288 struct listnode node;
289 struct listnode content;
290 log_id_t id;
291 char prio;
292 char name[];
293 } * names;
294 struct listnode name_list;
295 struct listnode *node, *n;
296 size_t len, prefix_len;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700297
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800298 if (!fn) {
299 return -EINVAL;
300 }
301
302 /* Add just enough clues in logger_list and transp to make API function */
303 memset(&logger_list, 0, sizeof(logger_list));
304 memset(&transp, 0, sizeof(transp));
305
Tom Cherry71ba1642019-01-10 10:37:36 -0800306 logger_list.mode = ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK | ANDROID_LOG_RDONLY;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800307 transp.logMask = (unsigned)-1;
308 if (logId != LOG_ID_ANY) {
309 transp.logMask = (1 << logId);
310 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800311 transp.logMask &= ~((1 << LOG_ID_KERNEL) | (1 << LOG_ID_EVENTS) | (1 << LOG_ID_SECURITY));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800312 if (!transp.logMask) {
313 return -EINVAL;
314 }
315
316 /* Initialize name list */
317 list_init(&name_list);
318
319 ret = SSIZE_MAX;
320
321 /* Validate incoming prefix, shift until it contains only 0 or 1 : or / */
322 prefix_len = 0;
323 if (prefix) {
324 const char *prev = NULL, *last = NULL, *cp = prefix;
325 while ((cp = strpbrk(cp, "/:"))) {
326 prev = last;
327 last = cp;
328 cp = cp + 1;
329 }
330 if (prev) {
331 prefix = prev + 1;
332 }
333 prefix_len = strlen(prefix);
334 }
335
336 /* Read the file content */
337 while (pmsgRead(&logger_list, &transp, &transp.logMsg) > 0) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800338 const char* cp;
339 size_t hdr_size = transp.logMsg.entry.hdr_size ? transp.logMsg.entry.hdr_size
340 : sizeof(transp.logMsg.entry_v1);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800341 char* msg = (char*)&transp.logMsg + hdr_size;
Tom Cherry71ba1642019-01-10 10:37:36 -0800342 const char* split = NULL;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800343
Tom Cherry71ba1642019-01-10 10:37:36 -0800344 if ((hdr_size < sizeof(transp.logMsg.entry_v1)) || (hdr_size > sizeof(transp.logMsg.entry))) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800345 continue;
346 }
347 /* Check for invalid sequence number */
348 if ((transp.logMsg.entry.nsec % ANDROID_LOG_PMSG_FILE_SEQUENCE) ||
349 ((transp.logMsg.entry.nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
350 ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE)) {
351 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700352 }
353
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800354 /* Determine if it has <dirbase>:<filebase> format for tag */
355 len = transp.logMsg.entry.len - sizeof(prio);
Tom Cherry71ba1642019-01-10 10:37:36 -0800356 for (cp = msg + sizeof(prio); *cp && isprint(*cp) && !isspace(*cp) && --len; ++cp) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800357 if (*cp == ':') {
358 if (split) {
359 break;
360 }
361 split = cp;
362 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700363 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800364 if (*cp || !split) {
365 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700366 }
367
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800368 /* Filters */
369 if (prefix_len && strncmp(msg + sizeof(prio), prefix, prefix_len)) {
370 size_t offset;
371 /*
372 * Allow : to be a synonym for /
373 * Things we do dealing with const char * and do not alloc
374 */
375 split = strchr(prefix, ':');
376 if (split) {
377 continue;
378 }
379 split = strchr(prefix, '/');
380 if (!split) {
381 continue;
382 }
383 offset = split - prefix;
Tom Cherry71ba1642019-01-10 10:37:36 -0800384 if ((msg[offset + sizeof(prio)] != ':') || strncmp(msg + sizeof(prio), prefix, offset)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800385 continue;
386 }
387 ++offset;
Tom Cherry71ba1642019-01-10 10:37:36 -0800388 if ((prefix_len > offset) &&
389 strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800390 continue;
391 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700392 }
393
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800394 if ((prio != ANDROID_LOG_ANY) && (*msg < prio)) {
395 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700396 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700397
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800398 /* check if there is an existing entry */
399 list_for_each(node, &name_list) {
400 names = node_to_item(node, struct names, node);
Tom Cherry71ba1642019-01-10 10:37:36 -0800401 if (!strcmp(names->name, msg + sizeof(prio)) && (names->id == transp.logMsg.entry.lid) &&
402 (names->prio == *msg)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800403 break;
404 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700405 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800406
407 /* We do not have an existing entry, create and add one */
408 if (node == &name_list) {
409 static const char numbers[] = "0123456789";
410 unsigned long long nl;
411
412 len = strlen(msg + sizeof(prio)) + 1;
Tom Cherry71ba1642019-01-10 10:37:36 -0800413 names = static_cast<struct names*>(calloc(1, sizeof(*names) + len));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800414 if (!names) {
415 ret = -ENOMEM;
416 break;
417 }
418 strcpy(names->name, msg + sizeof(prio));
Tom Cherry71ba1642019-01-10 10:37:36 -0800419 names->id = static_cast<log_id_t>(transp.logMsg.entry.lid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800420 names->prio = *msg;
421 list_init(&names->content);
422 /*
423 * Insert in reverse numeric _then_ alpha sorted order as
424 * representative of log rotation:
425 *
426 * log.10
427 * klog.10
428 * . . .
429 * log.2
430 * klog.2
431 * log.1
432 * klog.1
433 * log
434 * klog
435 *
436 * thus when we present the content, we are provided the oldest
437 * first, which when 'refreshed' could spill off the end of the
438 * pmsg FIFO but retaining the newest data for last with best
439 * chances to survive.
440 */
441 nl = 0;
442 cp = strpbrk(names->name, numbers);
443 if (cp) {
444 nl = strtoull(cp, NULL, 10);
445 }
446 list_for_each_reverse(node, &name_list) {
447 struct names* a_name = node_to_item(node, struct names, node);
448 const char* r = a_name->name;
449 int compare = 0;
450
451 unsigned long long nr = 0;
452 cp = strpbrk(r, numbers);
453 if (cp) {
454 nr = strtoull(cp, NULL, 10);
455 }
456 if (nr != nl) {
457 compare = (nl > nr) ? 1 : -1;
458 }
459 if (compare == 0) {
460 compare = strcmp(names->name, r);
461 }
462 if (compare <= 0) {
463 break;
464 }
465 }
466 list_add_head(node, &names->node);
467 }
468
469 /* Remove any file fragments that match our sequence number */
470 list_for_each_safe(node, n, &names->content) {
471 content = node_to_item(node, struct content, node);
472 if (transp.logMsg.entry.nsec == content->entry.nsec) {
473 list_remove(&content->node);
474 free(content);
475 }
476 }
477
478 /* Add content */
Tom Cherry71ba1642019-01-10 10:37:36 -0800479 content = static_cast<struct content*>(
480 calloc(1, sizeof(content->node) + hdr_size + transp.logMsg.entry.len));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800481 if (!content) {
482 ret = -ENOMEM;
483 break;
484 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800485 memcpy(&content->entry, &transp.logMsg.entry, hdr_size + transp.logMsg.entry.len);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800486
487 /* Insert in sequence number sorted order, to ease reconstruction */
488 list_for_each_reverse(node, &names->content) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800489 if ((node_to_item(node, struct content, node))->entry.nsec < transp.logMsg.entry.nsec) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800490 break;
491 }
492 }
493 list_add_head(node, &content->node);
494 }
495 pmsgClose(&logger_list, &transp);
496
497 /* Progress through all the collected files */
498 list_for_each_safe(node, n, &name_list) {
499 struct listnode *content_node, *m;
500 char* buf;
501 size_t sequence, tag_len;
502
503 names = node_to_item(node, struct names, node);
504
505 /* Construct content into a linear buffer */
506 buf = NULL;
507 len = 0;
508 sequence = 0;
509 tag_len = strlen(names->name) + sizeof(char); /* tag + nul */
510 list_for_each_safe(content_node, m, &names->content) {
511 ssize_t add_len;
512
513 content = node_to_item(content_node, struct content, node);
514 add_len = content->entry.len - tag_len - sizeof(prio);
515 if (add_len <= 0) {
516 list_remove(content_node);
517 free(content);
518 continue;
519 }
520
521 if (!buf) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800522 buf = static_cast<char*>(malloc(sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800523 if (!buf) {
524 ret = -ENOMEM;
525 list_remove(content_node);
526 free(content);
527 continue;
528 }
529 *buf = '\0';
530 }
531
532 /* Missing sequence numbers */
533 while (sequence < content->entry.nsec) {
534 /* plus space for enforced nul */
Tom Cherry71ba1642019-01-10 10:37:36 -0800535 buf = static_cast<char*>(realloc_or_free(buf, len + sizeof(char) + sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800536 if (!buf) {
537 break;
538 }
539 buf[len] = '\f'; /* Mark missing content with a form feed */
540 buf[++len] = '\0';
541 sequence += ANDROID_LOG_PMSG_FILE_SEQUENCE;
542 }
543 if (!buf) {
544 ret = -ENOMEM;
545 list_remove(content_node);
546 free(content);
547 continue;
548 }
549 /* plus space for enforced nul */
Tom Cherry71ba1642019-01-10 10:37:36 -0800550 buf = static_cast<char*>(realloc_or_free(buf, len + add_len + sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800551 if (!buf) {
552 ret = -ENOMEM;
553 list_remove(content_node);
554 free(content);
555 continue;
556 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800557 memcpy(buf + len, (char*)&content->entry + content->entry.hdr_size + tag_len + sizeof(prio),
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800558 add_len);
559 len += add_len;
560 buf[len] = '\0'; /* enforce trailing hidden nul */
561 sequence = content->entry.nsec + ANDROID_LOG_PMSG_FILE_SEQUENCE;
562
563 list_remove(content_node);
564 free(content);
565 }
566 if (buf) {
567 if (len) {
568 /* Buffer contains enforced trailing nul just beyond length */
569 ssize_t r;
570 *strchr(names->name, ':') = '/'; /* Convert back to filename */
571 r = (*fn)(names->id, names->prio, names->name, buf, len, arg);
572 if ((ret >= 0) && (r > 0)) {
573 if (ret == SSIZE_MAX) {
574 ret = r;
575 } else {
576 ret += r;
577 }
578 } else if (r < ret) {
579 ret = r;
580 }
581 }
582 free(buf);
583 }
584 list_remove(node);
585 free(names);
586 }
587 return (ret == SSIZE_MAX) ? -ENOENT : ret;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700588}