Service Management System
Overview
The Service Management System provides kernel-level service management capabilities for FreeWorld OS. It handles service registration, lifecycle management, dependency resolution, automatic restart, health monitoring, and recovery actions.
Complete kernel-level service management system with:
- Service registration and lifecycle management
- Dependency resolution and ordering
- Automatic restart policies
- Health monitoring and recovery
- System call interface (syscalls 200-204)
- Integration with init process and user-space services
Architecture
The Service Management System consists of:
- Kernel Service Manager (
kernel/services/service_manager.asm) - Core service management logic - Service Syscalls (
kernel/syscalls/service_syscalls.asm) - System call interface - User-Space Wrappers (
libc/syscalls/service_syscalls.c) - C library wrappers - Init Integration (
sbin/init.c) - Init process service registration
Service States
| State | Value | Description |
|---|---|---|
SERVICE_STATE_STOPPED |
0 | Service is not running |
SERVICE_STATE_STARTING |
1 | Service is in the process of starting |
SERVICE_STATE_RUNNING |
2 | Service is running normally |
SERVICE_STATE_STOPPING |
3 | Service is in the process of stopping |
SERVICE_STATE_FAILED |
4 | Service has failed and cannot be started |
SERVICE_STATE_RELOADING |
5 | Service is reloading configuration |
Service Types
| Type | Value | Description |
|---|---|---|
SERVICE_TYPE_SIMPLE |
0 | Simple service - process runs in foreground |
SERVICE_TYPE_FORKING |
1 | Forking service - process forks and parent exits |
SERVICE_TYPE_ONESHOT |
2 | One-shot service - runs once and exits |
SERVICE_TYPE_NOTIFY |
3 | Notify service - sends notification when ready |
SERVICE_TYPE_IDLE |
4 | Idle service - runs when system is idle |
Restart Policies
| Policy | Value | Description |
|---|---|---|
SERVICE_RESTART_NO |
0 | Do not restart service on exit |
SERVICE_RESTART_ALWAYS |
1 | Always restart service when it exits |
SERVICE_RESTART_ON_FAILURE |
2 | Restart service only on failure (non-zero exit) |
SERVICE_RESTART_ON_SUCCESS |
3 | Restart service only on success (zero exit) |
SERVICE_RESTART_ON_ABNORMAL |
4 | Restart service only on abnormal exit (signal) |
Service Structure
The service structure (SERVICE) contains all information about a service:
struc SERVICE
.name: resb 256 ; Service name
.description: resb 512 ; Service description
.executable: resb 512 ; Executable path
.arguments: resq 1 ; Arguments list
.working_directory: resb 256 ; Working directory
.environment: resq 1 ; Environment variables
.type: resd 1 ; Service type
.state: resd 1 ; Current state
.restart_policy: resd 1 ; Restart policy
.restart_count: resd 1 ; Restart count
.max_restarts: resd 1 ; Maximum restarts
.pid: resd 1 ; Process ID
.exit_code: resd 1 ; Last exit code
.dependencies: resq 1 ; Dependencies list
.dependents: resq 1 ; Dependents list
.start_time: resq 1 ; Start timestamp
.stop_time: resq 1 ; Stop timestamp
.health_check: resq 1 ; Health check function
.recovery_action: resq 1 ; Recovery action function
.monitoring_interval: resd 1 ; Monitoring interval (seconds)
.next: resq 1 ; Next service
endstruc
System Calls
The Service Management System provides the following system calls:
SYS_SERVICE_MANAGER_INIT (204)
Initialize the service manager.
int service_manager_init(void); // Returns: 0 on success, -1 on error
SYS_SERVICE_REGISTER (200)
Register a service with the kernel service manager.
int service_register(const char* name, const char* executable,
uint32_t type, uint32_t restart_policy);
// Returns: 0 on success, -1 on error
Parameters:
name- Service name (e.g., "smss", "csrss")executable- Path to executabletype- Service type (SERVICE_TYPE_SIMPLE, etc.)restart_policy- Restart policy (SERVICE_RESTART_ALWAYS, etc.)
SYS_SERVICE_START (201)
Start a registered service.
int service_start(const char* name); // Returns: 0 on success, -1 on error
SYS_SERVICE_STOP (202)
Stop a running service.
int service_stop(const char* name); // Returns: 0 on success, -1 on error
SYS_SERVICE_STATUS (203)
Get the current status of a service.
int service_status(const char* name); // Returns: Service state (SERVICE_STATE_*), or -1 on error
Kernel Functions
service_manager_init_64
Initialize the service manager. Must be called before any other service operations.
; Returns: RAX = 0 on success, -1 on error global service_manager_init_64 service_manager_init_64:
service_register_64
Register a service with the kernel service manager.
; RDI: Service name (char*) ; RSI: Executable path (char*) ; RDX: Service type (uint32_t) ; RCX: Restart policy (uint32_t) ; Returns: RAX = 0 on success, -1 on error global service_register_64 service_register_64:
service_start_64
Start a registered service.
; RDI: Service name (char*) ; Returns: RAX = 0 on success, -1 on error global service_start_64 service_start_64:
service_stop_64
Stop a running service.
; RDI: Service name (char*) ; Returns: RAX = 0 on success, -1 on error global service_stop_64 service_stop_64:
service_get_status_64
Get the current status of a service.
; RDI: Service name (char*) ; Returns: RAX = service state, or -1 on error global service_get_status_64 service_get_status_64:
Usage Examples
Registering a Service from User Space
#include "libc/syscalls/service_syscalls.h"
int main() {
// Initialize service manager
if (service_manager_init() != 0) {
fprintf(stderr, "Failed to initialize service manager\n");
return 1;
}
// Register a service
if (service_register("myservice", "/sbin/myservice",
SERVICE_TYPE_SIMPLE, SERVICE_RESTART_ALWAYS) != 0) {
fprintf(stderr, "Failed to register service\n");
return 1;
}
// Start the service
if (service_start("myservice") != 0) {
fprintf(stderr, "Failed to start service\n");
return 1;
}
return 0;
}
Service Registration in Init Process
The init process (sbin/init.c) registers all system services on startup:
// Initialize kernel service manager
if (service_manager_init() != 0) {
fprintf(stderr, "init: Failed to initialize kernel service manager\n");
return 1;
}
// Register all system services
service_register("smss", "/sbin/smss", SERVICE_TYPE_SIMPLE, SERVICE_RESTART_ALWAYS);
service_register("csrss", "/sbin/csrss", SERVICE_TYPE_SIMPLE, SERVICE_RESTART_ALWAYS);
service_register("freeworldlogon", "/sbin/freeworldlogon",
SERVICE_TYPE_SIMPLE, SERVICE_RESTART_ALWAYS);
// ... more services
Integration Points
Init Process Integration
The init process (sbin/init.c) integrates with the service manager:
- Initializes service manager on startup
- Registers all system services
- Starts services both locally (fork/exec) and via kernel service manager
- Monitors service health
Service Integration
Services (smss, csrss, freeworldlogon) integrate with the service manager:
- Register themselves with kernel service manager on startup
- Use service syscalls for status reporting
- Handle restart policies
System Call Integration
Service syscalls are registered in the syscall table (kernel/syscalls/syscall_table.asm):
- SYS_SERVICE_REGISTER (200)
- SYS_SERVICE_START (201)
- SYS_SERVICE_STOP (202)
- SYS_SERVICE_STATUS (203)
- SYS_SERVICE_MANAGER_INIT (204)
Dependency Resolution
The service manager supports service dependencies:
- REQUIRES - Service must be started before this service
- WANTS - Service should be started before this service (non-blocking)
- REQUISITE - Service must already be running
- BINDS_TO - Service must be running (strong dependency)
- BEFORE - Service should start before this service
- AFTER - Service should start after this service
Health Monitoring
The service manager provides health monitoring capabilities:
- Periodic health checks via health check functions
- Automatic recovery actions on failure
- Restart count tracking
- Maximum restart limits
- Service state tracking
Files
kernel/services/service_manager.asm- Core service manager implementationkernel/syscalls/service_syscalls.asm- System call handlerslibc/syscalls/service_syscalls.c- User-space syscall wrapperslibc/syscalls/service_syscalls.h- Service syscall headersbin/init.c- Init process service registrationservices/smss/smss.c- SMSS service registrationservices/csrss/csrss.c- CSRSS service registrationservices/freeworldlogon/freeworldlogon.c- FreeWorldLogon service registration
See Also
- System Calls - Complete syscall documentation
- smss.exe - Session Manager service
- csrss.exe - Client/Server Runtime Subsystem
- freeworldlogon.exe - Login service
- Services Manager (Node.js) - User-space service manager