#ifndef SCORPIOX_NSSM_H #define SCORPIOX_NSSM_H #include "platform.h" #ifdef __cplusplus extern "C" { #endif /* Lifecycle & information */ SCORPIOX_API int nssm_init(void); SCORPIOX_API void nssm_cleanup(void); SCORPIOX_API const char *nssm_version(void); SCORPIOX_API const char *nssm_last_error(void); /* Service Control Manager */ SCORPIOX_API int nssm_service_install( const char *service_name, const char *exe_path, const char *app_args, const char *display_name, const char *description, int start_type /* 2=Auto, 3=Manual, 4=Disabled */ ); SCORPIOX_API int nssm_service_remove(const char *service_name, int force); SCORPIOX_API int nssm_service_start(const char *service_name); SCORPIOX_API int nssm_service_stop(const char *service_name, int timeout_ms); SCORPIOX_API int nssm_service_restart(const char *service_name, int timeout_ms); SCORPIOX_API int nssm_service_pause(const char *service_name); SCORPIOX_API int nssm_service_continue(const char *service_name); SCORPIOX_API int nssm_service_status(const char *service_name); SCORPIOX_API const char *nssm_service_status_string(const char *service_name); SCORPIOX_API const char *nssm_service_list_json(void); SCORPIOX_API const char *nssm_service_get_config_json(const char *service_name); /* Parameter store (Registry Parameters / mock) */ SCORPIOX_API int nssm_service_set_param(const char *service_name, const char *param_name, const char *param_value); SCORPIOX_API const char *nssm_service_get_param(const char *service_name, const char *param_name); SCORPIOX_API int nssm_service_reset_param(const char *service_name, const char *param_name); /* SCM recovery (failure actions) — the same policy Services.msc's Recovery tab * * Action codes: 0 = take no action, 1 = restart the service, 2 = reboot, * 3 = run a command (representable in the engine, not exposed by the UI). * reset_seconds: SCM dwResetPeriod — the failure counter resets after this many * seconds without a failure. 0 = never reset. * Returns 0 on success, -1 on failure with nssm_last_error() set. */ SCORPIOX_API int nssm_service_set_recovery(const char *service_name, int first, int second, int subsequent, int reset_seconds); /* Read back the SCM recovery policy as JSON: * {"reset_seconds":86400,"first":1,"second":1,"subsequent":0} * Never NULL; "{}" if the service cannot be queried. A service with no policy * reads {"reset_seconds":0,"first":0,"second":0,"subsequent":0}. */ SCORPIOX_API const char *nssm_service_get_recovery_json(const char *service_name); /* Service identity (display name + description) of an EXISTING service — the * two boxes Services.msc's own service property sheet edits. This is what * `nssm_service_install` writes once; this call changes them afterwards. * * display_name: written with ChangeServiceConfigW and SERVICE_NO_CHANGE for * every other argument, so the binPath/start type are never rewritten from a * stale read. An empty/NULL display_name falls back to the service name — * never leave a service with a blank display name (use the same rule the * app's install path uses). * description: written with ChangeServiceConfig2W(SERVICE_CONFIG_DESCRIPTION). * An empty string CLEARS the description — that is the user's intent, not an * error. NULL leaves the description untouched. * * Opens the service with SERVICE_ALL_ACCESS, for the reason measured on .212 * (2026-10-31) and recorded on the recovery path: SERVICE_CHANGE_CONFIG alone * returns ERROR_ACCESS_DENIED (5) on a service our own install created. * * Returns 0 on success, -1 on failure with nssm_last_error() set. */ SCORPIOX_API int nssm_service_set_identity(const char *service_name, const char *display_name, const char *description); /* Service dependencies — "this service depends on X, Y" — the Dependencies * tab of Services.msc's own service property sheet. * * deps is a NEWLINE-separated list of service names (same delimiter convention * as env_extra): "RpcSs\nTcpip". Empty or NULL clears the dependency list — * that is the user's intent, not an error. Entries are not validated against * installed services: a dependency may legitimately name a service this * application does not manage (RpcSs, Tcpip, ...). The SCM accepts any name and * refuses to START the service when a dependency is missing — that rule belongs * to the SCM, not to us. * * Windows: ChangeServiceConfigW with SERVICE_NO_CHANGE for every other * argument and lpDependencies (the 8th argument) as a double-NUL-terminated * MULTI_SZ. Opens with SERVICE_ALL_ACCESS for the reason recorded on the * recovery/identity paths: SERVICE_CHANGE_CONFIG alone returns * ERROR_ACCESS_DENIED (5) on a service our own install created. * * Read back through nssm_service_get_config_json, which always carries a root * "dependencies" array ([] when there are none) taken from the SCM's own * QUERY_SERVICE_CONFIGW — never from our own cache. * * Returns 0 on success, -1 on failure with nssm_last_error() set. */ SCORPIOX_API int nssm_service_set_dependencies(const char *service_name, const char *deps); /* Service log-on identity — "who does this service run as" — the Log On tab of * Services.msc's own service property sheet. * * account is the SCM's own string, written verbatim and never mapped through a * table of our own: "LocalSystem", "NT AUTHORITY\\NetworkService", * "NT AUTHORITY\\LocalService", or a user-supplied "DOMAIN\\user" / ".\\user". * NULL or "" is refused (a service must always have an identity). * * password semantics: * NULL or "" -> leave the password unchanged (edit without retyping). * a string -> written as the service password. * An account that DIFFERS from the SCM's current service start name written * with no password is refused with "password required for " — the * engine does the comparison because it already holds the service open, so the * app never has to re-read to decide. A system account (LocalSystem / * NetworkService / LocalService) needs no password. * * Windows: ChangeServiceConfigW with SERVICE_NO_CHANGE for every other argument * and lpServiceStartName (the 9th argument) / lpPassword (the 10th). Opens with * SERVICE_ALL_ACCESS for the reason recorded on the recovery/identity paths. * * The password is never read back, never logged, and never appears in any JSON: * Windows does not return it and neither do we. Read the identity back through * nssm_service_get_config_json's root "service_start_name" (and every * nssm_service_list_json row's "start_name"), both taken from the SCM's own * QUERY_SERVICE_CONFIGW. * * Returns 0 on success, -1 on failure with nssm_last_error() set. */ SCORPIOX_API int nssm_service_set_account(const char *service_name, const char *account, const char *password); /* Automatic (Delayed start) — Services.msc's Startup-type entry * "Automatic (Delayed Start)", i.e. `sc config start= delayed-auto`. * * This is ORTHOGONAL to the start type: a delayed service keeps * START_TYPE 2 AUTO_START in `sc qc` and simply carries DelayedAutostart = 1 * under its service key, so the boot loader starts it after the critical * services instead of in the first wave. We never invent a fifth start type. * * delayed: 1 = on, 0 = off. Anything else is refused. * * Windows: ChangeServiceConfig2W(svc, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, * &SERVICE_DELAYED_AUTO_START_INFO{ fDelayedAutostart }) — its own info-level * call with its own struct, so it cannot disturb the description, failure * actions, dependencies or identity writes. Opens with SERVICE_ALL_ACCESS for * the measured reason recorded on the recovery/identity/dependencies paths: * SERVICE_CHANGE_CONFIG alone returns ERROR_ACCESS_DENIED (5) on a service our * own install created. * * An unqueryable policy is never reported as "off". Read it back through * nssm_service_get_config_json's root "delayed_auto_start" and every * nssm_service_list_json row's "delayed_auto_start" — BOTH KEYS ARE OMITTED * when the SCM cannot answer, exactly like start_type/start_type_known. * * Returns 0 on success, -1 on failure with nssm_last_error() set. */ SCORPIOX_API int nssm_service_set_delayed_start(const char *service_name, int delayed); /* Start type of an EXISTING service — Services.msc's "Startup type" dropdown, * i.e. `sc config start= auto|demand|disabled`. * * start_type uses the engine's existing Win32 codes and nothing else: * 2 = Automatic (SERVICE_AUTO_START) * 3 = Manual (SERVICE_DEMAND_START) * 4 = Disabled (SERVICE_DISABLED) * Any other value is refused before the SCM is touched, with nssm_last_error() * naming the accepted set — the same refusal the install path already emits, so * a caller can never silently write SERVICE_BOOT_START (0) and hand Windows a * service it cannot start. * * Windows: ChangeServiceConfigW with dwStartType as the ONLY argument that is * not SERVICE_NO_CHANGE, and all six trailing pointers NULL — a stale read can * never rewrite the binPath, the dependency list, the log-on account or the * display name. Opens with SERVICE_ALL_ACCESS for the reason recorded on the * recovery/identity paths: SERVICE_CHANGE_CONFIG alone returns * ERROR_ACCESS_DENIED (5) on a service our own install created. * * DelayedAutostart is a separate SERVICE_CONFIG_DELAYED_AUTO_START_INFO value * and is deliberately NOT touched here — it survives a start-type write, which * is what `sc config start=` does too. The SCM ignores the flag while the * service is Manual/Disabled. * * Read back through nssm_service_get_config_json's root "start_type" and every * nssm_service_list_json row's "start_type" / "start_type_known" pair. * * Returns 0 on success, -1 on failure with nssm_last_error() set. */ SCORPIOX_API int nssm_service_set_start_type(const char *service_name, int start_type); /* Dispatcher & supervisor */ typedef struct { const char *exe_path; const char *args; const char *work_dir; const char *stdout_path; const char *stderr_path; /* Extra environment variables for the supervised process, as a list of * NAME=VALUE entries separated by '\n'. Merged over the service's own * environment; the child inherits everything else as usual. NULL or empty * means "inherit unchanged". */ const char *env_extra; int auto_restart; int throttle_ms; int rotate_files; unsigned long rotate_bytes; int stop_timeout_ms; } nssm_supervisor_config_t; SCORPIOX_API int nssm_service_run_dispatcher(const char *service_name); SCORPIOX_API int nssm_supervisor_run(const nssm_supervisor_config_t *config); SCORPIOX_API void nssm_supervisor_request_stop(void); #ifdef __cplusplus } #endif #endif /* SCORPIOX_NSSM_H */