blob: 7bc6e4a39478fe56be66f14b81fb45226c632366 [file] [log] [blame]
Mark Salyzyndd9094a2016-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 Salyzyn5b2ce402016-03-14 14:15:50 -070017#include <ctype.h>
Mark Salyzyndd9094a2016-03-01 13:45:42 -080018#include <errno.h>
19#include <fcntl.h>
20#include <stdbool.h>
Mark Salyzyn5b2ce402016-03-14 14:15:50 -070021#include <stdlib.h>
Mark Salyzyndd9094a2016-03-01 13:45:42 -080022#include <string.h>
23#include <sys/types.h>
24
25#include <private/android_filesystem_config.h>
26#include <private/android_logger.h>
27
28#include "config_read.h"
29#include "logger.h"
30
31static int pmsgAvailable(log_id_t logId);
Mark Salyzyn6e315682017-03-09 08:09:43 -080032static int pmsgVersion(struct android_log_logger* logger,
33 struct android_log_transport_context* transp);
34static int pmsgRead(struct android_log_logger_list* logger_list,
Tom Cherryf623f022019-01-10 10:37:36 -080035 struct android_log_transport_context* transp, struct log_msg* log_msg);
Mark Salyzyn6e315682017-03-09 08:09:43 -080036static void pmsgClose(struct android_log_logger_list* logger_list,
37 struct android_log_transport_context* transp);
38static int pmsgClear(struct android_log_logger* logger,
39 struct android_log_transport_context* transp);
Mark Salyzyndd9094a2016-03-01 13:45:42 -080040
41LIBLOG_HIDDEN struct android_log_transport_read pmsgLoggerRead = {
Tom Cherryf623f022019-01-10 10:37:36 -080042 .node = {&pmsgLoggerRead.node, &pmsgLoggerRead.node},
43 .name = "pmsg",
44 .available = pmsgAvailable,
45 .version = pmsgVersion,
46 .read = pmsgRead,
47 .poll = NULL,
48 .close = pmsgClose,
49 .clear = pmsgClear,
50 .setSize = NULL,
51 .getSize = NULL,
52 .getReadableSize = NULL,
53 .getPrune = NULL,
54 .setPrune = NULL,
55 .getStats = NULL,
Mark Salyzyndd9094a2016-03-01 13:45:42 -080056};
57
Mark Salyzyn6e315682017-03-09 08:09:43 -080058static int pmsgAvailable(log_id_t logId) {
59 if (logId > LOG_ID_SECURITY) {
60 return -EINVAL;
61 }
62 if (access("/dev/pmsg0", W_OK) == 0) {
63 return 0;
64 }
65 return -EBADF;
Mark Salyzyndd9094a2016-03-01 13:45:42 -080066}
67
68/* Determine the credentials of the caller */
Mark Salyzyn6e315682017-03-09 08:09:43 -080069static bool uid_has_log_permission(uid_t uid) {
Tom Cherryf623f022019-01-10 10:37:36 -080070 return (uid == AID_SYSTEM) || (uid == AID_LOG) || (uid == AID_ROOT) || (uid == AID_LOGD);
Mark Salyzyndd9094a2016-03-01 13:45:42 -080071}
72
Mark Salyzyn6e315682017-03-09 08:09:43 -080073static uid_t get_best_effective_uid() {
74 uid_t euid;
75 uid_t uid;
76 gid_t gid;
77 ssize_t i;
78 static uid_t last_uid = (uid_t)-1;
Mark Salyzyndd9094a2016-03-01 13:45:42 -080079
Mark Salyzyn6e315682017-03-09 08:09:43 -080080 if (last_uid != (uid_t)-1) {
81 return last_uid;
82 }
83 uid = __android_log_uid();
84 if (uid_has_log_permission(uid)) {
Mark Salyzyndd9094a2016-03-01 13:45:42 -080085 return last_uid = uid;
Mark Salyzyn6e315682017-03-09 08:09:43 -080086 }
87 euid = geteuid();
88 if (uid_has_log_permission(euid)) {
89 return last_uid = euid;
90 }
91 gid = getgid();
92 if (uid_has_log_permission(gid)) {
93 return last_uid = gid;
94 }
95 gid = getegid();
96 if (uid_has_log_permission(gid)) {
97 return last_uid = gid;
98 }
99 i = getgroups((size_t)0, NULL);
100 if (i > 0) {
101 gid_t list[i];
102
103 getgroups(i, list);
104 while (--i >= 0) {
105 if (uid_has_log_permission(list[i])) {
106 return last_uid = list[i];
107 }
108 }
109 }
110 return last_uid = uid;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800111}
112
Mark Salyzyn6e315682017-03-09 08:09:43 -0800113static int pmsgClear(struct android_log_logger* logger __unused,
114 struct android_log_transport_context* transp __unused) {
115 if (uid_has_log_permission(get_best_effective_uid())) {
116 return unlink("/sys/fs/pstore/pmsg-ramoops-0");
117 }
118 errno = EPERM;
119 return -1;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800120}
121
122/*
123 * returns the logger version
124 */
Mark Salyzyn6e315682017-03-09 08:09:43 -0800125static int pmsgVersion(struct android_log_logger* logger __unused,
126 struct android_log_transport_context* transp __unused) {
127 return 4;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800128}
129
Mark Salyzyn6e315682017-03-09 08:09:43 -0800130static int pmsgRead(struct android_log_logger_list* logger_list,
Tom Cherryf623f022019-01-10 10:37:36 -0800131 struct android_log_transport_context* transp, struct log_msg* log_msg) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800132 ssize_t ret;
133 off_t current, next;
134 uid_t uid;
135 struct android_log_logger* logger;
136 struct __attribute__((__packed__)) {
137 android_pmsg_log_header_t p;
138 android_log_header_t l;
139 uint8_t prio;
140 } buf;
141 static uint8_t preread_count;
142 bool is_system;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800143
Mark Salyzyn6e315682017-03-09 08:09:43 -0800144 memset(log_msg, 0, sizeof(*log_msg));
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800145
Mark Salyzyn6e315682017-03-09 08:09:43 -0800146 if (atomic_load(&transp->context.fd) <= 0) {
147 int i, fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800148
Mark Salyzyn6e315682017-03-09 08:09:43 -0800149 if (fd < 0) {
150 return -errno;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800151 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800152 if (fd == 0) { /* Argggg */
153 fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
154 close(0);
155 if (fd < 0) {
156 return -errno;
157 }
158 }
159 i = atomic_exchange(&transp->context.fd, fd);
160 if ((i > 0) && (i != fd)) {
161 close(i);
162 }
163 preread_count = 0;
164 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800165
Mark Salyzyn6e315682017-03-09 08:09:43 -0800166 while (1) {
167 int fd;
Mark Salyzynceee2f52016-10-10 07:27:42 -0700168
Mark Salyzyn6e315682017-03-09 08:09:43 -0800169 if (preread_count < sizeof(buf)) {
170 fd = atomic_load(&transp->context.fd);
171 if (fd <= 0) {
172 return -EBADF;
173 }
Tom Cherryf623f022019-01-10 10:37:36 -0800174 ret = TEMP_FAILURE_RETRY(read(fd, &buf.p.magic + preread_count, sizeof(buf) - preread_count));
Mark Salyzyn6e315682017-03-09 08:09:43 -0800175 if (ret < 0) {
176 return -errno;
177 }
178 preread_count += ret;
179 }
180 if (preread_count != sizeof(buf)) {
181 return preread_count ? -EIO : -EAGAIN;
182 }
183 if ((buf.p.magic != LOGGER_MAGIC) || (buf.p.len <= sizeof(buf)) ||
Tom Cherryf623f022019-01-10 10:37:36 -0800184 (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) || (buf.l.id >= LOG_ID_MAX) ||
185 (buf.l.realtime.tv_nsec >= NS_PER_SEC) ||
Mark Salyzyn6e315682017-03-09 08:09:43 -0800186 ((buf.l.id != LOG_ID_EVENTS) && (buf.l.id != LOG_ID_SECURITY) &&
Tom Cherryf623f022019-01-10 10:37:36 -0800187 ((buf.prio == ANDROID_LOG_UNKNOWN) || (buf.prio == ANDROID_LOG_DEFAULT) ||
Mark Salyzyn6e315682017-03-09 08:09:43 -0800188 (buf.prio >= ANDROID_LOG_SILENT)))) {
189 do {
190 memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
191 } while (preread_count && (buf.p.magic != LOGGER_MAGIC));
192 continue;
193 }
194 preread_count = 0;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800195
Mark Salyzyn6e315682017-03-09 08:09:43 -0800196 if ((transp->logMask & (1 << buf.l.id)) &&
197 ((!logger_list->start.tv_sec && !logger_list->start.tv_nsec) ||
198 ((logger_list->start.tv_sec <= buf.l.realtime.tv_sec) &&
199 ((logger_list->start.tv_sec != buf.l.realtime.tv_sec) ||
200 (logger_list->start.tv_nsec <= buf.l.realtime.tv_nsec)))) &&
201 (!logger_list->pid || (logger_list->pid == buf.p.pid))) {
202 uid = get_best_effective_uid();
203 is_system = uid_has_log_permission(uid);
204 if (is_system || (uid == buf.p.uid)) {
205 char* msg = is_system ? log_msg->entry_v4.msg : log_msg->entry_v3.msg;
206 *msg = buf.prio;
Mark Salyzynceee2f52016-10-10 07:27:42 -0700207 fd = atomic_load(&transp->context.fd);
208 if (fd <= 0) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800209 return -EBADF;
Mark Salyzynceee2f52016-10-10 07:27:42 -0700210 }
Tom Cherryf623f022019-01-10 10:37:36 -0800211 ret = TEMP_FAILURE_RETRY(read(fd, msg + sizeof(buf.prio), buf.p.len - sizeof(buf)));
Mark Salyzyn6e315682017-03-09 08:09:43 -0800212 if (ret < 0) {
213 return -errno;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800214 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800215 if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
216 return -EIO;
Mark Salyzynceee2f52016-10-10 07:27:42 -0700217 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800218
219 log_msg->entry_v4.len = buf.p.len - sizeof(buf) + sizeof(buf.prio);
220 log_msg->entry_v4.hdr_size =
221 is_system ? sizeof(log_msg->entry_v4) : sizeof(log_msg->entry_v3);
222 log_msg->entry_v4.pid = buf.p.pid;
223 log_msg->entry_v4.tid = buf.l.tid;
224 log_msg->entry_v4.sec = buf.l.realtime.tv_sec;
225 log_msg->entry_v4.nsec = buf.l.realtime.tv_nsec;
226 log_msg->entry_v4.lid = buf.l.id;
227 if (is_system) {
228 log_msg->entry_v4.uid = buf.p.uid;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800229 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800230
231 return ret + sizeof(buf.prio) + log_msg->entry_v4.hdr_size;
232 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800233 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800234
235 fd = atomic_load(&transp->context.fd);
236 if (fd <= 0) {
237 return -EBADF;
238 }
239 current = TEMP_FAILURE_RETRY(lseek(fd, (off_t)0, SEEK_CUR));
240 if (current < 0) {
241 return -errno;
242 }
243 fd = atomic_load(&transp->context.fd);
244 if (fd <= 0) {
245 return -EBADF;
246 }
Tom Cherryf623f022019-01-10 10:37:36 -0800247 next = TEMP_FAILURE_RETRY(lseek(fd, (off_t)(buf.p.len - sizeof(buf)), SEEK_CUR));
Mark Salyzyn6e315682017-03-09 08:09:43 -0800248 if (next < 0) {
249 return -errno;
250 }
251 if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
252 return -EIO;
253 }
254 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800255}
256
Mark Salyzyn6e315682017-03-09 08:09:43 -0800257static void pmsgClose(struct android_log_logger_list* logger_list __unused,
258 struct android_log_transport_context* transp) {
259 int fd = atomic_exchange(&transp->context.fd, 0);
260 if (fd > 0) {
261 close(fd);
262 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800263}
Mark Salyzyn5b2ce402016-03-14 14:15:50 -0700264
George Burgess IV32851b22018-10-03 14:10:00 -0700265static void* realloc_or_free(void* ptr, size_t new_size) {
266 void* result = realloc(ptr, new_size);
267 if (!result) {
268 free(ptr);
269 }
270 return result;
271}
272
Tom Cherryf623f022019-01-10 10:37:36 -0800273LIBLOG_ABI_PRIVATE ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio,
274 const char* prefix,
275 __android_log_pmsg_file_read_fn fn,
276 void* arg) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800277 ssize_t ret;
278 struct android_log_logger_list logger_list;
279 struct android_log_transport_context transp;
280 struct content {
281 struct listnode node;
282 union {
283 struct logger_entry_v4 entry;
284 struct logger_entry_v4 entry_v4;
285 struct logger_entry_v3 entry_v3;
286 struct logger_entry_v2 entry_v2;
287 struct logger_entry entry_v1;
288 };
289 } * content;
290 struct names {
291 struct listnode node;
292 struct listnode content;
293 log_id_t id;
294 char prio;
295 char name[];
296 } * names;
297 struct listnode name_list;
298 struct listnode *node, *n;
299 size_t len, prefix_len;
Mark Salyzyn5b2ce402016-03-14 14:15:50 -0700300
Mark Salyzyn6e315682017-03-09 08:09:43 -0800301 if (!fn) {
302 return -EINVAL;
303 }
304
305 /* Add just enough clues in logger_list and transp to make API function */
306 memset(&logger_list, 0, sizeof(logger_list));
307 memset(&transp, 0, sizeof(transp));
308
Tom Cherryf623f022019-01-10 10:37:36 -0800309 logger_list.mode = ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK | ANDROID_LOG_RDONLY;
Mark Salyzyn6e315682017-03-09 08:09:43 -0800310 transp.logMask = (unsigned)-1;
311 if (logId != LOG_ID_ANY) {
312 transp.logMask = (1 << logId);
313 }
Tom Cherryf623f022019-01-10 10:37:36 -0800314 transp.logMask &= ~((1 << LOG_ID_KERNEL) | (1 << LOG_ID_EVENTS) | (1 << LOG_ID_SECURITY));
Mark Salyzyn6e315682017-03-09 08:09:43 -0800315 if (!transp.logMask) {
316 return -EINVAL;
317 }
318
319 /* Initialize name list */
320 list_init(&name_list);
321
322 ret = SSIZE_MAX;
323
324 /* Validate incoming prefix, shift until it contains only 0 or 1 : or / */
325 prefix_len = 0;
326 if (prefix) {
327 const char *prev = NULL, *last = NULL, *cp = prefix;
328 while ((cp = strpbrk(cp, "/:"))) {
329 prev = last;
330 last = cp;
331 cp = cp + 1;
332 }
333 if (prev) {
334 prefix = prev + 1;
335 }
336 prefix_len = strlen(prefix);
337 }
338
339 /* Read the file content */
340 while (pmsgRead(&logger_list, &transp, &transp.logMsg) > 0) {
Tom Cherryf623f022019-01-10 10:37:36 -0800341 const char* cp;
342 size_t hdr_size = transp.logMsg.entry.hdr_size ? transp.logMsg.entry.hdr_size
343 : sizeof(transp.logMsg.entry_v1);
Mark Salyzyn6e315682017-03-09 08:09:43 -0800344 char* msg = (char*)&transp.logMsg + hdr_size;
Tom Cherryf623f022019-01-10 10:37:36 -0800345 const char* split = NULL;
Mark Salyzyn6e315682017-03-09 08:09:43 -0800346
Tom Cherryf623f022019-01-10 10:37:36 -0800347 if ((hdr_size < sizeof(transp.logMsg.entry_v1)) || (hdr_size > sizeof(transp.logMsg.entry))) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800348 continue;
349 }
350 /* Check for invalid sequence number */
351 if ((transp.logMsg.entry.nsec % ANDROID_LOG_PMSG_FILE_SEQUENCE) ||
352 ((transp.logMsg.entry.nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
353 ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE)) {
354 continue;
Mark Salyzyn5b2ce402016-03-14 14:15:50 -0700355 }
356
Mark Salyzyn6e315682017-03-09 08:09:43 -0800357 /* Determine if it has <dirbase>:<filebase> format for tag */
358 len = transp.logMsg.entry.len - sizeof(prio);
Tom Cherryf623f022019-01-10 10:37:36 -0800359 for (cp = msg + sizeof(prio); *cp && isprint(*cp) && !isspace(*cp) && --len; ++cp) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800360 if (*cp == ':') {
361 if (split) {
362 break;
363 }
364 split = cp;
365 }
Mark Salyzyn5b2ce402016-03-14 14:15:50 -0700366 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800367 if (*cp || !split) {
368 continue;
Mark Salyzyn5b2ce402016-03-14 14:15:50 -0700369 }
370
Mark Salyzyn6e315682017-03-09 08:09:43 -0800371 /* Filters */
372 if (prefix_len && strncmp(msg + sizeof(prio), prefix, prefix_len)) {
373 size_t offset;
374 /*
375 * Allow : to be a synonym for /
376 * Things we do dealing with const char * and do not alloc
377 */
378 split = strchr(prefix, ':');
379 if (split) {
380 continue;
381 }
382 split = strchr(prefix, '/');
383 if (!split) {
384 continue;
385 }
386 offset = split - prefix;
Tom Cherryf623f022019-01-10 10:37:36 -0800387 if ((msg[offset + sizeof(prio)] != ':') || strncmp(msg + sizeof(prio), prefix, offset)) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800388 continue;
389 }
390 ++offset;
Tom Cherryf623f022019-01-10 10:37:36 -0800391 if ((prefix_len > offset) &&
392 strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800393 continue;
394 }
Mark Salyzyn5b2ce402016-03-14 14:15:50 -0700395 }
396
Mark Salyzyn6e315682017-03-09 08:09:43 -0800397 if ((prio != ANDROID_LOG_ANY) && (*msg < prio)) {
398 continue;
Mark Salyzyn5b2ce402016-03-14 14:15:50 -0700399 }
Mark Salyzyn5b2ce402016-03-14 14:15:50 -0700400
Mark Salyzyn6e315682017-03-09 08:09:43 -0800401 /* check if there is an existing entry */
402 list_for_each(node, &name_list) {
403 names = node_to_item(node, struct names, node);
Tom Cherryf623f022019-01-10 10:37:36 -0800404 if (!strcmp(names->name, msg + sizeof(prio)) && (names->id == transp.logMsg.entry.lid) &&
405 (names->prio == *msg)) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800406 break;
407 }
Mark Salyzyn5b2ce402016-03-14 14:15:50 -0700408 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800409
410 /* We do not have an existing entry, create and add one */
411 if (node == &name_list) {
412 static const char numbers[] = "0123456789";
413 unsigned long long nl;
414
415 len = strlen(msg + sizeof(prio)) + 1;
Tom Cherryf623f022019-01-10 10:37:36 -0800416 names = static_cast<struct names*>(calloc(1, sizeof(*names) + len));
Mark Salyzyn6e315682017-03-09 08:09:43 -0800417 if (!names) {
418 ret = -ENOMEM;
419 break;
420 }
421 strcpy(names->name, msg + sizeof(prio));
Tom Cherryf623f022019-01-10 10:37:36 -0800422 names->id = static_cast<log_id_t>(transp.logMsg.entry.lid);
Mark Salyzyn6e315682017-03-09 08:09:43 -0800423 names->prio = *msg;
424 list_init(&names->content);
425 /*
426 * Insert in reverse numeric _then_ alpha sorted order as
427 * representative of log rotation:
428 *
429 * log.10
430 * klog.10
431 * . . .
432 * log.2
433 * klog.2
434 * log.1
435 * klog.1
436 * log
437 * klog
438 *
439 * thus when we present the content, we are provided the oldest
440 * first, which when 'refreshed' could spill off the end of the
441 * pmsg FIFO but retaining the newest data for last with best
442 * chances to survive.
443 */
444 nl = 0;
445 cp = strpbrk(names->name, numbers);
446 if (cp) {
447 nl = strtoull(cp, NULL, 10);
448 }
449 list_for_each_reverse(node, &name_list) {
450 struct names* a_name = node_to_item(node, struct names, node);
451 const char* r = a_name->name;
452 int compare = 0;
453
454 unsigned long long nr = 0;
455 cp = strpbrk(r, numbers);
456 if (cp) {
457 nr = strtoull(cp, NULL, 10);
458 }
459 if (nr != nl) {
460 compare = (nl > nr) ? 1 : -1;
461 }
462 if (compare == 0) {
463 compare = strcmp(names->name, r);
464 }
465 if (compare <= 0) {
466 break;
467 }
468 }
469 list_add_head(node, &names->node);
470 }
471
472 /* Remove any file fragments that match our sequence number */
473 list_for_each_safe(node, n, &names->content) {
474 content = node_to_item(node, struct content, node);
475 if (transp.logMsg.entry.nsec == content->entry.nsec) {
476 list_remove(&content->node);
477 free(content);
478 }
479 }
480
481 /* Add content */
Tom Cherryf623f022019-01-10 10:37:36 -0800482 content = static_cast<struct content*>(
483 calloc(1, sizeof(content->node) + hdr_size + transp.logMsg.entry.len));
Mark Salyzyn6e315682017-03-09 08:09:43 -0800484 if (!content) {
485 ret = -ENOMEM;
486 break;
487 }
Tom Cherryf623f022019-01-10 10:37:36 -0800488 memcpy(&content->entry, &transp.logMsg.entry, hdr_size + transp.logMsg.entry.len);
Mark Salyzyn6e315682017-03-09 08:09:43 -0800489
490 /* Insert in sequence number sorted order, to ease reconstruction */
491 list_for_each_reverse(node, &names->content) {
Tom Cherryf623f022019-01-10 10:37:36 -0800492 if ((node_to_item(node, struct content, node))->entry.nsec < transp.logMsg.entry.nsec) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800493 break;
494 }
495 }
496 list_add_head(node, &content->node);
497 }
498 pmsgClose(&logger_list, &transp);
499
500 /* Progress through all the collected files */
501 list_for_each_safe(node, n, &name_list) {
502 struct listnode *content_node, *m;
503 char* buf;
504 size_t sequence, tag_len;
505
506 names = node_to_item(node, struct names, node);
507
508 /* Construct content into a linear buffer */
509 buf = NULL;
510 len = 0;
511 sequence = 0;
512 tag_len = strlen(names->name) + sizeof(char); /* tag + nul */
513 list_for_each_safe(content_node, m, &names->content) {
514 ssize_t add_len;
515
516 content = node_to_item(content_node, struct content, node);
517 add_len = content->entry.len - tag_len - sizeof(prio);
518 if (add_len <= 0) {
519 list_remove(content_node);
520 free(content);
521 continue;
522 }
523
524 if (!buf) {
Tom Cherryf623f022019-01-10 10:37:36 -0800525 buf = static_cast<char*>(malloc(sizeof(char)));
Mark Salyzyn6e315682017-03-09 08:09:43 -0800526 if (!buf) {
527 ret = -ENOMEM;
528 list_remove(content_node);
529 free(content);
530 continue;
531 }
532 *buf = '\0';
533 }
534
535 /* Missing sequence numbers */
536 while (sequence < content->entry.nsec) {
537 /* plus space for enforced nul */
Tom Cherryf623f022019-01-10 10:37:36 -0800538 buf = static_cast<char*>(realloc_or_free(buf, len + sizeof(char) + sizeof(char)));
Mark Salyzyn6e315682017-03-09 08:09:43 -0800539 if (!buf) {
540 break;
541 }
542 buf[len] = '\f'; /* Mark missing content with a form feed */
543 buf[++len] = '\0';
544 sequence += ANDROID_LOG_PMSG_FILE_SEQUENCE;
545 }
546 if (!buf) {
547 ret = -ENOMEM;
548 list_remove(content_node);
549 free(content);
550 continue;
551 }
552 /* plus space for enforced nul */
Tom Cherryf623f022019-01-10 10:37:36 -0800553 buf = static_cast<char*>(realloc_or_free(buf, len + add_len + sizeof(char)));
Mark Salyzyn6e315682017-03-09 08:09:43 -0800554 if (!buf) {
555 ret = -ENOMEM;
556 list_remove(content_node);
557 free(content);
558 continue;
559 }
Tom Cherryf623f022019-01-10 10:37:36 -0800560 memcpy(buf + len, (char*)&content->entry + content->entry.hdr_size + tag_len + sizeof(prio),
Mark Salyzyn6e315682017-03-09 08:09:43 -0800561 add_len);
562 len += add_len;
563 buf[len] = '\0'; /* enforce trailing hidden nul */
564 sequence = content->entry.nsec + ANDROID_LOG_PMSG_FILE_SEQUENCE;
565
566 list_remove(content_node);
567 free(content);
568 }
569 if (buf) {
570 if (len) {
571 /* Buffer contains enforced trailing nul just beyond length */
572 ssize_t r;
573 *strchr(names->name, ':') = '/'; /* Convert back to filename */
574 r = (*fn)(names->id, names->prio, names->name, buf, len, arg);
575 if ((ret >= 0) && (r > 0)) {
576 if (ret == SSIZE_MAX) {
577 ret = r;
578 } else {
579 ret += r;
580 }
581 } else if (r < ret) {
582 ret = r;
583 }
584 }
585 free(buf);
586 }
587 list_remove(node);
588 free(names);
589 }
590 return (ret == SSIZE_MAX) ? -ENOENT : ret;
Mark Salyzyn5b2ce402016-03-14 14:15:50 -0700591}