Merge branch 'mainstream'
[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", &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", &lockfile), 0);
67     lockfile_unlock(lockfile);
68
69     CHECK(lockfile_lock("file", &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", &lockfile), 0);
79     CHECK(lockfile_lock("file", &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", &lockfile), 0);
89     CHECK(lockfile_lock("file", &lockfile), EDEADLK);
90     CHECK(lockfile_lock("file", &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         lockfile_postfork();
100         return CHILD;
101
102     default:
103         return PARENT;
104
105     case -1:
106         /* Error. */
107         ovs_fatal(errno, "fork failed");
108     }
109 }
110
111 static void
112 run_lock_blocks_other_process(void)
113 {
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;
119
120     CHECK(lockfile_lock("file", &lockfile), 0);
121     if (do_fork() == CHILD) {
122         lockfile_unlock(lockfile);
123         CHECK(lockfile_lock("file", &lockfile), EAGAIN);
124         exit(11);
125     }
126 }
127
128 static void
129 run_lock_twice_blocks_other_process(void)
130 {
131     struct lockfile *lockfile, *dummy;
132
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);
137         exit(11);
138     }
139 }
140
141 static void
142 run_lock_and_unlock_allows_other_process(void)
143 {
144     struct lockfile *lockfile;
145
146     CHECK(lockfile_lock("file", &lockfile), 0);
147     lockfile_unlock(lockfile);
148
149     if (do_fork() == CHILD) {
150         CHECK(lockfile_lock("file", &lockfile), 0);
151         exit(11);
152     }
153 }
154
155 static void
156 run_lock_multiple(void)
157 {
158     struct lockfile *a, *b, *c, *dummy;
159
160     CHECK(lockfile_lock("a", &a), 0);
161     CHECK(lockfile_lock("b", &b), 0);
162     CHECK(lockfile_lock("c", &c), 0);
163
164     lockfile_unlock(a);
165     CHECK(lockfile_lock("a", &a), 0);
166     CHECK(lockfile_lock("a", &dummy), EDEADLK);
167     lockfile_unlock(a);
168
169     lockfile_unlock(b);
170     CHECK(lockfile_lock("a", &a), 0);
171
172     lockfile_unlock(c);
173     lockfile_unlock(a);
174 }
175
176 /* Checks that locking a dangling symlink works OK.  (It used to hang.) */
177 static void
178 run_lock_symlink(void)
179 {
180     struct lockfile *a, *b, *dummy;
181     struct stat s;
182
183     /* Create a symlink .a.~lock~ pointing to .b.~lock~. */
184     CHECK(symlink(".b.~lock~", ".a.~lock~"), 0);
185     CHECK(lstat(".a.~lock~", &s), 0);
186     CHECK(S_ISLNK(s.st_mode) != 0, 1);
187     CHECK(stat(".a.~lock~", &s), -1);
188     CHECK(errno, ENOENT);
189     CHECK(stat(".b.~lock~", &s), -1);
190     CHECK(errno, ENOENT);
191
192     CHECK(lockfile_lock("a", &a), 0);
193     CHECK(lockfile_lock("a", &dummy), EDEADLK);
194     CHECK(lockfile_lock("b", &dummy), EDEADLK);
195     lockfile_unlock(a);
196
197     CHECK(lockfile_lock("b", &b), 0);
198     CHECK(lockfile_lock("b", &dummy), EDEADLK);
199     CHECK(lockfile_lock("a", &dummy), EDEADLK);
200     lockfile_unlock(b);
201
202     CHECK(lstat(".a.~lock~", &s), 0);
203     CHECK(S_ISLNK(s.st_mode) != 0, 1);
204     CHECK(stat(".a.~lock~", &s), 0);
205     CHECK(S_ISREG(s.st_mode) != 0, 1);
206     CHECK(stat(".b.~lock~", &s), 0);
207     CHECK(S_ISREG(s.st_mode) != 0, 1);
208 }
209
210 /* Checks that locking a file that is itself a symlink yields a lockfile in the
211  * directory that the symlink points to, named for the target of the
212  * symlink.
213  *
214  * (That is, if "a" is a symlink to "dir/b", then "a"'s lockfile is named
215  * "dir/.b.~lock".) */
216 static void
217 run_lock_symlink_to_dir(void)
218 {
219     struct lockfile *a, *dummy;
220     struct stat s;
221
222     /* Create a symlink "a" pointing to "dir/b". */
223     CHECK(mkdir("dir", 0700), 0);
224     CHECK(symlink("dir/b", "a"), 0);
225     CHECK(lstat("a", &s), 0);
226     CHECK(S_ISLNK(s.st_mode) != 0, 1);
227
228     /* Lock 'a'. */
229     CHECK(lockfile_lock("a", &a), 0);
230     CHECK(lstat("dir/.b.~lock~", &s), 0);
231     CHECK(S_ISREG(s.st_mode) != 0, 1);
232     CHECK(lstat(".a.~lock~", &s), -1);
233     CHECK(errno, ENOENT);
234     CHECK(lockfile_lock("dir/b", &dummy), EDEADLK);
235
236     lockfile_unlock(a);
237 }
238
239 static void
240 run_help(void)
241 {
242     size_t i;
243
244     printf("usage: %s TESTNAME\n"
245            "where TESTNAME is one of the following:\n",
246            program_name);
247     for (i = 0; tests[i].name; i++) {
248         fprintf(stderr, "\t%s\n", tests[i].name);
249     }
250 }
251
252 static const struct test tests[] = {
253 #define TEST(NAME) { #NAME, run_##NAME }
254     TEST(lock_and_unlock),
255     TEST(lock_and_unlock_twice),
256     TEST(lock_blocks_same_process),
257     TEST(lock_blocks_same_process_twice),
258     TEST(lock_blocks_other_process),
259     TEST(lock_twice_blocks_other_process),
260     TEST(lock_and_unlock_allows_other_process),
261     TEST(lock_multiple),
262     TEST(lock_symlink),
263     TEST(lock_symlink_to_dir),
264     TEST(help),
265     { NULL, NULL }
266 #undef TEST
267 };
268
269 int
270 main(int argc, char *argv[])
271 {
272     size_t i;
273
274     set_program_name(argv[0]);
275     vlog_set_pattern(VLF_CONSOLE, "%c|%p|%m");
276     vlog_set_levels(NULL, VLF_SYSLOG, VLL_OFF);
277
278     if (argc != 2) {
279         ovs_fatal(0, "exactly one argument required; use \"%s help\" for help",
280                   program_name);
281         return 1;
282     }
283
284     for (i = 0; tests[i].name; i++) {
285         if (!strcmp(argv[1], tests[i].name)) {
286             int n_children;
287             int status;
288
289             (tests[i].function)();
290
291             n_children = 0;
292             while (wait(&status) > 0) {
293                 if (WIFEXITED(status) && WEXITSTATUS(status) == 11) {
294                     n_children++;
295                 } else {
296                     ovs_fatal(0, "child exited in unexpected way: %s",
297                               process_status_msg(status));
298                 }
299             }
300             if (errno != ECHILD) {
301                 ovs_fatal(errno, "wait");
302             }
303
304             printf("%s: success (%d child%s)\n",
305                    tests[i].name, n_children, n_children != 1 ? "ren" : "");
306             exit(0);
307         }
308     }
309     ovs_fatal(0, "unknown test \"%s\"; use \"%s help\" for help",
310               argv[1], program_name);
311 }
312