tdb: Fix a "increases alignment" warning
[samba.git] / lib / 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 <fcntl.h>
8 #include <stdlib.h>
9 #include <limits.h>
10 #include <string.h>
11 #include <errno.h>
12 #include "../common/tdb_private.h"
13 #include "tap-interface.h"
14 #include <stdio.h>
15 #include <stdarg.h>
16
17 static struct tdb_context *tdb;
18
19 static enum agent_return do_operation(enum operation op, const char *name)
20 {
21         TDB_DATA k;
22         enum agent_return ret;
23         TDB_DATA data;
24
25         if (op != OPEN && op != OPEN_WITH_CLEAR_IF_FIRST && !tdb) {
26                 diag("external: No tdb open!");
27                 return OTHER_FAILURE;
28         }
29
30         k.dptr = discard_const_p(uint8_t, name);
31         k.dsize = strlen(name);
32
33         locking_would_block = 0;
34         switch (op) {
35         case OPEN:
36                 if (tdb) {
37                         diag("Already have tdb %s open", tdb_name(tdb));
38                         return OTHER_FAILURE;
39                 }
40                 tdb = tdb_open_ex(name, 0, TDB_DEFAULT, O_RDWR, 0,
41                                   &taplogctx, NULL);
42                 if (!tdb) {
43                         if (!locking_would_block)
44                                 diag("Opening tdb gave %s", strerror(errno));
45                         ret = OTHER_FAILURE;
46                 } else
47                         ret = SUCCESS;
48                 break;
49         case OPEN_WITH_CLEAR_IF_FIRST:
50                 if (tdb)
51                         return OTHER_FAILURE;
52                 tdb = tdb_open_ex(name, 0, TDB_CLEAR_IF_FIRST, O_RDWR, 0,
53                                   &taplogctx, NULL);
54                 ret = tdb ? SUCCESS : OTHER_FAILURE;
55                 break;
56         case TRANSACTION_START:
57                 ret = tdb_transaction_start(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
58                 break;
59         case FETCH:
60                 data = tdb_fetch(tdb, k);
61                 if (data.dptr == NULL) {
62                         if (tdb_error(tdb) == TDB_ERR_NOEXIST)
63                                 ret = FAILED;
64                         else
65                                 ret = OTHER_FAILURE;
66                 } else if (data.dsize != k.dsize
67                            || memcmp(data.dptr, k.dptr, k.dsize) != 0) {
68                         ret = OTHER_FAILURE;
69                 } else {
70                         ret = SUCCESS;
71                 }
72                 free(data.dptr);
73                 break;
74         case STORE:
75                 ret = tdb_store(tdb, k, k, 0) == 0 ? SUCCESS : OTHER_FAILURE;
76                 break;
77         case TRANSACTION_COMMIT:
78                 ret = tdb_transaction_commit(tdb)==0 ? SUCCESS : OTHER_FAILURE;
79                 break;
80         case CHECK:
81                 ret = tdb_check(tdb, NULL, NULL) == 0 ? SUCCESS : OTHER_FAILURE;
82                 break;
83         case NEEDS_RECOVERY:
84                 ret = tdb_needs_recovery(tdb) ? SUCCESS : FAILED;
85                 break;
86         case CLOSE:
87                 ret = tdb_close(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
88                 tdb = NULL;
89                 break;
90         case PING:
91                 ret = SUCCESS;
92                 break;
93         case UNMAP:
94                 ret = tdb_munmap(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
95                 if (ret == SUCCESS) {
96                         tdb->flags |= TDB_NOMMAP;
97                 }
98                 break;
99         default:
100                 ret = OTHER_FAILURE;
101         }
102
103         if (locking_would_block)
104                 ret = WOULD_HAVE_BLOCKED;
105
106         return ret;
107 }
108
109 struct agent {
110         int cmdfd, responsefd;
111         pid_t pid;
112 };
113
114 /* Do this before doing any tdb stuff.  Return handle, or NULL. */
115 struct agent *prepare_external_agent(void)
116 {
117         int ret;
118         int command[2], response[2];
119         char name[1+PATH_MAX];
120         struct agent *agent = malloc(sizeof(*agent));
121
122         if (pipe(command) != 0 || pipe(response) != 0) {
123                 fprintf(stderr, "pipe failed: %s\n", strerror(errno));
124                 exit(1);
125         }
126
127         agent->pid = fork();
128         if (agent->pid < 0) {
129                 fprintf(stderr, "fork failed: %s\n", strerror(errno));
130                 exit(1);
131         }
132
133         if (agent->pid != 0) {
134                 close(command[0]);
135                 close(response[1]);
136                 agent->cmdfd = command[1];
137                 agent->responsefd = response[0];
138                 return agent;
139         }
140
141         close(command[1]);
142         close(response[0]);
143
144         /* We want to fail, not block. */
145         nonblocking_locks = true;
146         log_prefix = "external: ";
147         while ((ret = read(command[0], name, sizeof(name))) > 0) {
148                 enum agent_return result;
149
150                 result = do_operation(name[0], name+1);
151                 if (write(response[1], &result, sizeof(result))
152                     != sizeof(result))
153                         abort();
154         }
155         exit(0);
156 }
157
158 void shutdown_agent(struct agent *agent)
159 {
160         pid_t p;
161
162         close(agent->cmdfd);
163         close(agent->responsefd);
164         p = waitpid(agent->pid, NULL, WNOHANG);
165         if (p == 0) {
166                 kill(agent->pid, SIGKILL);
167         }
168         waitpid(agent->pid, NULL, 0);
169         free(agent);
170 }
171
172 /* Ask the external agent to try to do an operation. */
173 enum agent_return external_agent_operation(struct agent *agent,
174                                            enum operation op,
175                                            const char *name)
176 {
177         enum agent_return res;
178         unsigned int len;
179         char *string;
180
181         if (!name)
182                 name = "";
183         len = 1 + strlen(name) + 1;
184         string = malloc(len);
185
186         string[0] = op;
187         strncpy(string+1, name, len - 1);
188         string[len-1] = '\0';
189
190         if (write(agent->cmdfd, string, len) != len
191             || read(agent->responsefd, &res, sizeof(res)) != sizeof(res))
192                 res = AGENT_DIED;
193
194         free(string);
195         return res;
196 }
197
198 const char *agent_return_name(enum agent_return ret)
199 {
200         return ret == SUCCESS ? "SUCCESS"
201                 : ret == WOULD_HAVE_BLOCKED ? "WOULD_HAVE_BLOCKED"
202                 : ret == AGENT_DIED ? "AGENT_DIED"
203                 : ret == FAILED ? "FAILED"
204                 : ret == OTHER_FAILURE ? "OTHER_FAILURE"
205                 : "**INVALID**";
206 }
207
208 const char *operation_name(enum operation op)
209 {
210         switch (op) {
211         case OPEN: return "OPEN";
212         case OPEN_WITH_CLEAR_IF_FIRST: return "OPEN_WITH_CLEAR_IF_FIRST";
213         case TRANSACTION_START: return "TRANSACTION_START";
214         case FETCH: return "FETCH";
215         case STORE: return "STORE";
216         case TRANSACTION_COMMIT: return "TRANSACTION_COMMIT";
217         case CHECK: return "CHECK";
218         case NEEDS_RECOVERY: return "NEEDS_RECOVERY";
219         case CLOSE: return "CLOSE";
220         case PING: return "PING";
221         case UNMAP: return "UNMAP";
222         }
223         return "**INVALID**";
224 }