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 
17 #define LOG_TAG "dumpstate"
18 
19 #include <binder/IPCThreadState.h>
20 
21 #include "DumpstateInternal.h"
22 #include "DumpstateService.h"
23 #include "dumpstate.h"
24 
25 namespace {
26 
27 // Returns true if we should start the service and wait for a listener
28 // to bind with bugreport options.
ShouldStartServiceAndWait(int argc,char * argv[])29 bool ShouldStartServiceAndWait(int argc, char* argv[]) {
30     bool do_wait = false;
31     int c;
32     // Keep flags in sync with Dumpstate::DumpOptions::Initialize.
33     while ((c = getopt(argc, argv, "wdho:svqzpPBRSV:")) != -1 && !do_wait) {
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 
main(int argc,char * argv[])52 int main(int argc, char* argv[]) {
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         }
59         MYLOGI("'dumpstate' service started and will wait for a call to startBugreport()");
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     }
68 }
69