Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize logging properly when starting Windows service #6088

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions mullvad-daemon/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use mullvad_daemon::{
logging,
management_interface::{ManagementInterfaceEventBroadcaster, ManagementInterfaceServer},
rpc_uniqueness_check,
runtime::new_runtime_builder,
version, Daemon, DaemonCommandChannel, DaemonCommandSender,
rpc_uniqueness_check, runtime, version, Daemon, DaemonCommandChannel, DaemonCommandSender,
};
use std::{path::PathBuf, thread, time::Duration};
use talpid_types::ErrorExt;
Expand All @@ -22,11 +20,7 @@ const DAEMON_LOG_FILENAME: &str = "daemon.log";
const EARLY_BOOT_LOG_FILENAME: &str = "early-boot-fw.log";

fn main() {
let runtime = new_runtime_builder().build().unwrap_or_else(|e| {
eprintln!("{}", e.display_chain());
std::process::exit(1);
});

let runtime = new_runtime();
let exit_code = match runtime.block_on(run()) {
Ok(_) => 0,
Err(error) => {
Expand All @@ -44,6 +38,19 @@ fn main() {
std::process::exit(exit_code);
}

fn new_runtime() -> tokio::runtime::Runtime {
let mut builder = match cli::get_config().command {
#[cfg(target_os = "windows")]
cli::Command::RunAsService | cli::Command::RegisterService => runtime::new_current_thread(),
_ => runtime::new_multi_thread(),
};

builder.build().unwrap_or_else(|e| {
eprintln!("{}", e.display_chain());
std::process::exit(1);
})
}

async fn run() -> Result<(), String> {
let config = cli::get_config();

Expand All @@ -69,8 +76,8 @@ async fn run() -> Result<(), String> {

#[cfg(target_os = "windows")]
cli::Command::RunAsService => {
init_logger(config, None)?;
assert_unique().await?;
let _ = init_daemon_logging(config)?;
system_service::run()
}

Expand Down
8 changes: 7 additions & 1 deletion mullvad-daemon/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use tokio::runtime;

pub fn new_runtime_builder() -> runtime::Builder {
pub fn new_multi_thread() -> runtime::Builder {
let mut builder = runtime::Builder::new_multi_thread();
builder.worker_threads(4).enable_all();
builder
}

pub fn new_current_thread() -> runtime::Builder {
let mut builder = runtime::Builder::new_current_thread();
builder.enable_all();
builder
}
4 changes: 2 additions & 2 deletions mullvad-daemon/src/system_service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cli;
use mullvad_daemon::{runtime::new_runtime_builder, DaemonShutdownHandle};
use mullvad_daemon::{runtime::new_multi_thread, DaemonShutdownHandle};
use once_cell::sync::Lazy;
use std::{
env,
Expand Down Expand Up @@ -98,7 +98,7 @@ pub fn handle_service_main(_arguments: Vec<OsString>) {

let log_dir = crate::get_log_dir(cli::get_config()).expect("Log dir should be available here");

let runtime = new_runtime_builder().build();
let runtime = new_multi_thread().build();
let runtime = match runtime {
Err(error) => {
log::error!("{}", error.display_chain());
Expand Down
4 changes: 2 additions & 2 deletions mullvad-jni/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use jnix::{
};
use mullvad_api::{rest::Error as RestError, StatusCode};
use mullvad_daemon::{
device, exception_logging, logging, runtime::new_runtime_builder,
device, exception_logging, logging, runtime::new_multi_thread,
settings::patch::Error as PatchError, version, Daemon, DaemonCommandChannel,
};
use mullvad_types::{
Expand Down Expand Up @@ -551,7 +551,7 @@ fn spawn_daemon(
.map_err(Error::CreateGlobalReference)?;
let (tx, rx) = mpsc::channel();

let runtime = new_runtime_builder()
let runtime = new_multi_thread()
.build()
.map_err(Error::InitializeTokioRuntime)?;

Expand Down
Loading