xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fcntl/fcntl37.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Yang Xu <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  * Test basic error handling for fcntl(2) using F_SETPIPE_SZ, F_GETPIPE_SZ
7*49cdfc7eSAndroid Build Coastguard Worker  * argument.
8*49cdfc7eSAndroid Build Coastguard Worker  * 1)fcntl fails with EINVAL when cmd is F_SETPIPE_SZ and the arg is
9*49cdfc7eSAndroid Build Coastguard Worker  * beyond 1<<31.
10*49cdfc7eSAndroid Build Coastguard Worker  * 2)fcntl fails with EBUSY when cmd is F_SETPIPE_SZ and the arg is smaller
11*49cdfc7eSAndroid Build Coastguard Worker  * than the amount of the current used buffer space.
12*49cdfc7eSAndroid Build Coastguard Worker  * 3)fcntl fails with EPERM when cmd is F_SETPIPE_SZ and the arg is over
13*49cdfc7eSAndroid Build Coastguard Worker  * /proc/sys/fs/pipe-max-size limit under unprivileged users.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/capability.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker static int fds[2];
25*49cdfc7eSAndroid Build Coastguard Worker static unsigned int invalid_value, half_value, sys_value;
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
28*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int *setvalue;
29*49cdfc7eSAndroid Build Coastguard Worker 	int exp_err;
30*49cdfc7eSAndroid Build Coastguard Worker 	char *message;
31*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
32*49cdfc7eSAndroid Build Coastguard Worker 	{&invalid_value, EINVAL, "F_SETPIPE_SZ and size is beyond 1<<31"},
33*49cdfc7eSAndroid Build Coastguard Worker 	{&half_value, EBUSY, "F_SETPIPE_SZ and size < data stored in pipe"},
34*49cdfc7eSAndroid Build Coastguard Worker 	{&sys_value, EPERM, "F_SETPIPE_SZ and size is over limit for unpriviledged user"},
35*49cdfc7eSAndroid Build Coastguard Worker };
36*49cdfc7eSAndroid Build Coastguard Worker 
verify_fcntl(unsigned int n)37*49cdfc7eSAndroid Build Coastguard Worker static void verify_fcntl(unsigned int n)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "%s", tc->message);
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(fds[1], F_SETPIPE_SZ, *(tc->setvalue)));
44*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
45*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "F_SETPIPE_SZ succeed and return %ld", TST_RET);
46*49cdfc7eSAndroid Build Coastguard Worker 		return;
47*49cdfc7eSAndroid Build Coastguard Worker 	}
48*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == tc->exp_err)
49*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "F_SETPIPE_SZ failed as expected");
50*49cdfc7eSAndroid Build Coastguard Worker 	else
51*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "F_SETPIPE_SZ failed expected %s got",
52*49cdfc7eSAndroid Build Coastguard Worker 				tst_strerrno(tc->exp_err));
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)55*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker 	char *wrbuf;
58*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int orig_value;
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(fds);
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(fds[1], F_GETPIPE_SZ));
63*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == EINVAL)
64*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "kernel doesn't support F_GET/SETPIPE_SZ");
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	orig_value = TST_RET;
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	wrbuf = SAFE_MALLOC(orig_value);
69*49cdfc7eSAndroid Build Coastguard Worker 	memset(wrbuf, 'x', orig_value);
70*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, fds[1], wrbuf, orig_value);
71*49cdfc7eSAndroid Build Coastguard Worker 	free(wrbuf);
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_SCANF("/proc/sys/fs/pipe-max-size", "%d", &sys_value);
74*49cdfc7eSAndroid Build Coastguard Worker 	sys_value++;
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 	half_value = orig_value / 2;
77*49cdfc7eSAndroid Build Coastguard Worker 	invalid_value = (1U << 31) + 1;
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)80*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
81*49cdfc7eSAndroid Build Coastguard Worker {
82*49cdfc7eSAndroid Build Coastguard Worker 	if (fds[0] > 0)
83*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fds[0]);
84*49cdfc7eSAndroid Build Coastguard Worker 	if (fds[1] > 0)
85*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fds[1]);
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
89*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
90*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
91*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
92*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_fcntl,
93*49cdfc7eSAndroid Build Coastguard Worker 	.caps = (struct tst_cap []) {
94*49cdfc7eSAndroid Build Coastguard Worker 		TST_CAP(TST_CAP_DROP, CAP_SYS_RESOURCE),
95*49cdfc7eSAndroid Build Coastguard Worker 		{}
96*49cdfc7eSAndroid Build Coastguard Worker 	},
97*49cdfc7eSAndroid Build Coastguard Worker };
98