412010b30690eea1c11c43d21412629b838826b0
[sliver-openvswitch.git] / tests / test-lockfile.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18
19 #include "lockfile.h"
20
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <sys/stat.h>
24 #include <sys/wait.h>
25 #include <unistd.h>
26
27 #include "process.h"
28 #include "timeval.h"
29 #include "util.h"
30 #include "vlog.h"
31
32 struct test {
33     const char *name;
34     void (*function)(void);
35 };
36
37 static const struct test tests[];
38
39 #define CHECK(A, B) check(A, B, #A, #B, __FILE__, __LINE__)
40 static void
41 check(int a, int b,
42       const char *a_string, const char *b_string, const char *file, int line)
43 {
44     if (a != b) {
45         fprintf(stderr, "%s:%d: expected %s == %s but %d != %d\n",
46                 file, line, a_string, b_string, a, b);
47         fflush(stderr);
48         abort();
49     }
50 }
51
52 static void
53 run_lock_and_unlock(void)
54 {
55     struct lockfile *lockfile;
56
57     CHECK(lockfile_lock("file", 0, &lockfile), 0);
58     lockfile_unlock(lockfile);
59 }
60
61 static void
62 run_lock_and_unlock_twice(void)
63 {
64     struct lockfile *lockfile;
65
66     CHECK(lockfile_lock("file", 0, &lockfile), 0);
67     lockfile_unlock(lockfile);
68
69     CHECK(lockfile_lock("file", 0, &lockfile), 0);
70     lockfile_unlock(lockfile);
71 }
72
73 static void
74 run_lock_blocks_same_process(void)
75 {
76     struct lockfile *lockfile;
77
78     CHECK(lockfile_lock("file", 0, &lockfile), 0);
79     CHECK(lockfile_lock("file", 0, &lockfile), EDEADLK);
80     lockfile_unlock(lockfile);
81 }
82
83 static void
84 run_lock_blocks_same_process_twice(void)
85 {
86     struct lockfile *lockfile;
87
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);
92 }
93
94 static enum { PARENT, CHILD }
95 do_fork(void)
96 {
97     switch (fork()) {
98     case 0:
99         time_postfork();
100         lockfile_postfork();
101         return CHILD;
102
103     default:
104         return PARENT;
105
106     case -1:
107         /* Error. */
108         ovs_fatal(errno, "fork failed");
109     }
110 }
111
112 static void
113 run_lock_blocks_other_process(void)
114 {
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;
120
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);
125         exit(11);
126     }
127 }
128
129 static void
130 run_lock_twice_blocks_other_process(void)
131 {
132     struct lockfile *lockfile, *dummy;
133
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);
138         exit(11);
139     }
140 }
141
142 static void
143 run_lock_and_unlock_allows_other_process(void)
144 {
145     struct lockfile *lockfile;
146
147     CHECK(lockfile_lock("file", 0, &lockfile), 0);
148     lockfile_unlock(lockfile);
149
150     if (do_fork() == CHILD) {
151         CHECK(lockfile_lock("file", 0, &lockfile), 0);
152         exit(11);
153     }
154 }
155
156 static void
157 run_lock_timeout_gets_the_lock(void)
158 {
159     struct lockfile *lockfile;
160
161     CHECK(lockfile_lock("file", 0, &lockfile), 0);
162
163     if (do_fork() == CHILD) {
164         lockfile_unlock(lockfile);
165         CHECK(lockfile_lock("file", TIME_UPDATE_INTERVAL * 3, &lockfile), 0);
166         exit(11);
167     } else {
168         long long int now = time_msec();
169         while (time_msec() < now + TIME_UPDATE_INTERVAL) {
170             pause();
171         }
172         lockfile_unlock(lockfile);
173     }
174 }
175
176 static void
177 run_lock_timeout_runs_out(void)
178 {
179     struct lockfile *lockfile;
180
181     CHECK(lockfile_lock("file", 0, &lockfile), 0);
182
183     if (do_fork() == CHILD) {
184         lockfile_unlock(lockfile);
185         CHECK(lockfile_lock("file", TIME_UPDATE_INTERVAL, &lockfile),
186               ETIMEDOUT);
187         exit(11);
188     } else {
189         long long int now = time_msec();
190         while (time_msec() < now + TIME_UPDATE_INTERVAL * 3) {
191             pause();
192         }
193         lockfile_unlock(lockfile);
194     }
195 }
196
197 static void
198 run_lock_multiple(void)
199 {
200     struct lockfile *a, *b, *c, *dummy;
201
202     CHECK(lockfile_lock("a", 0, &a), 0);
203     CHECK(lockfile_lock("b", 0, &b), 0);
204     CHECK(lockfile_lock("c", 0, &c), 0);
205
206     lockfile_unlock(a);
207     CHECK(lockfile_lock("a", 0, &a), 0);
208     CHECK(lockfile_lock("a", 0, &dummy), EDEADLK);
209     lockfile_unlock(a);
210
211     lockfile_unlock(b);
212     CHECK(lockfile_lock("a", 0, &a), 0);
213
214     lockfile_unlock(c);
215     lockfile_unlock(a);
216 }
217
218 /* Checks that locking a dangling symlink works OK.  (It used to hang.) */
219 static void
220 run_lock_symlink(void)
221 {
222     struct lockfile *a, *b, *dummy;
223     struct stat s;
224
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);
233
234     CHECK(lockfile_lock("a", 0, &a), 0);
235     CHECK(lockfile_lock("a", 0, &dummy), EDEADLK);
236     CHECK(lockfile_lock("b", 0, &dummy), EDEADLK);
237     lockfile_unlock(a);
238
239     CHECK(lockfile_lock("b", 0, &b), 0);
240     CHECK(lockfile_lock("b", 0, &dummy), EDEADLK);
241     CHECK(lockfile_lock("a", 0, &dummy), EDEADLK);
242     lockfile_unlock(b);
243
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);
250 }
251
252 /* Checks that locking a file that is itself a symlink yields a lockfile in the
253  * directory that the symlink points to, named for the target of the
254  * symlink.
255  *
256  * (That is, if "a" is a symlink to "dir/b", then "a"'s lockfile is named
257  * "dir/.b.~lock".) */
258 static void
259 run_lock_symlink_to_dir(void)
260 {
261     struct lockfile *a, *dummy;
262     struct stat s;
263
264     /* Create a symlink "a" pointing to "dir/b". */
265     CHECK(mkdir("dir", 0700), 0);
266     CHECK(symlink("dir/b", "a"), 0);
267     CHECK(lstat("a", &s), 0);
268     CHECK(S_ISLNK(s.st_mode) != 0, 1);
269
270     /* Lock 'a'. */
271     CHECK(lockfile_lock("a", 0, &a), 0);
272     CHECK(lstat("dir/.b.~lock~", &s), 0);
273     CHECK(S_ISREG(s.st_mode) != 0, 1);
274     CHECK(lstat(".a.~lock~", &s), -1);
275     CHECK(errno, ENOENT);
276     CHECK(lockfile_lock("dir/b", 0, &dummy), EDEADLK);
277
278     lockfile_unlock(a);
279 }
280
281 static void
282 run_help(void)
283 {
284     size_t i;
285
286     printf("usage: %s TESTNAME\n"
287            "where TESTNAME is one of the following:\n",
288            program_name);
289     for (i = 0; tests[i].name; i++) {
290         fprintf(stderr, "\t%s\n", tests[i].name);
291     }
292 }
293
294 static const struct test tests[] = {
295 #define TEST(NAME) { #NAME, run_##NAME }
296     TEST(lock_and_unlock),
297     TEST(lock_and_unlock_twice),
298     TEST(lock_blocks_same_process),
299     TEST(lock_blocks_same_process_twice),
300     TEST(lock_blocks_other_process),
301     TEST(lock_twice_blocks_other_process),
302     TEST(lock_and_unlock_allows_other_process),
303     TEST(lock_timeout_gets_the_lock),
304     TEST(lock_timeout_runs_out),
305     TEST(lock_multiple),
306     TEST(lock_symlink),
307     TEST(lock_symlink_to_dir),
308     TEST(help),
309     { NULL, NULL }
310 #undef TEST
311 };
312
313 int
314 main(int argc, char *argv[])
315 {
316     extern struct vlog_module VLM_lockfile;
317     size_t i;
318
319     set_program_name(argv[0]);
320     vlog_set_levels(&VLM_lockfile, VLF_ANY_FACILITY, VLL_ERR);
321
322     if (argc != 2) {
323         ovs_fatal(0, "exactly one argument required; use \"%s help\" for help",
324                   program_name);
325         return 1;
326     }
327
328     for (i = 0; tests[i].name; i++) {
329         if (!strcmp(argv[1], tests[i].name)) {
330             int n_children;
331             int status;
332
333             (tests[i].function)();
334
335             n_children = 0;
336             while (wait(&status) > 0) {
337                 if (WIFEXITED(status) && WEXITSTATUS(status) == 11) {
338                     n_children++;
339                 } else {
340                     ovs_fatal(0, "child exited in unexpected way: %s",
341                               process_status_msg(status));
342                 }
343             }
344             if (errno != ECHILD) {
345                 ovs_fatal(errno, "wait");
346             }
347
348             printf("%s: success (%d child%s)\n",
349                    tests[i].name, n_children, n_children != 1 ? "ren" : "");
350             exit(0);
351         }
352     }
353     ovs_fatal(0, "unknown test \"%s\"; use \"%s help\" for help",
354               argv[1], program_name);
355 }
356