Overview

The Services Manager provides comprehensive service management capabilities for FreeWorld OS. It can start, stop, restart, and monitor all system services, manage startup types, handle dependencies, and provide auto-restart functionality.

Features

  • Service Registration: Register services with executable paths, arguments, and working directories
  • Service Control: Start, stop, restart services with graceful shutdown support
  • Startup Types: Automatic, manual, or disabled startup configuration
  • Auto-Restart: Automatic restart of services that exit unexpectedly
  • Critical Services: Mark services as critical (must stay running)
  • Dependencies: Handle service dependencies (start dependencies first)
  • Service Monitoring: Periodic monitoring of service health
  • Service Registry: Persistent storage of service configuration
  • Event Integration: EventBus integration for service events
  • Logging: Comprehensive logging via logd

Service States

State Description
stopped Service is not running
starting Service is in the process of starting
running Service is running normally
stopping Service is in the process of stopping
paused Service is paused (future feature)
error Service encountered an error

Startup Types

Type Description
automatic Service starts automatically at system boot
manual Service must be started manually
disabled Service is disabled and cannot be started

Default Services

The Services Manager automatically registers these default system services:

  • csrss - Client/Server Runtime Subsystem (automatic, critical, auto-restart)
  • logd - Logging Daemon (automatic, critical, auto-restart)
  • networkd - Network Daemon (automatic, auto-restart)
  • securityd - Security Daemon (automatic, auto-restart)
  • freeworldlogon - Login Manager (automatic, critical, auto-restart)

API Reference

registerService(serviceInfo)

Register a new service with the Services Manager.

const serviceInfo = new ServiceInfo(
    'myservice',
    'My Service Description',
    'path/to/executable',
    ['arg1', 'arg2'],
    '/working/directory'
);
serviceInfo.startupType = SERVICE_STARTUP_AUTOMATIC;
serviceInfo.restartOnExit = true;
serviceInfo.critical = false;
servicesManager.registerService(serviceInfo);

startService(name)

Start a service. Returns a promise with success status and message.

const result = await servicesManager.startService('logd');
if (result.success) {
    console.log(`Service started: ${result.message}`);
}

stopService(name, force)

Stop a service. If force is true, uses SIGKILL instead of SIGTERM.

const result = await servicesManager.stopService('logd', false);

restartService(name)

Restart a service (stops then starts).

const result = await servicesManager.restartService('networkd');

getServiceStatus(name)

Get current status of a service.

const status = servicesManager.getServiceStatus('logd');
console.log(`State: ${status.state}, PID: ${status.pid}`);

listServices()

Get list of all registered services with their status.

const services = servicesManager.listServices();
services.forEach(service => {
    console.log(`${service.name}: ${service.state}`);
});

setStartupType(name, startupType)

Set the startup type for a service.

servicesManager.setStartupType('myservice', SERVICE_STARTUP_AUTOMATIC);

startAutomaticServices()

Start all services configured for automatic startup.

const results = await servicesManager.startAutomaticServices();

startMonitoring(interval)

Start monitoring services for health (checks if processes are still running).

servicesManager.startMonitoring(5000); // Check every 5 seconds

Integration

The Services Manager integrates with:

  • FreeWorldSystem: Initialized in system/integration.js
  • EventBus: Emits service events (started, stopped, error)
  • logd: Logs all service operations
  • smss: Works alongside smss for service management

Service Registry

Service configuration is persisted to system/config/services.json. This includes:

  • Service name and description
  • Executable path and arguments
  • Working directory
  • Startup type
  • Auto-restart configuration
  • Critical flag
  • Dependencies

Usage Example

const FreeWorldSystem = require('./system/integration');
const system = new FreeWorldSystem();
await system.init();

const servicesManager = system.getServicesManager();

// Get service status
const status = servicesManager.getServiceStatus('logd');
console.log(`logd is ${status.state}`);

// Start a service
await servicesManager.startService('networkd');

// List all services
const services = servicesManager.listServices();
services.forEach(s => {
    console.log(`${s.name}: ${s.state} (PID: ${s.pid || 'N/A'})`);
});