File: sockets03.c

package info (click to toggle)
criu 4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • 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 (127 lines) | stat: -rw-r--r-- 2,236 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
#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 <sys/mount.h>
#include <limits.h>
#include <fcntl.h>

#include "zdtmtst.h"

const char *test_doc = "Test unix stream sockets with mismatch in shutdown state\n";
const char *test_author = "Andrey Ryabinin <aryabinin@virtuozzo.com>";

#define SK_DATA "packet"

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

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

int main(int argc, char *argv[])
{
	int sk[3];
	struct sockaddr_un addr;
	unsigned int addrlen;
	char path[PATH_MAX];
	char buf[64];
	char *cwd;
	int ret;

	test_init(argc, argv);

	signal(SIGPIPE, SIG_IGN);

	cwd = get_current_dir_name();
	if (!cwd) {
		fail("getcwd");
		exit(1);
	}

	snprintf(path, sizeof(path), "%s/%s", cwd, filename);
	unlink(path);

	addr.sun_family = AF_UNIX;
	addrlen = strlen(path);
	if (addrlen >= sizeof(addr.sun_path))
		return 1;
	memcpy(addr.sun_path, path, addrlen);
	addrlen += sizeof(addr.sun_family);

	sk[0] = socket(AF_UNIX, SOCK_TYPE, 0);
	sk[1] = socket(AF_UNIX, SOCK_TYPE, 0);
	if (sk[0] < 0 || sk[1] < 0) {
		fail("socket");
		exit(1);
	}

	ret = bind(sk[0], (struct sockaddr *)&addr, addrlen);
	if (ret) {
		fail("bind");
		exit(1);
	}

	ret = listen(sk[0], 16);
	if (ret) {
		fail("listen");
		exit(1);
	}

	ret = shutdown(sk[1], SHUT_RD);
	if (ret) {
		fail("shutdown");
		exit(1);
	}

	ret = connect(sk[1], (struct sockaddr *)&addr, addrlen);
	if (ret) {
		fail("connect");
		exit(1);
	}

	sk[2] = accept(sk[0], NULL, NULL);
	if (sk[2] < 0) {
		fail("accept");
		exit(1);
	}

	test_daemon();
	test_waitsig();

	if (write(sk[1], SK_DATA, sizeof(SK_DATA)) < 0) {
		fail("write");
		exit(1);
	}

	if (read(sk[2], &buf, sizeof(buf)) < 0) {
		fail("read");
		exit(1);
	}

	if (strncmp(buf, SK_DATA, sizeof(SK_DATA))) {
		fail("data corrupted");
		exit(1);
	}

	if (write(sk[2], SK_DATA, sizeof(SK_DATA)) >= 0) {
		fail("successful write to shutdown receiver");
		exit(1);
	}

	close(sk[0]);
	close(sk[1]);
	close(sk[2]);

	pass();
	return 0;
}