2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
34 void (*function)(void);
37 static const struct test tests[];
39 #define CHECK(A, B) check(A, B, #A, #B, __FILE__, __LINE__)
42 const char *a_string, const char *b_string, const char *file, int line)
45 fprintf(stderr, "%s:%d: expected %s == %s but %d != %d\n",
46 file, line, a_string, b_string, a, b);
53 run_lock_and_unlock(void)
55 struct lockfile *lockfile;
57 CHECK(lockfile_lock("file", 0, &lockfile), 0);
58 lockfile_unlock(lockfile);
62 run_lock_and_unlock_twice(void)
64 struct lockfile *lockfile;
66 CHECK(lockfile_lock("file", 0, &lockfile), 0);
67 lockfile_unlock(lockfile);
69 CHECK(lockfile_lock("file", 0, &lockfile), 0);
70 lockfile_unlock(lockfile);
74 run_lock_blocks_same_process(void)
76 struct lockfile *lockfile;
78 CHECK(lockfile_lock("file", 0, &lockfile), 0);
79 CHECK(lockfile_lock("file", 0, &lockfile), EDEADLK);
80 lockfile_unlock(lockfile);
84 run_lock_blocks_same_process_twice(void)
86 struct lockfile *lockfile;
88 CHECK(lockfile_lock("file", 0, &lockfile), 0);
89 CHECK(lockfile_lock("file", 0, &lockfile), EDEADLK);
90 CHECK(lockfile_lock("file", 0, &lockfile), EDEADLK);
91 lockfile_unlock(lockfile);
94 static enum { PARENT, CHILD }
108 ovs_fatal(errno, "fork failed");
113 run_lock_blocks_other_process(void)
115 /* Making this static prevents a memory leak warning from valgrind for the
116 * parent process, which cannot easily unlock (and free) 'lockfile' because
117 * it can only do so after the child has exited, and it's the caller of
118 * this function that does the wait() call. */
119 static struct lockfile *lockfile;
121 CHECK(lockfile_lock("file", 0, &lockfile), 0);
122 if (do_fork() == CHILD) {
123 lockfile_unlock(lockfile);
124 CHECK(lockfile_lock("file", 0, &lockfile), EAGAIN);
130 run_lock_twice_blocks_other_process(void)
132 struct lockfile *lockfile, *dummy;
134 CHECK(lockfile_lock("file", 0, &lockfile), 0);
135 CHECK(lockfile_lock("file", 0, &dummy), EDEADLK);
136 if (do_fork() == CHILD) {
137 CHECK(lockfile_lock("file", 0, &dummy), EAGAIN);
143 run_lock_and_unlock_allows_other_process(void)
145 struct lockfile *lockfile;
147 CHECK(lockfile_lock("file", 0, &lockfile), 0);
148 lockfile_unlock(lockfile);
150 if (do_fork() == CHILD) {
151 CHECK(lockfile_lock("file", 0, &lockfile), 0);
157 run_lock_timeout_gets_the_lock(void)
159 struct lockfile *lockfile;
161 CHECK(lockfile_lock("file", 0, &lockfile), 0);
163 if (do_fork() == CHILD) {
164 lockfile_unlock(lockfile);
165 CHECK(lockfile_lock("file", TIME_UPDATE_INTERVAL * 3, &lockfile), 0);
168 long long int now = time_msec();
169 while (time_msec() < now + TIME_UPDATE_INTERVAL) {
172 lockfile_unlock(lockfile);
177 run_lock_timeout_runs_out(void)
179 struct lockfile *lockfile;
181 CHECK(lockfile_lock("file", 0, &lockfile), 0);
183 if (do_fork() == CHILD) {
184 lockfile_unlock(lockfile);
185 CHECK(lockfile_lock("file", TIME_UPDATE_INTERVAL, &lockfile),
189 long long int now = time_msec();
190 while (time_msec() < now + TIME_UPDATE_INTERVAL * 3) {
193 lockfile_unlock(lockfile);
198 run_lock_multiple(void)
200 struct lockfile *a, *b, *c, *dummy;
202 CHECK(lockfile_lock("a", 0, &a), 0);
203 CHECK(lockfile_lock("b", 0, &b), 0);
204 CHECK(lockfile_lock("c", 0, &c), 0);
207 CHECK(lockfile_lock("a", 0, &a), 0);
208 CHECK(lockfile_lock("a", 0, &dummy), EDEADLK);
212 CHECK(lockfile_lock("a", 0, &a), 0);
218 /* Checks that locking a dangling symlink works OK. (It used to hang.) */
220 run_lock_symlink(void)
222 struct lockfile *a, *b, *dummy;
225 /* Create a symlink .a.~lock~ pointing to .b.~lock~. */
226 CHECK(symlink(".b.~lock~", ".a.~lock~"), 0);
227 CHECK(lstat(".a.~lock~", &s), 0);
228 CHECK(S_ISLNK(s.st_mode) != 0, 1);
229 CHECK(stat(".a.~lock~", &s), -1);
230 CHECK(errno, ENOENT);
231 CHECK(stat(".b.~lock~", &s), -1);
232 CHECK(errno, ENOENT);
234 CHECK(lockfile_lock("a", 0, &a), 0);
235 CHECK(lockfile_lock("a", 0, &dummy), EDEADLK);
236 CHECK(lockfile_lock("b", 0, &dummy), EDEADLK);
239 CHECK(lockfile_lock("b", 0, &b), 0);
240 CHECK(lockfile_lock("b", 0, &dummy), EDEADLK);
241 CHECK(lockfile_lock("a", 0, &dummy), EDEADLK);
244 CHECK(lstat(".a.~lock~", &s), 0);
245 CHECK(S_ISLNK(s.st_mode) != 0, 1);
246 CHECK(stat(".a.~lock~", &s), 0);
247 CHECK(S_ISREG(s.st_mode) != 0, 1);
248 CHECK(stat(".b.~lock~", &s), 0);
249 CHECK(S_ISREG(s.st_mode) != 0, 1);
257 printf("usage: %s TESTNAME\n"
258 "where TESTNAME is one of the following:\n",
260 for (i = 0; tests[i].name; i++) {
261 fprintf(stderr, "\t%s\n", tests[i].name);
265 static const struct test tests[] = {
266 #define TEST(NAME) { #NAME, run_##NAME }
267 TEST(lock_and_unlock),
268 TEST(lock_and_unlock_twice),
269 TEST(lock_blocks_same_process),
270 TEST(lock_blocks_same_process_twice),
271 TEST(lock_blocks_other_process),
272 TEST(lock_twice_blocks_other_process),
273 TEST(lock_and_unlock_allows_other_process),
274 TEST(lock_timeout_gets_the_lock),
275 TEST(lock_timeout_runs_out),
284 main(int argc, char *argv[])
286 extern struct vlog_module VLM_lockfile;
289 set_program_name(argv[0]);
290 vlog_set_levels(&VLM_lockfile, VLF_ANY_FACILITY, VLL_ERR);
293 ovs_fatal(0, "exactly one argument required; use \"%s help\" for help",
298 for (i = 0; tests[i].name; i++) {
299 if (!strcmp(argv[1], tests[i].name)) {
303 (tests[i].function)();
306 while (wait(&status) > 0) {
307 if (WIFEXITED(status) && WEXITSTATUS(status) == 11) {
310 ovs_fatal(0, "child exited in unexpected way: %s",
311 process_status_msg(status));
314 if (errno != ECHILD) {
315 ovs_fatal(errno, "wait");
318 printf("%s: success (%d child%s)\n",
319 tests[i].name, n_children, n_children != 1 ? "ren" : "");
323 ovs_fatal(0, "unknown test \"%s\"; use \"%s help\" for help",
324 argv[1], program_name);