2 * Copyright (c) 2009, 2010, 2011, 2012, 2014 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.
29 #include "openvswitch/vlog.h"
33 void (*function)(void);
36 static void run_help(void);
38 #define CHECK(A, B) check(A, B, #A, #B, __FILE__, __LINE__)
41 const char *a_string, const char *b_string, const char *file, int line)
44 fprintf(stderr, "%s:%d: expected %s == %s but %d != %d\n",
45 file, line, a_string, b_string, a, b);
52 run_lock_and_unlock(void)
54 struct lockfile *lockfile;
56 CHECK(lockfile_lock("file", &lockfile), 0);
57 lockfile_unlock(lockfile);
61 run_lock_and_unlock_twice(void)
63 struct lockfile *lockfile;
65 CHECK(lockfile_lock("file", &lockfile), 0);
66 lockfile_unlock(lockfile);
68 CHECK(lockfile_lock("file", &lockfile), 0);
69 lockfile_unlock(lockfile);
73 run_lock_blocks_same_process(void)
75 struct lockfile *lockfile;
77 CHECK(lockfile_lock("file", &lockfile), 0);
78 CHECK(lockfile_lock("file", &lockfile), EDEADLK);
79 lockfile_unlock(lockfile);
83 run_lock_blocks_same_process_twice(void)
85 struct lockfile *lockfile;
87 CHECK(lockfile_lock("file", &lockfile), 0);
88 CHECK(lockfile_lock("file", &lockfile), EDEADLK);
89 CHECK(lockfile_lock("file", &lockfile), EDEADLK);
90 lockfile_unlock(lockfile);
94 static enum { PARENT, CHILD }
107 ovs_fatal(errno, "fork failed");
112 run_lock_blocks_other_process(void)
114 /* Making this static prevents a memory leak warning from valgrind for the
115 * parent process, which cannot easily unlock (and free) 'lockfile' because
116 * it can only do so after the child has exited, and it's the caller of
117 * this function that does the wait() call. */
118 static struct lockfile *lockfile;
120 CHECK(lockfile_lock("file", &lockfile), 0);
121 if (do_fork() == CHILD) {
122 lockfile_unlock(lockfile);
123 CHECK(lockfile_lock("file", &lockfile), EAGAIN);
129 run_lock_twice_blocks_other_process(void)
131 struct lockfile *lockfile, *dummy;
133 CHECK(lockfile_lock("file", &lockfile), 0);
134 CHECK(lockfile_lock("file", &dummy), EDEADLK);
135 if (do_fork() == CHILD) {
136 CHECK(lockfile_lock("file", &dummy), EAGAIN);
142 run_lock_and_unlock_allows_other_process(void)
144 struct lockfile *lockfile;
146 CHECK(lockfile_lock("file", &lockfile), 0);
147 lockfile_unlock(lockfile);
149 if (do_fork() == CHILD) {
150 CHECK(lockfile_lock("file", &lockfile), 0);
155 /* Checks that locking a dangling symlink works OK. (It used to hang.) */
157 run_lock_symlink(void)
159 struct lockfile *a, *b, *dummy;
162 /* Create a symlink .a.~lock~ pointing to .b.~lock~. */
163 CHECK(symlink(".b.~lock~", ".a.~lock~"), 0);
164 CHECK(lstat(".a.~lock~", &s), 0);
165 CHECK(S_ISLNK(s.st_mode) != 0, 1);
166 CHECK(stat(".a.~lock~", &s), -1);
167 CHECK(errno, ENOENT);
168 CHECK(stat(".b.~lock~", &s), -1);
169 CHECK(errno, ENOENT);
171 CHECK(lockfile_lock("a", &a), 0);
172 CHECK(lockfile_lock("a", &dummy), EDEADLK);
173 CHECK(lockfile_lock("b", &dummy), EDEADLK);
176 CHECK(lockfile_lock("b", &b), 0);
177 CHECK(lockfile_lock("b", &dummy), EDEADLK);
178 CHECK(lockfile_lock("a", &dummy), EDEADLK);
181 CHECK(lstat(".a.~lock~", &s), 0);
182 CHECK(S_ISLNK(s.st_mode) != 0, 1);
183 CHECK(stat(".a.~lock~", &s), 0);
184 CHECK(S_ISREG(s.st_mode) != 0, 1);
185 CHECK(stat(".b.~lock~", &s), 0);
186 CHECK(S_ISREG(s.st_mode) != 0, 1);
189 /* Checks that locking a file that is itself a symlink yields a lockfile in the
190 * directory that the symlink points to, named for the target of the
193 * (That is, if "a" is a symlink to "dir/b", then "a"'s lockfile is named
194 * "dir/.b.~lock".) */
196 run_lock_symlink_to_dir(void)
198 struct lockfile *a, *dummy;
201 /* Create a symlink "a" pointing to "dir/b". */
202 CHECK(mkdir("dir", 0700), 0);
203 CHECK(symlink("dir/b", "a"), 0);
204 CHECK(lstat("a", &s), 0);
205 CHECK(S_ISLNK(s.st_mode) != 0, 1);
208 CHECK(lockfile_lock("a", &a), 0);
209 CHECK(lstat("dir/.b.~lock~", &s), 0);
210 CHECK(S_ISREG(s.st_mode) != 0, 1);
211 CHECK(lstat(".a.~lock~", &s), -1);
212 CHECK(errno, ENOENT);
213 CHECK(lockfile_lock("dir/b", &dummy), EDEADLK);
220 run_lock_multiple(void)
222 struct lockfile *a, *b, *c, *dummy;
224 CHECK(lockfile_lock("a", &a), 0);
225 CHECK(lockfile_lock("b", &b), 0);
226 CHECK(lockfile_lock("c", &c), 0);
229 CHECK(lockfile_lock("a", &a), 0);
230 CHECK(lockfile_lock("a", &dummy), EDEADLK);
234 CHECK(lockfile_lock("a", &a), 0);
241 static const struct test tests[] = {
242 #define TEST(NAME) { #NAME, run_##NAME }
243 TEST(lock_and_unlock),
244 TEST(lock_and_unlock_twice),
245 TEST(lock_blocks_same_process),
246 TEST(lock_blocks_same_process_twice),
248 TEST(lock_blocks_other_process),
249 TEST(lock_twice_blocks_other_process),
250 TEST(lock_and_unlock_allows_other_process),
252 TEST(lock_symlink_to_dir),
265 printf("usage: %s TESTNAME\n"
266 "where TESTNAME is one of the following:\n",
268 for (i = 0; tests[i].name; i++) {
269 fprintf(stderr, "\t%s\n", tests[i].name);
274 test_lockfile_main(int argc, char *argv[])
278 set_program_name(argv[0]);
279 vlog_set_pattern(VLF_CONSOLE, "%c|%p|%m");
280 vlog_set_levels(NULL, VLF_SYSLOG, VLL_OFF);
283 ovs_fatal(0, "exactly one argument required; use \"%s help\" for help",
287 for (i = 0; tests[i].name; i++) {
288 if (!strcmp(argv[1], tests[i].name)) {
292 (tests[i].function)();
296 while (wait(&status) > 0) {
297 if (WIFEXITED(status) && WEXITSTATUS(status) == 11) {
300 ovs_fatal(0, "child exited in unexpected way: %s",
301 process_status_msg(status));
304 if (errno != ECHILD) {
305 ovs_fatal(errno, "wait");
309 printf("%s: success (%d child%s)\n",
310 tests[i].name, n_children, n_children != 1 ? "ren" : "");
314 ovs_fatal(0, "unknown test \"%s\"; use \"%s help\" for help",
315 argv[1], program_name);
318 OVSTEST_REGISTER("test-lockfile", test_lockfile_main);