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