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 <helper/time_support.h>
31 #include <target/target.h>
32 #include <target/target_request.h>
33 #include <target/openrisc/jsp_server.h>
35 #include "tcl_server.h"
36 #include "telnet_server.h"
45 #include <netinet/tcp.h>
48 static struct service
*services
;
50 enum shutdown_reason
{
51 CONTINUE_MAIN_LOOP
, /* stay in main event loop */
52 SHUTDOWN_REQUESTED
, /* set by shutdown command; exit the event loop and quit the debugger */
53 SHUTDOWN_WITH_ERROR_CODE
, /* set by shutdown command; quit with non-zero return code */
54 SHUTDOWN_WITH_SIGNAL_CODE
/* set by sig_handler; exec shutdown then exit with signal as return code */
56 static enum shutdown_reason shutdown_openocd
= CONTINUE_MAIN_LOOP
;
58 /* store received signal to exit application by killing ourselves */
59 static int last_signal
;
61 /* set the polling period to 100ms */
62 static int polling_period
= 100;
64 /* address by name on which to listen for incoming TCP/IP connections */
65 static char *bindto_name
;
67 static int add_connection(struct service
*service
, struct command_context
*cmd_ctx
)
69 socklen_t address_size
;
70 struct connection
*c
, **p
;
74 c
= malloc(sizeof(struct connection
));
77 memset(&c
->sin
, 0, sizeof(c
->sin
));
78 c
->cmd_ctx
= copy_command_context(cmd_ctx
);
80 c
->input_pending
= false;
84 if (service
->type
== CONNECTION_TCP
) {
85 address_size
= sizeof(c
->sin
);
87 c
->fd
= accept(service
->fd
, (struct sockaddr
*)&service
->sin
, &address_size
);
90 /* This increases performance dramatically for e.g. GDB load which
91 * does not have a sliding window protocol.
93 * Ignore errors from this fn as it probably just means less performance
95 setsockopt(c
->fd
, /* socket affected */
96 IPPROTO_TCP
, /* set option at TCP level */
97 TCP_NODELAY
, /* name of option */
98 (char *)&flag
, /* the cast is historical cruft */
99 sizeof(int)); /* length of option value */
101 LOG_INFO("accepting '%s' connection on tcp/%s", service
->name
, service
->port
);
102 retval
= service
->new_connection(c
);
103 if (retval
!= ERROR_OK
) {
105 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
106 command_done(c
->cmd_ctx
);
110 } else if (service
->type
== CONNECTION_STDINOUT
) {
112 c
->fd_out
= fileno(stdout
);
115 /* we are using stdin/out so ignore ctrl-c under windoze */
116 SetConsoleCtrlHandler(NULL
, TRUE
);
119 /* do not check for new connections again on stdin */
122 LOG_INFO("accepting '%s' connection from pipe", service
->name
);
123 retval
= service
->new_connection(c
);
124 if (retval
!= ERROR_OK
) {
125 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
126 command_done(c
->cmd_ctx
);
130 } else if (service
->type
== CONNECTION_PIPE
) {
132 /* do not check for new connections again on stdin */
135 char *out_file
= alloc_printf("%so", service
->port
);
136 c
->fd_out
= open(out_file
, O_WRONLY
);
138 if (c
->fd_out
== -1) {
139 LOG_ERROR("could not open %s", service
->port
);
140 command_done(c
->cmd_ctx
);
145 LOG_INFO("accepting '%s' connection from pipe %s", service
->name
, service
->port
);
146 retval
= service
->new_connection(c
);
147 if (retval
!= ERROR_OK
) {
148 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
149 command_done(c
->cmd_ctx
);
155 /* add to the end of linked list */
156 for (p
= &service
->connections
; *p
; p
= &(*p
)->next
)
160 if (service
->max_connections
!= CONNECTION_LIMIT_UNLIMITED
)
161 service
->max_connections
--;
166 static int remove_connection(struct service
*service
, struct connection
*connection
)
168 struct connection
**p
= &service
->connections
;
169 struct connection
*c
;
171 /* find connection */
173 if (c
->fd
== connection
->fd
) {
174 service
->connection_closed(c
);
175 if (service
->type
== CONNECTION_TCP
)
177 else if (service
->type
== CONNECTION_PIPE
) {
178 /* The service will listen to the pipe again */
179 c
->service
->fd
= c
->fd
;
182 command_done(c
->cmd_ctx
);
184 /* delete connection */
188 if (service
->max_connections
!= CONNECTION_LIMIT_UNLIMITED
)
189 service
->max_connections
++;
194 /* redirect p to next list pointer */
201 static void free_service(struct service
*c
)
208 int add_service(const struct service_driver
*driver
, const char *port
,
209 int max_connections
, void *priv
)
211 struct service
*c
, **p
;
213 int so_reuseaddr_option
= 1;
215 c
= malloc(sizeof(struct service
));
217 c
->name
= strdup(driver
->name
);
218 c
->port
= strdup(port
);
219 c
->max_connections
= 1; /* Only TCP/IP ports can support more than one connection */
221 c
->connections
= NULL
;
222 c
->new_connection_during_keep_alive
= driver
->new_connection_during_keep_alive_handler
;
223 c
->new_connection
= driver
->new_connection_handler
;
224 c
->input
= driver
->input_handler
;
225 c
->connection_closed
= driver
->connection_closed_handler
;
226 c
->keep_client_alive
= driver
->keep_client_alive_handler
;
230 if (strcmp(c
->port
, "pipe") == 0)
231 c
->type
= CONNECTION_STDINOUT
;
234 portnumber
= strtol(c
->port
, &end
, 0);
235 if (!*end
&& (parse_long(c
->port
, &portnumber
) == ERROR_OK
)) {
236 c
->portnumber
= portnumber
;
237 c
->type
= CONNECTION_TCP
;
239 c
->type
= CONNECTION_PIPE
;
242 if (c
->type
== CONNECTION_TCP
) {
243 c
->max_connections
= max_connections
;
245 c
->fd
= socket(AF_INET
, SOCK_STREAM
, 0);
247 LOG_ERROR("error creating socket: %s", strerror(errno
));
255 (void *)&so_reuseaddr_option
,
258 socket_nonblock(c
->fd
);
260 memset(&c
->sin
, 0, sizeof(c
->sin
));
261 c
->sin
.sin_family
= AF_INET
;
264 c
->sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
266 hp
= gethostbyname(bindto_name
);
268 LOG_ERROR("couldn't resolve bindto address: %s", bindto_name
);
273 memcpy(&c
->sin
.sin_addr
, hp
->h_addr_list
[0], hp
->h_length
);
275 c
->sin
.sin_port
= htons(c
->portnumber
);
277 if (bind(c
->fd
, (struct sockaddr
*)&c
->sin
, sizeof(c
->sin
)) == -1) {
278 LOG_ERROR("couldn't bind %s to socket on port %d: %s", c
->name
, c
->portnumber
, strerror(errno
));
286 setsockopt(c
->fd
, IPPROTO_TCP
, TCP_MAXSEG
, &segsize
, sizeof(int));
288 int window_size
= 128 * 1024;
290 /* These setsockopt()s must happen before the listen() */
292 setsockopt(c
->fd
, SOL_SOCKET
, SO_SNDBUF
,
293 (char *)&window_size
, sizeof(window_size
));
294 setsockopt(c
->fd
, SOL_SOCKET
, SO_RCVBUF
,
295 (char *)&window_size
, sizeof(window_size
));
297 if (listen(c
->fd
, 1) == -1) {
298 LOG_ERROR("couldn't listen on socket: %s", strerror(errno
));
304 struct sockaddr_in addr_in
;
305 addr_in
.sin_port
= 0;
306 socklen_t addr_in_size
= sizeof(addr_in
);
307 if (getsockname(c
->fd
, (struct sockaddr
*)&addr_in
, &addr_in_size
) == 0)
308 LOG_INFO("Listening on port %hu for %s connections",
309 ntohs(addr_in
.sin_port
), c
->name
);
310 } else if (c
->type
== CONNECTION_STDINOUT
) {
311 c
->fd
= fileno(stdin
);
314 /* for win32 set stdin/stdout to binary mode */
315 if (_setmode(_fileno(stdout
), _O_BINARY
) < 0)
316 LOG_WARNING("cannot change stdout mode to binary");
317 if (_setmode(_fileno(stdin
), _O_BINARY
) < 0)
318 LOG_WARNING("cannot change stdin mode to binary");
319 if (_setmode(_fileno(stderr
), _O_BINARY
) < 0)
320 LOG_WARNING("cannot change stderr mode to binary");
322 socket_nonblock(c
->fd
);
324 } else if (c
->type
== CONNECTION_PIPE
) {
326 /* we currently do not support named pipes under win32
327 * so exit openocd for now */
328 LOG_ERROR("Named pipes currently not supported under this os");
332 /* Pipe we're reading from */
333 c
->fd
= open(c
->port
, O_RDONLY
| O_NONBLOCK
);
335 LOG_ERROR("could not open %s", c
->port
);
342 /* add to the end of linked list */
343 for (p
= &services
; *p
; p
= &(*p
)->next
)
350 static void remove_connections(struct service
*service
)
352 struct connection
*connection
;
354 connection
= service
->connections
;
357 struct connection
*tmp
;
359 tmp
= connection
->next
;
360 remove_connection(service
, connection
);
365 int remove_service(const char *name
, const char *port
)
368 struct service
*prev
;
372 for (tmp
= services
; tmp
; prev
= tmp
, tmp
= tmp
->next
) {
373 if (!strcmp(tmp
->name
, name
) && !strcmp(tmp
->port
, port
)) {
374 remove_connections(tmp
);
377 services
= tmp
->next
;
379 prev
->next
= tmp
->next
;
381 if (tmp
->type
!= CONNECTION_STDINOUT
)
382 close_socket(tmp
->fd
);
394 static int remove_services(void)
396 struct service
*c
= services
;
400 struct service
*next
= c
->next
;
402 remove_connections(c
);
406 if (c
->type
== CONNECTION_PIPE
) {
415 /* remember the last service for unlinking */
424 void server_keep_clients_alive(void)
426 for (struct service
*s
= services
; s
; s
= s
->next
)
427 if (s
->keep_client_alive
)
428 for (struct connection
*c
= s
->connections
; c
; c
= c
->next
)
429 s
->keep_client_alive(c
);
432 int server_loop(struct command_context
*command_context
)
434 struct service
*service
;
438 /* used in select() */
442 /* used in accept() */
445 int64_t next_event
= timeval_ms() + polling_period
;
448 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
449 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
452 while (shutdown_openocd
== CONTINUE_MAIN_LOOP
) {
453 /* monitor sockets for activity */
457 /* add service and connection fds to read_fds */
458 for (service
= services
; service
; service
= service
->next
) {
459 if (service
->fd
!= -1) {
460 /* listen for new connections */
461 FD_SET(service
->fd
, &read_fds
);
463 if (service
->fd
> fd_max
)
464 fd_max
= service
->fd
;
467 if (service
->connections
) {
468 struct connection
*c
;
470 for (c
= service
->connections
; c
; c
= c
->next
) {
471 /* check for activity on the connection */
472 FD_SET(c
->fd
, &read_fds
);
482 /* we're just polling this iteration, this is faster on embedded
485 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
487 /* Every 100ms, can be changed with "poll_period" command */
488 int timeout_ms
= next_event
- timeval_ms();
491 else if (timeout_ms
> polling_period
)
492 timeout_ms
= polling_period
;
493 tv
.tv_usec
= timeout_ms
* 1000;
494 /* Only while we're sleeping we'll let others run */
495 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
501 errno
= WSAGetLastError();
503 if (errno
== WSAEINTR
)
506 LOG_ERROR("error during select: %s", strerror(errno
));
514 LOG_ERROR("error during select: %s", strerror(errno
));
521 /* We only execute these callbacks when there was nothing to do or we timed
523 target_call_timer_callbacks_now();
524 next_event
= target_timer_next_event();
525 process_jim_events(command_context
);
527 FD_ZERO(&read_fds
); /* eCos leaves read_fds unchanged in this case! */
529 /* We timed out/there was nothing to do, timeout rather than poll next time
533 /* There was something to do, next time we'll just poll */
537 /* This is a simple back-off algorithm where we immediately
538 * re-poll if we did something this time around.
540 * This greatly improves performance of DCC.
542 poll_ok
= poll_ok
|| target_got_message();
544 for (service
= services
; service
; service
= service
->next
) {
545 /* handle new connections on listeners */
546 if ((service
->fd
!= -1)
547 && (FD_ISSET(service
->fd
, &read_fds
))) {
548 if (service
->max_connections
!= 0)
549 add_connection(service
, command_context
);
551 if (service
->type
== CONNECTION_TCP
) {
552 struct sockaddr_in sin
;
553 socklen_t address_size
= sizeof(sin
);
555 tmp_fd
= accept(service
->fd
,
556 (struct sockaddr
*)&service
->sin
,
558 close_socket(tmp_fd
);
561 "rejected '%s' connection, no more connections allowed",
566 /* handle activity on connections */
567 if (service
->connections
) {
568 struct connection
*c
;
570 for (c
= service
->connections
; c
; ) {
571 if ((c
->fd
>= 0 && FD_ISSET(c
->fd
, &read_fds
)) || c
->input_pending
) {
572 retval
= service
->input(c
);
573 if (retval
!= ERROR_OK
) {
574 struct connection
*next
= c
->next
;
575 if (service
->type
== CONNECTION_PIPE
||
576 service
->type
== CONNECTION_STDINOUT
) {
577 /* if connection uses a pipe then
578 * shutdown openocd on error */
579 shutdown_openocd
= SHUTDOWN_REQUESTED
;
581 remove_connection(service
, c
);
582 LOG_INFO("dropped '%s' connection",
595 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
596 if (msg
.message
== WM_QUIT
)
597 shutdown_openocd
= SHUTDOWN_WITH_SIGNAL_CODE
;
602 /* when quit for signal or CTRL-C, run (eventually user implemented) "shutdown" */
603 if (shutdown_openocd
== SHUTDOWN_WITH_SIGNAL_CODE
)
604 command_run_line(command_context
, "shutdown");
606 return shutdown_openocd
== SHUTDOWN_WITH_ERROR_CODE
? ERROR_FAIL
: ERROR_OK
;
609 static void sig_handler(int sig
)
611 /* store only first signal that hits us */
612 if (shutdown_openocd
== CONTINUE_MAIN_LOOP
) {
613 shutdown_openocd
= SHUTDOWN_WITH_SIGNAL_CODE
;
615 LOG_DEBUG("Terminating on Signal %d", sig
);
617 LOG_DEBUG("Ignored extra Signal %d", sig
);
622 BOOL WINAPI
control_handler(DWORD ctrl_type
)
624 shutdown_openocd
= SHUTDOWN_WITH_SIGNAL_CODE
;
628 static void sigkey_handler(int sig
)
630 /* ignore keystroke generated signals if not in foreground process group */
632 if (tcgetpgrp(STDIN_FILENO
) > 0)
635 LOG_DEBUG("Ignored Signal %d", sig
);
640 int server_host_os_entry(void)
642 /* this currently only calls WSAStartup on native win32 systems
643 * before any socket operations are performed.
644 * This is an issue if you call init in your config script */
647 WORD version_requested
;
650 version_requested
= MAKEWORD(2, 2);
652 if (WSAStartup(version_requested
, &wsadata
) != 0) {
653 LOG_ERROR("Failed to Open Winsock");
660 int server_host_os_close(void)
668 int server_preinit(void)
671 /* register ctrl-c handler */
672 SetConsoleCtrlHandler(control_handler
, TRUE
);
674 signal(SIGBREAK
, sig_handler
);
675 signal(SIGINT
, sig_handler
);
677 signal(SIGHUP
, sig_handler
);
678 signal(SIGPIPE
, sig_handler
);
679 signal(SIGQUIT
, sigkey_handler
);
680 signal(SIGINT
, sigkey_handler
);
682 signal(SIGTERM
, sig_handler
);
683 signal(SIGABRT
, sig_handler
);
688 int server_init(struct command_context
*cmd_ctx
)
690 int ret
= tcl_init();
695 ret
= telnet_init("Open On-Chip Debugger");
697 if (ret
!= ERROR_OK
) {
705 int server_quit(void)
711 SetConsoleCtrlHandler(control_handler
, FALSE
);
716 /* return signal number so we can kill ourselves */
720 void server_free(void)
723 telnet_service_free();
729 void exit_on_signal(int sig
)
732 /* bring back default system handler and kill yourself */
733 signal(sig
, SIG_DFL
);
738 int connection_write(struct connection
*connection
, const void *data
, int len
)
741 /* successful no-op. Sockets and pipes behave differently here... */
744 if (connection
->service
->type
== CONNECTION_TCP
)
745 return write_socket(connection
->fd_out
, data
, len
);
747 return write(connection
->fd_out
, data
, len
);
750 int connection_read(struct connection
*connection
, void *data
, int len
)
752 if (connection
->service
->type
== CONNECTION_TCP
)
753 return read_socket(connection
->fd
, data
, len
);
755 return read(connection
->fd
, data
, len
);
758 /* tell the server we want to shut down */
759 COMMAND_HANDLER(handle_shutdown_command
)
761 LOG_USER("shutdown command invoked");
763 shutdown_openocd
= SHUTDOWN_REQUESTED
;
765 command_run_line(CMD_CTX
, "_run_pre_shutdown_commands");
768 if (!strcmp(CMD_ARGV
[0], "error")) {
769 shutdown_openocd
= SHUTDOWN_WITH_ERROR_CODE
;
774 return ERROR_COMMAND_CLOSE_CONNECTION
;
777 COMMAND_HANDLER(handle_poll_period_command
)
780 LOG_WARNING("You need to set a period value");
782 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], polling_period
);
784 LOG_INFO("set servers polling period to %ums", polling_period
);
789 COMMAND_HANDLER(handle_bindto_command
)
793 command_print(CMD
, "bindto name: %s", bindto_name
);
797 bindto_name
= strdup(CMD_ARGV
[0]);
800 return ERROR_COMMAND_SYNTAX_ERROR
;
805 static const struct command_registration server_command_handlers
[] = {
808 .handler
= &handle_shutdown_command
,
811 .help
= "shut the server down",
814 .name
= "poll_period",
815 .handler
= &handle_poll_period_command
,
818 .help
= "set the servers polling period",
822 .handler
= &handle_bindto_command
,
823 .mode
= COMMAND_CONFIG
,
825 .help
= "Specify address by name on which to listen for "
826 "incoming TCP/IP connections",
828 COMMAND_REGISTRATION_DONE
831 int server_register_commands(struct command_context
*cmd_ctx
)
833 int retval
= telnet_register_commands(cmd_ctx
);
834 if (retval
!= ERROR_OK
)
837 retval
= tcl_register_commands(cmd_ctx
);
838 if (retval
!= ERROR_OK
)
841 retval
= jsp_register_commands(cmd_ctx
);
842 if (retval
!= ERROR_OK
)
845 return register_commands(cmd_ctx
, NULL
, server_command_handlers
);
848 COMMAND_HELPER(server_port_command
, unsigned short *out
)
852 command_print(CMD
, "%d", *out
);
857 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], port
);
862 return ERROR_COMMAND_SYNTAX_ERROR
;
867 COMMAND_HELPER(server_pipe_command
, char **out
)
871 command_print(CMD
, "%s", *out
);
875 if (CMD_CTX
->mode
== COMMAND_EXEC
) {
876 LOG_WARNING("unable to change server port after init");
877 return ERROR_COMMAND_ARGUMENT_INVALID
;
880 *out
= strdup(CMD_ARGV
[0]);
884 return ERROR_COMMAND_SYNTAX_ERROR
;
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)