Skip to content

Commit

Permalink
refactor: replace winapi with windows-sys (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekse committed Apr 1, 2024
1 parent 8517baa commit e1e8c4e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ ctrlc = { version = "3.1", features = ["termination"] }
log = "0.4"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winbase", "winerror", "winuser", "winsvc", "libloaderapi", "errhandlingapi"] }
widestring = "0.4.3"
windows-sys = { version = "0.52", features = ["Win32_Foundation", "Win32_System_RemoteDesktop", "Win32_System_LibraryLoader", "Win32_UI_WindowsAndMessaging",
"Win32_Security", "Win32_System_Services", "Win32_System_Diagnostics_Debug"]}

[target.'cfg(target_os = "linux")'.dependencies]
systemd-rs = { version="^0.1.2", optional = true }
Expand Down
5 changes: 1 addition & 4 deletions cmdlet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ build = "build.rs"
[dependencies]
libc = "0"
log = "0.4"
log4rs = "0.8"
log4rs = "1.3"
ctrlc = "3.1"
cfg-if = "0.1"
base64 = "0.12"
Expand All @@ -20,9 +20,6 @@ serde_json = "1.0"
which = { version = "3.0", default-features = false, features = [] }
expand_str = "0.1"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winbase", "winuser", "winsvc", "libloaderapi", "errhandlingapi", "winerror"] }

[target.'cfg(windows)'.build-dependencies]
embed-resource = "1.3"

Expand Down
5 changes: 1 addition & 4 deletions examples/foobar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ path = "src/main.rs"
[dependencies]
libc = "0"
log = "0.4"
log4rs = "0.8"
log4rs = "1.3"
clap = { version = "2.31", features = ["yaml"] }
ctrlc = "3.1"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winbase", "winuser", "winsvc", "libloaderapi", "errhandlingapi", "winerror"] }

[dependencies.ceviche]
path = "../.."
75 changes: 40 additions & 35 deletions src/controller/windows.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
use std::ffi::OsStr;
use std::ffi::{c_void, OsStr};
use std::iter::once;
use std::os::windows::ffi::OsStrExt;
use std::ptr;
use std::sync::mpsc;
use std::{thread, time};

use widestring::WideCString;
use winapi::shared::minwindef::*;
use winapi::shared::winerror::*;
use winapi::um::errhandlingapi::*;
use winapi::um::libloaderapi::*;
use winapi::um::winbase::*;
use winapi::um::winnt::*;
use winapi::um::winsvc::*;
use winapi::um::winuser::*;
use winapi::{self, STRUCT};
use windows_sys::core::PWSTR;
use windows_sys::Win32::{
Foundation::{MAX_PATH, ERROR_CALL_NOT_IMPLEMENTED, GetLastError},
Security::SC_HANDLE,
System::{
Diagnostics::Debug::{FormatMessageW, FORMAT_MESSAGE_FROM_SYSTEM},
LibraryLoader::GetModuleFileNameW,
RemoteDesktop::WTSSESSION_NOTIFICATION,
Services::*,
},
UI::WindowsAndMessaging::{
WTS_CONSOLE_CONNECT, WTS_CONSOLE_DISCONNECT, WTS_REMOTE_CONNECT, WTS_REMOTE_DISCONNECT,
WTS_SESSION_LOGON, WTS_SESSION_LOGOFF, WTS_SESSION_LOCK, WTS_SESSION_UNLOCK
}
};

use crate::controller::{ControllerInterface, ServiceMainFn};
use crate::session;
use crate::Error;
use crate::ServiceEvent;

static mut SERVICE_CONTROL_HANDLE: SERVICE_STATUS_HANDLE = ptr::null_mut();
type DWORD = u32;
type LPVOID = *mut c_void;

STRUCT! {#[allow(non_snake_case)]
struct SERVICE_DESCRIPTION_W {
lpDescription: LPWSTR,
}}
static mut SERVICE_CONTROL_HANDLE: SERVICE_STATUS_HANDLE = 0;

type WindowsServiceMainWrapperFn = extern "system" fn(argc: DWORD, argv: *mut LPWSTR);
type WindowsServiceMainWrapperFn = extern "system" fn(argc: DWORD, argv: *mut PWSTR);
pub type Session = session::Session_<u32>;

struct Service {
Expand All @@ -37,7 +41,7 @@ struct Service {

impl Drop for Service {
fn drop(&mut self) {
if !self.handle.is_null() {
if !(self.handle == 0) {
unsafe { CloseServiceHandle(self.handle) };
}
}
Expand All @@ -51,7 +55,7 @@ impl ServiceControlManager {
fn open(desired_access: DWORD) -> Result<ServiceControlManager, Error> {
let handle = unsafe { OpenSCManagerW(ptr::null_mut(), ptr::null_mut(), desired_access) };

if handle.is_null() {
if handle == 0 {
Err(Error::new(&format!(
"OpenSCManager: {}",
get_last_error_text()
Expand All @@ -70,7 +74,7 @@ impl ServiceControlManager {
)
};

if handle.is_null() {
if handle == 0 {
Err(Error::new(&format!(
"OpenServiceW: {}",
get_last_error_text()
Expand All @@ -83,7 +87,7 @@ impl ServiceControlManager {

impl Drop for ServiceControlManager {
fn drop(&mut self) {
if !self.handle.is_null() {
if !(self.handle == 0) {
unsafe { CloseServiceHandle(self.handle) };
}
}
Expand Down Expand Up @@ -132,7 +136,7 @@ impl ControllerInterface for WindowsController {
ptr::null_mut(),
);

if service.is_null() {
if service == 0 {
return Err(Error::new(&format!(
"CreateService: {}",
get_last_error_text()
Expand All @@ -143,11 +147,11 @@ impl ControllerInterface for WindowsController {

let mut description = get_utf16(self.description.as_str());

let mut sd = SERVICE_DESCRIPTION_W {
let mut sd = SERVICE_DESCRIPTIONW {
lpDescription: description.as_mut_ptr(),
};

let p_sd = &mut sd as *mut _ as *mut winapi::ctypes::c_void;
let p_sd = &mut sd as *mut _ as *mut c_void;
ChangeServiceConfig2W(service, SERVICE_CONFIG_DESCRIPTION, p_sd);
CloseServiceHandle(service);

Expand Down Expand Up @@ -261,7 +265,7 @@ impl WindowsController {
dwCheckPoint: 0,
dwWaitHint: 0,
},
status_handle: ptr::null_mut(),
status_handle: 0,
controls_accepted: SERVICE_ACCEPT_STOP,
}
}
Expand All @@ -272,11 +276,11 @@ impl WindowsController {
service_main_wrapper: WindowsServiceMainWrapperFn,
) -> Result<(), Error> {
unsafe {
let service_name = get_utf16(self.service_name.as_str());
let mut service_name = get_utf16(self.service_name.as_str());

let service_table: &[*const SERVICE_TABLE_ENTRYW] = &[
&SERVICE_TABLE_ENTRYW {
lpServiceName: service_name.as_ptr(),
lpServiceName: service_name.as_mut_ptr(),
lpServiceProc: Some(service_main_wrapper),
},
ptr::null(),
Expand Down Expand Up @@ -335,8 +339,8 @@ unsafe extern "system" fn service_handler<T>(
0
}
SERVICE_CONTROL_SESSIONCHANGE => {
let event = event_type as usize;
let session_notification = event_data as PWTSSESSION_NOTIFICATION;
let event = event_type;
let session_notification = event_data as *const WTSSESSION_NOTIFICATION;
let session_id = (*session_notification).dwSessionId;
let session = Session::new(session_id);

Expand Down Expand Up @@ -372,7 +376,7 @@ unsafe extern "system" fn service_handler<T>(
}
}

fn get_args(argc: DWORD, argv: *mut LPWSTR) -> Vec<String> {
fn get_args(argc: DWORD, argv: *mut PWSTR) -> Vec<String> {
let mut args = Vec::new();
for i in 0..argc {
unsafe {
Expand All @@ -390,9 +394,9 @@ pub fn get_utf16(value: &str) -> Vec<u16> {

pub fn get_filename() -> String {
unsafe {
let mut filename = [0u16; MAX_PATH];
let mut filename = [0u16; MAX_PATH as usize];
let _size = GetModuleFileNameW(
ptr::null_mut(),
0,
filename.as_mut_ptr(),
filename.len() as DWORD,
);
Expand Down Expand Up @@ -420,17 +424,18 @@ pub fn get_last_error_text() -> String {
#[macro_export]
macro_rules! Service {
($name:expr, $function:ident) => {
use $crate::winapi::shared::minwindef::DWORD;
use $crate::winapi::um::winnt::LPWSTR;
use std::ffi::c_void;
type DWORD = u32;
type PWSTR = *mut u16;

extern "system" fn service_main_wrapper(argc: DWORD, argv: *mut LPWSTR) {
extern "system" fn service_main_wrapper(argc: DWORD, argv: *mut PWSTR) {
dispatch($function, $name, argc, argv);
}
};
}

#[doc(hidden)]
pub fn dispatch<T>(service_main: ServiceMainFn<T>, name: &str, argc: DWORD, argv: *mut LPWSTR) {
pub fn dispatch<T>(service_main: ServiceMainFn<T>, name: &str, argc: DWORD, argv: *mut PWSTR) {
let args = get_args(argc, argv);
let service_name = get_utf16(name);
let (mut tx, rx) = mpsc::channel();
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub mod controller;
pub mod session;

#[cfg(windows)]
pub use winapi;
pub use windows_sys;

use self::controller::Session;
use std::fmt;
Expand Down

0 comments on commit e1e8c4e

Please sign in to comment.