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