blob: d52d45976e12236c1998d5a82f0be2ade592c793 [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
Hao Wangf6e22962017-12-04 14:10:40 +080038// listening sockets who are watching this entry's log id.
39void LogReader::notifyNewLog(log_mask_t logMask) {
40 FlushCommand command(*this, logMask);
Mark Salyzyn0175b072014-02-26 09:50:16 -080041 runOnEachSocket(&command);
42}
43
Tom Cherry4f227862018-10-08 17:33:50 -070044// Note returning false will release the SocketClient instance.
Mark Salyzyn501c3732017-03-10 14:31:54 -080045bool LogReader::onDataAvailable(SocketClient* cli) {
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070046 static bool name_set;
47 if (!name_set) {
48 prctl(PR_SET_NAME, "logd.reader");
49 name_set = true;
50 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070051
Mark Salyzyn0175b072014-02-26 09:50:16 -080052 char buffer[255];
53
54 int len = read(cli->getSocket(), buffer, sizeof(buffer) - 1);
55 if (len <= 0) {
56 doSocketDelete(cli);
57 return false;
58 }
59 buffer[len] = '\0';
60
Tom Cherry4f227862018-10-08 17:33:50 -070061 // Clients are only allowed to send one command, disconnect them if they
62 // send another.
63 LogTimeEntry::wrlock();
64 for (const auto& entry : mLogbuf.mTimes) {
65 if (entry->mClient == cli) {
66 entry->release_Locked();
67 LogTimeEntry::unlock();
68 return false;
69 }
70 }
71 LogTimeEntry::unlock();
72
Mark Salyzyn0175b072014-02-26 09:50:16 -080073 unsigned long tail = 0;
74 static const char _tail[] = " tail=";
Mark Salyzyn501c3732017-03-10 14:31:54 -080075 char* cp = strstr(buffer, _tail);
Mark Salyzyn0175b072014-02-26 09:50:16 -080076 if (cp) {
77 tail = atol(cp + sizeof(_tail) - 1);
78 }
79
Mark Salyzynfa3716b2014-02-14 16:05:05 -080080 log_time start(log_time::EPOCH);
81 static const char _start[] = " start=";
82 cp = strstr(buffer, _start);
83 if (cp) {
84 // Parse errors will result in current time
85 start.strptime(cp + sizeof(_start) - 1, "%s.%q");
86 }
87
Mark Salyzynb75cce02015-11-30 11:35:56 -080088 uint64_t timeout = 0;
89 static const char _timeout[] = " timeout=";
90 cp = strstr(buffer, _timeout);
91 if (cp) {
Tom Cherryc9fa42c2020-04-08 10:37:09 -070092 timeout = atol(cp + sizeof(_timeout) - 1) * NS_PER_SEC + log_time(CLOCK_MONOTONIC).nsec();
Mark Salyzynb75cce02015-11-30 11:35:56 -080093 }
94
Mark Salyzyn0175b072014-02-26 09:50:16 -080095 unsigned int logMask = -1;
96 static const char _logIds[] = " lids=";
97 cp = strstr(buffer, _logIds);
98 if (cp) {
99 logMask = 0;
100 cp += sizeof(_logIds) - 1;
101 while (*cp && *cp != '\0') {
102 int val = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800103 while (isdigit(*cp)) {
104 val = val * 10 + *cp - '0';
Mark Salyzyn0175b072014-02-26 09:50:16 -0800105 ++cp;
106 }
107 logMask |= 1 << val;
108 if (*cp != ',') {
109 break;
110 }
111 ++cp;
112 }
113 }
114
115 pid_t pid = 0;
116 static const char _pid[] = " pid=";
117 cp = strstr(buffer, _pid);
118 if (cp) {
119 pid = atol(cp + sizeof(_pid) - 1);
120 }
121
122 bool nonBlock = false;
Mark Salyzyn0eeb06b2016-12-02 10:08:48 -0800123 if (!fastcmp<strncmp>(buffer, "dumpAndClose", 12)) {
Mark Salyzynf669acb2014-09-16 09:19:47 -0700124 // Allow writer to get some cycles, and wait for pending notifications
125 sched_yield();
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700126 LogTimeEntry::wrlock();
Mark Salyzynf669acb2014-09-16 09:19:47 -0700127 LogTimeEntry::unlock();
128 sched_yield();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800129 nonBlock = true;
130 }
131
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800132 log_time sequence = start;
133 //
134 // This somewhat expensive data validation operation is required
135 // for non-blocking, with timeout. The incoming timestamp must be
136 // in range of the list, if not, return immediately. This is
137 // used to prevent us from from getting stuck in timeout processing
138 // with an invalid time.
139 //
140 // Find if time is really present in the logs, monotonic or real, implicit
141 // conversion from monotonic or real as necessary to perform the check.
142 // Exit in the check loop ASAP as you find a transition from older to
143 // newer, but use the last entry found to ensure overlap.
144 //
145 if (nonBlock && (sequence != log_time::EPOCH) && timeout) {
146 class LogFindStart { // A lambda by another name
147 private:
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800148 const pid_t mPid;
149 const unsigned mLogMask;
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800150 bool mStartTimeSet;
151 log_time mStart;
152 log_time& mSequence;
153 log_time mLast;
154 bool mIsMonotonic;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800155
Mark Salyzyn501c3732017-03-10 14:31:54 -0800156 public:
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800157 LogFindStart(pid_t pid, unsigned logMask, log_time& sequence,
158 bool isMonotonic)
Mark Salyzyn501c3732017-03-10 14:31:54 -0800159 : mPid(pid),
160 mLogMask(logMask),
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800161 mStartTimeSet(false),
162 mStart(sequence),
163 mSequence(sequence),
164 mLast(sequence),
165 mIsMonotonic(isMonotonic) {
Mark Salyzyn77187782015-05-12 15:21:31 -0700166 }
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800167
Mark Salyzyn501c3732017-03-10 14:31:54 -0800168 static int callback(const LogBufferElement* element, void* obj) {
169 LogFindStart* me = reinterpret_cast<LogFindStart*>(obj);
170 if ((!me->mPid || (me->mPid == element->getPid())) &&
171 (me->mLogMask & (1 << element->getLogId()))) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800172 log_time real = element->getRealTime();
173 if (me->mStart == real) {
174 me->mSequence = real;
175 me->mStartTimeSet = true;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800176 return -1;
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800177 } else if (!me->mIsMonotonic || android::isMonotonic(real)) {
178 if (me->mStart < real) {
179 me->mSequence = me->mLast;
180 me->mStartTimeSet = true;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800181 return -1;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800182 }
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800183 me->mLast = real;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700184 } else {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800185 me->mLast = real;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800186 }
187 }
188 return false;
189 }
190
Mark Salyzyn501c3732017-03-10 14:31:54 -0800191 bool found() {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800192 return mStartTimeSet;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800193 }
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800194
195 } logFindStart(pid, logMask, sequence,
Mark Salyzynb6bee332015-09-08 08:56:32 -0700196 logbuf().isMonotonic() && android::isMonotonic(start));
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800197
Mark Salyzynae2abf12017-03-31 10:48:39 -0700198 logbuf().flushTo(cli, sequence, nullptr, FlushCommand::hasReadLogs(cli),
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800199 FlushCommand::hasSecurityLogs(cli),
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800200 logFindStart.callback, &logFindStart);
201
202 if (!logFindStart.found()) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800203 doSocketDelete(cli);
204 return false;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800205 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800206 }
207
Mark Salyzynfa3add32016-12-29 07:26:30 -0800208 android::prdebug(
209 "logdr: UID=%d GID=%d PID=%d %c tail=%lu logMask=%x pid=%d "
210 "start=%" PRIu64 "ns timeout=%" PRIu64 "ns\n",
211 cli->getUid(), cli->getGid(), cli->getPid(), nonBlock ? 'n' : 'b', tail,
212 logMask, (int)pid, sequence.nsec(), timeout);
213
Tom Cherrye2d30d12018-10-19 13:51:35 -0700214 if (sequence == log_time::EPOCH) {
215 timeout = 0;
216 }
217
Tom Cherry4f227862018-10-08 17:33:50 -0700218 LogTimeEntry::wrlock();
219 auto entry = std::make_unique<LogTimeEntry>(
220 *this, cli, nonBlock, tail, logMask, pid, sequence, timeout);
221 if (!entry->startReader_Locked()) {
222 LogTimeEntry::unlock();
223 return false;
224 }
225
226 // release client and entry reference counts once done
227 cli->incRef();
228 mLogbuf.mTimes.emplace_front(std::move(entry));
Mark Salyzyn5c77ad52016-02-23 08:55:43 -0800229
230 // Set acceptable upper limit to wait for slow reader processing b/27242723
231 struct timeval t = { LOGD_SNDTIMEO, 0 };
Mark Salyzyn501c3732017-03-10 14:31:54 -0800232 setsockopt(cli->getSocket(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&t,
233 sizeof(t));
Mark Salyzyn5c77ad52016-02-23 08:55:43 -0800234
Tom Cherry4f227862018-10-08 17:33:50 -0700235 LogTimeEntry::unlock();
236
Mark Salyzyn0175b072014-02-26 09:50:16 -0800237 return true;
238}
239
Mark Salyzyn501c3732017-03-10 14:31:54 -0800240void LogReader::doSocketDelete(SocketClient* cli) {
241 LastLogTimes& times = mLogbuf.mTimes;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700242 LogTimeEntry::wrlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800243 LastLogTimes::iterator it = times.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800244 while (it != times.end()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700245 LogTimeEntry* entry = it->get();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800246 if (entry->mClient == cli) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800247 entry->release_Locked();
248 break;
249 }
250 it++;
251 }
252 LogTimeEntry::unlock();
253}
Mark Salyzyndfc47e82014-03-24 10:26:47 -0700254
255int LogReader::getLogSocket() {
256 static const char socketName[] = "logdr";
257 int sock = android_get_control_socket(socketName);
258
259 if (sock < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800260 sock = socket_local_server(
261 socketName, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET);
Mark Salyzyndfc47e82014-03-24 10:26:47 -0700262 }
263
264 return sock;
265}