blob: 679c159574dd63dea004a184ba5187665b2c7ed3 [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
154 if (transp->context.fd <= 0) {
Mark Salyzyn78786da2016-04-28 16:06:24 -0700155 int 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 }
167 transp->context.fd = fd;
168 preread_count = 0;
169 }
170
171 while(1) {
172 if (preread_count < sizeof(buf)) {
173 ret = TEMP_FAILURE_RETRY(read(transp->context.fd,
174 &buf.p.magic + preread_count,
175 sizeof(buf) - preread_count));
176 if (ret < 0) {
177 return -errno;
178 }
179 preread_count += ret;
180 }
181 if (preread_count != sizeof(buf)) {
182 return preread_count ? -EIO : -EAGAIN;
183 }
Mark Salyzyn3d8afe92016-07-13 08:30:30 -0700184 if ((buf.p.magic != LOGGER_MAGIC) ||
185 (buf.p.len <= sizeof(buf)) ||
186 (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) ||
187 (buf.l.id >= LOG_ID_MAX) ||
188 (buf.l.realtime.tv_nsec >= NS_PER_SEC) ||
189 ((buf.l.id != LOG_ID_EVENTS) &&
190 (buf.l.id != LOG_ID_SECURITY) &&
191 ((buf.prio == ANDROID_LOG_UNKNOWN) ||
192 (buf.prio == ANDROID_LOG_DEFAULT) ||
193 (buf.prio >= ANDROID_LOG_SILENT)))) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800194 do {
195 memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
196 } while (preread_count && (buf.p.magic != LOGGER_MAGIC));
197 continue;
198 }
199 preread_count = 0;
200
201 if ((transp->logMask & (1 << buf.l.id)) &&
202 ((!logger_list->start.tv_sec && !logger_list->start.tv_nsec) ||
203 ((logger_list->start.tv_sec <= buf.l.realtime.tv_sec) &&
204 ((logger_list->start.tv_sec != buf.l.realtime.tv_sec) ||
205 (logger_list->start.tv_nsec <=
206 buf.l.realtime.tv_nsec)))) &&
207 (!logger_list->pid || (logger_list->pid == buf.p.pid))) {
208 uid = get_best_effective_uid();
209 is_system = uid_has_log_permission(uid);
210 if (is_system || (uid == buf.p.uid)) {
Mark Salyzyn3d8afe92016-07-13 08:30:30 -0700211 char *msg = is_system ?
212 log_msg->entry_v4.msg :
213 log_msg->entry_v3.msg;
214 *msg = buf.prio;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800215 ret = TEMP_FAILURE_RETRY(read(transp->context.fd,
Mark Salyzyn3d8afe92016-07-13 08:30:30 -0700216 msg + sizeof(buf.prio),
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800217 buf.p.len - sizeof(buf)));
218 if (ret < 0) {
219 return -errno;
220 }
221 if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
222 return -EIO;
223 }
224
Mark Salyzyn3d8afe92016-07-13 08:30:30 -0700225 log_msg->entry_v4.len = buf.p.len - sizeof(buf) + sizeof(buf.prio);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800226 log_msg->entry_v4.hdr_size = is_system ?
227 sizeof(log_msg->entry_v4) :
228 sizeof(log_msg->entry_v3);
229 log_msg->entry_v4.pid = buf.p.pid;
230 log_msg->entry_v4.tid = buf.l.tid;
231 log_msg->entry_v4.sec = buf.l.realtime.tv_sec;
232 log_msg->entry_v4.nsec = buf.l.realtime.tv_nsec;
233 log_msg->entry_v4.lid = buf.l.id;
234 if (is_system) {
235 log_msg->entry_v4.uid = buf.p.uid;
236 }
237
Mark Salyzyn3d8afe92016-07-13 08:30:30 -0700238 return ret + sizeof(buf.prio) + log_msg->entry_v4.hdr_size;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800239 }
240 }
241
242 current = TEMP_FAILURE_RETRY(lseek(transp->context.fd,
243 (off_t)0, SEEK_CUR));
244 if (current < 0) {
245 return -errno;
246 }
247 next = TEMP_FAILURE_RETRY(lseek(transp->context.fd,
248 (off_t)(buf.p.len - sizeof(buf)),
249 SEEK_CUR));
250 if (next < 0) {
251 return -errno;
252 }
253 if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
254 return -EIO;
255 }
256 }
257}
258
259static void pmsgClose(struct android_log_logger_list *logger_list __unused,
260 struct android_log_transport_context *transp) {
261 if (transp->context.fd > 0) {
262 close (transp->context.fd);
263 }
264 transp->context.fd = 0;
265}
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700266
267LIBLOG_ABI_PRIVATE ssize_t __android_log_pmsg_file_read(
268 log_id_t logId,
269 char prio,
270 const char *prefix,
271 __android_log_pmsg_file_read_fn fn, void *arg) {
272 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;
295
296 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
304 logger_list.mode = ANDROID_LOG_PSTORE |
305 ANDROID_LOG_NONBLOCK |
306 ANDROID_LOG_RDONLY;
307 transp.logMask = (unsigned)-1;
308 if (logId != LOG_ID_ANY) {
309 transp.logMask = (1 << logId);
310 }
311 transp.logMask &= ~((1 << LOG_ID_KERNEL) |
312 (1 << LOG_ID_EVENTS) |
313 (1 << LOG_ID_SECURITY));
314 if (!transp.logMask) {
315 return -EINVAL;
316 }
317
318 /* Initialize name list */
319 list_init(&name_list);
320
321 ret = SSIZE_MAX;
322
323 /* Validate incoming prefix, shift until it contains only 0 or 1 : or / */
324 prefix_len = 0;
325 if (prefix) {
326 const char *prev = NULL, *last = NULL, *cp = prefix;
327 while ((cp = strpbrk(cp, "/:"))) {
328 prev = last;
329 last = cp;
330 cp = cp + 1;
331 }
332 if (prev) {
333 prefix = prev + 1;
334 }
335 prefix_len = strlen(prefix);
336 }
337
338 /* Read the file content */
339 while (pmsgRead(&logger_list, &transp, &transp.logMsg) > 0) {
340 char *cp;
341 size_t hdr_size = transp.logMsg.entry.hdr_size ?
342 transp.logMsg.entry.hdr_size : sizeof(transp.logMsg.entry_v1);
343 char *msg = (char *)&transp.logMsg + hdr_size;
344 char *split = NULL;
345
Mark Salyzyn305374c2016-08-18 14:59:41 -0700346 if ((hdr_size < sizeof(transp.logMsg.entry_v1)) ||
347 (hdr_size > sizeof(transp.logMsg.entry))) {
348 continue;
349 }
Mark Salyzyn864e8e82016-03-14 14:15:50 -0700350 /* 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;
355 }
356
357 /* Determine if it has <dirbase>:<filebase> format for tag */
358 len = transp.logMsg.entry.len - sizeof(prio);
359 for (cp = msg + sizeof(prio);
360 *cp && isprint(*cp) && !isspace(*cp) && --len;
361 ++cp) {
362 if (*cp == ':') {
363 if (split) {
364 break;
365 }
366 split = cp;
367 }
368 }
369 if (*cp || !split) {
370 continue;
371 }
372
373 /* Filters */
374 if (prefix_len && strncmp(msg + sizeof(prio), prefix, prefix_len)) {
375 size_t offset;
376 /*
377 * Allow : to be a synonym for /
378 * Things we do dealing with const char * and do not alloc
379 */
380 split = strchr(prefix, ':');
381 if (split) {
382 continue;
383 }
384 split = strchr(prefix, '/');
385 if (!split) {
386 continue;
387 }
388 offset = split - prefix;
389 if ((msg[offset + sizeof(prio)] != ':') ||
390 strncmp(msg + sizeof(prio), prefix, offset)) {
391 continue;
392 }
393 ++offset;
394 if ((prefix_len > offset) &&
395 strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) {
396 continue;
397 }
398 }
399
400 if ((prio != ANDROID_LOG_ANY) && (*msg < prio)) {
401 continue;
402 }
403
404 /* check if there is an existing entry */
405 list_for_each(node, &name_list) {
406 names = node_to_item(node, struct names, node);
407 if (!strcmp(names->name, msg + sizeof(prio)) &&
408 (names->id == transp.logMsg.entry.lid) &&
409 (names->prio == *msg)) {
410 break;
411 }
412 }
413
414 /* We do not have an existing entry, create and add one */
415 if (node == &name_list) {
416 static const char numbers[] = "0123456789";
417 unsigned long long nl;
418
419 len = strlen(msg + sizeof(prio)) + 1;
420 names = calloc(1, sizeof(*names) + len);
421 if (!names) {
422 ret = -ENOMEM;
423 break;
424 }
425 strcpy(names->name, msg + sizeof(prio));
426 names->id = transp.logMsg.entry.lid;
427 names->prio = *msg;
428 list_init(&names->content);
429 /*
430 * Insert in reverse numeric _then_ alpha sorted order as
431 * representative of log rotation:
432 *
433 * log.10
434 * klog.10
435 * . . .
436 * log.2
437 * klog.2
438 * log.1
439 * klog.1
440 * log
441 * klog
442 *
443 * thus when we present the content, we are provided the oldest
444 * first, which when 'refreshed' could spill off the end of the
445 * pmsg FIFO but retaining the newest data for last with best
446 * chances to survive.
447 */
448 nl = 0;
449 cp = strpbrk(names->name, numbers);
450 if (cp) {
451 nl = strtoull(cp, NULL, 10);
452 }
453 list_for_each_reverse(node, &name_list) {
454 struct names *a_name = node_to_item(node, struct names, node);
455 const char *r = a_name->name;
456 int compare = 0;
457
458 unsigned long long nr = 0;
459 cp = strpbrk(r, numbers);
460 if (cp) {
461 nr = strtoull(cp, NULL, 10);
462 }
463 if (nr != nl) {
464 compare = (nl > nr) ? 1 : -1;
465 }
466 if (compare == 0) {
467 compare = strcmp(names->name, r);
468 }
469 if (compare <= 0) {
470 break;
471 }
472 }
473 list_add_head(node, &names->node);
474 }
475
476 /* Remove any file fragments that match our sequence number */
477 list_for_each_safe(node, n, &names->content) {
478 content = node_to_item(node, struct content, node);
479 if (transp.logMsg.entry.nsec == content->entry.nsec) {
480 list_remove(&content->node);
481 free(content);
482 }
483 }
484
485 /* Add content */
486 content = calloc(1, sizeof(content->node) +
487 hdr_size + transp.logMsg.entry.len);
488 if (!content) {
489 ret = -ENOMEM;
490 break;
491 }
492 memcpy(&content->entry, &transp.logMsg.entry,
493 hdr_size + transp.logMsg.entry.len);
494
495 /* Insert in sequence number sorted order, to ease reconstruction */
496 list_for_each_reverse(node, &names->content) {
497 if ((node_to_item(node, struct content, node))->entry.nsec <
498 transp.logMsg.entry.nsec) {
499 break;
500 }
501 }
502 list_add_head(node, &content->node);
503 }
504 pmsgClose(&logger_list, &transp);
505
506 /* Progress through all the collected files */
507 list_for_each_safe(node, n, &name_list) {
508 struct listnode *content_node, *m;
509 char *buf;
510 size_t sequence, tag_len;
511
512 names = node_to_item(node, struct names, node);
513
514 /* Construct content into a linear buffer */
515 buf = NULL;
516 len = 0;
517 sequence = 0;
518 tag_len = strlen(names->name) + sizeof(char); /* tag + nul */
519 list_for_each_safe(content_node, m, &names->content) {
520 ssize_t add_len;
521
522 content = node_to_item(content_node, struct content, node);
523 add_len = content->entry.len - tag_len - sizeof(prio);
524 if (add_len <= 0) {
525 list_remove(content_node);
526 free(content);
527 continue;
528 }
529
530 if (!buf) {
531 buf = malloc(sizeof(char));
532 if (!buf) {
533 ret = -ENOMEM;
534 list_remove(content_node);
535 free(content);
536 continue;
537 }
538 *buf = '\0';
539 }
540
541 /* Missing sequence numbers */
542 while (sequence < content->entry.nsec) {
543 /* plus space for enforced nul */
544 buf = realloc(buf, len + sizeof(char) + sizeof(char));
545 if (!buf) {
546 break;
547 }
548 buf[len] = '\f'; /* Mark missing content with a form feed */
549 buf[++len] = '\0';
550 sequence += ANDROID_LOG_PMSG_FILE_SEQUENCE;
551 }
552 if (!buf) {
553 ret = -ENOMEM;
554 list_remove(content_node);
555 free(content);
556 continue;
557 }
558 /* plus space for enforced nul */
559 buf = realloc(buf, len + add_len + sizeof(char));
560 if (!buf) {
561 ret = -ENOMEM;
562 list_remove(content_node);
563 free(content);
564 continue;
565 }
566 memcpy(buf + len,
567 (char *)&content->entry + content->entry.hdr_size +
568 tag_len + sizeof(prio),
569 add_len);
570 len += add_len;
571 buf[len] = '\0'; /* enforce trailing hidden nul */
572 sequence = content->entry.nsec + ANDROID_LOG_PMSG_FILE_SEQUENCE;
573
574 list_remove(content_node);
575 free(content);
576 }
577 if (buf) {
578 if (len) {
579 /* Buffer contains enforced trailing nul just beyond length */
580 ssize_t r;
581 *strchr(names->name, ':') = '/'; /* Convert back to filename */
582 r = (*fn)(names->id, names->prio, names->name, buf, len, arg);
583 if ((ret >= 0) && (r > 0)) {
584 if (ret == SSIZE_MAX) {
585 ret = r;
586 } else {
587 ret += r;
588 }
589 } else if (r < ret) {
590 ret = r;
591 }
592 }
593 free(buf);
594 }
595 list_remove(node);
596 free(names);
597 }
598 return (ret == SSIZE_MAX) ? -ENOENT : ret;
599}