System services, also known as Windows services, are specialized applications that run in the background on a Windows operating system.
Unlike typical applications that require user interaction, services can run without a user logged in, perform long-running tasks, and start automatically when the system boots up.
This makes them crucial for a wide range of functionalities, from managing hardware and network connections to running databases and web servers.
Key Characteristics of Windows Services:
Background Operation: Services run in the system's background, independent of the user's session.
Automatic Startup: They can be configured to start automatically when the operating system starts.
User Independence: Services can run even when no user is logged on.
System Resource Management: Services often manage critical system resources and provide core functionalities.
Interprocess Communication (IPC): Services frequently use IPC mechanisms to communicate with other services or user-mode applications.
Understanding the architecture and management of Windows services is fundamental for developing robust and reliable drivers and system-level applications.
Common Use Cases for Services
Windows services are employed in various scenarios to provide essential system capabilities. Some common examples include:
Network Services: DHCP Client, DNS Client, Workstation, Server.
System Utilities: Windows Update, Task Scheduler, Print Spooler.
Application Backends: SQL Server, IIS Web Server, various third-party application daemons.
Hardware Management: Services that manage specific hardware devices or drivers.
Service Control Manager (SCM)
The Service Control Manager (SCM) is a core Windows component responsible for starting, stopping, querying, and configuring services.
Developers interact with the SCM through the Service Control Manager API to manage their services.
Key SCM functions include:
Registering services with the operating system.
Starting and stopping service executables.
Managing service dependencies.
Responding to control requests from the SCM (e.g., stop, pause).
Service Dependencies
Services can be configured to depend on other services. The SCM ensures that dependent services are started after the services they rely on. This dependency management is crucial for ensuring proper system initialization and operation.
Core Concepts of Windows Services
Service Programs
A service program is an executable file that implements the service. It must adhere to specific protocols to communicate with the Service Control Manager (SCM). The primary entry point for a service program is typically the ServiceMain function.
Service Control Handler
A service program must implement a HandlerEx routine to receive control requests from the SCM. This routine handles commands such as SERVICE_CONTROL_STOP, SERVICE_CONTROL_PAUSE, and SERVICE_CONTROL_INTERROGATE.
Service Status
A service reports its current status (e.g., RUNNING, STOPPED, PAUSED) to the SCM using the SetServiceStatus function. This status information is vital for the SCM to manage the service lifecycle effectively.
Service Configuration
Services can be configured with various parameters, including their startup type (Automatic, Manual, Disabled), dependencies, account under which they run, and recovery options. These configurations are managed through the Registry and accessible via tools like Services.msc and SC command-line utility.
Service Accounts
Services can run under different security contexts, such as LocalSystem, NetworkService, or LocalService, or a specific user account. The choice of account impacts the service's privileges and access rights.
Key Service Management APIs
Service Control Manager APIs
These functions are used to interact with the SCM and manage services.
OpenSCManager: Opens a handle to the SCM database.
CreateService: Creates a new service entry in the SCM database.
StartService: Starts a specified service.
ControlService: Sends a control code to a specified service.
QueryServiceStatus: Retrieves the status of a specified service.
CloseServiceHandle: Closes a handle to an SCM object.
Service Program APIs
These functions are used within a service executable to report status and handle controls.
StartServiceCtrlDispatcher: Connects the calling thread to the SCM and registers the service control routine.
RegisterServiceCtrlHandlerEx: Registers a handler function for service control requests.
SetServiceStatus: Reports the current status of the service to the SCM.
Example Service Main Function Structure
VOID ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) {
// Register the service control handler
g_ServiceStatusHandle = RegisterServiceCtrlHandlerEx(
SERVICE_NAME,
HandlerEx,
NULL);
if (!g_ServiceStatusHandle) {
// Handle error
return;
}
// Report initial status to SCM
SERVICE_STATUS ss;
ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ss.dwCurrentState = SERVICE_START_PENDING;
ss.dwControlsAccepted = 0;
ss.dwWin32ExitCode = 0;
ss.dwServiceSpecificExitCode = 0;
ss.dwCheckPoint = 0;
ss.dwWaitHint = 0;
SetServiceStatus(g_ServiceStatusHandle, &ss);
// ... Perform service initialization ...
// Report running status
ss.dwCurrentState = SERVICE_RUNNING;
ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
ss.dwCheckPoint = 0;
ss.dwWaitHint = 0;
SetServiceStatus(g_ServiceStatusHandle, &ss);
// ... Service main loop ...
}