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); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 71 | ALOGD("section %d status %d\n", section, status); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 72 | return Status::ok(); |
| 73 | } |
| 74 | |
| 75 | Status |
| 76 | StatusListener::onReportServiceStatus(const String16& service, int32_t status) |
| 77 | { |
| 78 | fprintf(stderr, "service '%s' status %d\n", String8(service).string(), status); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 79 | ALOGD("service '%s' status %d\n", String8(service).string(), status); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 80 | return Status::ok(); |
| 81 | } |
| 82 | |
| 83 | Status |
| 84 | StatusListener::onReportFinished() |
| 85 | { |
| 86 | fprintf(stderr, "done\n"); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 87 | ALOGD("done\n"); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 88 | exit(0); |
| 89 | return Status::ok(); |
| 90 | } |
| 91 | |
| 92 | Status |
| 93 | StatusListener::onReportFailed() |
| 94 | { |
| 95 | fprintf(stderr, "failed\n"); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 96 | ALOGD("failed\n"); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 97 | exit(1); |
| 98 | return Status::ok(); |
| 99 | } |
| 100 | |
| 101 | // ================================================================================ |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 102 | static void section_list(FILE* out) { |
| 103 | IncidentSection sections[INCIDENT_SECTION_COUNT]; |
| 104 | int i = 0; |
| 105 | int j = 0; |
| 106 | // sort the sections based on id |
| 107 | while (i < INCIDENT_SECTION_COUNT) { |
| 108 | IncidentSection curr = INCIDENT_SECTIONS[i]; |
| 109 | for (int k = 0; k < j; k++) { |
| 110 | if (curr.id > sections[k].id) { |
| 111 | continue; |
| 112 | } |
| 113 | IncidentSection tmp = curr; |
| 114 | curr = sections[k]; |
| 115 | sections[k] = tmp; |
| 116 | } |
| 117 | sections[j] = curr; |
| 118 | i++; |
| 119 | j++; |
| 120 | } |
| 121 | |
| 122 | fprintf(out, "available sections:\n"); |
| 123 | for (int i = 0; i < INCIDENT_SECTION_COUNT; ++i) { |
| 124 | fprintf(out, "id: %4d, name: %s\n", sections[i].id, sections[i].name); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // ================================================================================ |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 129 | static IncidentSection const* |
| 130 | find_section(const char* name) |
| 131 | { |
| 132 | size_t low = 0; |
| 133 | size_t high = INCIDENT_SECTION_COUNT - 1; |
| 134 | |
| 135 | while (low <= high) { |
| 136 | size_t mid = (low + high) >> 1; |
| 137 | IncidentSection const* section = INCIDENT_SECTIONS + mid; |
| 138 | |
| 139 | int cmp = strcmp(section->name, name); |
| 140 | if (cmp < 0) { |
| 141 | low = mid + 1; |
| 142 | } else if (cmp > 0) { |
| 143 | high = mid - 1; |
| 144 | } else { |
| 145 | return section; |
| 146 | } |
| 147 | } |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | // ================================================================================ |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 152 | static int |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 153 | get_privacy_policy(const char* arg) |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 154 | { |
Yi Jin | b8344dc | 2018-01-24 17:33:35 -0800 | [diff] [blame] | 155 | if (strcmp(arg, "L") == 0 |
| 156 | || strcmp(arg, "LOCAL") == 0) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 157 | return PRIVACY_POLICY_LOCAL; |
Yi Jin | b8344dc | 2018-01-24 17:33:35 -0800 | [diff] [blame] | 158 | } |
| 159 | if (strcmp(arg, "E") == 0 |
| 160 | || strcmp(arg, "EXPLICIT") == 0) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 161 | return PRIVACY_POLICY_EXPLICIT; |
Yi Jin | b8344dc | 2018-01-24 17:33:35 -0800 | [diff] [blame] | 162 | } |
| 163 | if (strcmp(arg, "A") == 0 |
| 164 | || strcmp(arg, "AUTO") == 0 |
| 165 | || strcmp(arg, "AUTOMATIC") == 0) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 166 | return PRIVACY_POLICY_AUTOMATIC; |
Yi Jin | b8344dc | 2018-01-24 17:33:35 -0800 | [diff] [blame] | 167 | } |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 168 | return -1; // return the default value |
| 169 | } |
| 170 | |
| 171 | // ================================================================================ |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 172 | static bool |
| 173 | parse_receiver_arg(const string& arg, string* pkg, string* cls) |
| 174 | { |
| 175 | if (arg.length() == 0) { |
| 176 | return true; |
| 177 | } |
| 178 | size_t slash = arg.find('/'); |
| 179 | if (slash == string::npos) { |
| 180 | return false; |
| 181 | } |
| 182 | if (slash == 0 || slash == arg.length() - 1) { |
| 183 | return false; |
| 184 | } |
| 185 | if (arg.find('/', slash+1) != string::npos) { |
| 186 | return false; |
| 187 | } |
| 188 | pkg->assign(arg, 0, slash); |
| 189 | cls->assign(arg, slash+1); |
| 190 | if ((*cls)[0] == '.') { |
| 191 | *cls = (*pkg) + (*cls); |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | // ================================================================================ |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 197 | static void |
| 198 | usage(FILE* out) |
| 199 | { |
| 200 | fprintf(out, "usage: incident OPTIONS [SECTION...]\n"); |
| 201 | fprintf(out, "\n"); |
| 202 | fprintf(out, "Takes an incident report.\n"); |
| 203 | fprintf(out, "\n"); |
| 204 | fprintf(out, "OPTIONS\n"); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 205 | fprintf(out, " -l list available sections\n"); |
| 206 | fprintf(out, " -p privacy spec, LOCAL, EXPLICIT or AUTOMATIC. Default AUTOMATIC.\n"); |
| 207 | fprintf(out, "\n"); |
| 208 | fprintf(out, "and one of these destinations:\n"); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 209 | fprintf(out, " -b (default) print the report to stdout (in proto format)\n"); |
| 210 | fprintf(out, " -d send the report into dropbox\n"); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 211 | fprintf(out, " -s PKG/CLS send broadcast to the broadcast receiver.\n"); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 212 | fprintf(out, "\n"); |
| 213 | fprintf(out, " SECTION the field numbers of the incident report fields to include\n"); |
| 214 | fprintf(out, "\n"); |
| 215 | } |
| 216 | |
| 217 | int |
| 218 | main(int argc, char** argv) |
| 219 | { |
| 220 | Status status; |
| 221 | IncidentReportArgs args; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 222 | enum { DEST_UNSET, DEST_DROPBOX, DEST_STDOUT, DEST_BROADCAST } destination = DEST_UNSET; |
| 223 | int privacyPolicy = PRIVACY_POLICY_AUTOMATIC; |
| 224 | string receiverArg; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 225 | |
| 226 | // Parse the args |
| 227 | int opt; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 228 | while ((opt = getopt(argc, argv, "bhdlp:s:")) != -1) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 229 | switch (opt) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 230 | case 'h': |
| 231 | usage(stdout); |
| 232 | return 0; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 233 | case 'l': |
| 234 | section_list(stdout); |
| 235 | return 0; |
| 236 | case 'b': |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 237 | if (!(destination == DEST_UNSET || destination == DEST_STDOUT)) { |
| 238 | usage(stderr); |
| 239 | return 1; |
| 240 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 241 | destination = DEST_STDOUT; |
| 242 | break; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 243 | case 'd': |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 244 | if (!(destination == DEST_UNSET || destination == DEST_DROPBOX)) { |
| 245 | usage(stderr); |
| 246 | return 1; |
| 247 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 248 | destination = DEST_DROPBOX; |
| 249 | break; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 250 | case 'p': |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 251 | privacyPolicy = get_privacy_policy(optarg); |
| 252 | break; |
| 253 | case 's': |
| 254 | if (destination != DEST_UNSET) { |
| 255 | usage(stderr); |
| 256 | return 1; |
| 257 | } |
| 258 | destination = DEST_BROADCAST; |
| 259 | receiverArg = optarg; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 260 | break; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 261 | default: |
| 262 | usage(stderr); |
| 263 | return 1; |
| 264 | } |
| 265 | } |
| 266 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 267 | string pkg; |
| 268 | string cls; |
| 269 | if (parse_receiver_arg(receiverArg, &pkg, &cls)) { |
| 270 | args.setReceiverPkg(pkg); |
| 271 | args.setReceiverCls(cls); |
| 272 | } else { |
| 273 | fprintf(stderr, "badly formatted -s package/class option: %s\n\n", receiverArg.c_str()); |
| 274 | usage(stderr); |
| 275 | return 1; |
| 276 | } |
| 277 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 278 | if (optind == argc) { |
| 279 | args.setAll(true); |
| 280 | } else { |
| 281 | for (int i=optind; i<argc; i++) { |
| 282 | const char* arg = argv[i]; |
| 283 | char* end; |
| 284 | if (arg[0] != '\0') { |
| 285 | int section = strtol(arg, &end, 0); |
| 286 | if (*end == '\0') { |
| 287 | args.addSection(section); |
| 288 | } else { |
| 289 | IncidentSection const* ic = find_section(arg); |
| 290 | if (ic == NULL) { |
| 291 | fprintf(stderr, "Invalid section: %s\n", arg); |
| 292 | return 1; |
| 293 | } |
| 294 | args.addSection(ic->id); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 299 | args.setPrivacyPolicy(privacyPolicy); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 300 | |
| 301 | // Start the thread pool. |
| 302 | sp<ProcessState> ps(ProcessState::self()); |
| 303 | ps->startThreadPool(); |
| 304 | ps->giveThreadPoolName(); |
| 305 | |
| 306 | // Look up the service |
| 307 | sp<IIncidentManager> service = interface_cast<IIncidentManager>( |
| 308 | defaultServiceManager()->getService(android::String16("incident"))); |
| 309 | if (service == NULL) { |
| 310 | fprintf(stderr, "Couldn't look up the incident service\n"); |
| 311 | return 1; |
| 312 | } |
| 313 | |
| 314 | // Construct the stream |
| 315 | int fds[2]; |
| 316 | pipe(fds); |
| 317 | |
| 318 | unique_fd readEnd(fds[0]); |
| 319 | unique_fd writeEnd(fds[1]); |
| 320 | |
| 321 | if (destination == DEST_STDOUT) { |
| 322 | // Call into the service |
| 323 | sp<StatusListener> listener(new StatusListener()); |
| 324 | status = service->reportIncidentToStream(args, listener, writeEnd); |
| 325 | |
| 326 | if (!status.isOk()) { |
| 327 | fprintf(stderr, "reportIncident returned \"%s\"\n", status.toString8().string()); |
Yi Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 328 | return 1; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // Wait for the result and print out the data they send. |
| 332 | //IPCThreadState::self()->joinThreadPool(); |
| 333 | |
| 334 | while (true) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 335 | uint8_t buf[4096]; |
| 336 | ssize_t amt = TEMP_FAILURE_RETRY(read(fds[0], buf, sizeof(buf))); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 337 | if (amt < 0) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 338 | break; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 339 | } else if (amt == 0) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 340 | break; |
| 341 | } |
| 342 | |
| 343 | ssize_t wamt = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buf, amt)); |
| 344 | if (wamt != amt) { |
| 345 | return errno; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | } else { |
| 349 | status = service->reportIncident(args); |
| 350 | if (!status.isOk()) { |
| 351 | fprintf(stderr, "reportIncident returned \"%s\"\n", status.toString8().string()); |
| 352 | return 1; |
| 353 | } else { |
| 354 | return 0; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | } |