Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 17 | #include <errno.h> |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 18 | #include <string.h> |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 19 | #include <sys/prctl.h> |
| 20 | |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 21 | #include <private/android_logger.h> |
| 22 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 23 | #include "FlushCommand.h" |
| 24 | #include "LogBuffer.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 25 | #include "LogReader.h" |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 26 | #include "LogTimes.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 27 | |
| 28 | pthread_mutex_t LogTimeEntry::timesLock = PTHREAD_MUTEX_INITIALIZER; |
| 29 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 30 | LogTimeEntry::LogTimeEntry(LogReader& reader, SocketClient* client, |
Hao Wang | f6e2296 | 2017-12-04 14:10:40 +0800 | [diff] [blame] | 31 | bool nonBlock, unsigned long tail, log_mask_t logMask, |
| 32 | pid_t pid, log_time start, uint64_t timeout) |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 33 | : mRefCount(1), |
| 34 | mRelease(false), |
| 35 | mError(false), |
| 36 | threadRunning(false), |
| 37 | leadingDropped(false), |
| 38 | mReader(reader), |
| 39 | mLogMask(logMask), |
| 40 | mPid(pid), |
| 41 | mCount(0), |
| 42 | mTail(tail), |
| 43 | mIndex(0), |
| 44 | mClient(client), |
| 45 | mStart(start), |
| 46 | mNonBlock(nonBlock), |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 47 | mEnd(log_time(android_log_clockid())) { |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 48 | mTimeout.tv_sec = timeout / NS_PER_SEC; |
| 49 | mTimeout.tv_nsec = timeout % NS_PER_SEC; |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 50 | memset(mLastTid, 0, sizeof(mLastTid)); |
| 51 | pthread_cond_init(&threadTriggeredCondition, nullptr); |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 52 | cleanSkip_Locked(); |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 53 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 54 | |
| 55 | void LogTimeEntry::startReader_Locked(void) { |
Mark Salyzyn | 765f782 | 2014-04-04 12:47:44 -0700 | [diff] [blame] | 56 | pthread_attr_t attr; |
| 57 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 58 | threadRunning = true; |
Mark Salyzyn | 765f782 | 2014-04-04 12:47:44 -0700 | [diff] [blame] | 59 | |
| 60 | if (!pthread_attr_init(&attr)) { |
| 61 | if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 62 | if (!pthread_create(&mThread, &attr, LogTimeEntry::threadStart, |
| 63 | this)) { |
Mark Salyzyn | 765f782 | 2014-04-04 12:47:44 -0700 | [diff] [blame] | 64 | pthread_attr_destroy(&attr); |
| 65 | return; |
| 66 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 67 | } |
Mark Salyzyn | 765f782 | 2014-04-04 12:47:44 -0700 | [diff] [blame] | 68 | pthread_attr_destroy(&attr); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 69 | } |
Mark Salyzyn | 765f782 | 2014-04-04 12:47:44 -0700 | [diff] [blame] | 70 | threadRunning = false; |
| 71 | if (mClient) { |
| 72 | mClient->decRef(); |
| 73 | } |
| 74 | decRef_Locked(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 77 | void LogTimeEntry::threadStop(void* obj) { |
| 78 | LogTimeEntry* me = reinterpret_cast<LogTimeEntry*>(obj); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 79 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 80 | wrlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 81 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 82 | if (me->mNonBlock) { |
| 83 | me->error_Locked(); |
| 84 | } |
| 85 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 86 | SocketClient* client = me->mClient; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 87 | |
| 88 | if (me->isError_Locked()) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 89 | LogReader& reader = me->mReader; |
| 90 | LastLogTimes& times = reader.logbuf().mTimes; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 91 | |
| 92 | LastLogTimes::iterator it = times.begin(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 93 | while (it != times.end()) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 94 | if (*it == me) { |
| 95 | times.erase(it); |
Mark Salyzyn | c348416 | 2016-03-01 14:59:32 -0800 | [diff] [blame] | 96 | me->release_nodelete_Locked(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 97 | break; |
| 98 | } |
| 99 | it++; |
| 100 | } |
| 101 | |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 102 | me->mClient = nullptr; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 103 | reader.release(client); |
| 104 | } |
| 105 | |
| 106 | if (client) { |
| 107 | client->decRef(); |
| 108 | } |
| 109 | |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 110 | me->threadRunning = false; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 111 | me->decRef_Locked(); |
| 112 | |
| 113 | unlock(); |
| 114 | } |
| 115 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 116 | void* LogTimeEntry::threadStart(void* obj) { |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 117 | prctl(PR_SET_NAME, "logd.reader.per"); |
| 118 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 119 | LogTimeEntry* me = reinterpret_cast<LogTimeEntry*>(obj); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 120 | |
| 121 | pthread_cleanup_push(threadStop, obj); |
| 122 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 123 | SocketClient* client = me->mClient; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 124 | if (!client) { |
| 125 | me->error(); |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 126 | return nullptr; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 129 | LogBuffer& logbuf = me->mReader.logbuf(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 130 | |
| 131 | bool privileged = FlushCommand::hasReadLogs(client); |
Mark Salyzyn | 8fa8896 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 132 | bool security = FlushCommand::hasSecurityLogs(client); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 133 | |
Mark Salyzyn | 047cc07 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 134 | me->leadingDropped = true; |
| 135 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 136 | wrlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 137 | |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 138 | log_time start = me->mStart; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 139 | |
Mark Salyzyn | de4bb9c | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 140 | while (me->threadRunning && !me->isError_Locked()) { |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 141 | if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) { |
| 142 | if (pthread_cond_timedwait(&me->threadTriggeredCondition, |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 143 | ×Lock, &me->mTimeout) == ETIMEDOUT) { |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 144 | me->mTimeout.tv_sec = 0; |
| 145 | me->mTimeout.tv_nsec = 0; |
| 146 | } |
| 147 | if (!me->threadRunning || me->isError_Locked()) { |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 152 | unlock(); |
| 153 | |
| 154 | if (me->mTail) { |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 155 | logbuf.flushTo(client, start, nullptr, privileged, security, |
| 156 | FilterFirstPass, me); |
Mark Salyzyn | 047cc07 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 157 | me->leadingDropped = true; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 158 | } |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 159 | start = logbuf.flushTo(client, start, me->mLastTid, privileged, |
| 160 | security, FilterSecondPass, me); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 161 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 162 | wrlock(); |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 163 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 164 | if (start == LogBufferElement::FLUSH_ERROR) { |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 165 | me->error_Locked(); |
Mark Salyzyn | de4bb9c | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 166 | break; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 169 | me->mStart = start + log_time(0, 1); |
Mark Salyzyn | de4bb9c | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 170 | |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 171 | if (me->mNonBlock || !me->threadRunning || me->isError_Locked()) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 172 | break; |
| 173 | } |
| 174 | |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 175 | me->cleanSkip_Locked(); |
| 176 | |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 177 | if (!me->mTimeout.tv_sec && !me->mTimeout.tv_nsec) { |
| 178 | pthread_cond_wait(&me->threadTriggeredCondition, ×Lock); |
| 179 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | unlock(); |
| 183 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 184 | pthread_cleanup_pop(true); |
| 185 | |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 186 | return nullptr; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // A first pass to count the number of elements |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 190 | int LogTimeEntry::FilterFirstPass(const LogBufferElement* element, void* obj) { |
| 191 | LogTimeEntry* me = reinterpret_cast<LogTimeEntry*>(obj); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 192 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 193 | LogTimeEntry::wrlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 194 | |
Mark Salyzyn | 047cc07 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 195 | if (me->leadingDropped) { |
| 196 | if (element->getDropped()) { |
| 197 | LogTimeEntry::unlock(); |
| 198 | return false; |
| 199 | } |
| 200 | me->leadingDropped = false; |
| 201 | } |
| 202 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 203 | if (me->mCount == 0) { |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 204 | me->mStart = element->getRealTime(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 207 | if ((!me->mPid || (me->mPid == element->getPid())) && |
| 208 | (me->isWatching(element->getLogId()))) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 209 | ++me->mCount; |
| 210 | } |
| 211 | |
| 212 | LogTimeEntry::unlock(); |
| 213 | |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | // A second pass to send the selected elements |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 218 | int LogTimeEntry::FilterSecondPass(const LogBufferElement* element, void* obj) { |
| 219 | LogTimeEntry* me = reinterpret_cast<LogTimeEntry*>(obj); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 220 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 221 | LogTimeEntry::wrlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 222 | |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 223 | me->mStart = element->getRealTime(); |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 224 | |
| 225 | if (me->skipAhead[element->getLogId()]) { |
| 226 | me->skipAhead[element->getLogId()]--; |
Mark Salyzyn | 8d7656b | 2014-06-05 15:58:43 -0700 | [diff] [blame] | 227 | goto skip; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Mark Salyzyn | 047cc07 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 230 | if (me->leadingDropped) { |
| 231 | if (element->getDropped()) { |
| 232 | goto skip; |
| 233 | } |
| 234 | me->leadingDropped = false; |
| 235 | } |
| 236 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 237 | // Truncate to close race between first and second pass |
| 238 | if (me->mNonBlock && me->mTail && (me->mIndex >= me->mCount)) { |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 239 | goto stop; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 240 | } |
| 241 | |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 242 | if (!me->isWatching(element->getLogId())) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 243 | goto skip; |
| 244 | } |
| 245 | |
| 246 | if (me->mPid && (me->mPid != element->getPid())) { |
| 247 | goto skip; |
| 248 | } |
| 249 | |
| 250 | if (me->isError_Locked()) { |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 251 | goto stop; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | if (!me->mTail) { |
| 255 | goto ok; |
| 256 | } |
| 257 | |
| 258 | ++me->mIndex; |
| 259 | |
| 260 | if ((me->mCount > me->mTail) && (me->mIndex <= (me->mCount - me->mTail))) { |
| 261 | goto skip; |
| 262 | } |
| 263 | |
| 264 | if (!me->mNonBlock) { |
| 265 | me->mTail = 0; |
| 266 | } |
| 267 | |
| 268 | ok: |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 269 | if (!me->skipAhead[element->getLogId()]) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 270 | LogTimeEntry::unlock(); |
| 271 | return true; |
| 272 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 273 | // FALLTHRU |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 274 | |
| 275 | skip: |
| 276 | LogTimeEntry::unlock(); |
| 277 | return false; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 278 | |
| 279 | stop: |
| 280 | LogTimeEntry::unlock(); |
| 281 | return -1; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 282 | } |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 283 | |
| 284 | void LogTimeEntry::cleanSkip_Locked(void) { |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 285 | memset(skipAhead, 0, sizeof(skipAhead)); |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 286 | } |