Skip to content

Commit

Permalink
Merge branch 'add-service-description'
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrej Mihajlov committed Oct 27, 2020
2 parents 82969f8 + 2b1b19b commit 2728c17
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Add support for service description. (See: `Service::set_description`)

### Fixed
- Fix segmentation fault in `Service` functions, that query service config, by moving buffer
allocation on heap.


## [0.3.0] - 2020-06-18
### Added
- Add support for configuring the service SID info.
Expand Down
3 changes: 2 additions & 1 deletion examples/install_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ fn main() -> windows_service::Result<()> {
account_name: None, // run as System
account_password: None,
};
let _service = service_manager.create_service(&service_info, ServiceAccess::empty())?;
let service = service_manager.create_service(&service_info, ServiceAccess::CHANGE_CONFIG)?;
service.set_description("Windows service example from windows-service-rs")?;
Ok(())
}

Expand Down
15 changes: 8 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@
//! [`ServiceStatus::checkpoint`]: service::ServiceStatus::checkpoint
//! [`StartPending`]: service::ServiceState::StartPending
//! [`Running`]: service::ServiceState::Running
//! [`ServiceActionType`]: service::ServiceActionType
//! [`ServiceErrorControl`]: service::ServiceErrorControl
//! [`ServiceState`]: service::ServiceState
#![cfg(windows)]

Expand Down Expand Up @@ -224,19 +221,19 @@ pub enum Error {
#[error(display = "Invalid start argument")]
InvalidStartArgument(#[error(source)] widestring::NulError<u16>),

/// Invalid raw representation of [`ServiceState`].
/// Invalid raw representation of [`ServiceState`](service::ServiceState).
#[error(display = "Invalid service state value")]
InvalidServiceState(#[error(source)] service::ParseRawError),

/// Invalid raw representation of [`ServiceStartType`].
/// Invalid raw representation of [`ServiceStartType`](service::ServiceStartType).
#[error(display = "Invalid service start value")]
InvalidServiceStartType(#[error(source)] service::ParseRawError),

/// Invalid raw representation of [`ServiceErrorControl`].
/// Invalid raw representation of [`ServiceErrorControl`](service::ServiceErrorControl).
#[error(display = "Invalid service error control value")]
InvalidServiceErrorControl(#[error(source)] service::ParseRawError),

/// Invalid raw representation of [`ServiceActionType`]
/// Invalid raw representation of [`ServiceActionType`](service::ServiceActionType).
#[error(display = "Invalid service action type")]
InvalidServiceActionType(#[error(source)] service::ParseRawError),

Expand All @@ -248,6 +245,10 @@ pub enum Error {
#[error(display = "Invalid service action failures command")]
InvalidServiceActionFailuresCommand(#[error(source)] widestring::NulError<u16>),

/// Invalid service description
#[error(display = "Invalid service description")]
InvalidServiceDescription(#[error(source)] widestring::NulError<u16>),

/// IO error when calling winapi
#[error(display = "IO error in winapi call")]
Winapi(#[error(source)] std::io::Error),
Expand Down
18 changes: 17 additions & 1 deletion src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,22 @@ impl Service {
}
}

/// Set service description.
///
/// Required permission: [`ServiceAccess::CHANGE_CONFIG`].
pub fn set_description(&self, description: impl AsRef<OsStr>) -> crate::Result<()> {
let wide_str =
WideCString::from_os_str(description).map_err(Error::InvalidServiceDescription)?;
let mut service_description = winsvc::SERVICE_DESCRIPTIONW {
lpDescription: wide_str.as_ptr() as *mut _,
};

unsafe {
self.change_config2(winsvc::SERVICE_CONFIG_DESCRIPTION, &mut service_description)
.map_err(Error::Winapi)
}
}

/// Private helper to send the control commands to the system.
fn send_control_command(&self, command: ServiceControl) -> crate::Result<ServiceStatus> {
let mut raw_status = unsafe { mem::zeroed::<winsvc::SERVICE_STATUS>() };
Expand Down Expand Up @@ -1699,7 +1715,7 @@ fn to_wide_slice(
) -> ::std::result::Result<Option<Vec<u16>>, NulError<u16>> {
if let Some(s) = s {
Ok(Some(
WideCString::from_os_str(s).map(|s| s.as_slice_with_nul().to_vec())?,
WideCString::from_os_str(s).map(|s| s.into_vec_with_nul())?,
))
} else {
Ok(None)
Expand Down

0 comments on commit 2728c17

Please sign in to comment.