|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
1 | 2 | /* |
2 | 3 | * Copyright (c) 2014 Fujitsu Ltd. |
3 | 4 | * Author: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com> |
4 | | - * |
5 | | - * This program is free software; you can redistribute it and/or modify it |
6 | | - * under the terms of version 2 of the GNU General Public License as |
7 | | - * published by the Free Software Foundation. |
8 | | - * |
9 | | - * This program is distributed in the hope that it would be useful, but |
10 | | - * WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
12 | | - * |
13 | | - * You should have received a copy of the GNU General Public License along |
14 | | - * with this program; if not, write the Free Software Foundation, Inc., |
15 | | - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 5 | + * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com> |
16 | 6 | */ |
17 | 7 |
|
18 | | -/* |
19 | | - * Description: |
20 | | - * Verify that, |
21 | | - * Basic test for fcntl(2) using F_SETPIPE_SZ, F_GETPIPE_SZ argument. |
| 8 | +/*\ |
| 9 | + * [Description] |
| 10 | + * |
| 11 | + * Verify that, fetching and changing the capacity of a pipe works as |
| 12 | + * expected with fcntl(2) syscall using F_GETPIPE_SZ, F_SETPIPE_SZ arguments. |
22 | 13 | */ |
23 | 14 |
|
24 | | - |
25 | | -#include <stdio.h> |
26 | | -#include <errno.h> |
27 | | -#include <unistd.h> |
28 | | -#include <string.h> |
29 | | -#include <signal.h> |
30 | | -#include <sys/types.h> |
31 | | -#include <pwd.h> |
32 | | - |
33 | | -#include "test.h" |
34 | | -#include "safe_macros.h" |
| 15 | +#include "tst_test.h" |
35 | 16 | #include "lapi/fcntl.h" |
36 | 17 |
|
37 | | -char *TCID = "fcntl30"; |
38 | | -int TST_TOTAL = 1; |
39 | | - |
40 | | -static void setup(void); |
41 | | -static void cleanup(void); |
| 18 | +static int fds[2]; |
| 19 | +static int max_size_unpriv; |
42 | 20 |
|
43 | | -int main(int ac, char **av) |
| 21 | +static void run(void) |
44 | 22 | { |
45 | | - int lc; |
46 | | - int pipe_fds[2], test_fd; |
47 | | - int orig_pipe_size, new_pipe_size; |
| 23 | + SAFE_PIPE(fds); |
48 | 24 |
|
| 25 | + TST_EXP_POSITIVE(fcntl(fds[1], F_GETPIPE_SZ)); |
49 | 26 |
|
50 | | - tst_parse_opts(ac, av, NULL, NULL); |
| 27 | + TST_EXP_POSITIVE(fcntl(fds[1], F_SETPIPE_SZ, max_size_unpriv)); |
| 28 | + TST_EXP_POSITIVE(fcntl(fds[1], F_GETPIPE_SZ)); |
| 29 | + TST_EXP_EXPR(TST_RET >= max_size_unpriv, |
| 30 | + "new pipe size (%ld) >= requested size (%d)", |
| 31 | + TST_RET, max_size_unpriv); |
51 | 32 |
|
52 | | - setup(); |
53 | | - |
54 | | - for (lc = 0; TEST_LOOPING(lc); lc++) { |
55 | | - tst_count = 0; |
56 | | - |
57 | | - SAFE_PIPE(cleanup, pipe_fds); |
58 | | - test_fd = pipe_fds[1]; |
59 | | - |
60 | | - TEST(fcntl(test_fd, F_GETPIPE_SZ)); |
61 | | - if (TEST_RETURN < 0) { |
62 | | - tst_brkm(TFAIL | TTERRNO, cleanup, |
63 | | - "fcntl get pipe size failed"); |
64 | | - } |
65 | | - |
66 | | - orig_pipe_size = TEST_RETURN; |
67 | | - new_pipe_size = orig_pipe_size * 2; |
68 | | - TEST(fcntl(test_fd, F_SETPIPE_SZ, new_pipe_size)); |
69 | | - if (TEST_RETURN < 0) { |
70 | | - tst_brkm(TFAIL | TTERRNO, cleanup, |
71 | | - "fcntl test F_SETPIPE_SZ failed"); |
72 | | - } |
73 | | - |
74 | | - TEST(fcntl(test_fd, F_GETPIPE_SZ)); |
75 | | - if (TEST_RETURN < 0) { |
76 | | - tst_brkm(TFAIL | TTERRNO, cleanup, |
77 | | - "fcntl test F_GETPIPE_SZ failed"); |
78 | | - } |
79 | | - tst_resm(TINFO, "orig_pipe_size: %d new_pipe_size: %d", |
80 | | - orig_pipe_size, new_pipe_size); |
81 | | - if (TEST_RETURN >= new_pipe_size) { |
82 | | - tst_resm(TPASS, "fcntl test F_GETPIPE_SZ and F_SETPIPE_SZ passed"); |
83 | | - } else { |
84 | | - tst_resm(TFAIL, "fcntl test F_GETPIPE_SZ and F_SETPIPE_SZ failed"); |
85 | | - } |
86 | | - SAFE_CLOSE(cleanup, pipe_fds[0]); |
87 | | - SAFE_CLOSE(cleanup, pipe_fds[1]); |
88 | | - } |
89 | | - |
90 | | - cleanup(); |
91 | | - tst_exit(); |
| 33 | + SAFE_CLOSE(fds[0]); |
| 34 | + SAFE_CLOSE(fds[1]); |
92 | 35 | } |
93 | 36 |
|
94 | 37 | static void setup(void) |
95 | 38 | { |
96 | | - tst_sig(NOFORK, DEF_HANDLER, cleanup); |
97 | | - |
98 | | - TEST_PAUSE; |
| 39 | + SAFE_FILE_SCANF("/proc/sys/fs/pipe-max-size", "%d", &max_size_unpriv); |
99 | 40 | } |
100 | 41 |
|
101 | 42 | static void cleanup(void) |
102 | 43 | { |
| 44 | + if (fds[0] > 0) |
| 45 | + SAFE_CLOSE(fds[0]); |
| 46 | + if (fds[1] > 0) |
| 47 | + SAFE_CLOSE(fds[1]); |
103 | 48 | } |
| 49 | + |
| 50 | +static struct tst_test test = { |
| 51 | + .test_all = run, |
| 52 | + .setup = setup, |
| 53 | + .cleanup = cleanup |
| 54 | +}; |
0 commit comments