blob: 005fec8027261127ca637ea1d633cd9be39905fd [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 .node = {&pmsgLoggerRead.node, &pmsgLoggerRead.node},
41 .name = "pmsg",
42 .available = pmsgAvailable,
43 .version = pmsgVersion,
44 .read = pmsgRead,
45 .poll = NULL,
46 .close = pmsgClose,
47 .clear = pmsgClear,
48 .setSize = NULL,
49 .getSize = NULL,
50 .getReadableSize = NULL,
51 .getPrune = NULL,
52 .setPrune = NULL,
53 .getStats = NULL,
Mark Salyzyn018a96d2016-03-01 13:45:42 -080054};
55
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080056static int pmsgAvailable(log_id_t logId) {
57 if (logId > LOG_ID_SECURITY) {
58 return -EINVAL;
59 }
60 if (access("/dev/pmsg0", W_OK) == 0) {
61 return 0;
62 }
63 return -EBADF;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080064}
65
66/* Determine the credentials of the caller */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080067static bool uid_has_log_permission(uid_t uid) {
Tom Cherry71ba1642019-01-10 10:37:36 -080068 return (uid == AID_SYSTEM) || (uid == AID_LOG) || (uid == AID_ROOT) || (uid == AID_LOGD);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080069}
70
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080071static uid_t get_best_effective_uid() {
72 uid_t euid;
73 uid_t uid;
74 gid_t gid;
75 ssize_t i;
76 static uid_t last_uid = (uid_t)-1;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080077
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080078 if (last_uid != (uid_t)-1) {
79 return last_uid;
80 }
81 uid = __android_log_uid();
82 if (uid_has_log_permission(uid)) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -080083 return last_uid = uid;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080084 }
85 euid = geteuid();
86 if (uid_has_log_permission(euid)) {
87 return last_uid = euid;
88 }
89 gid = getgid();
90 if (uid_has_log_permission(gid)) {
91 return last_uid = gid;
92 }
93 gid = getegid();
94 if (uid_has_log_permission(gid)) {
95 return last_uid = gid;
96 }
97 i = getgroups((size_t)0, NULL);
98 if (i > 0) {
99 gid_t list[i];
100
101 getgroups(i, list);
102 while (--i >= 0) {
103 if (uid_has_log_permission(list[i])) {
104 return last_uid = list[i];
105 }
106 }
107 }
108 return last_uid = uid;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800109}
110
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800111static int pmsgClear(struct android_log_logger* logger __unused,
112 struct android_log_transport_context* transp __unused) {
113 if (uid_has_log_permission(get_best_effective_uid())) {
114 return unlink("/sys/fs/pstore/pmsg-ramoops-0");
115 }
116 errno = EPERM;
117 return -1;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800118}
119
120/*
121 * returns the logger version
122 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800123static int pmsgVersion(struct android_log_logger* logger __unused,
124 struct android_log_transport_context* transp __unused) {
125 return 4;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800126}
127
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800128static int pmsgRead(struct android_log_logger_list* logger_list,
Tom Cherry71ba1642019-01-10 10:37:36 -0800129 struct android_log_transport_context* transp, struct log_msg* log_msg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800130 ssize_t ret;
131 off_t current, next;
132 uid_t uid;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800133 struct __attribute__((__packed__)) {
134 android_pmsg_log_header_t p;
135 android_log_header_t l;
136 uint8_t prio;
137 } buf;
138 static uint8_t preread_count;
139 bool is_system;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800140
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800141 memset(log_msg, 0, sizeof(*log_msg));
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800142
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800143 if (atomic_load(&transp->context.fd) <= 0) {
144 int i, fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800145
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800146 if (fd < 0) {
147 return -errno;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800148 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800149 if (fd == 0) { /* Argggg */
150 fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
151 close(0);
152 if (fd < 0) {
153 return -errno;
154 }
155 }
156 i = atomic_exchange(&transp->context.fd, fd);
157 if ((i > 0) && (i != fd)) {
158 close(i);
159 }
160 preread_count = 0;
161 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800162
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800163 while (1) {
164 int fd;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700165
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800166 if (preread_count < sizeof(buf)) {
167 fd = atomic_load(&transp->context.fd);
168 if (fd <= 0) {
169 return -EBADF;
170 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800171 ret = TEMP_FAILURE_RETRY(read(fd, &buf.p.magic + preread_count, sizeof(buf) - preread_count));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800172 if (ret < 0) {
173 return -errno;
174 }
175 preread_count += ret;
176 }
177 if (preread_count != sizeof(buf)) {
178 return preread_count ? -EIO : -EAGAIN;
179 }
180 if ((buf.p.magic != LOGGER_MAGIC) || (buf.p.len <= sizeof(buf)) ||
Tom Cherry71ba1642019-01-10 10:37:36 -0800181 (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) || (buf.l.id >= LOG_ID_MAX) ||
182 (buf.l.realtime.tv_nsec >= NS_PER_SEC) ||
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800183 ((buf.l.id != LOG_ID_EVENTS) && (buf.l.id != LOG_ID_SECURITY) &&
Tom Cherry71ba1642019-01-10 10:37:36 -0800184 ((buf.prio == ANDROID_LOG_UNKNOWN) || (buf.prio == ANDROID_LOG_DEFAULT) ||
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800185 (buf.prio >= ANDROID_LOG_SILENT)))) {
186 do {
187 memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
188 } while (preread_count && (buf.p.magic != LOGGER_MAGIC));
189 continue;
190 }
191 preread_count = 0;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800192
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800193 if ((transp->logMask & (1 << buf.l.id)) &&
194 ((!logger_list->start.tv_sec && !logger_list->start.tv_nsec) ||
195 ((logger_list->start.tv_sec <= buf.l.realtime.tv_sec) &&
196 ((logger_list->start.tv_sec != buf.l.realtime.tv_sec) ||
197 (logger_list->start.tv_nsec <= buf.l.realtime.tv_nsec)))) &&
198 (!logger_list->pid || (logger_list->pid == buf.p.pid))) {
199 uid = get_best_effective_uid();
200 is_system = uid_has_log_permission(uid);
201 if (is_system || (uid == buf.p.uid)) {
202 char* msg = is_system ? log_msg->entry_v4.msg : log_msg->entry_v3.msg;
203 *msg = buf.prio;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700204 fd = atomic_load(&transp->context.fd);
205 if (fd <= 0) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800206 return -EBADF;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700207 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800208 ret = TEMP_FAILURE_RETRY(read(fd, msg + sizeof(buf.prio), buf.p.len - sizeof(buf)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800209 if (ret < 0) {
210 return -errno;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800211 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800212 if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
213 return -EIO;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700214 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800215
216 log_msg->entry_v4.len = buf.p.len - sizeof(buf) + sizeof(buf.prio);
217 log_msg->entry_v4.hdr_size =
218 is_system ? sizeof(log_msg->entry_v4) : sizeof(log_msg->entry_v3);
219 log_msg->entry_v4.pid = buf.p.pid;
220 log_msg->entry_v4.tid = buf.l.tid;
221 log_msg->entry_v4.sec = buf.l.realtime.tv_sec;
222 log_msg->entry_v4.nsec = buf.l.realtime.tv_nsec;
223 log_msg->entry_v4.lid = buf.l.id;
224 if (is_system) {
225 log_msg->entry_v4.uid = buf.p.uid;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800226 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800227
228 return ret + sizeof(buf.prio) + log_msg->entry_v4.hdr_size;
229 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800230 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800231
232 fd = atomic_load(&transp->context.fd);
233 if (fd <= 0) {
234 return -EBADF;
235 }
236 current = TEMP_FAILURE_RETRY(lseek(fd, (off_t)0, SEEK_CUR));
237 if (current < 0) {
238 return -errno;
239 }
240 fd = atomic_load(&transp->context.fd);
241 if (fd <= 0) {
242 return -EBADF;
243 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800244 next = TEMP_FAILURE_RETRY(lseek(fd, (off_t)(buf.p.len - sizeof(buf)), SEEK_CUR));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800245 if (next < 0) {
246 return -errno;
247 }
248 if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
249 return -EIO;
250 }
251 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800252}
253
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800254static void pmsgClose(struct android_log_logger_list* logger_list __unused,
255 struct android_log_transport_context* transp) {
256 int fd = atomic_exchange(&transp->context.fd, 0);
257 if (fd > 0) {
258 close(fd);
259 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800260}
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700261
George Burgess IV559387d2018-10-03 14:10:00 -0700262static void* realloc_or_free(void* ptr, size_t new_size) {
263 void* result = realloc(ptr, new_size);
264 if (!result) {
265 free(ptr);
266 }
267 return result;
268}
269
Tom Cherry2d9779e2019-02-08 11:46:19 -0800270ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* prefix,
271 __android_log_pmsg_file_read_fn fn, void* arg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800272 ssize_t ret;
273 struct android_log_logger_list logger_list;
274 struct android_log_transport_context transp;
275 struct content {
276 struct listnode node;
277 union {
278 struct logger_entry_v4 entry;
279 struct logger_entry_v4 entry_v4;
280 struct logger_entry_v3 entry_v3;
281 struct logger_entry_v2 entry_v2;
282 struct logger_entry entry_v1;
283 };
284 } * content;
285 struct names {
286 struct listnode node;
287 struct listnode content;
288 log_id_t id;
289 char prio;
290 char name[];
291 } * names;
292 struct listnode name_list;
293 struct listnode *node, *n;
294 size_t len, prefix_len;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700295
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800296 if (!fn) {
297 return -EINVAL;
298 }
299
300 /* Add just enough clues in logger_list and transp to make API function */
301 memset(&logger_list, 0, sizeof(logger_list));
302 memset(&transp, 0, sizeof(transp));
303
Tom Cherry71ba1642019-01-10 10:37:36 -0800304 logger_list.mode = ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK | ANDROID_LOG_RDONLY;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800305 transp.logMask = (unsigned)-1;
306 if (logId != LOG_ID_ANY) {
307 transp.logMask = (1 << logId);
308 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800309 transp.logMask &= ~((1 << LOG_ID_KERNEL) | (1 << LOG_ID_EVENTS) | (1 << LOG_ID_SECURITY));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800310 if (!transp.logMask) {
311 return -EINVAL;
312 }
313
314 /* Initialize name list */
315 list_init(&name_list);
316
317 ret = SSIZE_MAX;
318
319 /* Validate incoming prefix, shift until it contains only 0 or 1 : or / */
320 prefix_len = 0;
321 if (prefix) {
322 const char *prev = NULL, *last = NULL, *cp = prefix;
323 while ((cp = strpbrk(cp, "/:"))) {
324 prev = last;
325 last = cp;
326 cp = cp + 1;
327 }
328 if (prev) {
329 prefix = prev + 1;
330 }
331 prefix_len = strlen(prefix);
332 }
333
334 /* Read the file content */
335 while (pmsgRead(&logger_list, &transp, &transp.logMsg) > 0) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800336 const char* cp;
337 size_t hdr_size = transp.logMsg.entry.hdr_size ? transp.logMsg.entry.hdr_size
338 : sizeof(transp.logMsg.entry_v1);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800339 char* msg = (char*)&transp.logMsg + hdr_size;
Tom Cherry71ba1642019-01-10 10:37:36 -0800340 const char* split = NULL;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800341
Tom Cherry71ba1642019-01-10 10:37:36 -0800342 if ((hdr_size < sizeof(transp.logMsg.entry_v1)) || (hdr_size > sizeof(transp.logMsg.entry))) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800343 continue;
344 }
345 /* Check for invalid sequence number */
346 if ((transp.logMsg.entry.nsec % ANDROID_LOG_PMSG_FILE_SEQUENCE) ||
347 ((transp.logMsg.entry.nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
348 ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE)) {
349 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700350 }
351
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800352 /* Determine if it has <dirbase>:<filebase> format for tag */
353 len = transp.logMsg.entry.len - sizeof(prio);
Tom Cherry71ba1642019-01-10 10:37:36 -0800354 for (cp = msg + sizeof(prio); *cp && isprint(*cp) && !isspace(*cp) && --len; ++cp) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800355 if (*cp == ':') {
356 if (split) {
357 break;
358 }
359 split = cp;
360 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700361 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800362 if (*cp || !split) {
363 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700364 }
365
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800366 /* Filters */
367 if (prefix_len && strncmp(msg + sizeof(prio), prefix, prefix_len)) {
368 size_t offset;
369 /*
370 * Allow : to be a synonym for /
371 * Things we do dealing with const char * and do not alloc
372 */
373 split = strchr(prefix, ':');
374 if (split) {
375 continue;
376 }
377 split = strchr(prefix, '/');
378 if (!split) {
379 continue;
380 }
381 offset = split - prefix;
Tom Cherry71ba1642019-01-10 10:37:36 -0800382 if ((msg[offset + sizeof(prio)] != ':') || strncmp(msg + sizeof(prio), prefix, offset)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800383 continue;
384 }
385 ++offset;
Tom Cherry71ba1642019-01-10 10:37:36 -0800386 if ((prefix_len > offset) &&
387 strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800388 continue;
389 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700390 }
391
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800392 if ((prio != ANDROID_LOG_ANY) && (*msg < prio)) {
393 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700394 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700395
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800396 /* check if there is an existing entry */
397 list_for_each(node, &name_list) {
398 names = node_to_item(node, struct names, node);
Tom Cherry71ba1642019-01-10 10:37:36 -0800399 if (!strcmp(names->name, msg + sizeof(prio)) && (names->id == transp.logMsg.entry.lid) &&
400 (names->prio == *msg)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800401 break;
402 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700403 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800404
405 /* We do not have an existing entry, create and add one */
406 if (node == &name_list) {
407 static const char numbers[] = "0123456789";
408 unsigned long long nl;
409
410 len = strlen(msg + sizeof(prio)) + 1;
Tom Cherry71ba1642019-01-10 10:37:36 -0800411 names = static_cast<struct names*>(calloc(1, sizeof(*names) + len));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800412 if (!names) {
413 ret = -ENOMEM;
414 break;
415 }
416 strcpy(names->name, msg + sizeof(prio));
Tom Cherry71ba1642019-01-10 10:37:36 -0800417 names->id = static_cast<log_id_t>(transp.logMsg.entry.lid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800418 names->prio = *msg;
419 list_init(&names->content);
420 /*
421 * Insert in reverse numeric _then_ alpha sorted order as
422 * representative of log rotation:
423 *
424 * log.10
425 * klog.10
426 * . . .
427 * log.2
428 * klog.2
429 * log.1
430 * klog.1
431 * log
432 * klog
433 *
434 * thus when we present the content, we are provided the oldest
435 * first, which when 'refreshed' could spill off the end of the
436 * pmsg FIFO but retaining the newest data for last with best
437 * chances to survive.
438 */
439 nl = 0;
440 cp = strpbrk(names->name, numbers);
441 if (cp) {
442 nl = strtoull(cp, NULL, 10);
443 }
444 list_for_each_reverse(node, &name_list) {
445 struct names* a_name = node_to_item(node, struct names, node);
446 const char* r = a_name->name;
447 int compare = 0;
448
449 unsigned long long nr = 0;
450 cp = strpbrk(r, numbers);
451 if (cp) {
452 nr = strtoull(cp, NULL, 10);
453 }
454 if (nr != nl) {
455 compare = (nl > nr) ? 1 : -1;
456 }
457 if (compare == 0) {
458 compare = strcmp(names->name, r);
459 }
460 if (compare <= 0) {
461 break;
462 }
463 }
464 list_add_head(node, &names->node);
465 }
466
467 /* Remove any file fragments that match our sequence number */
468 list_for_each_safe(node, n, &names->content) {
469 content = node_to_item(node, struct content, node);
470 if (transp.logMsg.entry.nsec == content->entry.nsec) {
471 list_remove(&content->node);
472 free(content);
473 }
474 }
475
476 /* Add content */
Tom Cherry71ba1642019-01-10 10:37:36 -0800477 content = static_cast<struct content*>(
478 calloc(1, sizeof(content->node) + hdr_size + transp.logMsg.entry.len));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800479 if (!content) {
480 ret = -ENOMEM;
481 break;
482 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800483 memcpy(&content->entry, &transp.logMsg.entry, hdr_size + transp.logMsg.entry.len);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800484
485 /* Insert in sequence number sorted order, to ease reconstruction */
486 list_for_each_reverse(node, &names->content) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800487 if ((node_to_item(node, struct content, node))->entry.nsec < transp.logMsg.entry.nsec) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800488 break;
489 }
490 }
491 list_add_head(node, &content->node);
492 }
493 pmsgClose(&logger_list, &transp);
494
495 /* Progress through all the collected files */
496 list_for_each_safe(node, n, &name_list) {
497 struct listnode *content_node, *m;
498 char* buf;
499 size_t sequence, tag_len;
500
501 names = node_to_item(node, struct names, node);
502
503 /* Construct content into a linear buffer */
504 buf = NULL;
505 len = 0;
506 sequence = 0;
507 tag_len = strlen(names->name) + sizeof(char); /* tag + nul */
508 list_for_each_safe(content_node, m, &names->content) {
509 ssize_t add_len;
510
511 content = node_to_item(content_node, struct content, node);
512 add_len = content->entry.len - tag_len - sizeof(prio);
513 if (add_len <= 0) {
514 list_remove(content_node);
515 free(content);
516 continue;
517 }
518
519 if (!buf) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800520 buf = static_cast<char*>(malloc(sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800521 if (!buf) {
522 ret = -ENOMEM;
523 list_remove(content_node);
524 free(content);
525 continue;
526 }
527 *buf = '\0';
528 }
529
530 /* Missing sequence numbers */
531 while (sequence < content->entry.nsec) {
532 /* plus space for enforced nul */
Tom Cherry71ba1642019-01-10 10:37:36 -0800533 buf = static_cast<char*>(realloc_or_free(buf, len + sizeof(char) + sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800534 if (!buf) {
535 break;
536 }
537 buf[len] = '\f'; /* Mark missing content with a form feed */
538 buf[++len] = '\0';
539 sequence += ANDROID_LOG_PMSG_FILE_SEQUENCE;
540 }
541 if (!buf) {
542 ret = -ENOMEM;
543 list_remove(content_node);
544 free(content);
545 continue;
546 }
547 /* plus space for enforced nul */
Tom Cherry71ba1642019-01-10 10:37:36 -0800548 buf = static_cast<char*>(realloc_or_free(buf, len + add_len + sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800549 if (!buf) {
550 ret = -ENOMEM;
551 list_remove(content_node);
552 free(content);
553 continue;
554 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800555 memcpy(buf + len, (char*)&content->entry + content->entry.hdr_size + tag_len + sizeof(prio),
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800556 add_len);
557 len += add_len;
558 buf[len] = '\0'; /* enforce trailing hidden nul */
559 sequence = content->entry.nsec + ANDROID_LOG_PMSG_FILE_SEQUENCE;
560
561 list_remove(content_node);
562 free(content);
563 }
564 if (buf) {
565 if (len) {
566 /* Buffer contains enforced trailing nul just beyond length */
567 ssize_t r;
568 *strchr(names->name, ':') = '/'; /* Convert back to filename */
569 r = (*fn)(names->id, names->prio, names->name, buf, len, arg);
570 if ((ret >= 0) && (r > 0)) {
571 if (ret == SSIZE_MAX) {
572 ret = r;
573 } else {
574 ret += r;
575 }
576 } else if (r < ret) {
577 ret = r;
578 }
579 }
580 free(buf);
581 }
582 list_remove(node);
583 free(names);
584 }
585 return (ret == SSIZE_MAX) ? -ENOENT : ret;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700586}