From c6443ee2e10c85d0a801d6c9213cef1c8d3f9911 Mon Sep 17 00:00:00 2001 From: Rzy <168882072+Iky969@users.noreply.github.com> Date: Tue, 1 Dec 2026 01:13:47 +0000 Subject: [PATCH] refactor(ninit): extract core prelude into ninit_core.c --- ninit.c | 111 ++----------------------------------------------- ninit_core.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 107 deletions(-) create mode 100644 ninit_core.c diff --git a/ninit.c b/ninit.c index 2f3af8b..a00dbf5 100644 --- a/ninit.c +++ b/ninit.c @@ -87,113 +87,10 @@ #include #include -#ifndef MAX_SERVICES -#define MAX_SERVICES 64 -#endif -#ifndef MAX_LINE -#define MAX_LINE 1024 -#endif -#ifndef RESPAWN_DELAY -#define RESPAWN_DELAY 2 -#endif -#ifndef MAX_SPAWN_RETRY -#define MAX_SPAWN_RETRY 3 /* maximum fork() retries for once services */ -#endif -#ifndef ONCE_START_TIMEOUT -#define ONCE_START_TIMEOUT 30 /* maximum runtime (seconds) for once services */ -#endif -#ifndef CRASH_LIMIT -#define CRASH_LIMIT 5 /* consecutive crashes before respawn is aborted */ -#endif -#ifndef BACKOFF_MAX -#define BACKOFF_MAX 32 /* upper bound for exponential respawn backoff (seconds) */ -#endif -#ifndef STABLE_RUNTIME -#define STABLE_RUNTIME 10 /* seconds of stable runtime before backoff is reset */ -#endif -#ifndef SIGTERM_GRACE_SECS -#define SIGTERM_GRACE_SECS 5 /* T13: default SIGTERM -> SIGKILL grace window (seconds) */ -#endif -#ifndef SPAWN_RETRY_BASE_MS -#define SPAWN_RETRY_BASE_MS 100 /* T11: fork() EAGAIN backoff base: 100/200/400 ms */ -#endif - -/* Service lifecycle states (T10). The numeric values are part of the internal - contract of svc_transition_allowed(); the relative order of the pre-existing - states (STOPPED < RUNNING < DONE < FAILED) is preserved while STARTING (= 1) - and STOPPING (= 3) are inserted at their canonical positions. - Every write to service_t.state goes through svc_set_state(). */ -typedef enum { - SVC_STATE_STOPPED = 0, /* idle: never started, stopped, or waiting to respawn */ - SVC_STATE_STARTING = 1, /* spawn sequence in progress (fork / retry backoff) */ - SVC_STATE_RUNNING = 2, /* spawned and tracked via pid/pgid */ - SVC_STATE_STOPPING = 3, /* stop requested: SIGTERM sent, grace window running */ - SVC_STATE_DONE = 4, /* once service finished with exit status 0 */ - SVC_STATE_FAILED = 5 /* terminal: spawn, exit, timeout, or dependency failure */ -} svc_state_t; - -typedef struct { - char name[128]; - char cmd[512]; - pid_t pid; - pid_t pgid; /* service process group: child calls setsid() so - pgid == pid at spawn. INTENTIONALLY not cleared when - the leader exits — the group may outlive its - leader (audit SANE-01). */ - int respawn; - int need_respawn; /* 1 = waiting for RESPAWN_DELAY window (without blocking loop) */ - int spawn_tries; /* consecutive fork() failures; reset to 0 on successful spawn */ - int cgroup_fail_open; /* 1 = cgroup setup failed, orphan cleanup via kill(-pgid) */ - long long last_start; /* last spawn monotonic_ms() (once timeout & respawn backoff) */ - int crash_count; /* consecutive crashes during respawn (start-limit) */ - int respawn_delay; /* current respawn delay (seconds, exponential backoff) */ - int timeout; /* timeout for once service (seconds); default ONCE_START_TIMEOUT; 0 = disabled */ - char deps_str[256]; - svc_state_t state; - int grace; /* T13: SIGTERM grace seconds (:grace=N), default SIGTERM_GRACE_SECS */ - int sigkill_sent; /* T13: 1 = SIGKILL already escalated (log/report once) */ - int fork_tries; /* T11: EAGAIN retries used in the current start episode */ - long long fork_retry_at; /* T11: monotonic ms deadline of the next fork() retry; 0 = due/none */ - int fork_first_logged; /* T11: 1 = "fork : " logged for this episode */ - char memory_max[64]; - char cpu_max[64]; - char pids_max[64]; - uid_t uid; /* FEAT-62: privilege dropping */ - gid_t gid; - int need_drop; - int manual_stop; /* FEAT-63: manually stopped via initctl */ - int restart_pending;/* FEAT-63: restart requested via initctl */ - int on_fail_rescue; /* 1 = enter supervised rescue shell on SVC_STATE_FAILED */ - long long stop_t0; -} service_t; - -static service_t services[MAX_SERVICES]; -static int num_services = 0; - -static int find_service(const char *name); -static void enable_cgroup_subtree(const char *path); -static void cgroup_kill_service(const char *name); -static void kill_service_group(const service_t *svc, int sig); -static int service_still_alive(service_t *svc); - -static volatile sig_atomic_t sigchld_flag = 0; -static volatile sig_atomic_t sigterm_flag = 0; -static volatile sig_atomic_t sigint_flag = 0; -static volatile sig_atomic_t sigusr1_flag = 0; -static volatile sig_atomic_t sigusr2_flag = 0; - -static int log_file_fd = -1; -static int g_log_fifo_fd = -1; /* read end of /run/log.fifo; also used during shutdown */ -static int initctl_fd = -1; /* /run/initctl fd; closed before exec */ -static int shutting_down = 0; /* 1 = shutdown in progress: do not respawn services */ -static int rescue_active = 0; /* 1 = rescue shell active: gate service start/restart */ -static pid_t rescue_shell_pid = -1; - -/* fstab mountpoints successfully mounted by mount_fstab(); used by - shutdown_all() to unmount before reboot (T8E). */ -#define MAX_SEEN_MNT 64 -static char g_seen_mnt[MAX_SEEN_MNT][MAX_LINE]; -static int g_seen_cnt = 0; +/* ---------- prelude ---------- */ +/* Unity phase: implementation lives in ninit_core.c, included here so this + * stays one translation unit. */ +#include "ninit_core.c" /* ---------- logging ---------- */ /* Unity phase: implementation lives in ninit_logging.c, included here so this diff --git a/ninit_core.c b/ninit_core.c new file mode 100644 index 0000000..e266c5d --- /dev/null +++ b/ninit_core.c @@ -0,0 +1,115 @@ +/* + * ninit_core.c - core configuration, service types, and shared state + * + * Unity phase: this file is included by the single compilation entry point. + * It is not a separate translation unit yet. Keep it free of direct references + * to the main source file so the consumer guard stays green. + */ + +#ifndef MAX_SERVICES +#define MAX_SERVICES 64 +#endif +#ifndef MAX_LINE +#define MAX_LINE 1024 +#endif +#ifndef RESPAWN_DELAY +#define RESPAWN_DELAY 2 +#endif +#ifndef MAX_SPAWN_RETRY +#define MAX_SPAWN_RETRY 3 /* maximum fork() retries for once services */ +#endif +#ifndef ONCE_START_TIMEOUT +#define ONCE_START_TIMEOUT 30 /* maximum runtime (seconds) for once services */ +#endif +#ifndef CRASH_LIMIT +#define CRASH_LIMIT 5 /* consecutive crashes before respawn is aborted */ +#endif +#ifndef BACKOFF_MAX +#define BACKOFF_MAX 32 /* upper bound for exponential respawn backoff (seconds) */ +#endif +#ifndef STABLE_RUNTIME +#define STABLE_RUNTIME 10 /* seconds of stable runtime before backoff is reset */ +#endif +#ifndef SIGTERM_GRACE_SECS +#define SIGTERM_GRACE_SECS 5 /* T13: default SIGTERM -> SIGKILL grace window (seconds) */ +#endif +#ifndef SPAWN_RETRY_BASE_MS +#define SPAWN_RETRY_BASE_MS 100 /* T11: fork() EAGAIN backoff base: 100/200/400 ms */ +#endif + +/* Service lifecycle states (T10). The numeric values are part of the internal + contract of svc_transition_allowed(); the relative order of the pre-existing + states (STOPPED < RUNNING < DONE < FAILED) is preserved while STARTING (= 1) + and STOPPING (= 3) are inserted at their canonical positions. + Every write to service_t.state goes through svc_set_state(). */ +typedef enum { + SVC_STATE_STOPPED = 0, /* idle: never started, stopped, or waiting to respawn */ + SVC_STATE_STARTING = 1, /* spawn sequence in progress (fork / retry backoff) */ + SVC_STATE_RUNNING = 2, /* spawned and tracked via pid/pgid */ + SVC_STATE_STOPPING = 3, /* stop requested: SIGTERM sent, grace window running */ + SVC_STATE_DONE = 4, /* once service finished with exit status 0 */ + SVC_STATE_FAILED = 5 /* terminal: spawn, exit, timeout, or dependency failure */ +} svc_state_t; + +typedef struct { + char name[128]; + char cmd[512]; + pid_t pid; + pid_t pgid; /* service process group: child calls setsid() so + pgid == pid at spawn. INTENTIONALLY not cleared when + the leader exits — the group may outlive its + leader (audit SANE-01). */ + int respawn; + int need_respawn; /* 1 = waiting for RESPAWN_DELAY window (without blocking loop) */ + int spawn_tries; /* consecutive fork() failures; reset to 0 on successful spawn */ + int cgroup_fail_open; /* 1 = cgroup setup failed, orphan cleanup via kill(-pgid) */ + long long last_start; /* last spawn monotonic_ms() (once timeout & respawn backoff) */ + int crash_count; /* consecutive crashes during respawn (start-limit) */ + int respawn_delay; /* current respawn delay (seconds, exponential backoff) */ + int timeout; /* timeout for once service (seconds); default ONCE_START_TIMEOUT; 0 = disabled */ + char deps_str[256]; + svc_state_t state; + int grace; /* T13: SIGTERM grace seconds (:grace=N), default SIGTERM_GRACE_SECS */ + int sigkill_sent; /* T13: 1 = SIGKILL already escalated (log/report once) */ + int fork_tries; /* T11: EAGAIN retries used in the current start episode */ + long long fork_retry_at; /* T11: monotonic ms deadline of the next fork() retry; 0 = due/none */ + int fork_first_logged; /* T11: 1 = "fork : " logged for this episode */ + char memory_max[64]; + char cpu_max[64]; + char pids_max[64]; + uid_t uid; /* FEAT-62: privilege dropping */ + gid_t gid; + int need_drop; + int manual_stop; /* FEAT-63: manually stopped via initctl */ + int restart_pending;/* FEAT-63: restart requested via initctl */ + int on_fail_rescue; /* 1 = enter supervised rescue shell on SVC_STATE_FAILED */ + long long stop_t0; +} service_t; + +static service_t services[MAX_SERVICES]; +static int num_services = 0; + +static int find_service(const char *name); +static void enable_cgroup_subtree(const char *path); +static void cgroup_kill_service(const char *name); +static void kill_service_group(const service_t *svc, int sig); +static int service_still_alive(service_t *svc); + +static volatile sig_atomic_t sigchld_flag = 0; +static volatile sig_atomic_t sigterm_flag = 0; +static volatile sig_atomic_t sigint_flag = 0; +static volatile sig_atomic_t sigusr1_flag = 0; +static volatile sig_atomic_t sigusr2_flag = 0; + +static int log_file_fd = -1; +static int g_log_fifo_fd = -1; /* read end of /run/log.fifo; also used during shutdown */ +static int initctl_fd = -1; /* /run/initctl fd; closed before exec */ +static int shutting_down = 0; /* 1 = shutdown in progress: do not respawn services */ +static int rescue_active = 0; /* 1 = rescue shell active: gate service start/restart */ +static pid_t rescue_shell_pid = -1; + +/* fstab mountpoints successfully mounted by mount_fstab(); used by + shutdown_all() to unmount before reboot (T8E). */ +#define MAX_SEEN_MNT 64 +static char g_seen_mnt[MAX_SEEN_MNT][MAX_LINE]; +static int g_seen_cnt = 0;