Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 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 Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 17 | #include <ctype.h> |
Mark Salyzyn | fa3add3 | 2016-12-29 07:26:30 -0800 | [diff] [blame] | 18 | #include <inttypes.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 19 | #include <poll.h> |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 20 | #include <sys/prctl.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 21 | #include <sys/socket.h> |
Mark Salyzyn | 5c77ad5 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 23 | |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 24 | #include <chrono> |
| 25 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | #include <cutils/sockets.h> |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame^] | 27 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | aeaaf81 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 28 | #include <private/android_logger.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 29 | |
Mark Salyzyn | 2ad0bd0 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 30 | #include "LogBuffer.h" |
| 31 | #include "LogBufferElement.h" |
| 32 | #include "LogReader.h" |
| 33 | #include "LogUtils.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 34 | |
Tom Cherry | 79d54f7 | 2020-05-04 11:13:55 -0700 | [diff] [blame] | 35 | static bool CanReadSecurityLogs(SocketClient* client) { |
| 36 | return client->getUid() == AID_SYSTEM || client->getGid() == AID_SYSTEM; |
| 37 | } |
| 38 | |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 39 | LogReader::LogReader(LogBuffer* logbuf, LogReaderList* reader_list) |
| 40 | : SocketListener(getLogSocket(), true), log_buffer_(logbuf), reader_list_(reader_list) {} |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 41 | |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 42 | // Note returning false will release the SocketClient instance. |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 43 | bool LogReader::onDataAvailable(SocketClient* cli) { |
Mark Salyzyn | e3aeeee | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 44 | static bool name_set; |
| 45 | if (!name_set) { |
| 46 | prctl(PR_SET_NAME, "logd.reader"); |
| 47 | name_set = true; |
| 48 | } |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 49 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 50 | 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 Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 59 | // Clients are only allowed to send one command, disconnect them if they |
| 60 | // send another. |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 61 | { |
| 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 Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 70 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 71 | unsigned long tail = 0; |
| 72 | static const char _tail[] = " tail="; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 73 | char* cp = strstr(buffer, _tail); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 74 | if (cp) { |
| 75 | tail = atol(cp + sizeof(_tail) - 1); |
| 76 | } |
| 77 | |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 78 | 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 Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 86 | std::chrono::steady_clock::time_point deadline = {}; |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 87 | static const char _timeout[] = " timeout="; |
| 88 | cp = strstr(buffer, _timeout); |
| 89 | if (cp) { |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 90 | long timeout_seconds = atol(cp + sizeof(_timeout) - 1); |
| 91 | deadline = std::chrono::steady_clock::now() + std::chrono::seconds(timeout_seconds); |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 94 | 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 Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 102 | while (isdigit(*cp)) { |
| 103 | val = val * 10 + *cp - '0'; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 104 | ++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 Salyzyn | 0eeb06b | 2016-12-02 10:08:48 -0800 | [diff] [blame] | 122 | if (!fastcmp<strncmp>(buffer, "dumpAndClose", 12)) { |
Mark Salyzyn | f669acb | 2014-09-16 09:19:47 -0700 | [diff] [blame] | 123 | // Allow writer to get some cycles, and wait for pending notifications |
| 124 | sched_yield(); |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 125 | reader_list_->reader_threads_lock().lock(); |
| 126 | reader_list_->reader_threads_lock().unlock(); |
Mark Salyzyn | f669acb | 2014-09-16 09:19:47 -0700 | [diff] [blame] | 127 | sched_yield(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 128 | nonBlock = true; |
| 129 | } |
| 130 | |
Tom Cherry | 79d54f7 | 2020-05-04 11:13:55 -0700 | [diff] [blame] | 131 | bool privileged = clientHasLogCredentials(cli); |
| 132 | bool can_read_security = CanReadSecurityLogs(cli); |
| 133 | |
Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 134 | uint64_t sequence = 1; |
| 135 | // Convert realtime to sequence number |
| 136 | if (start != log_time::EPOCH) { |
Tom Cherry | 320f596 | 2020-05-04 17:25:34 -0700 | [diff] [blame] | 137 | bool start_time_set = false; |
Tom Cherry | 320f596 | 2020-05-04 17:25:34 -0700 | [diff] [blame] | 138 | uint64_t last = sequence; |
Tom Cherry | f2c2746 | 2020-04-08 14:36:05 -0700 | [diff] [blame] | 139 | auto log_find_start = [pid, logMask, start, &sequence, &start_time_set, |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 140 | &last](const LogBufferElement* element) -> FlushToResult { |
Tom Cherry | 320f596 | 2020-05-04 17:25:34 -0700 | [diff] [blame] | 141 | if (pid && pid != element->getPid()) { |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 142 | return FlushToResult::kSkip; |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 143 | } |
Tom Cherry | 320f596 | 2020-05-04 17:25:34 -0700 | [diff] [blame] | 144 | if ((logMask & (1 << element->getLogId())) == 0) { |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 145 | return FlushToResult::kSkip; |
Tom Cherry | 320f596 | 2020-05-04 17:25:34 -0700 | [diff] [blame] | 146 | } |
| 147 | if (start == element->getRealTime()) { |
| 148 | sequence = element->getSequence(); |
| 149 | start_time_set = true; |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 150 | return FlushToResult::kStop; |
Tom Cherry | f2c2746 | 2020-04-08 14:36:05 -0700 | [diff] [blame] | 151 | } else { |
Tom Cherry | 320f596 | 2020-05-04 17:25:34 -0700 | [diff] [blame] | 152 | if (start < element->getRealTime()) { |
| 153 | sequence = last; |
| 154 | start_time_set = true; |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 155 | return FlushToResult::kStop; |
Tom Cherry | 320f596 | 2020-05-04 17:25:34 -0700 | [diff] [blame] | 156 | } |
| 157 | last = element->getSequence(); |
Tom Cherry | 320f596 | 2020-05-04 17:25:34 -0700 | [diff] [blame] | 158 | } |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 159 | return FlushToResult::kSkip; |
Tom Cherry | 320f596 | 2020-05-04 17:25:34 -0700 | [diff] [blame] | 160 | }; |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 161 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame^] | 162 | log_buffer_->FlushTo(cli, sequence, nullptr, privileged, can_read_security, log_find_start); |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 163 | |
Tom Cherry | 320f596 | 2020-05-04 17:25:34 -0700 | [diff] [blame] | 164 | if (!start_time_set) { |
Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 165 | if (nonBlock) { |
| 166 | doSocketDelete(cli); |
| 167 | return false; |
| 168 | } |
| 169 | sequence = LogBufferElement::getCurrentSequence(); |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 170 | } |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Mark Salyzyn | fa3add3 | 2016-12-29 07:26:30 -0800 | [diff] [blame] | 173 | android::prdebug( |
Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 174 | "logdr: UID=%d GID=%d PID=%d %c tail=%lu logMask=%x pid=%d " |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 175 | "start=%" PRIu64 "ns deadline=%" PRIi64 "ns\n", |
Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 176 | cli->getUid(), cli->getGid(), cli->getPid(), nonBlock ? 'n' : 'b', tail, logMask, |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 177 | (int)pid, start.nsec(), static_cast<int64_t>(deadline.time_since_epoch().count())); |
Mark Salyzyn | fa3add3 | 2016-12-29 07:26:30 -0800 | [diff] [blame] | 178 | |
Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 179 | if (start == log_time::EPOCH) { |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 180 | deadline = {}; |
Tom Cherry | e2d30d1 | 2018-10-19 13:51:35 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 183 | 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 Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 187 | if (!entry->startReader_Locked()) { |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 188 | return false; |
| 189 | } |
| 190 | |
| 191 | // release client and entry reference counts once done |
| 192 | cli->incRef(); |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 193 | reader_list_->reader_threads().emplace_front(std::move(entry)); |
Mark Salyzyn | 5c77ad5 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 194 | |
| 195 | // Set acceptable upper limit to wait for slow reader processing b/27242723 |
| 196 | struct timeval t = { LOGD_SNDTIMEO, 0 }; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 197 | setsockopt(cli->getSocket(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&t, |
| 198 | sizeof(t)); |
Mark Salyzyn | 5c77ad5 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 199 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 200 | return true; |
| 201 | } |
| 202 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 203 | void LogReader::doSocketDelete(SocketClient* cli) { |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 204 | 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 Cherry | 6ec71e9 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 207 | LogReaderThread* entry = it->get(); |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 208 | if (entry->client() == cli) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 209 | entry->release_Locked(); |
| 210 | break; |
| 211 | } |
| 212 | it++; |
| 213 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 214 | } |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 215 | |
| 216 | int LogReader::getLogSocket() { |
| 217 | static const char socketName[] = "logdr"; |
| 218 | int sock = android_get_control_socket(socketName); |
| 219 | |
| 220 | if (sock < 0) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 221 | sock = socket_local_server( |
| 222 | socketName, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET); |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return sock; |
| 226 | } |