Welcome to mirror list, hosted at ThFree Co, Russian Federation.

unix_sock.c « transition « zdtm « test - github.com/checkpoint-restore/criu.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: caa5a4a57b939b74509db3ca1ef2f2d009078156 (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <fcntl.h>

#include "zdtmtst.h"

const char *test_doc = "Multi-client - server app";
const char *test_author = "Roman Kagan <rkagan@parallels.com>";

#define PROCS_DEF 4
#define PROCS_MAX 64
unsigned int num_procs = PROCS_DEF;
TEST_OPTION(num_procs, uint,
	    "# processes to create "
	    "(default " __stringify(PROCS_DEF) ", max " __stringify(PROCS_MAX) ")",
	    0);

char *filename;
TEST_OPTION(filename, string, "file name", 1);

#define ACCEPT_TIMEOUT 100 /* max delay for the child to connect */

static int fill_sock_name(struct sockaddr_un *name, const char *filename)
{
	if (strlen(filename) >= sizeof(name->sun_path))
		return -1;

	name->sun_family = AF_LOCAL;
	strcpy(name->sun_path, filename);
	return 0;
}

static int setup_srv_sock(void)
{
	struct sockaddr_un name;
	int sock;

	if (fill_sock_name(&name, filename) < 0) {
		pr_err("filename \"%s\" is too long\n", filename);
		return -1;
	}

	sock = socket(PF_LOCAL, SOCK_STREAM, 0);
	if (sock < 0) {
		pr_perror("can't create socket");
		return -1;
	}

	if (bind(sock, (struct sockaddr *)&name, SUN_LEN(&name)) < 0) {
		pr_perror("can't bind to socket \"%s\"", filename);
		goto err;
	}

	if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
		pr_perror("can't make socket \"%s\" non-blocking", filename);
		goto err;
	}

	if (listen(sock, 1) < 0) {
		pr_perror("can't listen on a socket \"%s\"", filename);
		goto err;
	}

	return sock;
err:
	close(sock);
	return -1;
}

static int accept_one_conn(int sock)
{
	int acc_sock;
	fd_set fds;
	struct timeval timeout = {
		.tv_sec = ACCEPT_TIMEOUT,
	};

	FD_ZERO(&fds);
	FD_SET(sock, &fds);

	switch (select(FD_SETSIZE, &fds, NULL, NULL, &timeout)) {
	case 1:
		break;
	case 0:
		pr_err("timeout accepting a connection\n");
		return -1;
	default:
		pr_perror("error while waiting for a connection");
		return -1;
	}

	acc_sock = accept(sock, NULL, NULL);
	if (acc_sock < 0)
		pr_perror("error accepting a connection");
	return acc_sock;
}

static int setup_clnt_sock(void)
{
	struct sockaddr_un name;
	int sock;
	int ret = 0;

	if (fill_sock_name(&name, filename) < 0) {
		pr_err("filename \"%s\" is too long\n", filename);
		return -1;
	}

	sock = socket(PF_LOCAL, SOCK_STREAM, 0);
	if (sock < 0) {
		ret = -errno;
		pr_perror("can't create socket");
		return ret;
	}

	if (connect(sock, (struct sockaddr *)&name, SUN_LEN(&name)) < 0) {
		ret = -errno;
		pr_perror("can't connect");
		goto err;
	}

	return sock;
err:
	close(sock);
	return ret;
}

#define BUFLEN 1000

static int child(void)
{
	int ret = 1;
	uint8_t buf[BUFLEN];
	uint32_t crc = ~0;
	int sock = setup_clnt_sock();

	if (sock < 0) {
		ret = -sock;
		goto out;
	}

	signal(SIGPIPE, SIG_IGN);
	while (test_go()) {
		datagen(buf, sizeof(buf), &crc);
		if (write(sock, buf, sizeof(buf)) < 0 && (test_go() /* signal NOT received */ ||
							  (errno != EINTR && errno != EPIPE && errno != ECONNRESET))) {
			ret = errno;
			fail("child write");
			goto out;
		}
	}

	ret = 0;
out:
	close(sock);
	return ret;
}

int main(int argc, char **argv)
{
	struct {
		pid_t pid;
		int sock;
		uint32_t crc;
	} child_desc[PROCS_MAX];
	int i, nproc;
	int sock;
	uint8_t buf[BUFLEN];
	fd_set active_fds, read_fds;

	test_init(argc, argv);

	if (num_procs > PROCS_MAX) {
		pr_err("%d processes is too many: max = %d\n", num_procs, PROCS_MAX);
		exit(1);
	}

	sock = setup_srv_sock();
	if (sock < 0)
		exit(1);

	FD_ZERO(&active_fds);
	for (nproc = 0; nproc < num_procs; nproc++) {
		child_desc[nproc].pid = test_fork();
		if (child_desc[nproc].pid < 0) {
			pr_perror("can't fork");
			goto cleanup;
		}

		if (child_desc[nproc].pid == 0) {
			close(sock);
			exit(child());
		}

		child_desc[nproc].sock = accept_one_conn(sock);
		if (child_desc[nproc].sock < 0) {
			kill(child_desc[nproc].pid, SIGKILL);
			goto cleanup;
		}

		child_desc[nproc].crc = ~0;
		FD_SET(child_desc[nproc].sock, &active_fds);
	}

	close(sock); /* no more connections */
	test_daemon();

	while (test_go()) {
		read_fds = active_fds;
		if (select(FD_SETSIZE, &read_fds, NULL, NULL, NULL) < 0 && errno != EINTR) {
			fail("error waiting for data");
			goto out;
		}

		for (i = 0; i < num_procs; i++)
			if (FD_ISSET(child_desc[i].sock, &read_fds)) {
				if (read(child_desc[i].sock, buf, sizeof(buf)) < 0) {
					if (errno == EINTR) /* we're asked to stop */
						break;
					else {
						fail("error reading data from socket");
						goto out;
					}
				}

				if (datachk(buf, sizeof(buf), &child_desc[i].crc)) {
					fail("CRC mismatch");
					goto out;
				}
			}
	}

out:
	test_waitsig();

	if (kill(0, SIGTERM)) {
		fail("failed to send SIGTERM to my process group");
		goto cleanup; /* shouldn't wait() in this case */
	}

	while (nproc-- > 0) {
		int chret;
		/*
		 * Close socket to make sure that child's write() returns.
		 * This is to avoid race when server stopped reading & sent
		 * signal to child, child has checked for signal & found none
		 * (not yet delivered), then called write(), blocking forever.
		 */
		if (close(child_desc[nproc].sock))
			fail("Can't close server socket");

		if (wait(&chret) < 0) {
			fail("can't wait for a child");
			goto cleanup;
		}

		chret = WEXITSTATUS(chret);
		if (chret) {
			fail("child exited with non-zero code %d (%s)", chret, strerror(chret));
			goto cleanup;
		}
	}

	pass();

cleanup:
	while (nproc-- > 0) {
		close(child_desc[nproc].sock);
		if (child_desc[nproc].pid > 0)
			kill(child_desc[nproc].pid, SIGKILL);
	}
	close(sock);
	unlink(filename);
	return 0;
}