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