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