blob: c67d2bfb114805f8ffe750129016475141e76401 [file] [log] [blame]
Mark Salyzyn12bac902014-02-26 09:50:16 -08001/*
Mark Salyzyne6477492014-02-21 13:54:07 -08002 * Copyright (C) 2012-2014 The Android Open Source Project
Mark Salyzyn12bac902014-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 Salyzyne6477492014-02-21 13:54:07 -080018
Mark Salyzyn51053da2016-02-23 08:55:43 -080019#include <private/android_filesystem_config.h>
20
Mark Salyzyn12bac902014-02-26 09:50:16 -080021#include "FlushCommand.h"
Mark Salyzyn51053da2016-02-23 08:55:43 -080022#include "LogBuffer.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080023#include "LogBufferElement.h"
Mark Salyzyne6477492014-02-21 13:54:07 -080024#include "LogCommand.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080025#include "LogReader.h"
Mark Salyzyne6477492014-02-21 13:54:07 -080026#include "LogTimes.h"
Mark Salyzyn8885daf2015-12-04 10:59:45 -080027#include "LogUtils.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080028
Mark Salyzynda65bcb2017-03-10 14:31:54 -080029FlushCommand::FlushCommand(LogReader& reader, bool nonBlock, unsigned long tail,
Mark Salyzynda1b4a12017-03-10 08:44:14 -080030 unsigned int logMask, pid_t pid, log_time start,
Mark Salyzynda65bcb2017-03-10 14:31:54 -080031 uint64_t timeout)
32 : mReader(reader),
33 mNonBlock(nonBlock),
34 mTail(tail),
35 mLogMask(logMask),
36 mPid(pid),
37 mStart(start),
Mark Salyzynda1b4a12017-03-10 08:44:14 -080038 mTimeout((start != log_time::EPOCH) ? timeout : 0) {
Mark Salyzyncd766f92015-05-12 15:21:31 -070039}
Mark Salyzyn12bac902014-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.
Mark Salyzynda65bcb2017-03-10 14:31:54 -080050void FlushCommand::runSocketCommand(SocketClient* client) {
51 LogTimeEntry* entry = NULL;
52 LastLogTimes& times = mReader.logbuf().mTimes;
Mark Salyzyn12bac902014-02-26 09:50:16 -080053
54 LogTimeEntry::lock();
55 LastLogTimes::iterator it = times.begin();
Mark Salyzynda65bcb2017-03-10 14:31:54 -080056 while (it != times.end()) {
Mark Salyzyn12bac902014-02-26 09:50:16 -080057 entry = (*it);
58 if (entry->mClient == client) {
Mark Salyzyn552b4752015-11-30 11:35:56 -080059 if (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec) {
Mark Salyzyn7515f4d2017-03-14 13:23:06 -070060 if (mReader.logbuf().isMonotonic()) {
61 LogTimeEntry::unlock();
62 return;
63 }
64 // If the user changes the time in a gross manner that
65 // invalidates the timeout, fall through and trigger.
66 log_time now(CLOCK_REALTIME);
67 if (((entry->mEnd + entry->mTimeout) > now) &&
68 (now > entry->mEnd)) {
69 LogTimeEntry::unlock();
70 return;
71 }
Mark Salyzyn552b4752015-11-30 11:35:56 -080072 }
Mark Salyzyn12bac902014-02-26 09:50:16 -080073 entry->triggerReader_Locked();
74 if (entry->runningReader_Locked()) {
75 LogTimeEntry::unlock();
76 return;
77 }
78 entry->incRef_Locked();
79 break;
80 }
81 it++;
82 }
83
84 if (it == times.end()) {
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -080085 // Create LogTimeEntry in notifyNewLog() ?
Mark Salyzynda65bcb2017-03-10 14:31:54 -080086 if (mTail == (unsigned long)-1) {
Mark Salyzyn12bac902014-02-26 09:50:16 -080087 LogTimeEntry::unlock();
88 return;
89 }
Mark Salyzyn552b4752015-11-30 11:35:56 -080090 entry = new LogTimeEntry(mReader, client, mNonBlock, mTail, mLogMask,
91 mPid, mStart, mTimeout);
Mark Salyzyn3eecd8c2015-08-19 13:10:14 -070092 times.push_front(entry);
Mark Salyzyn12bac902014-02-26 09:50:16 -080093 }
94
95 client->incRef();
96
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -080097 // release client and entry reference counts once done
Mark Salyzyn12bac902014-02-26 09:50:16 -080098 entry->startReader_Locked();
99 LogTimeEntry::unlock();
100}
101
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800102bool FlushCommand::hasReadLogs(SocketClient* client) {
Mark Salyzyne6477492014-02-21 13:54:07 -0800103 return clientHasLogCredentials(client);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800104}
Mark Salyzyn924c0b32016-01-26 14:32:35 -0800105
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800106static bool clientHasSecurityCredentials(SocketClient* client) {
Mark Salyzyn924c0b32016-01-26 14:32:35 -0800107 return (client->getUid() == AID_SYSTEM) || (client->getGid() == AID_SYSTEM);
108}
109
Mark Salyzynda65bcb2017-03-10 14:31:54 -0800110bool FlushCommand::hasSecurityLogs(SocketClient* client) {
Mark Salyzyn924c0b32016-01-26 14:32:35 -0800111 return clientHasSecurityCredentials(client);
112}