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" |
| 20 | |
| 21 | #include <binder/IPCThreadState.h> |
| 22 | #include <binder/IServiceManager.h> |
| 23 | #include <cutils/log.h> |
| 24 | #include <private/android_filesystem_config.h> |
| 25 | #include <utils/Looper.h> |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame^] | 26 | #include <utils/String16.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 27 | |
| 28 | #include <unistd.h> |
| 29 | #include <stdio.h> |
| 30 | |
| 31 | using namespace android; |
| 32 | |
| 33 | // ================================================================================ |
| 34 | StatsService::StatsService(const sp<Looper>& handlerLooper) |
| 35 | { |
| 36 | ALOGD("stats service constructed"); |
| 37 | } |
| 38 | |
| 39 | StatsService::~StatsService() |
| 40 | { |
| 41 | } |
| 42 | |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame^] | 43 | // Implement our own because the default binder implementation isn't |
| 44 | // properly handling SHELL_COMMAND_TRANSACTION |
| 45 | status_t |
| 46 | StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 47 | { |
| 48 | status_t err; |
| 49 | |
| 50 | switch (code) { |
| 51 | case SHELL_COMMAND_TRANSACTION: { |
| 52 | int in = data.readFileDescriptor(); |
| 53 | int out = data.readFileDescriptor(); |
| 54 | int err = data.readFileDescriptor(); |
| 55 | int argc = data.readInt32(); |
| 56 | Vector<String8> args; |
| 57 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 58 | args.add(String8(data.readString16())); |
| 59 | } |
| 60 | sp<IShellCallback> shellCallback = IShellCallback::asInterface( |
| 61 | data.readStrongBinder()); |
| 62 | sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface( |
| 63 | data.readStrongBinder()); |
| 64 | |
| 65 | FILE* fin = fdopen(in, "r"); |
| 66 | FILE* fout = fdopen(out, "w"); |
| 67 | FILE* ferr = fdopen(err, "w"); |
| 68 | |
| 69 | if (fin == NULL || fout == NULL || ferr == NULL) { |
| 70 | resultReceiver->send(NO_MEMORY); |
| 71 | } else { |
| 72 | err = command(fin, fout, ferr, args); |
| 73 | resultReceiver->send(err); |
| 74 | } |
| 75 | |
| 76 | if (fin != NULL) { |
| 77 | fflush(fin); |
| 78 | fclose(fin); |
| 79 | } |
| 80 | if (fout != NULL) { |
| 81 | fflush(fout); |
| 82 | fclose(fout); |
| 83 | } |
| 84 | if (fout != NULL) { |
| 85 | fflush(ferr); |
| 86 | fclose(ferr); |
| 87 | } |
| 88 | |
| 89 | return NO_ERROR; |
| 90 | } |
| 91 | default: { |
| 92 | return BnStatsManager::onTransact(code, data, reply, flags); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 97 | status_t |
| 98 | StatsService::dump(int fd, const Vector<String16>& args) |
| 99 | { |
| 100 | FILE* out = fdopen(fd, "w"); |
| 101 | if (out == NULL) { |
| 102 | return NO_MEMORY; // the fd is already open |
| 103 | } |
| 104 | |
| 105 | fprintf(out, "StatsService::dump:"); |
| 106 | ALOGD("StatsService::dump:"); |
| 107 | const int N = args.size(); |
| 108 | for (int i=0; i<N; i++) { |
| 109 | fprintf(out, " %s", String8(args[i]).string()); |
| 110 | ALOGD(" %s", String8(args[i]).string()); |
| 111 | } |
| 112 | fprintf(out, "\n"); |
| 113 | |
| 114 | fclose(out); |
| 115 | return NO_ERROR; |
| 116 | } |
| 117 | |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame^] | 118 | status_t |
| 119 | StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) |
| 120 | { |
| 121 | fprintf(out, "StatsService::command:"); |
| 122 | ALOGD("StatsService::command:"); |
| 123 | const int N = args.size(); |
| 124 | for (int i=0; i<N; i++) { |
| 125 | fprintf(out, " %s", String8(args[i]).string()); |
| 126 | ALOGD(" %s", String8(args[i]).string()); |
| 127 | } |
| 128 | fprintf(out, "\n"); |
| 129 | |
| 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 | |