Files
glacier-ng/lib/glacier_cdef.lua
2026-10-29 09:15:34 -04:00

425 lines
14 KiB
Lua

-- lib/glacier_cdef.lua
--
-- Single source of truth for every ffi.cdef() declaration used across
-- gpkg, gstore, and the Lua test suite. These used to be hand-copied
-- into each entry point separately, which is exactly what let a stale
-- gl_install_pkg/gl_remove_pkg signature drift silently into gpkg
-- during the transaction-context refactor: nothing caught it, because
-- Lua FFI has no compiler to reject a mismatched declaration the way C
-- would.
--
-- require() caches modules, so the ffi.cdef() call below runs exactly
-- once per process no matter how many files require() this — calling
-- ffi.cdef() twice with conflicting content would error, but that's
-- not a concern here since there's only one copy now.
--
-- This module declares the C ABI only. It does not call ffi.load() for
-- you automatically — different entry points may want the library from
-- different explicit paths — but M.load() below gives every caller a
-- consistent, CWD-independent way to find it if they don't need
-- anything more specific.
local ffi = require("ffi")
ffi.cdef[[
typedef unsigned int uid_t;
typedef int pid_t;
unsigned int getuid(void);
unsigned int getpid(void);
uid_t geteuid(void);
void free(void *ptr);
char *getcwd(char *buf, size_t size);
char *strerror(int errnum);
void lg_printf(int loglevel, const char *fmt, ...);
void lg_gpkg_help(int argc, const char *argv[]);
void lg_gstore_help(int argc, const char *argv[]);
void lg_syspkg_help(int argc, const char *argv[]);
typedef enum {
INDEX,
STORE
} index_store_t;
_Bool gl_usr_istore_exists(index_store_t index_or_store, int uid);
int gl_usr_base_status(uid_t uid);
int gl_prune_backups(const char *dir, int keep);
/* --- pkg.h: glacier.conf --- */
typedef enum {
GL_CONF_OK = 0,
GL_CONF_ERR_READ = 1,
GL_CONF_ERR_MISSING = 2,
GL_CONF_ERR_TYPE = 3,
GL_CONF_ERR_INVALID = 4,
GL_CONF_ERR_SPACE = 5
} gl_conf_status_t;
gl_conf_status_t gl_conf_get_int(const char *path, const char *key, int *out);
gl_conf_status_t gl_conf_repo_keys(const char *path, const char *repo,
char *out, size_t outlen);
gl_conf_status_t gl_conf_repos(const char *path, char *out, size_t outlen);
typedef struct {
char *traits;
char *cflags;
char *cxxflags;
char *ldflags;
char *configure;
int jobs;
char *build_user;
char *build_root;
} gl_build_conf_t;
typedef enum {
GL_CONF_SYSTEM = 0,
GL_CONF_USER = 1,
GL_CONF_PACKAGE = 2
} gl_conf_kind_t;
gl_conf_status_t gl_conf_build(const char *path, gl_conf_kind_t kind,
gl_build_conf_t *out);
void gl_build_conf_free(gl_build_conf_t *c);
/* --- pkg.h: repository indexes --- */
typedef struct {
char *name;
char *ver;
char *file;
char *sha256;
int64_t size;
char *deps;
char *confs;
char *build_deps;
char *traits;
} gl_index_pkg_t;
typedef struct {
char *repo;
int64_t serial;
int64_t valid_until;
size_t npkgs;
gl_index_pkg_t *pkgs;
} gl_index_t;
typedef enum {
GL_INDEX_OK = 0,
GL_INDEX_ERR_FORMAT = 1,
GL_INDEX_ERR_REPO = 2,
GL_INDEX_ERR_NOMEM = 3
} gl_index_status_t;
gl_index_status_t gl_index_parse(const char *text, size_t len,
const char *repo, gl_index_t *out);
void gl_index_free(gl_index_t *idx);
typedef struct {
char *name;
char *ver;
char *repo;
char *desc;
char *maint;
char *copyright;
char *sources;
char *patches;
char *build;
char *configure;
char *traits;
char *trait_args;
char *deps;
char *build_deps;
char *confs;
} gl_recipe_t;
typedef enum {
GL_RECIPE_OK = 0,
GL_RECIPE_ERR_READ = 1,
GL_RECIPE_ERR_BUNDLE = 2,
GL_RECIPE_ERR_FORMAT = 3,
GL_RECIPE_ERR_NOMEM = 4
} gl_recipe_status_t;
gl_recipe_status_t gl_recipe_parse(const char *text, size_t len, gl_recipe_t *out);
gl_recipe_status_t gl_recipe_read(const char *bundle, gl_recipe_t *out);
const char *gl_recipe_error(void);
void gl_recipe_free(gl_recipe_t *r);
bool gl_valid_trait(const char *s);
/* --- transaction.h --- */
typedef enum {
GL_CTX_LIVE,
GL_CTX_STAGE
} gl_ctx_mode_t;
typedef enum {
GL_SCOPE_USR,
GL_SCOPE_SYS
} gl_ctx_scope_t;
typedef struct {
uid_t uid;
char root_path[4096];
char index_path[4096];
char store_path[4096];
char links_path[4096];
gl_ctx_mode_t mode;
gl_ctx_scope_t scope;
int lock_fd;
int overwrite;
char sysroot[4096];
int link_refusals;
int strict;
} gl_context_t;
typedef enum {
GL_TXN_OK = 0,
GL_TXN_ERR_LOCK = 1,
GL_TXN_ERR_MKDIR = 2,
GL_TXN_ERR_PERM = 3,
GL_TXN_ERR_RENAME = 4,
GL_TXN_ERR_SYNC = 5,
GL_TXN_ERR_CLEANUP = 6,
GL_TXN_ERR_NO_TXN = 7,
GL_TXN_ERR_XDEV = 8,
GL_TXN_ERR_DEPS = 9
} gl_txn_status_t;
int gl_check_sysroot(const char *sysroot);
int gl_init_live_context(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid);
int gl_init_live_context_at(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid,
const char *sysroot);
gl_txn_status_t gl_init_stage_context(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid);
gl_txn_status_t gl_init_stage_context_at(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid,
const char *sysroot);
gl_txn_status_t gl_begin_transaction(gl_context_t *ctx);
int gl_scope_state_dir(const gl_context_t *ctx, const char *name, char *out);
int gl_scope_cache_dir(const gl_context_t *ctx, const char *repo, char *out);
int gl_scope_build_dir(const gl_context_t *ctx, const char *name, int owner, char *out);
gl_txn_status_t gl_commit_transaction(gl_context_t *ctx);
gl_txn_status_t gl_abort_transaction(gl_context_t *ctx);
/* --- istoreutils.h --- */
typedef enum {
GL_INSTALL_OK = 0,
GL_INSTALL_ERR_OPEN = 1,
GL_INSTALL_ERR_MANIFEST = 2,
GL_INSTALL_ERR_EXTRACT = 3,
GL_INSTALL_ERR_INDEX = 4,
GL_INSTALL_ERR_ALREADY_INSTALLED = 5,
GL_INSTALL_SKIPPED = 6,
GL_INSTALL_ERR_SCOPE = 7,
GL_INSTALL_ERR_DEPS = 8
} gl_install_status_t;
typedef enum {
GL_REMOVE_OK = 0,
GL_REMOVE_ERR_NOT_FOUND = 1,
GL_REMOVE_ERR_UNLINK = 2,
GL_REMOVE_ERR_INDEX = 3,
GL_REMOVE_ERR_DEPS = 4
} gl_remove_status_t;
typedef struct {
char **repos;
size_t count;
} gl_repo_list_t;
int gl_init_user(uid_t uid, bool isVerbose);
int gl_init_sys(bool isVerbose);
int gl_init_sys_at(const char *sysroot, bool isVerbose);
int gl_create_fs_tree(const char *root, bool isVerbose);
int gl_delete_user(uid_t uid, bool isVerbose);
gl_repo_list_t gl_find_pkg_repos(gl_context_t *ctx, const char *pkg_name);
void gl_free_repo_list(gl_repo_list_t *list);
gl_install_status_t gl_install_pkg(gl_context_t *ctx, const char *gpkg_path);
gl_install_status_t gl_extract_files(const char *gpkg_path, const char *dest);
gl_remove_status_t gl_remove_pkg(gl_context_t *ctx, const char *pkg_name, const char *pkg_repo);
int gl_backup_pkg(gl_context_t *ctx, const char *repo, const char *name, const char *out_path);
int gl_relink_store(gl_context_t *ctx);
int gl_set_pkg_auto(gl_context_t *ctx, const char *repo, const char *name, bool automatic);
int gl_set_pkg_src(gl_context_t *ctx, const char *repo, const char *name, const char *src_repo);
int gl_pkg_src(gl_context_t *ctx, const char *repo, const char *name, char *out, size_t len);
/* --- deps.h (built into libglacier_istoreutils.so) --- */
int gl_check_plan(const gl_context_t *live,
const char *const *add_paths, size_t n_add,
const char *const *remove_ids, size_t n_remove);
int gl_check_txn(const gl_context_t *stage);
int gl_backup_istore(const char *tar_path, const char *glacier_root, const char *uid);
/* --- pkg.h: parsed manifests (arrays end at a NULL name) --- */
typedef struct {
int maj;
int min;
int pat;
char *tag;
} semver_t;
typedef struct {
char *dep_name;
semver_t dep_ver;
} dep_t;
typedef struct {
char *conf_name;
semver_t conf_ver;
} conf_t;
typedef struct {
char *pkg_name;
semver_t pkg_ver;
char *pkg_desc;
char *pkg_maint;
char *pkg_copyright;
char *pkg_repo;
dep_t *pkg_dependencies;
conf_t *pkg_conflicts;
char *pkg_traits;
} gl_pkg_t;
/* libglacier_istoreutils.so; *pkg must be zeroed, free with gl_free_pkg */
int gl_read_pkg_manifest(const char *gpkg_path, gl_pkg_t *pkg);
int gl_gpm2gpkg(const char *filename, gl_pkg_t *pkg);
void gl_free_pkg(gl_pkg_t *pkg);
/* --- libc: process control, used by the crash/contention tests --- */
pid_t fork(void);
pid_t waitpid(pid_t pid, int *status, int options);
int kill(pid_t pid, int sig);
void _exit(int status);
unsigned int sleep(unsigned int seconds);
/* --- libc: mount, used by the EXDEV test --- */
int mount(const char *source, const char *target, const char *filesystemtype,
unsigned long mountflags, const void *data);
int umount2(const char *target, int flags);
/* --- sha256.h (libglacier_verify.so, private) --- */
typedef struct {
uint32_t state[8];
uint64_t bitlen;
uint8_t data[64];
size_t datalen;
} gl_sha256_ctx_t;
void gl_sha256_init(gl_sha256_ctx_t *ctx);
void gl_sha256_update(gl_sha256_ctx_t *ctx, const uint8_t *data, size_t len);
void gl_sha256_final(gl_sha256_ctx_t *ctx, uint8_t digest[32]);
void gl_sha256_to_hex(const uint8_t digest[32], char *out);
/* --- sign.h (libglacier_verify.so, private) --- */
typedef enum {
GL_SIGN_OK = 0,
GL_SIGN_ERR_IO = 1,
GL_SIGN_ERR_FORMAT = 2,
GL_SIGN_ERR_ENCRYPTED = 3,
GL_SIGN_ERR_KEYNUM = 4,
GL_SIGN_ERR_BAD = 5,
GL_SIGN_ERR_TOOBIG = 6,
GL_SIGN_ERR_RANDOM = 7,
GL_SIGN_ERR_EXISTS = 8,
GL_SIGN_ERR_WRITE = 9,
} gl_sign_status_t;
typedef struct {
uint8_t keynum[8];
uint8_t key[32];
} gl_sign_pubkey_t;
typedef struct {
uint8_t keynum[8];
uint8_t sig[64];
} gl_sign_sig_t;
int gl_sign_read_pubkey(const char *path, gl_sign_pubkey_t *out);
int gl_sign_check_seckey(const char *path);
int gl_sign_read_sig(const char *path, gl_sign_sig_t *out);
int gl_sign_verify(const gl_sign_pubkey_t *pub, const gl_sign_sig_t *sig,
const uint8_t *msg, size_t len);
int gl_sign_verify_file(const char *pubkey_path, const char *msg_path,
const char *sig_path);
int gl_sign_file(const char *seckey_path, const char *msg_path,
const char *sig_path);
int gl_sign_generate(const char *pubkey_path, const char *seckey_path,
const char *comment);
]]
local M = { ffi = ffi }
M.SCOPE_USR = 0
M.SCOPE_SYS = 1
local TXN_NAMES = {
[0] = "GL_TXN_OK", [1] = "GL_TXN_ERR_LOCK", [2] = "GL_TXN_ERR_MKDIR",
[3] = "GL_TXN_ERR_PERM", [4] = "GL_TXN_ERR_RENAME", [5] = "GL_TXN_ERR_SYNC",
[6] = "GL_TXN_ERR_CLEANUP", [7] = "GL_TXN_ERR_NO_TXN", [8] = "GL_TXN_ERR_XDEV",
[9] = "GL_TXN_ERR_DEPS",
}
local INSTALL_NAMES = {
[0] = "GL_INSTALL_OK", [1] = "GL_INSTALL_ERR_OPEN", [2] = "GL_INSTALL_ERR_MANIFEST",
[3] = "GL_INSTALL_ERR_EXTRACT", [4] = "GL_INSTALL_ERR_INDEX",
[5] = "GL_INSTALL_ERR_ALREADY_INSTALLED", [6] = "GL_INSTALL_SKIPPED",
[7] = "GL_INSTALL_ERR_SCOPE", [8] = "GL_INSTALL_ERR_DEPS",
}
local REMOVE_NAMES = {
[0] = "GL_REMOVE_OK", [1] = "GL_REMOVE_ERR_NOT_FOUND",
[2] = "GL_REMOVE_ERR_UNLINK", [3] = "GL_REMOVE_ERR_INDEX",
[4] = "GL_REMOVE_ERR_DEPS",
}
local SIGN_NAMES = {
[0] = "GL_SIGN_OK", [1] = "GL_SIGN_ERR_IO", [2] = "GL_SIGN_ERR_FORMAT",
[3] = "GL_SIGN_ERR_ENCRYPTED", [4] = "GL_SIGN_ERR_KEYNUM", [5] = "GL_SIGN_ERR_BAD",
[6] = "GL_SIGN_ERR_TOOBIG", [7] = "GL_SIGN_ERR_RANDOM", [8] = "GL_SIGN_ERR_EXISTS",
[9] = "GL_SIGN_ERR_WRITE",
}
function M.txn_name(v) return TXN_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
function M.install_name(v) return INSTALL_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
function M.sign_name(v) return SIGN_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
function M.remove_name(v) return REMOVE_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
function M.new_context()
return ffi.new("gl_context_t")
end
-- Human-readable current errno, for reporting why a libc call (mount,
-- etc.) failed rather than just that it did. Must be called immediately
-- after the failing call, before any other libc-touching code runs.
function M.strerror()
local err = ffi.errno()
return err .. " (" .. ffi.string(ffi.C.strerror(err)) .. ")"
end
-- This module's own directory on disk, independent of CWD and
-- independent of how deeply nested the require()ing script is. Lua's
-- debug.getinfo(1, "S").source gives the path this exact file was
-- loaded from, prefixed with "@" for a real file — not the path of
-- whoever called into it.
local function this_dir()
local src = debug.getinfo(1, "S").source
if src:sub(1, 1) == "@" then src = src:sub(2) end
return src:match("(.*/)") or "./"
end
-- Loads the combined library. Resolution order:
-- 1. an explicit path argument
-- 2. LIBGLACIER_SO env var
-- 3. glacier/libglacier-ng.so relative to *this file's own location*
-- (i.e. <project_root>/lib/glacier/libglacier-ng.so), not CWD —
-- this is what makes M.load() work correctly no matter where the
-- calling script was invoked from.
function M.load(path)
local target = path or os.getenv("LIBGLACIER_SO")
or (this_dir() .. "glacier/libglacier-ng.so")
local ok, lib = pcall(ffi.load, target)
if not ok then
io.stderr:write(string.format(
"glacier_cdef: could not load %s (%s)\n" ..
"Set LIBGLACIER_SO to the full path of the built .so if it isn't at the default location.\n",
target, tostring(lib)))
os.exit(1)
end
return lib
end
return M