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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
/*
* Copyright © 2019 Advanced Driver Information Technology GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*******************************************************************************
** **
** TARGET : linux **
** **
** PROJECT : waltham-receiver **
** **
** PURPOSE : This file handles connection with remote-client **
** **
*******************************************************************************/
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include "wth-receiver-comm.h"
#define MAX_EPOLL_WATCHES 2
#define DEFAULT_TCP_PORT 34400
uint16_t tcp_port = 0;
const char *my_app_id = NULL;
static bool *signal_int_handler_run_flag;
/** Print out the application help
*/
static void
usage(void)
{
printf("Usage: waltham receiver [options]\n");
printf("Options:\n");
printf(" -p --port number TCP port number\n");
printf(" -i --app_id Specify an app_id\n");
printf(" -h --help Usage\n");
}
static struct option long_options[] = {
{"port", required_argument, 0, 'p'},
{"app_id", required_argument, NULL, 'i'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
/**
* parse_args
*
* Parses the application arguments
* The arguments are parsed and saved in static structure for future use.
*
* @param argc The amount of arguments
* @param argv The table of arguments
*
* @return 0 on success, -1 otherwise
*/
static int
parse_args(int argc, char *argv[])
{
int c = -1;
int long_index = 0;
while ((c = getopt_long(argc, argv, "i:p:vh",
long_options,
&long_index)) != -1) {
switch (c) {
case 'i':
my_app_id = optarg;
break;
case 'p':
tcp_port = (uint16_t) atoi(optarg);
break;
case 'v':
printf("No verbose logs for release mode");
break;
case 'h':
usage();
return -1;
default:
wth_error("Try %s -h for more information.\n", argv[0]);
return -1;
}
}
if (tcp_port == 0) {
tcp_port = DEFAULT_TCP_PORT;
}
return 0;
}
static int
watch_ctl(struct watch *w, int op, uint32_t events)
{
struct epoll_event ee;
ee.events = events;
ee.data.ptr = w;
return epoll_ctl(w->receiver->epoll_fd, op, w->fd, &ee);
}
/**
* listen_socket_handle_data
*
* Handles all incoming events on socket
*
* @param names struct watch *w ,uint32_t events
* @param value pointer to watch connection it holds receiver information, Incoming events information
* @return none
*/
static void
listen_socket_handle_data(struct watch *w, uint32_t events)
{
struct receiver *srv = container_of(w, struct receiver, listen_watch);
if (events & EPOLLERR) {
wth_error("Listening socket errored out.\n");
srv->running = false;
return;
}
if (events & EPOLLHUP) {
wth_error("Listening socket hung up.\n");
srv->running = false;
return;
}
if (events & EPOLLIN) {
receiver_accept_client(srv);
}
}
static void
receiver_clean_up_child(pid_t pid, struct receiver *srv)
{
struct client *client;
/* client represents connection with the transmitter, so removing it
* from the client list isn't what need to happen. Mark the
* pid_destroying as to give it a chance for next time */
wl_list_for_each(client, &srv->client_list, link) {
if (client->pid == pid) {
client->pid_destroying = false;
}
}
}
static void
wait_for_children(struct receiver *srv, pid_t pid)
{
pid_t w;
int status;
int options = WUNTRACED | WCONTINUED;
do {
w = waitpid(pid, &status, options);
if (w == -1) {
fprintf(stderr, "err no children to wait for!\n");
}
if (WIFEXITED(status)) {
if (w > 0) {
receiver_clean_up_child(w, srv);
}
} else if (WIFSIGNALED(status)) {
// FIXME should remove the client here, and identify with the pid
} else if (WIFSTOPPED(status)) {
} else if (WIFCONTINUED(status)) {
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
/**
* receiver_mainloop
*
* This is the main loop, which will flush all pending clients requests and
* listen to input events from socket
*
* @param names void *data
* @param value pointer to receiver struct -
* struct holds the client connection information
* @return none
*/
static void
receiver_mainloop(struct receiver *srv)
{
struct epoll_event ee[MAX_EPOLL_WATCHES];
struct watch *w;
int count;
int i;
srv->running = true;
while (srv->running) {
/* Run any idle tasks at this point. */
bool should_wait_for_children = false;
struct client *client;
receiver_flush_clients(srv);
wl_list_for_each(client, &srv->client_list, link) {
if (client->pid > 0 && client->pid_destroying) {
should_wait_for_children = true;
break;
}
}
/*
* wait_for_children() will block and wait for children that
* died out/terminated correctly as the remote that end sent
* the destroy surface request. pid_destroying is the one that
* allows to make that distinction and will be set by the
* destroy surface handler.
*
* 'children' is just one. A new child will be started when the
* remote side sents a request to create a surface and will be
* killed in the handler that destroys the surface.
*/
if (should_wait_for_children)
wait_for_children(srv, client->pid);
/* Wait for events or signals */
count = epoll_wait(srv->epoll_fd,
ee, ARRAY_LENGTH(ee), -1);
if (count < 0 && errno != EINTR) {
perror("Error with epoll_wait");
break;
}
/* Handle all fds, both the listening socket
* (see listen_socket_handle_data()) and clients
* (see connection_handle_data()).
*/
for (i = 0; i < count; i++) {
w = ee[i].data.ptr;
w->cb(w, ee[i].events);
}
}
}
static int
receiver_listen(uint16_t tcp_port)
{
int fd;
int reuse = 1;
struct sockaddr_in addr;
fd = socket(AF_INET, SOCK_STREAM, 0);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(tcp_port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof reuse);
if (bind(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
wth_error("Failed to bind to port %d", tcp_port);
close(fd);
return -1;
}
if (listen(fd, 1024) < 0) {
wth_error("Failed to listen to port %d", tcp_port);
close (fd);
return -1;
}
return fd;
}
static void
signal_int_handler(int signum)
{
if (!*signal_int_handler_run_flag)
abort();
*signal_int_handler_run_flag = false;
}
static void
set_sigint_handler(bool *running)
{
struct sigaction sigint;
signal_int_handler_run_flag = running;
sigint.sa_handler = signal_int_handler;
sigemptyset(&sigint.sa_mask);
sigint.sa_flags = SA_RESETHAND;
sigaction(SIGINT, &sigint, NULL);
}
/**
* main
*
* waltham receiver main function, it accepts tcp port number as argument.
* Establishes connection on the port and listen to port for incoming connection
* request from waltham clients
*
* @param names argv - argument list and argc -argument count
* @param value tcp port number as argument
* @return 0 on success, -1 on error
*/
int main(int argc, char *argv[])
{
struct receiver srv = { 0 };
struct client *c;
/* Get command line arguments */
if (parse_args(argc, argv) != 0) {
return -1;
}
set_sigint_handler(&srv.running);
wl_list_init(&srv.client_list);
srv.epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if (srv.epoll_fd == -1) {
perror("Error on epoll_create1");
exit(1);
}
srv.listen_fd = receiver_listen(tcp_port);
if (srv.listen_fd < 0) {
perror("Error setting up listening socket");
exit(1);
}
srv.listen_watch.receiver = &srv;
srv.listen_watch.cb = listen_socket_handle_data;
srv.listen_watch.fd = srv.listen_fd;
if (watch_ctl(&srv.listen_watch, EPOLL_CTL_ADD, EPOLLIN) < 0) {
perror("Error setting up listen polling");
exit(1);
}
receiver_mainloop(&srv);
/* destroy all things */
wl_list_last_until_empty(c, &srv.client_list, link)
client_destroy(c);
close(srv.listen_fd);
close(srv.epoll_fd);
return 0;
}
|