xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pipe/pipe15.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) 2023 SUSE LLC Marius Kittler <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This is a regression test for hangup on pipe operations. See
10*49cdfc7eSAndroid Build Coastguard Worker  * https://www.spinics.net/lists/linux-api/msg49762.html for
11*49cdfc7eSAndroid Build Coastguard Worker  * additional context. It tests that pipe operations do not block
12*49cdfc7eSAndroid Build Coastguard Worker  * indefinitely when going to the soft limit on the total size of
13*49cdfc7eSAndroid Build Coastguard Worker  * all pipes created by a single user.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
17*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_stdio.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static int pipe_count;
26*49cdfc7eSAndroid Build Coastguard Worker static int *pipes;
27*49cdfc7eSAndroid Build Coastguard Worker static char *buffer;
28*49cdfc7eSAndroid Build Coastguard Worker 
run(void)29*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker 	const int *const pipe = pipes + 2 * (pipe_count - 1);
32*49cdfc7eSAndroid Build Coastguard Worker 	const int buffer_size = SAFE_FCNTL(pipe[1], F_GETPIPE_SZ);
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Soft-limited buffer size: %d bytes", buffer_size);
35*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(1, pipe[1], buffer, buffer_size);
36*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_READ(1, pipe[0], buffer, buffer_size - 1);
37*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(1, pipe[1], buffer, 1);
38*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Pipe operation did not block");
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_READ(1, pipe[0], buffer, 2);
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)43*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker 	int pipe[2];
46*49cdfc7eSAndroid Build Coastguard Worker 	int page_size = getpagesize(), soft_limit;
47*49cdfc7eSAndroid Build Coastguard Worker 	struct rlimit nfd;
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(pipe);
50*49cdfc7eSAndroid Build Coastguard Worker 	const int buffer_size = SAFE_FCNTL(pipe[1], F_GETPIPE_SZ);
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(pipe[0]);
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(pipe[1]);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_SCANF("/proc/sys/fs/pipe-user-pages-soft", "%i", &soft_limit);
56*49cdfc7eSAndroid Build Coastguard Worker 	pipe_count = soft_limit * page_size / buffer_size;
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Soft limit for pipes: %i pages", soft_limit);
59*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Buffer size: %d byte", buffer_size);
60*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Creating %i pipes", pipe_count);
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_GETRLIMIT(RLIMIT_NOFILE, &nfd);
63*49cdfc7eSAndroid Build Coastguard Worker 	if (nfd.rlim_max < (unsigned long)pipe_count * 2 + 3)
64*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "NOFILE limit max too low: %lu < %i", nfd.rlim_max, pipe_count);
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	if (nfd.rlim_cur < nfd.rlim_max) {
67*49cdfc7eSAndroid Build Coastguard Worker 		nfd.rlim_cur = nfd.rlim_max;
68*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETRLIMIT(RLIMIT_NOFILE, &nfd);
69*49cdfc7eSAndroid Build Coastguard Worker 	}
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	buffer = SAFE_MALLOC(buffer_size);
72*49cdfc7eSAndroid Build Coastguard Worker 	pipes = SAFE_MALLOC(pipe_count * 2 * sizeof(int));
73*49cdfc7eSAndroid Build Coastguard Worker 	for (int i = 0; i < pipe_count; ++i)
74*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_PIPE(pipes + i * 2);
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)78*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
79*49cdfc7eSAndroid Build Coastguard Worker {
80*49cdfc7eSAndroid Build Coastguard Worker 	if (pipes) {
81*49cdfc7eSAndroid Build Coastguard Worker 		for (int i = 0; i < pipe_count * 2; i++) {
82*49cdfc7eSAndroid Build Coastguard Worker 			if (pipes[i] > 0)
83*49cdfc7eSAndroid Build Coastguard Worker 				SAFE_CLOSE(pipes[i]);
84*49cdfc7eSAndroid Build Coastguard Worker 		}
85*49cdfc7eSAndroid Build Coastguard Worker 		free(pipes);
86*49cdfc7eSAndroid Build Coastguard Worker 	}
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	if (buffer)
89*49cdfc7eSAndroid Build Coastguard Worker 		free(buffer);
90*49cdfc7eSAndroid Build Coastguard Worker }
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
93*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
94*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
95*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
96*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]){
97*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "46c4c9d1beb7"},
98*49cdfc7eSAndroid Build Coastguard Worker 	},
99*49cdfc7eSAndroid Build Coastguard Worker };
100