Cleanup lpc2000.c
[openocd.git] / src / server / server.c
blob96f06b3feff65095bde1d1697a1c85b8ca806b55
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Ø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 ***************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
31 #include "server.h"
32 #include <target/target.h>
33 #include <target/target_request.h>
34 #include "openocd.h"
35 #include "tcl_server.h"
36 #include "telnet_server.h"
38 #include <signal.h>
40 #ifndef _WIN32
41 #include <netinet/tcp.h>
42 #endif
44 static struct service *services;
46 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
47 static int shutdown_openocd;
49 static int add_connection(struct service *service, struct command_context *cmd_ctx)
51 socklen_t address_size;
52 struct connection *c, **p;
53 int retval;
54 int flag = 1;
56 c = malloc(sizeof(struct connection));
57 c->fd = -1;
58 c->fd_out = -1;
59 memset(&c->sin, 0, sizeof(c->sin));
60 c->cmd_ctx = copy_command_context(cmd_ctx);
61 c->service = service;
62 c->input_pending = 0;
63 c->priv = NULL;
64 c->next = NULL;
66 if (service->type == CONNECTION_TCP) {
67 address_size = sizeof(c->sin);
69 c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
70 c->fd_out = c->fd;
72 /* This increases performance dramatically for e.g. GDB load which
73 * does not have a sliding window protocol.
75 * Ignore errors from this fn as it probably just means less performance
77 setsockopt(c->fd, /* socket affected */
78 IPPROTO_TCP, /* set option at TCP level */
79 TCP_NODELAY, /* name of option */
80 (char *)&flag, /* the cast is historical cruft */
81 sizeof(int)); /* length of option value */
83 LOG_INFO("accepting '%s' connection from %s", service->name, service->port);
84 retval = service->new_connection(c);
85 if (retval != ERROR_OK) {
86 close_socket(c->fd);
87 LOG_ERROR("attempted '%s' connection rejected", service->name);
88 free(c);
89 return retval;
91 } else if (service->type == CONNECTION_STDINOUT) {
92 c->fd = service->fd;
93 c->fd_out = fileno(stdout);
95 #ifdef _WIN32
96 /* we are using stdin/out so ignore ctrl-c under windoze */
97 SetConsoleCtrlHandler(NULL, TRUE);
98 #endif
100 /* do not check for new connections again on stdin */
101 service->fd = -1;
103 LOG_INFO("accepting '%s' connection from pipe", service->name);
104 retval = service->new_connection(c);
105 if (retval != ERROR_OK) {
106 LOG_ERROR("attempted '%s' connection rejected", service->name);
107 free(c);
108 return retval;
110 } else if (service->type == CONNECTION_PIPE) {
111 c->fd = service->fd;
112 /* do not check for new connections again on stdin */
113 service->fd = -1;
115 char *out_file = alloc_printf("%so", service->port);
116 c->fd_out = open(out_file, O_WRONLY);
117 free(out_file);
118 if (c->fd_out == -1) {
119 LOG_ERROR("could not open %s", service->port);
120 exit(1);
123 LOG_INFO("accepting '%s' connection from pipe %s", service->name, service->port);
124 retval = service->new_connection(c);
125 if (retval != ERROR_OK) {
126 LOG_ERROR("attempted '%s' connection rejected", service->name);
127 free(c);
128 return retval;
132 /* add to the end of linked list */
133 for (p = &service->connections; *p; p = &(*p)->next)
135 *p = c;
137 service->max_connections--;
139 return ERROR_OK;
142 static int remove_connection(struct service *service, struct connection *connection)
144 struct connection **p = &service->connections;
145 struct connection *c;
147 /* find connection */
148 while ((c = *p)) {
149 if (c->fd == connection->fd) {
150 service->connection_closed(c);
151 if (service->type == CONNECTION_TCP)
152 close_socket(c->fd);
153 else if (service->type == CONNECTION_PIPE) {
154 /* The service will listen to the pipe again */
155 c->service->fd = c->fd;
158 command_done(c->cmd_ctx);
160 /* delete connection */
161 *p = c->next;
162 free(c);
164 service->max_connections++;
165 break;
168 /* redirect p to next list pointer */
169 p = &(*p)->next;
172 return ERROR_OK;
175 /* FIX! make service return error instead of invoking exit() */
176 int add_service(char *name,
177 const char *port,
178 int max_connections,
179 new_connection_handler_t new_connection_handler,
180 input_handler_t input_handler,
181 connection_closed_handler_t connection_closed_handler,
182 void *priv)
184 struct service *c, **p;
185 int so_reuseaddr_option = 1;
187 c = malloc(sizeof(struct service));
189 c->name = strdup(name);
190 c->port = strdup(port);
191 c->max_connections = 1; /* Only TCP/IP ports can support more than one connection */
192 c->fd = -1;
193 c->connections = NULL;
194 c->new_connection = new_connection_handler;
195 c->input = input_handler;
196 c->connection_closed = connection_closed_handler;
197 c->priv = priv;
198 c->next = NULL;
199 long portnumber;
200 if (strcmp(c->port, "pipe") == 0)
201 c->type = CONNECTION_STDINOUT;
202 else {
203 char *end;
204 portnumber = strtol(c->port, &end, 0);
205 if (!*end && (parse_long(c->port, &portnumber) == ERROR_OK)) {
206 c->portnumber = portnumber;
207 c->type = CONNECTION_TCP;
208 } else
209 c->type = CONNECTION_PIPE;
212 if (c->type == CONNECTION_TCP) {
213 c->max_connections = max_connections;
215 c->fd = socket(AF_INET, SOCK_STREAM, 0);
216 if (c->fd == -1) {
217 LOG_ERROR("error creating socket: %s", strerror(errno));
218 exit(-1);
221 setsockopt(c->fd,
222 SOL_SOCKET,
223 SO_REUSEADDR,
224 (void *)&so_reuseaddr_option,
225 sizeof(int));
227 socket_nonblock(c->fd);
229 memset(&c->sin, 0, sizeof(c->sin));
230 c->sin.sin_family = AF_INET;
231 c->sin.sin_addr.s_addr = INADDR_ANY;
232 c->sin.sin_port = htons(c->portnumber);
234 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1) {
235 LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
236 exit(-1);
239 #ifndef _WIN32
240 int segsize = 65536;
241 setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
242 #endif
243 int window_size = 128 * 1024;
245 /* These setsockopt()s must happen before the listen() */
247 setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
248 (char *)&window_size, sizeof(window_size));
249 setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
250 (char *)&window_size, sizeof(window_size));
252 if (listen(c->fd, 1) == -1) {
253 LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
254 exit(-1);
256 } else if (c->type == CONNECTION_STDINOUT) {
257 c->fd = fileno(stdin);
259 #ifdef _WIN32
260 /* for win32 set stdin/stdout to binary mode */
261 if (_setmode(_fileno(stdout), _O_BINARY) < 0)
262 LOG_WARNING("cannot change stdout mode to binary");
263 if (_setmode(_fileno(stdin), _O_BINARY) < 0)
264 LOG_WARNING("cannot change stdin mode to binary");
265 if (_setmode(_fileno(stderr), _O_BINARY) < 0)
266 LOG_WARNING("cannot change stderr mode to binary");
267 #else
268 socket_nonblock(c->fd);
269 #endif
270 } else if (c->type == CONNECTION_PIPE) {
271 #ifdef _WIN32
272 /* we currenty do not support named pipes under win32
273 * so exit openocd for now */
274 LOG_ERROR("Named pipes currently not supported under this os");
275 exit(1);
276 #else
277 /* Pipe we're reading from */
278 c->fd = open(c->port, O_RDONLY | O_NONBLOCK);
279 if (c->fd == -1) {
280 LOG_ERROR("could not open %s", c->port);
281 exit(1);
283 #endif
286 /* add to the end of linked list */
287 for (p = &services; *p; p = &(*p)->next)
289 *p = c;
291 return ERROR_OK;
294 static int remove_services(void)
296 struct service *c = services;
298 /* loop service */
299 while (c) {
300 struct service *next = c->next;
302 if (c->name)
303 free((void *)c->name);
305 if (c->type == CONNECTION_PIPE) {
306 if (c->fd != -1)
307 close(c->fd);
309 if (c->port)
310 free((void *)c->port);
312 if (c->priv)
313 free(c->priv);
315 /* delete service */
316 free(c);
318 /* remember the last service for unlinking */
319 c = next;
322 services = NULL;
324 return ERROR_OK;
327 int server_loop(struct command_context *command_context)
329 struct service *service;
331 bool poll_ok = true;
333 /* used in select() */
334 fd_set read_fds;
335 int fd_max;
337 /* used in accept() */
338 int retval;
340 #ifndef _WIN32
341 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
342 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
343 #endif
345 while (!shutdown_openocd) {
346 /* monitor sockets for activity */
347 fd_max = 0;
348 FD_ZERO(&read_fds);
350 /* add service and connection fds to read_fds */
351 for (service = services; service; service = service->next) {
352 if (service->fd != -1) {
353 /* listen for new connections */
354 FD_SET(service->fd, &read_fds);
356 if (service->fd > fd_max)
357 fd_max = service->fd;
360 if (service->connections) {
361 struct connection *c;
363 for (c = service->connections; c; c = c->next) {
364 /* check for activity on the connection */
365 FD_SET(c->fd, &read_fds);
366 if (c->fd > fd_max)
367 fd_max = c->fd;
372 struct timeval tv;
373 tv.tv_sec = 0;
374 if (poll_ok) {
375 /* we're just polling this iteration, this is faster on embedded
376 * hosts */
377 tv.tv_usec = 0;
378 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
379 } else {
380 /* Every 100ms */
381 tv.tv_usec = 100000;
382 /* Only while we're sleeping we'll let others run */
383 openocd_sleep_prelude();
384 kept_alive();
385 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
386 openocd_sleep_postlude();
389 if (retval == -1) {
390 #ifdef _WIN32
392 errno = WSAGetLastError();
394 if (errno == WSAEINTR)
395 FD_ZERO(&read_fds);
396 else {
397 LOG_ERROR("error during select: %s", strerror(errno));
398 exit(-1);
400 #else
402 if (errno == EINTR)
403 FD_ZERO(&read_fds);
404 else {
405 LOG_ERROR("error during select: %s", strerror(errno));
406 exit(-1);
408 #endif
411 if (retval == 0) {
412 /* We only execute these callbacks when there was nothing to do or we timed
413 *out */
414 target_call_timer_callbacks();
415 process_jim_events(command_context);
417 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
419 /* We timed out/there was nothing to do, timeout rather than poll next time
421 poll_ok = false;
422 } else {
423 /* There was something to do, next time we'll just poll */
424 poll_ok = true;
427 /* This is a simple back-off algorithm where we immediately
428 * re-poll if we did something this time around.
430 * This greatly improves performance of DCC.
432 poll_ok = poll_ok || target_got_message();
434 for (service = services; service; service = service->next) {
435 /* handle new connections on listeners */
436 if ((service->fd != -1)
437 && (FD_ISSET(service->fd, &read_fds))) {
438 if (service->max_connections > 0)
439 add_connection(service, command_context);
440 else {
441 if (service->type == CONNECTION_TCP) {
442 struct sockaddr_in sin;
443 socklen_t address_size = sizeof(sin);
444 int tmp_fd;
445 tmp_fd = accept(service->fd,
446 (struct sockaddr *)&service->sin,
447 &address_size);
448 close_socket(tmp_fd);
450 LOG_INFO(
451 "rejected '%s' connection, no more connections allowed",
452 service->name);
456 /* handle activity on connections */
457 if (service->connections) {
458 struct connection *c;
460 for (c = service->connections; c; ) {
461 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending) {
462 retval = service->input(c);
463 if (retval != ERROR_OK) {
464 struct connection *next = c->next;
465 if (service->type == CONNECTION_PIPE) {
466 /* if connection uses a pipe then
467 *shutdown openocd on error */
468 shutdown_openocd = 1;
470 remove_connection(service, c);
471 LOG_INFO("dropped '%s' connection",
472 service->name);
473 c = next;
474 continue;
477 c = c->next;
482 #ifdef _WIN32
483 MSG msg;
484 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
485 if (msg.message == WM_QUIT)
486 shutdown_openocd = 1;
488 #endif
491 return ERROR_OK;
494 #ifdef _WIN32
495 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
497 shutdown_openocd = 1;
498 return TRUE;
501 void sig_handler(int sig)
503 shutdown_openocd = 1;
505 #endif
507 int server_preinit(void)
509 /* this currently only calls WSAStartup on native win32 systems
510 * before any socket operations are performed.
511 * This is an issue if you call init in your config script */
513 #ifdef _WIN32
514 WORD wVersionRequested;
515 WSADATA wsaData;
517 wVersionRequested = MAKEWORD(2, 2);
519 if (WSAStartup(wVersionRequested, &wsaData) != 0) {
520 LOG_ERROR("Failed to Open Winsock");
521 exit(-1);
524 /* register ctrl-c handler */
525 SetConsoleCtrlHandler(ControlHandler, TRUE);
527 signal(SIGINT, sig_handler);
528 signal(SIGTERM, sig_handler);
529 signal(SIGBREAK, sig_handler);
530 signal(SIGABRT, sig_handler);
531 #endif
533 return ERROR_OK;
536 int server_init(struct command_context *cmd_ctx)
538 int ret = tcl_init();
539 if (ERROR_OK != ret)
540 return ret;
542 return telnet_init("Open On-Chip Debugger");
545 int server_quit(void)
547 remove_services();
549 #ifdef _WIN32
550 WSACleanup();
551 SetConsoleCtrlHandler(ControlHandler, FALSE);
552 #endif
554 return ERROR_OK;
557 int connection_write(struct connection *connection, const void *data, int len)
559 if (len == 0) {
560 /* successful no-op. Sockets and pipes behave differently here... */
561 return 0;
563 if (connection->service->type == CONNECTION_TCP)
564 return write_socket(connection->fd_out, data, len);
565 else
566 return write(connection->fd_out, data, len);
569 int connection_read(struct connection *connection, void *data, int len)
571 if (connection->service->type == CONNECTION_TCP)
572 return read_socket(connection->fd, data, len);
573 else
574 return read(connection->fd, data, len);
577 /* tell the server we want to shut down */
578 COMMAND_HANDLER(handle_shutdown_command)
580 LOG_USER("shutdown command invoked");
582 shutdown_openocd = 1;
584 return ERROR_OK;
587 static const struct command_registration server_command_handlers[] = {
589 .name = "shutdown",
590 .handler = &handle_shutdown_command,
591 .mode = COMMAND_ANY,
592 .usage = "",
593 .help = "shut the server down",
595 COMMAND_REGISTRATION_DONE
598 int server_register_commands(struct command_context *cmd_ctx)
600 int retval = telnet_register_commands(cmd_ctx);
601 if (ERROR_OK != retval)
602 return retval;
604 retval = tcl_register_commands(cmd_ctx);
605 if (ERROR_OK != retval)
606 return retval;
608 return register_commands(cmd_ctx, NULL, server_command_handlers);
611 SERVER_PORT_COMMAND()
613 switch (CMD_ARGC) {
614 case 0:
615 command_print(CMD_CTX, "%d", *out);
616 break;
617 case 1:
619 uint16_t port;
620 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
621 *out = port;
622 break;
624 default:
625 return ERROR_COMMAND_SYNTAX_ERROR;
627 return ERROR_OK;
630 SERVER_PIPE_COMMAND()
632 switch (CMD_ARGC) {
633 case 0:
634 command_print(CMD_CTX, "%s", *out);
635 break;
636 case 1:
638 if (CMD_CTX->mode == COMMAND_EXEC) {
639 LOG_WARNING("unable to change server port after init");
640 return ERROR_COMMAND_ARGUMENT_INVALID;
642 const char *t = strdup(CMD_ARGV[0]);
643 free((void *)*out);
644 *out = t;
645 break;
647 default:
648 return ERROR_COMMAND_SYNTAX_ERROR;
650 return ERROR_OK;