File: sockets01.c

package info (click to toggle)
criu 4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: ansic: 139,280; python: 7,484; sh: 3,824; java: 2,799; makefile: 2,659; asm: 1,137; perl: 206; xml: 117; exp: 45
file content (162 lines) | stat: -rw-r--r-- 3,508 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <limits.h>
#include <fcntl.h>

#include "zdtmtst.h"

const char *test_doc = "Test unix sockets shutdown";
const char *test_author = "Pavel Emelyanov <xemul@parallels.com>";

#define fin(msg)                \
	do {                    \
		pr_perror(msg); \
		exit(1);        \
	} while (0)
#define ffin(msg)          \
	do {               \
		fail(msg); \
		exit(1);   \
	} while (0)

#define TEST_MSG "test-message"
static char buf[sizeof(TEST_MSG)];

#ifdef ZDTM_UNIX_SEQPACKET
#define SOCK_TYPE SOCK_SEQPACKET
#else
#define SOCK_TYPE SOCK_STREAM
#endif

int main(int argc, char *argv[])
{
	int spu[2], spb[2], dpu[2], dpb[2], dpd[2];
	int ret;

	test_init(argc, argv);

	signal(SIGPIPE, SIG_IGN);

	/* spu -- stream pair, unidirectional shutdown */
	if (socketpair(PF_UNIX, SOCK_TYPE, 0, spu) < 0)
		fin("no stream pair 1");

	if (shutdown(spu[0], SHUT_RD) < 0)
		fin("no stream shutdown 1");

	/* spb -- stream pair, bidirectional shutdown */
	if (socketpair(PF_UNIX, SOCK_TYPE, 0, spb) < 0)
		fin("no stream pair 2");

	if (shutdown(spb[0], SHUT_RDWR) < 0)
		fin("no stream shutdown 2");

	/* dpu -- dgram pair, one end read shutdown */
	if (socketpair(PF_UNIX, SOCK_DGRAM, 0, dpu) < 0)
		fin("no dgram pair 1");

	if (shutdown(dpu[0], SHUT_RD) < 0)
		fin("no dgram shutdown 1");

	/* dpb -- dgram pair, one end read-write shutdown */
	if (socketpair(PF_UNIX, SOCK_DGRAM, 0, dpb) < 0)
		fin("no dgram pair 2");

	if (shutdown(dpb[0], SHUT_RDWR) < 0)
		fin("no dgram shutdown 2");

	/* dpd -- dgram pair, one end write shutdown with data */
	if (socketpair(PF_UNIX, SOCK_DGRAM, 0, dpd) < 0)
		fin("no dgram pair 3");

	if (write(dpd[0], TEST_MSG, sizeof(TEST_MSG)) < 0)
		fin("no dgram write");

	if (shutdown(dpd[0], SHUT_WR) < 0)
		fin("no dgram shutdown 3");

	test_daemon();
	test_waitsig();

	/*
	 * spu -- check that one direction is blocked and
	 * the other one is not
	 */

	ret = write(spu[0], TEST_MSG, sizeof(TEST_MSG));
	if (ret < 0)
		ffin("SU shutdown broken 1");

	ret = read(spu[1], buf, sizeof(buf));
	if (ret < 0)
		ffin("SU shutdown broken 2");

	ret = write(spu[1], TEST_MSG, sizeof(TEST_MSG));
	if (ret >= 0)
		ffin("SU shutdown broken 3");

	/*
	 * spb -- check that both ends are off
	 */

	ret = write(spb[0], TEST_MSG, sizeof(TEST_MSG));
	if (ret >= 0)
		ffin("SB shutdown broken 1");

	ret = write(spb[1], TEST_MSG, sizeof(TEST_MSG));
	if (ret >= 0)
		ffin("SB shutdown broken 2");

	/*
	 * dpu -- check that one direction works, and
	 * the other does not
	 */

	ret = write(dpu[0], TEST_MSG, sizeof(TEST_MSG));
	if (ret < 0)
		ffin("DU shutdown broken 1");

	ret = read(dpu[1], buf, sizeof(buf));
	if (ret < 0)
		ffin("DU shutdown broken 2");

	ret = write(dpu[1], TEST_MSG, sizeof(TEST_MSG));
	if (ret >= 0)
		ffin("DU shutdown broken 3");

	/*
	 * dpb -- check that both ends are read
	 */

	ret = write(dpb[0], TEST_MSG, sizeof(TEST_MSG));
	if (ret >= 0)
		ffin("DB shutdown broken 1");

	ret = write(dpb[1], TEST_MSG, sizeof(TEST_MSG));
	if (ret >= 0)
		ffin("DB shutdown broken 2");

	/*
	 * dpd -- check that data is in there, but can't
	 * feed more
	 */

	ret = read(dpd[1], buf, sizeof(buf));
	if (ret < 0)
		ffin("DD shutdown nodata");

	ret = write(dpd[0], TEST_MSG, sizeof(buf));
	if (ret >= 0)
		ffin("DB shutdown broken");

	pass();
	return 0;
}