blob: af19279a40c94bcf0d4e1d56e93837e9c5aeb0d5 [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 Salyzyn5c77ad52016-02-23 08:55:43 -080021#include <sys/types.h>
Mark Salyzyndfc47e82014-03-24 10:26:47 -070022
Mark Salyzyn0175b072014-02-26 09:50:16 -080023#include <cutils/sockets.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070024#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080025
Mark Salyzyn0175b072014-02-26 09:50:16 -080026#include "FlushCommand.h"
Mark Salyzyn2ad0bd02016-02-23 08:55:43 -080027#include "LogBuffer.h"
28#include "LogBufferElement.h"
29#include "LogReader.h"
30#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080031
Mark Salyzyn501c3732017-03-10 14:31:54 -080032LogReader::LogReader(LogBuffer* logbuf)
33 : SocketListener(getLogSocket(), true), mLogbuf(*logbuf) {
Mark Salyzyn77187782015-05-12 15:21:31 -070034}
Mark Salyzyn0175b072014-02-26 09:50:16 -080035
36// When we are notified a new log entry is available, inform
37// all of our listening sockets.
38void LogReader::notifyNewLog() {
39 FlushCommand command(*this);
40 runOnEachSocket(&command);
41}
42
Mark Salyzyn501c3732017-03-10 14:31:54 -080043bool LogReader::onDataAvailable(SocketClient* cli) {
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070044 static bool name_set;
45 if (!name_set) {
46 prctl(PR_SET_NAME, "logd.reader");
47 name_set = true;
48 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070049
Mark Salyzyn0175b072014-02-26 09:50:16 -080050 char buffer[255];
51
52 int len = read(cli->getSocket(), buffer, sizeof(buffer) - 1);
53 if (len <= 0) {
54 doSocketDelete(cli);
55 return false;
56 }
57 buffer[len] = '\0';
58
59 unsigned long tail = 0;
60 static const char _tail[] = " tail=";
Mark Salyzyn501c3732017-03-10 14:31:54 -080061 char* cp = strstr(buffer, _tail);
Mark Salyzyn0175b072014-02-26 09:50:16 -080062 if (cp) {
63 tail = atol(cp + sizeof(_tail) - 1);
64 }
65
Mark Salyzynfa3716b2014-02-14 16:05:05 -080066 log_time start(log_time::EPOCH);
67 static const char _start[] = " start=";
68 cp = strstr(buffer, _start);
69 if (cp) {
70 // Parse errors will result in current time
71 start.strptime(cp + sizeof(_start) - 1, "%s.%q");
72 }
73
Mark Salyzynb75cce02015-11-30 11:35:56 -080074 uint64_t timeout = 0;
75 static const char _timeout[] = " timeout=";
76 cp = strstr(buffer, _timeout);
77 if (cp) {
78 timeout = atol(cp + sizeof(_timeout) - 1) * NS_PER_SEC +
79 log_time(CLOCK_REALTIME).nsec();
80 }
81
Mark Salyzyn0175b072014-02-26 09:50:16 -080082 unsigned int logMask = -1;
83 static const char _logIds[] = " lids=";
84 cp = strstr(buffer, _logIds);
85 if (cp) {
86 logMask = 0;
87 cp += sizeof(_logIds) - 1;
88 while (*cp && *cp != '\0') {
89 int val = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -080090 while (isdigit(*cp)) {
91 val = val * 10 + *cp - '0';
Mark Salyzyn0175b072014-02-26 09:50:16 -080092 ++cp;
93 }
94 logMask |= 1 << val;
95 if (*cp != ',') {
96 break;
97 }
98 ++cp;
99 }
100 }
101
102 pid_t pid = 0;
103 static const char _pid[] = " pid=";
104 cp = strstr(buffer, _pid);
105 if (cp) {
106 pid = atol(cp + sizeof(_pid) - 1);
107 }
108
109 bool nonBlock = false;
Mark Salyzyn0eeb06b2016-12-02 10:08:48 -0800110 if (!fastcmp<strncmp>(buffer, "dumpAndClose", 12)) {
Mark Salyzynf669acb2014-09-16 09:19:47 -0700111 // Allow writer to get some cycles, and wait for pending notifications
112 sched_yield();
113 LogTimeEntry::lock();
114 LogTimeEntry::unlock();
115 sched_yield();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800116 nonBlock = true;
117 }
118
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800119 log_time sequence = start;
120 //
121 // This somewhat expensive data validation operation is required
122 // for non-blocking, with timeout. The incoming timestamp must be
123 // in range of the list, if not, return immediately. This is
124 // used to prevent us from from getting stuck in timeout processing
125 // with an invalid time.
126 //
127 // Find if time is really present in the logs, monotonic or real, implicit
128 // conversion from monotonic or real as necessary to perform the check.
129 // Exit in the check loop ASAP as you find a transition from older to
130 // newer, but use the last entry found to ensure overlap.
131 //
132 if (nonBlock && (sequence != log_time::EPOCH) && timeout) {
133 class LogFindStart { // A lambda by another name
134 private:
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800135 const pid_t mPid;
136 const unsigned mLogMask;
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800137 bool mStartTimeSet;
138 log_time mStart;
139 log_time& mSequence;
140 log_time mLast;
141 bool mIsMonotonic;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800142
Mark Salyzyn501c3732017-03-10 14:31:54 -0800143 public:
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800144 LogFindStart(pid_t pid, unsigned logMask, log_time& sequence,
145 bool isMonotonic)
Mark Salyzyn501c3732017-03-10 14:31:54 -0800146 : mPid(pid),
147 mLogMask(logMask),
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800148 mStartTimeSet(false),
149 mStart(sequence),
150 mSequence(sequence),
151 mLast(sequence),
152 mIsMonotonic(isMonotonic) {
Mark Salyzyn77187782015-05-12 15:21:31 -0700153 }
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800154
Mark Salyzyn501c3732017-03-10 14:31:54 -0800155 static int callback(const LogBufferElement* element, void* obj) {
156 LogFindStart* me = reinterpret_cast<LogFindStart*>(obj);
157 if ((!me->mPid || (me->mPid == element->getPid())) &&
158 (me->mLogMask & (1 << element->getLogId()))) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800159 log_time real = element->getRealTime();
160 if (me->mStart == real) {
161 me->mSequence = real;
162 me->mStartTimeSet = true;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800163 return -1;
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800164 } else if (!me->mIsMonotonic || android::isMonotonic(real)) {
165 if (me->mStart < real) {
166 me->mSequence = me->mLast;
167 me->mStartTimeSet = true;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800168 return -1;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800169 }
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800170 me->mLast = real;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700171 } else {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800172 me->mLast = real;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800173 }
174 }
175 return false;
176 }
177
Mark Salyzyn501c3732017-03-10 14:31:54 -0800178 bool found() {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800179 return mStartTimeSet;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800180 }
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800181
182 } logFindStart(pid, logMask, sequence,
Mark Salyzynb6bee332015-09-08 08:56:32 -0700183 logbuf().isMonotonic() && android::isMonotonic(start));
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800184
Mark Salyzynae2abf12017-03-31 10:48:39 -0700185 logbuf().flushTo(cli, sequence, nullptr, FlushCommand::hasReadLogs(cli),
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800186 FlushCommand::hasSecurityLogs(cli),
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800187 logFindStart.callback, &logFindStart);
188
189 if (!logFindStart.found()) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800190 doSocketDelete(cli);
191 return false;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800192 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800193 }
194
Mark Salyzynb75cce02015-11-30 11:35:56 -0800195 FlushCommand command(*this, nonBlock, tail, logMask, pid, sequence, timeout);
Mark Salyzyn5c77ad52016-02-23 08:55:43 -0800196
197 // Set acceptable upper limit to wait for slow reader processing b/27242723
198 struct timeval t = { LOGD_SNDTIMEO, 0 };
Mark Salyzyn501c3732017-03-10 14:31:54 -0800199 setsockopt(cli->getSocket(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&t,
200 sizeof(t));
Mark Salyzyn5c77ad52016-02-23 08:55:43 -0800201
Mark Salyzyn0175b072014-02-26 09:50:16 -0800202 command.runSocketCommand(cli);
203 return true;
204}
205
Mark Salyzyn501c3732017-03-10 14:31:54 -0800206void LogReader::doSocketDelete(SocketClient* cli) {
207 LastLogTimes& times = mLogbuf.mTimes;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800208 LogTimeEntry::lock();
209 LastLogTimes::iterator it = times.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800210 while (it != times.end()) {
211 LogTimeEntry* entry = (*it);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800212 if (entry->mClient == cli) {
213 times.erase(it);
214 entry->release_Locked();
215 break;
216 }
217 it++;
218 }
219 LogTimeEntry::unlock();
220}
Mark Salyzyndfc47e82014-03-24 10:26:47 -0700221
222int LogReader::getLogSocket() {
223 static const char socketName[] = "logdr";
224 int sock = android_get_control_socket(socketName);
225
226 if (sock < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800227 sock = socket_local_server(
228 socketName, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET);
Mark Salyzyndfc47e82014-03-24 10:26:47 -0700229 }
230
231 return sock;
232}