Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Nandana Dutt | 235864b | 2019-01-22 12:10:16 +0000 | [diff] [blame] | 17 | #define LOG_TAG "dumpstate" |
| 18 | |
| 19 | #include <binder/IPCThreadState.h> |
| 20 | |
| 21 | #include "DumpstateInternal.h" |
| 22 | #include "DumpstateService.h" |
Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 23 | #include "dumpstate.h" |
| 24 | |
Nandana Dutt | 235864b | 2019-01-22 12:10:16 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | // Returns true if we should start the service and wait for a listener |
| 28 | // to bind with bugreport options. |
| 29 | bool ShouldStartServiceAndWait(int argc, char* argv[]) { |
| 30 | bool do_wait = false; |
| 31 | int c; |
| 32 | // Keep flags in sync with Dumpstate::DumpOptions::Initialize. |
mhasank | d451a47 | 2020-05-26 18:02:39 -0700 | [diff] [blame] | 33 | while ((c = getopt(argc, argv, "dho:svqzpLPBRSV:w")) != -1 && !do_wait) { |
Nandana Dutt | 235864b | 2019-01-22 12:10:16 +0000 | [diff] [blame] | 34 | switch (c) { |
| 35 | case 'w': |
| 36 | do_wait = true; |
| 37 | break; |
| 38 | default: |
| 39 | // Ignore all other options |
| 40 | break; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Reset next index used by getopt so getopt can be called called again in Dumpstate::Run to |
| 45 | // parse bugreport options. |
| 46 | optind = 1; |
| 47 | return do_wait; |
| 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 52 | int main(int argc, char* argv[]) { |
Nandana Dutt | 235864b | 2019-01-22 12:10:16 +0000 | [diff] [blame] | 53 | if (ShouldStartServiceAndWait(argc, argv)) { |
| 54 | int ret; |
| 55 | if ((ret = android::os::DumpstateService::Start()) != android::OK) { |
| 56 | MYLOGE("Unable to start 'dumpstate' service: %d", ret); |
| 57 | exit(1); |
| 58 | } |
Kean Mariotti | 306633e | 2022-09-05 16:30:47 +0000 | [diff] [blame] | 59 | MYLOGI("'dumpstate' service started and will wait for a call"); |
Nandana Dutt | 235864b | 2019-01-22 12:10:16 +0000 | [diff] [blame] | 60 | |
| 61 | // Waits forever for an incoming connection. |
| 62 | // TODO(b/111441001): should this time out? |
| 63 | android::IPCThreadState::self()->joinThreadPool(); |
| 64 | return 0; |
| 65 | } else { |
| 66 | return run_main(argc, argv); |
| 67 | } |
Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 68 | } |