blob: 745e8477ec431957d8b7b966a65d3fbf0c458bb9 [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2013 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 Salyzynfa3716b2014-02-14 16:05:05 -080017#include <ctype.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080018#include <poll.h>
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070019#include <sys/prctl.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080020#include <sys/socket.h>
Mark Salyzyndfc47e82014-03-24 10:26:47 -070021
Mark Salyzyn0175b072014-02-26 09:50:16 -080022#include <cutils/sockets.h>
23
24#include "LogReader.h"
25#include "FlushCommand.h"
26
27LogReader::LogReader(LogBuffer *logbuf)
Mark Salyzyndfc47e82014-03-24 10:26:47 -070028 : SocketListener(getLogSocket(), true)
Mark Salyzyn0175b072014-02-26 09:50:16 -080029 , mLogbuf(*logbuf)
30{ }
31
32// When we are notified a new log entry is available, inform
33// all of our listening sockets.
34void LogReader::notifyNewLog() {
35 FlushCommand command(*this);
36 runOnEachSocket(&command);
37}
38
39bool LogReader::onDataAvailable(SocketClient *cli) {
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070040 static bool name_set;
41 if (!name_set) {
42 prctl(PR_SET_NAME, "logd.reader");
43 name_set = true;
44 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070045
Mark Salyzyn0175b072014-02-26 09:50:16 -080046 char buffer[255];
47
48 int len = read(cli->getSocket(), buffer, sizeof(buffer) - 1);
49 if (len <= 0) {
50 doSocketDelete(cli);
51 return false;
52 }
53 buffer[len] = '\0';
54
55 unsigned long tail = 0;
56 static const char _tail[] = " tail=";
57 char *cp = strstr(buffer, _tail);
58 if (cp) {
59 tail = atol(cp + sizeof(_tail) - 1);
60 }
61
Mark Salyzynfa3716b2014-02-14 16:05:05 -080062 log_time start(log_time::EPOCH);
63 static const char _start[] = " start=";
64 cp = strstr(buffer, _start);
65 if (cp) {
66 // Parse errors will result in current time
67 start.strptime(cp + sizeof(_start) - 1, "%s.%q");
68 }
69
Mark Salyzyn0175b072014-02-26 09:50:16 -080070 unsigned int logMask = -1;
71 static const char _logIds[] = " lids=";
72 cp = strstr(buffer, _logIds);
73 if (cp) {
74 logMask = 0;
75 cp += sizeof(_logIds) - 1;
76 while (*cp && *cp != '\0') {
77 int val = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -080078 while (isdigit(*cp)) {
79 val = val * 10 + *cp - '0';
Mark Salyzyn0175b072014-02-26 09:50:16 -080080 ++cp;
81 }
82 logMask |= 1 << val;
83 if (*cp != ',') {
84 break;
85 }
86 ++cp;
87 }
88 }
89
90 pid_t pid = 0;
91 static const char _pid[] = " pid=";
92 cp = strstr(buffer, _pid);
93 if (cp) {
94 pid = atol(cp + sizeof(_pid) - 1);
95 }
96
97 bool nonBlock = false;
98 if (strncmp(buffer, "dumpAndClose", 12) == 0) {
Mark Salyzynf669acb2014-09-16 09:19:47 -070099 // Allow writer to get some cycles, and wait for pending notifications
100 sched_yield();
101 LogTimeEntry::lock();
102 LogTimeEntry::unlock();
103 sched_yield();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800104 nonBlock = true;
105 }
106
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800107 uint64_t sequence = 1;
108 // Convert realtime to sequence number
109 if (start != log_time::EPOCH) {
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800110 class LogFindStart {
111 const pid_t mPid;
112 const unsigned mLogMask;
113 bool startTimeSet;
114 log_time &start;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800115 uint64_t &sequence;
116 uint64_t last;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800117
118 public:
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800119 LogFindStart(unsigned logMask, pid_t pid, log_time &start, uint64_t &sequence)
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800120 : mPid(pid)
121 , mLogMask(logMask)
122 , startTimeSet(false)
123 , start(start)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800124 , sequence(sequence)
125 , last(sequence)
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800126 { }
127
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800128 static int callback(const LogBufferElement *element, void *obj) {
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800129 LogFindStart *me = reinterpret_cast<LogFindStart *>(obj);
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800130 if ((!me->mPid || (me->mPid == element->getPid()))
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800131 && (me->mLogMask & (1 << element->getLogId()))) {
132 if (me->start == element->getRealTime()) {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800133 me->sequence = element->getSequence();
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800134 me->startTimeSet = true;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800135 return -1;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800136 } else {
137 if (me->start < element->getRealTime()) {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800138 me->sequence = me->last;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800139 me->startTimeSet = true;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800140 return -1;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800141 }
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800142 me->last = element->getSequence();
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800143 }
144 }
145 return false;
146 }
147
148 bool found() { return startTimeSet; }
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800149 } logFindStart(logMask, pid, start, sequence);
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800150
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800151 logbuf().flushTo(cli, sequence, FlushCommand::hasReadLogs(cli),
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800152 logFindStart.callback, &logFindStart);
153
154 if (!logFindStart.found()) {
155 if (nonBlock) {
156 doSocketDelete(cli);
157 return false;
158 }
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800159 sequence = LogBufferElement::getCurrentSequence();
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800160 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800161 }
162
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800163 FlushCommand command(*this, nonBlock, tail, logMask, pid, sequence);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800164 command.runSocketCommand(cli);
165 return true;
166}
167
168void LogReader::doSocketDelete(SocketClient *cli) {
169 LastLogTimes &times = mLogbuf.mTimes;
170 LogTimeEntry::lock();
171 LastLogTimes::iterator it = times.begin();
172 while(it != times.end()) {
173 LogTimeEntry *entry = (*it);
174 if (entry->mClient == cli) {
175 times.erase(it);
176 entry->release_Locked();
177 break;
178 }
179 it++;
180 }
181 LogTimeEntry::unlock();
182}
Mark Salyzyndfc47e82014-03-24 10:26:47 -0700183
184int LogReader::getLogSocket() {
185 static const char socketName[] = "logdr";
186 int sock = android_get_control_socket(socketName);
187
188 if (sock < 0) {
189 sock = socket_local_server(socketName,
190 ANDROID_SOCKET_NAMESPACE_RESERVED,
191 SOCK_SEQPACKET);
192 }
193
194 return sock;
195}