• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_protos::ProtoStorageReturnMessage;
18 use aconfigd_rust::aconfigd::Aconfigd;
19 use anyhow::{anyhow, bail, Result};
20 use log::{debug, error, info};
21 use std::io::{Read, Write};
22 use std::os::fd::AsRawFd;
23 use std::os::unix::net::UnixListener;
24 use std::path::Path;
25 
26 const ACONFIGD_SOCKET: &str = "aconfigd_system";
27 const ACONFIGD_ROOT_DIR: &str = "/metadata/aconfig";
28 const STORAGE_RECORDS: &str = "/metadata/aconfig/storage_records.pb";
29 const PLATFORM_STORAGE_RECORDS: &str = "/metadata/aconfig/platform_storage_records.pb";
30 const ACONFIGD_SOCKET_BACKLOG: i32 = 8;
31 
32 /// start aconfigd socket service
start_socket() -> Result<()>33 pub fn start_socket() -> Result<()> {
34     let fd = rustutils::sockets::android_get_control_socket(ACONFIGD_SOCKET)?;
35 
36     // SAFETY: Safe because this doesn't modify any memory and we check the return value.
37     let ret = unsafe { libc::listen(fd.as_raw_fd(), ACONFIGD_SOCKET_BACKLOG) };
38     if ret < 0 {
39         bail!(std::io::Error::last_os_error());
40     }
41 
42     let listener = UnixListener::from(fd);
43 
44     let storage_records = if aconfig_new_storage_flags::enable_aconfigd_from_mainline() {
45         PLATFORM_STORAGE_RECORDS
46     } else {
47         STORAGE_RECORDS
48     };
49 
50     let mut aconfigd = Aconfigd::new(Path::new(ACONFIGD_ROOT_DIR), Path::new(storage_records));
51     aconfigd.initialize_from_storage_record()?;
52 
53     debug!("start waiting for a new client connection through socket.");
54     for stream in listener.incoming() {
55         match stream {
56             Ok(mut stream) => {
57                 if let Err(errmsg) = aconfigd.handle_socket_request_from_stream(&mut stream) {
58                     error!("failed to handle socket request: {:?}", errmsg);
59                 }
60             }
61             Err(errmsg) => {
62                 error!("failed to listen for an incoming message: {:?}", errmsg);
63             }
64         }
65     }
66 
67     Ok(())
68 }
69 
70 /// initialize mainline module storage files
mainline_init() -> Result<()>71 pub fn mainline_init() -> Result<()> {
72     let mut aconfigd = Aconfigd::new(Path::new(ACONFIGD_ROOT_DIR), Path::new(STORAGE_RECORDS));
73     aconfigd.initialize_from_storage_record()?;
74     Ok(aconfigd.initialize_mainline_storage()?)
75 }
76 
77 /// initialize platform storage files
platform_init() -> Result<()>78 pub fn platform_init() -> Result<()> {
79     let storage_records = if aconfig_new_storage_flags::enable_aconfigd_from_mainline() {
80         PLATFORM_STORAGE_RECORDS
81     } else {
82         STORAGE_RECORDS
83     };
84 
85     let mut aconfigd = Aconfigd::new(Path::new(ACONFIGD_ROOT_DIR), Path::new(storage_records));
86     aconfigd.remove_boot_files()?;
87     aconfigd.initialize_from_storage_record()?;
88     Ok(aconfigd.initialize_platform_storage()?)
89 }
90