| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [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 "incident" | 
|  | 18 |  | 
|  | 19 | #include "incident_sections.h" | 
|  | 20 |  | 
|  | 21 | #include <android/os/BnIncidentReportStatusListener.h> | 
|  | 22 | #include <android/os/IIncidentManager.h> | 
|  | 23 | #include <android/os/IncidentReportArgs.h> | 
|  | 24 | #include <binder/IPCThreadState.h> | 
|  | 25 | #include <binder/IServiceManager.h> | 
|  | 26 | #include <utils/Looper.h> | 
|  | 27 |  | 
| Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 28 | #include <cstring> | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 29 | #include <fcntl.h> | 
|  | 30 | #include <getopt.h> | 
|  | 31 | #include <stdio.h> | 
|  | 32 | #include <stdlib.h> | 
|  | 33 | #include <unistd.h> | 
|  | 34 |  | 
|  | 35 | using namespace android; | 
|  | 36 | using namespace android::base; | 
|  | 37 | using namespace android::binder; | 
|  | 38 | using namespace android::os; | 
|  | 39 |  | 
|  | 40 | // ================================================================================ | 
|  | 41 | class StatusListener : public BnIncidentReportStatusListener { | 
|  | 42 | public: | 
|  | 43 | StatusListener(); | 
|  | 44 | virtual ~StatusListener(); | 
|  | 45 |  | 
|  | 46 | virtual Status onReportStarted(); | 
|  | 47 | virtual Status onReportSectionStatus(int32_t section, int32_t status); | 
|  | 48 | virtual Status onReportServiceStatus(const String16& service, int32_t status); | 
|  | 49 | virtual Status onReportFinished(); | 
|  | 50 | virtual Status onReportFailed(); | 
|  | 51 | }; | 
|  | 52 |  | 
|  | 53 | StatusListener::StatusListener() | 
|  | 54 | { | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | StatusListener::~StatusListener() | 
|  | 58 | { | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | Status | 
|  | 62 | StatusListener::onReportStarted() | 
|  | 63 | { | 
|  | 64 | return Status::ok(); | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | Status | 
|  | 68 | StatusListener::onReportSectionStatus(int32_t section, int32_t status) | 
|  | 69 | { | 
|  | 70 | fprintf(stderr, "section %d status %d\n", section, status); | 
|  | 71 | return Status::ok(); | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | Status | 
|  | 75 | StatusListener::onReportServiceStatus(const String16& service, int32_t status) | 
|  | 76 | { | 
|  | 77 | fprintf(stderr, "service '%s' status %d\n", String8(service).string(), status); | 
|  | 78 | return Status::ok(); | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | Status | 
|  | 82 | StatusListener::onReportFinished() | 
|  | 83 | { | 
|  | 84 | fprintf(stderr, "done\n"); | 
|  | 85 | exit(0); | 
|  | 86 | return Status::ok(); | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | Status | 
|  | 90 | StatusListener::onReportFailed() | 
|  | 91 | { | 
|  | 92 | fprintf(stderr, "failed\n"); | 
|  | 93 | exit(1); | 
|  | 94 | return Status::ok(); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | // ================================================================================ | 
| Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 98 | static void section_list(FILE* out) { | 
|  | 99 | IncidentSection sections[INCIDENT_SECTION_COUNT]; | 
|  | 100 | int i = 0; | 
|  | 101 | int j = 0; | 
|  | 102 | // sort the sections based on id | 
|  | 103 | while (i < INCIDENT_SECTION_COUNT) { | 
|  | 104 | IncidentSection curr = INCIDENT_SECTIONS[i]; | 
|  | 105 | for (int k = 0; k < j; k++) { | 
|  | 106 | if (curr.id > sections[k].id) { | 
|  | 107 | continue; | 
|  | 108 | } | 
|  | 109 | IncidentSection tmp = curr; | 
|  | 110 | curr = sections[k]; | 
|  | 111 | sections[k] = tmp; | 
|  | 112 | } | 
|  | 113 | sections[j] = curr; | 
|  | 114 | i++; | 
|  | 115 | j++; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | fprintf(out, "available sections:\n"); | 
|  | 119 | for (int i = 0; i < INCIDENT_SECTION_COUNT; ++i) { | 
|  | 120 | fprintf(out, "id: %4d, name: %s\n", sections[i].id, sections[i].name); | 
|  | 121 | } | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | // ================================================================================ | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 125 | static IncidentSection const* | 
|  | 126 | find_section(const char* name) | 
|  | 127 | { | 
|  | 128 | size_t low = 0; | 
|  | 129 | size_t high = INCIDENT_SECTION_COUNT - 1; | 
|  | 130 |  | 
|  | 131 | while (low <= high) { | 
|  | 132 | size_t mid = (low + high) >> 1; | 
|  | 133 | IncidentSection const* section = INCIDENT_SECTIONS + mid; | 
|  | 134 |  | 
|  | 135 | int cmp = strcmp(section->name, name); | 
|  | 136 | if (cmp < 0) { | 
|  | 137 | low = mid + 1; | 
|  | 138 | } else if (cmp > 0) { | 
|  | 139 | high = mid - 1; | 
|  | 140 | } else { | 
|  | 141 | return section; | 
|  | 142 | } | 
|  | 143 | } | 
|  | 144 | return NULL; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | // ================================================================================ | 
| Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 148 | static int | 
|  | 149 | get_dest(const char* arg) | 
|  | 150 | { | 
|  | 151 | if (strcmp(arg, "LOCAL") == 0) return 0; | 
|  | 152 | if (strcmp(arg, "EXPLICIT") == 0) return 1; | 
|  | 153 | if (strcmp(arg, "AUTOMATIC") == 0) return 2; | 
|  | 154 | return -1; // return the default value | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | // ================================================================================ | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 158 | static void | 
|  | 159 | usage(FILE* out) | 
|  | 160 | { | 
|  | 161 | fprintf(out, "usage: incident OPTIONS [SECTION...]\n"); | 
|  | 162 | fprintf(out, "\n"); | 
|  | 163 | fprintf(out, "Takes an incident report.\n"); | 
|  | 164 | fprintf(out, "\n"); | 
|  | 165 | fprintf(out, "OPTIONS\n"); | 
|  | 166 | fprintf(out, "  -b           (default) print the report to stdout (in proto format)\n"); | 
|  | 167 | fprintf(out, "  -d           send the report into dropbox\n"); | 
| Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 168 | fprintf(out, "  -l           list available sections\n"); | 
| Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 169 | fprintf(out, "  -p           privacy spec, LOCAL, EXPLICIT or AUTOMATIC\n"); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 170 | fprintf(out, "\n"); | 
|  | 171 | fprintf(out, "  SECTION     the field numbers of the incident report fields to include\n"); | 
|  | 172 | fprintf(out, "\n"); | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | int | 
|  | 176 | main(int argc, char** argv) | 
|  | 177 | { | 
|  | 178 | Status status; | 
|  | 179 | IncidentReportArgs args; | 
|  | 180 | enum { DEST_DROPBOX, DEST_STDOUT } destination = DEST_STDOUT; | 
| Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 181 | int dest = -1; // default | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 182 |  | 
|  | 183 | // Parse the args | 
|  | 184 | int opt; | 
| Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 185 | while ((opt = getopt(argc, argv, "bhdlp:")) != -1) { | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 186 | switch (opt) { | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 187 | case 'h': | 
|  | 188 | usage(stdout); | 
|  | 189 | return 0; | 
| Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 190 | case 'l': | 
|  | 191 | section_list(stdout); | 
|  | 192 | return 0; | 
|  | 193 | case 'b': | 
|  | 194 | destination = DEST_STDOUT; | 
|  | 195 | break; | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 196 | case 'd': | 
|  | 197 | destination = DEST_DROPBOX; | 
|  | 198 | break; | 
| Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 199 | case 'p': | 
|  | 200 | dest = get_dest(optarg); | 
|  | 201 | break; | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 202 | default: | 
|  | 203 | usage(stderr); | 
|  | 204 | return 1; | 
|  | 205 | } | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | if (optind == argc) { | 
|  | 209 | args.setAll(true); | 
|  | 210 | } else { | 
|  | 211 | for (int i=optind; i<argc; i++) { | 
|  | 212 | const char* arg = argv[i]; | 
|  | 213 | char* end; | 
|  | 214 | if (arg[0] != '\0') { | 
|  | 215 | int section = strtol(arg, &end, 0); | 
|  | 216 | if (*end == '\0') { | 
|  | 217 | args.addSection(section); | 
|  | 218 | } else { | 
|  | 219 | IncidentSection const* ic = find_section(arg); | 
|  | 220 | if (ic == NULL) { | 
|  | 221 | fprintf(stderr, "Invalid section: %s\n", arg); | 
|  | 222 | return 1; | 
|  | 223 | } | 
|  | 224 | args.addSection(ic->id); | 
|  | 225 | } | 
|  | 226 | } | 
|  | 227 | } | 
|  | 228 | } | 
| Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 229 | args.setDest(dest); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 230 |  | 
|  | 231 | // Start the thread pool. | 
|  | 232 | sp<ProcessState> ps(ProcessState::self()); | 
|  | 233 | ps->startThreadPool(); | 
|  | 234 | ps->giveThreadPoolName(); | 
|  | 235 |  | 
|  | 236 | // Look up the service | 
|  | 237 | sp<IIncidentManager> service = interface_cast<IIncidentManager>( | 
|  | 238 | defaultServiceManager()->getService(android::String16("incident"))); | 
|  | 239 | if (service == NULL) { | 
|  | 240 | fprintf(stderr, "Couldn't look up the incident service\n"); | 
|  | 241 | return 1; | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | // Construct the stream | 
|  | 245 | int fds[2]; | 
|  | 246 | pipe(fds); | 
|  | 247 |  | 
|  | 248 | unique_fd readEnd(fds[0]); | 
|  | 249 | unique_fd writeEnd(fds[1]); | 
|  | 250 |  | 
|  | 251 | if (destination == DEST_STDOUT) { | 
|  | 252 | // Call into the service | 
|  | 253 | sp<StatusListener> listener(new StatusListener()); | 
|  | 254 | status = service->reportIncidentToStream(args, listener, writeEnd); | 
|  | 255 |  | 
|  | 256 | if (!status.isOk()) { | 
|  | 257 | fprintf(stderr, "reportIncident returned \"%s\"\n", status.toString8().string()); | 
| Yi Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 258 | return 1; | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 259 | } | 
|  | 260 |  | 
|  | 261 | // Wait for the result and print out the data they send. | 
|  | 262 | //IPCThreadState::self()->joinThreadPool(); | 
|  | 263 |  | 
|  | 264 | while (true) { | 
|  | 265 | int amt = splice(fds[0], NULL, STDOUT_FILENO, NULL, 4096, 0); | 
|  | 266 | fprintf(stderr, "spliced %d bytes\n", amt); | 
|  | 267 | if (amt < 0) { | 
|  | 268 | return errno; | 
|  | 269 | } else if (amt == 0) { | 
|  | 270 | return 0; | 
|  | 271 | } | 
|  | 272 | } | 
|  | 273 | } else { | 
|  | 274 | status = service->reportIncident(args); | 
|  | 275 | if (!status.isOk()) { | 
|  | 276 | fprintf(stderr, "reportIncident returned \"%s\"\n", status.toString8().string()); | 
|  | 277 | return 1; | 
|  | 278 | } else { | 
|  | 279 | return 0; | 
|  | 280 | } | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | } |