1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::ffi::OsStr;
use std::io;
use std::os::raw::c_void;
use std::os::windows::io::{AsRawHandle, RawHandle};
use widestring::WideCString;
use windows_sys::Win32::{
Foundation::{ERROR_CALL_NOT_IMPLEMENTED, NO_ERROR},
System::Services,
};
use crate::service::{ServiceControl, ServiceStatus};
use crate::{Error, Result};
#[derive(Debug, Clone, Copy)]
pub struct ServiceStatusHandle(Services::SERVICE_STATUS_HANDLE);
impl ServiceStatusHandle {
fn from_handle(handle: Services::SERVICE_STATUS_HANDLE) -> Self {
ServiceStatusHandle(handle)
}
pub fn set_service_status(&self, service_status: ServiceStatus) -> crate::Result<()> {
let raw_service_status = service_status.to_raw();
let result = unsafe { Services::SetServiceStatus(self.0, &raw_service_status) };
if result == 0 {
Err(Error::Winapi(io::Error::last_os_error()))
} else {
Ok(())
}
}
}
impl AsRawHandle for ServiceStatusHandle {
fn as_raw_handle(&self) -> RawHandle {
self.0 as _
}
}
unsafe impl Send for ServiceStatusHandle {}
unsafe impl Sync for ServiceStatusHandle {}
#[derive(Debug)]
pub enum ServiceControlHandlerResult {
NoError,
NotImplemented,
Other(u32),
}
impl ServiceControlHandlerResult {
pub fn to_raw(&self) -> u32 {
match *self {
ServiceControlHandlerResult::NoError => NO_ERROR,
ServiceControlHandlerResult::NotImplemented => ERROR_CALL_NOT_IMPLEMENTED,
ServiceControlHandlerResult::Other(code) => code,
}
}
}
pub fn register<F>(service_name: impl AsRef<OsStr>, event_handler: F) -> Result<ServiceStatusHandle>
where
F: FnMut(ServiceControl) -> ServiceControlHandlerResult + 'static + Send,
{
let heap_event_handler: Box<F> = Box::new(event_handler);
let context: *mut F = Box::into_raw(heap_event_handler);
let service_name = WideCString::from_os_str(service_name)
.map_err(|_| Error::ArgumentHasNulByte("service name"))?;
let status_handle = unsafe {
Services::RegisterServiceCtrlHandlerExW(
service_name.as_ptr(),
Some(service_control_handler::<F>),
context as *mut c_void,
)
};
if status_handle == 0 {
let _: Box<F> = unsafe { Box::from_raw(context) };
Err(Error::Winapi(io::Error::last_os_error()))
} else {
Ok(ServiceStatusHandle::from_handle(status_handle))
}
}
#[allow(dead_code)]
extern "system" fn service_control_handler<F>(
control: u32,
event_type: u32,
event_data: *mut c_void,
context: *mut c_void,
) -> u32
where
F: FnMut(ServiceControl) -> ServiceControlHandlerResult,
{
let event_handler: &mut F = unsafe { &mut *(context as *mut F) };
match unsafe { ServiceControl::from_raw(control, event_type, event_data) } {
Ok(service_control) => {
let need_release = matches!(
service_control,
ServiceControl::Stop | ServiceControl::Shutdown | ServiceControl::Preshutdown,
);
let return_code = event_handler(service_control).to_raw();
if need_release {
let _: Box<F> = unsafe { Box::from_raw(context as *mut F) };
}
return_code
}
Err(_) => ServiceControlHandlerResult::NotImplemented.to_raw(),
}
}