1 #include "external-agent.h"
3 #include "lock-tracking.h"
13 #include <ccan/tdb2/tdb1_private.h>
14 #include <ccan/tap/tap.h>
18 static struct tdb_context *tdb;
20 static enum TDB_ERROR clear_if_first(int fd, void *arg)
22 /* We hold a lock offset 4 always, so we can tell if anyone is holding it.
23 * (This is compatible with tdb1's TDB_CLEAR_IF_FIRST flag). */
27 fl.l_whence = SEEK_SET;
31 if (fcntl(fd, F_SETLK, &fl) == 0) {
32 /* We must be first ones to open it! */
33 diag("agent truncating file!");
34 if (ftruncate(fd, 0) != 0) {
39 if (fcntl(fd, F_SETLKW, &fl) != 0) {
45 static enum agent_return do_operation(enum operation op, const char *name)
48 enum agent_return ret;
51 union tdb_attribute cif;
53 if (op != OPEN && op != OPEN_WITH_HOOK && !tdb) {
54 diag("external: No tdb open!");
58 diag("external: %s", operation_name(op));
60 k = tdb_mkdata(name, strlen(name));
62 locking_would_block = 0;
66 diag("Already have tdb %s open", tdb->name);
69 tdb = tdb_open(name, TDB_DEFAULT, O_RDWR, 0, &tap_log_attr);
71 if (!locking_would_block)
72 diag("Opening tdb gave %s", strerror(errno));
80 diag("Already have tdb %s open", tdb->name);
83 cif.openhook.base.attr = TDB_ATTRIBUTE_OPENHOOK;
84 cif.openhook.base.next = &tap_log_attr;
85 cif.openhook.fn = clear_if_first;
86 tdb = tdb_open(name, TDB_DEFAULT, O_RDWR, 0, &cif);
88 if (!locking_would_block)
89 diag("Opening tdb gave %s", strerror(errno));
96 ecode = tdb_fetch(tdb, k, &data);
97 if (ecode == TDB_ERR_NOEXIST) {
99 } else if (ecode < 0) {
101 } else if (!tdb_deq(data, k)) {
110 ret = tdb_store(tdb, k, k, 0) == 0 ? SUCCESS : OTHER_FAILURE;
112 case TRANSACTION_START:
113 ret = tdb_transaction_start(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
115 case TRANSACTION_COMMIT:
116 ret = tdb_transaction_commit(tdb)==0 ? SUCCESS : OTHER_FAILURE;
119 if (tdb->flags & TDB_VERSION1)
120 ret = tdb1_needs_recovery(tdb) ? SUCCESS : FAILED;
122 ret = tdb_needs_recovery(tdb) ? SUCCESS : FAILED;
125 ret = tdb_check(tdb, NULL, NULL) == 0 ? SUCCESS : OTHER_FAILURE;
128 ret = tdb_close(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
132 /* We do this async */
139 if (locking_would_block)
140 ret = WOULD_HAVE_BLOCKED;
146 int cmdfd, responsefd;
149 /* Do this before doing any tdb stuff. Return handle, or NULL. */
150 struct agent *prepare_external_agent(void)
153 int command[2], response[2];
154 char name[1+PATH_MAX];
156 if (pipe(command) != 0 || pipe(response) != 0)
164 struct agent *agent = malloc(sizeof(*agent));
168 agent->cmdfd = command[1];
169 agent->responsefd = response[0];
176 /* We want to fail, not block. */
177 nonblocking_locks = true;
178 log_prefix = "external: ";
179 while ((ret = read(command[0], name, sizeof(name))) > 0) {
180 enum agent_return result;
182 result = do_operation(name[0], name+1);
183 if (write(response[1], &result, sizeof(result))
185 err(1, "Writing response");
186 if (name[0] == SEND_SIGNAL) {
187 struct timeval ten_ms;
189 ten_ms.tv_usec = 10000;
190 select(0, NULL, NULL, NULL, &ten_ms);
191 kill(getppid(), SIGUSR1);
197 /* Ask the external agent to try to do an operation. */
198 enum agent_return external_agent_operation(struct agent *agent,
202 enum agent_return res;
208 len = 1 + strlen(name) + 1;
209 string = malloc(len);
212 strcpy(string+1, name);
214 if (write(agent->cmdfd, string, len) != len
215 || read(agent->responsefd, &res, sizeof(res)) != sizeof(res))
222 const char *agent_return_name(enum agent_return ret)
224 return ret == SUCCESS ? "SUCCESS"
225 : ret == WOULD_HAVE_BLOCKED ? "WOULD_HAVE_BLOCKED"
226 : ret == AGENT_DIED ? "AGENT_DIED"
227 : ret == FAILED ? "FAILED"
228 : ret == OTHER_FAILURE ? "OTHER_FAILURE"
232 const char *operation_name(enum operation op)
235 case OPEN: return "OPEN";
236 case OPEN_WITH_HOOK: return "OPEN_WITH_HOOK";
237 case FETCH: return "FETCH";
238 case STORE: return "STORE";
239 case CHECK: return "CHECK";
240 case TRANSACTION_START: return "TRANSACTION_START";
241 case TRANSACTION_COMMIT: return "TRANSACTION_COMMIT";
242 case NEEDS_RECOVERY: return "NEEDS_RECOVERY";
243 case SEND_SIGNAL: return "SEND_SIGNAL";
244 case CLOSE: return "CLOSE";
246 return "**INVALID**";
249 void free_external_agent(struct agent *agent)
252 close(agent->responsefd);