1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
4 #include "external-agent.h"
7 #define alarm fast_alarm
9 /* Speed things up by doing things in milliseconds. */
10 static unsigned int fast_alarm(unsigned int milli_seconds)
14 it.it_interval.tv_sec = it.it_interval.tv_usec = 0;
15 it.it_value.tv_sec = milli_seconds / 1000;
16 it.it_value.tv_usec = milli_seconds * 1000;
17 setitimer(ITIMER_REAL, &it, NULL);
21 #define CatchSignal(sig, handler) signal((sig), (handler))
23 static void do_nothing(int signum)
27 /* This example code is taken from SAMBA, so try not to change it. */
28 static struct flock flock_struct;
30 /* Return a value which is none of v1, v2 or v3. */
31 static inline short int invalid_value(short int v1, short int v2, short int v3)
33 short int try = (v1+v2+v3)^((v1+v2+v3) << 16);
34 while (try == v1 || try == v2 || try == v3)
39 /* We invalidate in as many ways as we can, so the OS rejects it */
40 static void invalidate_flock_struct(int signum)
42 flock_struct.l_type = invalid_value(F_RDLCK, F_WRLCK, F_UNLCK);
43 flock_struct.l_whence = invalid_value(SEEK_SET, SEEK_CUR, SEEK_END);
44 flock_struct.l_start = -1;
45 /* A large negative. */
46 flock_struct.l_len = (((off_t)1 << (sizeof(off_t)*CHAR_BIT - 1)) + 1);
49 static int timeout_lock(int fd, int rw, off_t off, off_t len, bool waitflag,
52 int ret, saved_errno = errno;
53 unsigned int timeout = *(unsigned int *)_timeout;
55 flock_struct.l_type = rw;
56 flock_struct.l_whence = SEEK_SET;
57 flock_struct.l_start = off;
58 flock_struct.l_len = len;
60 CatchSignal(SIGALRM, invalidate_flock_struct);
65 ret = fcntl(fd, F_SETLKW, &flock_struct);
67 ret = fcntl(fd, F_SETLK, &flock_struct);
72 /* Not signalled? Something else went wrong. */
73 if (flock_struct.l_len == len) {
74 if (errno == EAGAIN || errno == EINTR)
89 static int tdb_chainlock_with_timeout_internal(struct tdb_context *tdb,
94 union tdb_attribute locking;
98 locking.base.attr = TDB_ATTRIBUTE_FLOCK;
99 ecode = tdb_get_attribute(tdb, &locking);
100 if (ecode != TDB_SUCCESS)
103 /* Replace locking function with our own. */
104 locking.flock.data = &timeout;
105 locking.flock.lock = timeout_lock;
107 ecode = tdb_set_attribute(tdb, &locking);
108 if (ecode != TDB_SUCCESS)
111 if (rw_type == F_RDLCK)
112 ecode = tdb_chainlock_read(tdb, key);
114 ecode = tdb_chainlock(tdb, key);
117 tdb_unset_attribute(tdb, TDB_ATTRIBUTE_FLOCK);
122 int main(int argc, char *argv[])
125 struct tdb_context *tdb;
126 TDB_DATA key = tdb_mkdata("hello", 5);
127 int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
128 TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
131 plan_tests(sizeof(flags) / sizeof(flags[0]) * 15);
133 agent = prepare_external_agent();
135 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
136 enum TDB_ERROR ecode;
137 tdb = tdb_open("run-locktimeout.tdb", flags[i],
138 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
142 /* Simple cases: should succeed. */
143 ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
145 ok1(ecode == TDB_SUCCESS);
146 ok1(tap_log_messages == 0);
148 tdb_chainunlock_read(tdb, key);
149 ok1(tap_log_messages == 0);
151 ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
153 ok1(ecode == TDB_SUCCESS);
154 ok1(tap_log_messages == 0);
156 tdb_chainunlock(tdb, key);
157 ok1(tap_log_messages == 0);
159 /* OK, get agent to start transaction, then we should time out. */
160 ok1(external_agent_operation(agent, OPEN, "run-locktimeout.tdb")
162 ok1(external_agent_operation(agent, TRANSACTION_START, "")
164 ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
166 ok1(ecode == TDB_ERR_LOCK);
167 ok1(tap_log_messages == 0);
169 /* Even if we get a different signal, should be fine. */
170 CatchSignal(SIGUSR1, do_nothing);
171 external_agent_operation(agent, SEND_SIGNAL, "");
172 ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
174 ok1(ecode == TDB_ERR_LOCK);
175 ok1(tap_log_messages == 0);
177 ok1(external_agent_operation(agent, TRANSACTION_COMMIT, "")
179 ok1(external_agent_operation(agent, CLOSE, "")
183 free_external_agent(agent);
184 return exit_status();