blob: 6d6931631f74a33978d00248c408bd5debf2c69a [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 Salyzynfa3add32016-12-29 07:26:30 -080018#include <inttypes.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080019#include <poll.h>
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070020#include <sys/prctl.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080021#include <sys/socket.h>
Mark Salyzyn5c77ad52016-02-23 08:55:43 -080022#include <sys/types.h>
Mark Salyzyndfc47e82014-03-24 10:26:47 -070023
Mark Salyzyn0175b072014-02-26 09:50:16 -080024#include <cutils/sockets.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070025#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080026
Mark Salyzyn0175b072014-02-26 09:50:16 -080027#include "FlushCommand.h"
Mark Salyzyn2ad0bd02016-02-23 08:55:43 -080028#include "LogBuffer.h"
29#include "LogBufferElement.h"
30#include "LogReader.h"
31#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080032
Mark Salyzyn501c3732017-03-10 14:31:54 -080033LogReader::LogReader(LogBuffer* logbuf)
34 : SocketListener(getLogSocket(), true), mLogbuf(*logbuf) {
Mark Salyzyn77187782015-05-12 15:21:31 -070035}
Mark Salyzyn0175b072014-02-26 09:50:16 -080036
37// When we are notified a new log entry is available, inform
38// all of our listening sockets.
39void LogReader::notifyNewLog() {
40 FlushCommand command(*this);
41 runOnEachSocket(&command);
42}
43
Mark Salyzyn501c3732017-03-10 14:31:54 -080044bool LogReader::onDataAvailable(SocketClient* cli) {
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070045 static bool name_set;
46 if (!name_set) {
47 prctl(PR_SET_NAME, "logd.reader");
48 name_set = true;
49 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070050
Mark Salyzyn0175b072014-02-26 09:50:16 -080051 char buffer[255];
52
53 int len = read(cli->getSocket(), buffer, sizeof(buffer) - 1);
54 if (len <= 0) {
55 doSocketDelete(cli);
56 return false;
57 }
58 buffer[len] = '\0';
59
60 unsigned long tail = 0;
61 static const char _tail[] = " tail=";
Mark Salyzyn501c3732017-03-10 14:31:54 -080062 char* cp = strstr(buffer, _tail);
Mark Salyzyn0175b072014-02-26 09:50:16 -080063 if (cp) {
64 tail = atol(cp + sizeof(_tail) - 1);
65 }
66
Mark Salyzynfa3716b2014-02-14 16:05:05 -080067 log_time start(log_time::EPOCH);
68 static const char _start[] = " start=";
69 cp = strstr(buffer, _start);
70 if (cp) {
71 // Parse errors will result in current time
72 start.strptime(cp + sizeof(_start) - 1, "%s.%q");
73 }
74
Mark Salyzynb75cce02015-11-30 11:35:56 -080075 uint64_t timeout = 0;
76 static const char _timeout[] = " timeout=";
77 cp = strstr(buffer, _timeout);
78 if (cp) {
79 timeout = atol(cp + sizeof(_timeout) - 1) * NS_PER_SEC +
80 log_time(CLOCK_REALTIME).nsec();
81 }
82
Mark Salyzyn0175b072014-02-26 09:50:16 -080083 unsigned int logMask = -1;
84 static const char _logIds[] = " lids=";
85 cp = strstr(buffer, _logIds);
86 if (cp) {
87 logMask = 0;
88 cp += sizeof(_logIds) - 1;
89 while (*cp && *cp != '\0') {
90 int val = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -080091 while (isdigit(*cp)) {
92 val = val * 10 + *cp - '0';
Mark Salyzyn0175b072014-02-26 09:50:16 -080093 ++cp;
94 }
95 logMask |= 1 << val;
96 if (*cp != ',') {
97 break;
98 }
99 ++cp;
100 }
101 }
102
103 pid_t pid = 0;
104 static const char _pid[] = " pid=";
105 cp = strstr(buffer, _pid);
106 if (cp) {
107 pid = atol(cp + sizeof(_pid) - 1);
108 }
109
110 bool nonBlock = false;
Mark Salyzyn0eeb06b2016-12-02 10:08:48 -0800111 if (!fastcmp<strncmp>(buffer, "dumpAndClose", 12)) {
Mark Salyzynf669acb2014-09-16 09:19:47 -0700112 // Allow writer to get some cycles, and wait for pending notifications
113 sched_yield();
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700114 LogTimeEntry::wrlock();
Mark Salyzynf669acb2014-09-16 09:19:47 -0700115 LogTimeEntry::unlock();
116 sched_yield();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800117 nonBlock = true;
118 }
119
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800120 log_time sequence = start;
121 //
122 // This somewhat expensive data validation operation is required
123 // for non-blocking, with timeout. The incoming timestamp must be
124 // in range of the list, if not, return immediately. This is
125 // used to prevent us from from getting stuck in timeout processing
126 // with an invalid time.
127 //
128 // Find if time is really present in the logs, monotonic or real, implicit
129 // conversion from monotonic or real as necessary to perform the check.
130 // Exit in the check loop ASAP as you find a transition from older to
131 // newer, but use the last entry found to ensure overlap.
132 //
133 if (nonBlock && (sequence != log_time::EPOCH) && timeout) {
134 class LogFindStart { // A lambda by another name
135 private:
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800136 const pid_t mPid;
137 const unsigned mLogMask;
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800138 bool mStartTimeSet;
139 log_time mStart;
140 log_time& mSequence;
141 log_time mLast;
142 bool mIsMonotonic;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800143
Mark Salyzyn501c3732017-03-10 14:31:54 -0800144 public:
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800145 LogFindStart(pid_t pid, unsigned logMask, log_time& sequence,
146 bool isMonotonic)
Mark Salyzyn501c3732017-03-10 14:31:54 -0800147 : mPid(pid),
148 mLogMask(logMask),
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800149 mStartTimeSet(false),
150 mStart(sequence),
151 mSequence(sequence),
152 mLast(sequence),
153 mIsMonotonic(isMonotonic) {
Mark Salyzyn77187782015-05-12 15:21:31 -0700154 }
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800155
Mark Salyzyn501c3732017-03-10 14:31:54 -0800156 static int callback(const LogBufferElement* element, void* obj) {
157 LogFindStart* me = reinterpret_cast<LogFindStart*>(obj);
158 if ((!me->mPid || (me->mPid == element->getPid())) &&
159 (me->mLogMask & (1 << element->getLogId()))) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800160 log_time real = element->getRealTime();
161 if (me->mStart == real) {
162 me->mSequence = real;
163 me->mStartTimeSet = true;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800164 return -1;
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800165 } else if (!me->mIsMonotonic || android::isMonotonic(real)) {
166 if (me->mStart < real) {
167 me->mSequence = me->mLast;
168 me->mStartTimeSet = true;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800169 return -1;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800170 }
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800171 me->mLast = real;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700172 } else {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800173 me->mLast = real;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800174 }
175 }
176 return false;
177 }
178
Mark Salyzyn501c3732017-03-10 14:31:54 -0800179 bool found() {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800180 return mStartTimeSet;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800181 }
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800182
183 } logFindStart(pid, logMask, sequence,
Mark Salyzynb6bee332015-09-08 08:56:32 -0700184 logbuf().isMonotonic() && android::isMonotonic(start));
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800185
Mark Salyzynae2abf12017-03-31 10:48:39 -0700186 logbuf().flushTo(cli, sequence, nullptr, FlushCommand::hasReadLogs(cli),
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800187 FlushCommand::hasSecurityLogs(cli),
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800188 logFindStart.callback, &logFindStart);
189
190 if (!logFindStart.found()) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800191 doSocketDelete(cli);
192 return false;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800193 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800194 }
195
Mark Salyzynfa3add32016-12-29 07:26:30 -0800196 android::prdebug(
197 "logdr: UID=%d GID=%d PID=%d %c tail=%lu logMask=%x pid=%d "
198 "start=%" PRIu64 "ns timeout=%" PRIu64 "ns\n",
199 cli->getUid(), cli->getGid(), cli->getPid(), nonBlock ? 'n' : 'b', tail,
200 logMask, (int)pid, sequence.nsec(), timeout);
201
Mark Salyzynb75cce02015-11-30 11:35:56 -0800202 FlushCommand command(*this, nonBlock, tail, logMask, pid, sequence, timeout);
Mark Salyzyn5c77ad52016-02-23 08:55:43 -0800203
204 // Set acceptable upper limit to wait for slow reader processing b/27242723
205 struct timeval t = { LOGD_SNDTIMEO, 0 };
Mark Salyzyn501c3732017-03-10 14:31:54 -0800206 setsockopt(cli->getSocket(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&t,
207 sizeof(t));
Mark Salyzyn5c77ad52016-02-23 08:55:43 -0800208
Mark Salyzyn0175b072014-02-26 09:50:16 -0800209 command.runSocketCommand(cli);
210 return true;
211}
212
Mark Salyzyn501c3732017-03-10 14:31:54 -0800213void LogReader::doSocketDelete(SocketClient* cli) {
214 LastLogTimes& times = mLogbuf.mTimes;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700215 LogTimeEntry::wrlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800216 LastLogTimes::iterator it = times.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800217 while (it != times.end()) {
218 LogTimeEntry* entry = (*it);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800219 if (entry->mClient == cli) {
220 times.erase(it);
221 entry->release_Locked();
222 break;
223 }
224 it++;
225 }
226 LogTimeEntry::unlock();
227}
Mark Salyzyndfc47e82014-03-24 10:26:47 -0700228
229int LogReader::getLogSocket() {
230 static const char socketName[] = "logdr";
231 int sock = android_get_control_socket(socketName);
232
233 if (sock < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800234 sock = socket_local_server(
235 socketName, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET);
Mark Salyzyndfc47e82014-03-24 10:26:47 -0700236 }
237
238 return sock;
239}