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