xref: /aosp_15_r20/system/server_configurable_flags/aconfigd/src/main.rs (revision 207333786ba243bc7d4d69ef6b05487aa7071806)
1*20733378SAndroid Build Coastguard Worker /*
2*20733378SAndroid Build Coastguard Worker  * Copyright (C) 2024 The Android Open Source Project
3*20733378SAndroid Build Coastguard Worker  *
4*20733378SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*20733378SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*20733378SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*20733378SAndroid Build Coastguard Worker  *
8*20733378SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*20733378SAndroid Build Coastguard Worker  *
10*20733378SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*20733378SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*20733378SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*20733378SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*20733378SAndroid Build Coastguard Worker  * limitations under the License.
15*20733378SAndroid Build Coastguard Worker  */
16*20733378SAndroid Build Coastguard Worker 
17*20733378SAndroid Build Coastguard Worker //! `aconfigd-mainline` is a daemon binary that responsible for:
18*20733378SAndroid Build Coastguard Worker //! (1) initialize mainline storage files
19*20733378SAndroid Build Coastguard Worker //! (2) initialize and maintain a persistent socket based service
20*20733378SAndroid Build Coastguard Worker 
21*20733378SAndroid Build Coastguard Worker use clap::Parser;
22*20733378SAndroid Build Coastguard Worker use log::{error, info};
23*20733378SAndroid Build Coastguard Worker use std::panic;
24*20733378SAndroid Build Coastguard Worker 
25*20733378SAndroid Build Coastguard Worker mod aconfigd_commands;
26*20733378SAndroid Build Coastguard Worker 
27*20733378SAndroid Build Coastguard Worker #[derive(Parser, Debug)]
28*20733378SAndroid Build Coastguard Worker struct Cli {
29*20733378SAndroid Build Coastguard Worker     #[clap(subcommand)]
30*20733378SAndroid Build Coastguard Worker     command: Command,
31*20733378SAndroid Build Coastguard Worker }
32*20733378SAndroid Build Coastguard Worker 
33*20733378SAndroid Build Coastguard Worker #[derive(Parser, Debug)]
34*20733378SAndroid Build Coastguard Worker enum Command {
35*20733378SAndroid Build Coastguard Worker     /// start aconfigd socket.
36*20733378SAndroid Build Coastguard Worker     StartSocket,
37*20733378SAndroid Build Coastguard Worker 
38*20733378SAndroid Build Coastguard Worker     /// initialize platform storage files.
39*20733378SAndroid Build Coastguard Worker     PlatformInit,
40*20733378SAndroid Build Coastguard Worker 
41*20733378SAndroid Build Coastguard Worker     /// initialize mainline module storage files.
42*20733378SAndroid Build Coastguard Worker     MainlineInit,
43*20733378SAndroid Build Coastguard Worker }
44*20733378SAndroid Build Coastguard Worker 
main()45*20733378SAndroid Build Coastguard Worker fn main() {
46*20733378SAndroid Build Coastguard Worker     if !aconfig_new_storage_flags::enable_aconfig_storage_daemon() {
47*20733378SAndroid Build Coastguard Worker         info!("aconfigd_system is disabled, exiting");
48*20733378SAndroid Build Coastguard Worker         std::process::exit(0);
49*20733378SAndroid Build Coastguard Worker     }
50*20733378SAndroid Build Coastguard Worker 
51*20733378SAndroid Build Coastguard Worker     // SAFETY: nobody has taken ownership of the inherited FDs yet.
52*20733378SAndroid Build Coastguard Worker     // This needs to be called before logger initialization as logger setup will create a
53*20733378SAndroid Build Coastguard Worker     // file descriptor.
54*20733378SAndroid Build Coastguard Worker     unsafe {
55*20733378SAndroid Build Coastguard Worker         if let Err(errmsg) = rustutils::inherited_fd::init_once() {
56*20733378SAndroid Build Coastguard Worker             error!("failed to run init_once for inherited fds: {:?}.", errmsg);
57*20733378SAndroid Build Coastguard Worker             std::process::exit(1);
58*20733378SAndroid Build Coastguard Worker         }
59*20733378SAndroid Build Coastguard Worker     };
60*20733378SAndroid Build Coastguard Worker 
61*20733378SAndroid Build Coastguard Worker     // setup android logger, direct to logcat
62*20733378SAndroid Build Coastguard Worker     android_logger::init_once(
63*20733378SAndroid Build Coastguard Worker         android_logger::Config::default()
64*20733378SAndroid Build Coastguard Worker             .with_tag("aconfigd_system")
65*20733378SAndroid Build Coastguard Worker             .with_max_level(log::LevelFilter::Trace),
66*20733378SAndroid Build Coastguard Worker     );
67*20733378SAndroid Build Coastguard Worker     info!("starting aconfigd_system commands.");
68*20733378SAndroid Build Coastguard Worker 
69*20733378SAndroid Build Coastguard Worker     let cli = Cli::parse();
70*20733378SAndroid Build Coastguard Worker     let command_return = match cli.command {
71*20733378SAndroid Build Coastguard Worker         Command::StartSocket => aconfigd_commands::start_socket(),
72*20733378SAndroid Build Coastguard Worker         Command::PlatformInit => aconfigd_commands::platform_init(),
73*20733378SAndroid Build Coastguard Worker         Command::MainlineInit => {
74*20733378SAndroid Build Coastguard Worker             if aconfig_new_storage_flags::enable_aconfigd_from_mainline() {
75*20733378SAndroid Build Coastguard Worker                 info!("aconfigd_mainline is enabled, skipping mainline init");
76*20733378SAndroid Build Coastguard Worker                 std::process::exit(1);
77*20733378SAndroid Build Coastguard Worker             }
78*20733378SAndroid Build Coastguard Worker             aconfigd_commands::mainline_init()
79*20733378SAndroid Build Coastguard Worker         }
80*20733378SAndroid Build Coastguard Worker     };
81*20733378SAndroid Build Coastguard Worker 
82*20733378SAndroid Build Coastguard Worker     if let Err(errmsg) = command_return {
83*20733378SAndroid Build Coastguard Worker         error!("failed to run aconfigd command: {:?}.", errmsg);
84*20733378SAndroid Build Coastguard Worker         std::process::exit(1);
85*20733378SAndroid Build Coastguard Worker     }
86*20733378SAndroid Build Coastguard Worker }
87