RHEL packaging: Try to fix makerpms.sh on RHEL.
[Samba/gebeck_regimport.git] / lib / ntdb / test / external-agent.c
bloba06b70f7885d1924449956fa3a17643907bd5256
1 #include "external-agent.h"
2 #include "logging.h"
3 #include "lock-tracking.h"
4 #include <sys/types.h>
5 #include <sys/wait.h>
6 #include <unistd.h>
7 #include <ccan/err/err.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <limits.h>
11 #include <string.h>
12 #include <errno.h>
13 #include "tap-interface.h"
14 #include <stdio.h>
15 #include <stdarg.h>
17 static struct ntdb_context *ntdb;
19 void (*external_agent_free)(void *) = free;
21 static enum NTDB_ERROR clear_if_first(int fd, void *arg)
23 /* We hold a lock offset 4 always, so we can tell if anyone is holding it.
24 * (This is compatible with tdb's TDB_CLEAR_IF_FIRST flag). */
25 struct flock fl;
27 fl.l_type = F_WRLCK;
28 fl.l_whence = SEEK_SET;
29 fl.l_start = 4;
30 fl.l_len = 1;
32 if (fcntl(fd, F_SETLK, &fl) == 0) {
33 /* We must be first ones to open it! */
34 diag("agent truncating file!");
35 if (ftruncate(fd, 0) != 0) {
36 return NTDB_ERR_IO;
39 fl.l_type = F_RDLCK;
40 if (fcntl(fd, F_SETLKW, &fl) != 0) {
41 return NTDB_ERR_IO;
43 return NTDB_SUCCESS;
46 static enum agent_return do_operation(enum operation op, const char *name)
48 NTDB_DATA k, d;
49 enum agent_return ret;
50 NTDB_DATA data;
51 enum NTDB_ERROR ecode;
52 union ntdb_attribute cif;
53 const char *eq;
55 if (op != OPEN && op != OPEN_WITH_HOOK && !ntdb) {
56 diag("external: No ntdb open!");
57 return OTHER_FAILURE;
60 diag("external: %s", operation_name(op));
62 eq = strchr(name, '=');
63 if (eq) {
64 k = ntdb_mkdata(name, eq - name);
65 d = ntdb_mkdata(eq + 1, strlen(eq+1));
66 } else {
67 k = ntdb_mkdata(name, strlen(name));
68 d.dsize = 0;
69 d.dptr = NULL;
72 locking_would_block = 0;
73 switch (op) {
74 case OPEN:
75 if (ntdb) {
76 diag("Already have ntdb %s open", ntdb_name(ntdb));
77 return OTHER_FAILURE;
79 ntdb = ntdb_open(name, MAYBE_NOSYNC, O_RDWR, 0, &tap_log_attr);
80 if (!ntdb) {
81 if (!locking_would_block)
82 diag("Opening ntdb gave %s", strerror(errno));
83 forget_locking();
84 ret = OTHER_FAILURE;
85 } else
86 ret = SUCCESS;
87 break;
88 case OPEN_WITH_HOOK:
89 if (ntdb) {
90 diag("Already have ntdb %s open", ntdb_name(ntdb));
91 return OTHER_FAILURE;
93 cif.openhook.base.attr = NTDB_ATTRIBUTE_OPENHOOK;
94 cif.openhook.base.next = &tap_log_attr;
95 cif.openhook.fn = clear_if_first;
96 ntdb = ntdb_open(name, MAYBE_NOSYNC, O_RDWR, 0, &cif);
97 if (!ntdb) {
98 if (!locking_would_block)
99 diag("Opening ntdb gave %s", strerror(errno));
100 forget_locking();
101 ret = OTHER_FAILURE;
102 } else
103 ret = SUCCESS;
104 break;
105 case FETCH:
106 ecode = ntdb_fetch(ntdb, k, &data);
107 if (ecode == NTDB_ERR_NOEXIST) {
108 ret = FAILED;
109 } else if (ecode < 0) {
110 ret = OTHER_FAILURE;
111 } else if (!ntdb_deq(data, d)) {
112 ret = OTHER_FAILURE;
113 external_agent_free(data.dptr);
114 } else {
115 ret = SUCCESS;
116 external_agent_free(data.dptr);
118 break;
119 case STORE:
120 ret = ntdb_store(ntdb, k, d, 0) == 0 ? SUCCESS : OTHER_FAILURE;
121 break;
122 case TRANSACTION_START:
123 ret = ntdb_transaction_start(ntdb) == 0 ? SUCCESS : OTHER_FAILURE;
124 break;
125 case TRANSACTION_COMMIT:
126 ret = ntdb_transaction_commit(ntdb)==0 ? SUCCESS : OTHER_FAILURE;
127 break;
128 case NEEDS_RECOVERY:
129 ret = external_agent_needs_rec(ntdb);
130 break;
131 case CHECK:
132 ret = ntdb_check(ntdb, NULL, NULL) == 0 ? SUCCESS : OTHER_FAILURE;
133 break;
134 case CLOSE:
135 ret = ntdb_close(ntdb) == 0 ? SUCCESS : OTHER_FAILURE;
136 ntdb = NULL;
137 break;
138 case SEND_SIGNAL:
139 /* We do this async */
140 ret = SUCCESS;
141 break;
142 default:
143 ret = OTHER_FAILURE;
146 if (locking_would_block)
147 ret = WOULD_HAVE_BLOCKED;
149 return ret;
152 struct agent {
153 int cmdfd, responsefd;
156 /* Do this before doing any ntdb stuff. Return handle, or NULL. */
157 struct agent *prepare_external_agent(void)
159 int pid, ret;
160 int command[2], response[2];
161 char name[1+PATH_MAX];
163 if (pipe(command) != 0 || pipe(response) != 0)
164 return NULL;
166 pid = fork();
167 if (pid < 0)
168 return NULL;
170 if (pid != 0) {
171 struct agent *agent = malloc(sizeof(*agent));
173 close(command[0]);
174 close(response[1]);
175 agent->cmdfd = command[1];
176 agent->responsefd = response[0];
177 return agent;
180 close(command[1]);
181 close(response[0]);
183 /* We want to fail, not block. */
184 nonblocking_locks = true;
185 log_prefix = "external: ";
186 while ((ret = read(command[0], name, sizeof(name))) > 0) {
187 enum agent_return result;
189 result = do_operation(name[0], name+1);
190 if (write(response[1], &result, sizeof(result))
191 != sizeof(result))
192 err(1, "Writing response");
193 if (name[0] == SEND_SIGNAL) {
194 struct timeval ten_ms;
195 ten_ms.tv_sec = 0;
196 ten_ms.tv_usec = 10000;
197 select(0, NULL, NULL, NULL, &ten_ms);
198 kill(getppid(), SIGUSR1);
201 exit(0);
204 /* Ask the external agent to try to do an operation. */
205 enum agent_return external_agent_operation(struct agent *agent,
206 enum operation op,
207 const char *name)
209 enum agent_return res;
210 unsigned int len;
211 char *string;
213 if (!name)
214 name = "";
215 len = 1 + strlen(name) + 1;
216 string = malloc(len);
218 string[0] = op;
219 strcpy(string+1, name);
221 if (write(agent->cmdfd, string, len) != len
222 || read(agent->responsefd, &res, sizeof(res)) != sizeof(res))
223 res = AGENT_DIED;
225 free(string);
226 return res;
229 const char *agent_return_name(enum agent_return ret)
231 return ret == SUCCESS ? "SUCCESS"
232 : ret == WOULD_HAVE_BLOCKED ? "WOULD_HAVE_BLOCKED"
233 : ret == AGENT_DIED ? "AGENT_DIED"
234 : ret == FAILED ? "FAILED"
235 : ret == OTHER_FAILURE ? "OTHER_FAILURE"
236 : "**INVALID**";
239 const char *operation_name(enum operation op)
241 switch (op) {
242 case OPEN: return "OPEN";
243 case OPEN_WITH_HOOK: return "OPEN_WITH_HOOK";
244 case FETCH: return "FETCH";
245 case STORE: return "STORE";
246 case CHECK: return "CHECK";
247 case TRANSACTION_START: return "TRANSACTION_START";
248 case TRANSACTION_COMMIT: return "TRANSACTION_COMMIT";
249 case NEEDS_RECOVERY: return "NEEDS_RECOVERY";
250 case SEND_SIGNAL: return "SEND_SIGNAL";
251 case CLOSE: return "CLOSE";
253 return "**INVALID**";
256 void free_external_agent(struct agent *agent)
258 close(agent->cmdfd);
259 close(agent->responsefd);
260 free(agent);