tsocket_bsd: Attempt to increase the SO_SNDBUF if we get EMSGSIZE in sendto()
[Samba.git] / lib / ntdb / test / api-locktimeout.c
blob5e24d58dc438e6946fb46cd089d216e11a69f808
1 #include "config.h"
2 #include "ntdb.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 ntdb_chainlock_with_timeout_internal(struct ntdb_context *ntdb,
98 NTDB_DATA key,
99 unsigned int timeout,
100 int rw_type)
102 union ntdb_attribute locking;
103 enum NTDB_ERROR ecode;
105 if (timeout) {
106 locking.base.attr = NTDB_ATTRIBUTE_FLOCK;
107 ecode = ntdb_get_attribute(ntdb, &locking);
108 if (ecode != NTDB_SUCCESS)
109 return ecode;
111 /* Replace locking function with our own. */
112 locking.flock.data = &timeout;
113 locking.flock.lock = timeout_lock;
115 ecode = ntdb_set_attribute(ntdb, &locking);
116 if (ecode != NTDB_SUCCESS)
117 return ecode;
119 if (rw_type == F_RDLCK)
120 ecode = ntdb_chainlock_read(ntdb, key);
121 else
122 ecode = ntdb_chainlock(ntdb, key);
124 if (timeout) {
125 ntdb_unset_attribute(ntdb, NTDB_ATTRIBUTE_FLOCK);
127 return ecode;
130 int main(int argc, char *argv[])
132 unsigned int i;
133 struct ntdb_context *ntdb;
134 NTDB_DATA key = ntdb_mkdata("hello", 5);
135 int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
136 NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
137 struct agent *agent;
139 plan_tests(sizeof(flags) / sizeof(flags[0]) * 15);
141 agent = prepare_external_agent();
143 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
144 enum NTDB_ERROR ecode;
145 ntdb = ntdb_open("run-locktimeout.ntdb",
146 flags[i]|MAYBE_NOSYNC,
147 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
148 if (!ok1(ntdb))
149 break;
151 /* Simple cases: should succeed. */
152 ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
153 F_RDLCK);
154 ok1(ecode == NTDB_SUCCESS);
155 ok1(tap_log_messages == 0);
157 ntdb_chainunlock_read(ntdb, key);
158 ok1(tap_log_messages == 0);
160 ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
161 F_WRLCK);
162 ok1(ecode == NTDB_SUCCESS);
163 ok1(tap_log_messages == 0);
165 ntdb_chainunlock(ntdb, key);
166 ok1(tap_log_messages == 0);
168 /* OK, get agent to start transaction, then we should time out. */
169 ok1(external_agent_operation(agent, OPEN, "run-locktimeout.ntdb")
170 == SUCCESS);
171 ok1(external_agent_operation(agent, TRANSACTION_START, "")
172 == SUCCESS);
173 ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
174 F_WRLCK);
175 ok1(ecode == NTDB_ERR_LOCK);
176 ok1(tap_log_messages == 0);
178 /* Even if we get a different signal, should be fine. */
179 CatchSignal(SIGUSR1, do_nothing);
180 external_agent_operation(agent, SEND_SIGNAL, "");
181 ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
182 F_WRLCK);
183 ok1(ecode == NTDB_ERR_LOCK);
184 ok1(tap_log_messages == 0);
186 ok1(external_agent_operation(agent, TRANSACTION_COMMIT, "")
187 == SUCCESS);
188 ok1(external_agent_operation(agent, CLOSE, "")
189 == SUCCESS);
190 ntdb_close(ntdb);
192 free_external_agent(agent);
193 return exit_status();