xref: /aosp_15_r20/frameworks/native/cmds/dumpstate/main.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "dumpstate"
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <binder/IPCThreadState.h>
20*38e8c45fSAndroid Build Coastguard Worker 
21*38e8c45fSAndroid Build Coastguard Worker #include "DumpstateInternal.h"
22*38e8c45fSAndroid Build Coastguard Worker #include "DumpstateService.h"
23*38e8c45fSAndroid Build Coastguard Worker #include "dumpstate.h"
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker namespace {
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker // Returns true if we should start the service and wait for a listener
28*38e8c45fSAndroid Build Coastguard Worker // to bind with bugreport options.
ShouldStartServiceAndWait(int argc,char * argv[])29*38e8c45fSAndroid Build Coastguard Worker bool ShouldStartServiceAndWait(int argc, char* argv[]) {
30*38e8c45fSAndroid Build Coastguard Worker     bool do_wait = false;
31*38e8c45fSAndroid Build Coastguard Worker     int c;
32*38e8c45fSAndroid Build Coastguard Worker     // Keep flags in sync with Dumpstate::DumpOptions::Initialize.
33*38e8c45fSAndroid Build Coastguard Worker     while ((c = getopt(argc, argv, "dho:svqzpLPBRSV:w")) != -1 && !do_wait) {
34*38e8c45fSAndroid Build Coastguard Worker         switch (c) {
35*38e8c45fSAndroid Build Coastguard Worker             case 'w':
36*38e8c45fSAndroid Build Coastguard Worker                 do_wait = true;
37*38e8c45fSAndroid Build Coastguard Worker                 break;
38*38e8c45fSAndroid Build Coastguard Worker             default:
39*38e8c45fSAndroid Build Coastguard Worker                 // Ignore all other options
40*38e8c45fSAndroid Build Coastguard Worker                 break;
41*38e8c45fSAndroid Build Coastguard Worker         }
42*38e8c45fSAndroid Build Coastguard Worker     }
43*38e8c45fSAndroid Build Coastguard Worker 
44*38e8c45fSAndroid Build Coastguard Worker     // Reset next index used by getopt so getopt can be called called again in Dumpstate::Run to
45*38e8c45fSAndroid Build Coastguard Worker     // parse bugreport options.
46*38e8c45fSAndroid Build Coastguard Worker     optind = 1;
47*38e8c45fSAndroid Build Coastguard Worker     return do_wait;
48*38e8c45fSAndroid Build Coastguard Worker }
49*38e8c45fSAndroid Build Coastguard Worker 
50*38e8c45fSAndroid Build Coastguard Worker }  // namespace
51*38e8c45fSAndroid Build Coastguard Worker 
main(int argc,char * argv[])52*38e8c45fSAndroid Build Coastguard Worker int main(int argc, char* argv[]) {
53*38e8c45fSAndroid Build Coastguard Worker     if (ShouldStartServiceAndWait(argc, argv)) {
54*38e8c45fSAndroid Build Coastguard Worker         int ret;
55*38e8c45fSAndroid Build Coastguard Worker         if ((ret = android::os::DumpstateService::Start()) != android::OK) {
56*38e8c45fSAndroid Build Coastguard Worker             MYLOGE("Unable to start 'dumpstate' service: %d", ret);
57*38e8c45fSAndroid Build Coastguard Worker             exit(1);
58*38e8c45fSAndroid Build Coastguard Worker         }
59*38e8c45fSAndroid Build Coastguard Worker         MYLOGI("'dumpstate' service started and will wait for a call");
60*38e8c45fSAndroid Build Coastguard Worker 
61*38e8c45fSAndroid Build Coastguard Worker         // Waits forever for an incoming connection.
62*38e8c45fSAndroid Build Coastguard Worker         // TODO(b/111441001): should this time out?
63*38e8c45fSAndroid Build Coastguard Worker         android::IPCThreadState::self()->joinThreadPool();
64*38e8c45fSAndroid Build Coastguard Worker         return 0;
65*38e8c45fSAndroid Build Coastguard Worker     } else {
66*38e8c45fSAndroid Build Coastguard Worker         return run_main(argc, argv);
67*38e8c45fSAndroid Build Coastguard Worker     }
68*38e8c45fSAndroid Build Coastguard Worker }
69