blob: 6f913720a580faf2571e59292d7640094aaf63cb [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
Tom Cherry68630a02020-05-11 16:29:29 -070024#include <chrono>
25
Mark Salyzyn0175b072014-02-26 09:50:16 -080026#include <cutils/sockets.h>
Tom Cherryd5b38382020-05-12 13:16:41 -070027#include <private/android_filesystem_config.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070028#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080029
Mark Salyzyn2ad0bd02016-02-23 08:55:43 -080030#include "LogBuffer.h"
31#include "LogBufferElement.h"
32#include "LogReader.h"
33#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080034
Tom Cherry79d54f72020-05-04 11:13:55 -070035static bool CanReadSecurityLogs(SocketClient* client) {
36 return client->getUid() == AID_SYSTEM || client->getGid() == AID_SYSTEM;
37}
38
Tom Cherry68630a02020-05-11 16:29:29 -070039LogReader::LogReader(LogBuffer* logbuf, LogReaderList* reader_list)
40 : SocketListener(getLogSocket(), true), log_buffer_(logbuf), reader_list_(reader_list) {}
Mark Salyzyn0175b072014-02-26 09:50:16 -080041
Tom Cherry4f227862018-10-08 17:33:50 -070042// Note returning false will release the SocketClient instance.
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
Tom Cherry4f227862018-10-08 17:33:50 -070059 // Clients are only allowed to send one command, disconnect them if they
60 // send another.
Tom Cherry68630a02020-05-11 16:29:29 -070061 {
62 auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
63 for (const auto& entry : reader_list_->reader_threads()) {
64 if (entry->client() == cli) {
65 entry->release_Locked();
66 return false;
67 }
Tom Cherry4f227862018-10-08 17:33:50 -070068 }
69 }
Tom Cherry4f227862018-10-08 17:33:50 -070070
Mark Salyzyn0175b072014-02-26 09:50:16 -080071 unsigned long tail = 0;
72 static const char _tail[] = " tail=";
Mark Salyzyn501c3732017-03-10 14:31:54 -080073 char* cp = strstr(buffer, _tail);
Mark Salyzyn0175b072014-02-26 09:50:16 -080074 if (cp) {
75 tail = atol(cp + sizeof(_tail) - 1);
76 }
77
Mark Salyzynfa3716b2014-02-14 16:05:05 -080078 log_time start(log_time::EPOCH);
79 static const char _start[] = " start=";
80 cp = strstr(buffer, _start);
81 if (cp) {
82 // Parse errors will result in current time
83 start.strptime(cp + sizeof(_start) - 1, "%s.%q");
84 }
85
Tom Cherry68630a02020-05-11 16:29:29 -070086 std::chrono::steady_clock::time_point deadline = {};
Mark Salyzynb75cce02015-11-30 11:35:56 -080087 static const char _timeout[] = " timeout=";
88 cp = strstr(buffer, _timeout);
89 if (cp) {
Tom Cherry68630a02020-05-11 16:29:29 -070090 long timeout_seconds = atol(cp + sizeof(_timeout) - 1);
91 deadline = std::chrono::steady_clock::now() + std::chrono::seconds(timeout_seconds);
Mark Salyzynb75cce02015-11-30 11:35:56 -080092 }
93
Mark Salyzyn0175b072014-02-26 09:50:16 -080094 unsigned int logMask = -1;
95 static const char _logIds[] = " lids=";
96 cp = strstr(buffer, _logIds);
97 if (cp) {
98 logMask = 0;
99 cp += sizeof(_logIds) - 1;
100 while (*cp && *cp != '\0') {
101 int val = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800102 while (isdigit(*cp)) {
103 val = val * 10 + *cp - '0';
Mark Salyzyn0175b072014-02-26 09:50:16 -0800104 ++cp;
105 }
106 logMask |= 1 << val;
107 if (*cp != ',') {
108 break;
109 }
110 ++cp;
111 }
112 }
113
114 pid_t pid = 0;
115 static const char _pid[] = " pid=";
116 cp = strstr(buffer, _pid);
117 if (cp) {
118 pid = atol(cp + sizeof(_pid) - 1);
119 }
120
121 bool nonBlock = false;
Mark Salyzyn0eeb06b2016-12-02 10:08:48 -0800122 if (!fastcmp<strncmp>(buffer, "dumpAndClose", 12)) {
Mark Salyzynf669acb2014-09-16 09:19:47 -0700123 // Allow writer to get some cycles, and wait for pending notifications
124 sched_yield();
Tom Cherry68630a02020-05-11 16:29:29 -0700125 reader_list_->reader_threads_lock().lock();
126 reader_list_->reader_threads_lock().unlock();
Mark Salyzynf669acb2014-09-16 09:19:47 -0700127 sched_yield();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800128 nonBlock = true;
129 }
130
Tom Cherry79d54f72020-05-04 11:13:55 -0700131 bool privileged = clientHasLogCredentials(cli);
132 bool can_read_security = CanReadSecurityLogs(cli);
133
Tom Cherry10d086e2019-08-21 14:16:34 -0700134 uint64_t sequence = 1;
135 // Convert realtime to sequence number
136 if (start != log_time::EPOCH) {
Tom Cherry320f5962020-05-04 17:25:34 -0700137 bool start_time_set = false;
Tom Cherry320f5962020-05-04 17:25:34 -0700138 uint64_t last = sequence;
Tom Cherryf2c27462020-04-08 14:36:05 -0700139 auto log_find_start = [pid, logMask, start, &sequence, &start_time_set,
Tom Cherry68630a02020-05-11 16:29:29 -0700140 &last](const LogBufferElement* element) -> FlushToResult {
Tom Cherry320f5962020-05-04 17:25:34 -0700141 if (pid && pid != element->getPid()) {
Tom Cherry68630a02020-05-11 16:29:29 -0700142 return FlushToResult::kSkip;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800143 }
Tom Cherry320f5962020-05-04 17:25:34 -0700144 if ((logMask & (1 << element->getLogId())) == 0) {
Tom Cherry68630a02020-05-11 16:29:29 -0700145 return FlushToResult::kSkip;
Tom Cherry320f5962020-05-04 17:25:34 -0700146 }
147 if (start == element->getRealTime()) {
148 sequence = element->getSequence();
149 start_time_set = true;
Tom Cherry68630a02020-05-11 16:29:29 -0700150 return FlushToResult::kStop;
Tom Cherryf2c27462020-04-08 14:36:05 -0700151 } else {
Tom Cherry320f5962020-05-04 17:25:34 -0700152 if (start < element->getRealTime()) {
153 sequence = last;
154 start_time_set = true;
Tom Cherry68630a02020-05-11 16:29:29 -0700155 return FlushToResult::kStop;
Tom Cherry320f5962020-05-04 17:25:34 -0700156 }
157 last = element->getSequence();
Tom Cherry320f5962020-05-04 17:25:34 -0700158 }
Tom Cherry68630a02020-05-11 16:29:29 -0700159 return FlushToResult::kSkip;
Tom Cherry320f5962020-05-04 17:25:34 -0700160 };
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800161
Tom Cherryd5b38382020-05-12 13:16:41 -0700162 log_buffer_->FlushTo(cli, sequence, nullptr, privileged, can_read_security, log_find_start);
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800163
Tom Cherry320f5962020-05-04 17:25:34 -0700164 if (!start_time_set) {
Tom Cherry10d086e2019-08-21 14:16:34 -0700165 if (nonBlock) {
166 doSocketDelete(cli);
167 return false;
168 }
169 sequence = LogBufferElement::getCurrentSequence();
Mark Salyzyna1c60cf2014-02-19 07:33:12 -0800170 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800171 }
172
Mark Salyzynfa3add32016-12-29 07:26:30 -0800173 android::prdebug(
Tom Cherry10d086e2019-08-21 14:16:34 -0700174 "logdr: UID=%d GID=%d PID=%d %c tail=%lu logMask=%x pid=%d "
Tom Cherry68630a02020-05-11 16:29:29 -0700175 "start=%" PRIu64 "ns deadline=%" PRIi64 "ns\n",
Tom Cherry10d086e2019-08-21 14:16:34 -0700176 cli->getUid(), cli->getGid(), cli->getPid(), nonBlock ? 'n' : 'b', tail, logMask,
Tom Cherry68630a02020-05-11 16:29:29 -0700177 (int)pid, start.nsec(), static_cast<int64_t>(deadline.time_since_epoch().count()));
Mark Salyzynfa3add32016-12-29 07:26:30 -0800178
Tom Cherry10d086e2019-08-21 14:16:34 -0700179 if (start == log_time::EPOCH) {
Tom Cherry68630a02020-05-11 16:29:29 -0700180 deadline = {};
Tom Cherrye2d30d12018-10-19 13:51:35 -0700181 }
182
Tom Cherry68630a02020-05-11 16:29:29 -0700183 auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
184 auto entry = std::make_unique<LogReaderThread>(*this, *reader_list_, cli, nonBlock, tail,
185 logMask, pid, start, sequence, deadline,
186 privileged, can_read_security);
Tom Cherry4f227862018-10-08 17:33:50 -0700187 if (!entry->startReader_Locked()) {
Tom Cherry4f227862018-10-08 17:33:50 -0700188 return false;
189 }
190
191 // release client and entry reference counts once done
192 cli->incRef();
Tom Cherry68630a02020-05-11 16:29:29 -0700193 reader_list_->reader_threads().emplace_front(std::move(entry));
Mark Salyzyn5c77ad52016-02-23 08:55:43 -0800194
195 // Set acceptable upper limit to wait for slow reader processing b/27242723
196 struct timeval t = { LOGD_SNDTIMEO, 0 };
Mark Salyzyn501c3732017-03-10 14:31:54 -0800197 setsockopt(cli->getSocket(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&t,
198 sizeof(t));
Mark Salyzyn5c77ad52016-02-23 08:55:43 -0800199
Mark Salyzyn0175b072014-02-26 09:50:16 -0800200 return true;
201}
202
Mark Salyzyn501c3732017-03-10 14:31:54 -0800203void LogReader::doSocketDelete(SocketClient* cli) {
Tom Cherry68630a02020-05-11 16:29:29 -0700204 auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
205 auto it = reader_list_->reader_threads().begin();
206 while (it != reader_list_->reader_threads().end()) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700207 LogReaderThread* entry = it->get();
Tom Cherrycef47bb2020-05-04 17:10:16 -0700208 if (entry->client() == cli) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800209 entry->release_Locked();
210 break;
211 }
212 it++;
213 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800214}
Mark Salyzyndfc47e82014-03-24 10:26:47 -0700215
216int LogReader::getLogSocket() {
217 static const char socketName[] = "logdr";
218 int sock = android_get_control_socket(socketName);
219
220 if (sock < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800221 sock = socket_local_server(
222 socketName, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET);
Mark Salyzyndfc47e82014-03-24 10:26:47 -0700223 }
224
225 return sock;
226}