Debian Codegraph @

src/backend/daemon.rs

raw ◀ ▶
1 Debian patch touch this file
•1 // Fragments - daemon.rs
2 // Copyright (C) 2022-2025 Felix Häcker <[email protected]>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 use std::cell::RefCell;
18 use std::fs;
19 use std::path::Path;
20
21 use async_process::{Child, Command, Stdio};
22 use nix::sys::signal::{self, Signal};
23 use nix::unistd::Pid;
24
25 use crate::path;
26 use crate::settings::{settings_manager, Key};
27
28 #[derive(Default)]
29 pub struct Daemon {
30 child: RefCell<Option<Child>>,
31 }
32
33 impl Daemon {
34 pub async fn start(&self) -> anyhow::Result<()> {
35 debug!("Start transmission daemon...");
36 self.ensure_rpc_enabled();
37
38 if self.child.borrow().is_some() {
39 warn!("Daemon is already running!");
40 return Ok(());
41 }
42
43 // Allow all addresses ("*") if remote access is enabled,
44 // and not just localhost
45 let remote_access = if settings_manager::boolean(Key::RemoteAccess) {
46 "*"
47 } else {
48 "127.0.0.1,::1"
49 };
50
51 let args = [
52 "-f",
53 "--no-auth",
54 "--config-dir",
55 path::CONFIG.to_str().unwrap(),
56 "--port",
57 "9091",
58 "--rpc-bind-address",
59 "0.0.0.0",
60 "--allowed",
61 remote_access,
62 ];
63 debug!("Daemon args: {:#?}", args);
64 let child = Command::new("transmission-daemon")
65 .args(args)
66 .stdout(Stdio::piped())
67 .spawn()?;
68
69 *self.child.borrow_mut() = Some(child);
70 Ok(())
71 }
72
73 pub async fn stop(&self) -> anyhow::Result<()> {
74 debug!("Stop transmission daemon...");
75 let child = { self.child.try_borrow_mut()?.take() };
76
77 match child {
78 Some(mut child) => {
79 let pid = Pid::from_raw(child.id().try_into().unwrap());
80 signal::kill(pid, Signal::SIGTERM)?;
81
82 let status = child.status().await?;
83 debug!("Daemon stopped, status code: {}", status);
84 }
85 None => warn!("Daemon is not running, unable to stop it"),
86 }
87
88 Ok(())
89 }
90
91 pub fn is_running(&self) -> bool {
92 self.child.borrow().is_some()
93 }
94
95 // Fragments-v1 (old Vala edition) had rpc disabled by default.
96 // Fragments-v2 (Rust rewrite) needs RPC enabled to communicate with
97 // transmission-daemon
98 fn ensure_rpc_enabled(&self) {
99 let config_path = Path::new(&*path::CONFIG).join("settings.json");
100
101 match fs::read_to_string(&config_path) {
102 Ok(data) => {
103 if data.contains("\"rpc-enabled\": false") {
104 info!("RPC is disabled in settings.json. Enabling... ");
105 let updated = data.replace("\"rpc-enabled\": false", "\"rpc-enabled\": true");
106
107 // Write updated config file
108 if let Err(err) = fs::write(&config_path, updated) {
109 warn!(
110 "Cannot enable rpc, Fragments will probably not work: {}",
111 err.to_string()
112 )
113 }
114 }
115 }
116 Err(err) => warn!("Can't ensure that rpc is enabled: {}", err.to_string()),
117 };
118 }
119 }