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