-
Notifications
You must be signed in to change notification settings - Fork 996
/
fcntl34.c
150 lines (121 loc) · 3.31 KB
/
fcntl34.c
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
/*
* Copyright (c) 2016 Oracle and/or its affiliates. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it would be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Alexey Kodanev <alexey.kodanev@oracle.com>
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <sched.h>
#include "lapi/fcntl.h"
#include "tst_test.h"
static int thread_cnt;
static const int max_thread_cnt = 32;
static const char fname[] = "tst_ofd_locks";
const int writes_num = 100;
const int write_size = 4096;
static void setup(void)
{
thread_cnt = tst_ncpus_conf() * 3;
if (thread_cnt > max_thread_cnt)
thread_cnt = max_thread_cnt;
}
static void spawn_threads(pthread_t *id, void *(*thread_fn)(void *))
{
intptr_t i;
tst_res(TINFO, "spawning '%d' threads", thread_cnt);
for (i = 0; i < thread_cnt; ++i)
SAFE_PTHREAD_CREATE(id + i, NULL, thread_fn, (void *)i);
}
static void wait_threads(pthread_t *id)
{
int i;
tst_res(TINFO, "waiting for '%d' threads", thread_cnt);
for (i = 0; i < thread_cnt; ++i)
SAFE_PTHREAD_JOIN(id[i], NULL);
}
void *thread_fn_01(void *arg)
{
int i;
unsigned char buf[write_size];
int fd = SAFE_OPEN(fname, O_RDWR);
memset(buf, (intptr_t)arg, write_size);
struct flock64 lck = {
.l_whence = SEEK_SET,
.l_start = 0,
.l_len = 1,
};
for (i = 0; i < writes_num; ++i) {
lck.l_type = F_WRLCK;
if (fcntl(fd, F_OFD_SETLKW, &lck) == -1)
tst_brk(TBROK | TERRNO, "fcntl() failed");
SAFE_LSEEK(fd, 0, SEEK_END);
SAFE_WRITE(1, fd, buf, write_size);
lck.l_type = F_UNLCK;
if (fcntl(fd, F_OFD_SETLKW, &lck) == -1)
tst_brk(TBROK | TERRNO, "fcntl() failed");
sched_yield();
}
SAFE_CLOSE(fd);
return NULL;
}
static void test01(void)
{
intptr_t i;
int k;
pthread_t id[thread_cnt];
int res[thread_cnt];
unsigned char buf[write_size];
tst_res(TINFO, "write to a file inside threads with OFD locks");
int fd = SAFE_OPEN(fname, O_CREAT | O_TRUNC | O_RDWR, 0600);
memset(res, 0, sizeof(res));
spawn_threads(id, thread_fn_01);
wait_threads(id);
tst_res(TINFO, "verifying file's data");
SAFE_LSEEK(fd, 0, SEEK_SET);
for (i = 0; i < writes_num * thread_cnt; ++i) {
SAFE_READ(1, fd, buf, write_size);
if (buf[0] >= thread_cnt) {
tst_res(TFAIL, "unexpected data read");
return;
}
++res[buf[0]];
for (k = 1; k < write_size; ++k) {
if (buf[0] != buf[k]) {
tst_res(TFAIL, "unexpected data read");
return;
}
}
}
for (i = 0; i < thread_cnt; ++i) {
if (res[i] != writes_num) {
tst_res(TFAIL, "corrupted data found");
return;
}
}
SAFE_CLOSE(fd);
tst_res(TPASS, "OFD locks synchronized access between threads");
}
static struct tst_test test = {
.tid = "fcntl34",
.min_kver = "3.15.0",
.needs_tmpdir = 1,
.test_all = test01,
.setup = setup
};