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