1 #include "external-agent.h"
2 #include "lock-tracking.h"
13 #include <ccan/tdb/tdb.h>
14 #include <ccan/tdb/tdb_private.h>
15 #include <ccan/tap/tap.h>
19 static struct tdb_context *tdb;
21 static enum agent_return do_operation(enum operation op, const char *name)
24 enum agent_return ret;
27 if (op != OPEN && op != OPEN_WITH_CLEAR_IF_FIRST && !tdb) {
28 diag("external: No tdb open!");
32 k.dptr = (void *)name;
33 k.dsize = strlen(name);
35 locking_would_block = 0;
39 diag("Already have tdb %s open", tdb_name(tdb));
42 tdb = tdb_open_ex(name, 0, TDB_DEFAULT, O_RDWR, 0,
45 if (!locking_would_block)
46 diag("Opening tdb gave %s", strerror(errno));
51 case OPEN_WITH_CLEAR_IF_FIRST:
54 tdb = tdb_open_ex(name, 0, TDB_CLEAR_IF_FIRST, O_RDWR, 0,
56 ret = tdb ? SUCCESS : OTHER_FAILURE;
58 case TRANSACTION_START:
59 ret = tdb_transaction_start(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
62 data = tdb_fetch(tdb, k);
63 if (data.dptr == NULL) {
64 if (tdb_error(tdb) == TDB_ERR_NOEXIST)
68 } else if (data.dsize != k.dsize
69 || memcmp(data.dptr, k.dptr, k.dsize) != 0) {
77 ret = tdb_store(tdb, k, k, 0) == 0 ? SUCCESS : OTHER_FAILURE;
79 case TRANSACTION_COMMIT:
80 ret = tdb_transaction_commit(tdb)==0 ? SUCCESS : OTHER_FAILURE;
83 ret = tdb_check(tdb, NULL, NULL) == 0 ? SUCCESS : OTHER_FAILURE;
86 ret = tdb_needs_recovery(tdb) ? SUCCESS : FAILED;
89 ret = tdb_close(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
96 if (locking_would_block)
97 ret = WOULD_HAVE_BLOCKED;
103 int cmdfd, responsefd;
106 /* Do this before doing any tdb stuff. Return handle, or NULL. */
107 struct agent *prepare_external_agent(void)
110 int command[2], response[2];
111 char name[1+PATH_MAX];
113 if (pipe(command) != 0 || pipe(response) != 0)
121 struct agent *agent = malloc(sizeof(*agent));
125 agent->cmdfd = command[1];
126 agent->responsefd = response[0];
133 /* We want to fail, not block. */
134 nonblocking_locks = true;
135 log_prefix = "external: ";
136 while ((ret = read(command[0], name, sizeof(name))) > 0) {
137 enum agent_return result;
139 result = do_operation(name[0], name+1);
140 if (write(response[1], &result, sizeof(result))
142 err(1, "Writing response");
147 /* Ask the external agent to try to do an operation. */
148 enum agent_return external_agent_operation(struct agent *agent,
152 enum agent_return res;
158 len = 1 + strlen(name) + 1;
159 string = malloc(len);
162 strcpy(string+1, name);
164 if (write(agent->cmdfd, string, len) != len
165 || read(agent->responsefd, &res, sizeof(res)) != sizeof(res))
172 const char *agent_return_name(enum agent_return ret)
174 return ret == SUCCESS ? "SUCCESS"
175 : ret == WOULD_HAVE_BLOCKED ? "WOULD_HAVE_BLOCKED"
176 : ret == AGENT_DIED ? "AGENT_DIED"
177 : ret == FAILED ? "FAILED"
178 : ret == OTHER_FAILURE ? "OTHER_FAILURE"
182 const char *operation_name(enum operation op)
185 case OPEN: return "OPEN";
186 case OPEN_WITH_CLEAR_IF_FIRST: return "OPEN_WITH_CLEAR_IF_FIRST";
187 case TRANSACTION_START: return "TRANSACTION_START";
188 case FETCH: return "FETCH";
189 case STORE: return "STORE";
190 case TRANSACTION_COMMIT: return "TRANSACTION_COMMIT";
191 case CHECK: return "CHECK";
192 case NEEDS_RECOVERY: return "NEEDS_RECOVERY";
193 case CLOSE: return "CLOSE";
195 return "**INVALID**";