Skip to content

Commit 03339c7

Browse files
committed
Install as windows service
1 parent 65ea8f0 commit 03339c7

File tree

4 files changed

+181
-9
lines changed

4 files changed

+181
-9
lines changed

Cargo.lock

Lines changed: 116 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ serialport = "4.0"
1212
smbios-lib = "0.7.3"
1313
log = { version = "0.4", features = ["max_level_debug", "release_max_level_info"] }
1414
winlog = "0.2"
15+
windows-service = "0.3"
1516

1617
[build-dependencies]
1718
winres = "0.1"

src/main.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use log::{
44
};
55
use std::{
66
env::current_exe,
7+
ffi::OsString,
78
io::{
89
self,
910
BufRead,
@@ -19,11 +20,26 @@ use std::{
1920
thread::sleep,
2021
time::Duration,
2122
};
22-
2323
use thelio_io::{
2424
fan::FanCurve,
2525
Io,
2626
};
27+
use windows_service::{
28+
define_windows_service,
29+
service::{
30+
ServiceControl,
31+
ServiceControlAccept,
32+
ServiceExitCode,
33+
ServiceState,
34+
ServiceStatus,
35+
ServiceType,
36+
},
37+
service_dispatcher,
38+
service_control_handler::{
39+
self,
40+
ServiceControlHandlerResult,
41+
},
42+
};
2743

2844
fn driver_loop(curve: &FanCurve, ios: &mut [Io], wrapper: &mut Child) -> io::Result<()> {
2945
let mut wrapper_in = wrapper.stdin.take().unwrap();
@@ -129,12 +145,42 @@ fn driver() -> io::Result<()> {
129145
res
130146
}
131147

132-
fn main() {
148+
fn service_main(_args: Vec<OsString>) {
149+
// Windows event log
133150
winlog::init("System76 Thelio Io").expect("failed to initialize logging");
134151

152+
// Handle service events
153+
let status_handle = service_control_handler::register("thelio-io", |event| -> ServiceControlHandlerResult {
154+
//TODO: handle stop event
155+
match event {
156+
ServiceControl::Interrogate => ServiceControlHandlerResult::NoError,
157+
_ => ServiceControlHandlerResult::NotImplemented,
158+
}
159+
}).expect("failed to register for service events");
160+
161+
// Update service status
162+
status_handle.set_service_status(ServiceStatus {
163+
service_type: ServiceType::OWN_PROCESS,
164+
current_state: ServiceState::Running,
165+
controls_accepted: ServiceControlAccept::empty(),
166+
exit_code: ServiceExitCode::Win32(0),
167+
checkpoint: 0,
168+
wait_hint: Duration::default(),
169+
process_id: None,
170+
}).expect("failed to set service status");
171+
172+
// Run driver
135173
if let Err(err) = driver() {
136174
error!("{}\n{:#?}", err, err);
137-
eprintln!("Error: {}\n{:#?}", err, err);
175+
//TODO: set service status
138176
exit(1);
139177
}
140178
}
179+
180+
define_windows_service!(ffi_service_main, service_main);
181+
182+
fn main() -> Result<(), windows_service::Error> {
183+
// Dispatch service
184+
service_dispatcher::start("thelio-io", ffi_service_main)?;
185+
Ok(())
186+
}

wix/main.wxs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,21 @@
8686
DiskId='1'
8787
Source='target\$(var.Profile)\thelio-io.exe'
8888
KeyPath='yes'/>
89-
<RegistryValue
90-
Root='HKLM'
91-
Key='Software\Microsoft\Windows\CurrentVersion\Run'
92-
Name='System76 Thelio Io'
93-
Value='[#exe0]'
94-
Type='string'/>
89+
<ServiceInstall
90+
Id="ServiceInstaller"
91+
Name="thelio-io"
92+
DisplayName="System76 Thelio Io"
93+
Type="ownProcess"
94+
Account="LocalSystem"
95+
Start="auto"
96+
ErrorControl="normal"/>
97+
<ServiceControl
98+
Id="ServiceControl"
99+
Name="thelio-io"
100+
Start="install"
101+
Stop="both"
102+
Remove="uninstall"
103+
Wait="yes"/>
95104
<util:EventSource
96105
Log="Application"
97106
Name="System76 Thelio Io"

0 commit comments

Comments
 (0)