Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 2 | * Copyright (C) 2012-2014 The Android Open Source Project |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 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 | |
| 17 | #include <stdlib.h> |
Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 18 | |
Mark Salyzyn | 2ad0bd0 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 19 | #include <private/android_filesystem_config.h> |
| 20 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 21 | #include "FlushCommand.h" |
Mark Salyzyn | 2ad0bd0 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 22 | #include "LogBuffer.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 23 | #include "LogBufferElement.h" |
Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 24 | #include "LogCommand.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 25 | #include "LogReader.h" |
Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 26 | #include "LogTimes.h" |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 27 | #include "LogUtils.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 28 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 29 | // runSocketCommand is called once for every open client on the |
| 30 | // log reader socket. Here we manage and associated the reader |
| 31 | // client tracking and log region locks LastLogTimes list of |
| 32 | // LogTimeEntrys, and spawn a transitory per-client thread to |
| 33 | // work at filing data to the socket. |
| 34 | // |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 35 | // global LogTimeEntry::wrlock() is used to protect access, |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 36 | // reference counts are used to ensure that individual |
| 37 | // LogTimeEntry lifetime is managed when not protected. |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 38 | void FlushCommand::runSocketCommand(SocketClient* client) { |
Yi Kong | c8d09dd | 2018-07-13 17:39:22 -0700 | [diff] [blame] | 39 | LogTimeEntry* entry = nullptr; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 40 | LastLogTimes& times = mReader.logbuf().mTimes; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 41 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 42 | LogTimeEntry::wrlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 43 | LastLogTimes::iterator it = times.begin(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 44 | while (it != times.end()) { |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 45 | entry = it->get(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 46 | if (entry->mClient == client) { |
Hao Wang | f6e2296 | 2017-12-04 14:10:40 +0800 | [diff] [blame] | 47 | if (!entry->isWatchingMultiple(mLogMask)) { |
| 48 | LogTimeEntry::unlock(); |
| 49 | return; |
| 50 | } |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 51 | if (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec) { |
Mark Salyzyn | 5e00177 | 2017-03-14 13:23:06 -0700 | [diff] [blame] | 52 | if (mReader.logbuf().isMonotonic()) { |
| 53 | LogTimeEntry::unlock(); |
| 54 | return; |
| 55 | } |
| 56 | // If the user changes the time in a gross manner that |
| 57 | // invalidates the timeout, fall through and trigger. |
| 58 | log_time now(CLOCK_REALTIME); |
| 59 | if (((entry->mEnd + entry->mTimeout) > now) && |
| 60 | (now > entry->mEnd)) { |
| 61 | LogTimeEntry::unlock(); |
| 62 | return; |
| 63 | } |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 64 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 65 | entry->triggerReader_Locked(); |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 66 | LogTimeEntry::unlock(); |
| 67 | return; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 68 | } |
| 69 | it++; |
| 70 | } |
| 71 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 72 | LogTimeEntry::unlock(); |
| 73 | } |
| 74 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 75 | bool FlushCommand::hasReadLogs(SocketClient* client) { |
Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 76 | return clientHasLogCredentials(client); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 77 | } |
Mark Salyzyn | 8fa8896 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 78 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 79 | static bool clientHasSecurityCredentials(SocketClient* client) { |
Mark Salyzyn | 8fa8896 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 80 | return (client->getUid() == AID_SYSTEM) || (client->getGid() == AID_SYSTEM); |
| 81 | } |
| 82 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 83 | bool FlushCommand::hasSecurityLogs(SocketClient* client) { |
Mark Salyzyn | 8fa8896 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 84 | return clientHasSecurityCredentials(client); |
| 85 | } |