1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
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. *
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. *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
25 ***************************************************************************/
32 #include <target/target.h>
33 #include <target/target_request.h>
34 #include <target/openrisc/jsp_server.h>
36 #include "tcl_server.h"
37 #include "telnet_server.h"
42 #include <netinet/tcp.h>
45 static struct service *services;
47 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
48 static int shutdown_openocd;
50 /* store received signal to exit application by killing ourselves */
51 static int last_signal;
53 /* set the polling period to 100ms */
54 static int polling_period = 100;
56 static int add_connection(struct service *service, struct command_context *cmd_ctx)
58 socklen_t address_size;
59 struct connection *c, **p;
63 c = malloc(sizeof(struct connection));
66 memset(&c->sin, 0, sizeof(c->sin));
67 c->cmd_ctx = copy_command_context(cmd_ctx);
73 if (service->type == CONNECTION_TCP) {
74 address_size = sizeof(c->sin);
76 c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
79 /* This increases performance dramatically for e.g. GDB load which
80 * does not have a sliding window protocol.
82 * Ignore errors from this fn as it probably just means less performance
84 setsockopt(c->fd, /* socket affected */
85 IPPROTO_TCP, /* set option at TCP level */
86 TCP_NODELAY, /* name of option */
87 (char *)&flag, /* the cast is historical cruft */
88 sizeof(int)); /* length of option value */
90 LOG_INFO("accepting '%s' connection on tcp/%s", service->name, service->port);
91 retval = service->new_connection(c);
92 if (retval != ERROR_OK) {
94 LOG_ERROR("attempted '%s' connection rejected", service->name);
95 command_done(c->cmd_ctx);
99 } else if (service->type == CONNECTION_STDINOUT) {
101 c->fd_out = fileno(stdout);
104 /* we are using stdin/out so ignore ctrl-c under windoze */
105 SetConsoleCtrlHandler(NULL, TRUE);
108 /* do not check for new connections again on stdin */
111 LOG_INFO("accepting '%s' connection from pipe", service->name);
112 retval = service->new_connection(c);
113 if (retval != ERROR_OK) {
114 LOG_ERROR("attempted '%s' connection rejected", service->name);
115 command_done(c->cmd_ctx);
119 } else if (service->type == CONNECTION_PIPE) {
121 /* do not check for new connections again on stdin */
124 char *out_file = alloc_printf("%so", service->port);
125 c->fd_out = open(out_file, O_WRONLY);
127 if (c->fd_out == -1) {
128 LOG_ERROR("could not open %s", service->port);
132 LOG_INFO("accepting '%s' connection from pipe %s", service->name, service->port);
133 retval = service->new_connection(c);
134 if (retval != ERROR_OK) {
135 LOG_ERROR("attempted '%s' connection rejected", service->name);
136 command_done(c->cmd_ctx);
142 /* add to the end of linked list */
143 for (p = &service->connections; *p; p = &(*p)->next)
147 service->max_connections--;
152 static int remove_connection(struct service *service, struct connection *connection)
154 struct connection **p = &service->connections;
155 struct connection *c;
157 /* find connection */
159 if (c->fd == connection->fd) {
160 service->connection_closed(c);
161 if (service->type == CONNECTION_TCP)
163 else if (service->type == CONNECTION_PIPE) {
164 /* The service will listen to the pipe again */
165 c->service->fd = c->fd;
168 command_done(c->cmd_ctx);
170 /* delete connection */
174 service->max_connections++;
178 /* redirect p to next list pointer */
185 /* FIX! make service return error instead of invoking exit() */
186 int add_service(char *name,
189 new_connection_handler_t new_connection_handler,
190 input_handler_t input_handler,
191 connection_closed_handler_t connection_closed_handler,
194 struct service *c, **p;
195 int so_reuseaddr_option = 1;
197 c = malloc(sizeof(struct service));
199 c->name = strdup(name);
200 c->port = strdup(port);
201 c->max_connections = 1; /* Only TCP/IP ports can support more than one connection */
203 c->connections = NULL;
204 c->new_connection = new_connection_handler;
205 c->input = input_handler;
206 c->connection_closed = connection_closed_handler;
210 if (strcmp(c->port, "pipe") == 0)
211 c->type = CONNECTION_STDINOUT;
214 portnumber = strtol(c->port, &end, 0);
215 if (!*end && (parse_long(c->port, &portnumber) == ERROR_OK)) {
216 c->portnumber = portnumber;
217 c->type = CONNECTION_TCP;
219 c->type = CONNECTION_PIPE;
222 if (c->type == CONNECTION_TCP) {
223 c->max_connections = max_connections;
225 c->fd = socket(AF_INET, SOCK_STREAM, 0);
227 LOG_ERROR("error creating socket: %s", strerror(errno));
234 (void *)&so_reuseaddr_option,
237 socket_nonblock(c->fd);
239 memset(&c->sin, 0, sizeof(c->sin));
240 c->sin.sin_family = AF_INET;
241 c->sin.sin_addr.s_addr = INADDR_ANY;
242 c->sin.sin_port = htons(c->portnumber);
244 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1) {
245 LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
251 setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
253 int window_size = 128 * 1024;
255 /* These setsockopt()s must happen before the listen() */
257 setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
258 (char *)&window_size, sizeof(window_size));
259 setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
260 (char *)&window_size, sizeof(window_size));
262 if (listen(c->fd, 1) == -1) {
263 LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
266 } else if (c->type == CONNECTION_STDINOUT) {
267 c->fd = fileno(stdin);
270 /* for win32 set stdin/stdout to binary mode */
271 if (_setmode(_fileno(stdout), _O_BINARY) < 0)
272 LOG_WARNING("cannot change stdout mode to binary");
273 if (_setmode(_fileno(stdin), _O_BINARY) < 0)
274 LOG_WARNING("cannot change stdin mode to binary");
275 if (_setmode(_fileno(stderr), _O_BINARY) < 0)
276 LOG_WARNING("cannot change stderr mode to binary");
278 socket_nonblock(c->fd);
280 } else if (c->type == CONNECTION_PIPE) {
282 /* we currenty do not support named pipes under win32
283 * so exit openocd for now */
284 LOG_ERROR("Named pipes currently not supported under this os");
287 /* Pipe we're reading from */
288 c->fd = open(c->port, O_RDONLY | O_NONBLOCK);
290 LOG_ERROR("could not open %s", c->port);
296 /* add to the end of linked list */
297 for (p = &services; *p; p = &(*p)->next)
304 static int remove_services(void)
306 struct service *c = services;
310 struct service *next = c->next;
315 if (c->type == CONNECTION_PIPE) {
328 /* remember the last service for unlinking */
337 int server_loop(struct command_context *command_context)
339 struct service *service;
343 /* used in select() */
347 /* used in accept() */
351 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
352 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
355 while (!shutdown_openocd) {
356 /* monitor sockets for activity */
360 /* add service and connection fds to read_fds */
361 for (service = services; service; service = service->next) {
362 if (service->fd != -1) {
363 /* listen for new connections */
364 FD_SET(service->fd, &read_fds);
366 if (service->fd > fd_max)
367 fd_max = service->fd;
370 if (service->connections) {
371 struct connection *c;
373 for (c = service->connections; c; c = c->next) {
374 /* check for activity on the connection */
375 FD_SET(c->fd, &read_fds);
385 /* we're just polling this iteration, this is faster on embedded
388 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
390 /* Every 100ms, can be changed with "poll_period" command */
391 tv.tv_usec = polling_period * 1000;
392 /* Only while we're sleeping we'll let others run */
393 openocd_sleep_prelude();
395 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
396 openocd_sleep_postlude();
402 errno = WSAGetLastError();
404 if (errno == WSAEINTR)
407 LOG_ERROR("error during select: %s", strerror(errno));
415 LOG_ERROR("error during select: %s", strerror(errno));
422 /* We only execute these callbacks when there was nothing to do or we timed
424 target_call_timer_callbacks();
425 process_jim_events(command_context);
427 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
429 /* We timed out/there was nothing to do, timeout rather than poll next time
433 /* There was something to do, next time we'll just poll */
437 /* This is a simple back-off algorithm where we immediately
438 * re-poll if we did something this time around.
440 * This greatly improves performance of DCC.
442 poll_ok = poll_ok || target_got_message();
444 for (service = services; service; service = service->next) {
445 /* handle new connections on listeners */
446 if ((service->fd != -1)
447 && (FD_ISSET(service->fd, &read_fds))) {
448 if (service->max_connections > 0)
449 add_connection(service, command_context);
451 if (service->type == CONNECTION_TCP) {
452 struct sockaddr_in sin;
453 socklen_t address_size = sizeof(sin);
455 tmp_fd = accept(service->fd,
456 (struct sockaddr *)&service->sin,
458 close_socket(tmp_fd);
461 "rejected '%s' connection, no more connections allowed",
466 /* handle activity on connections */
467 if (service->connections) {
468 struct connection *c;
470 for (c = service->connections; c; ) {
471 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending) {
472 retval = service->input(c);
473 if (retval != ERROR_OK) {
474 struct connection *next = c->next;
475 if (service->type == CONNECTION_PIPE ||
476 service->type == CONNECTION_STDINOUT) {
477 /* if connection uses a pipe then
478 * shutdown openocd on error */
479 shutdown_openocd = 1;
481 remove_connection(service, c);
482 LOG_INFO("dropped '%s' connection",
495 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
496 if (msg.message == WM_QUIT)
497 shutdown_openocd = 1;
506 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
508 shutdown_openocd = 1;
513 void sig_handler(int sig)
515 /* store only first signal that hits us */
518 shutdown_openocd = 1;
521 int server_preinit(void)
523 /* this currently only calls WSAStartup on native win32 systems
524 * before any socket operations are performed.
525 * This is an issue if you call init in your config script */
528 WORD wVersionRequested;
531 wVersionRequested = MAKEWORD(2, 2);
533 if (WSAStartup(wVersionRequested, &wsaData) != 0) {
534 LOG_ERROR("Failed to Open Winsock");
538 /* register ctrl-c handler */
539 SetConsoleCtrlHandler(ControlHandler, TRUE);
541 signal(SIGBREAK, sig_handler);
543 signal(SIGINT, sig_handler);
544 signal(SIGTERM, sig_handler);
545 signal(SIGABRT, sig_handler);
550 int server_init(struct command_context *cmd_ctx)
552 int ret = tcl_init();
556 return telnet_init("Open On-Chip Debugger");
559 int server_quit(void)
566 SetConsoleCtrlHandler(ControlHandler, FALSE);
571 /* return signal number so we can kill ourselves */
575 void exit_on_signal(int sig)
578 /* bring back default system handler and kill yourself */
579 signal(sig, SIG_DFL);
584 int connection_write(struct connection *connection, const void *data, int len)
587 /* successful no-op. Sockets and pipes behave differently here... */
590 if (connection->service->type == CONNECTION_TCP)
591 return write_socket(connection->fd_out, data, len);
593 return write(connection->fd_out, data, len);
596 int connection_read(struct connection *connection, void *data, int len)
598 if (connection->service->type == CONNECTION_TCP)
599 return read_socket(connection->fd, data, len);
601 return read(connection->fd, data, len);
604 /* tell the server we want to shut down */
605 COMMAND_HANDLER(handle_shutdown_command)
607 LOG_USER("shutdown command invoked");
609 shutdown_openocd = 1;
611 return ERROR_COMMAND_CLOSE_CONNECTION;
614 COMMAND_HANDLER(handle_poll_period_command)
617 LOG_WARNING("You need to set a period value");
619 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], polling_period);
621 LOG_INFO("set servers polling period to %ums", polling_period);
626 static const struct command_registration server_command_handlers[] = {
629 .handler = &handle_shutdown_command,
632 .help = "shut the server down",
635 .name = "poll_period",
636 .handler = &handle_poll_period_command,
639 .help = "set the servers polling period",
641 COMMAND_REGISTRATION_DONE
644 int server_register_commands(struct command_context *cmd_ctx)
646 int retval = telnet_register_commands(cmd_ctx);
647 if (ERROR_OK != retval)
650 retval = tcl_register_commands(cmd_ctx);
651 if (ERROR_OK != retval)
654 retval = jsp_register_commands(cmd_ctx);
655 if (ERROR_OK != retval)
658 return register_commands(cmd_ctx, NULL, server_command_handlers);
661 COMMAND_HELPER(server_port_command, unsigned short *out)
665 command_print(CMD_CTX, "%d", *out);
670 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
675 return ERROR_COMMAND_SYNTAX_ERROR;
680 COMMAND_HELPER(server_pipe_command, char **out)
684 command_print(CMD_CTX, "%s", *out);
688 if (CMD_CTX->mode == COMMAND_EXEC) {
689 LOG_WARNING("unable to change server port after init");
690 return ERROR_COMMAND_ARGUMENT_INVALID;
693 *out = strdup(CMD_ARGV[0]);
697 return ERROR_COMMAND_SYNTAX_ERROR;