055b5de736f6f04334fd990dc2409d1630650ff6
[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         case SEND_SIGNAL:
128                 /* We do this async */
129                 ret = SUCCESS;
130                 break;
131         default:
132                 ret = OTHER_FAILURE;
133         }
134
135         if (locking_would_block)
136                 ret = WOULD_HAVE_BLOCKED;
137
138         return ret;
139 }
140
141 struct agent {
142         int cmdfd, responsefd;
143 };
144
145 /* Do this before doing any tdb stuff.  Return handle, or NULL. */
146 struct agent *prepare_external_agent(void)
147 {
148         int pid, ret;
149         int command[2], response[2];
150         char name[1+PATH_MAX];
151
152         if (pipe(command) != 0 || pipe(response) != 0)
153                 return NULL;
154
155         pid = fork();
156         if (pid < 0)
157                 return NULL;
158
159         if (pid != 0) {
160                 struct agent *agent = malloc(sizeof(*agent));
161
162                 close(command[0]);
163                 close(response[1]);
164                 agent->cmdfd = command[1];
165                 agent->responsefd = response[0];
166                 return agent;
167         }
168
169         close(command[1]);
170         close(response[0]);
171
172         /* We want to fail, not block. */
173         nonblocking_locks = true;
174         log_prefix = "external: ";
175         while ((ret = read(command[0], name, sizeof(name))) > 0) {
176                 enum agent_return result;
177
178                 result = do_operation(name[0], name+1);
179                 if (write(response[1], &result, sizeof(result))
180                     != sizeof(result))
181                         err(1, "Writing response");
182                 if (name[0] == SEND_SIGNAL) {
183                         struct timeval ten_ms;
184                         ten_ms.tv_sec = 0;
185                         ten_ms.tv_usec = 10000;
186                         select(0, NULL, NULL, NULL, &ten_ms);
187                         kill(getppid(), SIGUSR1);
188                 }
189         }
190         exit(0);
191 }
192
193 /* Ask the external agent to try to do an operation. */
194 enum agent_return external_agent_operation(struct agent *agent,
195                                            enum operation op,
196                                            const char *name)
197 {
198         enum agent_return res;
199         unsigned int len;
200         char *string;
201
202         if (!name)
203                 name = "";
204         len = 1 + strlen(name) + 1;
205         string = malloc(len);
206
207         string[0] = op;
208         strcpy(string+1, name);
209
210         if (write(agent->cmdfd, string, len) != len
211             || read(agent->responsefd, &res, sizeof(res)) != sizeof(res))
212                 res = AGENT_DIED;
213
214         free(string);
215         return res;
216 }
217
218 const char *agent_return_name(enum agent_return ret)
219 {
220         return ret == SUCCESS ? "SUCCESS"
221                 : ret == WOULD_HAVE_BLOCKED ? "WOULD_HAVE_BLOCKED"
222                 : ret == AGENT_DIED ? "AGENT_DIED"
223                 : ret == FAILED ? "FAILED"
224                 : ret == OTHER_FAILURE ? "OTHER_FAILURE"
225                 : "**INVALID**";
226 }
227
228 const char *operation_name(enum operation op)
229 {
230         switch (op) {
231         case OPEN: return "OPEN";
232         case OPEN_WITH_HOOK: return "OPEN_WITH_HOOK";
233         case FETCH: return "FETCH";
234         case STORE: return "STORE";
235         case CHECK: return "CHECK";
236         case TRANSACTION_START: return "TRANSACTION_START";
237         case TRANSACTION_COMMIT: return "TRANSACTION_COMMIT";
238         case NEEDS_RECOVERY: return "NEEDS_RECOVERY";
239         case SEND_SIGNAL: return "SEND_SIGNAL";
240         case CLOSE: return "CLOSE";
241         }
242         return "**INVALID**";
243 }
244
245 void free_external_agent(struct agent *agent)
246 {
247         close(agent->cmdfd);
248         close(agent->responsefd);
249         free(agent);
250 }