3 #include "../private.h"
4 #include "tap-interface.h"
7 #include "external-agent.h"
8 #include "helpapi-external-agent.h"
11 #define alarm fast_alarm
13 /* Speed things up by doing things in milliseconds. */
14 static unsigned int fast_alarm(unsigned int milli_seconds)
18 it.it_interval.tv_sec = it.it_interval.tv_usec = 0;
19 it.it_value.tv_sec = milli_seconds / 1000;
20 it.it_value.tv_usec = milli_seconds * 1000;
21 setitimer(ITIMER_REAL, &it, NULL);
25 #define CatchSignal(sig, handler) signal((sig), (handler))
27 static void do_nothing(int signum)
31 /* This example code is taken from SAMBA, so try not to change it. */
32 static struct flock flock_struct;
34 /* Return a value which is none of v1, v2 or v3. */
35 static inline short int invalid_value(short int v1, short int v2, short int v3)
37 short int try = (v1+v2+v3)^((v1+v2+v3) << 16);
38 while (try == v1 || try == v2 || try == v3)
43 /* We invalidate in as many ways as we can, so the OS rejects it */
44 static void invalidate_flock_struct(int signum)
46 flock_struct.l_type = invalid_value(F_RDLCK, F_WRLCK, F_UNLCK);
47 flock_struct.l_whence = invalid_value(SEEK_SET, SEEK_CUR, SEEK_END);
48 flock_struct.l_start = -1;
49 /* A large negative. */
50 flock_struct.l_len = (((off_t)1 << (sizeof(off_t)*CHAR_BIT - 1)) + 1);
53 static int timeout_lock(int fd, int rw, off_t off, off_t len, bool waitflag,
56 int ret, saved_errno = errno;
57 unsigned int timeout = *(unsigned int *)_timeout;
59 flock_struct.l_type = rw;
60 flock_struct.l_whence = SEEK_SET;
61 flock_struct.l_start = off;
62 flock_struct.l_len = len;
64 CatchSignal(SIGALRM, invalidate_flock_struct);
69 ret = fcntl(fd, F_SETLKW, &flock_struct);
71 ret = fcntl(fd, F_SETLK, &flock_struct);
76 /* Not signalled? Something else went wrong. */
77 if (flock_struct.l_len == len) {
78 if (errno == EAGAIN || errno == EINTR)
93 static int ntdb_chainlock_with_timeout_internal(struct ntdb_context *ntdb,
98 union ntdb_attribute locking;
99 enum NTDB_ERROR ecode;
102 locking.base.attr = NTDB_ATTRIBUTE_FLOCK;
103 ecode = ntdb_get_attribute(ntdb, &locking);
104 if (ecode != NTDB_SUCCESS)
107 /* Replace locking function with our own. */
108 locking.flock.data = &timeout;
109 locking.flock.lock = timeout_lock;
111 ecode = ntdb_set_attribute(ntdb, &locking);
112 if (ecode != NTDB_SUCCESS)
115 if (rw_type == F_RDLCK)
116 ecode = ntdb_chainlock_read(ntdb, key);
118 ecode = ntdb_chainlock(ntdb, key);
121 ntdb_unset_attribute(ntdb, NTDB_ATTRIBUTE_FLOCK);
126 int main(int argc, char *argv[])
129 struct ntdb_context *ntdb;
130 NTDB_DATA key = ntdb_mkdata("hello", 5);
131 int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
132 NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
135 plan_tests(sizeof(flags) / sizeof(flags[0]) * 15);
137 agent = prepare_external_agent();
139 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
140 enum NTDB_ERROR ecode;
141 ntdb = ntdb_open("run-locktimeout.ntdb",
142 flags[i]|MAYBE_NOSYNC,
143 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
147 /* Simple cases: should succeed. */
148 ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
150 ok1(ecode == NTDB_SUCCESS);
151 ok1(tap_log_messages == 0);
153 ntdb_chainunlock_read(ntdb, key);
154 ok1(tap_log_messages == 0);
156 ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
158 ok1(ecode == NTDB_SUCCESS);
159 ok1(tap_log_messages == 0);
161 ntdb_chainunlock(ntdb, key);
162 ok1(tap_log_messages == 0);
164 /* OK, get agent to start transaction, then we should time out. */
165 ok1(external_agent_operation(agent, OPEN, "run-locktimeout.ntdb")
167 ok1(external_agent_operation(agent, TRANSACTION_START, "")
169 ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
171 ok1(ecode == NTDB_ERR_LOCK);
172 ok1(tap_log_messages == 0);
174 /* Even if we get a different signal, should be fine. */
175 CatchSignal(SIGUSR1, do_nothing);
176 external_agent_operation(agent, SEND_SIGNAL, "");
177 ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
179 ok1(ecode == NTDB_ERR_LOCK);
180 ok1(tap_log_messages == 0);
182 ok1(external_agent_operation(agent, TRANSACTION_COMMIT, "")
184 ok1(external_agent_operation(agent, CLOSE, "")
188 free_external_agent(agent);
189 return exit_status();