xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/vmsplice/vmsplice04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2019 SUSE LLC
4  * Author: Jorik Cronenberg <[email protected]>
5  *
6  * Test vmsplice() to a full pipe with SPLICE_F_NONBLOCK and without.
7  *
8  * With SPLICE_F_NONBLOCK vmsplice() should return with errno EAGAIN
9  * Without SPLICE_F_NONBLOCK it should block.
10  */
11 
12 #define _GNU_SOURCE
13 
14 #include "tst_test.h"
15 #include "lapi/vmsplice.h"
16 #include "lapi/fcntl.h"
17 #include <stdlib.h>
18 
19 static int pipes[2];
20 static ssize_t pipe_max_size;
21 static char *write_buffer;
22 static struct iovec iov;
23 
vmsplice_test(void)24 static void vmsplice_test(void)
25 {
26 	int status;
27 	int pid;
28 
29 	TEST(vmsplice(pipes[1], &iov, 1, SPLICE_F_NONBLOCK));
30 
31 	if (TST_RET < 0 && TST_ERR == EAGAIN) {
32 		tst_res(TPASS | TTERRNO,
33 		    "vmsplice(..., SPLICE_F_NONBLOCK) failed as expected");
34 	} else if (TST_RET < 0) {
35 		tst_res(TFAIL | TTERRNO,
36 		    "vmsplice(..., SPLICE_F_NONBLOCK) shall fail with EAGAIN");
37 	} else {
38 		tst_res(TFAIL,
39 		    "vmsplice(..., SPLICE_F_NONBLOCK) wrote to a full pipe");
40 	}
41 
42 	pid = SAFE_FORK();
43 	if (!pid) {
44 		TEST(vmsplice(pipes[1], &iov, 1, 0));
45 		if (TST_RET < 0)
46 			tst_res(TFAIL | TTERRNO, "vmsplice(..., 0) failed");
47 		else
48 			tst_res(TFAIL,
49 			    "vmsplice(..., 0) wrote to a full pipe");
50 		exit(0);
51 	}
52 
53 	if (TST_PROCESS_STATE_WAIT(pid, 'S', 1000) < 0)
54 		return;
55 	else
56 		tst_res(TPASS, "vmsplice(..., 0) blocked");
57 
58 	SAFE_KILL(pid, SIGKILL);
59 	SAFE_WAIT(&status);
60 }
61 
cleanup(void)62 static void cleanup(void)
63 {
64 	if (pipes[1] > 0)
65 		SAFE_CLOSE(pipes[1]);
66 	if (pipes[0] > 0)
67 		SAFE_CLOSE(pipes[0]);
68 }
69 
setup(void)70 static void setup(void)
71 {
72 	SAFE_PIPE(pipes);
73 
74 	pipe_max_size = SAFE_FCNTL(pipes[1], F_GETPIPE_SZ);
75 	write_buffer = tst_alloc(pipe_max_size);
76 
77 	iov.iov_base = write_buffer;
78 	iov.iov_len = pipe_max_size;
79 
80 	TEST(vmsplice(pipes[1], &iov, 1, 0));
81 	if (TST_RET < 0) {
82 		tst_brk(TBROK | TTERRNO,
83 		    "Initial vmsplice() to fill pipe failed");
84 	}
85 }
86 
87 static struct tst_test test = {
88 	.setup = setup,
89 	.cleanup = cleanup,
90 	.test_all = vmsplice_test,
91 	.forks_child = 1,
92 };
93