Approval was a prompt on stdin: the terminal could answer, a phone could not, and in the TUI the whole path was dead code that failed closed. Now a request is two files: ~/.splinter/ack/pending/<id> the request (tool, args, why, expiry) ~/.splinter/ack/<id>.answer the answer (yes/no plus who) The blocked process waits on the file and nothing else. Channels are notifiers - a command per channel pushes the request out - and whoever reads the reply drops the answer: the gateway, a phone, a script, another terminal. No second poller, no lost Telegram updates, no special case per channel. SPLINTER_ACK_CHANNELS=... comma separated commands, each gets the request SPLINTER_ACK_TTL=120 seconds; a timeout is a no splinter --ack <id> yes|no answer from anywhere splinter --ack-list what is waiting The terminal is one more channel, not a special case: a thread reads stdin and drops the answer, while the main path waits on the file like everybody else. Kid mode uses the same mechanism instead of its own command. Verified: tests/test_ack.c, 7 checks - request, yes, no, who answered, timeout is a no, answering a request that does not exist fails, and the pending list shows the arguments. make check runs it with the other 21.
91 lines
2.5 KiB
C
91 lines
2.5 KiB
C
/*
|
|
* The ack mechanism: a request is a file, an answer is a file, and any process
|
|
* may write it. Silence is not consent.
|
|
*/
|
|
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include "ack.h"
|
|
#include "splinter.h"
|
|
|
|
static int checks, fails;
|
|
|
|
#define CHECK(cond, what) do { \
|
|
checks++; \
|
|
if (!(cond)) { \
|
|
fails++; \
|
|
printf("FAIL %s (line %d)\n", what, __LINE__); \
|
|
} else { \
|
|
printf("ok %s\n", what); \
|
|
} \
|
|
} while (0)
|
|
|
|
static int answer_id;
|
|
static int answer_yes;
|
|
|
|
static void *
|
|
answerer(void *arg)
|
|
{
|
|
(void)arg;
|
|
{ struct timespec ts = { 0, 300000000L }; (void)nanosleep(&ts, NULL); }
|
|
(void)sp_ack_answer(answer_id, answer_yes, "test-phone");
|
|
return NULL;
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
char out[2048], who[64];
|
|
struct sp_buf log;
|
|
int id, yes;
|
|
pthread_t th;
|
|
|
|
(void)setenv("SPLINTER_ACK_DIR", "/var/tmp/splinter_test_ack", 1);
|
|
(void)system("rm -rf /var/tmp/splinter_test_ack");
|
|
|
|
id = sp_ack_request("run", "{\"command\":\"ls\"}", "mutate", 5);
|
|
CHECK(id > 0, "request is written and returns an id");
|
|
|
|
/* the answer comes from another thread, like another process would */
|
|
answer_id = id;
|
|
answer_yes = 1;
|
|
(void)pthread_create(&th, NULL, answerer, NULL);
|
|
yes = sp_ack_wait(id, 5, who, sizeof(who));
|
|
(void)pthread_join(th, NULL);
|
|
CHECK(yes == 1, "an answer of yes is read back");
|
|
CHECK(strcmp(who, "test-phone") == 0, "the answer says who gave it");
|
|
|
|
id = sp_ack_request("run", "{\"command\":\"rm -rf /\"}", "destructive", 1);
|
|
answer_id = id;
|
|
answer_yes = 0;
|
|
(void)pthread_create(&th, NULL, answerer, NULL);
|
|
yes = sp_ack_wait(id, 5, who, sizeof(who));
|
|
(void)pthread_join(th, NULL);
|
|
CHECK(yes == 0, "an answer of no is read back as no");
|
|
|
|
id = sp_ack_request("run", "slow", "mutate", 1);
|
|
CHECK(sp_ack_wait(id, 1, who, sizeof(who)) == 0,
|
|
"a timeout is a no, not a yes");
|
|
|
|
CHECK(sp_ack_answer(999999, 1, "nobody") != 0,
|
|
"answering a request that does not exist fails");
|
|
|
|
id = sp_ack_request("edit", "{\"path\":\"/etc/passwd\"}", "mutate", 30);
|
|
if (sp_buf_init(&log) == 0) {
|
|
(void)sp_ack_list(&log);
|
|
snprintf(out, sizeof(out), "%s", log.p != NULL ? log.p : "");
|
|
sp_buf_free(&log);
|
|
CHECK(strstr(out, "\"path\":\"/etc/passwd\"") != NULL,
|
|
"the pending list shows what is being asked");
|
|
}
|
|
(void)sp_ack_answer(id, 0, "test");
|
|
|
|
printf("\n%d checks, %d failures\n", checks, fails);
|
|
(void)system("rm -rf /var/tmp/splinter_test_ack");
|
|
return fails == 0 ? 0 : 1;
|
|
}
|