1 /*
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
17 #include "pmsg_reader.h"
18
19 #include <ctype.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25
26 #include <cutils/list.h>
27 #include <private/android_logger.h>
28
29 #include "logger.h"
30
PmsgRead(struct logger_list * logger_list,struct log_msg * log_msg)31 int PmsgRead(struct logger_list* logger_list, struct log_msg* log_msg) {
32 ssize_t ret;
33 off_t current, next;
34 struct __attribute__((__packed__)) {
35 android_pmsg_log_header_t p;
36 android_log_header_t l;
37 uint8_t prio;
38 } buf;
39 static uint8_t preread_count;
40
41 memset(log_msg, 0, sizeof(*log_msg));
42
43 if (atomic_load(&logger_list->fd) <= 0) {
44 int i, fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
45
46 if (fd < 0) {
47 return -errno;
48 }
49 if (fd == 0) { /* Argggg */
50 fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
51 close(0);
52 if (fd < 0) {
53 return -errno;
54 }
55 }
56 i = atomic_exchange(&logger_list->fd, fd);
57 if ((i > 0) && (i != fd)) {
58 close(i);
59 }
60 preread_count = 0;
61 }
62
63 while (1) {
64 int fd;
65
66 if (preread_count < sizeof(buf)) {
67 fd = atomic_load(&logger_list->fd);
68 if (fd <= 0) {
69 return -EBADF;
70 }
71 ret = TEMP_FAILURE_RETRY(read(fd, &buf.p.magic + preread_count, sizeof(buf) - preread_count));
72 if (ret < 0) {
73 return -errno;
74 }
75 preread_count += ret;
76 }
77 if (preread_count != sizeof(buf)) {
78 return preread_count ? -EIO : -EAGAIN;
79 }
80 if ((buf.p.magic != LOGGER_MAGIC) || (buf.p.len <= sizeof(buf)) ||
81 (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) ||
82 !__android_log_id_is_valid(static_cast<log_id_t>(buf.l.id)) ||
83 (buf.l.realtime.tv_nsec >= NS_PER_SEC) ||
84 ((buf.l.id != LOG_ID_EVENTS) && (buf.l.id != LOG_ID_SECURITY) &&
85 ((buf.prio == ANDROID_LOG_UNKNOWN) || (buf.prio == ANDROID_LOG_DEFAULT) ||
86 (buf.prio >= ANDROID_LOG_SILENT)))) {
87 do {
88 memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
89 } while (preread_count && (buf.p.magic != LOGGER_MAGIC));
90 continue;
91 }
92 preread_count = 0;
93
94 if ((logger_list->log_mask & (1 << buf.l.id)) &&
95 ((!logger_list->start.tv_sec && !logger_list->start.tv_nsec) ||
96 ((logger_list->start.tv_sec <= buf.l.realtime.tv_sec) &&
97 ((logger_list->start.tv_sec != buf.l.realtime.tv_sec) ||
98 (logger_list->start.tv_nsec <= buf.l.realtime.tv_nsec)))) &&
99 (!logger_list->pid || (logger_list->pid == buf.p.pid))) {
100 char* msg = reinterpret_cast<char*>(&log_msg->entry) + sizeof(log_msg->entry);
101 *msg = buf.prio;
102 fd = atomic_load(&logger_list->fd);
103 if (fd <= 0) {
104 return -EBADF;
105 }
106 ret = TEMP_FAILURE_RETRY(read(fd, msg + sizeof(buf.prio), buf.p.len - sizeof(buf)));
107 if (ret < 0) {
108 return -errno;
109 }
110 if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
111 return -EIO;
112 }
113
114 log_msg->entry.len = buf.p.len - sizeof(buf) + sizeof(buf.prio);
115 log_msg->entry.hdr_size = sizeof(log_msg->entry);
116 log_msg->entry.pid = buf.p.pid;
117 log_msg->entry.tid = buf.l.tid;
118 log_msg->entry.sec = buf.l.realtime.tv_sec;
119 log_msg->entry.nsec = buf.l.realtime.tv_nsec;
120 log_msg->entry.lid = buf.l.id;
121 log_msg->entry.uid = buf.p.uid;
122
123 return ret + sizeof(buf.prio) + log_msg->entry.hdr_size;
124 }
125
126 fd = atomic_load(&logger_list->fd);
127 if (fd <= 0) {
128 return -EBADF;
129 }
130 current = TEMP_FAILURE_RETRY(lseek(fd, (off_t)0, SEEK_CUR));
131 if (current < 0) {
132 return -errno;
133 }
134 fd = atomic_load(&logger_list->fd);
135 if (fd <= 0) {
136 return -EBADF;
137 }
138 next = TEMP_FAILURE_RETRY(lseek(fd, (off_t)(buf.p.len - sizeof(buf)), SEEK_CUR));
139 if (next < 0) {
140 return -errno;
141 }
142 if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
143 return -EIO;
144 }
145 }
146 }
147
PmsgClose(struct logger_list * logger_list)148 void PmsgClose(struct logger_list* logger_list) {
149 int fd = atomic_exchange(&logger_list->fd, 0);
150 if (fd > 0) {
151 close(fd);
152 }
153 }
154
realloc_or_free(void * ptr,size_t new_size)155 static void* realloc_or_free(void* ptr, size_t new_size) {
156 void* result = realloc(ptr, new_size);
157 if (!result) {
158 free(ptr);
159 }
160 return result;
161 }
162
__android_log_pmsg_file_read(log_id_t logId,char prio,const char * prefix,__android_log_pmsg_file_read_fn fn,void * arg)163 ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* prefix,
164 __android_log_pmsg_file_read_fn fn, void* arg) {
165 ssize_t ret;
166 struct content {
167 struct listnode node;
168 struct logger_entry entry;
169 } * content;
170 struct names {
171 struct listnode node;
172 struct listnode content;
173 log_id_t id;
174 char prio;
175 char name[];
176 } * names;
177 struct listnode name_list;
178 struct listnode *node, *n;
179 size_t len, prefix_len;
180
181 if (!fn) {
182 return -EINVAL;
183 }
184
185 /* Add just enough clues in logger_list and transp to make API function */
186 struct logger_list logger_list = {
187 .mode = static_cast<int>(ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK),
188 .log_mask = (unsigned)-1};
189 logger_list.log_mask = (1 << logId);
190 logger_list.log_mask &= ~((1 << LOG_ID_KERNEL) | (1 << LOG_ID_EVENTS) | (1 << LOG_ID_SECURITY));
191 if (!logger_list.log_mask) {
192 return -EINVAL;
193 }
194
195 /* Initialize name list */
196 list_init(&name_list);
197
198 ret = SSIZE_MAX;
199
200 /* Validate incoming prefix, shift until it contains only 0 or 1 : or / */
201 prefix_len = 0;
202 if (prefix) {
203 const char *prev = NULL, *last = NULL, *cp = prefix;
204 while ((cp = strpbrk(cp, "/:"))) {
205 prev = last;
206 last = cp;
207 cp = cp + 1;
208 }
209 if (prev) {
210 prefix = prev + 1;
211 }
212 prefix_len = strlen(prefix);
213 }
214
215 /* Read the file content */
216 log_msg log_msg;
217 while (PmsgRead(&logger_list, &log_msg) > 0) {
218 const char* cp;
219 size_t hdr_size = log_msg.entry.hdr_size;
220
221 char* msg = (char*)&log_msg + hdr_size;
222 const char* split = NULL;
223
224 if (hdr_size != sizeof(log_msg.entry)) {
225 continue;
226 }
227 /* Check for invalid sequence number */
228 if (log_msg.entry.nsec % ANDROID_LOG_PMSG_FILE_SEQUENCE ||
229 (log_msg.entry.nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
230 ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE) {
231 continue;
232 }
233
234 /* Determine if it has <dirbase>:<filebase> format for tag */
235 len = log_msg.entry.len - sizeof(prio);
236 for (cp = msg + sizeof(prio); *cp && isprint(*cp) && !isspace(*cp) && --len; ++cp) {
237 if (*cp == ':') {
238 if (split) {
239 break;
240 }
241 split = cp;
242 }
243 }
244 if (*cp || !split) {
245 continue;
246 }
247
248 /* Filters */
249 if (prefix_len && strncmp(msg + sizeof(prio), prefix, prefix_len)) {
250 size_t offset;
251 /*
252 * Allow : to be a synonym for /
253 * Things we do dealing with const char * and do not alloc
254 */
255 split = strchr(prefix, ':');
256 if (split) {
257 continue;
258 }
259 split = strchr(prefix, '/');
260 if (!split) {
261 continue;
262 }
263 offset = split - prefix;
264 if ((msg[offset + sizeof(prio)] != ':') || strncmp(msg + sizeof(prio), prefix, offset)) {
265 continue;
266 }
267 ++offset;
268 if ((prefix_len > offset) &&
269 strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) {
270 continue;
271 }
272 }
273
274 if ((prio != ANDROID_LOG_ANY) && (*msg < prio)) {
275 continue;
276 }
277
278 /* check if there is an existing entry */
279 list_for_each(node, &name_list) {
280 names = node_to_item(node, struct names, node);
281 if (!strcmp(names->name, msg + sizeof(prio)) && names->id == log_msg.entry.lid &&
282 names->prio == *msg) {
283 break;
284 }
285 }
286
287 /* We do not have an existing entry, create and add one */
288 if (node == &name_list) {
289 static const char numbers[] = "0123456789";
290 unsigned long long nl;
291
292 len = strlen(msg + sizeof(prio)) + 1;
293 names = static_cast<struct names*>(calloc(1, sizeof(*names) + len));
294 if (!names) {
295 ret = -ENOMEM;
296 break;
297 }
298 strcpy(names->name, msg + sizeof(prio));
299 names->id = static_cast<log_id_t>(log_msg.entry.lid);
300 names->prio = *msg;
301 list_init(&names->content);
302 /*
303 * Insert in reverse numeric _then_ alpha sorted order as
304 * representative of log rotation:
305 *
306 * log.10
307 * klog.10
308 * . . .
309 * log.2
310 * klog.2
311 * log.1
312 * klog.1
313 * log
314 * klog
315 *
316 * thus when we present the content, we are provided the oldest
317 * first, which when 'refreshed' could spill off the end of the
318 * pmsg FIFO but retaining the newest data for last with best
319 * chances to survive.
320 */
321 nl = 0;
322 cp = strpbrk(names->name, numbers);
323 if (cp) {
324 nl = strtoull(cp, NULL, 10);
325 }
326 list_for_each_reverse(node, &name_list) {
327 struct names* a_name = node_to_item(node, struct names, node);
328 const char* r = a_name->name;
329 int compare = 0;
330
331 unsigned long long nr = 0;
332 cp = strpbrk(r, numbers);
333 if (cp) {
334 nr = strtoull(cp, NULL, 10);
335 }
336 if (nr != nl) {
337 compare = (nl > nr) ? 1 : -1;
338 }
339 if (compare == 0) {
340 compare = strcmp(names->name, r);
341 }
342 if (compare <= 0) {
343 break;
344 }
345 }
346 list_add_head(node, &names->node);
347 }
348
349 /* Remove any file fragments that match our sequence number */
350 list_for_each_safe(node, n, &names->content) {
351 content = node_to_item(node, struct content, node);
352 if (log_msg.entry.nsec == content->entry.nsec) {
353 list_remove(&content->node);
354 free(content);
355 }
356 }
357
358 /* Add content */
359 content = static_cast<struct content*>(
360 calloc(1, sizeof(content->node) + hdr_size + log_msg.entry.len));
361 if (!content) {
362 ret = -ENOMEM;
363 break;
364 }
365 memcpy(&content->entry, &log_msg.entry, hdr_size + log_msg.entry.len);
366
367 /* Insert in sequence number sorted order, to ease reconstruction */
368 list_for_each_reverse(node, &names->content) {
369 if ((node_to_item(node, struct content, node))->entry.nsec < log_msg.entry.nsec) {
370 break;
371 }
372 }
373 list_add_head(node, &content->node);
374 }
375 PmsgClose(&logger_list);
376
377 /* Progress through all the collected files */
378 list_for_each_safe(node, n, &name_list) {
379 struct listnode *content_node, *m;
380 char* buf;
381 size_t sequence, tag_len;
382
383 names = node_to_item(node, struct names, node);
384
385 /* Construct content into a linear buffer */
386 buf = NULL;
387 len = 0;
388 sequence = 0;
389 tag_len = strlen(names->name) + sizeof(char); /* tag + nul */
390 list_for_each_safe(content_node, m, &names->content) {
391 ssize_t add_len;
392
393 content = node_to_item(content_node, struct content, node);
394 add_len = content->entry.len - tag_len - sizeof(prio);
395 if (add_len <= 0) {
396 list_remove(content_node);
397 free(content);
398 continue;
399 }
400
401 if (!buf) {
402 buf = static_cast<char*>(malloc(sizeof(char)));
403 if (!buf) {
404 ret = -ENOMEM;
405 list_remove(content_node);
406 free(content);
407 continue;
408 }
409 *buf = '\0';
410 }
411
412 /* Missing sequence numbers */
413 while (sequence < content->entry.nsec) {
414 /* plus space for enforced nul */
415 buf = static_cast<char*>(realloc_or_free(buf, len + sizeof(char) + sizeof(char)));
416 if (!buf) {
417 break;
418 }
419 buf[len] = '\f'; /* Mark missing content with a form feed */
420 buf[++len] = '\0';
421 sequence += ANDROID_LOG_PMSG_FILE_SEQUENCE;
422 }
423 if (!buf) {
424 ret = -ENOMEM;
425 list_remove(content_node);
426 free(content);
427 continue;
428 }
429 /* plus space for enforced nul */
430 buf = static_cast<char*>(realloc_or_free(buf, len + add_len + sizeof(char)));
431 if (!buf) {
432 ret = -ENOMEM;
433 list_remove(content_node);
434 free(content);
435 continue;
436 }
437 memcpy(buf + len, (char*)&content->entry + content->entry.hdr_size + tag_len + sizeof(prio),
438 add_len);
439 len += add_len;
440 buf[len] = '\0'; /* enforce trailing hidden nul */
441 sequence = content->entry.nsec + ANDROID_LOG_PMSG_FILE_SEQUENCE;
442
443 list_remove(content_node);
444 free(content);
445 }
446 if (buf) {
447 if (len) {
448 /* Buffer contains enforced trailing nul just beyond length */
449 ssize_t r;
450 *strchr(names->name, ':') = '/'; /* Convert back to filename */
451 r = (*fn)(names->id, names->prio, names->name, buf, len, arg);
452 if ((ret >= 0) && (r > 0)) {
453 if (ret == SSIZE_MAX) {
454 ret = r;
455 } else {
456 ret += r;
457 }
458 } else if (r < ret) {
459 ret = r;
460 }
461 }
462 free(buf);
463 }
464 list_remove(node);
465 free(names);
466 }
467 return (ret == SSIZE_MAX) ? -ENOENT : ret;
468 }
469