blob: 030c760c8643ec4573b72c31cff08d44f1cf666c [file] [log] [blame]
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07001/*
2 * Copyright (C) 2016 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#define LOG_TAG "statsd"
18
19#include "StatsService.h"
Yao Chen482d2722017-09-12 13:25:43 -070020#include "DropboxReader.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070021
22#include <binder/IPCThreadState.h>
23#include <binder/IServiceManager.h>
24#include <cutils/log.h>
25#include <private/android_filesystem_config.h>
26#include <utils/Looper.h>
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070027#include <utils/String16.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070028
29#include <unistd.h>
30#include <stdio.h>
Yao Chen482d2722017-09-12 13:25:43 -070031#include <stdlib.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070032
33using namespace android;
34
35// ================================================================================
36StatsService::StatsService(const sp<Looper>& handlerLooper)
37{
38 ALOGD("stats service constructed");
39}
40
41StatsService::~StatsService()
42{
43}
44
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070045// Implement our own because the default binder implementation isn't
46// properly handling SHELL_COMMAND_TRANSACTION
47status_t
48StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
49{
50 status_t err;
51
52 switch (code) {
53 case SHELL_COMMAND_TRANSACTION: {
54 int in = data.readFileDescriptor();
55 int out = data.readFileDescriptor();
56 int err = data.readFileDescriptor();
57 int argc = data.readInt32();
58 Vector<String8> args;
59 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
60 args.add(String8(data.readString16()));
61 }
62 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
63 data.readStrongBinder());
64 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
65 data.readStrongBinder());
66
67 FILE* fin = fdopen(in, "r");
68 FILE* fout = fdopen(out, "w");
69 FILE* ferr = fdopen(err, "w");
70
71 if (fin == NULL || fout == NULL || ferr == NULL) {
72 resultReceiver->send(NO_MEMORY);
73 } else {
74 err = command(fin, fout, ferr, args);
75 resultReceiver->send(err);
76 }
77
78 if (fin != NULL) {
79 fflush(fin);
80 fclose(fin);
81 }
82 if (fout != NULL) {
83 fflush(fout);
84 fclose(fout);
85 }
86 if (fout != NULL) {
87 fflush(ferr);
88 fclose(ferr);
89 }
90
91 return NO_ERROR;
92 }
93 default: {
94 return BnStatsManager::onTransact(code, data, reply, flags);
95 }
96 }
97}
98
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070099status_t
100StatsService::dump(int fd, const Vector<String16>& args)
101{
102 FILE* out = fdopen(fd, "w");
103 if (out == NULL) {
104 return NO_MEMORY; // the fd is already open
105 }
106
107 fprintf(out, "StatsService::dump:");
108 ALOGD("StatsService::dump:");
109 const int N = args.size();
110 for (int i=0; i<N; i++) {
111 fprintf(out, " %s", String8(args[i]).string());
112 ALOGD(" %s", String8(args[i]).string());
113 }
114 fprintf(out, "\n");
115
116 fclose(out);
117 return NO_ERROR;
118}
119
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700120status_t
121StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args)
122{
Yao Chen482d2722017-09-12 13:25:43 -0700123 if (args.size() > 0) {
124 if (!args[0].compare(String8("print-stats-log")) && args.size() > 1) {
125 return doPrintStatsLog(out, args);
126 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700127 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700128
Yao Chen482d2722017-09-12 13:25:43 -0700129 printCmdHelp(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700130 return NO_ERROR;
131}
132
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700133Status
134StatsService::systemRunning()
135{
136 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
137 return Status::fromExceptionCode(Status::EX_SECURITY,
138 "Only system uid can call systemRunning");
139 }
140
141 // When system_server is up and running, schedule the dropbox task to run.
142 ALOGD("StatsService::systemRunning");
143
144 return Status::ok();
145}
146
Yao Chen482d2722017-09-12 13:25:43 -0700147status_t
148StatsService::doPrintStatsLog(FILE* out, const Vector<String8>& args) {
149 long msec = 0;
150
151 if (args.size() > 2) {
152 msec = strtol(args[2].string(), NULL, 10);
153 }
154 return DropboxReader::readStatsLogs(out, args[1].string(), msec);
155}
156
157void
158StatsService::printCmdHelp(FILE* out) {
159 fprintf(out, "Usage:\n");
160 fprintf(out, "\t print-stats-log [tag_required] [timestamp_nsec_optional]\n");
161}