• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
4  */
5 
6 /*\
7  * [Description]
8  *
9  * This test verifies the following shutdown() functionalities:
10  *
11  * - SHUT_RD should enable send() ops but disable recv() ops
12  * - SHUT_WR should enable recv() ops but disable send() ops
13  * - SHUT_RDWR should disable both recv() and send() ops
14  */
15 
16 #include "tst_test.h"
17 #include "tst_safe_net.h"
18 
19 #define MSGSIZE 4
20 #define SOCKETFILE "socket"
21 
22 #define OP_DESC(x) .shutdown_op = x, .desc = #x
23 static struct tcase {
24 	int shutdown_op;
25 	int recv_flag;
26 	int recv_err;
27 	int send_flag;
28 	int send_err;
29 	char *desc;
30 } tcases[] = {
31 	{OP_DESC(SHUT_RD)},
32 	{OP_DESC(SHUT_WR), .recv_flag = MSG_DONTWAIT, .recv_err = EWOULDBLOCK,
33 		.send_flag = MSG_NOSIGNAL, .send_err = EPIPE},
34 	{OP_DESC(SHUT_RDWR), .send_flag = MSG_NOSIGNAL, .send_err = EPIPE}
35 };
36 
37 static struct sockaddr_un *sock_addr;
38 
run_server(void)39 static void run_server(void)
40 {
41 	int server_sock;
42 
43 	server_sock = SAFE_SOCKET(sock_addr->sun_family, SOCK_STREAM, 0);
44 
45 	SAFE_BIND(server_sock,
46 		(struct sockaddr *)sock_addr,
47 		sizeof(struct sockaddr_un));
48 	SAFE_LISTEN(server_sock, 10);
49 
50 	tst_res(TINFO, "Running server on socket file");
51 
52 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
53 
54 	SAFE_CLOSE(server_sock);
55 	SAFE_UNLINK(SOCKETFILE);
56 }
57 
start_test(void)58 static int start_test(void)
59 {
60 	int client_sock;
61 
62 	if (!SAFE_FORK()) {
63 		run_server();
64 		_exit(0);
65 	}
66 
67 	TST_CHECKPOINT_WAIT(0);
68 
69 	tst_res(TINFO, "Connecting to the server");
70 
71 	client_sock = SAFE_SOCKET(sock_addr->sun_family, SOCK_STREAM, 0);
72 	SAFE_CONNECT(client_sock,
73 		(struct sockaddr *)sock_addr,
74 		sizeof(struct sockaddr_un));
75 
76 	return client_sock;
77 }
78 
run(unsigned int n)79 static void run(unsigned int n)
80 {
81 	struct tcase *tc = &tcases[n];
82 	int client_sock;
83 	char buff[MSGSIZE] = {0};
84 
85 	client_sock = start_test();
86 
87 	tst_res(TINFO, "Testing %s flag", tc->desc);
88 
89 	TST_EXP_PASS(shutdown(client_sock, tc->shutdown_op));
90 
91 	if (tc->recv_err)
92 		TST_EXP_FAIL(recv(client_sock, buff, MSGSIZE, tc->recv_flag), tc->recv_err);
93 	else
94 		SAFE_RECV(0, client_sock, buff, MSGSIZE, tc->recv_flag);
95 
96 	if (tc->send_err)
97 		TST_EXP_FAIL(send(client_sock, buff, MSGSIZE, tc->send_flag), tc->send_err);
98 	else
99 		SAFE_SEND(MSGSIZE, client_sock, buff, MSGSIZE, tc->send_flag);
100 
101 	SAFE_CLOSE(client_sock);
102 	TST_CHECKPOINT_WAKE(0);
103 }
104 
setup(void)105 static void setup(void)
106 {
107 	sock_addr->sun_family = AF_UNIX;
108 	memcpy(sock_addr->sun_path, SOCKETFILE, sizeof(SOCKETFILE));
109 }
110 
111 static struct tst_test test = {
112 	.test = run,
113 	.tcnt = ARRAY_SIZE(tcases),
114 	.setup = setup,
115 	.forks_child = 1,
116 	.needs_checkpoints = 1,
117 	.bufs = (struct tst_buffers []) {
118 		{&sock_addr, .size = sizeof(struct sockaddr_un)},
119 		{}
120 	}
121 };
122