merge
[ccan] / ccan / tdb / test / external-transaction.c
1 #include "external-transaction.h"
2 #include <sys/types.h>
3 #include <sys/wait.h>
4 #include <unistd.h>
5 #include <err.h>
6 #include <fcntl.h>
7 #include <stdlib.h>
8 #include <limits.h>
9 #include <string.h>
10 #include <ccan/tdb/tdb.h>
11
12 static volatile sig_atomic_t alarmed;
13 static void do_alarm(int signum)
14 {
15         alarmed++;
16 }
17
18 static int do_transaction(const char *name)
19 {
20         TDB_DATA k = { .dptr = (void *)"a", .dsize = 1 };
21         TDB_DATA d = { .dptr = (void *)"b", .dsize = 1 };
22         struct tdb_context *tdb = tdb_open(name, 0, 0, O_RDWR, 0);
23
24         if (!tdb)
25                 return -1;
26
27         alarmed = 0;
28         tdb_setalarm_sigptr(tdb, &alarmed);
29
30         alarm(1);
31         if (tdb_transaction_start(tdb) != 0)
32                 goto maybe_alarmed;
33
34         if (tdb_store(tdb, k, d, 0) != 0) {
35                 tdb_transaction_cancel(tdb);
36                 tdb_close(tdb);
37                 return -2;
38         }
39
40         if (tdb_transaction_commit(tdb) == 0) {
41                 tdb_delete(tdb, k);
42                 tdb_close(tdb);
43                 return 1;
44         }
45
46         tdb_delete(tdb, k);
47 maybe_alarmed:
48         tdb_close(tdb);
49         if (alarmed)
50                 return 0;
51         return -3;
52 }
53
54
55 /* Do this before doing any tdb stuff.  Return handle, or -1. */
56 int prepare_external_agent(void)
57 {
58         int pid;
59         int command[2], response[2];
60         struct sigaction act = { .sa_handler = do_alarm };
61         char name[PATH_MAX];
62
63         if (pipe(command) != 0 || pipe(response) != 0)
64                 return -1;
65
66         pid = fork();
67         if (pid < 0)
68                 return -1;
69
70         if (pid != 0) {
71                 close(command[0]);
72                 close(response[1]);
73                 /* FIXME: Make fds consective. */
74                 dup2(command[1]+1, response[1]);
75                 return command[1];
76         }
77
78         close(command[1]);
79         close(response[0]);
80         sigaction(SIGALRM, &act, NULL);
81
82         while (read(command[0], name, sizeof(name)) != 0) {
83                 int result = do_transaction(name);
84                 if (write(response[1], &result, sizeof(result))
85                     != sizeof(result))
86                         err(1, "Writing response");
87         }
88         exit(0);
89 }
90
91 /* Ask the external agent to try to do a transaction. */
92 bool external_agent_transaction(int handle, const char *tdbname)
93 {
94         int res;
95
96         if (write(handle, tdbname, strlen(tdbname)+1)
97             != strlen(tdbname)+1)
98                 err(1, "Writing to agent");
99
100         if (read(handle+1, &res, sizeof(res)) != sizeof(res))
101                 err(1, "Reading from agent");
102
103         if (res > 1)
104                 errx(1, "Agent returned %u\n", res);
105
106         return res;
107 }