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 /* set the polling period to 100ms */
51 static int polling_period
= 100;
53 static int add_connection(struct service
*service
, struct command_context
*cmd_ctx
)
55 socklen_t address_size
;
56 struct connection
*c
, **p
;
60 c
= malloc(sizeof(struct connection
));
63 memset(&c
->sin
, 0, sizeof(c
->sin
));
64 c
->cmd_ctx
= copy_command_context(cmd_ctx
);
70 if (service
->type
== CONNECTION_TCP
) {
71 address_size
= sizeof(c
->sin
);
73 c
->fd
= accept(service
->fd
, (struct sockaddr
*)&service
->sin
, &address_size
);
76 /* This increases performance dramatically for e.g. GDB load which
77 * does not have a sliding window protocol.
79 * Ignore errors from this fn as it probably just means less performance
81 setsockopt(c
->fd
, /* socket affected */
82 IPPROTO_TCP
, /* set option at TCP level */
83 TCP_NODELAY
, /* name of option */
84 (char *)&flag
, /* the cast is historical cruft */
85 sizeof(int)); /* length of option value */
87 LOG_INFO("accepting '%s' connection on tcp/%s", service
->name
, service
->port
);
88 retval
= service
->new_connection(c
);
89 if (retval
!= ERROR_OK
) {
91 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
92 command_done(c
->cmd_ctx
);
96 } else if (service
->type
== CONNECTION_STDINOUT
) {
98 c
->fd_out
= fileno(stdout
);
101 /* we are using stdin/out so ignore ctrl-c under windoze */
102 SetConsoleCtrlHandler(NULL
, TRUE
);
105 /* do not check for new connections again on stdin */
108 LOG_INFO("accepting '%s' connection from pipe", service
->name
);
109 retval
= service
->new_connection(c
);
110 if (retval
!= ERROR_OK
) {
111 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
112 command_done(c
->cmd_ctx
);
116 } else if (service
->type
== CONNECTION_PIPE
) {
118 /* do not check for new connections again on stdin */
121 char *out_file
= alloc_printf("%so", service
->port
);
122 c
->fd_out
= open(out_file
, O_WRONLY
);
124 if (c
->fd_out
== -1) {
125 LOG_ERROR("could not open %s", service
->port
);
129 LOG_INFO("accepting '%s' connection from pipe %s", service
->name
, service
->port
);
130 retval
= service
->new_connection(c
);
131 if (retval
!= ERROR_OK
) {
132 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
133 command_done(c
->cmd_ctx
);
139 /* add to the end of linked list */
140 for (p
= &service
->connections
; *p
; p
= &(*p
)->next
)
144 service
->max_connections
--;
149 static int remove_connection(struct service
*service
, struct connection
*connection
)
151 struct connection
**p
= &service
->connections
;
152 struct connection
*c
;
154 /* find connection */
156 if (c
->fd
== connection
->fd
) {
157 service
->connection_closed(c
);
158 if (service
->type
== CONNECTION_TCP
)
160 else if (service
->type
== CONNECTION_PIPE
) {
161 /* The service will listen to the pipe again */
162 c
->service
->fd
= c
->fd
;
165 command_done(c
->cmd_ctx
);
167 /* delete connection */
171 service
->max_connections
++;
175 /* redirect p to next list pointer */
182 /* FIX! make service return error instead of invoking exit() */
183 int add_service(char *name
,
186 new_connection_handler_t new_connection_handler
,
187 input_handler_t input_handler
,
188 connection_closed_handler_t connection_closed_handler
,
191 struct service
*c
, **p
;
192 int so_reuseaddr_option
= 1;
194 c
= malloc(sizeof(struct service
));
196 c
->name
= strdup(name
);
197 c
->port
= strdup(port
);
198 c
->max_connections
= 1; /* Only TCP/IP ports can support more than one connection */
200 c
->connections
= NULL
;
201 c
->new_connection
= new_connection_handler
;
202 c
->input
= input_handler
;
203 c
->connection_closed
= connection_closed_handler
;
207 if (strcmp(c
->port
, "pipe") == 0)
208 c
->type
= CONNECTION_STDINOUT
;
211 portnumber
= strtol(c
->port
, &end
, 0);
212 if (!*end
&& (parse_long(c
->port
, &portnumber
) == ERROR_OK
)) {
213 c
->portnumber
= portnumber
;
214 c
->type
= CONNECTION_TCP
;
216 c
->type
= CONNECTION_PIPE
;
219 if (c
->type
== CONNECTION_TCP
) {
220 c
->max_connections
= max_connections
;
222 c
->fd
= socket(AF_INET
, SOCK_STREAM
, 0);
224 LOG_ERROR("error creating socket: %s", strerror(errno
));
231 (void *)&so_reuseaddr_option
,
234 socket_nonblock(c
->fd
);
236 memset(&c
->sin
, 0, sizeof(c
->sin
));
237 c
->sin
.sin_family
= AF_INET
;
238 c
->sin
.sin_addr
.s_addr
= INADDR_ANY
;
239 c
->sin
.sin_port
= htons(c
->portnumber
);
241 if (bind(c
->fd
, (struct sockaddr
*)&c
->sin
, sizeof(c
->sin
)) == -1) {
242 LOG_ERROR("couldn't bind to socket: %s", strerror(errno
));
248 setsockopt(c
->fd
, IPPROTO_TCP
, TCP_MAXSEG
, &segsize
, sizeof(int));
250 int window_size
= 128 * 1024;
252 /* These setsockopt()s must happen before the listen() */
254 setsockopt(c
->fd
, SOL_SOCKET
, SO_SNDBUF
,
255 (char *)&window_size
, sizeof(window_size
));
256 setsockopt(c
->fd
, SOL_SOCKET
, SO_RCVBUF
,
257 (char *)&window_size
, sizeof(window_size
));
259 if (listen(c
->fd
, 1) == -1) {
260 LOG_ERROR("couldn't listen on socket: %s", strerror(errno
));
263 } else if (c
->type
== CONNECTION_STDINOUT
) {
264 c
->fd
= fileno(stdin
);
267 /* for win32 set stdin/stdout to binary mode */
268 if (_setmode(_fileno(stdout
), _O_BINARY
) < 0)
269 LOG_WARNING("cannot change stdout mode to binary");
270 if (_setmode(_fileno(stdin
), _O_BINARY
) < 0)
271 LOG_WARNING("cannot change stdin mode to binary");
272 if (_setmode(_fileno(stderr
), _O_BINARY
) < 0)
273 LOG_WARNING("cannot change stderr mode to binary");
275 socket_nonblock(c
->fd
);
277 } else if (c
->type
== CONNECTION_PIPE
) {
279 /* we currenty do not support named pipes under win32
280 * so exit openocd for now */
281 LOG_ERROR("Named pipes currently not supported under this os");
284 /* Pipe we're reading from */
285 c
->fd
= open(c
->port
, O_RDONLY
| O_NONBLOCK
);
287 LOG_ERROR("could not open %s", c
->port
);
293 /* add to the end of linked list */
294 for (p
= &services
; *p
; p
= &(*p
)->next
)
301 static int remove_services(void)
303 struct service
*c
= services
;
307 struct service
*next
= c
->next
;
312 if (c
->type
== CONNECTION_PIPE
) {
325 /* remember the last service for unlinking */
334 int server_loop(struct command_context
*command_context
)
336 struct service
*service
;
340 /* used in select() */
344 /* used in accept() */
348 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
349 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
352 while (!shutdown_openocd
) {
353 /* monitor sockets for activity */
357 /* add service and connection fds to read_fds */
358 for (service
= services
; service
; service
= service
->next
) {
359 if (service
->fd
!= -1) {
360 /* listen for new connections */
361 FD_SET(service
->fd
, &read_fds
);
363 if (service
->fd
> fd_max
)
364 fd_max
= service
->fd
;
367 if (service
->connections
) {
368 struct connection
*c
;
370 for (c
= service
->connections
; c
; c
= c
->next
) {
371 /* check for activity on the connection */
372 FD_SET(c
->fd
, &read_fds
);
382 /* we're just polling this iteration, this is faster on embedded
385 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
387 /* Every 100ms, can be changed with "poll_period" command */
388 tv
.tv_usec
= polling_period
* 1000;
389 /* Only while we're sleeping we'll let others run */
390 openocd_sleep_prelude();
392 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
393 openocd_sleep_postlude();
399 errno
= WSAGetLastError();
401 if (errno
== WSAEINTR
)
404 LOG_ERROR("error during select: %s", strerror(errno
));
412 LOG_ERROR("error during select: %s", strerror(errno
));
419 /* We only execute these callbacks when there was nothing to do or we timed
421 target_call_timer_callbacks();
422 process_jim_events(command_context
);
424 FD_ZERO(&read_fds
); /* eCos leaves read_fds unchanged in this case! */
426 /* We timed out/there was nothing to do, timeout rather than poll next time
430 /* There was something to do, next time we'll just poll */
434 /* This is a simple back-off algorithm where we immediately
435 * re-poll if we did something this time around.
437 * This greatly improves performance of DCC.
439 poll_ok
= poll_ok
|| target_got_message();
441 for (service
= services
; service
; service
= service
->next
) {
442 /* handle new connections on listeners */
443 if ((service
->fd
!= -1)
444 && (FD_ISSET(service
->fd
, &read_fds
))) {
445 if (service
->max_connections
> 0)
446 add_connection(service
, command_context
);
448 if (service
->type
== CONNECTION_TCP
) {
449 struct sockaddr_in sin
;
450 socklen_t address_size
= sizeof(sin
);
452 tmp_fd
= accept(service
->fd
,
453 (struct sockaddr
*)&service
->sin
,
455 close_socket(tmp_fd
);
458 "rejected '%s' connection, no more connections allowed",
463 /* handle activity on connections */
464 if (service
->connections
) {
465 struct connection
*c
;
467 for (c
= service
->connections
; c
; ) {
468 if ((FD_ISSET(c
->fd
, &read_fds
)) || c
->input_pending
) {
469 retval
= service
->input(c
);
470 if (retval
!= ERROR_OK
) {
471 struct connection
*next
= c
->next
;
472 if (service
->type
== CONNECTION_PIPE
||
473 service
->type
== CONNECTION_STDINOUT
) {
474 /* if connection uses a pipe then
475 * shutdown openocd on error */
476 shutdown_openocd
= 1;
478 remove_connection(service
, c
);
479 LOG_INFO("dropped '%s' connection",
492 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
493 if (msg
.message
== WM_QUIT
)
494 shutdown_openocd
= 1;
503 BOOL WINAPI
ControlHandler(DWORD dwCtrlType
)
505 shutdown_openocd
= 1;
509 void sig_handler(int sig
)
511 shutdown_openocd
= 1;
515 int server_preinit(void)
517 /* this currently only calls WSAStartup on native win32 systems
518 * before any socket operations are performed.
519 * This is an issue if you call init in your config script */
522 WORD wVersionRequested
;
525 wVersionRequested
= MAKEWORD(2, 2);
527 if (WSAStartup(wVersionRequested
, &wsaData
) != 0) {
528 LOG_ERROR("Failed to Open Winsock");
532 /* register ctrl-c handler */
533 SetConsoleCtrlHandler(ControlHandler
, TRUE
);
535 signal(SIGINT
, sig_handler
);
536 signal(SIGTERM
, sig_handler
);
537 signal(SIGBREAK
, sig_handler
);
538 signal(SIGABRT
, sig_handler
);
544 int server_init(struct command_context
*cmd_ctx
)
546 int ret
= tcl_init();
550 return telnet_init("Open On-Chip Debugger");
553 int server_quit(void)
559 SetConsoleCtrlHandler(ControlHandler
, FALSE
);
565 int connection_write(struct connection
*connection
, const void *data
, int len
)
568 /* successful no-op. Sockets and pipes behave differently here... */
571 if (connection
->service
->type
== CONNECTION_TCP
)
572 return write_socket(connection
->fd_out
, data
, len
);
574 return write(connection
->fd_out
, data
, len
);
577 int connection_read(struct connection
*connection
, void *data
, int len
)
579 if (connection
->service
->type
== CONNECTION_TCP
)
580 return read_socket(connection
->fd
, data
, len
);
582 return read(connection
->fd
, data
, len
);
585 /* tell the server we want to shut down */
586 COMMAND_HANDLER(handle_shutdown_command
)
588 LOG_USER("shutdown command invoked");
590 shutdown_openocd
= 1;
595 COMMAND_HANDLER(handle_poll_period_command
)
598 LOG_WARNING("You need to set a period value");
600 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], polling_period
);
602 LOG_INFO("set servers polling period to %ums", polling_period
);
607 static const struct command_registration server_command_handlers
[] = {
610 .handler
= &handle_shutdown_command
,
613 .help
= "shut the server down",
616 .name
= "poll_period",
617 .handler
= &handle_poll_period_command
,
620 .help
= "set the servers polling period",
622 COMMAND_REGISTRATION_DONE
625 int server_register_commands(struct command_context
*cmd_ctx
)
627 int retval
= telnet_register_commands(cmd_ctx
);
628 if (ERROR_OK
!= retval
)
631 retval
= tcl_register_commands(cmd_ctx
);
632 if (ERROR_OK
!= retval
)
635 retval
= jsp_register_commands(cmd_ctx
);
636 if (ERROR_OK
!= retval
)
639 return register_commands(cmd_ctx
, NULL
, server_command_handlers
);
642 COMMAND_HELPER(server_port_command
, unsigned short *out
)
646 command_print(CMD_CTX
, "%d", *out
);
651 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], port
);
656 return ERROR_COMMAND_SYNTAX_ERROR
;
661 COMMAND_HELPER(server_pipe_command
, char **out
)
665 command_print(CMD_CTX
, "%s", *out
);
669 if (CMD_CTX
->mode
== COMMAND_EXEC
) {
670 LOG_WARNING("unable to change server port after init");
671 return ERROR_COMMAND_ARGUMENT_INVALID
;
674 *out
= strdup(CMD_ARGV
[0]);
678 return ERROR_COMMAND_SYNTAX_ERROR
;