dd43c2043f570b364c4b508005c5da77b271cbd7
[ccan] / ccan / tdb / test / external-agent.c
1 #include "external-agent.h"
2 #include "lock-tracking.h"
3 #include <sys/types.h>
4 #include <sys/wait.h>
5 #include <unistd.h>
6 #include <err.h>
7 #include <fcntl.h>
8 #include <stdlib.h>
9 #include <limits.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <ccan/tdb/tdb.h>
13 #include <ccan/tdb/tdb_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 void taplog(struct tdb_context *tdb,
21                    enum tdb_debug_level level,
22                    const char *fmt, ...)
23 {
24         va_list ap;
25         char line[200];
26
27         va_start(ap, fmt);
28         vsprintf(line, fmt, ap);
29         va_end(ap);
30
31         diag("external: %s", line);
32 }
33
34 static enum agent_return do_operation(enum operation op, const char *name)
35 {
36         struct tdb_logging_context logctx = { taplog, NULL };
37         TDB_DATA k;
38         enum agent_return ret;
39         TDB_DATA data;
40
41         if (op != OPEN && op != OPEN_WITH_CLEAR_IF_FIRST && !tdb) {
42                 diag("external: No tdb open!");
43                 return OTHER_FAILURE;
44         }
45
46         k.dptr = (void *)name;
47         k.dsize = strlen(name);
48
49         locking_would_block = 0;
50         switch (op) {
51         case OPEN:
52                 if (tdb) {
53                         diag("Already have tdb %s open", tdb_name(tdb));
54                         return OTHER_FAILURE;
55                 }
56                 tdb = tdb_open_ex(name, 0, TDB_DEFAULT, O_RDWR, 0,
57                                   &logctx, NULL);
58                 if (!tdb) {
59                         if (!locking_would_block)
60                                 diag("Opening tdb gave %s", strerror(errno));
61                         ret = OTHER_FAILURE;
62                 } else
63                         ret = SUCCESS;
64                 break;
65         case OPEN_WITH_CLEAR_IF_FIRST:
66                 if (tdb)
67                         return OTHER_FAILURE;
68                 tdb = tdb_open_ex(name, 0, TDB_CLEAR_IF_FIRST, O_RDWR, 0,
69                                   &logctx, NULL);
70                 ret = tdb ? SUCCESS : OTHER_FAILURE;
71                 break;
72         case TRANSACTION_START:
73                 ret = tdb_transaction_start(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
74                 break;
75         case FETCH:
76                 data = tdb_fetch(tdb, k);
77                 if (data.dptr == NULL) {
78                         if (tdb_error(tdb) == TDB_ERR_NOEXIST)
79                                 ret = FAILED;
80                         else
81                                 ret = OTHER_FAILURE;
82                 } else if (data.dsize != k.dsize
83                            || memcmp(data.dptr, k.dptr, k.dsize) != 0) {
84                         ret = OTHER_FAILURE;
85                 } else {
86                         ret = SUCCESS;
87                 }
88                 free(data.dptr);
89                 break;
90         case STORE:
91                 ret = tdb_store(tdb, k, k, 0) == 0 ? SUCCESS : OTHER_FAILURE;
92                 break;
93         case TRANSACTION_COMMIT:
94                 ret = tdb_transaction_commit(tdb)==0 ? SUCCESS : OTHER_FAILURE;
95                 break;
96         case CHECK:
97                 ret = tdb_check(tdb, NULL, NULL) == 0 ? SUCCESS : OTHER_FAILURE;
98                 break;
99         case NEEDS_RECOVERY:
100                 ret = tdb_needs_recovery(tdb) ? SUCCESS : FAILED;
101                 break;
102         case CLOSE:
103                 ret = tdb_close(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
104                 tdb = NULL;
105                 break;
106         default:
107                 ret = OTHER_FAILURE;
108         }
109
110         if (locking_would_block)
111                 ret = WOULD_HAVE_BLOCKED;
112
113         return ret;
114 }
115
116 struct agent {
117         int cmdfd, responsefd;
118 };
119
120 /* Do this before doing any tdb stuff.  Return handle, or NULL. */
121 struct agent *prepare_external_agent(void)
122 {
123         int pid, ret;
124         int command[2], response[2];
125         char name[1+PATH_MAX];
126
127         if (pipe(command) != 0 || pipe(response) != 0)
128                 return NULL;
129
130         pid = fork();
131         if (pid < 0)
132                 return NULL;
133
134         if (pid != 0) {
135                 struct agent *agent = malloc(sizeof(*agent));
136
137                 close(command[0]);
138                 close(response[1]);
139                 agent->cmdfd = command[1];
140                 agent->responsefd = response[0];
141                 return agent;
142         }
143
144         close(command[1]);
145         close(response[0]);
146
147         /* We want to fail, not block. */
148         nonblocking_locks = true;
149         while ((ret = read(command[0], name, sizeof(name))) > 0) {
150                 enum agent_return result;
151
152                 result = do_operation(name[0], name+1);
153                 if (write(response[1], &result, sizeof(result))
154                     != sizeof(result))
155                         err(1, "Writing response");
156         }
157         exit(0);
158 }
159
160 /* Ask the external agent to try to do an operation. */
161 enum agent_return external_agent_operation(struct agent *agent,
162                                            enum operation op,
163                                            const char *name)
164 {
165         enum agent_return res;
166         unsigned int len;
167         char *string;
168
169         if (!name)
170                 name = "";
171         len = 1 + strlen(name) + 1;
172         string = malloc(len);
173
174         string[0] = op;
175         strcpy(string+1, name);
176
177         if (write(agent->cmdfd, string, len) != len
178             || read(agent->responsefd, &res, sizeof(res)) != sizeof(res))
179                 res = AGENT_DIED;
180
181         free(string);
182         return res;
183 }
184
185 const char *agent_return_name(enum agent_return ret)
186 {
187         return ret == SUCCESS ? "SUCCESS"
188                 : ret == WOULD_HAVE_BLOCKED ? "WOULD_HAVE_BLOCKED"
189                 : ret == AGENT_DIED ? "AGENT_DIED"
190                 : ret == FAILED ? "FAILED"
191                 : ret == OTHER_FAILURE ? "OTHER_FAILURE"
192                 : "**INVALID**";
193 }
194
195 const char *operation_name(enum operation op)
196 {
197         switch (op) {
198         case OPEN: return "OPEN";
199         case OPEN_WITH_CLEAR_IF_FIRST: return "OPEN_WITH_CLEAR_IF_FIRST";
200         case TRANSACTION_START: return "TRANSACTION_START";
201         case FETCH: return "FETCH";
202         case STORE: return "STORE";
203         case TRANSACTION_COMMIT: return "TRANSACTION_COMMIT";
204         case CHECK: return "CHECK";
205         case NEEDS_RECOVERY: return "NEEDS_RECOVERY";
206         case CLOSE: return "CLOSE";
207         }
208         return "**INVALID**";
209 }