#ifndef O6H_GUARD_H #define O6H_GUARD_H #include #include #include #define HIST 64 #define MAX_ZONES 256 #define MAX_PROCS 4096 #define MAX_RULES 128 #define MAX_VICTIMS 32 #define MAX_SWAP_DEVICES 64 #define CONF_PATH "/opt/custom/etc/o6h-memory-guard.conf" #define LOG_DIR "/opt/custom/var/log/o6h-memory-guard" #define RUN_DIR "/opt/custom/var/run" #define VERSION "1.0.4" enum state { NORMAL, PRE_PRESSURE, PRESSURE, CRITICAL, EMERGENCY, RECOVERY }; enum policy { OBSERVE, ALLOW, PROTECT }; enum action { NONE, TERM, KILL }; typedef struct { double at; uint64_t value; } point_t; typedef struct { point_t p[HIST]; unsigned next, count; } history_t; typedef struct { int valid; double age; int64_t value; } delta_t; typedef struct { double at; uint64_t free, lots, des, min, scan, kernel, locked; uint64_t arc, swap_alloc, swap_reserved, swap_available; uint64_t swap_physical_used_bytes, anon_pageout_pages; int arc_ok, swap_virtual_ok, swap_physical_ok, pageout_ok; } sample_t; typedef struct { char uuid[37], alias[64]; enum policy policy; } zone_rule_t; typedef struct { int armed; unsigned pre_s, pressure_s, critical_s, recovery_s, critical_term_s; unsigned emergency_term_s, floor_kill_delay_s, max_cached_candidate_age_s; unsigned normal_scan_s, scan_budget_ms, cooldown_s, max_victims; unsigned max_emergency_victims, max_floor_victims; unsigned max_per_minute, log_mib, log_keep; uint64_t min_rss_kib; zone_rule_t zones[MAX_ZONES]; size_t nzones; char protected_names[MAX_RULES][32]; size_t nnames; int protected_pids[MAX_RULES]; size_t npids; } config_t; typedef struct { int pid, ppid, zid, initpid, nlwp; uint32_t uid, euid; int64_t start_sec, start_nsec; uint64_t zone_generation, rss_kib; char uuid[64], name[32]; } process_t; typedef struct { enum state state; double low_since, critical_since, recovery_since, last_at; uint64_t last_free; unsigned low_samples; int have_last, danger, floor; } engine_t; typedef struct { process_t pending; int has_pending; double term_at, next_at; process_t victims[MAX_VICTIMS]; unsigned nvictims; double rate[64]; unsigned nrate; } controller_t; typedef struct { enum action action; process_t target; const char *reason; const char *budget_override; } plan_t; void defaults(config_t *); int config_read(FILE *, config_t *, char *, size_t); int uuid_valid(const char *); const zone_rule_t *zone_rule(const config_t *, const char *); int protected_process(const config_t *, const process_t *); int same_process(const process_t *, const process_t *); void history_add(history_t *, double, uint64_t); delta_t history_delta(const history_t *, double, unsigned); uint64_t rank_score(uint64_t, delta_t); int swap_physical_bytes_from_blocks(uint64_t, uint64_t, uint64_t *); const char *state_name(enum state); void engine_init(engine_t *); void engine_gap(engine_t *); void engine_step(engine_t *, const config_t *, const sample_t *, delta_t, delta_t, delta_t); int was_victim(const controller_t *, const process_t *); plan_t action_plan(controller_t *, const config_t *, const engine_t *, const sample_t *, const process_t *); void action_commit(controller_t *, const config_t *, const sample_t *, const plan_t *); /* Native backend. No backend is linked into the portable policy tests. */ int platform_init(void); void platform_fini(void); int platform_sample(sample_t *); int platform_walk(int (*)(const process_t *, void *), void *, unsigned); int platform_signal(const config_t *, const process_t *, enum action, int, uint64_t); double monotime(void); int secure_file(const char *, int, unsigned); int secure_directory(const char *); #endif