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
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* This program is used to test the QEMUD fast pipes.
* See external/qemu/docs/ANDROID-QEMUD-PIPES.TXT for details.
*
* The program acts as a simple TCP server that accepts any data and
* discards it immediately.
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/* Default port number */
#define DEFAULT_PORT 8012
/* Try to execute x, looping around EINTR errors. */
#undef TEMP_FAILURE_RETRY
#define TEMP_FAILURE_RETRY(exp) ({ \
typeof (exp) _rc; \
do { \
_rc = (exp); \
} while (_rc == -1 && errno == EINTR); \
_rc; })
#define TFR TEMP_FAILURE_RETRY
/* Close a socket, preserving the value of errno */
static void
socket_close(int sock)
{
int old_errno = errno;
close(sock);
errno = old_errno;
}
/* Create a server socket bound to a loopback port */
static int
socket_loopback_server( int port, int type )
{
struct sockaddr_in addr;
int sock = socket(AF_INET, type, 0);
if (sock < 0) {
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
int n = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
if (TFR(bind(sock, (struct sockaddr*)&addr, sizeof(addr))) < 0) {
socket_close(sock);
return -1;
}
if (type == SOCK_STREAM) {
if (TFR(listen(sock, 4)) < 0) {
socket_close(sock);
return -1;
}
}
return sock;
}
/* Main program */
int main(void)
{
int sock, client;
int port = DEFAULT_PORT;
printf("Starting pipe test server on local port %d\n", port);
sock = socket_loopback_server( port, SOCK_STREAM );
if (sock < 0) {
fprintf(stderr, "Could not start server: %s\n", strerror(errno));
return 1;
}
RESTART:
client = TFR(accept(sock, NULL, NULL));
if (client < 0) {
fprintf(stderr, "Server error: %s\n", strerror(errno));
return 2;
}
printf("Client connected!\n");
/* Now, accept any incoming data, and send it back */
for (;;) {
char buff[8192], *p;
int ret, count;
ret = TFR(read(client, buff, sizeof(buff)));
if (ret < 0) {
fprintf(stderr, "Client read error: %s\n", strerror(errno));
socket_close(client);
return 3;
}
if (ret == 0) {
break;
}
}
printf("Client closed connection\n");
socket_close(client);
goto RESTART;
return 0;
}
|