xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fcntl/fcntl30.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2014 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Xiaoguang Wang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Verify that, fetching and changing the capacity of a pipe works as
12*49cdfc7eSAndroid Build Coastguard Worker  * expected with fcntl(2) syscall using F_GETPIPE_SZ, F_SETPIPE_SZ arguments.
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
16*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker static int fds[2];
19*49cdfc7eSAndroid Build Coastguard Worker static int max_size_unpriv;
20*49cdfc7eSAndroid Build Coastguard Worker 
run(void)21*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
22*49cdfc7eSAndroid Build Coastguard Worker {
23*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(fds);
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_POSITIVE(fcntl(fds[1], F_GETPIPE_SZ));
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_POSITIVE(fcntl(fds[1], F_SETPIPE_SZ, max_size_unpriv));
28*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_POSITIVE(fcntl(fds[1], F_GETPIPE_SZ));
29*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EXPR(TST_RET >= max_size_unpriv,
30*49cdfc7eSAndroid Build Coastguard Worker 				"new pipe size (%ld) >= requested size (%d)",
31*49cdfc7eSAndroid Build Coastguard Worker 				TST_RET, max_size_unpriv);
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fds[0]);
34*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fds[1]);
35*49cdfc7eSAndroid Build Coastguard Worker }
36*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)37*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_SCANF("/proc/sys/fs/pipe-max-size", "%d", &max_size_unpriv);
40*49cdfc7eSAndroid Build Coastguard Worker }
41*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)42*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker 	if (fds[0] > 0)
45*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fds[0]);
46*49cdfc7eSAndroid Build Coastguard Worker 	if (fds[1] > 0)
47*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fds[1]);
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
51*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
52*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
53*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup
54*49cdfc7eSAndroid Build Coastguard Worker };
55