Query the current runtime status of a service.
(service_handle: SC_HANDLE)
| 351 | |
| 352 | /// Query the current runtime status of a service. |
| 353 | unsafe fn query_status(service_handle: SC_HANDLE) -> Result<ServiceStatus, ServiceError> { |
| 354 | let mut buffer = vec![0u8; mem::size_of::<SERVICE_STATUS_PROCESS>()]; |
| 355 | let mut bytes_needed: u32 = 0; |
| 356 | |
| 357 | unsafe { |
| 358 | QueryServiceStatusEx( |
| 359 | service_handle, |
| 360 | SC_STATUS_PROCESS_INFO, |
| 361 | Some(&mut buffer), |
| 362 | &mut bytes_needed, |
| 363 | ) |
| 364 | .map_err(|e| t!("get.queryStatusFailed", error = e.to_string()).to_string())?; |
| 365 | } |
| 366 | |
| 367 | let status = unsafe { &*(buffer.as_ptr().cast::<SERVICE_STATUS_PROCESS>()) }; |
| 368 | |
| 369 | match status.dwCurrentState { |
| 370 | SERVICE_STOPPED => Ok(ServiceStatus::Stopped), |
| 371 | SERVICE_START_PENDING => Ok(ServiceStatus::StartPending), |
| 372 | SERVICE_STOP_PENDING => Ok(ServiceStatus::StopPending), |
| 373 | SERVICE_RUNNING => Ok(ServiceStatus::Running), |
| 374 | SERVICE_CONTINUE_PENDING => Ok(ServiceStatus::ContinuePending), |
| 375 | SERVICE_PAUSE_PENDING => Ok(ServiceStatus::PausePending), |
| 376 | SERVICE_PAUSED => Ok(ServiceStatus::Paused), |
| 377 | other => Err( |
| 378 | t!("get.queryStatusFailed", error = format!("unknown state: {}", other.0)) |
| 379 | .to_string() |
| 380 | .into(), |
| 381 | ), |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /// Convert a `ServiceStatus` to the corresponding Windows `SERVICE_STATUS_CURRENT_STATE` constant. |
| 386 | fn status_to_current_state(status: &ServiceStatus) -> SERVICE_STATUS_CURRENT_STATE { |
no outgoing calls
no test coverage detected