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.
35 void (*function)(void);
38 static void run_help(void);
40 #define CHECK(A, B) check(A, B, #A, #B, __FILE__, __LINE__)
43 const char *a_string, const char *b_string, const char *file, int line)
46 fprintf(stderr, "%s:%d: expected %s == %s but %d != %d\n",
47 file, line, a_string, b_string, a, b);
54 run_lock_and_unlock(void)
56 struct lockfile *lockfile;
58 CHECK(lockfile_lock("file", &lockfile), 0);
59 lockfile_unlock(lockfile);
63 run_lock_and_unlock_twice(void)
65 struct lockfile *lockfile;
67 CHECK(lockfile_lock("file", &lockfile), 0);
68 lockfile_unlock(lockfile);
70 CHECK(lockfile_lock("file", &lockfile), 0);
71 lockfile_unlock(lockfile);
75 run_lock_blocks_same_process(void)
77 struct lockfile *lockfile;
79 CHECK(lockfile_lock("file", &lockfile), 0);
80 CHECK(lockfile_lock("file", &lockfile), EDEADLK);
81 lockfile_unlock(lockfile);
85 run_lock_blocks_same_process_twice(void)
87 struct lockfile *lockfile;
89 CHECK(lockfile_lock("file", &lockfile), 0);
90 CHECK(lockfile_lock("file", &lockfile), EDEADLK);
91 CHECK(lockfile_lock("file", &lockfile), EDEADLK);
92 lockfile_unlock(lockfile);
96 static enum { PARENT, CHILD }
109 ovs_fatal(errno, "fork failed");
114 run_lock_blocks_other_process(void)
116 /* Making this static prevents a memory leak warning from valgrind for the
117 * parent process, which cannot easily unlock (and free) 'lockfile' because
118 * it can only do so after the child has exited, and it's the caller of
119 * this function that does the wait() call. */
120 static struct lockfile *lockfile;
122 CHECK(lockfile_lock("file", &lockfile), 0);
123 if (do_fork() == CHILD) {
124 lockfile_unlock(lockfile);
125 CHECK(lockfile_lock("file", &lockfile), EAGAIN);
131 run_lock_twice_blocks_other_process(void)
133 struct lockfile *lockfile, *dummy;
135 CHECK(lockfile_lock("file", &lockfile), 0);
136 CHECK(lockfile_lock("file", &dummy), EDEADLK);
137 if (do_fork() == CHILD) {
138 CHECK(lockfile_lock("file", &dummy), EAGAIN);
144 run_lock_and_unlock_allows_other_process(void)
146 struct lockfile *lockfile;
148 CHECK(lockfile_lock("file", &lockfile), 0);
149 lockfile_unlock(lockfile);
151 if (do_fork() == CHILD) {
152 CHECK(lockfile_lock("file", &lockfile), 0);
157 /* Checks that locking a dangling symlink works OK. (It used to hang.) */
159 run_lock_symlink(void)
161 struct lockfile *a, *b, *dummy;
164 /* Create a symlink .a.~lock~ pointing to .b.~lock~. */
165 CHECK(symlink(".b.~lock~", ".a.~lock~"), 0);
166 CHECK(lstat(".a.~lock~", &s), 0);
167 CHECK(S_ISLNK(s.st_mode) != 0, 1);
168 CHECK(stat(".a.~lock~", &s), -1);
169 CHECK(errno, ENOENT);
170 CHECK(stat(".b.~lock~", &s), -1);
171 CHECK(errno, ENOENT);
173 CHECK(lockfile_lock("a", &a), 0);
174 CHECK(lockfile_lock("a", &dummy), EDEADLK);
175 CHECK(lockfile_lock("b", &dummy), EDEADLK);
178 CHECK(lockfile_lock("b", &b), 0);
179 CHECK(lockfile_lock("b", &dummy), EDEADLK);
180 CHECK(lockfile_lock("a", &dummy), EDEADLK);
183 CHECK(lstat(".a.~lock~", &s), 0);
184 CHECK(S_ISLNK(s.st_mode) != 0, 1);
185 CHECK(stat(".a.~lock~", &s), 0);
186 CHECK(S_ISREG(s.st_mode) != 0, 1);
187 CHECK(stat(".b.~lock~", &s), 0);
188 CHECK(S_ISREG(s.st_mode) != 0, 1);
191 /* Checks that locking a file that is itself a symlink yields a lockfile in the
192 * directory that the symlink points to, named for the target of the
195 * (That is, if "a" is a symlink to "dir/b", then "a"'s lockfile is named
196 * "dir/.b.~lock".) */
198 run_lock_symlink_to_dir(void)
200 struct lockfile *a, *dummy;
203 /* Create a symlink "a" pointing to "dir/b". */
204 CHECK(mkdir("dir", 0700), 0);
205 CHECK(symlink("dir/b", "a"), 0);
206 CHECK(lstat("a", &s), 0);
207 CHECK(S_ISLNK(s.st_mode) != 0, 1);
210 CHECK(lockfile_lock("a", &a), 0);
211 CHECK(lstat("dir/.b.~lock~", &s), 0);
212 CHECK(S_ISREG(s.st_mode) != 0, 1);
213 CHECK(lstat(".a.~lock~", &s), -1);
214 CHECK(errno, ENOENT);
215 CHECK(lockfile_lock("dir/b", &dummy), EDEADLK);
222 run_lock_multiple(void)
224 struct lockfile *a, *b, *c, *dummy;
226 CHECK(lockfile_lock("a", &a), 0);
227 CHECK(lockfile_lock("b", &b), 0);
228 CHECK(lockfile_lock("c", &c), 0);
231 CHECK(lockfile_lock("a", &a), 0);
232 CHECK(lockfile_lock("a", &dummy), EDEADLK);
236 CHECK(lockfile_lock("a", &a), 0);
243 static const struct test tests[] = {
244 #define TEST(NAME) { #NAME, run_##NAME }
245 TEST(lock_and_unlock),
246 TEST(lock_and_unlock_twice),
247 TEST(lock_blocks_same_process),
248 TEST(lock_blocks_same_process_twice),
250 TEST(lock_blocks_other_process),
251 TEST(lock_twice_blocks_other_process),
252 TEST(lock_and_unlock_allows_other_process),
254 TEST(lock_symlink_to_dir),
267 printf("usage: %s TESTNAME\n"
268 "where TESTNAME is one of the following:\n",
270 for (i = 0; tests[i].name; i++) {
271 fprintf(stderr, "\t%s\n", tests[i].name);
276 test_lockfile_main(int argc, char *argv[])
280 set_program_name(argv[0]);
281 vlog_set_pattern(VLF_CONSOLE, "%c|%p|%m");
282 vlog_set_levels(NULL, VLF_SYSLOG, VLL_OFF);
285 ovs_fatal(0, "exactly one argument required; use \"%s help\" for help",
289 for (i = 0; tests[i].name; i++) {
290 if (!strcmp(argv[1], tests[i].name)) {
294 (tests[i].function)();
298 while (wait(&status) > 0) {
299 if (WIFEXITED(status) && WEXITSTATUS(status) == 11) {
302 ovs_fatal(0, "child exited in unexpected way: %s",
303 process_status_msg(status));
306 if (errno != ECHILD) {
307 ovs_fatal(errno, "wait");
311 printf("%s: success (%d child%s)\n",
312 tests[i].name, n_children, n_children != 1 ? "ren" : "");
316 ovs_fatal(0, "unknown test \"%s\"; use \"%s help\" for help",
317 argv[1], program_name);
320 OVSTEST_REGISTER("test-lockfile", test_lockfile_main);