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 | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 18 | #include <poll.h> |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 19 | #include <sys/prctl.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 20 | #include <sys/socket.h> |
Mark Salyzyn | 5c77ad5 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 21 | #include <sys/types.h> |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 22 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 23 | #include <cutils/sockets.h> |
Mark Salyzyn | aeaaf81 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 24 | #include <private/android_logger.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 25 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | #include "FlushCommand.h" |
Mark Salyzyn | 2ad0bd0 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 27 | #include "LogBuffer.h" |
| 28 | #include "LogBufferElement.h" |
| 29 | #include "LogReader.h" |
| 30 | #include "LogUtils.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 31 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 32 | LogReader::LogReader(LogBuffer* logbuf) |
| 33 | : SocketListener(getLogSocket(), true), mLogbuf(*logbuf) { |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 34 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 35 | |
| 36 | // When we are notified a new log entry is available, inform |
| 37 | // all of our listening sockets. |
| 38 | void LogReader::notifyNewLog() { |
| 39 | FlushCommand command(*this); |
| 40 | runOnEachSocket(&command); |
| 41 | } |
| 42 | |
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 | |
| 59 | unsigned long tail = 0; |
| 60 | static const char _tail[] = " tail="; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 61 | char* cp = strstr(buffer, _tail); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 62 | if (cp) { |
| 63 | tail = atol(cp + sizeof(_tail) - 1); |
| 64 | } |
| 65 | |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 66 | log_time start(log_time::EPOCH); |
| 67 | static const char _start[] = " start="; |
| 68 | cp = strstr(buffer, _start); |
| 69 | if (cp) { |
| 70 | // Parse errors will result in current time |
| 71 | start.strptime(cp + sizeof(_start) - 1, "%s.%q"); |
| 72 | } |
| 73 | |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 74 | uint64_t timeout = 0; |
| 75 | static const char _timeout[] = " timeout="; |
| 76 | cp = strstr(buffer, _timeout); |
| 77 | if (cp) { |
| 78 | timeout = atol(cp + sizeof(_timeout) - 1) * NS_PER_SEC + |
| 79 | log_time(CLOCK_REALTIME).nsec(); |
| 80 | } |
| 81 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 82 | unsigned int logMask = -1; |
| 83 | static const char _logIds[] = " lids="; |
| 84 | cp = strstr(buffer, _logIds); |
| 85 | if (cp) { |
| 86 | logMask = 0; |
| 87 | cp += sizeof(_logIds) - 1; |
| 88 | while (*cp && *cp != '\0') { |
| 89 | int val = 0; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 90 | while (isdigit(*cp)) { |
| 91 | val = val * 10 + *cp - '0'; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 92 | ++cp; |
| 93 | } |
| 94 | logMask |= 1 << val; |
| 95 | if (*cp != ',') { |
| 96 | break; |
| 97 | } |
| 98 | ++cp; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pid_t pid = 0; |
| 103 | static const char _pid[] = " pid="; |
| 104 | cp = strstr(buffer, _pid); |
| 105 | if (cp) { |
| 106 | pid = atol(cp + sizeof(_pid) - 1); |
| 107 | } |
| 108 | |
| 109 | bool nonBlock = false; |
Mark Salyzyn | 0eeb06b | 2016-12-02 10:08:48 -0800 | [diff] [blame] | 110 | if (!fastcmp<strncmp>(buffer, "dumpAndClose", 12)) { |
Mark Salyzyn | f669acb | 2014-09-16 09:19:47 -0700 | [diff] [blame] | 111 | // Allow writer to get some cycles, and wait for pending notifications |
| 112 | sched_yield(); |
| 113 | LogTimeEntry::lock(); |
| 114 | LogTimeEntry::unlock(); |
| 115 | sched_yield(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 116 | nonBlock = true; |
| 117 | } |
| 118 | |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 119 | log_time sequence = start; |
| 120 | // |
| 121 | // This somewhat expensive data validation operation is required |
| 122 | // for non-blocking, with timeout. The incoming timestamp must be |
| 123 | // in range of the list, if not, return immediately. This is |
| 124 | // used to prevent us from from getting stuck in timeout processing |
| 125 | // with an invalid time. |
| 126 | // |
| 127 | // Find if time is really present in the logs, monotonic or real, implicit |
| 128 | // conversion from monotonic or real as necessary to perform the check. |
| 129 | // Exit in the check loop ASAP as you find a transition from older to |
| 130 | // newer, but use the last entry found to ensure overlap. |
| 131 | // |
| 132 | if (nonBlock && (sequence != log_time::EPOCH) && timeout) { |
| 133 | class LogFindStart { // A lambda by another name |
| 134 | private: |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 135 | const pid_t mPid; |
| 136 | const unsigned mLogMask; |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 137 | bool mStartTimeSet; |
| 138 | log_time mStart; |
| 139 | log_time& mSequence; |
| 140 | log_time mLast; |
| 141 | bool mIsMonotonic; |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 142 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 143 | public: |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 144 | LogFindStart(pid_t pid, unsigned logMask, log_time& sequence, |
| 145 | bool isMonotonic) |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 146 | : mPid(pid), |
| 147 | mLogMask(logMask), |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 148 | mStartTimeSet(false), |
| 149 | mStart(sequence), |
| 150 | mSequence(sequence), |
| 151 | mLast(sequence), |
| 152 | mIsMonotonic(isMonotonic) { |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 153 | } |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 154 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 155 | static int callback(const LogBufferElement* element, void* obj) { |
| 156 | LogFindStart* me = reinterpret_cast<LogFindStart*>(obj); |
| 157 | if ((!me->mPid || (me->mPid == element->getPid())) && |
| 158 | (me->mLogMask & (1 << element->getLogId()))) { |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 159 | log_time real = element->getRealTime(); |
| 160 | if (me->mStart == real) { |
| 161 | me->mSequence = real; |
| 162 | me->mStartTimeSet = true; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 163 | return -1; |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 164 | } else if (!me->mIsMonotonic || android::isMonotonic(real)) { |
| 165 | if (me->mStart < real) { |
| 166 | me->mSequence = me->mLast; |
| 167 | me->mStartTimeSet = true; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 168 | return -1; |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 169 | } |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 170 | me->mLast = real; |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 171 | } else { |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 172 | me->mLast = real; |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 178 | bool found() { |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 179 | return mStartTimeSet; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 180 | } |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 181 | |
| 182 | } logFindStart(pid, logMask, sequence, |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 183 | logbuf().isMonotonic() && android::isMonotonic(start)); |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 184 | |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 185 | logbuf().flushTo(cli, sequence, nullptr, FlushCommand::hasReadLogs(cli), |
Mark Salyzyn | 8fa8896 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 186 | FlushCommand::hasSecurityLogs(cli), |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 187 | logFindStart.callback, &logFindStart); |
| 188 | |
| 189 | if (!logFindStart.found()) { |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 190 | doSocketDelete(cli); |
| 191 | return false; |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 192 | } |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 195 | FlushCommand command(*this, nonBlock, tail, logMask, pid, sequence, timeout); |
Mark Salyzyn | 5c77ad5 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 196 | |
| 197 | // Set acceptable upper limit to wait for slow reader processing b/27242723 |
| 198 | struct timeval t = { LOGD_SNDTIMEO, 0 }; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 199 | setsockopt(cli->getSocket(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&t, |
| 200 | sizeof(t)); |
Mark Salyzyn | 5c77ad5 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 201 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 202 | command.runSocketCommand(cli); |
| 203 | return true; |
| 204 | } |
| 205 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 206 | void LogReader::doSocketDelete(SocketClient* cli) { |
| 207 | LastLogTimes& times = mLogbuf.mTimes; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 208 | LogTimeEntry::lock(); |
| 209 | LastLogTimes::iterator it = times.begin(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 210 | while (it != times.end()) { |
| 211 | LogTimeEntry* entry = (*it); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 212 | if (entry->mClient == cli) { |
| 213 | times.erase(it); |
| 214 | entry->release_Locked(); |
| 215 | break; |
| 216 | } |
| 217 | it++; |
| 218 | } |
| 219 | LogTimeEntry::unlock(); |
| 220 | } |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 221 | |
| 222 | int LogReader::getLogSocket() { |
| 223 | static const char socketName[] = "logdr"; |
| 224 | int sock = android_get_control_socket(socketName); |
| 225 | |
| 226 | if (sock < 0) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 227 | sock = socket_local_server( |
| 228 | socketName, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET); |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return sock; |
| 232 | } |