xref: /aosp_15_r20/system/server_configurable_flags/aconfigd/src/main.rs (revision 207333786ba243bc7d4d69ef6b05487aa7071806)
1 /*
2  * Copyright (C) 2024 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 //! `aconfigd-mainline` is a daemon binary that responsible for:
18 //! (1) initialize mainline storage files
19 //! (2) initialize and maintain a persistent socket based service
20 
21 use clap::Parser;
22 use log::{error, info};
23 use std::panic;
24 
25 mod aconfigd_commands;
26 
27 #[derive(Parser, Debug)]
28 struct Cli {
29     #[clap(subcommand)]
30     command: Command,
31 }
32 
33 #[derive(Parser, Debug)]
34 enum Command {
35     /// start aconfigd socket.
36     StartSocket,
37 
38     /// initialize platform storage files.
39     PlatformInit,
40 
41     /// initialize mainline module storage files.
42     MainlineInit,
43 }
44 
main()45 fn main() {
46     if !aconfig_new_storage_flags::enable_aconfig_storage_daemon() {
47         info!("aconfigd_system is disabled, exiting");
48         std::process::exit(0);
49     }
50 
51     // SAFETY: nobody has taken ownership of the inherited FDs yet.
52     // This needs to be called before logger initialization as logger setup will create a
53     // file descriptor.
54     unsafe {
55         if let Err(errmsg) = rustutils::inherited_fd::init_once() {
56             error!("failed to run init_once for inherited fds: {:?}.", errmsg);
57             std::process::exit(1);
58         }
59     };
60 
61     // setup android logger, direct to logcat
62     android_logger::init_once(
63         android_logger::Config::default()
64             .with_tag("aconfigd_system")
65             .with_max_level(log::LevelFilter::Trace),
66     );
67     info!("starting aconfigd_system commands.");
68 
69     let cli = Cli::parse();
70     let command_return = match cli.command {
71         Command::StartSocket => aconfigd_commands::start_socket(),
72         Command::PlatformInit => aconfigd_commands::platform_init(),
73         Command::MainlineInit => {
74             if aconfig_new_storage_flags::enable_aconfigd_from_mainline() {
75                 info!("aconfigd_mainline is enabled, skipping mainline init");
76                 std::process::exit(1);
77             }
78             aconfigd_commands::mainline_init()
79         }
80     };
81 
82     if let Err(errmsg) = command_return {
83         error!("failed to run aconfigd command: {:?}.", errmsg);
84         std::process::exit(1);
85     }
86 }
87