1 #include "external-agent.h"
3 #include "lock-tracking.h"
13 #include <ccan/tdb2/private.h>
14 #include <ccan/tap/tap.h>
18 static struct tdb_context *tdb;
20 static enum agent_return do_operation(enum operation op, const char *name)
23 enum agent_return ret;
27 if (op != OPEN && !tdb) {
28 diag("external: No tdb open!");
32 diag("external: %s", operation_name(op));
34 k.dptr = (void *)name;
35 k.dsize = strlen(name);
37 locking_would_block = 0;
41 diag("Already have tdb %s open", tdb->name);
44 tdb = tdb_open(name, TDB_DEFAULT, O_RDWR, 0, &tap_log_attr);
46 if (!locking_would_block)
47 diag("Opening tdb gave %s", strerror(errno));
54 ecode = tdb_fetch(tdb, k, &data);
55 if (ecode == TDB_ERR_NOEXIST) {
57 } else if (ecode < 0) {
59 } else if (data.dsize != k.dsize
60 || memcmp(data.dptr, k.dptr, k.dsize) != 0) {
69 ret = tdb_store(tdb, k, k, 0) == 0 ? SUCCESS : OTHER_FAILURE;
71 case TRANSACTION_START:
72 ret = tdb_transaction_start(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
74 case TRANSACTION_COMMIT:
75 ret = tdb_transaction_commit(tdb)==0 ? SUCCESS : OTHER_FAILURE;
78 ret = tdb_needs_recovery(tdb) ? SUCCESS : FAILED;
81 ret = tdb_check(tdb, NULL, NULL) == 0 ? SUCCESS : OTHER_FAILURE;
84 ret = tdb_close(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
91 if (locking_would_block)
92 ret = WOULD_HAVE_BLOCKED;
98 int cmdfd, responsefd;
101 /* Do this before doing any tdb stuff. Return handle, or NULL. */
102 struct agent *prepare_external_agent(void)
105 int command[2], response[2];
106 char name[1+PATH_MAX];
108 if (pipe(command) != 0 || pipe(response) != 0)
116 struct agent *agent = malloc(sizeof(*agent));
120 agent->cmdfd = command[1];
121 agent->responsefd = response[0];
128 /* We want to fail, not block. */
129 nonblocking_locks = true;
130 log_prefix = "external: ";
131 while ((ret = read(command[0], name, sizeof(name))) > 0) {
132 enum agent_return result;
134 result = do_operation(name[0], name+1);
135 if (write(response[1], &result, sizeof(result))
137 err(1, "Writing response");
142 /* Ask the external agent to try to do an operation. */
143 enum agent_return external_agent_operation(struct agent *agent,
147 enum agent_return res;
153 len = 1 + strlen(name) + 1;
154 string = malloc(len);
157 strcpy(string+1, name);
159 if (write(agent->cmdfd, string, len) != len
160 || read(agent->responsefd, &res, sizeof(res)) != sizeof(res))
167 const char *agent_return_name(enum agent_return ret)
169 return ret == SUCCESS ? "SUCCESS"
170 : ret == WOULD_HAVE_BLOCKED ? "WOULD_HAVE_BLOCKED"
171 : ret == AGENT_DIED ? "AGENT_DIED"
172 : ret == FAILED ? "FAILED"
173 : ret == OTHER_FAILURE ? "OTHER_FAILURE"
177 const char *operation_name(enum operation op)
180 case OPEN: return "OPEN";
181 case FETCH: return "FETCH";
182 case STORE: return "STORE";
183 case CHECK: return "CHECK";
184 case TRANSACTION_START: return "TRANSACTION_START";
185 case TRANSACTION_COMMIT: return "TRANSACTION_COMMIT";
186 case NEEDS_RECOVERY: return "NEEDS_RECOVERY";
187 case CLOSE: return "CLOSE";
189 return "**INVALID**";
192 void free_external_agent(struct agent *agent)
195 close(agent->responsefd);