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 use aconfigd_rust::aconfigd::Aconfigd;
18 use anyhow::{bail, Result};
19 use log::{debug, error};
20 use std::os::fd::AsRawFd;
21 use std::os::unix::net::UnixListener;
22 use std::path::Path;
23
24 const ACONFIGD_SOCKET: &str = "aconfigd_mainline";
25 const ACONFIGD_ROOT_DIR: &str = "/metadata/aconfig";
26 const STORAGE_RECORDS: &str = "/metadata/aconfig/mainline_storage_records.pb";
27 const ACONFIGD_SOCKET_BACKLOG: i32 = 8;
28
29 /// start aconfigd socket service
start_socket() -> Result<()>30 pub fn start_socket() -> Result<()> {
31 let fd = rustutils::sockets::android_get_control_socket(ACONFIGD_SOCKET)?;
32
33 // SAFETY: Safe because this doesn't modify any memory and we check the return value.
34 let ret = unsafe { libc::listen(fd.as_raw_fd(), ACONFIGD_SOCKET_BACKLOG) };
35 if ret < 0 {
36 bail!(std::io::Error::last_os_error());
37 }
38
39 let listener = UnixListener::from(fd);
40
41 let mut aconfigd = Aconfigd::new(Path::new(ACONFIGD_ROOT_DIR), Path::new(STORAGE_RECORDS));
42 aconfigd.initialize_from_storage_record()?;
43
44 debug!("start waiting for a new client connection through socket.");
45 for stream in listener.incoming() {
46 match stream {
47 Ok(mut stream) => {
48 if let Err(errmsg) = aconfigd.handle_socket_request_from_stream(&mut stream) {
49 error!("failed to handle socket request: {:?}", errmsg);
50 }
51 }
52 Err(errmsg) => {
53 error!("failed to listen for an incoming message: {:?}", errmsg);
54 }
55 }
56 }
57
58 Ok(())
59 }
60
61 /// initialize mainline module storage files
init() -> Result<()>62 pub fn init() -> Result<()> {
63 let mut aconfigd = Aconfigd::new(Path::new(ACONFIGD_ROOT_DIR), Path::new(STORAGE_RECORDS));
64 aconfigd.remove_boot_files()?;
65 aconfigd.initialize_from_storage_record()?;
66 aconfigd.initialize_mainline_storage()?;
67 Ok(())
68 }
69
70 /// initialize bootstrapped mainline module storage files
bootstrap_init() -> Result<()>71 pub fn bootstrap_init() -> Result<()> {
72 Ok(())
73 }
74