blob: 26df0873f8015545c384e7160dedd01689003dc9 [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 Salyzyn8daa9af2014-04-28 14:07:23 -070040 prctl(PR_SET_NAME, "logd.reader");
41
Mark Salyzyn0175b072014-02-26 09:50:16 -080042 char buffer[255];
43
44 int len = read(cli->getSocket(), buffer, sizeof(buffer) - 1);
45 if (len <= 0) {
46 doSocketDelete(cli);
47 return false;
48 }
49 buffer[len] = '\0';
50
51 unsigned long tail = 0;
52 static const char _tail[] = " tail=";
53 char *cp = strstr(buffer, _tail);
54 if (cp) {
55 tail = atol(cp + sizeof(_tail) - 1);
56 }
57
Mark Salyzynfa3716b2014-02-14 16:05:05 -080058 log_time start(log_time::EPOCH);
59 static const char _start[] = " start=";
60 cp = strstr(buffer, _start);
61 if (cp) {
62 // Parse errors will result in current time
63 start.strptime(cp + sizeof(_start) - 1, "%s.%q");
64 }
65
Mark Salyzyn0175b072014-02-26 09:50:16 -080066 unsigned int logMask = -1;
67 static const char _logIds[] = " lids=";
68 cp = strstr(buffer, _logIds);
69 if (cp) {
70 logMask = 0;
71 cp += sizeof(_logIds) - 1;
72 while (*cp && *cp != '\0') {
73 int val = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -080074 while (isdigit(*cp)) {
75 val = val * 10 + *cp - '0';
Mark Salyzyn0175b072014-02-26 09:50:16 -080076 ++cp;
77 }
78 logMask |= 1 << val;
79 if (*cp != ',') {
80 break;
81 }
82 ++cp;
83 }
84 }
85
86 pid_t pid = 0;
87 static const char _pid[] = " pid=";
88 cp = strstr(buffer, _pid);
89 if (cp) {
90 pid = atol(cp + sizeof(_pid) - 1);
91 }
92
93 bool nonBlock = false;
94 if (strncmp(buffer, "dumpAndClose", 12) == 0) {
Mark Salyzynf669acb2014-09-16 09:19:47 -070095 // Allow writer to get some cycles, and wait for pending notifications
96 sched_yield();
97 LogTimeEntry::lock();
98 LogTimeEntry::unlock();
99 sched_yield();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800100 nonBlock = true;
101 }
102
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800103 // Convert realtime to monotonic time
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800104 if (start == log_time::EPOCH) {
105 start = LogTimeEntry::EPOCH;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800106 } else {
107 class LogFindStart {
108 const pid_t mPid;
109 const unsigned mLogMask;
110 bool startTimeSet;
111 log_time &start;
112 log_time last;
113
114 public:
115 LogFindStart(unsigned logMask, pid_t pid, log_time &start)
116 : mPid(pid)
117 , mLogMask(logMask)
118 , startTimeSet(false)
119 , start(start)
120 , last(LogTimeEntry::EPOCH)
121 { }
122
123 static bool callback(const LogBufferElement *element, void *obj) {
124 LogFindStart *me = reinterpret_cast<LogFindStart *>(obj);
125 if (!me->startTimeSet
126 && (!me->mPid || (me->mPid == element->getPid()))
127 && (me->mLogMask & (1 << element->getLogId()))) {
128 if (me->start == element->getRealTime()) {
129 me->start = element->getMonotonicTime();
130 me->startTimeSet = true;
131 } else {
132 if (me->start < element->getRealTime()) {
133 me->start = me->last;
134 me->startTimeSet = true;
135 }
136 me->last = element->getMonotonicTime();
137 }
138 }
139 return false;
140 }
141
142 bool found() { return startTimeSet; }
143 } logFindStart(logMask, pid, start);
144
145 logbuf().flushTo(cli, LogTimeEntry::EPOCH,
146 FlushCommand::hasReadLogs(cli),
147 logFindStart.callback, &logFindStart);
148
149 if (!logFindStart.found()) {
150 if (nonBlock) {
151 doSocketDelete(cli);
152 return false;
153 }
154 log_time now(CLOCK_MONOTONIC);
155 start = now;
156 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800157 }
158
159 FlushCommand command(*this, nonBlock, tail, logMask, pid, start);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800160 command.runSocketCommand(cli);
161 return true;
162}
163
164void LogReader::doSocketDelete(SocketClient *cli) {
165 LastLogTimes &times = mLogbuf.mTimes;
166 LogTimeEntry::lock();
167 LastLogTimes::iterator it = times.begin();
168 while(it != times.end()) {
169 LogTimeEntry *entry = (*it);
170 if (entry->mClient == cli) {
171 times.erase(it);
172 entry->release_Locked();
173 break;
174 }
175 it++;
176 }
177 LogTimeEntry::unlock();
178}
Mark Salyzyndfc47e82014-03-24 10:26:47 -0700179
180int LogReader::getLogSocket() {
181 static const char socketName[] = "logdr";
182 int sock = android_get_control_socket(socketName);
183
184 if (sock < 0) {
185 sock = socket_local_server(socketName,
186 ANDROID_SOCKET_NAMESPACE_RESERVED,
187 SOCK_SEQPACKET);
188 }
189
190 return sock;
191}