a8a615e9a6e4e7354652cb63054222b14898b519
[gnulib.git] / tests / test-flock.c
1 /* Test of flock() function.
2    Copyright (C) 2008-2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #include <config.h>
18
19 #include <sys/file.h>
20
21 #include "signature.h"
22 SIGNATURE_CHECK (flock, int, (int, int));
23
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <errno.h>
29
30 #define ASSERT(expr) \
31   do                                                                    \
32     {                                                                   \
33       if (!(expr))                                                      \
34         {                                                               \
35           fprintf (stderr, "%s:%d: assertion failed, errno = %d\n",     \
36                    __FILE__, __LINE__, errno);                          \
37           fflush (stderr);                                              \
38           abort ();                                                     \
39         }                                                               \
40     }                                                                   \
41   while (0)
42
43 static void
44 test_shared (const char *file, int fd)
45 {
46   /* Should be able to acquire several shared locks on a file, through
47    * different file table entries.
48    */
49   int fd2, r;
50
51   ASSERT (flock (fd, LOCK_SH) == 0);
52
53   fd2 = open (file, O_RDWR, 0644);
54   ASSERT (fd2 >= 0);
55
56   r = flock (fd2, LOCK_SH | LOCK_NB);
57   ASSERT (r == 0);              /* Was able to acquire a second shared lock. */
58
59   ASSERT (flock (fd, LOCK_UN) == 0);
60   ASSERT (close (fd2) == 0);
61 }
62
63 static void
64 test_exclusive (const char *file, int fd)
65 {
66   /* Should not be able to acquire more than one exclusive lock on a file. */
67   int fd2, r;
68
69   ASSERT (flock (fd, LOCK_EX) == 0);
70
71   fd2 = open (file, O_RDWR, 0644);
72   ASSERT (fd2 >= 0);
73
74   r = flock (fd2, LOCK_SH | LOCK_NB);
75   ASSERT (r == -1);             /* Was unable to acquire a second exclusive lock. */
76
77   ASSERT (flock (fd, LOCK_UN) == 0);
78   ASSERT (close (fd2) == 0);
79 }
80
81 int
82 main (int argc, char *argv[])
83 {
84   int fd;
85   const char *file = "test-flock.txt";
86
87   /* Open a non-empty file for testing. */
88   fd = open (file, O_RDWR | O_CREAT | O_TRUNC, 0644);
89   ASSERT (fd >= 0);
90   ASSERT (write (fd, "hello", 5) == 5);
91
92   /* Some impossible operation codes which should never be accepted. */
93   ASSERT (flock (fd, LOCK_SH | LOCK_EX) == -1);
94   ASSERT (errno == EINVAL);
95   ASSERT (flock (fd, LOCK_SH | LOCK_UN) == -1);
96   ASSERT (errno == EINVAL);
97   ASSERT (flock (fd, LOCK_EX | LOCK_UN) == -1);
98   ASSERT (errno == EINVAL);
99   ASSERT (flock (fd, 0) == -1);
100   ASSERT (errno == EINVAL);
101
102   test_shared (file, fd);
103   test_exclusive (file, fd);
104
105   /* Close and remove the test file. */
106   ASSERT (close (fd) == 0);
107   ASSERT (unlink (file) == 0);
108
109   return 0;
110 }