Artifact a78413d26e662ecffba2227c1998e1651502369d6d9dd9a081d117ab36e34b59:
- File
src/userdb.c
— part of check-in
[eae917e9e9]
at
2026-11-21 12:03:36
on branch shared-store
— Shared journal store: several pic processes on one workspace, one conversation each
Each conversation has a lease (an exclusive flock beside the journal) that its process holds while it advances it and that the kernel drops when the process and its forked children are gone. Taking a lease claims the conversation's request and settles what a dead owner left; open recovers only conversations whose lease is free. The journal lock is held shared and taken exclusive only to create or migrate the schema. SQLite runs in normal locking mode with the fair busy handler; dev builds stop on a write begun under an open read.
pic run takes only requests nobody is running. A second pic asking into a conversation in use is refused with its owner's pid. An edit that misses in a file another conversation changed says so and shows the file; a write to such a file is refused once.
New test/pic/shared.sh: two-process crash matrix, kill -9 beside a live pic, claim and admission races, stale-file results. No schema change. (user: dmestas, size: 25430) [annotate] [blame] [check-ins using] [more...]
/* pic's user database: ~/.pic/pic.sqlite. Tables are prefixed picu_ (the * journal's are pic_, libfossil's fsl*) so one file could hold them all. */ #include <errno.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #include "platform.h" #include "sqlar.h" #include "userdb.h" #define USERDB_VERSION "1" static sqlite3 *conn; static pid_t conn_pid; static const char schema[] = "CREATE TABLE IF NOT EXISTS picu_meta(key TEXT PRIMARY KEY, value TEXT NOT NULL);" "CREATE TABLE IF NOT EXISTS picu_setting(key TEXT PRIMARY KEY, value TEXT NOT NULL, updated_ms INTEGER NOT NULL);" /* Pi's model catalog per provider from pi.dev; models: JSON array, sqlar rule. */ "CREATE TABLE IF NOT EXISTS picu_catalog(provider TEXT PRIMARY KEY, sz INTEGER NOT NULL, models BLOB NOT NULL," " checked_ms INTEGER NOT NULL, last_modified_ms INTEGER NOT NULL, etag TEXT);" "CREATE TABLE IF NOT EXISTS picu_trust(path TEXT PRIMARY KEY, trusted INTEGER NOT NULL CHECK(trusted IN (0,1))," " decided_ms INTEGER NOT NULL);" "CREATE TABLE IF NOT EXISTS picu_scoped_model(position INTEGER PRIMARY KEY, pattern TEXT NOT NULL);" "CREATE TABLE IF NOT EXISTS picu_bug(id INTEGER PRIMARY KEY, created_ms INTEGER NOT NULL, workspace TEXT NOT NULL," " conversation TEXT, model TEXT, description TEXT NOT NULL);" "CREATE TABLE IF NOT EXISTS picu_workspace(path TEXT PRIMARY KEY, journal TEXT NOT NULL, last_used_ms INTEGER NOT NULL);" /* Pi v3 session lines of pic's conversations, for ctx: append-only per * conversation; line is the JSON line, deflated when smaller (sqlar rule). * kind is the line's Pi type, entry the journal entry it came from. */ "CREATE TABLE IF NOT EXISTS picu_pi_session_line(conversation TEXT NOT NULL, seq INTEGER NOT NULL," " workspace TEXT NOT NULL, kind TEXT NOT NULL, entry INTEGER, sz INTEGER NOT NULL, line BLOB NOT NULL," " PRIMARY KEY(conversation, seq));" /* A project held in this file: its repository is libfossil's tables * PREFIX* (fsl_repo_open_host). remote never carries userinfo (the * CHECK refuses an @ before the path), and the password is never kept: * cred names where it comes from (keychain or environment). */ "CREATE TABLE IF NOT EXISTS picu_project(name TEXT PRIMARY KEY, prefix TEXT NOT NULL UNIQUE," " remote TEXT NOT NULL CHECK(instr(substr(substr(remote, instr(remote, '://') + 3), 1," " instr(substr(remote, instr(remote, '://') + 3) || '/', '/') - 1), '@') = 0)," " cred TEXT, kind TEXT NOT NULL CHECK(kind IN ('fossil', 'zit'))," " state TEXT NOT NULL CHECK(state IN ('cloning', 'ready')), created_ms INTEGER NOT NULL);" /* A process pic started (procs.c): kind, the command (truncated), the * conversation and workspace that started it, pid and process group, the * OS's start token for pid (so a reused pid is not mistaken for it), the * pic process that recorded it (owner_pid), and, once it ends, when and * how (exit_code, signal; NULL when not known). ended_ms NULL: running. */ "CREATE TABLE IF NOT EXISTS picu_proc(id INTEGER PRIMARY KEY, kind TEXT NOT NULL, command TEXT NOT NULL," " workspace TEXT, conversation TEXT, pid INTEGER NOT NULL, pgid INTEGER NOT NULL, start_token INTEGER NOT NULL," " owner_pid INTEGER NOT NULL, started_ms INTEGER NOT NULL, ended_ms INTEGER, exit_code INTEGER, signal INTEGER);" /* The credential store (cred.c). picu_credential_key: the one master * key's parameters, never the key: where it comes from (source), how a * passphrase is stretched (kdf, iterations, salt), and a verifier (a * known text sealed under it) that tells a wrong passphrase from a right * one. picu_credential: a provider's key, sealed (nonce, ciphertext and * Poly1305 tag; the name is bound as AAD), so plaintext never reaches * this file, its WAL or a backup. */ "CREATE TABLE IF NOT EXISTS picu_credential_key(id INTEGER PRIMARY KEY CHECK(id = 1), source TEXT NOT NULL," " kdf TEXT NOT NULL, iterations INTEGER NOT NULL, salt BLOB NOT NULL, verifier BLOB NOT NULL," " created_ms INTEGER NOT NULL);" "CREATE TABLE IF NOT EXISTS picu_credential(name TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK(kind IN ('api_key'))," " sealed BLOB NOT NULL, created_ms INTEGER NOT NULL, used_ms INTEGER);" "INSERT OR IGNORE INTO picu_meta(key, value) VALUES('schema_version', '" USERDB_VERSION "');"; static int home_path(const char *name, char *out, size_t cap) { const char *home = getenv("HOME"); return home && *home && snprintf(out, cap, "%s/.pic/%s", home, name) < (int)cap ? 0 : -1; } /* A whole small file (malloc'd), or NULL. */ static char *slurp(const char *path) { FILE *f = fopen(path, "rb"); char *buf = NULL; long n; if (!f) { return NULL; } if (fseek(f, 0, SEEK_END) == 0 && (n = ftell(f)) >= 0 && n < (64L << 20) && fseek(f, 0, SEEK_SET) == 0 && (buf = malloc((size_t)n + 1))) { if (fread(buf, 1, (size_t)n, f) == (size_t)n) { buf[n] = '\0'; } else { free(buf); buf = NULL; } } fclose(f); return buf; } static long long now_ms(void) { return (long long)(pic_platform_get()->wall_ns() / 1000000); } static int run(sqlite3 *db, const char *sql, const char *text) { sqlite3_stmt *q = NULL; int rc; if (sqlite3_prepare_v2(db, sql, -1, &q, NULL) != SQLITE_OK) { return -1; } if (text) { sqlite3_bind_text(q, 1, text, -1, SQLITE_STATIC); } sqlite3_bind_int64(q, sqlite3_bind_parameter_count(q), now_ms()); rc = sqlite3_step(q); sqlite3_finalize(q); return rc == SQLITE_DONE || rc == SQLITE_ROW ? 0 : -1; } static int valid_json(sqlite3 *db, const char *text) { sqlite3_stmt *q = NULL; int valid = 0; if (sqlite3_prepare_v2(db, "SELECT json_valid(?1)", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, text, -1, SQLITE_STATIC); valid = sqlite3_step(q) == SQLITE_ROW && sqlite3_column_int(q, 0); sqlite3_finalize(q); } return valid; } /* The legacy JSON files, imported in one transaction, then deleted. A file * that is not valid JSON is left in place. */ static int migrate(sqlite3 *db) { char spath[PATH_MAX], mpath[PATH_MAX]; char *settings = home_path("settings.json", spath, sizeof spath) == 0 ? slurp(spath) : NULL; char *store = home_path("models-store.json", mpath, sizeof mpath) == 0 ? slurp(mpath) : NULL; int rc = 0; if (!settings && !store) { return 0; } if (sqlite3_exec(db, "BEGIN IMMEDIATE", NULL, NULL, NULL) != SQLITE_OK) { free(settings); free(store); return -1; } if (settings) { rc |= run(db, "INSERT OR IGNORE INTO picu_setting(key, value, updated_ms)" " SELECT key, CAST(value AS TEXT), ?2 FROM json_each(?1) WHERE json_valid(?1) AND value IS NOT NULL", settings); } if (store) { rc |= run(db, "INSERT OR IGNORE INTO picu_catalog(provider, sz, models, checked_ms, last_modified_ms, etag)" " SELECT key, length(CAST(json_extract(value, '$.models') AS BLOB))," " sqlar_compress(CAST(json_extract(value, '$.models') AS BLOB))," " coalesce(json_extract(value, '$.checkedAt'), 0), coalesce(json_extract(value, '$.lastModified'), 0)," " json_extract(value, '$.etag')" " FROM json_each(?1) WHERE json_valid(?1) AND json_type(value, '$.models') = 'array' AND ?2 IS NOT NULL", store); } if (rc != 0 || sqlite3_exec(db, "COMMIT", NULL, NULL, NULL) != SQLITE_OK) { (void)sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL); free(settings); free(store); return -1; } /* Committed: the files are no longer the record. Invalid JSON stays put. */ if (settings && valid_json(db, settings)) { (void)unlink(spath); } if (store && valid_json(db, store)) { (void)unlink(mpath); } free(settings); free(store); return 0; } int pic_userdb_path(char *out, size_t cap) { return home_path("pic.sqlite", out, cap); } /* SQLite's own busy handler backs off up to 100 ms per sleep, so a waiter * that started late can win the lock first. This one polls every 1-2 ms * (fixed plus jitter, so waiters do not move in step) until a 5 s deadline * from the first refusal. */ typedef struct { uint64_t start_ns; uint64_t rng; uint64_t limit_ms; } busy_state; static int fair_busy(void *ctx, int count) { busy_state *b = ctx; uint64_t now = pic_platform_get()->mono_ns(); sqlite3_vfs *vfs = sqlite3_vfs_find(pic_platform_get()->vfs); if (count == 0) { b->start_ns = now; } if (now - b->start_ns >= b->limit_ms * 1000000ull || !vfs) { return 0; } b->rng ^= b->rng << 13; /* xorshift64 */ b->rng ^= b->rng >> 7; b->rng ^= b->rng << 17; (void)vfs->xSleep(vfs, 1000 + (int)(b->rng % 1000)); return 1; } int pic_busy_wait(sqlite3 *db, long ms) { busy_state *b = sqlite3_malloc(sizeof *b); if (!b) { return -1; } b->start_ns = 0; b->limit_ms = (uint64_t)ms; if (pic_platform_get()->entropy(&b->rng, sizeof b->rng) != 0 || b->rng == 0) { b->rng = pic_platform_get()->mono_ns() | 1; } /* The handler replaces sqlite3_busy_timeout (one or the other); the * client data frees its state with the connection. */ if (sqlite3_set_clientdata(db, "pic_busy", b, sqlite3_free) != SQLITE_OK) { sqlite3_free(b); return -1; } return sqlite3_busy_handler(db, fair_busy, b) == SQLITE_OK ? 0 : -1; } int pic_userdb_settings(sqlite3 *db) { if (pic_busy_wait(db, PIC_USERDB_BUSY_MS) != 0) { return -1; } return sqlite3_exec(db, "PRAGMA synchronous=FULL; PRAGMA foreign_keys=OFF;" #ifdef __APPLE__ " PRAGMA fullfsync=ON;" #endif , NULL, NULL, NULL) == SQLITE_OK ? 0 : -1; } sqlite3 *pic_userdb(char *err, size_t errcap) { char path[PATH_MAX], dir[PATH_MAX]; const char *home = getenv("HOME"); sqlite3 *db = NULL; if (conn && conn_pid == getpid()) { return conn; } /* A connection inherited across fork is not this process's to use or * close (its locks and WAL state belong to the parent): left alone. */ conn = NULL; if (!home || !*home || snprintf(dir, sizeof dir, "%s/.pic", home) >= (int)sizeof dir || home_path("pic.sqlite", path, sizeof path) != 0) { if (err) { (void)snprintf(err, errcap, "no home directory for ~/.pic/pic.sqlite"); } return NULL; } if (mkdir(dir, 0700) != 0 && errno != EEXIST) { if (err) { (void)snprintf(err, errcap, "cannot create %s: %s", dir, strerror(errno)); } return NULL; } if (sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, pic_platform_get()->vfs) != SQLITE_OK || pic_userdb_settings(db) != 0 || sqlite3_exec(db, "PRAGMA journal_mode=WAL", NULL, NULL, NULL) != SQLITE_OK || pic_sqlar_register(db) != 0 || sqlite3_exec(db, schema, NULL, NULL, NULL) != SQLITE_OK || migrate(db) != 0) { if (err) { (void)snprintf(err, errcap, "%s: %s", path, db ? sqlite3_errmsg(db) : "cannot open"); } sqlite3_close(db); return NULL; } (void)chmod(path, 0600); conn = db; conn_pid = getpid(); return conn; } void pic_userdb_close(void) { if (conn && conn_pid == getpid()) { sqlite3_close(conn); } conn = NULL; } char *pic_setting(const char *key) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; char *out = NULL; if (db && sqlite3_prepare_v2(db, "SELECT value FROM picu_setting WHERE key = ?1", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, key, -1, SQLITE_STATIC); if (sqlite3_step(q) == SQLITE_ROW) { out = strdup((const char *)sqlite3_column_text(q, 0)); } } sqlite3_finalize(q); return out; } int pic_setting_set(const char *key, const char *value) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; int rc = -1; if (db && sqlite3_prepare_v2(db, value ? "INSERT INTO picu_setting(key, value, updated_ms) VALUES(?1, ?2, ?3)" " ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_ms = excluded.updated_ms" : "DELETE FROM picu_setting WHERE key = ?1", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, key, -1, SQLITE_STATIC); if (value) { sqlite3_bind_text(q, 2, value, -1, SQLITE_STATIC); sqlite3_bind_int64(q, 3, now_ms()); } rc = sqlite3_step(q) == SQLITE_DONE ? 0 : -1; } sqlite3_finalize(q); return rc; } int pic_setting_set_json(const char *key, const char *path, const char *json) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; int rc = -1; if (db && sqlite3_prepare_v2(db, "INSERT INTO picu_setting(key, value, updated_ms) VALUES(?1, json_set('{}', ?2, json(?3)), ?4)" " ON CONFLICT(key) DO UPDATE SET value = json_set(CASE WHEN json_valid(value)" " AND json_type(value) = 'object' THEN value ELSE '{}' END, ?2, json(?3))," " updated_ms = excluded.updated_ms", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, key, -1, SQLITE_STATIC); sqlite3_bind_text(q, 2, path, -1, SQLITE_STATIC); sqlite3_bind_text(q, 3, json, -1, SQLITE_STATIC); sqlite3_bind_int64(q, 4, now_ms()); rc = sqlite3_step(q) == SQLITE_DONE ? 0 : -1; } sqlite3_finalize(q); return rc; } int pic_settings_each(int (*fn)(void *ctx, const char *key, const char *value), void *ctx) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; if (!db || sqlite3_prepare_v2(db, "SELECT key, value FROM picu_setting ORDER BY key", -1, &q, NULL) != SQLITE_OK) { return -1; } while (sqlite3_step(q) == SQLITE_ROW && !fn(ctx, (const char *)sqlite3_column_text(q, 0), (const char *)sqlite3_column_text(q, 1))) { } sqlite3_finalize(q); return 0; } int pic_workspace_touch(const char *workspace, const char *journal) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; int rc = -1; if (db && sqlite3_prepare_v2(db, "INSERT INTO picu_workspace(path, journal, last_used_ms) VALUES(?1, ?2, ?3)" " ON CONFLICT(path) DO UPDATE SET journal = excluded.journal, last_used_ms = excluded.last_used_ms", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, workspace, -1, SQLITE_STATIC); sqlite3_bind_text(q, 2, journal, -1, SQLITE_STATIC); sqlite3_bind_int64(q, 3, now_ms()); rc = sqlite3_step(q) == SQLITE_DONE ? 0 : -1; } sqlite3_finalize(q); return rc; } int pic_scoped_set(const char *const *patterns, size_t n) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; int rc = -1; if (!db || sqlite3_exec(db, "BEGIN IMMEDIATE", NULL, NULL, NULL) != SQLITE_OK) { return -1; } if (sqlite3_exec(db, "DELETE FROM picu_scoped_model", NULL, NULL, NULL) == SQLITE_OK && sqlite3_prepare_v2(db, "INSERT INTO picu_scoped_model(position, pattern) VALUES(?1, ?2)", -1, &q, NULL) == SQLITE_OK) { rc = 0; for (size_t i = 0; i < n && rc == 0; i++) { sqlite3_bind_int64(q, 1, (sqlite3_int64)i); sqlite3_bind_text(q, 2, patterns[i], -1, SQLITE_STATIC); rc = sqlite3_step(q) == SQLITE_DONE ? 0 : -1; sqlite3_reset(q); } } sqlite3_finalize(q); if (rc != 0 || sqlite3_exec(db, "COMMIT", NULL, NULL, NULL) != SQLITE_OK) { (void)sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL); return -1; } return 0; } int pic_scoped_each(int (*fn)(void *ctx, const char *pattern), void *ctx) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; if (!db || sqlite3_prepare_v2(db, "SELECT pattern FROM picu_scoped_model ORDER BY position", -1, &q, NULL) != SQLITE_OK) { return -1; } while (sqlite3_step(q) == SQLITE_ROW && !fn(ctx, (const char *)sqlite3_column_text(q, 0))) { } sqlite3_finalize(q); return 0; } int pic_trust_get(const char *path) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; int out = -1; if (db && sqlite3_prepare_v2(db, "SELECT trusted FROM picu_trust WHERE path = ?1", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, path, -1, SQLITE_STATIC); if (sqlite3_step(q) == SQLITE_ROW) { out = sqlite3_column_int(q, 0); } } sqlite3_finalize(q); return out; } int pic_trust_set(const char *path, int trusted) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; int rc = -1; if (db && sqlite3_prepare_v2(db, "INSERT INTO picu_trust(path, trusted, decided_ms) VALUES(?1, ?2, ?3)" " ON CONFLICT(path) DO UPDATE SET trusted = excluded.trusted, decided_ms = excluded.decided_ms", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, path, -1, SQLITE_STATIC); sqlite3_bind_int(q, 2, trusted ? 1 : 0); sqlite3_bind_int64(q, 3, now_ms()); rc = sqlite3_step(q) == SQLITE_DONE ? 0 : -1; } sqlite3_finalize(q); return rc; } long long pic_bug_add(const char *workspace, const char *conversation, const char *model, const char *description) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; long long id = -1; if (db && sqlite3_prepare_v2(db, "INSERT INTO picu_bug(created_ms, workspace, conversation, model, description)" " VALUES(?1, ?2, ?3, ?4, ?5)", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_int64(q, 1, now_ms()); sqlite3_bind_text(q, 2, workspace, -1, SQLITE_STATIC); if (conversation && *conversation) { sqlite3_bind_text(q, 3, conversation, -1, SQLITE_STATIC); } if (model && *model) { sqlite3_bind_text(q, 4, model, -1, SQLITE_STATIC); } sqlite3_bind_text(q, 5, description, -1, SQLITE_STATIC); if (sqlite3_step(q) == SQLITE_DONE) { id = sqlite3_last_insert_rowid(db); } } sqlite3_finalize(q); return id; } int pic_bugs_each(int (*fn)(void *ctx, long long id, long long ms, const char *workspace, const char *description), void *ctx) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; if (!db || sqlite3_prepare_v2(db, "SELECT id, created_ms, workspace, description FROM picu_bug ORDER BY id", -1, &q, NULL) != SQLITE_OK) { return -1; } while (sqlite3_step(q) == SQLITE_ROW && !fn(ctx, sqlite3_column_int64(q, 0), sqlite3_column_int64(q, 1), (const char *)sqlite3_column_text(q, 2), (const char *)sqlite3_column_text(q, 3))) { } sqlite3_finalize(q); return 0; } int pic_projects_each(int (*fn)(void *ctx, const pic_project *p), void *ctx) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; if (!db || sqlite3_prepare_v2(db, "SELECT name, prefix, remote, cred, kind, state, created_ms FROM picu_project ORDER BY name", -1, &q, NULL) != SQLITE_OK) { return -1; } for (;;) { pic_project p; int rc = sqlite3_step(q); if (rc != SQLITE_ROW) { sqlite3_finalize(q); return rc == SQLITE_DONE ? 0 : -1; } p.name = (const char *)sqlite3_column_text(q, 0); p.prefix = (const char *)sqlite3_column_text(q, 1); p.remote = (const char *)sqlite3_column_text(q, 2); p.cred = (const char *)sqlite3_column_text(q, 3); p.kind = (const char *)sqlite3_column_text(q, 4); p.state = (const char *)sqlite3_column_text(q, 5); p.created_ms = sqlite3_column_int64(q, 6); if (fn(ctx, &p)) { sqlite3_finalize(q); return 0; } } } /* ---- picu_proc: the processes pic started ---------------------------------- */ /* One short write that does not wait for the disk: these rows are a * convenience, and a full fsync per command start would cost more than the * command's bookkeeping is worth. The connection's setting is put back. */ static int proc_write(sqlite3 *db, sqlite3_stmt *q) { int rc; (void)sqlite3_exec(db, "PRAGMA synchronous=OFF", NULL, NULL, NULL); rc = sqlite3_step(q); (void)sqlite3_exec(db, "PRAGMA synchronous=FULL", NULL, NULL, NULL); return rc == SQLITE_DONE ? 0 : -1; } static void bind_opt(sqlite3_stmt *q, int i, const char *text) { if (text && *text) { sqlite3_bind_text(q, i, text, -1, SQLITE_STATIC); } } long long pic_proc_add(const pic_proc *p) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; long long id = -1; if (db && sqlite3_prepare_v2(db, "INSERT INTO picu_proc(kind, command, workspace, conversation, pid, pgid, start_token," " owner_pid, started_ms) VALUES(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, p->kind, -1, SQLITE_STATIC); sqlite3_bind_text(q, 2, p->command, -1, SQLITE_STATIC); bind_opt(q, 3, p->workspace); bind_opt(q, 4, p->conversation); sqlite3_bind_int64(q, 5, p->pid); sqlite3_bind_int64(q, 6, p->pgid); sqlite3_bind_int64(q, 7, p->start_token); sqlite3_bind_int64(q, 8, p->owner_pid); sqlite3_bind_int64(q, 9, p->started_ms ? p->started_ms : now_ms()); if (proc_write(db, q) == 0) { id = sqlite3_last_insert_rowid(db); } } sqlite3_finalize(q); return id; } int pic_proc_finish(long long id, int exit_code, int sig) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; int rc = -1; if (db && sqlite3_prepare_v2(db, "UPDATE picu_proc SET ended_ms = ?2, exit_code = ?3, signal = ?4" " WHERE id = ?1 AND ended_ms IS NULL", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_int64(q, 1, id); sqlite3_bind_int64(q, 2, now_ms()); if (exit_code >= 0) { sqlite3_bind_int(q, 3, exit_code); } if (sig > 0) { sqlite3_bind_int(q, 4, sig); } rc = proc_write(db, q); } sqlite3_finalize(q); return rc; } int pic_procs_each(int all, int (*fn)(void *ctx, const pic_proc *p), void *ctx) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; if (!db || sqlite3_prepare_v2(db, "SELECT id, kind, command, workspace, conversation, pid, pgid, start_token, owner_pid," " started_ms, ended_ms, exit_code, signal FROM picu_proc" " WHERE ?1 OR ended_ms IS NULL ORDER BY id", -1, &q, NULL) != SQLITE_OK) { return -1; } sqlite3_bind_int(q, 1, all ? 1 : 0); for (;;) { pic_proc p; int rc = sqlite3_step(q); if (rc != SQLITE_ROW) { sqlite3_finalize(q); return rc == SQLITE_DONE ? 0 : -1; } p.id = sqlite3_column_int64(q, 0); p.kind = (const char *)sqlite3_column_text(q, 1); p.command = (const char *)sqlite3_column_text(q, 2); p.workspace = (const char *)sqlite3_column_text(q, 3); p.conversation = (const char *)sqlite3_column_text(q, 4); p.pid = sqlite3_column_int64(q, 5); p.pgid = sqlite3_column_int64(q, 6); p.start_token = sqlite3_column_int64(q, 7); p.owner_pid = sqlite3_column_int64(q, 8); p.started_ms = sqlite3_column_int64(q, 9); p.ended_ms = sqlite3_column_int64(q, 10); p.exit_code = sqlite3_column_type(q, 11) == SQLITE_NULL ? -1 : sqlite3_column_int(q, 11); p.signal = sqlite3_column_int(q, 12); if (fn(ctx, &p)) { sqlite3_finalize(q); return 0; } } } int pic_procs_prune(long long before_ms) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; int rc = -1; if (db && sqlite3_prepare_v2(db, "DELETE FROM picu_proc WHERE coalesce(ended_ms, started_ms) < ?1", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_int64(q, 1, before_ms); rc = proc_write(db, q); } sqlite3_finalize(q); return rc; }