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 <fcntl.h>
17 #include <pthread.h>
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <netinet/in.h>
21 #include <sys/socket.h>
22 #include <sys/syscall.h>
23 #include "lapi/fcntl.h"
24 #include "tst_test.h"
25
26 static int fds[2];
27
28 static struct tcase {
29 int type;
30 int flag;
31 int fl_flag;
32 char *des;
33 } tcases[] = {
34 {SOCK_STREAM, 0, F_GETFD, "no close-on-exec"},
35 {SOCK_STREAM | SOCK_CLOEXEC, FD_CLOEXEC, F_GETFD, "close-on-exec"},
36 {SOCK_STREAM, 0, F_GETFL, "no non-blocking"},
37 {SOCK_STREAM | SOCK_NONBLOCK, O_NONBLOCK, F_GETFL, "non-blocking"}
38 };
39
verify_socketpair(unsigned int n)40 static void verify_socketpair(unsigned int n)
41 {
42 int res, i;
43 struct tcase *tc = &tcases[n];
44
45 TEST(socketpair(PF_UNIX, tc->type, 0, fds));
46
47 if (TST_RET == -1)
48 tst_brk(TFAIL | TTERRNO, "socketpair() failed");
49
50 for (i = 0; i < 2; i++) {
51 res = SAFE_FCNTL(fds[i], tc->fl_flag);
52
53 if (tc->flag != 0 && (res & tc->flag) == 0) {
54 tst_res(TFAIL, "socketpair() failed to set %s flag for fds[%d]",
55 tc->des, i);
56 goto ret;
57 }
58
59 if (tc->flag == 0 && (res & tc->flag) != 0) {
60 tst_res(TFAIL, "socketpair() failed to set %s flag for fds[%d]",
61 tc->des, i);
62 goto ret;
63 }
64 }
65
66 tst_res(TPASS, "socketpair() passed to set %s flag", tc->des);
67
68 ret:
69 SAFE_CLOSE(fds[0]);
70 SAFE_CLOSE(fds[1]);
71 }
72
cleanup(void)73 static void cleanup(void)
74 {
75 if (fds[0] > 0)
76 SAFE_CLOSE(fds[0]);
77
78 if (fds[1] > 0)
79 SAFE_CLOSE(fds[1]);
80 }
81
82 static struct tst_test test = {
83 .tcnt = ARRAY_SIZE(tcases),
84 .test = verify_socketpair,
85 .min_kver = "2.6.27",
86 .cleanup = cleanup
87 };
88