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 | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 19 | #include "FlushCommand.h" |
| 20 | #include "LogBufferElement.h" |
Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 21 | #include "LogCommand.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 22 | #include "LogReader.h" |
Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 23 | #include "LogTimes.h" |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 24 | #include "LogUtils.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 25 | |
| 26 | FlushCommand::FlushCommand(LogReader &reader, |
| 27 | bool nonBlock, |
| 28 | unsigned long tail, |
| 29 | unsigned int logMask, |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 30 | pid_t pid, |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 31 | uint64_t start, |
| 32 | uint64_t timeout) : |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 33 | mReader(reader), |
| 34 | mNonBlock(nonBlock), |
| 35 | mTail(tail), |
| 36 | mLogMask(logMask), |
| 37 | mPid(pid), |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 38 | mStart(start), |
Mark Salyzyn | 0157097 | 2016-01-07 13:21:09 -0800 | [diff] [blame] | 39 | mTimeout((start > 1) ? timeout : 0) { |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 40 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 41 | |
| 42 | // runSocketCommand is called once for every open client on the |
| 43 | // log reader socket. Here we manage and associated the reader |
| 44 | // client tracking and log region locks LastLogTimes list of |
| 45 | // LogTimeEntrys, and spawn a transitory per-client thread to |
| 46 | // work at filing data to the socket. |
| 47 | // |
| 48 | // global LogTimeEntry::lock() is used to protect access, |
| 49 | // reference counts are used to ensure that individual |
| 50 | // LogTimeEntry lifetime is managed when not protected. |
| 51 | void FlushCommand::runSocketCommand(SocketClient *client) { |
| 52 | LogTimeEntry *entry = NULL; |
| 53 | LastLogTimes × = mReader.logbuf().mTimes; |
| 54 | |
| 55 | LogTimeEntry::lock(); |
| 56 | LastLogTimes::iterator it = times.begin(); |
| 57 | while(it != times.end()) { |
| 58 | entry = (*it); |
| 59 | if (entry->mClient == client) { |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 60 | if (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec) { |
| 61 | LogTimeEntry::unlock(); |
| 62 | return; |
| 63 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 64 | entry->triggerReader_Locked(); |
| 65 | if (entry->runningReader_Locked()) { |
| 66 | LogTimeEntry::unlock(); |
| 67 | return; |
| 68 | } |
| 69 | entry->incRef_Locked(); |
| 70 | break; |
| 71 | } |
| 72 | it++; |
| 73 | } |
| 74 | |
| 75 | if (it == times.end()) { |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 76 | // Create LogTimeEntry in notifyNewLog() ? |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 77 | if (mTail == (unsigned long) -1) { |
| 78 | LogTimeEntry::unlock(); |
| 79 | return; |
| 80 | } |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 81 | entry = new LogTimeEntry(mReader, client, mNonBlock, mTail, mLogMask, |
| 82 | mPid, mStart, mTimeout); |
Mark Salyzyn | 98dca2d | 2015-08-19 13:10:14 -0700 | [diff] [blame] | 83 | times.push_front(entry); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | client->incRef(); |
| 87 | |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 88 | // release client and entry reference counts once done |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 89 | entry->startReader_Locked(); |
| 90 | LogTimeEntry::unlock(); |
| 91 | } |
| 92 | |
| 93 | bool FlushCommand::hasReadLogs(SocketClient *client) { |
Mark Salyzyn | 1114f18 | 2014-02-21 13:54:07 -0800 | [diff] [blame] | 94 | return clientHasLogCredentials(client); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 95 | } |
Mark Salyzyn | 8fa8896 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 96 | |
| 97 | static bool clientHasSecurityCredentials(SocketClient *client) { |
| 98 | return (client->getUid() == AID_SYSTEM) || (client->getGid() == AID_SYSTEM); |
| 99 | } |
| 100 | |
| 101 | bool FlushCommand::hasSecurityLogs(SocketClient *client) { |
| 102 | return clientHasSecurityCredentials(client); |
| 103 | } |