1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/open.c>
3 #include <ccan/tdb2/free.c>
4 #include <ccan/tdb2/lock.c>
5 #include <ccan/tdb2/io.c>
6 #include <ccan/tdb2/hash.c>
7 #include <ccan/tdb2/transaction.c>
8 #include <ccan/tdb2/check.c>
9 #include <ccan/tap/tap.h>
11 #include "external-agent.h"
14 #define alarm fast_alarm
16 /* Speed things up by doing things in milliseconds. */
17 static unsigned int fast_alarm(unsigned int milli_seconds)
21 it.it_interval.tv_sec = it.it_interval.tv_usec = 0;
22 it.it_value.tv_sec = milli_seconds / 1000;
23 it.it_value.tv_usec = milli_seconds * 1000;
24 setitimer(ITIMER_REAL, &it, NULL);
28 #define CatchSignal(sig, handler) signal((sig), (handler))
30 static void do_nothing(int signum)
34 /* This example code is taken from SAMBA, so try not to change it. */
35 static struct flock flock_struct;
37 /* Return a value which is none of v1, v2 or v3. */
38 static inline short int invalid_value(short int v1, short int v2, short int v3)
40 short int try = (v1+v2+v3)^((v1+v2+v3) << 16);
41 while (try == v1 || try == v2 || try == v3)
46 /* We invalidate in as many ways as we can, so the OS rejects it */
47 static void invalidate_flock_struct(int signum)
49 flock_struct.l_type = invalid_value(F_RDLCK, F_WRLCK, F_UNLCK);
50 flock_struct.l_whence = invalid_value(SEEK_SET, SEEK_CUR, SEEK_END);
51 flock_struct.l_start = -1;
52 /* A large negative. */
53 flock_struct.l_len = (((off_t)1 << (sizeof(off_t)*CHAR_BIT - 1)) + 1);
56 static int timeout_lock(int fd, int rw, off_t off, off_t len, bool waitflag,
59 int ret, saved_errno = errno;
60 unsigned int timeout = *(unsigned int *)_timeout;
62 flock_struct.l_type = rw;
63 flock_struct.l_whence = SEEK_SET;
64 flock_struct.l_start = off;
65 flock_struct.l_len = len;
67 CatchSignal(SIGALRM, invalidate_flock_struct);
72 ret = fcntl(fd, F_SETLKW, &flock_struct);
74 ret = fcntl(fd, F_SETLK, &flock_struct);
79 /* Not signalled? Something else went wrong. */
80 if (flock_struct.l_len == len) {
81 if (errno == EAGAIN || errno == EINTR)
96 static int tdb_chainlock_with_timeout_internal(struct tdb_context *tdb,
101 union tdb_attribute locking;
102 enum TDB_ERROR ecode;
105 locking.base.attr = TDB_ATTRIBUTE_FLOCK;
106 ecode = tdb_get_attribute(tdb, &locking);
107 if (ecode != TDB_SUCCESS)
110 /* Replace locking function with our own. */
111 locking.flock.data = &timeout;
112 locking.flock.lock = timeout_lock;
114 ecode = tdb_set_attribute(tdb, &locking);
115 if (ecode != TDB_SUCCESS)
118 if (rw_type == F_RDLCK)
119 ecode = tdb_chainlock_read(tdb, key);
121 ecode = tdb_chainlock(tdb, key);
124 tdb_unset_attribute(tdb, TDB_ATTRIBUTE_FLOCK);
129 int main(int argc, char *argv[])
132 struct tdb_context *tdb;
133 TDB_DATA key = tdb_mkdata("hello", 5);
134 int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
135 TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
138 plan_tests(sizeof(flags) / sizeof(flags[0]) * 15);
140 agent = prepare_external_agent();
142 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
143 enum TDB_ERROR ecode;
144 tdb = tdb_open("run-locktimeout.tdb", flags[i],
145 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
149 /* Simple cases: should succeed. */
150 ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
152 ok1(ecode == TDB_SUCCESS);
153 ok1(tap_log_messages == 0);
155 tdb_chainunlock_read(tdb, key);
156 ok1(tap_log_messages == 0);
158 ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
160 ok1(ecode == TDB_SUCCESS);
161 ok1(tap_log_messages == 0);
163 tdb_chainunlock(tdb, key);
164 ok1(tap_log_messages == 0);
166 /* OK, get agent to start transaction, then we should time out. */
167 ok1(external_agent_operation(agent, OPEN, "run-locktimeout.tdb")
169 ok1(external_agent_operation(agent, TRANSACTION_START, "")
171 ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
173 ok1(ecode == TDB_ERR_LOCK);
174 ok1(tap_log_messages == 0);
176 /* Even if we get a different signal, should be fine. */
177 CatchSignal(SIGUSR1, do_nothing);
178 external_agent_operation(agent, SEND_SIGNAL, "");
179 ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
181 ok1(ecode == TDB_ERR_LOCK);
182 ok1(tap_log_messages == 0);
184 ok1(external_agent_operation(agent, TRANSACTION_COMMIT, "")
186 ok1(external_agent_operation(agent, CLOSE, "")
190 free_external_agent(agent);
191 return exit_status();