1 #include "external-agent.h"
12 #include <ccan/tdb2/private.h>
13 #include <ccan/tap/tap.h>
17 static struct tdb_context *tdb;
20 static unsigned int locking_would_block = 0;
21 static bool nonblocking_locks = false;
24 static enum agent_return do_operation(enum operation op, const char *name)
27 enum agent_return ret;
30 if (op != OPEN && !tdb) {
31 diag("external: No tdb open!");
35 k.dptr = (void *)name;
36 k.dsize = strlen(name);
38 locking_would_block = 0;
42 diag("Already have tdb %s open", tdb->name);
45 tdb = tdb_open(name, TDB_DEFAULT, O_RDWR, 0, &tap_log_attr);
47 if (!locking_would_block)
48 diag("Opening tdb gave %s", strerror(errno));
54 data = tdb_fetch(tdb, k);
55 if (data.dptr == NULL) {
56 if (tdb_error(tdb) == TDB_ERR_NOEXIST)
60 } else if (data.dsize != k.dsize
61 || memcmp(data.dptr, k.dptr, k.dsize) != 0) {
69 ret = tdb_store(tdb, k, k, 0) == 0 ? SUCCESS : OTHER_FAILURE;
72 case TRANSACTION_START:
73 ret = tdb_transaction_start(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
75 case TRANSACTION_COMMIT:
76 ret = tdb_transaction_commit(tdb)==0 ? SUCCESS : OTHER_FAILURE;
79 ret = tdb_needs_recovery(tdb) ? SUCCESS : FAILED;
83 ret = tdb_check(tdb, NULL, NULL) == 0 ? SUCCESS : OTHER_FAILURE;
86 ret = tdb_close(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
93 if (locking_would_block)
94 ret = WOULD_HAVE_BLOCKED;
100 int cmdfd, responsefd;
103 /* Do this before doing any tdb stuff. Return handle, or NULL. */
104 struct agent *prepare_external_agent(void)
107 int command[2], response[2];
108 char name[1+PATH_MAX];
110 if (pipe(command) != 0 || pipe(response) != 0)
118 struct agent *agent = malloc(sizeof(*agent));
122 agent->cmdfd = command[1];
123 agent->responsefd = response[0];
130 /* We want to fail, not block. */
131 nonblocking_locks = true;
132 log_prefix = "external: ";
133 while ((ret = read(command[0], name, sizeof(name))) > 0) {
134 enum agent_return result;
136 result = do_operation(name[0], name+1);
137 if (write(response[1], &result, sizeof(result))
139 err(1, "Writing response");
144 /* Ask the external agent to try to do an operation. */
145 enum agent_return external_agent_operation(struct agent *agent,
149 enum agent_return res;
155 len = 1 + strlen(name) + 1;
156 string = malloc(len);
159 strcpy(string+1, name);
161 if (write(agent->cmdfd, string, len) != len
162 || read(agent->responsefd, &res, sizeof(res)) != sizeof(res))
169 const char *agent_return_name(enum agent_return ret)
171 return ret == SUCCESS ? "SUCCESS"
172 : ret == WOULD_HAVE_BLOCKED ? "WOULD_HAVE_BLOCKED"
173 : ret == AGENT_DIED ? "AGENT_DIED"
174 : ret == FAILED ? "FAILED"
175 : ret == OTHER_FAILURE ? "OTHER_FAILURE"
179 const char *operation_name(enum operation op)
182 case OPEN: return "OPEN";
183 case FETCH: return "FETCH";
184 case STORE: return "STORE";
185 case CHECK: return "CHECK";
187 case TRANSACTION_START: return "TRANSACTION_START";
188 case TRANSACTION_COMMIT: return "TRANSACTION_COMMIT";
189 case NEEDS_RECOVERY: return "NEEDS_RECOVERY";
191 case CLOSE: return "CLOSE";
193 return "**INVALID**";