tdb2: Make TDB1 code use TDB2's open flags.
[ccan] / ccan / tdb2 / test / tdb1-external-agent.c
1 #include "tdb1-external-agent.h"
2 #include "tdb1-lock-tracking.h"
3 #include "tdb1-logging.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/tdb1.h>
14 #include <ccan/tdb2/tdb1_private.h>
15 #include <ccan/tap/tap.h>
16 #include <stdio.h>
17 #include <stdarg.h>
18
19 static struct tdb1_context *tdb;
20
21 static enum agent_return do_operation(enum operation op, const char *name)
22 {
23         TDB_DATA k;
24         enum agent_return ret;
25         TDB_DATA data;
26
27         if (op != OPEN && !tdb) {
28                 diag("external: No tdb open!");
29                 return OTHER_FAILURE;
30         }
31
32         k.dptr = (void *)name;
33         k.dsize = strlen(name);
34
35         locking_would_block1 = 0;
36         switch (op) {
37         case OPEN:
38                 if (tdb) {
39                         diag("Already have tdb %s open", tdb->name);
40                         return OTHER_FAILURE;
41                 }
42                 tdb = tdb1_open_ex(name, 0, TDB_DEFAULT, O_RDWR, 0,
43                                   &taplogctx, NULL);
44                 if (!tdb) {
45                         if (!locking_would_block1)
46                                 diag("Opening tdb gave %s", strerror(errno));
47                         ret = OTHER_FAILURE;
48                 } else
49                         ret = SUCCESS;
50                 break;
51         case TRANSACTION_START:
52                 ret = tdb1_transaction_start(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
53                 break;
54         case FETCH:
55                 data = tdb1_fetch(tdb, k);
56                 if (data.dptr == NULL) {
57                         if (tdb->last_error == TDB_ERR_NOEXIST)
58                                 ret = FAILED;
59                         else
60                                 ret = OTHER_FAILURE;
61                 } else if (data.dsize != k.dsize
62                            || memcmp(data.dptr, k.dptr, k.dsize) != 0) {
63                         ret = OTHER_FAILURE;
64                 } else {
65                         ret = SUCCESS;
66                 }
67                 free(data.dptr);
68                 break;
69         case STORE:
70                 ret = tdb1_store(tdb, k, k, 0) == 0 ? SUCCESS : OTHER_FAILURE;
71                 break;
72         case TRANSACTION_COMMIT:
73                 ret = tdb1_transaction_commit(tdb)==0 ? SUCCESS : OTHER_FAILURE;
74                 break;
75         case CHECK:
76                 ret = tdb1_check(tdb, NULL, NULL) == 0 ? SUCCESS : OTHER_FAILURE;
77                 break;
78         case NEEDS_RECOVERY:
79                 ret = tdb1_needs_recovery(tdb) ? SUCCESS : FAILED;
80                 break;
81         case CLOSE:
82                 ret = tdb1_close(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
83                 tdb = NULL;
84                 break;
85         default:
86                 ret = OTHER_FAILURE;
87         }
88
89         if (locking_would_block1)
90                 ret = WOULD_HAVE_BLOCKED;
91
92         return ret;
93 }
94
95 struct agent {
96         int cmdfd, responsefd;
97 };
98
99 /* Do this before doing any tdb stuff.  Return handle, or NULL. */
100 struct agent *prepare_external_agent1(void)
101 {
102         int pid, ret;
103         int command[2], response[2];
104         char name[1+PATH_MAX];
105
106         if (pipe(command) != 0 || pipe(response) != 0)
107                 return NULL;
108
109         pid = fork();
110         if (pid < 0)
111                 return NULL;
112
113         if (pid != 0) {
114                 struct agent *agent = malloc(sizeof(*agent));
115
116                 close(command[0]);
117                 close(response[1]);
118                 agent->cmdfd = command[1];
119                 agent->responsefd = response[0];
120                 return agent;
121         }
122
123         close(command[1]);
124         close(response[0]);
125
126         /* We want to fail, not block. */
127         nonblocking_locks1 = true;
128         log_prefix = "external: ";
129         while ((ret = read(command[0], name, sizeof(name))) > 0) {
130                 enum agent_return result;
131
132                 result = do_operation(name[0], name+1);
133                 if (write(response[1], &result, sizeof(result))
134                     != sizeof(result))
135                         err(1, "Writing response");
136         }
137         exit(0);
138 }
139
140 /* Ask the external agent to try to do an operation. */
141 enum agent_return external_agent_operation1(struct agent *agent,
142                                            enum operation op,
143                                            const char *name)
144 {
145         enum agent_return res;
146         unsigned int len;
147         char *string;
148
149         if (!name)
150                 name = "";
151         len = 1 + strlen(name) + 1;
152         string = malloc(len);
153
154         string[0] = op;
155         strcpy(string+1, name);
156
157         if (write(agent->cmdfd, string, len) != len
158             || read(agent->responsefd, &res, sizeof(res)) != sizeof(res))
159                 res = AGENT_DIED;
160
161         free(string);
162         return res;
163 }
164
165 const char *agent_return_name1(enum agent_return ret)
166 {
167         return ret == SUCCESS ? "SUCCESS"
168                 : ret == WOULD_HAVE_BLOCKED ? "WOULD_HAVE_BLOCKED"
169                 : ret == AGENT_DIED ? "AGENT_DIED"
170                 : ret == FAILED ? "FAILED"
171                 : ret == OTHER_FAILURE ? "OTHER_FAILURE"
172                 : "**INVALID**";
173 }
174
175 const char *operation_name1(enum operation op)
176 {
177         switch (op) {
178         case OPEN: return "OPEN";
179         case TRANSACTION_START: return "TRANSACTION_START";
180         case FETCH: return "FETCH";
181         case STORE: return "STORE";
182         case TRANSACTION_COMMIT: return "TRANSACTION_COMMIT";
183         case CHECK: return "CHECK";
184         case NEEDS_RECOVERY: return "NEEDS_RECOVERY";
185         case CLOSE: return "CLOSE";
186         }
187         return "**INVALID**";
188 }