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