tdb2: allow multiple chain locks.
[ccan] / ccan / tdb2 / test / external-agent.c
1 #include "external-agent.h"
2 #include "logging.h"
3 #include "lock-tracking.h"
4 #include <sys/types.h>
5 #include <sys/wait.h>
6 #include <unistd.h>
7 #include <err.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <limits.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <ccan/tdb2/private.h>
14 #include <ccan/tap/tap.h>
15 #include <stdio.h>
16 #include <stdarg.h>
17
18 static struct tdb_context *tdb;
19
20 static enum TDB_ERROR clear_if_first(int fd, void *arg)
21 {
22 /* We hold a lock offset 63 always, so we can tell if anyone is holding it. */
23         struct flock fl;
24
25         fl.l_type = F_WRLCK;
26         fl.l_whence = SEEK_SET;
27         fl.l_start = 63;
28         fl.l_len = 1;
29
30         if (fcntl(fd, F_SETLK, &fl) == 0) {
31                 /* We must be first ones to open it! */
32                 diag("agent truncating file!");
33                 if (ftruncate(fd, 0) != 0) {
34                         return TDB_ERR_IO;
35                 }
36         }
37         fl.l_type = F_RDLCK;
38         if (fcntl(fd, F_SETLKW, &fl) != 0) {
39                 return TDB_ERR_IO;
40         }
41         return TDB_SUCCESS;
42 }
43
44 static enum agent_return do_operation(enum operation op, const char *name)
45 {
46         TDB_DATA k;
47         enum agent_return ret;
48         TDB_DATA data;
49         enum TDB_ERROR ecode;
50         union tdb_attribute cif;
51
52         if (op != OPEN && op != OPEN_WITH_HOOK && !tdb) {
53                 diag("external: No tdb open!");
54                 return OTHER_FAILURE;
55         }
56
57         diag("external: %s", operation_name(op));
58
59         k = tdb_mkdata(name, strlen(name));
60
61         locking_would_block = 0;
62         switch (op) {
63         case OPEN:
64                 if (tdb) {
65                         diag("Already have tdb %s open", tdb->name);
66                         return OTHER_FAILURE;
67                 }
68                 tdb = tdb_open(name, TDB_DEFAULT, O_RDWR, 0, &tap_log_attr);
69                 if (!tdb) {
70                         if (!locking_would_block)
71                                 diag("Opening tdb gave %s", strerror(errno));
72                         forget_locking();
73                         ret = OTHER_FAILURE;
74                 } else
75                         ret = SUCCESS;
76                 break;
77         case OPEN_WITH_HOOK:
78                 if (tdb) {
79                         diag("Already have tdb %s open", tdb->name);
80                         return OTHER_FAILURE;
81                 }
82                 cif.openhook.base.attr = TDB_ATTRIBUTE_OPENHOOK;
83                 cif.openhook.base.next = &tap_log_attr;
84                 cif.openhook.fn = clear_if_first;
85                 tdb = tdb_open(name, TDB_DEFAULT, O_RDWR, 0, &cif);
86                 if (!tdb) {
87                         if (!locking_would_block)
88                                 diag("Opening tdb gave %s", strerror(errno));
89                         forget_locking();
90                         ret = OTHER_FAILURE;
91                 } else
92                         ret = SUCCESS;
93                 break;
94         case FETCH:
95                 ecode = tdb_fetch(tdb, k, &data);
96                 if (ecode == TDB_ERR_NOEXIST) {
97                         ret = FAILED;
98                 } else if (ecode < 0) {
99                         ret = OTHER_FAILURE;
100                 } else if (!tdb_deq(data, k)) {
101                         ret = OTHER_FAILURE;
102                         free(data.dptr);
103                 } else {
104                         ret = SUCCESS;
105                         free(data.dptr);
106                 }
107                 break;
108         case STORE:
109                 ret = tdb_store(tdb, k, k, 0) == 0 ? SUCCESS : OTHER_FAILURE;
110                 break;
111         case TRANSACTION_START:
112                 ret = tdb_transaction_start(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
113                 break;
114         case TRANSACTION_COMMIT:
115                 ret = tdb_transaction_commit(tdb)==0 ? SUCCESS : OTHER_FAILURE;
116                 break;
117         case NEEDS_RECOVERY:
118                 ret = tdb_needs_recovery(tdb) ? SUCCESS : FAILED;
119                 break;
120         case CHECK:
121                 ret = tdb_check(tdb, NULL, NULL) == 0 ? SUCCESS : OTHER_FAILURE;
122                 break;
123         case CLOSE:
124                 ret = tdb_close(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
125                 tdb = NULL;
126                 break;
127         default:
128                 ret = OTHER_FAILURE;
129         }
130
131         if (locking_would_block)
132                 ret = WOULD_HAVE_BLOCKED;
133
134         return ret;
135 }
136
137 struct agent {
138         int cmdfd, responsefd;
139 };
140
141 /* Do this before doing any tdb stuff.  Return handle, or NULL. */
142 struct agent *prepare_external_agent(void)
143 {
144         int pid, ret;
145         int command[2], response[2];
146         char name[1+PATH_MAX];
147
148         if (pipe(command) != 0 || pipe(response) != 0)
149                 return NULL;
150
151         pid = fork();
152         if (pid < 0)
153                 return NULL;
154
155         if (pid != 0) {
156                 struct agent *agent = malloc(sizeof(*agent));
157
158                 close(command[0]);
159                 close(response[1]);
160                 agent->cmdfd = command[1];
161                 agent->responsefd = response[0];
162                 return agent;
163         }
164
165         close(command[1]);
166         close(response[0]);
167
168         /* We want to fail, not block. */
169         nonblocking_locks = true;
170         log_prefix = "external: ";
171         while ((ret = read(command[0], name, sizeof(name))) > 0) {
172                 enum agent_return result;
173
174                 result = do_operation(name[0], name+1);
175                 if (write(response[1], &result, sizeof(result))
176                     != sizeof(result))
177                         err(1, "Writing response");
178         }
179         exit(0);
180 }
181
182 /* Ask the external agent to try to do an operation. */
183 enum agent_return external_agent_operation(struct agent *agent,
184                                            enum operation op,
185                                            const char *name)
186 {
187         enum agent_return res;
188         unsigned int len;
189         char *string;
190
191         if (!name)
192                 name = "";
193         len = 1 + strlen(name) + 1;
194         string = malloc(len);
195
196         string[0] = op;
197         strcpy(string+1, name);
198
199         if (write(agent->cmdfd, string, len) != len
200             || read(agent->responsefd, &res, sizeof(res)) != sizeof(res))
201                 res = AGENT_DIED;
202
203         free(string);
204         return res;
205 }
206
207 const char *agent_return_name(enum agent_return ret)
208 {
209         return ret == SUCCESS ? "SUCCESS"
210                 : ret == WOULD_HAVE_BLOCKED ? "WOULD_HAVE_BLOCKED"
211                 : ret == AGENT_DIED ? "AGENT_DIED"
212                 : ret == FAILED ? "FAILED"
213                 : ret == OTHER_FAILURE ? "OTHER_FAILURE"
214                 : "**INVALID**";
215 }
216
217 const char *operation_name(enum operation op)
218 {
219         switch (op) {
220         case OPEN: return "OPEN";
221         case OPEN_WITH_HOOK: return "OPEN_WITH_HOOK";
222         case FETCH: return "FETCH";
223         case STORE: return "STORE";
224         case CHECK: return "CHECK";
225         case TRANSACTION_START: return "TRANSACTION_START";
226         case TRANSACTION_COMMIT: return "TRANSACTION_COMMIT";
227         case NEEDS_RECOVERY: return "NEEDS_RECOVERY";
228         case CLOSE: return "CLOSE";
229         }
230         return "**INVALID**";
231 }
232
233 void free_external_agent(struct agent *agent)
234 {
235         close(agent->cmdfd);
236         close(agent->responsefd);
237         free(agent);
238 }