1/*
2 * Copyright (C) 2012-2014 The Android Open Source Project
3 *
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>
18
19#include <private/android_filesystem_config.h>
20
21#include "FlushCommand.h"
22#include "LogBuffer.h"
23#include "LogBufferElement.h"
24#include "LogCommand.h"
25#include "LogReader.h"
26#include "LogTimes.h"
27#include "LogUtils.h"
28
29FlushCommand::FlushCommand(LogReader &reader,
30                           bool nonBlock,
31                           unsigned long tail,
32                           unsigned int logMask,
33                           pid_t pid,
34                           uint64_t start,
35                           uint64_t timeout) :
36        mReader(reader),
37        mNonBlock(nonBlock),
38        mTail(tail),
39        mLogMask(logMask),
40        mPid(pid),
41        mStart(start),
42        mTimeout((start > 1) ? timeout : 0) {
43}
44
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) {
63            if (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec) {
64                LogTimeEntry::unlock();
65                return;
66            }
67            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()) {
79        // Create LogTimeEntry in notifyNewLog() ?
80        if (mTail == (unsigned long) -1) {
81            LogTimeEntry::unlock();
82            return;
83        }
84        entry = new LogTimeEntry(mReader, client, mNonBlock, mTail, mLogMask,
85                                 mPid, mStart, mTimeout);
86        times.push_front(entry);
87    }
88
89    client->incRef();
90
91    // release client and entry reference counts once done
92    entry->startReader_Locked();
93    LogTimeEntry::unlock();
94}
95
96bool FlushCommand::hasReadLogs(SocketClient *client) {
97    return clientHasLogCredentials(client);
98}
99
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}
107