smbd: Remove a confusing comment
[Samba.git] / lib / tdb / test / external-agent.c
blob443d382421598f0445209775da2e7e1525979a60
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>
17 static struct tdb_context *tdb;
19 static enum agent_return do_operation(enum operation op, const char *name)
21 TDB_DATA k;
22 enum agent_return ret;
23 TDB_DATA data;
25 if (op != OPEN && op != OPEN_WITH_CLEAR_IF_FIRST && !tdb) {
26 diag("external: No tdb open!");
27 return OTHER_FAILURE;
30 k.dptr = discard_const_p(uint8_t, name);
31 k.dsize = strlen(name);
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;
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;
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;
98 break;
99 default:
100 ret = OTHER_FAILURE;
103 if (locking_would_block)
104 ret = WOULD_HAVE_BLOCKED;
106 return ret;
109 struct agent {
110 int cmdfd, responsefd;
111 pid_t pid;
114 /* Do this before doing any tdb stuff. Return handle, or NULL. */
115 struct agent *prepare_external_agent(void)
117 int ret;
118 int command[2], response[2];
119 char name[1+PATH_MAX];
120 struct agent *agent = malloc(sizeof(*agent));
122 if (pipe(command) != 0 || pipe(response) != 0) {
123 fprintf(stderr, "pipe failed: %s\n", strerror(errno));
124 exit(1);
127 agent->pid = fork();
128 if (agent->pid < 0) {
129 fprintf(stderr, "fork failed: %s\n", strerror(errno));
130 exit(1);
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;
141 close(command[1]);
142 close(response[0]);
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;
150 result = do_operation(name[0], name+1);
151 if (write(response[1], &result, sizeof(result))
152 != sizeof(result))
153 abort();
155 exit(0);
158 void shutdown_agent(struct agent *agent)
160 pid_t p;
162 close(agent->cmdfd);
163 close(agent->responsefd);
164 p = waitpid(agent->pid, NULL, WNOHANG);
165 if (p == 0) {
166 kill(agent->pid, SIGKILL);
168 waitpid(agent->pid, NULL, 0);
169 free(agent);
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)
177 enum agent_return res;
178 unsigned int len;
179 char *string;
181 if (!name)
182 name = "";
183 len = 1 + strlen(name) + 1;
184 string = malloc(len);
186 string[0] = op;
187 strcpy(string+1, name);
189 if (write(agent->cmdfd, string, len) != len
190 || read(agent->responsefd, &res, sizeof(res)) != sizeof(res))
191 res = AGENT_DIED;
193 free(string);
194 return res;
197 const char *agent_return_name(enum agent_return ret)
199 return ret == SUCCESS ? "SUCCESS"
200 : ret == WOULD_HAVE_BLOCKED ? "WOULD_HAVE_BLOCKED"
201 : ret == AGENT_DIED ? "AGENT_DIED"
202 : ret == FAILED ? "FAILED"
203 : ret == OTHER_FAILURE ? "OTHER_FAILURE"
204 : "**INVALID**";
207 const char *operation_name(enum operation op)
209 switch (op) {
210 case OPEN: return "OPEN";
211 case OPEN_WITH_CLEAR_IF_FIRST: return "OPEN_WITH_CLEAR_IF_FIRST";
212 case TRANSACTION_START: return "TRANSACTION_START";
213 case FETCH: return "FETCH";
214 case STORE: return "STORE";
215 case TRANSACTION_COMMIT: return "TRANSACTION_COMMIT";
216 case CHECK: return "CHECK";
217 case NEEDS_RECOVERY: return "NEEDS_RECOVERY";
218 case CLOSE: return "CLOSE";
219 case PING: return "PING";
220 case UNMAP: return "UNMAP";
222 return "**INVALID**";