blob: 70ecbe0a5a0395f7897c84e7cccc9539436645f8 [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 Salyzyn2ad0bd02016-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 Salyzyn2ad0bd02016-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
Mark Salyzyn0175b072014-02-26 09:50:16 -080029// runSocketCommand is called once for every open client on the
30// log reader socket. Here we manage and associated the reader
31// client tracking and log region locks LastLogTimes list of
32// LogTimeEntrys, and spawn a transitory per-client thread to
33// work at filing data to the socket.
34//
Mark Salyzyn3c501b52017-04-18 14:09:45 -070035// global LogTimeEntry::wrlock() is used to protect access,
Mark Salyzyn0175b072014-02-26 09:50:16 -080036// reference counts are used to ensure that individual
37// LogTimeEntry lifetime is managed when not protected.
Mark Salyzyn501c3732017-03-10 14:31:54 -080038void FlushCommand::runSocketCommand(SocketClient* client) {
39 LogTimeEntry* entry = NULL;
40 LastLogTimes& times = mReader.logbuf().mTimes;
Mark Salyzyn0175b072014-02-26 09:50:16 -080041
Mark Salyzyn3c501b52017-04-18 14:09:45 -070042 LogTimeEntry::wrlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -080043 LastLogTimes::iterator it = times.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -080044 while (it != times.end()) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080045 entry = (*it);
46 if (entry->mClient == client) {
Hao Wangf6e22962017-12-04 14:10:40 +080047 if (!entry->isWatchingMultiple(mLogMask)) {
48 LogTimeEntry::unlock();
49 return;
50 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080051 if (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec) {
Mark Salyzyn5e001772017-03-14 13:23:06 -070052 if (mReader.logbuf().isMonotonic()) {
53 LogTimeEntry::unlock();
54 return;
55 }
56 // If the user changes the time in a gross manner that
57 // invalidates the timeout, fall through and trigger.
58 log_time now(CLOCK_REALTIME);
59 if (((entry->mEnd + entry->mTimeout) > now) &&
60 (now > entry->mEnd)) {
61 LogTimeEntry::unlock();
62 return;
63 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080064 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080065 entry->triggerReader_Locked();
66 if (entry->runningReader_Locked()) {
67 LogTimeEntry::unlock();
68 return;
69 }
70 entry->incRef_Locked();
71 break;
72 }
73 it++;
74 }
75
76 if (it == times.end()) {
Mark Salyzync03e72c2014-02-18 11:23:53 -080077 // Create LogTimeEntry in notifyNewLog() ?
Mark Salyzyn501c3732017-03-10 14:31:54 -080078 if (mTail == (unsigned long)-1) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080079 LogTimeEntry::unlock();
80 return;
81 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080082 entry = new LogTimeEntry(mReader, client, mNonBlock, mTail, mLogMask,
83 mPid, mStart, mTimeout);
Mark Salyzyn98dca2d2015-08-19 13:10:14 -070084 times.push_front(entry);
Mark Salyzyn0175b072014-02-26 09:50:16 -080085 }
86
87 client->incRef();
88
Mark Salyzync03e72c2014-02-18 11:23:53 -080089 // release client and entry reference counts once done
Mark Salyzyn0175b072014-02-26 09:50:16 -080090 entry->startReader_Locked();
91 LogTimeEntry::unlock();
92}
93
Mark Salyzyn501c3732017-03-10 14:31:54 -080094bool FlushCommand::hasReadLogs(SocketClient* client) {
Mark Salyzyn1114f182014-02-21 13:54:07 -080095 return clientHasLogCredentials(client);
Mark Salyzyn0175b072014-02-26 09:50:16 -080096}
Mark Salyzyn8fa88962016-01-26 14:32:35 -080097
Mark Salyzyn501c3732017-03-10 14:31:54 -080098static bool clientHasSecurityCredentials(SocketClient* client) {
Mark Salyzyn8fa88962016-01-26 14:32:35 -080099 return (client->getUid() == AID_SYSTEM) || (client->getGid() == AID_SYSTEM);
100}
101
Mark Salyzyn501c3732017-03-10 14:31:54 -0800102bool FlushCommand::hasSecurityLogs(SocketClient* client) {
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800103 return clientHasSecurityCredentials(client);
104}