0840c52399c0ef52ccda3c36ea12f3d57c8fb892
[openocd.git] / src / server / server.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 √ėyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "server.h"
31 #include "target.h"
32
33 #include <signal.h>
34
35 #ifndef _WIN32
36 #include <netinet/tcp.h>
37 #endif
38
39
40 service_t *services = NULL;
41
42 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
43 static int shutdown_openocd = 0;
44 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
45
46 /* set when using pipes rather than tcp */
47 int server_use_pipes = 0;
48
49 int add_connection(service_t *service, command_context_t *cmd_ctx)
50 {
51 socklen_t address_size;
52 connection_t *c, **p;
53 int retval;
54 int flag=1;
55
56 c = malloc(sizeof(connection_t));
57 c->fd = -1;
58 memset(&c->sin, 0, sizeof(c->sin));
59 c->cmd_ctx = copy_command_context(cmd_ctx);
60 c->service = service;
61 c->input_pending = 0;
62 c->priv = NULL;
63 c->next = NULL;
64
65 if (service->type == CONNECTION_TCP)
66 {
67 address_size = sizeof(c->sin);
68
69 c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
70
71 /* This increases performance dramatically for e.g. GDB load which
72 * does not have a sliding window protocol. */
73 retval=setsockopt(c->fd, /* socket affected */
74 IPPROTO_TCP, /* set option at TCP level */
75 TCP_NODELAY, /* name of option */
76 (char *)&flag, /* the cast is historical cruft */
77 sizeof(int)); /* length of option value */
78
79 LOG_INFO("accepting '%s' connection from %i", service->name, c->sin.sin_port);
80 if ((retval = service->new_connection(c)) != ERROR_OK)
81 {
82 close_socket(c->fd);
83 LOG_ERROR("attempted '%s' connection rejected", service->name);
84 free(c);
85 return retval;
86 }
87 }
88 else if (service->type == CONNECTION_PIPE)
89 {
90 c->fd = service->fd;
91
92 /* do not check for new connections again on stdin */
93 service->fd = -1;
94
95 LOG_INFO("accepting '%s' connection from pipe", service->name);
96 if ((retval = service->new_connection(c)) != ERROR_OK)
97 {
98 LOG_ERROR("attempted '%s' connection rejected", service->name);
99 free(c);
100 return retval;
101 }
102 }
103
104 /* add to the end of linked list */
105 for (p = &service->connections; *p; p = &(*p)->next);
106 *p = c;
107
108 service->max_connections--;
109
110 return ERROR_OK;
111 }
112
113 int remove_connection(service_t *service, connection_t *connection)
114 {
115 connection_t **p = &service->connections;
116 connection_t *c;
117
118 /* find connection */
119 while((c = *p))
120 {
121 if (c->fd == connection->fd)
122 {
123 service->connection_closed(c);
124 if (service->type == CONNECTION_TCP)
125 close_socket(c->fd);
126 command_done(c->cmd_ctx);
127
128 /* delete connection */
129 *p = c->next;
130 free(c);
131
132 service->max_connections++;
133 break;
134 }
135
136 /* redirect p to next list pointer */
137 p = &(*p)->next;
138 }
139
140 return ERROR_OK;
141 }
142
143 int add_service(char *name, enum connection_type type, unsigned short port, int max_connections, new_connection_handler_t new_connection_handler, input_handler_t input_handler, connection_closed_handler_t connection_closed_handler, void *priv)
144 {
145 service_t *c, **p;
146 int so_reuseaddr_option = 1;
147
148 c = malloc(sizeof(service_t));
149
150 c->name = strdup(name);
151 c->type = type;
152 c->port = port;
153 c->max_connections = max_connections;
154 c->fd = -1;
155 c->connections = NULL;
156 c->new_connection = new_connection_handler;
157 c->input = input_handler;
158 c->connection_closed = connection_closed_handler;
159 c->priv = priv;
160 c->next = NULL;
161
162 if (type == CONNECTION_TCP)
163 {
164 if ((c->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
165 {
166 LOG_ERROR("error creating socket: %s", strerror(errno));
167 exit(-1);
168 }
169
170 setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
171
172 socket_nonblock(c->fd);
173
174 memset(&c->sin, 0, sizeof(c->sin));
175 c->sin.sin_family = AF_INET;
176 c->sin.sin_addr.s_addr = INADDR_ANY;
177 c->sin.sin_port = htons(port);
178
179 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1)
180 {
181 LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
182 exit(-1);
183 }
184
185 #ifndef _WIN32
186 int segsize=65536;
187 setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
188 #endif
189 int window_size = 128 * 1024;
190
191 /* These setsockopt()s must happen before the listen() */
192
193 setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
194 (char *)&window_size, sizeof(window_size));
195 setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
196 (char *)&window_size, sizeof(window_size));
197
198 if (listen(c->fd, 1) == -1)
199 {
200 LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
201 exit(-1);
202 }
203 }
204 else if (type == CONNECTION_PIPE)
205 {
206 /* use stdin */
207 c->fd = STDIN_FILENO;
208
209 #ifdef _WIN32
210 /* for win32 set stdin/stdout to binary mode */
211 if (_setmode(_fileno(stdout), _O_BINARY) < 0)
212 LOG_WARNING("cannot change stdout mode to binary");
213 if (_setmode(_fileno(stdin), _O_BINARY) < 0)
214 LOG_WARNING("cannot change stdin mode to binary");
215 if (_setmode(_fileno(stderr), _O_BINARY) < 0)
216 LOG_WARNING("cannot change stderr mode to binary");
217 #else
218 socket_nonblock(c->fd);
219 #endif
220 }
221 else
222 {
223 LOG_ERROR("unknown connection type: %d", type);
224 exit(1);
225 }
226
227 /* add to the end of linked list */
228 for (p = &services; *p; p = &(*p)->next);
229 *p = c;
230
231 return ERROR_OK;
232 }
233
234 int remove_service(unsigned short port)
235 {
236 service_t **p = &services;
237 service_t *c;
238
239 /* find service */
240 while((c = *p))
241 {
242 if (c->port == port)
243 {
244 if (c->name)
245 free(c->name);
246
247 if (c->priv)
248 free(c->priv);
249
250 /* delete service */
251 *p = c->next;
252 free(c);
253 }
254
255 /* redirect p to next list pointer */
256 p = &(*p)->next;
257 }
258
259 return ERROR_OK;
260 }
261
262 int remove_services(void)
263 {
264 service_t *c = services;
265
266 /* loop service */
267 while(c)
268 {
269 service_t *next = c->next;
270
271 if (c->name)
272 free(c->name);
273
274 if (c->priv)
275 free(c->priv);
276
277 /* delete service */
278 free(c);
279
280 /* remember the last service for unlinking */
281 c = next;
282 }
283
284 services = NULL;
285
286 return ERROR_OK;
287 }
288
289 extern void openocd_sleep_prelude(void);
290 extern void openocd_sleep_postlude(void);
291
292 int server_loop(command_context_t *command_context)
293 {
294 service_t *service;
295
296 /* used in select() */
297 fd_set read_fds;
298 struct timeval tv;
299 int fd_max;
300
301 /* used in accept() */
302 int retval;
303
304 #ifndef _WIN32
305 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
306 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
307 #endif
308
309 /* do regular tasks after at most 10ms */
310 tv.tv_sec = 0;
311 tv.tv_usec = 10000;
312
313 while(!shutdown_openocd)
314 {
315 /* monitor sockets for acitvity */
316 fd_max = 0;
317 FD_ZERO(&read_fds);
318
319 /* add service and connection fds to read_fds */
320 for (service = services; service; service = service->next)
321 {
322 if (service->fd != -1)
323 {
324 /* listen for new connections */
325 FD_SET(service->fd, &read_fds);
326
327 if (service->fd > fd_max)
328 fd_max = service->fd;
329 }
330
331 if (service->connections)
332 {
333 connection_t *c;
334
335 for (c = service->connections; c; c = c->next)
336 {
337 /* check for activity on the connection */
338 FD_SET(c->fd, &read_fds);
339 if (c->fd > fd_max)
340 fd_max = c->fd;
341 }
342 }
343 }
344
345 #ifndef _WIN32
346 #if BUILD_ECOSBOARD == 0
347 if (server_use_pipes == 0)
348 {
349 /* add STDIN to read_fds */
350 FD_SET(fileno(stdin), &read_fds);
351 }
352 #endif
353 #endif
354
355 openocd_sleep_prelude();
356 kept_alive();
357
358 /* Only while we're sleeping we'll let others run */
359 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
360 openocd_sleep_postlude();
361
362 if (retval == -1)
363 {
364 #ifdef _WIN32
365
366 errno = WSAGetLastError();
367
368 if (errno == WSAEINTR)
369 FD_ZERO(&read_fds);
370 else
371 {
372 LOG_ERROR("error during select: %s", strerror(errno));
373 exit(-1);
374 }
375 #else
376
377 if (errno == EINTR)
378 {
379 FD_ZERO(&read_fds);
380 }
381 else
382 {
383 LOG_ERROR("error during select: %s", strerror(errno));
384 exit(-1);
385 }
386 #endif
387 }
388
389 target_call_timer_callbacks();
390 process_jim_events ();
391
392 if (retval == 0)
393 {
394 /* do regular tasks after at most 100ms */
395 tv.tv_sec = 0;
396 tv.tv_usec = 10000;
397 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
398 }
399
400 for (service = services; service; service = service->next)
401 {
402 /* handle new connections on listeners */
403 if ((service->fd != -1)
404 && (FD_ISSET(service->fd, &read_fds)))
405 {
406 if (service->max_connections > 0)
407 {
408 add_connection(service, command_context);
409 }
410 else
411 {
412 if (service->type != CONNECTION_PIPE)
413 {
414 struct sockaddr_in sin;
415 socklen_t address_size = sizeof(sin);
416 int tmp_fd;
417 tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
418 close_socket(tmp_fd);
419 }
420 LOG_INFO("rejected '%s' connection, no more connections allowed", service->name);
421 }
422 }
423
424 /* handle activity on connections */
425 if (service->connections)
426 {
427 connection_t *c;
428
429 for (c = service->connections; c;)
430 {
431 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending)
432 {
433 if ((retval = service->input(c)) != ERROR_OK)
434 {
435 connection_t *next = c->next;
436 if (service->type == CONNECTION_PIPE)
437 {
438 /* if connection uses a pipe then shutdown openocd on error */
439 shutdown_openocd = 1;
440 }
441 remove_connection(service, c);
442 LOG_INFO("dropped '%s' connection - error %d", service->name, retval);
443 c = next;
444 continue;
445 }
446 }
447 c = c->next;
448 }
449 }
450 }
451
452 #ifndef _WIN32
453 #if BUILD_ECOSBOARD == 0
454 /* check for data on stdin if not using pipes */
455 if (server_use_pipes == 0)
456 {
457 if (FD_ISSET(fileno(stdin), &read_fds))
458 {
459 if (getc(stdin) == 'x')
460 {
461 shutdown_openocd = 1;
462 }
463 }
464 }
465 #endif
466 #else
467 MSG msg;
468 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
469 {
470 if (msg.message == WM_QUIT)
471 shutdown_openocd = 1;
472 }
473 #endif
474 }
475
476 return ERROR_OK;
477 }
478
479 #ifdef _WIN32
480 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
481 {
482 shutdown_openocd = 1;
483 return TRUE;
484 }
485
486 void sig_handler(int sig) {
487 shutdown_openocd = 1;
488 }
489 #endif
490
491 int server_init(void)
492 {
493 #ifdef _WIN32
494 WORD wVersionRequested;
495 WSADATA wsaData;
496
497 wVersionRequested = MAKEWORD(2, 2);
498
499 if (WSAStartup(wVersionRequested, &wsaData) != 0)
500 {
501 LOG_ERROR("Failed to Open Winsock");
502 exit(-1);
503 }
504
505 if (server_use_pipes == 0)
506 {
507 /* register ctrl-c handler */
508 SetConsoleCtrlHandler(ControlHandler, TRUE);
509 }
510 else
511 {
512 /* we are using pipes so ignore ctrl-c */
513 SetConsoleCtrlHandler(NULL, TRUE);
514 }
515
516 signal(SIGINT, sig_handler);
517 signal(SIGTERM, sig_handler);
518 signal(SIGBREAK, sig_handler);
519 signal(SIGABRT, sig_handler);
520 #endif
521
522 return ERROR_OK;
523 }
524
525 int server_quit(void)
526 {
527 remove_services();
528
529 #ifdef _WIN32
530 WSACleanup();
531 SetConsoleCtrlHandler( ControlHandler, FALSE );
532 #endif
533
534 return ERROR_OK;
535 }
536
537 int server_register_commands(command_context_t *context)
538 {
539 register_command(context, NULL, "shutdown", handle_shutdown_command,
540 COMMAND_ANY, "shut the server down");
541
542 return ERROR_OK;
543 }
544
545 /* tell the server we want to shut down */
546 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
547 {
548 shutdown_openocd = 1;
549
550 return ERROR_COMMAND_CLOSE_CONNECTION;
551 }