File: socket_close_data01.c

package info (click to toggle)
criu 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,500 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 (115 lines) | stat: -rw-r--r-- 2,119 bytes parent folder | download | duplicates (3)
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
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/un.h>

#include "zdtmtst.h"

const char *test_doc = "Check data of bound socket and possibility to connect";
const char *test_author = "Kirill Tkhai <ktkhai@virtuozzo";

#define MSG "hello"
char *filename;
TEST_OPTION(filename, string, "file name", 1);

static int client(const char *iter)
{
	struct sockaddr_un addr;
	int sk;

	sk = socket(AF_UNIX, SOCK_DGRAM, 0);
	if (sk < 0) {
		pr_perror("open client %s", iter);
		return 1;
	}

	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, filename);

	if (connect(sk, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
		pr_perror("connect failed %s", iter);
		return 1;
	}

	if (send(sk, MSG, sizeof(MSG), 0) != sizeof(MSG)) {
		pr_perror("send failed %s", iter);
		return 1;
	}

	return 0;
}

int main(int argc, char **argv)
{
	struct sockaddr_un addr;
	int srv, status, ret;
	char buf[1024];

	test_init(argc, argv);

	srv = socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0);
	if (srv < 0) {
		pr_perror("open srv");
		exit(1);
	}

	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, filename);

	if (bind(srv, (struct sockaddr *)&addr, sizeof(struct sockaddr_un))) {
		pr_perror("bind srv");
		exit(1);
	}

	if (fork() == 0) {
		close(srv);
		client("(iter1)");
		exit(0);
	}
	ret = 1;
	if (wait(&status) == -1) {
		fail("wait failed");
		goto unlink;
	}
	if (status) {
		pr_err("A child exited with 0x%x\n", status);
		goto unlink;
	}

	test_daemon();
	test_waitsig();

	/* Test1: check we can read client message: */
	ret = read(srv, buf, sizeof(MSG));
	buf[ret > 0 ? ret : 0] = 0;
	if (ret != sizeof(MSG)) {
		fail("%d: %s", ret, buf);
		ret = 1;
		goto unlink;
	}

	/* Test2: check it's still possible to connect to the bound socket */
	if (fork() == 0) {
		exit(client("(iter2)"));
	}

	if (wait(&status) < 0) {
		fail("wait failed");
		goto unlink;
	}

	if (WEXITSTATUS(status) != 0) {
		fail("connect failed");
		goto unlink;
	}

	ret = 0;
	pass();
unlink:
	unlink(filename);
	return ret;
}