blob: a0a69c1894f62d7b5b569a57e3c0f5643bec74b6 [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>
20#include <stdbool.h>
Mark Salyzyn864e8e82016-03-14 14:15:50 -070021#include <stdlib.h>
Mark Salyzyn018a96d2016-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);
32static int pmsgVersion(struct android_log_logger *logger,
33 struct android_log_transport_context *transp);
34static int pmsgRead(struct android_log_logger_list *logger_list,
35 struct android_log_transport_context *transp,
36 struct log_msg *log_msg);
37static void pmsgClose(struct android_log_logger_list *logger_list,
38 struct android_log_transport_context *transp);
39static int pmsgClear(struct android_log_logger *logger,
40 struct android_log_transport_context *transp);
41
42LIBLOG_HIDDEN struct android_log_transport_read pmsgLoggerRead = {
43 .node = { &pmsgLoggerRead.node, &pmsgLoggerRead.node },
44 .name = "pmsg",
45 .available = pmsgAvailable,
46 .version = pmsgVersion,
47 .read = pmsgRead,
48 .poll = NULL,
49 .close = pmsgClose,
50 .clear = pmsgClear,
51 .setSize = NULL,
52 .getSize = NULL,
53 .getReadableSize = NULL,
54 .getPrune = NULL,
55 .setPrune = NULL,
56 .getStats = NULL,
57};
58
59static int pmsgAvailable(log_id_t logId)
60{
61 if (logId > LOG_ID_SECURITY) {
62 return -EINVAL;
63 }
64 if (access("/dev/pmsg0", W_OK) == 0) {
65 return 0;
66 }
67 return -EBADF;
68}
69
70/* Determine the credentials of the caller */
71static bool uid_has_log_permission(uid_t uid)
72{
73 return (uid == AID_SYSTEM) || (uid == AID_LOG) || (uid == AID_ROOT);
74}
75
76static uid_t get_best_effective_uid()
77{
78 uid_t euid;
79 uid_t uid;
80 gid_t gid;
81 ssize_t i;
82 static uid_t last_uid = (uid_t) -1;
83
84 if (last_uid != (uid_t) -1) {
85 return last_uid;
86 }
87 uid = __android_log_uid();
88 if (uid_has_log_permission(uid)) {
89 return last_uid = uid;
90 }
91 euid = geteuid();
92 if (uid_has_log_permission(euid)) {
93 return last_uid = euid;
94 }
95 gid = getgid();
96 if (uid_has_log_permission(gid)) {
97 return last_uid = gid;
98 }
99 gid = getegid();
100 if (uid_has_log_permission(gid)) {
101 return last_uid = gid;
102 }
103 i = getgroups((size_t) 0, NULL);
104 if (i > 0) {
105 gid_t list[i];
106
107 getgroups(i, list);
108 while (--i >= 0) {
109 if (uid_has_log_permission(list[i])) {
110 return last_uid = list[i];
111 }
112 }
113 }
114 return last_uid = uid;
115}
116
117static int pmsgClear(struct android_log_logger *logger __unused,
118 struct android_log_transport_context *transp __unused)
119{
120 if (uid_has_log_permission(get_best_effective_uid())) {
121 return unlink("/sys/fs/pstore/pmsg-ramoops-0");
122 }
123 errno = EPERM;
124 return -1;
125}
126
127/*
128 * returns the logger version
129 */
130static int pmsgVersion(struct android_log_logger *logger __unused,
131 struct android_log_transport_context *transp __unused)
132{
133 return 4;
134}
135
136static int pmsgRead(struct android_log_logger_list *logger_list,
137 struct android_log_transport_context *transp,
138 struct log_msg *log_msg)
139{
140 ssize_t ret;
141 off_t current, next;
142 uid_t uid;
143 struct android_log_logger *logger;
144 struct __attribute__((__packed__)) {
145 android_pmsg_log_header_t p;
146 android_log_header_t l;
Mark Salyzyn3d8afe92016-07-13 08:30:30 -0700147 uint8_t prio;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800148 } buf;
149 static uint8_t preread_count;
150 bool is_system;
151
152 memset(log_msg, 0, sizeof(*log_msg));
153
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700154 if (atomic_load(&transp->context.fd) <= 0) {
155 int i, fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800156
157 if (fd < 0) {
158 return -errno;
159 }
160 if (fd == 0) { /* Argggg */
Mark Salyzyn78786da2016-04-28 16:06:24 -0700161 fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800162 close(0);
163 if (fd < 0) {
164 return -errno;
165 }
166 }
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700167 i = atomic_exchange(&transp->context.fd, fd);
168 if ((i > 0) && (i != fd)) {
169 close(i);
170 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800171 preread_count = 0;
172 }
173
174 while(1) {
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700175 int fd;
176
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800177 if (preread_count < sizeof(buf)) {
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700178 fd = atomic_load(&transp->context.fd);
179 if (fd <= 0) {
180 return -EBADF;
181 }
182 ret = TEMP_FAILURE_RETRY(read(fd,
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800183 &buf.p.magic + preread_count,
184 sizeof(buf) - preread_count));
185 if (ret < 0) {
186 return -errno;
187 }
188 preread_count += ret;
189 }
190 if (preread_count != sizeof(buf)) {
191 return preread_count ? -EIO : -EAGAIN;
192 }
Mark Salyzyn3d8afe92016-07-13 08:30:30 -0700193 if ((buf.p.magic != LOGGER_MAGIC) ||
194 (buf.p.len <= sizeof(buf)) ||
195 (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) ||
196 (buf.l.id >= LOG_ID_MAX) ||
197 (buf.l.realtime.tv_nsec >= NS_PER_SEC) ||
198 ((buf.l.id != LOG_ID_EVENTS) &&
199 (buf.l.id != LOG_ID_SECURITY) &&
200 ((buf.prio == ANDROID_LOG_UNKNOWN) ||
201 (buf.prio == ANDROID_LOG_DEFAULT) ||
202 (buf.prio >= ANDROID_LOG_SILENT)))) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800203 do {
204 memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
205 } while (preread_count && (buf.p.magic != LOGGER_MAGIC));
206 continue;
207 }
208 preread_count = 0;
209
210 if ((transp->logMask & (1 << buf.l.id)) &&
211 ((!logger_list->start.tv_sec && !logger_list->start.tv_nsec) ||
212 ((logger_list->start.tv_sec <= buf.l.realtime.tv_sec) &&
213 ((logger_list->start.tv_sec != buf.l.realtime.tv_sec) ||
214 (logger_list->start.tv_nsec <=
215 buf.l.realtime.tv_nsec)))) &&
216 (!logger_list->pid || (logger_list->pid == buf.p.pid))) {
217 uid = get_best_effective_uid();
218 is_system = uid_has_log_permission(uid);
219 if (is_system || (uid == buf.p.uid)) {
Mark Salyzyn3d8afe92016-07-13 08:30:30 -0700220 char *msg = is_system ?
221 log_msg->entry_v4.msg :
222 log_msg->entry_v3.msg;
223 *msg = buf.prio;
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700224 fd = atomic_load(&transp->context.fd);
225 if (fd <= 0) {
226 return -EBADF;
227 }
228 ret = TEMP_FAILURE_RETRY(read(fd,
229 msg + sizeof(buf.prio),
230 buf.p.len - sizeof(buf)));
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800231 if (ret < 0) {
232 return -errno;
233 }
234 if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
235 return -EIO;
236 }
237
Mark Salyzyn3d8afe92016-07-13 08:30:30 -0700238 log_msg->entry_v4.len = buf.p.len - sizeof(buf) + sizeof(buf.prio);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800239 log_msg->entry_v4.hdr_size = is_system ?
240 sizeof(log_msg->entry_v4) :
241 sizeof(log_msg->entry_v3);
242 log_msg->entry_v4.pid = buf.p.pid;
243 log_msg->entry_v4.tid = buf.l.tid;
244 log_msg->entry_v4.sec = buf.l.realtime.tv_sec;
245 log_msg->entry_v4.nsec = buf.l.realtime.tv_nsec;
246 log_msg->entry_v4.lid = buf.l.id;
247 if (is_system) {
248 log_msg->entry_v4.uid = buf.p.uid;
249 }
250
Mark Salyzyn3d8afe92016-07-13 08:30:30 -0700251 return ret + sizeof(buf.prio) + log_msg->entry_v4.hdr_size;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800252 }
253 }
254
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700255 fd = atomic_load(&transp->context.fd);
256 if (fd <= 0) {
257 return -EBADF;
258 }
259 current = TEMP_FAILURE_RETRY(lseek(fd, (off_t)0, SEEK_CUR));
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800260 if (current < 0) {
261 return -errno;
262 }
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700263 fd = atomic_load(&transp->context.fd);
264 if (fd <= 0) {
265 return -EBADF;
266 }
267 next = TEMP_FAILURE_RETRY(lseek(fd,
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800268 (off_t)(buf.p.len - sizeof(buf)),
269 SEEK_CUR));
270 if (next < 0) {
271 return -errno;
272 }
273 if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
274 return -EIO;
275 }
276 }
277}
278
279static void pmsgClose(struct android_log_logger_list *logger_list __unused,
280 struct android_log_transport_context *transp) {
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700281 int fd = atomic_exchange(&transp->context.fd, 0);
282 if (fd > 0) {
283 close (fd);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800284 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800285}
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700286
287LIBLOG_ABI_PRIVATE ssize_t __android_log_pmsg_file_read(
288 log_id_t logId,
289 char prio,
290 const char *prefix,
291 __android_log_pmsg_file_read_fn fn, void *arg) {
292 ssize_t ret;
293 struct android_log_logger_list logger_list;
294 struct android_log_transport_context transp;
295 struct content {
296 struct listnode node;
297 union {
298 struct logger_entry_v4 entry;
299 struct logger_entry_v4 entry_v4;
300 struct logger_entry_v3 entry_v3;
301 struct logger_entry_v2 entry_v2;
302 struct logger_entry entry_v1;
303 };
304 } *content;
305 struct names {
306 struct listnode node;
307 struct listnode content;
308 log_id_t id;
309 char prio;
310 char name[];
311 } *names;
312 struct listnode name_list;
313 struct listnode *node, *n;
314 size_t len, prefix_len;
315
316 if (!fn) {
317 return -EINVAL;
318 }
319
320 /* Add just enough clues in logger_list and transp to make API function */
321 memset(&logger_list, 0, sizeof(logger_list));
322 memset(&transp, 0, sizeof(transp));
323
324 logger_list.mode = ANDROID_LOG_PSTORE |
325 ANDROID_LOG_NONBLOCK |
326 ANDROID_LOG_RDONLY;
327 transp.logMask = (unsigned)-1;
328 if (logId != LOG_ID_ANY) {
329 transp.logMask = (1 << logId);
330 }
331 transp.logMask &= ~((1 << LOG_ID_KERNEL) |
332 (1 << LOG_ID_EVENTS) |
333 (1 << LOG_ID_SECURITY));
334 if (!transp.logMask) {
335 return -EINVAL;
336 }
337
338 /* Initialize name list */
339 list_init(&name_list);
340
341 ret = SSIZE_MAX;
342
343 /* Validate incoming prefix, shift until it contains only 0 or 1 : or / */
344 prefix_len = 0;
345 if (prefix) {
346 const char *prev = NULL, *last = NULL, *cp = prefix;
347 while ((cp = strpbrk(cp, "/:"))) {
348 prev = last;
349 last = cp;
350 cp = cp + 1;
351 }
352 if (prev) {
353 prefix = prev + 1;
354 }
355 prefix_len = strlen(prefix);
356 }
357
358 /* Read the file content */
359 while (pmsgRead(&logger_list, &transp, &transp.logMsg) > 0) {
360 char *cp;
361 size_t hdr_size = transp.logMsg.entry.hdr_size ?
362 transp.logMsg.entry.hdr_size : sizeof(transp.logMsg.entry_v1);
363 char *msg = (char *)&transp.logMsg + hdr_size;
364 char *split = NULL;
365
Mark Salyzyn305374c2016-08-18 14:59:41 -0700366 if ((hdr_size < sizeof(transp.logMsg.entry_v1)) ||
367 (hdr_size > sizeof(transp.logMsg.entry))) {
368 continue;
369 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700370 /* Check for invalid sequence number */
371 if ((transp.logMsg.entry.nsec % ANDROID_LOG_PMSG_FILE_SEQUENCE) ||
372 ((transp.logMsg.entry.nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
373 ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE)) {
374 continue;
375 }
376
377 /* Determine if it has <dirbase>:<filebase> format for tag */
378 len = transp.logMsg.entry.len - sizeof(prio);
379 for (cp = msg + sizeof(prio);
380 *cp && isprint(*cp) && !isspace(*cp) && --len;
381 ++cp) {
382 if (*cp == ':') {
383 if (split) {
384 break;
385 }
386 split = cp;
387 }
388 }
389 if (*cp || !split) {
390 continue;
391 }
392
393 /* Filters */
394 if (prefix_len && strncmp(msg + sizeof(prio), prefix, prefix_len)) {
395 size_t offset;
396 /*
397 * Allow : to be a synonym for /
398 * Things we do dealing with const char * and do not alloc
399 */
400 split = strchr(prefix, ':');
401 if (split) {
402 continue;
403 }
404 split = strchr(prefix, '/');
405 if (!split) {
406 continue;
407 }
408 offset = split - prefix;
409 if ((msg[offset + sizeof(prio)] != ':') ||
410 strncmp(msg + sizeof(prio), prefix, offset)) {
411 continue;
412 }
413 ++offset;
414 if ((prefix_len > offset) &&
415 strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) {
416 continue;
417 }
418 }
419
420 if ((prio != ANDROID_LOG_ANY) && (*msg < prio)) {
421 continue;
422 }
423
424 /* check if there is an existing entry */
425 list_for_each(node, &name_list) {
426 names = node_to_item(node, struct names, node);
427 if (!strcmp(names->name, msg + sizeof(prio)) &&
428 (names->id == transp.logMsg.entry.lid) &&
429 (names->prio == *msg)) {
430 break;
431 }
432 }
433
434 /* We do not have an existing entry, create and add one */
435 if (node == &name_list) {
436 static const char numbers[] = "0123456789";
437 unsigned long long nl;
438
439 len = strlen(msg + sizeof(prio)) + 1;
440 names = calloc(1, sizeof(*names) + len);
441 if (!names) {
442 ret = -ENOMEM;
443 break;
444 }
445 strcpy(names->name, msg + sizeof(prio));
446 names->id = transp.logMsg.entry.lid;
447 names->prio = *msg;
448 list_init(&names->content);
449 /*
450 * Insert in reverse numeric _then_ alpha sorted order as
451 * representative of log rotation:
452 *
453 * log.10
454 * klog.10
455 * . . .
456 * log.2
457 * klog.2
458 * log.1
459 * klog.1
460 * log
461 * klog
462 *
463 * thus when we present the content, we are provided the oldest
464 * first, which when 'refreshed' could spill off the end of the
465 * pmsg FIFO but retaining the newest data for last with best
466 * chances to survive.
467 */
468 nl = 0;
469 cp = strpbrk(names->name, numbers);
470 if (cp) {
471 nl = strtoull(cp, NULL, 10);
472 }
473 list_for_each_reverse(node, &name_list) {
474 struct names *a_name = node_to_item(node, struct names, node);
475 const char *r = a_name->name;
476 int compare = 0;
477
478 unsigned long long nr = 0;
479 cp = strpbrk(r, numbers);
480 if (cp) {
481 nr = strtoull(cp, NULL, 10);
482 }
483 if (nr != nl) {
484 compare = (nl > nr) ? 1 : -1;
485 }
486 if (compare == 0) {
487 compare = strcmp(names->name, r);
488 }
489 if (compare <= 0) {
490 break;
491 }
492 }
493 list_add_head(node, &names->node);
494 }
495
496 /* Remove any file fragments that match our sequence number */
497 list_for_each_safe(node, n, &names->content) {
498 content = node_to_item(node, struct content, node);
499 if (transp.logMsg.entry.nsec == content->entry.nsec) {
500 list_remove(&content->node);
501 free(content);
502 }
503 }
504
505 /* Add content */
506 content = calloc(1, sizeof(content->node) +
507 hdr_size + transp.logMsg.entry.len);
508 if (!content) {
509 ret = -ENOMEM;
510 break;
511 }
512 memcpy(&content->entry, &transp.logMsg.entry,
513 hdr_size + transp.logMsg.entry.len);
514
515 /* Insert in sequence number sorted order, to ease reconstruction */
516 list_for_each_reverse(node, &names->content) {
517 if ((node_to_item(node, struct content, node))->entry.nsec <
518 transp.logMsg.entry.nsec) {
519 break;
520 }
521 }
522 list_add_head(node, &content->node);
523 }
524 pmsgClose(&logger_list, &transp);
525
526 /* Progress through all the collected files */
527 list_for_each_safe(node, n, &name_list) {
528 struct listnode *content_node, *m;
529 char *buf;
530 size_t sequence, tag_len;
531
532 names = node_to_item(node, struct names, node);
533
534 /* Construct content into a linear buffer */
535 buf = NULL;
536 len = 0;
537 sequence = 0;
538 tag_len = strlen(names->name) + sizeof(char); /* tag + nul */
539 list_for_each_safe(content_node, m, &names->content) {
540 ssize_t add_len;
541
542 content = node_to_item(content_node, struct content, node);
543 add_len = content->entry.len - tag_len - sizeof(prio);
544 if (add_len <= 0) {
545 list_remove(content_node);
546 free(content);
547 continue;
548 }
549
550 if (!buf) {
551 buf = malloc(sizeof(char));
552 if (!buf) {
553 ret = -ENOMEM;
554 list_remove(content_node);
555 free(content);
556 continue;
557 }
558 *buf = '\0';
559 }
560
561 /* Missing sequence numbers */
562 while (sequence < content->entry.nsec) {
563 /* plus space for enforced nul */
564 buf = realloc(buf, len + sizeof(char) + sizeof(char));
565 if (!buf) {
566 break;
567 }
568 buf[len] = '\f'; /* Mark missing content with a form feed */
569 buf[++len] = '\0';
570 sequence += ANDROID_LOG_PMSG_FILE_SEQUENCE;
571 }
572 if (!buf) {
573 ret = -ENOMEM;
574 list_remove(content_node);
575 free(content);
576 continue;
577 }
578 /* plus space for enforced nul */
579 buf = realloc(buf, len + add_len + sizeof(char));
580 if (!buf) {
581 ret = -ENOMEM;
582 list_remove(content_node);
583 free(content);
584 continue;
585 }
586 memcpy(buf + len,
587 (char *)&content->entry + content->entry.hdr_size +
588 tag_len + sizeof(prio),
589 add_len);
590 len += add_len;
591 buf[len] = '\0'; /* enforce trailing hidden nul */
592 sequence = content->entry.nsec + ANDROID_LOG_PMSG_FILE_SEQUENCE;
593
594 list_remove(content_node);
595 free(content);
596 }
597 if (buf) {
598 if (len) {
599 /* Buffer contains enforced trailing nul just beyond length */
600 ssize_t r;
601 *strchr(names->name, ':') = '/'; /* Convert back to filename */
602 r = (*fn)(names->id, names->prio, names->name, buf, len, arg);
603 if ((ret >= 0) && (r > 0)) {
604 if (ret == SSIZE_MAX) {
605 ret = r;
606 } else {
607 ret += r;
608 }
609 } else if (r < ret) {
610 ret = r;
611 }
612 }
613 free(buf);
614 }
615 list_remove(node);
616 free(names);
617 }
618 return (ret == SSIZE_MAX) ? -ENOENT : ret;
619}