s3:smb2_write: pass fsp->fnum to init_strict_lock_struct()
[Samba/gebeck_regimport.git] / lib / tdb2 / test / api-locktimeout.c
blob2466817a13dc816251a1f8bd7724794026d4dee4
1 #include "config.h"
2 #include "tdb2.h"
3 #include "tap-interface.h"
4 #include "system/wait.h"
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <sys/time.h>
8 #include <fcntl.h>
9 #include <limits.h>
10 #include <errno.h>
11 #include "logging.h"
12 #include "external-agent.h"
14 #undef alarm
15 #define alarm fast_alarm
17 /* Speed things up by doing things in milliseconds. */
18 static unsigned int fast_alarm(unsigned int milli_seconds)
20 struct itimerval it;
22 it.it_interval.tv_sec = it.it_interval.tv_usec = 0;
23 it.it_value.tv_sec = milli_seconds / 1000;
24 it.it_value.tv_usec = milli_seconds * 1000;
25 setitimer(ITIMER_REAL, &it, NULL);
26 return 0;
29 #define CatchSignal(sig, handler) signal((sig), (handler))
31 static void do_nothing(int signum)
35 /* This example code is taken from SAMBA, so try not to change it. */
36 static struct flock flock_struct;
38 /* Return a value which is none of v1, v2 or v3. */
39 static inline short int invalid_value(short int v1, short int v2, short int v3)
41 short int try = (v1+v2+v3)^((v1+v2+v3) << 16);
42 while (try == v1 || try == v2 || try == v3)
43 try++;
44 return try;
47 /* We invalidate in as many ways as we can, so the OS rejects it */
48 static void invalidate_flock_struct(int signum)
50 flock_struct.l_type = invalid_value(F_RDLCK, F_WRLCK, F_UNLCK);
51 flock_struct.l_whence = invalid_value(SEEK_SET, SEEK_CUR, SEEK_END);
52 flock_struct.l_start = -1;
53 /* A large negative. */
54 flock_struct.l_len = (((off_t)1 << (sizeof(off_t)*CHAR_BIT - 1)) + 1);
57 static int timeout_lock(int fd, int rw, off_t off, off_t len, bool waitflag,
58 void *_timeout)
60 int ret, saved_errno = errno;
61 unsigned int timeout = *(unsigned int *)_timeout;
63 flock_struct.l_type = rw;
64 flock_struct.l_whence = SEEK_SET;
65 flock_struct.l_start = off;
66 flock_struct.l_len = len;
68 CatchSignal(SIGALRM, invalidate_flock_struct);
69 alarm(timeout);
71 for (;;) {
72 if (waitflag)
73 ret = fcntl(fd, F_SETLKW, &flock_struct);
74 else
75 ret = fcntl(fd, F_SETLK, &flock_struct);
77 if (ret == 0)
78 break;
80 /* Not signalled? Something else went wrong. */
81 if (flock_struct.l_len == len) {
82 if (errno == EAGAIN || errno == EINTR)
83 continue;
84 saved_errno = errno;
85 break;
86 } else {
87 saved_errno = EINTR;
88 break;
92 alarm(0);
93 errno = saved_errno;
94 return ret;
97 static int tdb_chainlock_with_timeout_internal(struct tdb_context *tdb,
98 TDB_DATA key,
99 unsigned int timeout,
100 int rw_type)
102 union tdb_attribute locking;
103 enum TDB_ERROR ecode;
105 if (timeout) {
106 locking.base.attr = TDB_ATTRIBUTE_FLOCK;
107 ecode = tdb_get_attribute(tdb, &locking);
108 if (ecode != TDB_SUCCESS)
109 return ecode;
111 /* Replace locking function with our own. */
112 locking.flock.data = &timeout;
113 locking.flock.lock = timeout_lock;
115 ecode = tdb_set_attribute(tdb, &locking);
116 if (ecode != TDB_SUCCESS)
117 return ecode;
119 if (rw_type == F_RDLCK)
120 ecode = tdb_chainlock_read(tdb, key);
121 else
122 ecode = tdb_chainlock(tdb, key);
124 if (timeout) {
125 tdb_unset_attribute(tdb, TDB_ATTRIBUTE_FLOCK);
127 return ecode;
130 int main(int argc, char *argv[])
132 unsigned int i;
133 struct tdb_context *tdb;
134 TDB_DATA key = tdb_mkdata("hello", 5);
135 int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
136 TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
137 TDB_VERSION1, TDB_NOMMAP|TDB_VERSION1,
138 TDB_CONVERT|TDB_VERSION1,
139 TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
140 struct agent *agent;
142 plan_tests(sizeof(flags) / sizeof(flags[0]) * 15);
144 agent = prepare_external_agent();
146 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
147 enum TDB_ERROR ecode;
148 tdb = tdb_open("run-locktimeout.tdb", flags[i],
149 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
150 if (!ok1(tdb))
151 break;
153 /* Simple cases: should succeed. */
154 ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
155 F_RDLCK);
156 ok1(ecode == TDB_SUCCESS);
157 ok1(tap_log_messages == 0);
159 tdb_chainunlock_read(tdb, key);
160 ok1(tap_log_messages == 0);
162 ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
163 F_WRLCK);
164 ok1(ecode == TDB_SUCCESS);
165 ok1(tap_log_messages == 0);
167 tdb_chainunlock(tdb, key);
168 ok1(tap_log_messages == 0);
170 /* OK, get agent to start transaction, then we should time out. */
171 ok1(external_agent_operation(agent, OPEN, "run-locktimeout.tdb")
172 == SUCCESS);
173 ok1(external_agent_operation(agent, TRANSACTION_START, "")
174 == SUCCESS);
175 ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
176 F_WRLCK);
177 ok1(ecode == TDB_ERR_LOCK);
178 ok1(tap_log_messages == 0);
180 /* Even if we get a different signal, should be fine. */
181 CatchSignal(SIGUSR1, do_nothing);
182 external_agent_operation(agent, SEND_SIGNAL, "");
183 ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
184 F_WRLCK);
185 ok1(ecode == TDB_ERR_LOCK);
186 ok1(tap_log_messages == 0);
188 ok1(external_agent_operation(agent, TRANSACTION_COMMIT, "")
189 == SUCCESS);
190 ok1(external_agent_operation(agent, CLOSE, "")
191 == SUCCESS);
192 tdb_close(tdb);
194 free_external_agent(agent);
195 return exit_status();