blob: bf650cdaa1fa0983db801259b73bfb9088809c78 [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 Salyzynb75cce02015-11-30 11:35:56 -080030 uint64_t start,
31 uint64_t timeout) :
Mark Salyzyn77187782015-05-12 15:21:31 -070032 mReader(reader),
33 mNonBlock(nonBlock),
34 mTail(tail),
35 mLogMask(logMask),
36 mPid(pid),
Mark Salyzynb75cce02015-11-30 11:35:56 -080037 mStart(start),
38 mTimeout(timeout) {
Mark Salyzyn77187782015-05-12 15:21:31 -070039}
Mark Salyzyn0175b072014-02-26 09:50:16 -080040
41// runSocketCommand is called once for every open client on the
42// log reader socket. Here we manage and associated the reader
43// client tracking and log region locks LastLogTimes list of
44// LogTimeEntrys, and spawn a transitory per-client thread to
45// work at filing data to the socket.
46//
47// global LogTimeEntry::lock() is used to protect access,
48// reference counts are used to ensure that individual
49// LogTimeEntry lifetime is managed when not protected.
50void FlushCommand::runSocketCommand(SocketClient *client) {
51 LogTimeEntry *entry = NULL;
52 LastLogTimes &times = mReader.logbuf().mTimes;
53
54 LogTimeEntry::lock();
55 LastLogTimes::iterator it = times.begin();
56 while(it != times.end()) {
57 entry = (*it);
58 if (entry->mClient == client) {
Mark Salyzynb75cce02015-11-30 11:35:56 -080059 if (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec) {
60 LogTimeEntry::unlock();
61 return;
62 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080063 entry->triggerReader_Locked();
64 if (entry->runningReader_Locked()) {
65 LogTimeEntry::unlock();
66 return;
67 }
68 entry->incRef_Locked();
69 break;
70 }
71 it++;
72 }
73
74 if (it == times.end()) {
Mark Salyzync03e72c2014-02-18 11:23:53 -080075 // Create LogTimeEntry in notifyNewLog() ?
Mark Salyzyn0175b072014-02-26 09:50:16 -080076 if (mTail == (unsigned long) -1) {
77 LogTimeEntry::unlock();
78 return;
79 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080080 entry = new LogTimeEntry(mReader, client, mNonBlock, mTail, mLogMask,
81 mPid, mStart, mTimeout);
Mark Salyzyn98dca2d2015-08-19 13:10:14 -070082 times.push_front(entry);
Mark Salyzyn0175b072014-02-26 09:50:16 -080083 }
84
85 client->incRef();
86
Mark Salyzync03e72c2014-02-18 11:23:53 -080087 // release client and entry reference counts once done
Mark Salyzyn0175b072014-02-26 09:50:16 -080088 entry->startReader_Locked();
89 LogTimeEntry::unlock();
90}
91
92bool FlushCommand::hasReadLogs(SocketClient *client) {
Mark Salyzyn1114f182014-02-21 13:54:07 -080093 return clientHasLogCredentials(client);
Mark Salyzyn0175b072014-02-26 09:50:16 -080094}