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, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
30 #include <target/target.h>
31 #include <target/target_request.h>
32 #include <target/openrisc/jsp_server.h>
34 #include "tcl_server.h"
35 #include "telnet_server.h"
44 #include <netinet/tcp.h>
47 static struct service
*services
;
49 /* shutdown_openocd == 1: exit the main event loop, and quit the
50 * debugger; 2: quit with non-zero return code */
51 static int shutdown_openocd
;
53 /* store received signal to exit application by killing ourselves */
54 static int last_signal
;
56 /* set the polling period to 100ms */
57 static int polling_period
= 100;
59 /* address by name on which to listen for incoming TCP/IP connections */
60 static char *bindto_name
;
62 static int add_connection(struct service
*service
, struct command_context
*cmd_ctx
)
64 socklen_t address_size
;
65 struct connection
*c
, **p
;
69 c
= malloc(sizeof(struct connection
));
72 memset(&c
->sin
, 0, sizeof(c
->sin
));
73 c
->cmd_ctx
= copy_command_context(cmd_ctx
);
79 if (service
->type
== CONNECTION_TCP
) {
80 address_size
= sizeof(c
->sin
);
82 c
->fd
= accept(service
->fd
, (struct sockaddr
*)&service
->sin
, &address_size
);
85 /* This increases performance dramatically for e.g. GDB load which
86 * does not have a sliding window protocol.
88 * Ignore errors from this fn as it probably just means less performance
90 setsockopt(c
->fd
, /* socket affected */
91 IPPROTO_TCP
, /* set option at TCP level */
92 TCP_NODELAY
, /* name of option */
93 (char *)&flag
, /* the cast is historical cruft */
94 sizeof(int)); /* length of option value */
96 LOG_INFO("accepting '%s' connection on tcp/%s", service
->name
, service
->port
);
97 retval
= service
->new_connection(c
);
98 if (retval
!= ERROR_OK
) {
100 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
101 command_done(c
->cmd_ctx
);
105 } else if (service
->type
== CONNECTION_STDINOUT
) {
107 c
->fd_out
= fileno(stdout
);
110 /* we are using stdin/out so ignore ctrl-c under windoze */
111 SetConsoleCtrlHandler(NULL
, TRUE
);
114 /* do not check for new connections again on stdin */
117 LOG_INFO("accepting '%s' connection from pipe", service
->name
);
118 retval
= service
->new_connection(c
);
119 if (retval
!= ERROR_OK
) {
120 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
121 command_done(c
->cmd_ctx
);
125 } else if (service
->type
== CONNECTION_PIPE
) {
127 /* do not check for new connections again on stdin */
130 char *out_file
= alloc_printf("%so", service
->port
);
131 c
->fd_out
= open(out_file
, O_WRONLY
);
133 if (c
->fd_out
== -1) {
134 LOG_ERROR("could not open %s", service
->port
);
138 LOG_INFO("accepting '%s' connection from pipe %s", service
->name
, service
->port
);
139 retval
= service
->new_connection(c
);
140 if (retval
!= ERROR_OK
) {
141 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
142 command_done(c
->cmd_ctx
);
148 /* add to the end of linked list */
149 for (p
= &service
->connections
; *p
; p
= &(*p
)->next
)
153 if (service
->max_connections
!= CONNECTION_LIMIT_UNLIMITED
)
154 service
->max_connections
--;
159 static int remove_connection(struct service
*service
, struct connection
*connection
)
161 struct connection
**p
= &service
->connections
;
162 struct connection
*c
;
164 /* find connection */
166 if (c
->fd
== connection
->fd
) {
167 service
->connection_closed(c
);
168 if (service
->type
== CONNECTION_TCP
)
170 else if (service
->type
== CONNECTION_PIPE
) {
171 /* The service will listen to the pipe again */
172 c
->service
->fd
= c
->fd
;
175 command_done(c
->cmd_ctx
);
177 /* delete connection */
181 if (service
->max_connections
!= CONNECTION_LIMIT_UNLIMITED
)
182 service
->max_connections
++;
187 /* redirect p to next list pointer */
194 /* FIX! make service return error instead of invoking exit() */
195 int add_service(char *name
,
198 new_connection_handler_t new_connection_handler
,
199 input_handler_t input_handler
,
200 connection_closed_handler_t connection_closed_handler
,
203 struct service
*c
, **p
;
205 int so_reuseaddr_option
= 1;
207 c
= malloc(sizeof(struct service
));
209 c
->name
= strdup(name
);
210 c
->port
= strdup(port
);
211 c
->max_connections
= 1; /* Only TCP/IP ports can support more than one connection */
213 c
->connections
= NULL
;
214 c
->new_connection
= new_connection_handler
;
215 c
->input
= input_handler
;
216 c
->connection_closed
= connection_closed_handler
;
220 if (strcmp(c
->port
, "pipe") == 0)
221 c
->type
= CONNECTION_STDINOUT
;
224 portnumber
= strtol(c
->port
, &end
, 0);
225 if (!*end
&& (parse_long(c
->port
, &portnumber
) == ERROR_OK
)) {
226 c
->portnumber
= portnumber
;
227 c
->type
= CONNECTION_TCP
;
229 c
->type
= CONNECTION_PIPE
;
232 if (c
->type
== CONNECTION_TCP
) {
233 c
->max_connections
= max_connections
;
235 c
->fd
= socket(AF_INET
, SOCK_STREAM
, 0);
237 LOG_ERROR("error creating socket: %s", strerror(errno
));
244 (void *)&so_reuseaddr_option
,
247 socket_nonblock(c
->fd
);
249 memset(&c
->sin
, 0, sizeof(c
->sin
));
250 c
->sin
.sin_family
= AF_INET
;
252 if (bindto_name
== NULL
)
253 c
->sin
.sin_addr
.s_addr
= INADDR_ANY
;
255 hp
= gethostbyname(bindto_name
);
257 LOG_ERROR("couldn't resolve bindto address: %s", bindto_name
);
260 memcpy(&c
->sin
.sin_addr
, hp
->h_addr_list
[0], hp
->h_length
);
262 c
->sin
.sin_port
= htons(c
->portnumber
);
264 if (bind(c
->fd
, (struct sockaddr
*)&c
->sin
, sizeof(c
->sin
)) == -1) {
265 LOG_ERROR("couldn't bind %s to socket: %s", name
, strerror(errno
));
271 setsockopt(c
->fd
, IPPROTO_TCP
, TCP_MAXSEG
, &segsize
, sizeof(int));
273 int window_size
= 128 * 1024;
275 /* These setsockopt()s must happen before the listen() */
277 setsockopt(c
->fd
, SOL_SOCKET
, SO_SNDBUF
,
278 (char *)&window_size
, sizeof(window_size
));
279 setsockopt(c
->fd
, SOL_SOCKET
, SO_RCVBUF
,
280 (char *)&window_size
, sizeof(window_size
));
282 if (listen(c
->fd
, 1) == -1) {
283 LOG_ERROR("couldn't listen on socket: %s", strerror(errno
));
286 } else if (c
->type
== CONNECTION_STDINOUT
) {
287 c
->fd
= fileno(stdin
);
290 /* for win32 set stdin/stdout to binary mode */
291 if (_setmode(_fileno(stdout
), _O_BINARY
) < 0)
292 LOG_WARNING("cannot change stdout mode to binary");
293 if (_setmode(_fileno(stdin
), _O_BINARY
) < 0)
294 LOG_WARNING("cannot change stdin mode to binary");
295 if (_setmode(_fileno(stderr
), _O_BINARY
) < 0)
296 LOG_WARNING("cannot change stderr mode to binary");
298 socket_nonblock(c
->fd
);
300 } else if (c
->type
== CONNECTION_PIPE
) {
302 /* we currenty do not support named pipes under win32
303 * so exit openocd for now */
304 LOG_ERROR("Named pipes currently not supported under this os");
307 /* Pipe we're reading from */
308 c
->fd
= open(c
->port
, O_RDONLY
| O_NONBLOCK
);
310 LOG_ERROR("could not open %s", c
->port
);
316 /* add to the end of linked list */
317 for (p
= &services
; *p
; p
= &(*p
)->next
)
324 static int remove_services(void)
326 struct service
*c
= services
;
330 struct service
*next
= c
->next
;
335 if (c
->type
== CONNECTION_PIPE
) {
348 /* remember the last service for unlinking */
357 int server_loop(struct command_context
*command_context
)
359 struct service
*service
;
363 /* used in select() */
367 /* used in accept() */
371 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
372 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
375 while (!shutdown_openocd
) {
376 /* monitor sockets for activity */
380 /* add service and connection fds to read_fds */
381 for (service
= services
; service
; service
= service
->next
) {
382 if (service
->fd
!= -1) {
383 /* listen for new connections */
384 FD_SET(service
->fd
, &read_fds
);
386 if (service
->fd
> fd_max
)
387 fd_max
= service
->fd
;
390 if (service
->connections
) {
391 struct connection
*c
;
393 for (c
= service
->connections
; c
; c
= c
->next
) {
394 /* check for activity on the connection */
395 FD_SET(c
->fd
, &read_fds
);
405 /* we're just polling this iteration, this is faster on embedded
408 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
410 /* Every 100ms, can be changed with "poll_period" command */
411 tv
.tv_usec
= polling_period
* 1000;
412 /* Only while we're sleeping we'll let others run */
413 openocd_sleep_prelude();
415 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
416 openocd_sleep_postlude();
422 errno
= WSAGetLastError();
424 if (errno
== WSAEINTR
)
427 LOG_ERROR("error during select: %s", strerror(errno
));
435 LOG_ERROR("error during select: %s", strerror(errno
));
442 /* We only execute these callbacks when there was nothing to do or we timed
444 target_call_timer_callbacks();
445 process_jim_events(command_context
);
447 FD_ZERO(&read_fds
); /* eCos leaves read_fds unchanged in this case! */
449 /* We timed out/there was nothing to do, timeout rather than poll next time
453 /* There was something to do, next time we'll just poll */
457 /* This is a simple back-off algorithm where we immediately
458 * re-poll if we did something this time around.
460 * This greatly improves performance of DCC.
462 poll_ok
= poll_ok
|| target_got_message();
464 for (service
= services
; service
; service
= service
->next
) {
465 /* handle new connections on listeners */
466 if ((service
->fd
!= -1)
467 && (FD_ISSET(service
->fd
, &read_fds
))) {
468 if (service
->max_connections
!= 0)
469 add_connection(service
, command_context
);
471 if (service
->type
== CONNECTION_TCP
) {
472 struct sockaddr_in sin
;
473 socklen_t address_size
= sizeof(sin
);
475 tmp_fd
= accept(service
->fd
,
476 (struct sockaddr
*)&service
->sin
,
478 close_socket(tmp_fd
);
481 "rejected '%s' connection, no more connections allowed",
486 /* handle activity on connections */
487 if (service
->connections
) {
488 struct connection
*c
;
490 for (c
= service
->connections
; c
; ) {
491 if ((FD_ISSET(c
->fd
, &read_fds
)) || c
->input_pending
) {
492 retval
= service
->input(c
);
493 if (retval
!= ERROR_OK
) {
494 struct connection
*next
= c
->next
;
495 if (service
->type
== CONNECTION_PIPE
||
496 service
->type
== CONNECTION_STDINOUT
) {
497 /* if connection uses a pipe then
498 * shutdown openocd on error */
499 shutdown_openocd
= 1;
501 remove_connection(service
, c
);
502 LOG_INFO("dropped '%s' connection",
515 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
516 if (msg
.message
== WM_QUIT
)
517 shutdown_openocd
= 1;
522 return shutdown_openocd
!= 2 ? ERROR_OK
: ERROR_FAIL
;
526 BOOL WINAPI
ControlHandler(DWORD dwCtrlType
)
528 shutdown_openocd
= 1;
533 void sig_handler(int sig
)
535 /* store only first signal that hits us */
538 shutdown_openocd
= 1;
541 int server_preinit(void)
543 /* this currently only calls WSAStartup on native win32 systems
544 * before any socket operations are performed.
545 * This is an issue if you call init in your config script */
548 WORD wVersionRequested
;
551 wVersionRequested
= MAKEWORD(2, 2);
553 if (WSAStartup(wVersionRequested
, &wsaData
) != 0) {
554 LOG_ERROR("Failed to Open Winsock");
558 /* register ctrl-c handler */
559 SetConsoleCtrlHandler(ControlHandler
, TRUE
);
561 signal(SIGBREAK
, sig_handler
);
563 signal(SIGINT
, sig_handler
);
564 signal(SIGTERM
, sig_handler
);
565 signal(SIGABRT
, sig_handler
);
570 int server_init(struct command_context
*cmd_ctx
)
572 int ret
= tcl_init();
576 return telnet_init("Open On-Chip Debugger");
579 int server_quit(void)
586 SetConsoleCtrlHandler(ControlHandler
, FALSE
);
591 /* return signal number so we can kill ourselves */
595 void exit_on_signal(int sig
)
598 /* bring back default system handler and kill yourself */
599 signal(sig
, SIG_DFL
);
604 int connection_write(struct connection
*connection
, const void *data
, int len
)
607 /* successful no-op. Sockets and pipes behave differently here... */
610 if (connection
->service
->type
== CONNECTION_TCP
)
611 return write_socket(connection
->fd_out
, data
, len
);
613 return write(connection
->fd_out
, data
, len
);
616 int connection_read(struct connection
*connection
, void *data
, int len
)
618 if (connection
->service
->type
== CONNECTION_TCP
)
619 return read_socket(connection
->fd
, data
, len
);
621 return read(connection
->fd
, data
, len
);
624 /* tell the server we want to shut down */
625 COMMAND_HANDLER(handle_shutdown_command
)
627 LOG_USER("shutdown command invoked");
629 shutdown_openocd
= 1;
632 if (!strcmp(CMD_ARGV
[0], "error")) {
633 shutdown_openocd
= 2;
638 return ERROR_COMMAND_CLOSE_CONNECTION
;
641 COMMAND_HANDLER(handle_poll_period_command
)
644 LOG_WARNING("You need to set a period value");
646 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], polling_period
);
648 LOG_INFO("set servers polling period to %ums", polling_period
);
653 COMMAND_HANDLER(handle_bindto_command
)
657 command_print(CMD_CTX
, "bindto name: %s", bindto_name
);
661 bindto_name
= strdup(CMD_ARGV
[0]);
664 return ERROR_COMMAND_SYNTAX_ERROR
;
669 static const struct command_registration server_command_handlers
[] = {
672 .handler
= &handle_shutdown_command
,
675 .help
= "shut the server down",
678 .name
= "poll_period",
679 .handler
= &handle_poll_period_command
,
682 .help
= "set the servers polling period",
686 .handler
= &handle_bindto_command
,
689 .help
= "Specify address by name on which to listen for "
690 "incoming TCP/IP connections",
692 COMMAND_REGISTRATION_DONE
695 int server_register_commands(struct command_context
*cmd_ctx
)
697 int retval
= telnet_register_commands(cmd_ctx
);
698 if (ERROR_OK
!= retval
)
701 retval
= tcl_register_commands(cmd_ctx
);
702 if (ERROR_OK
!= retval
)
705 retval
= jsp_register_commands(cmd_ctx
);
706 if (ERROR_OK
!= retval
)
709 return register_commands(cmd_ctx
, NULL
, server_command_handlers
);
712 COMMAND_HELPER(server_port_command
, unsigned short *out
)
716 command_print(CMD_CTX
, "%d", *out
);
721 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], port
);
726 return ERROR_COMMAND_SYNTAX_ERROR
;
731 COMMAND_HELPER(server_pipe_command
, char **out
)
735 command_print(CMD_CTX
, "%s", *out
);
739 if (CMD_CTX
->mode
== COMMAND_EXEC
) {
740 LOG_WARNING("unable to change server port after init");
741 return ERROR_COMMAND_ARGUMENT_INVALID
;
744 *out
= strdup(CMD_ARGV
[0]);
748 return ERROR_COMMAND_SYNTAX_ERROR
;