2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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.
36 void (*function)(void);
39 static const struct test tests[];
42 run_lock_and_unlock(void)
44 struct lockfile *lockfile;
46 assert(lockfile_lock("file", 0, &lockfile) == 0);
47 lockfile_unlock(lockfile);
51 run_lock_and_unlock_twice(void)
53 struct lockfile *lockfile;
55 assert(lockfile_lock("file", 0, &lockfile) == 0);
56 lockfile_unlock(lockfile);
58 assert(lockfile_lock("file", 0, &lockfile) == 0);
59 lockfile_unlock(lockfile);
63 run_lock_blocks_same_process(void)
65 struct lockfile *lockfile;
67 assert(lockfile_lock("file", 0, &lockfile) == 0);
68 assert(lockfile_lock("file", 0, &lockfile) == EDEADLK);
69 lockfile_unlock(lockfile);
73 run_lock_blocks_same_process_twice(void)
75 struct lockfile *lockfile;
77 assert(lockfile_lock("file", 0, &lockfile) == 0);
78 assert(lockfile_lock("file", 0, &lockfile) == EDEADLK);
79 assert(lockfile_lock("file", 0, &lockfile) == EDEADLK);
80 lockfile_unlock(lockfile);
83 static enum { PARENT, CHILD }
97 ovs_fatal(errno, "fork failed");
102 run_lock_blocks_other_process(void)
104 /* Making this static prevents a memory leak warning from valgrind for the
105 * parent process, which cannot easily unlock (and free) 'lockfile' because
106 * it can only do so after the child has exited, and it's the caller of
107 * this function that does the wait() call. */
108 static struct lockfile *lockfile;
110 assert(lockfile_lock("file", 0, &lockfile) == 0);
111 if (do_fork() == CHILD) {
112 lockfile_unlock(lockfile);
113 assert(lockfile_lock("file", 0, &lockfile) == EAGAIN);
119 run_lock_twice_blocks_other_process(void)
121 struct lockfile *lockfile, *dummy;
123 assert(lockfile_lock("file", 0, &lockfile) == 0);
124 assert(lockfile_lock("file", 0, &dummy) == EDEADLK);
125 if (do_fork() == CHILD) {
126 assert(lockfile_lock("file", 0, &dummy) == EAGAIN);
132 run_lock_and_unlock_allows_other_process(void)
134 struct lockfile *lockfile;
136 assert(lockfile_lock("file", 0, &lockfile) == 0);
137 lockfile_unlock(lockfile);
139 if (do_fork() == CHILD) {
140 assert(lockfile_lock("file", 0, &lockfile) == 0);
146 run_lock_timeout_gets_the_lock(void)
148 struct lockfile *lockfile;
150 assert(lockfile_lock("file", 0, &lockfile) == 0);
152 if (do_fork() == CHILD) {
153 lockfile_unlock(lockfile);
154 assert(lockfile_lock("file", TIME_UPDATE_INTERVAL * 3,
158 long long int now = time_msec();
159 while (time_msec() < now + TIME_UPDATE_INTERVAL) {
162 lockfile_unlock(lockfile);
167 run_lock_timeout_runs_out(void)
169 struct lockfile *lockfile;
171 assert(lockfile_lock("file", 0, &lockfile) == 0);
173 if (do_fork() == CHILD) {
174 lockfile_unlock(lockfile);
175 assert(lockfile_lock("file", TIME_UPDATE_INTERVAL,
176 &lockfile) == ETIMEDOUT);
179 long long int now = time_msec();
180 while (time_msec() < now + TIME_UPDATE_INTERVAL * 3) {
183 lockfile_unlock(lockfile);
188 run_lock_multiple(void)
190 struct lockfile *a, *b, *c, *dummy;
192 assert(lockfile_lock("a", 0, &a) == 0);
193 assert(lockfile_lock("b", 0, &b) == 0);
194 assert(lockfile_lock("c", 0, &c) == 0);
197 assert(lockfile_lock("a", 0, &a) == 0);
198 assert(lockfile_lock("a", 0, &dummy) == EDEADLK);
202 assert(lockfile_lock("a", 0, &a) == 0);
213 printf("usage: %s TESTNAME\n"
214 "where TESTNAME is one of the following:\n",
216 for (i = 0; tests[i].name; i++) {
217 fprintf(stderr, "\t%s\n", tests[i].name);
221 static const struct test tests[] = {
222 #define TEST(NAME) { #NAME, run_##NAME }
223 TEST(lock_and_unlock),
224 TEST(lock_and_unlock_twice),
225 TEST(lock_blocks_same_process),
226 TEST(lock_blocks_same_process_twice),
227 TEST(lock_blocks_other_process),
228 TEST(lock_twice_blocks_other_process),
229 TEST(lock_and_unlock_allows_other_process),
230 TEST(lock_timeout_gets_the_lock),
231 TEST(lock_timeout_runs_out),
239 main(int argc, char *argv[])
241 extern struct vlog_module VLM_lockfile;
244 set_program_name(argv[0]);
245 vlog_set_levels(&VLM_lockfile, VLF_ANY_FACILITY, VLL_ERR);
248 ovs_fatal(0, "exactly one argument required; use \"%s help\" for help",
253 for (i = 0; tests[i].name; i++) {
254 if (!strcmp(argv[1], tests[i].name)) {
258 (tests[i].function)();
261 while (wait(&status) > 0) {
262 if (WIFEXITED(status) && WEXITSTATUS(status) == 11) {
265 ovs_fatal(0, "child exited in unexpected way: %s",
266 process_status_msg(status));
269 if (errno != ECHILD) {
270 ovs_fatal(errno, "wait");
273 printf("%s: success (%d child%s)\n",
274 tests[i].name, n_children, n_children != 1 ? "ren" : "");
278 ovs_fatal(0, "unknown test \"%s\"; use \"%s help\" for help",
279 argv[1], program_name);