blob: 6a26d00f3d8b9429164d2c710de4fa8789392f69 [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 Salyzyn317bfb92016-02-23 08:55:43 -080019#include <private/android_filesystem_config.h>
20
Mark Salyzyn0175b072014-02-26 09:50:16 -080021#include "FlushCommand.h"
Mark Salyzyn317bfb92016-02-23 08:55:43 -080022#include "LogBuffer.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080023#include "LogBufferElement.h"
Mark Salyzyn1114f182014-02-21 13:54:07 -080024#include "LogCommand.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080025#include "LogReader.h"
Mark Salyzyn1114f182014-02-21 13:54:07 -080026#include "LogTimes.h"
Mark Salyzyn083b0372015-12-04 10:59:45 -080027#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080028
29FlushCommand::FlushCommand(LogReader &reader,
30 bool nonBlock,
31 unsigned long tail,
32 unsigned int logMask,
Mark Salyzynfa3716b2014-02-14 16:05:05 -080033 pid_t pid,
Mark Salyzynb75cce02015-11-30 11:35:56 -080034 uint64_t start,
35 uint64_t timeout) :
Mark Salyzyn77187782015-05-12 15:21:31 -070036 mReader(reader),
37 mNonBlock(nonBlock),
38 mTail(tail),
39 mLogMask(logMask),
40 mPid(pid),
Mark Salyzynb75cce02015-11-30 11:35:56 -080041 mStart(start),
Mark Salyzyn01570972016-01-07 13:21:09 -080042 mTimeout((start > 1) ? timeout : 0) {
Mark Salyzyn77187782015-05-12 15:21:31 -070043}
Mark Salyzyn0175b072014-02-26 09:50:16 -080044
45// runSocketCommand is called once for every open client on the
46// log reader socket. Here we manage and associated the reader
47// client tracking and log region locks LastLogTimes list of
48// LogTimeEntrys, and spawn a transitory per-client thread to
49// work at filing data to the socket.
50//
51// global LogTimeEntry::lock() is used to protect access,
52// reference counts are used to ensure that individual
53// LogTimeEntry lifetime is managed when not protected.
54void FlushCommand::runSocketCommand(SocketClient *client) {
55 LogTimeEntry *entry = NULL;
56 LastLogTimes &times = mReader.logbuf().mTimes;
57
58 LogTimeEntry::lock();
59 LastLogTimes::iterator it = times.begin();
60 while(it != times.end()) {
61 entry = (*it);
62 if (entry->mClient == client) {
Mark Salyzynb75cce02015-11-30 11:35:56 -080063 if (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec) {
64 LogTimeEntry::unlock();
65 return;
66 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080067 entry->triggerReader_Locked();
68 if (entry->runningReader_Locked()) {
69 LogTimeEntry::unlock();
70 return;
71 }
72 entry->incRef_Locked();
73 break;
74 }
75 it++;
76 }
77
78 if (it == times.end()) {
Mark Salyzync03e72c2014-02-18 11:23:53 -080079 // Create LogTimeEntry in notifyNewLog() ?
Mark Salyzyn0175b072014-02-26 09:50:16 -080080 if (mTail == (unsigned long) -1) {
81 LogTimeEntry::unlock();
82 return;
83 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080084 entry = new LogTimeEntry(mReader, client, mNonBlock, mTail, mLogMask,
85 mPid, mStart, mTimeout);
Mark Salyzyn98dca2d2015-08-19 13:10:14 -070086 times.push_front(entry);
Mark Salyzyn0175b072014-02-26 09:50:16 -080087 }
88
89 client->incRef();
90
Mark Salyzync03e72c2014-02-18 11:23:53 -080091 // release client and entry reference counts once done
Mark Salyzyn0175b072014-02-26 09:50:16 -080092 entry->startReader_Locked();
93 LogTimeEntry::unlock();
94}
95
96bool FlushCommand::hasReadLogs(SocketClient *client) {
Mark Salyzyn1114f182014-02-21 13:54:07 -080097 return clientHasLogCredentials(client);
Mark Salyzyn0175b072014-02-26 09:50:16 -080098}
Mark Salyzyn8fa88962016-01-26 14:32:35 -080099
100static bool clientHasSecurityCredentials(SocketClient *client) {
101 return (client->getUid() == AID_SYSTEM) || (client->getGid() == AID_SYSTEM);
102}
103
104bool FlushCommand::hasSecurityLogs(SocketClient *client) {
105 return clientHasSecurityCredentials(client);
106}