xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fcntl/fcntl34.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2016 Oracle and/or its affiliates. All Rights Reserved.
4  * Author: Alexey Kodanev <[email protected]>
5  */
6 
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <pthread.h>
11 #include <sched.h>
12 
13 #include "fcntl_common.h"
14 #include "tst_safe_pthread.h"
15 
16 static int thread_cnt;
17 static const int max_thread_cnt = 32;
18 static const char fname[] = "tst_ofd_locks";
19 const int writes_num = 100;
20 const int write_size = 4096;
21 
setup(void)22 static void setup(void)
23 {
24 	thread_cnt = tst_ncpus_conf() * 3;
25 	if (thread_cnt > max_thread_cnt)
26 		thread_cnt = max_thread_cnt;
27 }
28 
spawn_threads(pthread_t * id,void * (* thread_fn)(void *))29 static void spawn_threads(pthread_t *id, void *(*thread_fn)(void *))
30 {
31 	intptr_t i;
32 
33 	tst_res(TINFO, "spawning '%d' threads", thread_cnt);
34 	for (i = 0; i < thread_cnt; ++i)
35 		SAFE_PTHREAD_CREATE(id + i, NULL, thread_fn, (void *)i);
36 }
37 
wait_threads(pthread_t * id)38 static void wait_threads(pthread_t *id)
39 {
40 	int i;
41 
42 	tst_res(TINFO, "waiting for '%d' threads", thread_cnt);
43 	for (i = 0; i < thread_cnt; ++i)
44 		SAFE_PTHREAD_JOIN(id[i], NULL);
45 }
46 
thread_fn_01(void * arg)47 void *thread_fn_01(void *arg)
48 {
49 	int i;
50 	unsigned char buf[write_size];
51 	int fd = SAFE_OPEN(fname, O_RDWR);
52 
53 	memset(buf, (intptr_t)arg, write_size);
54 
55 	struct flock lck = {
56 		.l_whence = SEEK_SET,
57 		.l_start  = 0,
58 		.l_len    = 1,
59 	};
60 
61 	for (i = 0; i < writes_num; ++i) {
62 		lck.l_type = F_WRLCK;
63 		FCNTL_COMPAT(fd, F_OFD_SETLKW, &lck);
64 
65 		SAFE_LSEEK(fd, 0, SEEK_END);
66 		SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, write_size);
67 
68 		lck.l_type = F_UNLCK;
69 		FCNTL_COMPAT(fd, F_OFD_SETLKW, &lck);
70 
71 		sched_yield();
72 	}
73 
74 	SAFE_CLOSE(fd);
75 
76 	return NULL;
77 }
78 
test01(void)79 static void test01(void)
80 {
81 	intptr_t i;
82 	int k;
83 	pthread_t id[thread_cnt];
84 	int res[thread_cnt];
85 	unsigned char buf[write_size];
86 
87 	tst_res(TINFO, "write to a file inside threads with OFD locks");
88 
89 	int fd = SAFE_OPEN(fname, O_CREAT | O_TRUNC | O_RDWR, 0600);
90 
91 	memset(res, 0, sizeof(res));
92 
93 	spawn_threads(id, thread_fn_01);
94 	wait_threads(id);
95 
96 	tst_res(TINFO, "verifying file's data");
97 	SAFE_LSEEK(fd, 0, SEEK_SET);
98 	for (i = 0; i < writes_num * thread_cnt; ++i) {
99 		SAFE_READ(1, fd, buf, write_size);
100 
101 		if (buf[0] >= thread_cnt) {
102 			tst_res(TFAIL, "unexpected data read");
103 			return;
104 		}
105 
106 		++res[buf[0]];
107 
108 		for (k = 1; k < write_size; ++k) {
109 			if (buf[0] != buf[k]) {
110 				tst_res(TFAIL, "unexpected data read");
111 				return;
112 			}
113 		}
114 	}
115 
116 	for (i = 0; i < thread_cnt; ++i) {
117 		if (res[i] != writes_num) {
118 			tst_res(TFAIL, "corrupted data found");
119 			return;
120 		}
121 	}
122 	SAFE_CLOSE(fd);
123 
124 	tst_res(TPASS, "OFD locks synchronized access between threads");
125 }
126 
127 static struct tst_test test = {
128 	.min_kver = "3.15.0",
129 	.needs_tmpdir = 1,
130 	.test_all = test01,
131 	.setup = setup
132 };
133