RADLib
RADical C++ application framework

RADService contains deployment-oriented helpers for running RADLib programs as long-lived services: systemd unit generation, watchdog/ready notifications, single-instance locks, and Unix daemonization. These helpers are useful for embedded appliances and desktop background tools that need reproducible service metadata.

A ServiceUnit describes a systemd service: its name, description, execStart command, optional workingDirectory, environment map, user, and wantedBy target. The restart policy is a RestartPolicy (No, OnFailure, or Always), and watchdogSec enables WatchdogSec when non-zero.

ServiceManager exposes the static helpers. generateSystemdUnit() serializes a ServiceUnit to unit-file text, and writeSystemdUnit() writes it into a target directory. restartPolicyName() converts a policy to its systemd token. For runtime supervision, notify() sends an sd_notify-compatible message to NOTIFY_SOCKET without linking libsystemd, with notifyReady() sending READY=1 and notifyWatchdog() sending WATCHDOG=1. daemonize() performs a Unix double-fork when supported.

SingleInstanceLock guards against concurrent instances using a RADCore::RADLockFile. Construct it with a lock path, call tryLock(), and check isLocked(); the lock is released in the destructor.

Core features:

  • systemd unit generation and writing from a ServiceUnit.
  • sd_notify-compatible ready/watchdog messages without libsystemd.
  • Single-instance process locks via RADCore::RADLockFile.
  • Optional Unix double-fork daemonization.

Example:

RADService::SingleInstanceLock lock("/run/radappd.lock");
if (!lock.tryLock()) {
return 1; // Another instance is already running.
}
unit.name = "radappd";
unit.description = "RAD appliance daemon";
unit.execStart = "/usr/bin/radappd";
unit.watchdogSec = 30;
RADService::ServiceManager::writeSystemdUnit(unit, "/etc/systemd/system");
static bool writeSystemdUnit(const ServiceUnit &unit, const std::string &directory, std::string *error=nullptr)
Writes a systemd unit file to directory.
static bool notifyReady(std::string *error=nullptr)
Sends READY=1 notification.
Single-instance app lock using RADCore::RADLockFile.
Definition: RADService.h:53
@ OnFailure
Restart on abnormal failure.
Systemd service unit description.
Definition: RADService.h:31
RestartPolicy restart
Restart policy.
Definition: RADService.h:43
std::string description
Description text.
Definition: RADService.h:35
int watchdogSec
Watchdog timeout seconds; 0 disables WatchdogSec.
Definition: RADService.h:45
std::string execStart
Executable command.
Definition: RADService.h:37
std::string name
Unit name without .service.
Definition: RADService.h:33