blob: fd45c4a0a8c3d312c2e7f186587415c9695e0edd [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 Salyzyn083b0372015-12-04 10:59:45 -080024#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080025
26FlushCommand::FlushCommand(LogReader &reader,
27 bool nonBlock,
28 unsigned long tail,
29 unsigned int logMask,
Mark Salyzynfa3716b2014-02-14 16:05:05 -080030 pid_t pid,
Mark Salyzynb75cce02015-11-30 11:35:56 -080031 uint64_t start,
32 uint64_t timeout) :
Mark Salyzyn77187782015-05-12 15:21:31 -070033 mReader(reader),
34 mNonBlock(nonBlock),
35 mTail(tail),
36 mLogMask(logMask),
37 mPid(pid),
Mark Salyzynb75cce02015-11-30 11:35:56 -080038 mStart(start),
Mark Salyzyn01570972016-01-07 13:21:09 -080039 mTimeout((start > 1) ? timeout : 0) {
Mark Salyzyn77187782015-05-12 15:21:31 -070040}
Mark Salyzyn0175b072014-02-26 09:50:16 -080041
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.
51void FlushCommand::runSocketCommand(SocketClient *client) {
52 LogTimeEntry *entry = NULL;
53 LastLogTimes &times = 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 Salyzynb75cce02015-11-30 11:35:56 -080060 if (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec) {
61 LogTimeEntry::unlock();
62 return;
63 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080064 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 Salyzync03e72c2014-02-18 11:23:53 -080076 // Create LogTimeEntry in notifyNewLog() ?
Mark Salyzyn0175b072014-02-26 09:50:16 -080077 if (mTail == (unsigned long) -1) {
78 LogTimeEntry::unlock();
79 return;
80 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080081 entry = new LogTimeEntry(mReader, client, mNonBlock, mTail, mLogMask,
82 mPid, mStart, mTimeout);
Mark Salyzyn98dca2d2015-08-19 13:10:14 -070083 times.push_front(entry);
Mark Salyzyn0175b072014-02-26 09:50:16 -080084 }
85
86 client->incRef();
87
Mark Salyzync03e72c2014-02-18 11:23:53 -080088 // release client and entry reference counts once done
Mark Salyzyn0175b072014-02-26 09:50:16 -080089 entry->startReader_Locked();
90 LogTimeEntry::unlock();
91}
92
93bool FlushCommand::hasReadLogs(SocketClient *client) {
Mark Salyzyn1114f182014-02-21 13:54:07 -080094 return clientHasLogCredentials(client);
Mark Salyzyn0175b072014-02-26 09:50:16 -080095}
Mark Salyzyn8fa88962016-01-26 14:32:35 -080096
97static bool clientHasSecurityCredentials(SocketClient *client) {
98 return (client->getUid() == AID_SYSTEM) || (client->getGid() == AID_SYSTEM);
99}
100
101bool FlushCommand::hasSecurityLogs(SocketClient *client) {
102 return clientHasSecurityCredentials(client);
103}