1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 //! daemon
16 
17 extern crate panic_handler;
18 use hdc::common::jdwp::Jdwp;
19 use hdc::common::base::Base;
20 use hdc::config;
21 use hdc::daemon_lib::*;
22 use hdc::utils::hdc_log::*;
23 use hdc::utils;
24 use std::io::Write;
25 use std::time::SystemTime;
26 #[cfg(feature = "emulator")]
27 use hdc::daemon_lib::bridge;
28 use hdc::daemon_lib::auth::clear_auth_pub_key_file;
29 use log::LevelFilter;
30 
logger_initnull31 fn logger_init(log_level: LevelFilter) {
32     env_logger::Builder::new()
33         .format(|buf, record| {
34             let ts = humantime::format_rfc3339_millis(SystemTime::now()).to_string();
35             let level = &record.level().to_string()[..1];
36             let file = record.file().unwrap_or("unknown");
37             writeln!(
38                 buf,
39                 "{} {} {} {}:{} - {}",
40                 &ts[..10],
41                 &ts[11..23],
42                 level,
43                 file.split('/').last().unwrap_or("unknown"),
44                 record.line().unwrap_or(0),
45                 record.args()
46             )
47         })
48         .filter(None, log_level)
49         .init();
50 }
51 
get_logger_lvnull52 fn get_logger_lv() -> LevelFilter {
53     let lv = std::env::var_os("HDCD_LOGLV")
54         .unwrap_or_default()
55         .to_str()
56         .unwrap_or_default()
57         .parse::<usize>()
58         .unwrap_or(0_usize);
59     config::LOG_LEVEL_ORDER[lv]
60 }
61 
mainnull62 fn main() {
63     let args: Vec<String> = std::env::args().collect();
64     panic_handler::init();
65     if args.len() == 2 && args[1] == "-v" {
66         println!("{}", config::get_version());
67         return;
68     }
69     logger_init(get_logger_lv());
70 
71     let _ = ylong_runtime::builder::RuntimeBuilder::new_multi_thread()
72         .worker_stack_size(16 * 1024 * 1024)
73         .worker_num(20)
74         .max_blocking_pool_size(64)
75         .build_global();
76 
77     #[cfg(not(feature = "emulator"))]
78     need_drop_root_privileges();
79     Base::init_process();
80     clear_auth_pub_key_file();
81 
82     ylong_runtime::block_on(async {
83         #[cfg(not(feature = "emulator"))]
84         let tcp_task = utils::spawn(async {
85             if let Err(e) = tcp_daemon_start(get_tcp_port()).await {
86                 hdc::error!("[Fail]tcp daemon failed: {}", e);
87             }
88         });
89         #[cfg(not(feature = "emulator"))]
90         let usb_task = utils::spawn(async {
91             if let Err(e) = usb_daemon_start().await {
92                 hdc::error!("[Fail]usb daemon failed: {}", e);
93             }
94         });
95         #[cfg(feature = "emulator")]
96         hdc::info!("daemon main emulator, start bridge daemon.");
97         #[cfg(feature = "emulator")]
98         let bridge_task = utils::spawn(async {
99             if let Err(e) = bridge_daemon_start().await {
100                 hdc::error!("[Fail]bridge daemon failed: {}", e);
101             }
102         });
103         let lock_value = Jdwp::get_instance();
104         let jdwp_server_task = utils::spawn(async {
105             jdwp_daemon_start(lock_value).await;
106         });
107         #[cfg(not(feature = "emulator"))]
108         let _ = tcp_task.await;
109         #[cfg(not(feature = "emulator"))]
110         let _ = usb_task.await;
111         #[cfg(feature = "emulator")]
112         let _ = bridge_task.await;
113         let _ = jdwp_server_task.await;
114     });
115 }
116