1a6250ce947dd47785830c325084d5d4add4bcc2
[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/target.h>
32 #include "openocd.h"
33 #include "tcl_server.h"
34 #include "telnet_server.h"
35
36 #include <signal.h>
37
38 #ifndef _WIN32
39 #include <netinet/tcp.h>
40 #endif
41
42
43 static struct service *services = NULL;
44
45 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
46 static int shutdown_openocd = 0;
47
48 /* set when using pipes rather than tcp */
49 int server_use_pipes = 0;
50
51 static int add_connection(struct service *service, struct command_context *cmd_ctx)
52 {
53 socklen_t address_size;
54 struct connection *c, **p;
55 int retval;
56 int flag = 1;
57
58 c = malloc(sizeof(struct connection));
59 c->fd = -1;
60 memset(&c->sin, 0, sizeof(c->sin));
61 c->cmd_ctx = copy_command_context(cmd_ctx);
62 c->service = service;
63 c->input_pending = 0;
64 c->priv = NULL;
65 c->next = NULL;
66
67 if (service->type == CONNECTION_TCP)
68 {
69 address_size = sizeof(c->sin);
70
71 c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
72
73 /* This increases performance dramatically for e.g. GDB load which
74 * does not have a sliding window protocol. */
75 retval = setsockopt(c->fd, /* socket affected */
76 IPPROTO_TCP, /* set option at TCP level */
77 TCP_NODELAY, /* name of option */
78 (char *)&flag, /* the cast is historical cruft */
79 sizeof(int)); /* length of option value */
80
81 LOG_INFO("accepting '%s' connection from %i", service->name, c->sin.sin_port);
82 if ((retval = service->new_connection(c)) != ERROR_OK)
83 {
84 close_socket(c->fd);
85 LOG_ERROR("attempted '%s' connection rejected", service->name);
86 free(c);
87 return retval;
88 }
89 }
90 else if (service->type == CONNECTION_PIPE)
91 {
92 c->fd = service->fd;
93
94 /* do not check for new connections again on stdin */
95 service->fd = -1;
96
97 LOG_INFO("accepting '%s' connection from pipe", service->name);
98 if ((retval = service->new_connection(c)) != ERROR_OK)
99 {
100 LOG_ERROR("attempted '%s' connection rejected", service->name);
101 free(c);
102 return retval;
103 }
104 }
105
106 /* add to the end of linked list */
107 for (p = &service->connections; *p; p = &(*p)->next);
108 *p = c;
109
110 service->max_connections--;
111
112 return ERROR_OK;
113 }
114
115 static int remove_connection(struct service *service, struct connection *connection)
116 {
117 struct connection **p = &service->connections;
118 struct connection *c;
119
120 /* find connection */
121 while ((c = *p))
122 {
123 if (c->fd == connection->fd)
124 {
125 service->connection_closed(c);
126 if (service->type == CONNECTION_TCP)
127 close_socket(c->fd);
128 command_done(c->cmd_ctx);
129
130 /* delete connection */
131 *p = c->next;
132 free(c);
133
134 service->max_connections++;
135 break;
136 }
137
138 /* redirect p to next list pointer */
139 p = &(*p)->next;
140 }
141
142 return ERROR_OK;
143 }
144
145 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)
146 {
147 struct service *c, **p;
148 int so_reuseaddr_option = 1;
149
150 c = malloc(sizeof(struct service));
151
152 c->name = strdup(name);
153 c->type = type;
154 c->port = port;
155 c->max_connections = max_connections;
156 c->fd = -1;
157 c->connections = NULL;
158 c->new_connection = new_connection_handler;
159 c->input = input_handler;
160 c->connection_closed = connection_closed_handler;
161 c->priv = priv;
162 c->next = NULL;
163
164 if (type == CONNECTION_TCP)
165 {
166 if ((c->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
167 {
168 LOG_ERROR("error creating socket: %s", strerror(errno));
169 exit(-1);
170 }
171
172 setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
173
174 socket_nonblock(c->fd);
175
176 memset(&c->sin, 0, sizeof(c->sin));
177 c->sin.sin_family = AF_INET;
178 c->sin.sin_addr.s_addr = INADDR_ANY;
179 c->sin.sin_port = htons(port);
180
181 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1)
182 {
183 LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
184 exit(-1);
185 }
186
187 #ifndef _WIN32
188 int segsize = 65536;
189 setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
190 #endif
191 int window_size = 128 * 1024;
192
193 /* These setsockopt()s must happen before the listen() */
194
195 setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
196 (char *)&window_size, sizeof(window_size));
197 setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
198 (char *)&window_size, sizeof(window_size));
199
200 if (listen(c->fd, 1) == -1)
201 {
202 LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
203 exit(-1);
204 }
205 }
206 else if (type == CONNECTION_PIPE)
207 {
208 /* use stdin */
209 c->fd = STDIN_FILENO;
210
211 #ifdef _WIN32
212 /* for win32 set stdin/stdout to binary mode */
213 if (_setmode(_fileno(stdout), _O_BINARY) < 0)
214 LOG_WARNING("cannot change stdout mode to binary");
215 if (_setmode(_fileno(stdin), _O_BINARY) < 0)
216 LOG_WARNING("cannot change stdin mode to binary");
217 if (_setmode(_fileno(stderr), _O_BINARY) < 0)
218 LOG_WARNING("cannot change stderr mode to binary");
219 #else
220 socket_nonblock(c->fd);
221 #endif
222 }
223 else
224 {
225 LOG_ERROR("unknown connection type: %d", type);
226 exit(1);
227 }
228
229 /* add to the end of linked list */
230 for (p = &services; *p; p = &(*p)->next);
231 *p = c;
232
233 return ERROR_OK;
234 }
235
236 int remove_service(unsigned short port)
237 {
238 struct service **p = &services;
239 struct service *c;
240
241 /* find service */
242 while ((c = *p))
243 {
244 if (c->port == port)
245 {
246 if (c->name)
247 free(c->name);
248
249 if (c->priv)
250 free(c->priv);
251
252 /* delete service */
253 *p = c->next;
254 free(c);
255 }
256
257 /* redirect p to next list pointer */
258 p = &(*p)->next;
259 }
260
261 return ERROR_OK;
262 }
263
264 static int remove_services(void)
265 {
266 struct service *c = services;
267
268 /* loop service */
269 while (c)
270 {
271 struct service *next = c->next;
272
273 if (c->name)
274 free(c->name);
275
276 if (c->priv)
277 free(c->priv);
278
279 /* delete service */
280 free(c);
281
282 /* remember the last service for unlinking */
283 c = next;
284 }
285
286 services = NULL;
287
288 return ERROR_OK;
289 }
290
291 int server_loop(struct command_context *command_context)
292 {
293 struct service *service;
294
295 bool poll = true;
296
297 /* used in select() */
298 fd_set read_fds;
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 while (!shutdown_openocd)
310 {
311 /* monitor sockets for activity */
312 fd_max = 0;
313 FD_ZERO(&read_fds);
314
315 /* add service and connection fds to read_fds */
316 for (service = services; service; service = service->next)
317 {
318 if (service->fd != -1)
319 {
320 /* listen for new connections */
321 FD_SET(service->fd, &read_fds);
322
323 if (service->fd > fd_max)
324 fd_max = service->fd;
325 }
326
327 if (service->connections)
328 {
329 struct connection *c;
330
331 for (c = service->connections; c; c = c->next)
332 {
333 /* check for activity on the connection */
334 FD_SET(c->fd, &read_fds);
335 if (c->fd > fd_max)
336 fd_max = c->fd;
337 }
338 }
339 }
340
341 #ifndef _WIN32
342 #if BUILD_ECOSBOARD == 0
343 if (server_use_pipes == 0)
344 {
345 /* add STDIN to read_fds */
346 FD_SET(fileno(stdin), &read_fds);
347 }
348 #endif
349 #endif
350
351 struct timeval tv;
352 tv.tv_sec = 0;
353 if (poll)
354 {
355 /* we're just polling this iteration, this is faster on embedded
356 * hosts */
357 tv.tv_usec = 0;
358 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
359 } else
360 {
361 /* Every 100ms */
362 tv.tv_usec = 100000;
363 /* Only while we're sleeping we'll let others run */
364 openocd_sleep_prelude();
365 kept_alive();
366 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
367 openocd_sleep_postlude();
368 }
369
370 if (retval == -1)
371 {
372 #ifdef _WIN32
373
374 errno = WSAGetLastError();
375
376 if (errno == WSAEINTR)
377 FD_ZERO(&read_fds);
378 else
379 {
380 LOG_ERROR("error during select: %s", strerror(errno));
381 exit(-1);
382 }
383 #else
384
385 if (errno == EINTR)
386 {
387 FD_ZERO(&read_fds);
388 }
389 else
390 {
391 LOG_ERROR("error during select: %s", strerror(errno));
392 exit(-1);
393 }
394 #endif
395 }
396
397 if (retval == 0)
398 {
399 /* We only execute these callbacks when there was nothing to do or we timed out */
400 target_call_timer_callbacks();
401 process_jim_events(command_context);
402
403 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
404
405 /* We timed out/there was nothing to do, timeout rather than poll next time */
406 poll = false;
407 } else
408 {
409 /* There was something to do, next time we'll just poll */
410 poll = true;
411 }
412
413 for (service = services; service; service = service->next)
414 {
415 /* handle new connections on listeners */
416 if ((service->fd != -1)
417 && (FD_ISSET(service->fd, &read_fds)))
418 {
419 if (service->max_connections > 0)
420 {
421 add_connection(service, command_context);
422 }
423 else
424 {
425 if (service->type != CONNECTION_PIPE)
426 {
427 struct sockaddr_in sin;
428 socklen_t address_size = sizeof(sin);
429 int tmp_fd;
430 tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
431 close_socket(tmp_fd);
432 }
433 LOG_INFO("rejected '%s' connection, no more connections allowed", service->name);
434 }
435 }
436
437 /* handle activity on connections */
438 if (service->connections)
439 {
440 struct connection *c;
441
442 for (c = service->connections; c;)
443 {
444 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending)
445 {
446 if ((retval = service->input(c)) != ERROR_OK)
447 {
448 struct connection *next = c->next;
449 if (service->type == CONNECTION_PIPE)
450 {
451 /* if connection uses a pipe then shutdown openocd on error */
452 shutdown_openocd = 1;
453 }
454 remove_connection(service, c);
455 LOG_INFO("dropped '%s' connection - error %d", service->name, retval);
456 c = next;
457 continue;
458 }
459 }
460 c = c->next;
461 }
462 }
463 }
464
465 #ifndef _WIN32
466 #if BUILD_ECOSBOARD == 0
467 /* check for data on stdin if not using pipes */
468 if (server_use_pipes == 0)
469 {
470 if (FD_ISSET(fileno(stdin), &read_fds))
471 {
472 if (getc(stdin) == 'x')
473 {
474 shutdown_openocd = 1;
475 }
476 }
477 }
478 #endif
479 #else
480 MSG msg;
481 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
482 {
483 if (msg.message == WM_QUIT)
484 shutdown_openocd = 1;
485 }
486 #endif
487 }
488
489 return ERROR_OK;
490 }
491
492 #ifdef _WIN32
493 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
494 {
495 shutdown_openocd = 1;
496 return TRUE;
497 }
498
499 void sig_handler(int sig) {
500 shutdown_openocd = 1;
501 }
502 #endif
503
504 int server_preinit(void)
505 {
506 /* this currently only calls WSAStartup on native win32 systems
507 * before any socket operations are performed.
508 * This is an issue if you call init in your config script */
509
510 #ifdef _WIN32
511 WORD wVersionRequested;
512 WSADATA wsaData;
513
514 wVersionRequested = MAKEWORD(2, 2);
515
516 if (WSAStartup(wVersionRequested, &wsaData) != 0)
517 {
518 LOG_ERROR("Failed to Open Winsock");
519 exit(-1);
520 }
521
522 if (server_use_pipes == 0)
523 {
524 /* register ctrl-c handler */
525 SetConsoleCtrlHandler(ControlHandler, TRUE);
526 }
527 else
528 {
529 /* we are using pipes so ignore ctrl-c */
530 SetConsoleCtrlHandler(NULL, TRUE);
531 }
532
533 signal(SIGINT, sig_handler);
534 signal(SIGTERM, sig_handler);
535 signal(SIGBREAK, sig_handler);
536 signal(SIGABRT, sig_handler);
537 #endif
538
539 return ERROR_OK;
540 }
541
542 int server_init(struct command_context *cmd_ctx)
543 {
544 int ret = tcl_init(cmd_ctx);
545 if (ERROR_OK != ret)
546 return ret;
547
548 return telnet_init("Open On-Chip Debugger");
549 }
550
551 int server_quit(void)
552 {
553 remove_services();
554
555 #ifdef _WIN32
556 WSACleanup();
557 SetConsoleCtrlHandler(ControlHandler, FALSE);
558 #endif
559
560 return ERROR_OK;
561 }
562
563 /* tell the server we want to shut down */
564 COMMAND_HANDLER(handle_shutdown_command)
565 {
566 LOG_USER("shutdown command invoked");
567
568 shutdown_openocd = 1;
569
570 return ERROR_OK;
571 }
572
573 static const struct command_registration server_command_handlers[] = {
574 {
575 .name = "shutdown",
576 .handler = &handle_shutdown_command,
577 .mode = COMMAND_ANY,
578 .help = "shut the server down",
579 },
580 COMMAND_REGISTRATION_DONE
581 };
582
583 int server_register_commands(struct command_context *cmd_ctx)
584 {
585 int retval = telnet_register_commands(cmd_ctx);
586 if (ERROR_OK != retval)
587 return retval;
588
589 retval = tcl_register_commands(cmd_ctx);
590 if (ERROR_OK != retval)
591 return retval;
592
593 return register_commands(cmd_ctx, NULL, server_command_handlers);
594 }
595
596 SERVER_PORT_COMMAND()
597 {
598 switch (CMD_ARGC) {
599 case 0:
600 command_print(CMD_CTX, "%d", *out);
601 break;
602 case 1:
603 {
604 uint16_t port;
605 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
606 *out = port;
607 break;
608 }
609 default:
610 return ERROR_INVALID_ARGUMENTS;
611 }
612 return ERROR_OK;
613 }