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