blob: 1306a467e5c48a8f50a484c87b8a657df563741d [file] [log] [blame]
Yao Chena80e5c02018-09-04 13:55:29 -07001/*
2 * Copyright (C) 2018 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#define DEBUG true // STOPSHIP if true
17#include "Log.h"
18
19#include "ShellSubscriber.h"
20
21#include "matchers/matcher_util.h"
22
23#include <android-base/file.h>
24
25using android::util::ProtoOutputStream;
26
27namespace android {
28namespace os {
29namespace statsd {
30
31void ShellSubscriber::startNewSubscription(int in, int out, sp<IResultReceiver> resultReceiver) {
32 VLOG("start new shell subscription");
33 {
34 std::lock_guard<std::mutex> lock(mMutex);
35 if (mResultReceiver != nullptr) {
36 VLOG("Only one shell subscriber is allowed.");
37 return;
38 }
39 mInput = in;
40 mOutput = out;
41 mResultReceiver = resultReceiver;
42 IInterface::asBinder(mResultReceiver)->linkToDeath(this);
43 }
44
45 // Spawn another thread to read the config updates from the input file descriptor
46 std::thread reader([in, this] { readConfig(in); });
47 reader.detach();
48
49 std::unique_lock<std::mutex> lk(mMutex);
50
51 mShellDied.wait(lk, [this, resultReceiver] { return mResultReceiver != resultReceiver; });
52 if (reader.joinable()) {
53 reader.join();
54 }
55}
56
57void ShellSubscriber::updateConfig(const ShellSubscription& config) {
58 std::lock_guard<std::mutex> lock(mMutex);
59 mPushedMatchers.clear();
60 for (const auto& pushed : config.pushed()) {
61 mPushedMatchers.push_back(pushed);
62 VLOG("adding matcher for atom %d", pushed.atom_id());
63 }
64}
65
66void ShellSubscriber::readConfig(int in) {
67 if (in <= 0) {
68 return;
69 }
70
71 while (1) {
72 size_t bufferSize = 0;
73 int result = 0;
74 if ((result = read(in, &bufferSize, sizeof(bufferSize))) == 0) {
75 VLOG("Done reading");
76 break;
77 } else if (result < 0 || result != sizeof(bufferSize)) {
78 ALOGE("Error reading config size");
79 break;
80 }
81
82 vector<uint8_t> buffer(bufferSize);
83 if ((result = read(in, buffer.data(), bufferSize)) > 0 && ((size_t)result) == bufferSize) {
84 ShellSubscription config;
85 if (config.ParseFromArray(buffer.data(), bufferSize)) {
86 updateConfig(config);
87 } else {
88 ALOGE("error parsing the config");
89 break;
90 }
91 } else {
92 VLOG("Error reading the config, returned: %d, expecting %zu", result, bufferSize);
93 break;
94 }
95 }
96}
97
98void ShellSubscriber::cleanUpLocked() {
99 // The file descriptors will be closed by binder.
100 mInput = 0;
101 mOutput = 0;
102 mResultReceiver = nullptr;
103 mPushedMatchers.clear();
104 VLOG("done clean up");
105}
106
107void ShellSubscriber::onLogEvent(const LogEvent& event) {
108 std::lock_guard<std::mutex> lock(mMutex);
109
110 if (mOutput <= 0) {
111 return;
112 }
113
114 for (const auto& matcher : mPushedMatchers) {
115 if (matchesSimple(*mUidMap, matcher, event)) {
Yao Chen398dd192018-10-01 14:49:03 -0700116 event.ToProto(mProto);
Yao Chena80e5c02018-09-04 13:55:29 -0700117 // First write the payload size.
118 size_t bufferSize = mProto.size();
119 write(mOutput, &bufferSize, sizeof(bufferSize));
120
121 // Then write the payload.
Yao Chena80e5c02018-09-04 13:55:29 -0700122 mProto.flush(mOutput);
123 mProto.clear();
124 break;
125 }
126 }
127}
128
129void ShellSubscriber::binderDied(const wp<IBinder>& who) {
130 {
131 VLOG("Shell exits");
132 std::lock_guard<std::mutex> lock(mMutex);
133 cleanUpLocked();
134 }
135 mShellDied.notify_all();
136}
137
138} // namespace statsd
139} // namespace os
Yao Chen398dd192018-10-01 14:49:03 -0700140} // namespace android