Skip to main content

windows_erg/service/
mod.rs

1//! Windows Service Control Manager operations.
2//!
3//! This module provides ergonomic wrappers around the Windows Service Control
4//! Manager (SCM) APIs for querying and controlling services.
5
6#![allow(clippy::module_inception)]
7
8mod manager;
9mod service;
10mod status;
11mod types;
12
13pub use manager::ServiceManager;
14pub use service::Service;
15pub use status::ServiceStatus;
16pub use types::{ServiceAccess, ServiceControl, ServiceManagerAccess, ServiceState};
17
18use std::time::Duration;
19
20use crate::Result;
21
22/// Query a service status by name.
23pub fn query(name: &str) -> Result<ServiceStatus> {
24    Service::open(name)?.query()
25}
26
27/// Start a service by name.
28pub fn start(name: &str) -> Result<()> {
29    Service::open_with_access(name, ServiceAccess::Start)?.start()
30}
31
32/// Stop a service by name.
33pub fn stop(name: &str) -> Result<()> {
34    Service::open_with_access(name, ServiceAccess::Stop)?.stop()
35}
36
37/// Restart a service by name, waiting for stopped state before start.
38pub fn restart(name: &str, timeout: Duration) -> Result<()> {
39    Service::open_with_access(
40        name,
41        ServiceAccess::Custom(
42            ServiceAccess::QueryStatus.to_windows()
43                | ServiceAccess::Start.to_windows()
44                | ServiceAccess::Stop.to_windows(),
45        ),
46    )?
47    .restart(timeout)
48}
49
50/// List all services.
51pub fn list() -> Result<Vec<ServiceStatus>> {
52    ServiceManager::connect()?.list()
53}
54
55/// List all services using a reusable output buffer.
56///
57/// Returns the number of services added to the output buffer.
58pub fn list_with_buffer(out_services: &mut Vec<ServiceStatus>) -> Result<usize> {
59    ServiceManager::connect()?.list_with_buffer(out_services)
60}
61
62/// List matching services using a reusable output buffer.
63///
64/// Returns the number of services added to the output buffer.
65pub fn list_with_filter<F>(out_services: &mut Vec<ServiceStatus>, filter: F) -> Result<usize>
66where
67    F: Fn(&ServiceStatus) -> bool,
68{
69    ServiceManager::connect()?.list_with_filter(out_services, filter)
70}