blob: 0b8c31bf999edaf1a38ce103a88cf0d607f092ac [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,
29 pid_t pid)
30 : mReader(reader)
31 , mNonBlock(nonBlock)
32 , mTail(tail)
33 , mLogMask(logMask)
34 , mPid(pid)
35{ }
36
37// runSocketCommand is called once for every open client on the
38// log reader socket. Here we manage and associated the reader
39// client tracking and log region locks LastLogTimes list of
40// LogTimeEntrys, and spawn a transitory per-client thread to
41// work at filing data to the socket.
42//
43// global LogTimeEntry::lock() is used to protect access,
44// reference counts are used to ensure that individual
45// LogTimeEntry lifetime is managed when not protected.
46void FlushCommand::runSocketCommand(SocketClient *client) {
47 LogTimeEntry *entry = NULL;
48 LastLogTimes &times = mReader.logbuf().mTimes;
49
50 LogTimeEntry::lock();
51 LastLogTimes::iterator it = times.begin();
52 while(it != times.end()) {
53 entry = (*it);
54 if (entry->mClient == client) {
55 entry->triggerReader_Locked();
56 if (entry->runningReader_Locked()) {
57 LogTimeEntry::unlock();
58 return;
59 }
60 entry->incRef_Locked();
61 break;
62 }
63 it++;
64 }
65
66 if (it == times.end()) {
Mark Salyzync03e72c2014-02-18 11:23:53 -080067 // Create LogTimeEntry in notifyNewLog() ?
Mark Salyzyn0175b072014-02-26 09:50:16 -080068 if (mTail == (unsigned long) -1) {
69 LogTimeEntry::unlock();
70 return;
71 }
72 entry = new LogTimeEntry(mReader, client, mNonBlock, mTail, mLogMask, mPid);
73 times.push_back(entry);
74 }
75
76 client->incRef();
77
Mark Salyzync03e72c2014-02-18 11:23:53 -080078 // release client and entry reference counts once done
Mark Salyzyn0175b072014-02-26 09:50:16 -080079 entry->startReader_Locked();
80 LogTimeEntry::unlock();
81}
82
83bool FlushCommand::hasReadLogs(SocketClient *client) {
Mark Salyzyn1114f182014-02-21 13:54:07 -080084 return clientHasLogCredentials(client);
Mark Salyzyn0175b072014-02-26 09:50:16 -080085}