blob: 9c5bc9536c216e2ac34aeaf1eaa413c17ed20609 [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
65/* Determine the credentials of the caller */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080066static bool uid_has_log_permission(uid_t uid) {
Tom Cherry71ba1642019-01-10 10:37:36 -080067 return (uid == AID_SYSTEM) || (uid == AID_LOG) || (uid == AID_ROOT) || (uid == AID_LOGD);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080068}
69
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080070static uid_t get_best_effective_uid() {
71 uid_t euid;
72 uid_t uid;
73 gid_t gid;
74 ssize_t i;
75 static uid_t last_uid = (uid_t)-1;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080076
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080077 if (last_uid != (uid_t)-1) {
78 return last_uid;
79 }
80 uid = __android_log_uid();
81 if (uid_has_log_permission(uid)) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -080082 return last_uid = uid;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080083 }
84 euid = geteuid();
85 if (uid_has_log_permission(euid)) {
86 return last_uid = euid;
87 }
88 gid = getgid();
89 if (uid_has_log_permission(gid)) {
90 return last_uid = gid;
91 }
92 gid = getegid();
93 if (uid_has_log_permission(gid)) {
94 return last_uid = gid;
95 }
96 i = getgroups((size_t)0, NULL);
97 if (i > 0) {
98 gid_t list[i];
99
100 getgroups(i, list);
101 while (--i >= 0) {
102 if (uid_has_log_permission(list[i])) {
103 return last_uid = list[i];
104 }
105 }
106 }
107 return last_uid = uid;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800108}
109
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800110static int pmsgClear(struct android_log_logger* logger __unused,
111 struct android_log_transport_context* transp __unused) {
112 if (uid_has_log_permission(get_best_effective_uid())) {
113 return unlink("/sys/fs/pstore/pmsg-ramoops-0");
114 }
115 errno = EPERM;
116 return -1;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800117}
118
119/*
120 * returns the logger version
121 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800122static int pmsgVersion(struct android_log_logger* logger __unused,
123 struct android_log_transport_context* transp __unused) {
124 return 4;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800125}
126
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800127static int pmsgRead(struct android_log_logger_list* logger_list,
Tom Cherry71ba1642019-01-10 10:37:36 -0800128 struct android_log_transport_context* transp, struct log_msg* log_msg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800129 ssize_t ret;
130 off_t current, next;
131 uid_t uid;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800132 struct __attribute__((__packed__)) {
133 android_pmsg_log_header_t p;
134 android_log_header_t l;
135 uint8_t prio;
136 } buf;
137 static uint8_t preread_count;
138 bool is_system;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800139
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800140 memset(log_msg, 0, sizeof(*log_msg));
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800141
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800142 if (atomic_load(&transp->context.fd) <= 0) {
143 int i, fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800144
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800145 if (fd < 0) {
146 return -errno;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800147 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800148 if (fd == 0) { /* Argggg */
149 fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
150 close(0);
151 if (fd < 0) {
152 return -errno;
153 }
154 }
155 i = atomic_exchange(&transp->context.fd, fd);
156 if ((i > 0) && (i != fd)) {
157 close(i);
158 }
159 preread_count = 0;
160 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800161
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800162 while (1) {
163 int fd;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700164
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800165 if (preread_count < sizeof(buf)) {
166 fd = atomic_load(&transp->context.fd);
167 if (fd <= 0) {
168 return -EBADF;
169 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800170 ret = TEMP_FAILURE_RETRY(read(fd, &buf.p.magic + preread_count, sizeof(buf) - preread_count));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800171 if (ret < 0) {
172 return -errno;
173 }
174 preread_count += ret;
175 }
176 if (preread_count != sizeof(buf)) {
177 return preread_count ? -EIO : -EAGAIN;
178 }
179 if ((buf.p.magic != LOGGER_MAGIC) || (buf.p.len <= sizeof(buf)) ||
Tom Cherry71ba1642019-01-10 10:37:36 -0800180 (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) || (buf.l.id >= LOG_ID_MAX) ||
181 (buf.l.realtime.tv_nsec >= NS_PER_SEC) ||
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800182 ((buf.l.id != LOG_ID_EVENTS) && (buf.l.id != LOG_ID_SECURITY) &&
Tom Cherry71ba1642019-01-10 10:37:36 -0800183 ((buf.prio == ANDROID_LOG_UNKNOWN) || (buf.prio == ANDROID_LOG_DEFAULT) ||
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800184 (buf.prio >= ANDROID_LOG_SILENT)))) {
185 do {
186 memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
187 } while (preread_count && (buf.p.magic != LOGGER_MAGIC));
188 continue;
189 }
190 preread_count = 0;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800191
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800192 if ((transp->logMask & (1 << buf.l.id)) &&
193 ((!logger_list->start.tv_sec && !logger_list->start.tv_nsec) ||
194 ((logger_list->start.tv_sec <= buf.l.realtime.tv_sec) &&
195 ((logger_list->start.tv_sec != buf.l.realtime.tv_sec) ||
196 (logger_list->start.tv_nsec <= buf.l.realtime.tv_nsec)))) &&
197 (!logger_list->pid || (logger_list->pid == buf.p.pid))) {
198 uid = get_best_effective_uid();
199 is_system = uid_has_log_permission(uid);
200 if (is_system || (uid == buf.p.uid)) {
201 char* msg = is_system ? log_msg->entry_v4.msg : log_msg->entry_v3.msg;
202 *msg = buf.prio;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700203 fd = atomic_load(&transp->context.fd);
204 if (fd <= 0) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800205 return -EBADF;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700206 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800207 ret = TEMP_FAILURE_RETRY(read(fd, msg + sizeof(buf.prio), buf.p.len - sizeof(buf)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800208 if (ret < 0) {
209 return -errno;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800210 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800211 if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
212 return -EIO;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700213 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800214
215 log_msg->entry_v4.len = buf.p.len - sizeof(buf) + sizeof(buf.prio);
216 log_msg->entry_v4.hdr_size =
217 is_system ? sizeof(log_msg->entry_v4) : sizeof(log_msg->entry_v3);
218 log_msg->entry_v4.pid = buf.p.pid;
219 log_msg->entry_v4.tid = buf.l.tid;
220 log_msg->entry_v4.sec = buf.l.realtime.tv_sec;
221 log_msg->entry_v4.nsec = buf.l.realtime.tv_nsec;
222 log_msg->entry_v4.lid = buf.l.id;
223 if (is_system) {
224 log_msg->entry_v4.uid = buf.p.uid;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800225 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800226
227 return ret + sizeof(buf.prio) + log_msg->entry_v4.hdr_size;
228 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800229 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800230
231 fd = atomic_load(&transp->context.fd);
232 if (fd <= 0) {
233 return -EBADF;
234 }
235 current = TEMP_FAILURE_RETRY(lseek(fd, (off_t)0, SEEK_CUR));
236 if (current < 0) {
237 return -errno;
238 }
239 fd = atomic_load(&transp->context.fd);
240 if (fd <= 0) {
241 return -EBADF;
242 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800243 next = TEMP_FAILURE_RETRY(lseek(fd, (off_t)(buf.p.len - sizeof(buf)), SEEK_CUR));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800244 if (next < 0) {
245 return -errno;
246 }
247 if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
248 return -EIO;
249 }
250 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800251}
252
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800253static void pmsgClose(struct android_log_logger_list* logger_list __unused,
254 struct android_log_transport_context* transp) {
255 int fd = atomic_exchange(&transp->context.fd, 0);
256 if (fd > 0) {
257 close(fd);
258 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800259}
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700260
George Burgess IV559387d2018-10-03 14:10:00 -0700261static void* realloc_or_free(void* ptr, size_t new_size) {
262 void* result = realloc(ptr, new_size);
263 if (!result) {
264 free(ptr);
265 }
266 return result;
267}
268
Tom Cherry2d9779e2019-02-08 11:46:19 -0800269ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* prefix,
270 __android_log_pmsg_file_read_fn fn, void* arg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800271 ssize_t ret;
272 struct android_log_logger_list logger_list;
273 struct android_log_transport_context transp;
274 struct content {
275 struct listnode node;
276 union {
277 struct logger_entry_v4 entry;
278 struct logger_entry_v4 entry_v4;
279 struct logger_entry_v3 entry_v3;
280 struct logger_entry_v2 entry_v2;
281 struct logger_entry entry_v1;
282 };
283 } * content;
284 struct names {
285 struct listnode node;
286 struct listnode content;
287 log_id_t id;
288 char prio;
289 char name[];
290 } * names;
291 struct listnode name_list;
292 struct listnode *node, *n;
293 size_t len, prefix_len;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700294
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800295 if (!fn) {
296 return -EINVAL;
297 }
298
299 /* Add just enough clues in logger_list and transp to make API function */
300 memset(&logger_list, 0, sizeof(logger_list));
301 memset(&transp, 0, sizeof(transp));
302
Tom Cherry71ba1642019-01-10 10:37:36 -0800303 logger_list.mode = ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK | ANDROID_LOG_RDONLY;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800304 transp.logMask = (unsigned)-1;
305 if (logId != LOG_ID_ANY) {
306 transp.logMask = (1 << logId);
307 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800308 transp.logMask &= ~((1 << LOG_ID_KERNEL) | (1 << LOG_ID_EVENTS) | (1 << LOG_ID_SECURITY));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800309 if (!transp.logMask) {
310 return -EINVAL;
311 }
312
313 /* Initialize name list */
314 list_init(&name_list);
315
316 ret = SSIZE_MAX;
317
318 /* Validate incoming prefix, shift until it contains only 0 or 1 : or / */
319 prefix_len = 0;
320 if (prefix) {
321 const char *prev = NULL, *last = NULL, *cp = prefix;
322 while ((cp = strpbrk(cp, "/:"))) {
323 prev = last;
324 last = cp;
325 cp = cp + 1;
326 }
327 if (prev) {
328 prefix = prev + 1;
329 }
330 prefix_len = strlen(prefix);
331 }
332
333 /* Read the file content */
334 while (pmsgRead(&logger_list, &transp, &transp.logMsg) > 0) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800335 const char* cp;
336 size_t hdr_size = transp.logMsg.entry.hdr_size ? transp.logMsg.entry.hdr_size
337 : sizeof(transp.logMsg.entry_v1);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800338 char* msg = (char*)&transp.logMsg + hdr_size;
Tom Cherry71ba1642019-01-10 10:37:36 -0800339 const char* split = NULL;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800340
Tom Cherry71ba1642019-01-10 10:37:36 -0800341 if ((hdr_size < sizeof(transp.logMsg.entry_v1)) || (hdr_size > sizeof(transp.logMsg.entry))) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800342 continue;
343 }
344 /* Check for invalid sequence number */
345 if ((transp.logMsg.entry.nsec % ANDROID_LOG_PMSG_FILE_SEQUENCE) ||
346 ((transp.logMsg.entry.nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
347 ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE)) {
348 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700349 }
350
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800351 /* Determine if it has <dirbase>:<filebase> format for tag */
352 len = transp.logMsg.entry.len - sizeof(prio);
Tom Cherry71ba1642019-01-10 10:37:36 -0800353 for (cp = msg + sizeof(prio); *cp && isprint(*cp) && !isspace(*cp) && --len; ++cp) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800354 if (*cp == ':') {
355 if (split) {
356 break;
357 }
358 split = cp;
359 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700360 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800361 if (*cp || !split) {
362 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700363 }
364
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800365 /* Filters */
366 if (prefix_len && strncmp(msg + sizeof(prio), prefix, prefix_len)) {
367 size_t offset;
368 /*
369 * Allow : to be a synonym for /
370 * Things we do dealing with const char * and do not alloc
371 */
372 split = strchr(prefix, ':');
373 if (split) {
374 continue;
375 }
376 split = strchr(prefix, '/');
377 if (!split) {
378 continue;
379 }
380 offset = split - prefix;
Tom Cherry71ba1642019-01-10 10:37:36 -0800381 if ((msg[offset + sizeof(prio)] != ':') || strncmp(msg + sizeof(prio), prefix, offset)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800382 continue;
383 }
384 ++offset;
Tom Cherry71ba1642019-01-10 10:37:36 -0800385 if ((prefix_len > offset) &&
386 strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800387 continue;
388 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700389 }
390
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800391 if ((prio != ANDROID_LOG_ANY) && (*msg < prio)) {
392 continue;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700393 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700394
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800395 /* check if there is an existing entry */
396 list_for_each(node, &name_list) {
397 names = node_to_item(node, struct names, node);
Tom Cherry71ba1642019-01-10 10:37:36 -0800398 if (!strcmp(names->name, msg + sizeof(prio)) && (names->id == transp.logMsg.entry.lid) &&
399 (names->prio == *msg)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800400 break;
401 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700402 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800403
404 /* We do not have an existing entry, create and add one */
405 if (node == &name_list) {
406 static const char numbers[] = "0123456789";
407 unsigned long long nl;
408
409 len = strlen(msg + sizeof(prio)) + 1;
Tom Cherry71ba1642019-01-10 10:37:36 -0800410 names = static_cast<struct names*>(calloc(1, sizeof(*names) + len));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800411 if (!names) {
412 ret = -ENOMEM;
413 break;
414 }
415 strcpy(names->name, msg + sizeof(prio));
Tom Cherry71ba1642019-01-10 10:37:36 -0800416 names->id = static_cast<log_id_t>(transp.logMsg.entry.lid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800417 names->prio = *msg;
418 list_init(&names->content);
419 /*
420 * Insert in reverse numeric _then_ alpha sorted order as
421 * representative of log rotation:
422 *
423 * log.10
424 * klog.10
425 * . . .
426 * log.2
427 * klog.2
428 * log.1
429 * klog.1
430 * log
431 * klog
432 *
433 * thus when we present the content, we are provided the oldest
434 * first, which when 'refreshed' could spill off the end of the
435 * pmsg FIFO but retaining the newest data for last with best
436 * chances to survive.
437 */
438 nl = 0;
439 cp = strpbrk(names->name, numbers);
440 if (cp) {
441 nl = strtoull(cp, NULL, 10);
442 }
443 list_for_each_reverse(node, &name_list) {
444 struct names* a_name = node_to_item(node, struct names, node);
445 const char* r = a_name->name;
446 int compare = 0;
447
448 unsigned long long nr = 0;
449 cp = strpbrk(r, numbers);
450 if (cp) {
451 nr = strtoull(cp, NULL, 10);
452 }
453 if (nr != nl) {
454 compare = (nl > nr) ? 1 : -1;
455 }
456 if (compare == 0) {
457 compare = strcmp(names->name, r);
458 }
459 if (compare <= 0) {
460 break;
461 }
462 }
463 list_add_head(node, &names->node);
464 }
465
466 /* Remove any file fragments that match our sequence number */
467 list_for_each_safe(node, n, &names->content) {
468 content = node_to_item(node, struct content, node);
469 if (transp.logMsg.entry.nsec == content->entry.nsec) {
470 list_remove(&content->node);
471 free(content);
472 }
473 }
474
475 /* Add content */
Tom Cherry71ba1642019-01-10 10:37:36 -0800476 content = static_cast<struct content*>(
477 calloc(1, sizeof(content->node) + hdr_size + transp.logMsg.entry.len));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800478 if (!content) {
479 ret = -ENOMEM;
480 break;
481 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800482 memcpy(&content->entry, &transp.logMsg.entry, hdr_size + transp.logMsg.entry.len);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800483
484 /* Insert in sequence number sorted order, to ease reconstruction */
485 list_for_each_reverse(node, &names->content) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800486 if ((node_to_item(node, struct content, node))->entry.nsec < transp.logMsg.entry.nsec) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800487 break;
488 }
489 }
490 list_add_head(node, &content->node);
491 }
492 pmsgClose(&logger_list, &transp);
493
494 /* Progress through all the collected files */
495 list_for_each_safe(node, n, &name_list) {
496 struct listnode *content_node, *m;
497 char* buf;
498 size_t sequence, tag_len;
499
500 names = node_to_item(node, struct names, node);
501
502 /* Construct content into a linear buffer */
503 buf = NULL;
504 len = 0;
505 sequence = 0;
506 tag_len = strlen(names->name) + sizeof(char); /* tag + nul */
507 list_for_each_safe(content_node, m, &names->content) {
508 ssize_t add_len;
509
510 content = node_to_item(content_node, struct content, node);
511 add_len = content->entry.len - tag_len - sizeof(prio);
512 if (add_len <= 0) {
513 list_remove(content_node);
514 free(content);
515 continue;
516 }
517
518 if (!buf) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800519 buf = static_cast<char*>(malloc(sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800520 if (!buf) {
521 ret = -ENOMEM;
522 list_remove(content_node);
523 free(content);
524 continue;
525 }
526 *buf = '\0';
527 }
528
529 /* Missing sequence numbers */
530 while (sequence < content->entry.nsec) {
531 /* plus space for enforced nul */
Tom Cherry71ba1642019-01-10 10:37:36 -0800532 buf = static_cast<char*>(realloc_or_free(buf, len + sizeof(char) + sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800533 if (!buf) {
534 break;
535 }
536 buf[len] = '\f'; /* Mark missing content with a form feed */
537 buf[++len] = '\0';
538 sequence += ANDROID_LOG_PMSG_FILE_SEQUENCE;
539 }
540 if (!buf) {
541 ret = -ENOMEM;
542 list_remove(content_node);
543 free(content);
544 continue;
545 }
546 /* plus space for enforced nul */
Tom Cherry71ba1642019-01-10 10:37:36 -0800547 buf = static_cast<char*>(realloc_or_free(buf, len + add_len + sizeof(char)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800548 if (!buf) {
549 ret = -ENOMEM;
550 list_remove(content_node);
551 free(content);
552 continue;
553 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800554 memcpy(buf + len, (char*)&content->entry + content->entry.hdr_size + tag_len + sizeof(prio),
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800555 add_len);
556 len += add_len;
557 buf[len] = '\0'; /* enforce trailing hidden nul */
558 sequence = content->entry.nsec + ANDROID_LOG_PMSG_FILE_SEQUENCE;
559
560 list_remove(content_node);
561 free(content);
562 }
563 if (buf) {
564 if (len) {
565 /* Buffer contains enforced trailing nul just beyond length */
566 ssize_t r;
567 *strchr(names->name, ':') = '/'; /* Convert back to filename */
568 r = (*fn)(names->id, names->prio, names->name, buf, len, arg);
569 if ((ret >= 0) && (r > 0)) {
570 if (ret == SSIZE_MAX) {
571 ret = r;
572 } else {
573 ret += r;
574 }
575 } else if (r < ret) {
576 ret = r;
577 }
578 }
579 free(buf);
580 }
581 list_remove(node);
582 free(names);
583 }
584 return (ret == SSIZE_MAX) ? -ENOENT : ret;
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700585}