C API
Program
Inspect the compiled program, permitted queries and normalized rules of a loaded policy.Inspect what a loaded policy compiled before using it: its admitted queries, identity, predicates, static facts, normalized rules and required capabilities. The Program guide shows a complete read-only inspection flow using a program borrowed from a live session.
| Used by | backend_t — storage_requirements · backend_t — prepare · backend_v6_t — storage_requirements · backend_v6_t — prepare · backend_v7_t — storage_requirements · backend_v7_t — prepare |
|---|
Handle: program_t
#include <maelys/datalog_program.h>
typedef struct maelys_datalog_program maelys_datalog_program_t;Functions
program_fact()
#include <maelys/datalog_program.h>
maelys_datalog_status_t
maelys_datalog_program_fact(
const maelys_datalog_program_t *program,
size_t index,
maelys_datalog_ir_atom_t *out_atom
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK with a normalized static fact. An out-of-range index returns NOT_FOUND; NULL arguments return INVALID_ARGUMENT.
maelys_datalog_ir_atom_t fact;
maelys_datalog_status_t status = maelys_datalog_program_fact(program, 0, &fact);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("static fact predicate: %s\n", fact.predicate);status: ok static fact predicate: user
program_fingerprint()
#include <maelys/datalog_program.h>
maelys_datalog_status_t
maelys_datalog_program_fingerprint(
const maelys_datalog_program_t *program,
char out_fingerprint[MAELYS_DATALOG_PUBLIC_FINGERPRINT_BYTES]
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK with the compiled-program fingerprint in the caller buffer. NULL arguments return INVALID_ARGUMENT.
char fingerprint[MAELYS_DATALOG_PUBLIC_FINGERPRINT_BYTES] = {0};
maelys_datalog_status_t status = maelys_datalog_program_fingerprint(program, fingerprint);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("compiled fingerprint present: %s\n", fingerprint[0] != '\0' ? "yes" : "no");status: ok compiled fingerprint present: yes
program_info()
#include <maelys/datalog_program.h>
maelys_datalog_status_t
maelys_datalog_program_info(
const maelys_datalog_program_t *program,
maelys_datalog_program_info_t *out_info
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK with a copied information record. NULL arguments return INVALID_ARGUMENT.
maelys_datalog_program_info_t info;
maelys_datalog_status_t status = maelys_datalog_program_info(program, &info);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("predicates: %zu\n", info.predicate_count);
printf("static facts: %zu\n", info.fact_count);
printf("rules: %zu\n", info.rule_count);status: ok predicates: 2 static facts: 1 rules: 1
program_predicate()
#include <maelys/datalog_program.h>
maelys_datalog_status_t
maelys_datalog_program_predicate(
const maelys_datalog_program_t *program,
size_t index,
maelys_datalog_predicate_t *out_predicate
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK with the indexed predicate descriptor. An out-of-range index returns NOT_FOUND; NULL arguments return INVALID_ARGUMENT.
maelys_datalog_predicate_t predicate;
maelys_datalog_status_t status = maelys_datalog_program_predicate(program, 0, &predicate);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("predicate: %s\n", predicate.name);status: ok predicate: user
program_query()
#include <maelys/datalog_program.h>
maelys_datalog_status_t
maelys_datalog_program_query(
const maelys_datalog_program_t *program,
size_t index,
maelys_datalog_predicate_t *out_predicate
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK with an admitted query; returns NOT_FOUND for an index outside the query count. NULL arguments return INVALID_ARGUMENT; output remains unchanged on failure.
maelys_datalog_predicate_t query;
maelys_datalog_status_t status = maelys_datalog_program_query(program, 0, &query);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("admitted query: %s\n", query.name);status: ok admitted query: allow
program_query_count()
#include <maelys/datalog_program.h>
maelys_datalog_status_t
maelys_datalog_program_query_count(
const maelys_datalog_program_t *program,
size_t *out_count
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK with the admitted-query count. NULL arguments return INVALID_ARGUMENT; output remains unchanged on failure.
size_t count = 0;
maelys_datalog_status_t status = maelys_datalog_program_query_count(program, &count);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("admitted queries: %zu\n", count);status: ok admitted queries: 1
program_rule()
#include <maelys/datalog_program.h>
maelys_datalog_status_t
maelys_datalog_program_rule(
const maelys_datalog_program_t *program,
size_t index,
maelys_datalog_ir_rule_t *out_rule
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK with a normalized rule. An out-of-range index returns NOT_FOUND; NULL arguments return INVALID_ARGUMENT.
maelys_datalog_ir_rule_t rule;
maelys_datalog_status_t status = maelys_datalog_program_rule(program, 0, &rule);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("rule head: %s\n", rule.head.predicate);
printf("body literals: %zu\n", rule.body_count);status: ok rule head: allow body literals: 1
program_rule_variable_name()
#include <maelys/datalog_program.h>
maelys_datalog_status_t
maelys_datalog_program_rule_variable_name(
const maelys_datalog_program_t *program,
size_t rule_index,
uint32_t variable,
const char **out_name
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK and assigns out_name to the complete, case-sensitive, NUL-terminated source spelling, borrowed until policy_free or, for a session_program view, session_free. Source buffers need not remain alive. No allocation. Returns NOT_FOUND for an out-of-range rule, a variable number of 26 or greater (including anonymous variables), an unused variable in this OR alternative, a rule built by program_add_rule, or a spelling omitted when the bounded metadata pool filled. INVALID_ARGUMENT means program, its underlying ruleset or out_name is NULL. Failure leaves out_name unchanged. Single-letter names keep their source spelling; variable numbers and explanation formats do not change. Named variables in aggregate projections and outputs, and named local variables, are covered.
maelys_datalog_ir_rule_t rule;
check_status(maelys_datalog_program_rule(program, 0, &rule));
const char *name = NULL;
maelys_datalog_status_t status = maelys_datalog_program_rule_variable_name(
program, 0, rule.head.terms[0].as.variable, &name);
printf("named lookup: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("source name: %s\n", name);
check_status(maelys_datalog_program_rule(program, 1, &rule));
const char *unchanged = name;
status = maelys_datalog_program_rule_variable_name(
program, 1, rule.body[0].atom.terms[0].as.variable, &name);
printf("anonymous lookup: %s\n", maelys_datalog_status_name(status));
if (status != MAELYS_DATALOG_STATUS_NOT_FOUND || name != unchanged) exit(EXIT_FAILURE);named lookup: ok source name: User anonymous lookup: not_found
Constants
Availability
HAS_RULE_VARIABLE_NAMES
#define MAELYS_DATALOG_HAS_RULE_VARIABLE_NAMES 1Reports that the SDK provides source-variable name lookup.
1
Handle: session_t ↗
Creation
session_program()
#include <maelys/datalog_program.h>
maelys_datalog_status_t
maelys_datalog_session_program(
const maelys_datalog_session_t *session,
const maelys_datalog_program_t **out_program
);Written on success.
maelys_datalog_status_t — OK on success.
Returns OK and a borrowed, read-only view owned by the live session. NULL arguments return INVALID_ARGUMENT.
const maelys_datalog_program_t *borrowed = NULL;
maelys_datalog_status_t status = maelys_datalog_session_program(session, &borrowed);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("program borrowed: %s\n", borrowed != NULL ? "yes" : "no");status: ok program borrowed: yes
Struct: program_info_t
#include <maelys/datalog_program.h>
Typedef
typedef struct {
uint32_t abi_version;
const char *policy_id;
const char *domain;
const char *fingerprint;
size_t predicate_count, fact_count, rule_count;
size_t max_input_facts, max_derived_facts, max_facts_per_predicate;
uint64_t required_capabilities;
} maelys_datalog_program_info_t;Constants
Program ABI
PROGRAM_ABI_VERSION
#define MAELYS_DATALOG_PROGRAM_ABI_VERSION 2uVersion of the read-only compiled-program inspection contract.
2
Struct: ir_atom_t
#include <maelys/datalog_program.h>
Typedef
typedef struct {
const char *predicate;
size_t arity;
maelys_datalog_ir_term_t terms[MAELYS_DATALOG_PUBLIC_MAX_TERMS];
} maelys_datalog_ir_atom_t;| Used by | program_add_fact() · explanation_obstacle_view_t — pattern · explanation_premise_view_t — atom · ir_literal_t — atom · ir_rule_t — head |
|---|
Struct: ir_rule_t
#include <maelys/datalog_program.h>
Typedef
typedef struct {
maelys_datalog_ir_atom_t head;
size_t body_count;
maelys_datalog_ir_literal_t body[MAELYS_DATALOG_IR_MAX_BODY];
size_t expression_count;
maelys_datalog_ir_expression_t expressions[MAELYS_DATALOG_IR_MAX_EXPRESSIONS];
maelys_datalog_source_location_t source;
} maelys_datalog_ir_rule_t;| Used by | program_add_rule() |
|---|
Constants
Rule limits
IR_MAX_BODY
#define MAELYS_DATALOG_IR_MAX_BODY 8uMaximum number of literals in a normalized rule body.
8
IR_MAX_EXPRESSIONS
#define MAELYS_DATALOG_IR_MAX_EXPRESSIONS 32uMaximum number of expression nodes in a normalized rule.
32
IR_MAX_VARIABLES
#define MAELYS_DATALOG_IR_MAX_VARIABLES 32uMaximum number of variables in a normalized rule.
32
Struct: ir_literal_t
#include <maelys/datalog_program.h>
Typedef
typedef struct {
maelys_datalog_ir_literal_kind_t kind;
maelys_datalog_ir_atom_t atom;
maelys_datalog_ir_term_t lhs, rhs;
maelys_datalog_ir_comparison_t comparison;
int has_arithmetic;
uint32_t lhs_expression, rhs_expression;
const char *filter_name;
const char *filter_semantic_id; /* Optional on input; checked if present. */
const unsigned char *pattern;
size_t pattern_length;
maelys_datalog_ir_term_t filter_value;
} maelys_datalog_ir_literal_t;| Used by | ir_rule_t — body |
|---|
Struct: ir_expression_t
#include <maelys/datalog_program.h>
Typedef
typedef struct {
maelys_datalog_ir_expression_kind_t kind;
/* Binary operands refer to earlier nodes. Each root's expanded tree must
* also fit MAX_EXPRESSIONS, bounding recursive evaluation of shared edges. */
uint32_t left, right;
maelys_datalog_ir_term_t term;
} maelys_datalog_ir_expression_t;| Used by | ir_rule_t — expressions |
|---|
Constants
Expression sentinel
IR_NO_EXPRESSION
#define MAELYS_DATALOG_IR_NO_EXPRESSION UINT32_MAXSentinel for a literal with no arithmetic-expression root.
4294967295
Struct: ir_term_t
#include <maelys/datalog_program.h>
Typedef
typedef struct {
maelys_datalog_ir_term_kind_t kind;
union {
const char *symbol;
int64_t integer;
int boolean;
uint32_t variable;
} as;
} maelys_datalog_ir_term_t;| Used by | ir_atom_t — terms · ir_expression_t — term · ir_literal_t — lhs · ir_literal_t — rhs · ir_literal_t — filter_value |
|---|
Struct: source_location_t
#include <maelys/datalog_program.h>
Typedef
typedef struct {
size_t line, column; /* 1-based, or both zero when unavailable. */
} maelys_datalog_source_location_t;| Used by | ir_rule_t — source |
|---|
Enum: ir_literal_kind_t
#include <maelys/datalog_program.h>
Typedef
typedef enum {
MAELYS_DATALOG_IR_ATOM = 1,
MAELYS_DATALOG_IR_COMPARISON = 2,
MAELYS_DATALOG_IR_NEGATION = 3,
MAELYS_DATALOG_IR_FILTER = 4,
/* atom = source pattern, lhs = local projection variable, rhs = output
* variable, both in 0..25. Other source variables in 0..25 are group keys
* bound by ordinary positive atoms; 26..31 are local existentials. Neither
* projection nor local existentials may escape into the surrounding rule.
* Other fields inactive. Requires CAP_AGGREGATES. */
MAELYS_DATALOG_IR_COUNT = 5,
/* Same active fields and scoping as COUNT; integer projection only.
* MIN/MAX fail on an empty group. SUM returns zero and sums distinct
* complete source facts (not distinct projected values). Requires the
* corresponding CAP_MIN, CAP_MAX or CAP_SUM, independently of COUNT. */
MAELYS_DATALOG_IR_MIN = 6,
MAELYS_DATALOG_IR_MAX = 7,
MAELYS_DATALOG_IR_SUM = 8
} maelys_datalog_ir_literal_kind_t;| Used by | ir_literal_t — kind |
|---|
Enum: ir_term_kind_t
#include <maelys/datalog_program.h>
Typedef
typedef enum {
MAELYS_DATALOG_IR_SYMBOL = 1,
MAELYS_DATALOG_IR_INTEGER = 2,
MAELYS_DATALOG_IR_BOOLEAN = 3,
MAELYS_DATALOG_IR_VARIABLE = 4
} maelys_datalog_ir_term_kind_t;| Used by | ir_term_t — kind |
|---|
Enum: ir_comparison_t
#include <maelys/datalog_program.h>
Typedef
typedef enum {
MAELYS_DATALOG_IR_EQ = 1,
MAELYS_DATALOG_IR_NE = 2,
MAELYS_DATALOG_IR_LT = 3,
MAELYS_DATALOG_IR_LE = 4,
MAELYS_DATALOG_IR_GT = 5,
MAELYS_DATALOG_IR_GE = 6
} maelys_datalog_ir_comparison_t;| Used by | explanation_obstacle_view_t — op · explanation_premise_view_t — op · ir_literal_t — comparison |
|---|
Enum: ir_expression_kind_t
#include <maelys/datalog_program.h>
Typedef
typedef enum {
MAELYS_DATALOG_IR_EXPR_INTEGER = 1,
MAELYS_DATALOG_IR_EXPR_VARIABLE = 2,
MAELYS_DATALOG_IR_EXPR_ADD = 3,
MAELYS_DATALOG_IR_EXPR_SUB = 4,
MAELYS_DATALOG_IR_EXPR_MUL = 5
} maelys_datalog_ir_expression_kind_t;| Used by | ir_expression_t — kind |
|---|