1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci* Copyright (c) Ulrich Drepper <drepper@redhat.com>
4f08c3bdfSopenharmony_ci* Copyright (c) International Business Machines Corp., 2009
5f08c3bdfSopenharmony_ci*/
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci* Test Name:	socketpair02
9f08c3bdfSopenharmony_ci*
10f08c3bdfSopenharmony_ci* Description:
11f08c3bdfSopenharmony_ci* This Program tests the new flag SOCK_CLOEXEC and SOCK_NONBLOCK introduced
12f08c3bdfSopenharmony_ci* in socketpair() in kernel 2.6.27.
13f08c3bdfSopenharmony_ci*/
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include <errno.h>
16f08c3bdfSopenharmony_ci#include <pthread.h>
17f08c3bdfSopenharmony_ci#include <stdio.h>
18f08c3bdfSopenharmony_ci#include <unistd.h>
19f08c3bdfSopenharmony_ci#include <netinet/in.h>
20f08c3bdfSopenharmony_ci#include <sys/socket.h>
21f08c3bdfSopenharmony_ci#include <sys/syscall.h>
22f08c3bdfSopenharmony_ci#include "lapi/fcntl.h"
23f08c3bdfSopenharmony_ci#include "tst_test.h"
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic int fds[2];
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic struct tcase {
28f08c3bdfSopenharmony_ci	int type;
29f08c3bdfSopenharmony_ci	int flag;
30f08c3bdfSopenharmony_ci	int fl_flag;
31f08c3bdfSopenharmony_ci	char *des;
32f08c3bdfSopenharmony_ci} tcases[] = {
33f08c3bdfSopenharmony_ci	{SOCK_STREAM, 0, F_GETFD, "no close-on-exec"},
34f08c3bdfSopenharmony_ci	{SOCK_STREAM | SOCK_CLOEXEC, FD_CLOEXEC, F_GETFD, "close-on-exec"},
35f08c3bdfSopenharmony_ci	{SOCK_STREAM, 0, F_GETFL, "no non-blocking"},
36f08c3bdfSopenharmony_ci	{SOCK_STREAM | SOCK_NONBLOCK, O_NONBLOCK, F_GETFL, "non-blocking"}
37f08c3bdfSopenharmony_ci};
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic void verify_socketpair(unsigned int n)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	int res, i;
42f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	TEST(socketpair(PF_UNIX, tc->type, 0, fds));
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	if (TST_RET == -1)
47f08c3bdfSopenharmony_ci		tst_brk(TFAIL | TTERRNO, "socketpair() failed");
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	for (i = 0; i < 2; i++) {
50f08c3bdfSopenharmony_ci		res = SAFE_FCNTL(fds[i], tc->fl_flag);
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci		if (tc->flag != 0 && (res & tc->flag) == 0) {
53f08c3bdfSopenharmony_ci			tst_res(TFAIL, "socketpair() failed to set %s flag for fds[%d]",
54f08c3bdfSopenharmony_ci				tc->des, i);
55f08c3bdfSopenharmony_ci			goto ret;
56f08c3bdfSopenharmony_ci		}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci		if (tc->flag == 0 && (res & tc->flag) != 0) {
59f08c3bdfSopenharmony_ci			tst_res(TFAIL, "socketpair() failed to set %s flag for fds[%d]",
60f08c3bdfSopenharmony_ci				tc->des, i);
61f08c3bdfSopenharmony_ci			goto ret;
62f08c3bdfSopenharmony_ci		}
63f08c3bdfSopenharmony_ci	}
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	tst_res(TPASS, "socketpair() passed to set %s flag", tc->des);
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ciret:
68f08c3bdfSopenharmony_ci	SAFE_CLOSE(fds[0]);
69f08c3bdfSopenharmony_ci	SAFE_CLOSE(fds[1]);
70f08c3bdfSopenharmony_ci}
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_cistatic void cleanup(void)
73f08c3bdfSopenharmony_ci{
74f08c3bdfSopenharmony_ci	if (fds[0] > 0)
75f08c3bdfSopenharmony_ci		SAFE_CLOSE(fds[0]);
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	if (fds[1] > 0)
78f08c3bdfSopenharmony_ci		SAFE_CLOSE(fds[1]);
79f08c3bdfSopenharmony_ci}
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_cistatic struct tst_test test = {
82f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
83f08c3bdfSopenharmony_ci	.test = verify_socketpair,
84f08c3bdfSopenharmony_ci	.cleanup = cleanup
85f08c3bdfSopenharmony_ci};
86