1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/wait.h>
#include <string.h>
#include <linux/limits.h>
#include "zdtmtst.h"
const char *test_doc = "Check that posix flocks are restored";
const char *test_author = "Qiang Huang <h.huangqiang@huawei.com>";
char *filename;
TEST_OPTION(filename, string, "file name", 1);
char file0[PATH_MAX];
char file1[PATH_MAX];
static int lock_reg(int fd, int cmd, int type, int whence,
off_t offset, off_t len)
{
struct flock lock;
lock.l_type = type; /* F_RDLCK, F_WRLCK, F_UNLCK */
lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */
lock.l_start = offset; /* byte offset, relative to l_whence */
lock.l_len = len; /* #bytes (0 means to EOF) */
errno = 0;
return fcntl(fd, cmd, &lock);
}
#define set_read_lock(fd, whence, offset, len) \
lock_reg(fd, F_SETLK, F_RDLCK, whence, offset, len)
#define set_write_lock(fd, whence, offset, len) \
lock_reg(fd, F_SETLK, F_WRLCK, whence, offset, len)
static int check_read_lock(int fd, int whence, off_t offset, off_t len)
{
struct flock lock;
int ret;
lock.l_type = F_RDLCK; /* F_RDLCK, F_WRLCK, F_UNLCK */
lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */
lock.l_start = offset; /* byte offset, relative to l_whence */
lock.l_len = len; /* #bytes (0 means to EOF) */
lock.l_pid = -1;
errno = 0;
ret = fcntl(fd, F_GETLK, &lock);
if (ret == -1) {
pr_perror("F_GETLK failed.");
return -1;
}
if (lock.l_pid == -1) {
/* Share lock should succeed. */
return 0;
}
fail("Read lock check failed.");
return -1;
}
static int check_write_lock(int fd, int whence, off_t offset, off_t len)
{
struct flock lock;
int ret;
pid_t ppid = getppid();
lock.l_type = F_WRLCK; /* F_RDLCK, F_WRLCK, F_UNLCK */
lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */
lock.l_start = offset; /* byte offset, relative to l_whence */
lock.l_len = len; /* #bytes (0 means to EOF) */
lock.l_pid = -1;
errno = 0;
ret = fcntl(fd, F_GETLK, &lock);
if (ret == -1) {
pr_perror("F_GETLK failed.");
return -1;
}
if (lock.l_pid == -1) {
fail("Write lock check failed.");
return -1;
}
/*
* It only succeed when the file lock's owner is exactly
* the same as the file lock was dumped.
*/
if (lock.l_pid == ppid)
return 0;
fail("Write lock check failed.");
return -1;
}
static int check_file_locks()
{
int fd_0, fd_1;
int ret0, ret1;
fd_0 = open(file0, O_RDWR | O_CREAT, 0644);
if (fd_0 < 0) {
pr_perror("Unable to open file %s", file0);
return -1;
}
ret0 = check_read_lock(fd_0, SEEK_SET, 0, 0);
fd_1 = open(file1, O_RDWR | O_CREAT, 0644);
if (fd_1 < 0) {
close(fd_0);
unlink(file0);
pr_perror("Unable to open file %s", file1);
return -1;
}
ret1 = check_write_lock(fd_1, SEEK_SET, 0, 0);
close(fd_0);
close(fd_1);
return ret0 | ret1;
}
int main(int argc, char **argv)
{
int fd_0, fd_1, ret;
pid_t pid;
test_init(argc, argv);
snprintf(file0, sizeof(file0), "%s.0", filename);
snprintf(file1, sizeof(file0), "%s.1", filename);
fd_0 = open(file0, O_RDWR | O_CREAT | O_EXCL, 0666);
if (fd_0 < 0) {
pr_perror("Unable to open file %s", file0);
return -1;
}
fd_1 = open(file1, O_RDWR | O_CREAT | O_EXCL, 0666);
if (fd_1 < 0) {
close(fd_0);
unlink(file0);
pr_perror("Unable to open file %s", file1);
return -1;
}
pid = fork();
if (pid < 0) {
pr_perror("Can't fork");
return -1;
}
if (pid == 0) { /* child will check father's file locks */
test_waitsig();
if (check_file_locks()) {
fail("Posix file lock check failed");
exit(1);
}
pass();
exit(0);
}
ret = set_read_lock(fd_0, SEEK_SET, 0, 0);
if (ret == -1) {
pr_perror("Failed to set read lock");
kill(pid, SIGTERM);
return -1;
}
ret = set_write_lock(fd_1, SEEK_SET, 0, 0);
if (ret == -1) {
pr_perror("Failed to set write lock");
kill(pid, SIGTERM);
return -1;
}
test_daemon();
test_waitsig();
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
close(fd_0);
close(fd_1);
unlink(file0);
unlink(file1);
return 0;
}
|