blob: 667a3f249293729e14ce72e739fb77849f38911e [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
Mark Salyzyn77187782015-05-12 15:21:31 -070027LogReader::LogReader(LogBuffer *logbuf) :
28 SocketListener(getLogSocket(), true),
29 mLogbuf(*logbuf) {
30}
Mark Salyzyn0175b072014-02-26 09:50:16 -080031
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 Salyzynb75cce02015-11-30 11:35:56 -080070 uint64_t timeout = 0;
71 static const char _timeout[] = " timeout=";
72 cp = strstr(buffer, _timeout);
73 if (cp) {
74 timeout = atol(cp + sizeof(_timeout) - 1) * NS_PER_SEC +
75 log_time(CLOCK_REALTIME).nsec();
76 }
77
Mark Salyzyn0175b072014-02-26 09:50:16 -080078 unsigned int logMask = -1;
79 static const char _logIds[] = " lids=";
80 cp = strstr(buffer, _logIds);
81 if (cp) {
82 logMask = 0;
83 cp += sizeof(_logIds) - 1;
84 while (*cp && *cp != '\0') {
85 int val = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -080086 while (isdigit(*cp)) {
87 val = val * 10 + *cp - '0';
Mark Salyzyn0175b072014-02-26 09:50:16 -080088 ++cp;
89 }
90 logMask |= 1 << val;
91 if (*cp != ',') {
92 break;
93 }
94 ++cp;
95 }
96 }
97
98 pid_t pid = 0;
99 static const char _pid[] = " pid=";
100 cp = strstr(buffer, _pid);
101 if (cp) {
102 pid = atol(cp + sizeof(_pid) - 1);
103 }
104
105 bool nonBlock = false;
Mark Salyzynddda2122015-10-02 09:22:52 -0700106 if (!fast<strncmp>(buffer, "dumpAndClose", 12)) {
Mark Salyzynf669acb2014-09-16 09:19:47 -0700107 // Allow writer to get some cycles, and wait for pending notifications
108 sched_yield();
109 LogTimeEntry::lock();
110 LogTimeEntry::unlock();
111 sched_yield();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800112 nonBlock = true;
113 }
114
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800115 uint64_t sequence = 1;
116 // Convert realtime to sequence number
117 if (start != log_time::EPOCH) {
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800118 class LogFindStart {
119 const pid_t mPid;
120 const unsigned mLogMask;
121 bool startTimeSet;
122 log_time &start;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800123 uint64_t &sequence;
124 uint64_t last;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700125 bool isMonotonic;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800126
127 public:
Mark Salyzynb6bee332015-09-08 08:56:32 -0700128 LogFindStart(unsigned logMask, pid_t pid, log_time &start, uint64_t &sequence, bool isMonotonic) :
Mark Salyzyn77187782015-05-12 15:21:31 -0700129 mPid(pid),
130 mLogMask(logMask),
131 startTimeSet(false),
132 start(start),
133 sequence(sequence),
Mark Salyzynb6bee332015-09-08 08:56:32 -0700134 last(sequence),
135 isMonotonic(isMonotonic) {
Mark Salyzyn77187782015-05-12 15:21:31 -0700136 }
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800137
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800138 static int callback(const LogBufferElement *element, void *obj) {
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800139 LogFindStart *me = reinterpret_cast<LogFindStart *>(obj);
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800140 if ((!me->mPid || (me->mPid == element->getPid()))
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800141 && (me->mLogMask & (1 << element->getLogId()))) {
142 if (me->start == element->getRealTime()) {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800143 me->sequence = element->getSequence();
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800144 me->startTimeSet = true;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800145 return -1;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700146 } else if (!me->isMonotonic ||
147 android::isMonotonic(element->getRealTime())) {
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800148 if (me->start < element->getRealTime()) {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800149 me->sequence = me->last;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800150 me->startTimeSet = true;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800151 return -1;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800152 }
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800153 me->last = element->getSequence();
Mark Salyzynb6bee332015-09-08 08:56:32 -0700154 } else {
155 me->last = element->getSequence();
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800156 }
157 }
158 return false;
159 }
160
161 bool found() { return startTimeSet; }
Mark Salyzynb6bee332015-09-08 08:56:32 -0700162 } logFindStart(logMask, pid, start, sequence,
163 logbuf().isMonotonic() && android::isMonotonic(start));
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800164
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800165 logbuf().flushTo(cli, sequence, FlushCommand::hasReadLogs(cli),
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800166 FlushCommand::hasSecurityLogs(cli),
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800167 logFindStart.callback, &logFindStart);
168
169 if (!logFindStart.found()) {
170 if (nonBlock) {
171 doSocketDelete(cli);
172 return false;
173 }
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800174 sequence = LogBufferElement::getCurrentSequence();
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800175 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800176 }
177
Mark Salyzynb75cce02015-11-30 11:35:56 -0800178 FlushCommand command(*this, nonBlock, tail, logMask, pid, sequence, timeout);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800179 command.runSocketCommand(cli);
180 return true;
181}
182
183void LogReader::doSocketDelete(SocketClient *cli) {
184 LastLogTimes &times = mLogbuf.mTimes;
185 LogTimeEntry::lock();
186 LastLogTimes::iterator it = times.begin();
187 while(it != times.end()) {
188 LogTimeEntry *entry = (*it);
189 if (entry->mClient == cli) {
190 times.erase(it);
191 entry->release_Locked();
192 break;
193 }
194 it++;
195 }
196 LogTimeEntry::unlock();
197}
Mark Salyzyndfc47e82014-03-24 10:26:47 -0700198
199int LogReader::getLogSocket() {
200 static const char socketName[] = "logdr";
201 int sock = android_get_control_socket(socketName);
202
203 if (sock < 0) {
204 sock = socket_local_server(socketName,
205 ANDROID_SOCKET_NAMESPACE_RESERVED,
206 SOCK_SEQPACKET);
207 }
208
209 return sock;
210}