Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame^] | 20 | #include "DropboxReader.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 21 | |
| 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 Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 27 | #include <utils/String16.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 28 | |
| 29 | #include <unistd.h> |
| 30 | #include <stdio.h> |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame^] | 31 | #include <stdlib.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 32 | |
| 33 | using namespace android; |
| 34 | |
| 35 | // ================================================================================ |
| 36 | StatsService::StatsService(const sp<Looper>& handlerLooper) |
| 37 | { |
| 38 | ALOGD("stats service constructed"); |
| 39 | } |
| 40 | |
| 41 | StatsService::~StatsService() |
| 42 | { |
| 43 | } |
| 44 | |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 45 | // Implement our own because the default binder implementation isn't |
| 46 | // properly handling SHELL_COMMAND_TRANSACTION |
| 47 | status_t |
| 48 | StatsService::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 Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 99 | status_t |
| 100 | StatsService::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 Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 120 | status_t |
| 121 | StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) |
| 122 | { |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame^] | 123 | if (args.size() > 0) { |
| 124 | if (!args[0].compare(String8("print-stats-log")) && args.size() > 1) { |
| 125 | return doPrintStatsLog(out, args); |
| 126 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 127 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 128 | |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame^] | 129 | printCmdHelp(out); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 130 | return NO_ERROR; |
| 131 | } |
| 132 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 133 | Status |
| 134 | StatsService::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 Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame^] | 147 | status_t |
| 148 | StatsService::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 | |
| 157 | void |
| 158 | StatsService::printCmdHelp(FILE* out) { |
| 159 | fprintf(out, "Usage:\n"); |
| 160 | fprintf(out, "\t print-stats-log [tag_required] [timestamp_nsec_optional]\n"); |
| 161 | } |