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 
24 mod aconfigd_commands;
25 
26 #[derive(Parser, Debug)]
27 struct Cli {
28     #[clap(subcommand)]
29     command: Command,
30 }
31 
32 #[derive(Parser, Debug)]
33 enum Command {
34     /// start aconfigd socket.
35     StartSocket,
36 
37     /// initialize mainline module storage files.
38     Init,
39 
40     /// initialize bootstrap mainline module storage files.
41     BootstrapInit,
42 }
43 
main()44 fn main() {
45     if !aconfig_new_storage_flags::enable_aconfig_storage_daemon()
46         || !aconfig_new_storage_flags::enable_aconfigd_from_mainline()
47     {
48         info!("aconfigd_mainline is disabled, exiting");
49         std::process::exit(0);
50     }
51 
52     // SAFETY: nobody has taken ownership of the inherited FDs yet.
53     // This needs to be called before logger initialization as logger setup will create a
54     // file descriptor.
55     unsafe {
56         if let Err(errmsg) = rustutils::inherited_fd::init_once() {
57             error!("failed to run init_once for inherited fds: {:?}.", errmsg);
58             std::process::exit(1);
59         }
60     };
61 
62     // setup android logger, direct to logcat
63     android_logger::init_once(
64         android_logger::Config::default()
65             .with_tag("aconfigd_mainline")
66             .with_max_level(log::LevelFilter::Trace),
67     );
68     info!("starting aconfigd_mainline commands.");
69 
70     let cli = Cli::parse();
71     let command_return = match cli.command {
72         Command::StartSocket => aconfigd_commands::start_socket(),
73         Command::Init => aconfigd_commands::init(),
74         Command::BootstrapInit => aconfigd_commands::bootstrap_init(),
75     };
76 
77     if let Err(errmsg) = command_return {
78         error!("failed to run aconfigd command: {:?}.", errmsg);
79         std::process::exit(1);
80     }
81 }
82