blob: d58492586225330a4be36831be66134cfa08ca5b [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
Mark Salyzyn1114f182014-02-21 13:54:07 -08002 * Copyright (C) 2012-2014 The Android Open Source Project
Mark Salyzyn0175b072014-02-26 09:50:16 -08003 *
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 Salyzyn1114f182014-02-21 13:54:07 -080018
Mark Salyzyn0175b072014-02-26 09:50:16 -080019#include "FlushCommand.h"
20#include "LogBufferElement.h"
Mark Salyzyn1114f182014-02-21 13:54:07 -080021#include "LogCommand.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080022#include "LogReader.h"
Mark Salyzyn1114f182014-02-21 13:54:07 -080023#include "LogTimes.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080024
25FlushCommand::FlushCommand(LogReader &reader,
26 bool nonBlock,
27 unsigned long tail,
28 unsigned int logMask,
Mark Salyzynfa3716b2014-02-14 16:05:05 -080029 pid_t pid,
Mark Salyzyn77187782015-05-12 15:21:31 -070030 uint64_t start) :
31 mReader(reader),
32 mNonBlock(nonBlock),
33 mTail(tail),
34 mLogMask(logMask),
35 mPid(pid),
36 mStart(start) {
37}
Mark Salyzyn0175b072014-02-26 09:50:16 -080038
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.
48void FlushCommand::runSocketCommand(SocketClient *client) {
49 LogTimeEntry *entry = NULL;
50 LastLogTimes &times = 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 Salyzync03e72c2014-02-18 11:23:53 -080069 // Create LogTimeEntry in notifyNewLog() ?
Mark Salyzyn0175b072014-02-26 09:50:16 -080070 if (mTail == (unsigned long) -1) {
71 LogTimeEntry::unlock();
72 return;
73 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -080074 entry = new LogTimeEntry(mReader, client, mNonBlock, mTail, mLogMask, mPid, mStart);
Mark Salyzyn0175b072014-02-26 09:50:16 -080075 times.push_back(entry);
76 }
77
78 client->incRef();
79
Mark Salyzync03e72c2014-02-18 11:23:53 -080080 // release client and entry reference counts once done
Mark Salyzyn0175b072014-02-26 09:50:16 -080081 entry->startReader_Locked();
82 LogTimeEntry::unlock();
83}
84
85bool FlushCommand::hasReadLogs(SocketClient *client) {
Mark Salyzyn1114f182014-02-21 13:54:07 -080086 return clientHasLogCredentials(client);
Mark Salyzyn0175b072014-02-26 09:50:16 -080087}