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 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 24 | #include <cutils/sockets.h> |
Mark Salyzyn | aeaaf81 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 25 | #include <private/android_logger.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 27 | #include "FlushCommand.h" |
Mark Salyzyn | 2ad0bd0 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 28 | #include "LogBuffer.h" |
| 29 | #include "LogBufferElement.h" |
| 30 | #include "LogReader.h" |
| 31 | #include "LogUtils.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 32 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 33 | LogReader::LogReader(LogBuffer* logbuf) |
| 34 | : SocketListener(getLogSocket(), true), mLogbuf(*logbuf) { |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 35 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 36 | |
| 37 | // When we are notified a new log entry is available, inform |
Hao Wang | f6e2296 | 2017-12-04 14:10:40 +0800 | [diff] [blame] | 38 | // listening sockets who are watching this entry's log id. |
| 39 | void LogReader::notifyNewLog(log_mask_t logMask) { |
| 40 | FlushCommand command(*this, logMask); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 41 | runOnEachSocket(&command); |
| 42 | } |
| 43 | |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 44 | // Note returning false will release the SocketClient instance. |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 45 | bool LogReader::onDataAvailable(SocketClient* cli) { |
Mark Salyzyn | e3aeeee | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 46 | static bool name_set; |
| 47 | if (!name_set) { |
| 48 | prctl(PR_SET_NAME, "logd.reader"); |
| 49 | name_set = true; |
| 50 | } |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 51 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 52 | 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 Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 61 | // 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 Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 73 | unsigned long tail = 0; |
| 74 | static const char _tail[] = " tail="; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 75 | char* cp = strstr(buffer, _tail); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 76 | if (cp) { |
| 77 | tail = atol(cp + sizeof(_tail) - 1); |
| 78 | } |
| 79 | |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 80 | 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 Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 88 | uint64_t timeout = 0; |
| 89 | static const char _timeout[] = " timeout="; |
| 90 | cp = strstr(buffer, _timeout); |
| 91 | if (cp) { |
| 92 | timeout = atol(cp + sizeof(_timeout) - 1) * NS_PER_SEC + |
| 93 | log_time(CLOCK_REALTIME).nsec(); |
| 94 | } |
| 95 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 96 | unsigned int logMask = -1; |
| 97 | static const char _logIds[] = " lids="; |
| 98 | cp = strstr(buffer, _logIds); |
| 99 | if (cp) { |
| 100 | logMask = 0; |
| 101 | cp += sizeof(_logIds) - 1; |
| 102 | while (*cp && *cp != '\0') { |
| 103 | int val = 0; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 104 | while (isdigit(*cp)) { |
| 105 | val = val * 10 + *cp - '0'; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 106 | ++cp; |
| 107 | } |
| 108 | logMask |= 1 << val; |
| 109 | if (*cp != ',') { |
| 110 | break; |
| 111 | } |
| 112 | ++cp; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | pid_t pid = 0; |
| 117 | static const char _pid[] = " pid="; |
| 118 | cp = strstr(buffer, _pid); |
| 119 | if (cp) { |
| 120 | pid = atol(cp + sizeof(_pid) - 1); |
| 121 | } |
| 122 | |
| 123 | bool nonBlock = false; |
Mark Salyzyn | 0eeb06b | 2016-12-02 10:08:48 -0800 | [diff] [blame] | 124 | if (!fastcmp<strncmp>(buffer, "dumpAndClose", 12)) { |
Mark Salyzyn | f669acb | 2014-09-16 09:19:47 -0700 | [diff] [blame] | 125 | // Allow writer to get some cycles, and wait for pending notifications |
| 126 | sched_yield(); |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 127 | LogTimeEntry::wrlock(); |
Mark Salyzyn | f669acb | 2014-09-16 09:19:47 -0700 | [diff] [blame] | 128 | LogTimeEntry::unlock(); |
| 129 | sched_yield(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 130 | nonBlock = true; |
| 131 | } |
| 132 | |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 133 | log_time sequence = start; |
| 134 | // |
| 135 | // This somewhat expensive data validation operation is required |
| 136 | // for non-blocking, with timeout. The incoming timestamp must be |
| 137 | // in range of the list, if not, return immediately. This is |
| 138 | // used to prevent us from from getting stuck in timeout processing |
| 139 | // with an invalid time. |
| 140 | // |
| 141 | // Find if time is really present in the logs, monotonic or real, implicit |
| 142 | // conversion from monotonic or real as necessary to perform the check. |
| 143 | // Exit in the check loop ASAP as you find a transition from older to |
| 144 | // newer, but use the last entry found to ensure overlap. |
| 145 | // |
| 146 | if (nonBlock && (sequence != log_time::EPOCH) && timeout) { |
| 147 | class LogFindStart { // A lambda by another name |
| 148 | private: |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 149 | const pid_t mPid; |
| 150 | const unsigned mLogMask; |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 151 | bool mStartTimeSet; |
| 152 | log_time mStart; |
| 153 | log_time& mSequence; |
| 154 | log_time mLast; |
| 155 | bool mIsMonotonic; |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 156 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 157 | public: |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 158 | LogFindStart(pid_t pid, unsigned logMask, log_time& sequence, |
| 159 | bool isMonotonic) |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 160 | : mPid(pid), |
| 161 | mLogMask(logMask), |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 162 | mStartTimeSet(false), |
| 163 | mStart(sequence), |
| 164 | mSequence(sequence), |
| 165 | mLast(sequence), |
| 166 | mIsMonotonic(isMonotonic) { |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 167 | } |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 168 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 169 | static int callback(const LogBufferElement* element, void* obj) { |
| 170 | LogFindStart* me = reinterpret_cast<LogFindStart*>(obj); |
| 171 | if ((!me->mPid || (me->mPid == element->getPid())) && |
| 172 | (me->mLogMask & (1 << element->getLogId()))) { |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 173 | log_time real = element->getRealTime(); |
| 174 | if (me->mStart == real) { |
| 175 | me->mSequence = real; |
| 176 | me->mStartTimeSet = true; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 177 | return -1; |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 178 | } else if (!me->mIsMonotonic || android::isMonotonic(real)) { |
| 179 | if (me->mStart < real) { |
| 180 | me->mSequence = me->mLast; |
| 181 | me->mStartTimeSet = true; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 182 | return -1; |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 183 | } |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 184 | me->mLast = real; |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 185 | } else { |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 186 | me->mLast = real; |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | return false; |
| 190 | } |
| 191 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 192 | bool found() { |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 193 | return mStartTimeSet; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 194 | } |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 195 | |
| 196 | } logFindStart(pid, logMask, sequence, |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 197 | logbuf().isMonotonic() && android::isMonotonic(start)); |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 198 | |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 199 | logbuf().flushTo(cli, sequence, nullptr, FlushCommand::hasReadLogs(cli), |
Mark Salyzyn | 8fa8896 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 200 | FlushCommand::hasSecurityLogs(cli), |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 201 | logFindStart.callback, &logFindStart); |
| 202 | |
| 203 | if (!logFindStart.found()) { |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 204 | doSocketDelete(cli); |
| 205 | return false; |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 206 | } |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Mark Salyzyn | fa3add3 | 2016-12-29 07:26:30 -0800 | [diff] [blame] | 209 | android::prdebug( |
| 210 | "logdr: UID=%d GID=%d PID=%d %c tail=%lu logMask=%x pid=%d " |
| 211 | "start=%" PRIu64 "ns timeout=%" PRIu64 "ns\n", |
| 212 | cli->getUid(), cli->getGid(), cli->getPid(), nonBlock ? 'n' : 'b', tail, |
| 213 | logMask, (int)pid, sequence.nsec(), timeout); |
| 214 | |
Tom Cherry | e2d30d1 | 2018-10-19 13:51:35 -0700 | [diff] [blame] | 215 | if (sequence == log_time::EPOCH) { |
| 216 | timeout = 0; |
| 217 | } |
| 218 | |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 219 | LogTimeEntry::wrlock(); |
| 220 | auto entry = std::make_unique<LogTimeEntry>( |
| 221 | *this, cli, nonBlock, tail, logMask, pid, sequence, timeout); |
| 222 | if (!entry->startReader_Locked()) { |
| 223 | LogTimeEntry::unlock(); |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | // release client and entry reference counts once done |
| 228 | cli->incRef(); |
| 229 | mLogbuf.mTimes.emplace_front(std::move(entry)); |
Mark Salyzyn | 5c77ad5 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 230 | |
| 231 | // Set acceptable upper limit to wait for slow reader processing b/27242723 |
| 232 | struct timeval t = { LOGD_SNDTIMEO, 0 }; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 233 | setsockopt(cli->getSocket(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&t, |
| 234 | sizeof(t)); |
Mark Salyzyn | 5c77ad5 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 235 | |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 236 | LogTimeEntry::unlock(); |
| 237 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 238 | return true; |
| 239 | } |
| 240 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 241 | void LogReader::doSocketDelete(SocketClient* cli) { |
| 242 | LastLogTimes& times = mLogbuf.mTimes; |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 243 | LogTimeEntry::wrlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 244 | LastLogTimes::iterator it = times.begin(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 245 | while (it != times.end()) { |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 246 | LogTimeEntry* entry = it->get(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 247 | if (entry->mClient == cli) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 248 | entry->release_Locked(); |
| 249 | break; |
| 250 | } |
| 251 | it++; |
| 252 | } |
| 253 | LogTimeEntry::unlock(); |
| 254 | } |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 255 | |
| 256 | int LogReader::getLogSocket() { |
| 257 | static const char socketName[] = "logdr"; |
| 258 | int sock = android_get_control_socket(socketName); |
| 259 | |
| 260 | if (sock < 0) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 261 | sock = socket_local_server( |
| 262 | socketName, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET); |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | return sock; |
| 266 | } |