/* Core's own RPC commands for a client (rpc.h): the user's settings, trust, * Ctrl+P's scope, bug reports, paired phones, whether hooks are configured * and process records. The terminal interface reaches core state only * through these and the other registered commands. */ #include #include #include #include #include "hooks.h" #include "procs.h" #include "rpc.h" #include "userdb.h" static int rc_done(pic_rpc_call *c, sqlite3_str *t) { char *res = sqlite3_str_finish(t); c->data = res ? strdup(res) : NULL; sqlite3_free(res); return c->data ? 0 : -1; } static int rc_need(pic_rpc_call *c, const char *value, const char *field) { if (!value) { (void)snprintf(c->err, sizeof c->err, "the command needs %s", field); return -1; } return 0; } /* get_setting key: {"value": the setting's text, or null when unset}. */ static int rc_get_setting(pic_rpc_call *c) { char *key = pic_rpc_text(c, "$.key"), *v; sqlite3_str *t; if (rc_need(c, key, "key") != 0) { return -1; } v = pic_setting(key); t = sqlite3_str_new(NULL); sqlite3_str_appendall(t, "{\"value\":"); if (v) { pic_rpc_quote(t, v); } else { sqlite3_str_appendall(t, "null"); } sqlite3_str_appendall(t, "}"); free(key); free(v); return rc_done(c, t); } /* set_setting key value (null removes it), or key path json: one field of a * JSON setting, as Pi writes compaction.enabled. */ static int rc_set_setting(pic_rpc_call *c) { char *key = pic_rpc_text(c, "$.key"), *value = pic_rpc_text(c, "$.value"), *path = pic_rpc_text(c, "$.path"), *json = pic_rpc_text(c, "$.json"); int rc = -1; if (rc_need(c, key, "key") == 0) { rc = path ? (json ? pic_setting_set_json(key, path, json) : -1) : pic_setting_set(key, value); if (rc != 0) { (void)snprintf(c->err, sizeof c->err, "could not save the setting %.200s", key); } } free(key); free(value); free(path); free(json); return rc; } /* get_trust path: {"trusted": 1, 0, or -1 undecided}. */ static int rc_get_trust(pic_rpc_call *c) { char *path = pic_rpc_text(c, "$.path"); sqlite3_str *t; if (rc_need(c, path, "path") != 0) { return -1; } t = sqlite3_str_new(NULL); sqlite3_str_appendf(t, "{\"trusted\":%d}", pic_trust_get(path)); free(path); return rc_done(c, t); } /* set_trust path trusted. */ static int rc_set_trust(pic_rpc_call *c) { char *path = pic_rpc_text(c, "$.path"), *trusted = pic_rpc_text(c, "$.trusted"); int rc = -1; if (rc_need(c, path, "path") == 0 && rc_need(c, trusted, "trusted") == 0) { rc = pic_trust_set(path, strcmp(trusted, "1") == 0 || strcmp(trusted, "true") == 0); if (rc != 0) { (void)snprintf(c->err, sizeof c->err, "could not save the trust decision"); } } free(path); free(trusted); return rc; } static int rc_scoped_row(void *ctx, const char *pattern) { sqlite3_str *t = ctx; if (sqlite3_str_length(t) > 13) { sqlite3_str_appendall(t, ","); } pic_rpc_quote(t, pattern); return 0; } /* get_scoped_models: {"patterns": Ctrl+P's scope, in cycling order}. */ static int rc_get_scoped(pic_rpc_call *c) { sqlite3_str *t = sqlite3_str_new(NULL); sqlite3_str_appendall(t, "{\"patterns\":["); (void)pic_scoped_each(rc_scoped_row, t); sqlite3_str_appendall(t, "]}"); return rc_done(c, t); } /* set_scoped_models patterns: replaces the scope ([] clears it). */ static int rc_set_scoped(pic_rpc_call *c) { const char **v = NULL; size_t k = 0; int rc = -1; { sqlite3 *db = NULL; sqlite3_stmt *q = NULL; if (sqlite3_open(":memory:", &db) == SQLITE_OK && sqlite3_prepare_v2(db, "SELECT value FROM json_each(?1, '$.patterns') WHERE json_valid(?1) AND type = 'text'", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, c->line, -1, SQLITE_STATIC); while (sqlite3_step(q) == SQLITE_ROW) { const char **nv = realloc(v, (k + 1) * sizeof *v); if (!nv) { break; } v = nv; v[k++] = strdup((const char *)sqlite3_column_text(q, 0)); } rc = pic_scoped_set(v, k); } sqlite3_finalize(q); sqlite3_close(db); } for (size_t i = 0; i < k; i++) { free((char *)(uintptr_t)v[i]); } free(v); if (rc != 0) { (void)snprintf(c->err, sizeof c->err, "could not save the scoped models"); } return rc; } /* report_bug description (workspace, session and model optional): {"id": the report's}. Kept * in the user database; nothing is sent anywhere. */ static int rc_report_bug(pic_rpc_call *c) { char *text = pic_rpc_text(c, "$.description"), *ws = pic_rpc_text(c, "$.workspace"), *sid = pic_rpc_text(c, "$.session"), *model = pic_rpc_text(c, "$.model"); long long id = -1; sqlite3_str *t = NULL; if (rc_need(c, text, "description") == 0) { id = pic_bug_add(ws ? ws : c->workspace, sid ? sid : c->session, model ? model : c->model, text); if (id < 0) { (void)snprintf(c->err, sizeof c->err, "could not keep the report"); } else { t = sqlite3_str_new(NULL); sqlite3_str_appendf(t, "{\"id\":%lld}", id); } } free(text); free(ws); free(sid); free(model); return t ? rc_done(c, t) : -1; } /* get_remote_phones: {"count": the phones paired for remote control}. */ static int rc_get_phones(pic_rpc_call *c) { sqlite3_str *t = sqlite3_str_new(NULL); sqlite3_str_appendf(t, "{\"count\":%d}", pic_remote_phones()); return rc_done(c, t); } /* get_hooks: {"configured": whether any hook is configured for the workspace}. */ static int rc_get_hooks(pic_rpc_call *c) { pic_hooks *h = pic_hooks_load(c->workspace, NULL, NULL); sqlite3_str *t = sqlite3_str_new(NULL); sqlite3_str_appendf(t, "{\"configured\":%s}", h ? "true" : "false"); pic_hooks_free(h); return rc_done(c, t); } /* proc_start kind command pid (pgid, workspace, session optional): records a * process the client started, for pic ps; {"id": its row, -1 when not recorded}. */ static int rc_proc_start(pic_rpc_call *c) { char *kind = pic_rpc_text(c, "$.kind"), *cmd = pic_rpc_text(c, "$.command"), *pid = pic_rpc_text(c, "$.pid"), *pgid = pic_rpc_text(c, "$.pgid"), *ws = pic_rpc_text(c, "$.workspace"), *sid = pic_rpc_text(c, "$.session"); sqlite3_str *t = NULL; if (rc_need(c, kind, "kind") == 0 && rc_need(c, cmd, "command") == 0 && rc_need(c, pid, "pid") == 0) { long p = strtol(pid, NULL, 10); pic_proc_scope(ws ? ws : c->workspace, sid ? sid : c->session); t = sqlite3_str_new(NULL); sqlite3_str_appendf(t, "{\"id\":%lld}", pic_proc_start(kind, cmd, p, pgid ? strtol(pgid, NULL, 10) : p)); } free(kind); free(cmd); free(pid); free(pgid); free(ws); free(sid); return t ? rc_done(c, t) : -1; } /* proc_end id exitCode signal: marks the process ended. */ static int rc_proc_end(pic_rpc_call *c) { char *id = pic_rpc_text(c, "$.id"), *code = pic_rpc_text(c, "$.exitCode"), *sig = pic_rpc_text(c, "$.signal"); int rc = rc_need(c, id, "id"); if (rc == 0) { pic_proc_end(strtoll(id, NULL, 10), code ? (int)strtol(code, NULL, 10) : -1, sig ? (int)strtol(sig, NULL, 10) : 0); } free(id); free(code); free(sig); return rc; } #define CORE (PIC_RPC_JOURNAL_FREE | PIC_RPC_IMMEDIATE) const pic_rpc_command pic_get_setting_rpc = {"get_setting", "one of the user's settings", CORE, rc_get_setting}; const pic_rpc_command pic_set_setting_rpc = {"set_setting", "saves one of the user's settings", CORE, rc_set_setting}; const pic_rpc_command pic_get_trust_rpc = {"get_trust", "whether a folder is trusted", CORE, rc_get_trust}; const pic_rpc_command pic_set_trust_rpc = {"set_trust", "trusts a folder or not", CORE, rc_set_trust}; const pic_rpc_command pic_get_scoped_rpc = {"get_scoped_models", "Ctrl+P's model scope", CORE, rc_get_scoped}; const pic_rpc_command pic_set_scoped_rpc = {"set_scoped_models", "replaces Ctrl+P's model scope", CORE, rc_set_scoped}; const pic_rpc_command pic_report_bug_rpc = {"report_bug", "keeps a bug report", CORE, rc_report_bug}; const pic_rpc_command pic_get_phones_rpc = {"get_remote_phones", "how many phones are paired", CORE, rc_get_phones}; const pic_rpc_command pic_get_hooks_rpc = {"get_hooks", "whether hooks are configured", CORE, rc_get_hooks}; const pic_rpc_command pic_proc_start_rpc = {"proc_start", "records a process the client started", CORE, rc_proc_start}; const pic_rpc_command pic_proc_end_rpc = {"proc_end", "marks a recorded process ended", CORE, rc_proc_end};