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 #include <server_configurable_flags/disaster_recovery.h>
18 #include <server_configurable_flags/get_flags.h>
19 #include <string>
20 
21 #include "android-base/logging.h"
22 
23 // flags_heatlh_check binary takes 1 argument -- reset_mode
24 // If reset_mode == BOOT_FAILURE, the binary will examine how many
25 // consecutive reboots have failed. If there are more than 4 consecutive
26 // reboot failures, flag reset will be performed.
27 // If reset_mode == UPDATABLE_CRASHING, the binary will directly perform
28 // flag reset actions.
29 int main(int argc, char* argv[]) {
30   if (argc != 2) {
31     LOG(ERROR) << "argc: " << std::to_string(argc) << ", it should only be 2.";
32     return 1;
33   }
34   const char* configuration_namespace = "configuration";
35   const char* disable_rescue_party_flag = "disable_rescue_party";
36   if (server_configurable_flags::GetServerConfigurableFlag(
37           configuration_namespace, disable_rescue_party_flag, "false") == "true") {
38     LOG(INFO) << "flags_heatlh_check is disabled by flag, skipping reset.";
39     return 0;
40   }
41 
42   std::string reset_mode_str(argv[1]);
43   server_configurable_flags::ResetMode reset_mode;
44   if (reset_mode_str == "BOOT_FAILURE") {
45     reset_mode = server_configurable_flags::BOOT_FAILURE;
46   } else if (reset_mode_str == "UPDATABLE_CRASHING") {
47     reset_mode = server_configurable_flags::UPDATABLE_CRASHING;
48   } else {
49     LOG(ERROR) << "invalid reset mode: " << reset_mode_str << ".";
50     return 1;
51   }
52 
53   server_configurable_flags::ServerConfigurableFlagsReset(reset_mode);
54 
55   return 0;
56 }