1 #include "external-transaction.h"
10 #include <ccan/tdb/tdb.h>
12 static volatile sig_atomic_t alarmed;
13 static void do_alarm(int signum)
18 static int do_transaction(const char *name)
20 TDB_DATA k = { .dptr = (void *)"a", .dsize = 1 };
21 TDB_DATA d = { .dptr = (void *)"b", .dsize = 1 };
22 struct tdb_context *tdb = tdb_open(name, 0, 0, O_RDWR, 0);
28 tdb_setalarm_sigptr(tdb, &alarmed);
31 if (tdb_transaction_start(tdb) != 0)
34 if (tdb_store(tdb, k, d, 0) != 0) {
35 tdb_transaction_cancel(tdb);
40 if (tdb_transaction_commit(tdb) == 0) {
55 /* Do this before doing any tdb stuff. Return handle, or -1. */
56 int prepare_external_agent(void)
59 int command[2], response[2];
60 struct sigaction act = { .sa_handler = do_alarm };
63 if (pipe(command) != 0 || pipe(response) != 0)
73 /* FIXME: Make fds consective. */
74 dup2(command[1]+1, response[1]);
80 sigaction(SIGALRM, &act, NULL);
82 while (read(command[0], name, sizeof(name)) != 0) {
83 int result = do_transaction(name);
84 if (write(response[1], &result, sizeof(result))
86 err(1, "Writing response");
91 /* Ask the external agent to try to do a transaction. */
92 bool external_agent_transaction(int handle, const char *tdbname)
96 if (write(handle, tdbname, strlen(tdbname)+1)
98 err(1, "Writing to agent");
100 if (read(handle+1, &res, sizeof(res)) != sizeof(res))
101 err(1, "Reading from agent");
104 errx(1, "Agent returned %u\n", res);