netlink-socket: Allow compiling on MSVC even without HAVE_NETLINK.
[cascardo/ovs.git] / tests / test-lockfile.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2014 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 #include "ovstest.h"
32
33 struct test {
34     const char *name;
35     void (*function)(void);
36 };
37
38 static void run_help(void);
39
40 #define CHECK(A, B) check(A, B, #A, #B, __FILE__, __LINE__)
41 static void
42 check(int a, int b,
43       const char *a_string, const char *b_string, const char *file, int line)
44 {
45     if (a != b) {
46         fprintf(stderr, "%s:%d: expected %s == %s but %d != %d\n",
47                 file, line, a_string, b_string, a, b);
48         fflush(stderr);
49         abort();
50     }
51 }
52
53 static void
54 run_lock_and_unlock(void)
55 {
56     struct lockfile *lockfile;
57
58     CHECK(lockfile_lock("file", &lockfile), 0);
59     lockfile_unlock(lockfile);
60 }
61
62 static void
63 run_lock_and_unlock_twice(void)
64 {
65     struct lockfile *lockfile;
66
67     CHECK(lockfile_lock("file", &lockfile), 0);
68     lockfile_unlock(lockfile);
69
70     CHECK(lockfile_lock("file", &lockfile), 0);
71     lockfile_unlock(lockfile);
72 }
73
74 static void
75 run_lock_blocks_same_process(void)
76 {
77     struct lockfile *lockfile;
78
79     CHECK(lockfile_lock("file", &lockfile), 0);
80     CHECK(lockfile_lock("file", &lockfile), EDEADLK);
81     lockfile_unlock(lockfile);
82 }
83
84 static void
85 run_lock_blocks_same_process_twice(void)
86 {
87     struct lockfile *lockfile;
88
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);
93 }
94
95 #ifndef _WIN32
96 static enum { PARENT, CHILD }
97 do_fork(void)
98 {
99     switch (fork()) {
100     case 0:
101         lockfile_postfork();
102         return CHILD;
103
104     default:
105         return PARENT;
106
107     case -1:
108         /* Error. */
109         ovs_fatal(errno, "fork failed");
110     }
111 }
112
113 static void
114 run_lock_blocks_other_process(void)
115 {
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;
121
122     CHECK(lockfile_lock("file", &lockfile), 0);
123     if (do_fork() == CHILD) {
124         lockfile_unlock(lockfile);
125         CHECK(lockfile_lock("file", &lockfile), EAGAIN);
126         exit(11);
127     }
128 }
129
130 static void
131 run_lock_twice_blocks_other_process(void)
132 {
133     struct lockfile *lockfile, *dummy;
134
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);
139         exit(11);
140     }
141 }
142
143 static void
144 run_lock_and_unlock_allows_other_process(void)
145 {
146     struct lockfile *lockfile;
147
148     CHECK(lockfile_lock("file", &lockfile), 0);
149     lockfile_unlock(lockfile);
150
151     if (do_fork() == CHILD) {
152         CHECK(lockfile_lock("file", &lockfile), 0);
153         exit(11);
154     }
155 }
156
157 /* Checks that locking a dangling symlink works OK.  (It used to hang.) */
158 static void
159 run_lock_symlink(void)
160 {
161     struct lockfile *a, *b, *dummy;
162     struct stat s;
163
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);
172
173     CHECK(lockfile_lock("a", &a), 0);
174     CHECK(lockfile_lock("a", &dummy), EDEADLK);
175     CHECK(lockfile_lock("b", &dummy), EDEADLK);
176     lockfile_unlock(a);
177
178     CHECK(lockfile_lock("b", &b), 0);
179     CHECK(lockfile_lock("b", &dummy), EDEADLK);
180     CHECK(lockfile_lock("a", &dummy), EDEADLK);
181     lockfile_unlock(b);
182
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);
189 }
190
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
193  * symlink.
194  *
195  * (That is, if "a" is a symlink to "dir/b", then "a"'s lockfile is named
196  * "dir/.b.~lock".) */
197 static void
198 run_lock_symlink_to_dir(void)
199 {
200     struct lockfile *a, *dummy;
201     struct stat s;
202
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);
208
209     /* Lock 'a'. */
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);
216
217     lockfile_unlock(a);
218 }
219 #endif /* _WIN32 */
220
221 static void
222 run_lock_multiple(void)
223 {
224     struct lockfile *a, *b, *c, *dummy;
225
226     CHECK(lockfile_lock("a", &a), 0);
227     CHECK(lockfile_lock("b", &b), 0);
228     CHECK(lockfile_lock("c", &c), 0);
229
230     lockfile_unlock(a);
231     CHECK(lockfile_lock("a", &a), 0);
232     CHECK(lockfile_lock("a", &dummy), EDEADLK);
233     lockfile_unlock(a);
234
235     lockfile_unlock(b);
236     CHECK(lockfile_lock("a", &a), 0);
237
238     lockfile_unlock(c);
239     lockfile_unlock(a);
240 }
241
242
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),
249 #ifndef _WIN32
250     TEST(lock_blocks_other_process),
251     TEST(lock_twice_blocks_other_process),
252     TEST(lock_and_unlock_allows_other_process),
253     TEST(lock_symlink),
254     TEST(lock_symlink_to_dir),
255 #endif /* _WIN32 */
256     TEST(lock_multiple),
257     TEST(help),
258     { NULL, NULL }
259 #undef TEST
260 };
261
262 static void
263 run_help(void)
264 {
265     size_t i;
266
267     printf("usage: %s TESTNAME\n"
268            "where TESTNAME is one of the following:\n",
269            program_name);
270     for (i = 0; tests[i].name; i++) {
271         fprintf(stderr, "\t%s\n", tests[i].name);
272     }
273 }
274
275 static void
276 test_lockfile_main(int argc, char *argv[])
277 {
278     size_t i;
279
280     set_program_name(argv[0]);
281     vlog_set_pattern(VLF_CONSOLE, "%c|%p|%m");
282     vlog_set_levels(NULL, VLF_SYSLOG, VLL_OFF);
283
284     if (argc != 2) {
285         ovs_fatal(0, "exactly one argument required; use \"%s help\" for help",
286                   program_name);
287     }
288
289     for (i = 0; tests[i].name; i++) {
290         if (!strcmp(argv[1], tests[i].name)) {
291             int n_children;
292             int status;
293
294             (tests[i].function)();
295
296             n_children = 0;
297 #ifndef _WIN32
298             while (wait(&status) > 0) {
299                 if (WIFEXITED(status) && WEXITSTATUS(status) == 11) {
300                     n_children++;
301                 } else {
302                     ovs_fatal(0, "child exited in unexpected way: %s",
303                               process_status_msg(status));
304                 }
305             }
306             if (errno != ECHILD) {
307                 ovs_fatal(errno, "wait");
308             }
309 #endif /* _WIN32 */
310
311             printf("%s: success (%d child%s)\n",
312                    tests[i].name, n_children, n_children != 1 ? "ren" : "");
313             exit(0);
314         }
315     }
316     ovs_fatal(0, "unknown test \"%s\"; use \"%s help\" for help",
317               argv[1], program_name);
318 }
319
320 OVSTEST_REGISTER("test-lockfile", test_lockfile_main);