1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2007-2010 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
10 * Copyright (C) 2008 by Spencer Oliver *
11 * spen@spen-soft.co.uk *
12 ***************************************************************************/
19 #include <helper/time_support.h>
20 #include <target/target.h>
21 #include <target/target_request.h>
22 #include <target/openrisc/jsp_server.h>
24 #include "tcl_server.h"
25 #include "telnet_server.h"
34 #include <netinet/tcp.h>
37 static struct service
*services
;
39 enum shutdown_reason
{
40 CONTINUE_MAIN_LOOP
, /* stay in main event loop */
41 SHUTDOWN_REQUESTED
, /* set by shutdown command; exit the event loop and quit the debugger */
42 SHUTDOWN_WITH_ERROR_CODE
, /* set by shutdown command; quit with non-zero return code */
43 SHUTDOWN_WITH_SIGNAL_CODE
/* set by sig_handler; exec shutdown then exit with signal as return code */
45 static enum shutdown_reason shutdown_openocd
= CONTINUE_MAIN_LOOP
;
47 /* store received signal to exit application by killing ourselves */
48 static int last_signal
;
50 /* set the polling period to 100ms */
51 static int polling_period
= 100;
53 /* address by name on which to listen for incoming TCP/IP connections */
54 static char *bindto_name
;
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
);
69 c
->input_pending
= false;
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
);
129 command_done(c
->cmd_ctx
);
134 LOG_INFO("accepting '%s' connection from pipe %s", service
->name
, service
->port
);
135 retval
= service
->new_connection(c
);
136 if (retval
!= ERROR_OK
) {
137 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
138 command_done(c
->cmd_ctx
);
144 /* add to the end of linked list */
145 for (p
= &service
->connections
; *p
; p
= &(*p
)->next
)
149 if (service
->max_connections
!= CONNECTION_LIMIT_UNLIMITED
)
150 service
->max_connections
--;
155 static int remove_connection(struct service
*service
, struct connection
*connection
)
157 struct connection
**p
= &service
->connections
;
158 struct connection
*c
;
160 /* find connection */
162 if (c
->fd
== connection
->fd
) {
163 service
->connection_closed(c
);
164 if (service
->type
== CONNECTION_TCP
)
166 else if (service
->type
== CONNECTION_PIPE
) {
167 /* The service will listen to the pipe again */
168 c
->service
->fd
= c
->fd
;
171 command_done(c
->cmd_ctx
);
173 /* delete connection */
177 if (service
->max_connections
!= CONNECTION_LIMIT_UNLIMITED
)
178 service
->max_connections
++;
183 /* redirect p to next list pointer */
190 static void free_service(struct service
*c
)
197 int add_service(const struct service_driver
*driver
, const char *port
,
198 int max_connections
, void *priv
)
200 struct service
*c
, **p
;
202 int so_reuseaddr_option
= 1;
204 c
= malloc(sizeof(struct service
));
206 c
->name
= strdup(driver
->name
);
207 c
->port
= strdup(port
);
208 c
->max_connections
= 1; /* Only TCP/IP ports can support more than one connection */
210 c
->connections
= NULL
;
211 c
->new_connection_during_keep_alive
= driver
->new_connection_during_keep_alive_handler
;
212 c
->new_connection
= driver
->new_connection_handler
;
213 c
->input
= driver
->input_handler
;
214 c
->connection_closed
= driver
->connection_closed_handler
;
215 c
->keep_client_alive
= driver
->keep_client_alive_handler
;
219 if (strcmp(c
->port
, "pipe") == 0)
220 c
->type
= CONNECTION_STDINOUT
;
223 portnumber
= strtol(c
->port
, &end
, 0);
224 if (!*end
&& (parse_long(c
->port
, &portnumber
) == ERROR_OK
)) {
225 c
->portnumber
= portnumber
;
226 c
->type
= CONNECTION_TCP
;
228 c
->type
= CONNECTION_PIPE
;
231 if (c
->type
== CONNECTION_TCP
) {
232 c
->max_connections
= max_connections
;
234 c
->fd
= socket(AF_INET
, SOCK_STREAM
, 0);
236 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
;
253 c
->sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
255 hp
= gethostbyname(bindto_name
);
257 LOG_ERROR("couldn't resolve bindto address: %s", bindto_name
);
262 memcpy(&c
->sin
.sin_addr
, hp
->h_addr_list
[0], hp
->h_length
);
264 c
->sin
.sin_port
= htons(c
->portnumber
);
266 if (bind(c
->fd
, (struct sockaddr
*)&c
->sin
, sizeof(c
->sin
)) == -1) {
267 LOG_ERROR("couldn't bind %s to socket on port %d: %s", c
->name
, c
->portnumber
, strerror(errno
));
275 setsockopt(c
->fd
, IPPROTO_TCP
, TCP_MAXSEG
, &segsize
, sizeof(int));
277 int window_size
= 128 * 1024;
279 /* These setsockopt()s must happen before the listen() */
281 setsockopt(c
->fd
, SOL_SOCKET
, SO_SNDBUF
,
282 (char *)&window_size
, sizeof(window_size
));
283 setsockopt(c
->fd
, SOL_SOCKET
, SO_RCVBUF
,
284 (char *)&window_size
, sizeof(window_size
));
286 if (listen(c
->fd
, 1) == -1) {
287 LOG_ERROR("couldn't listen on socket: %s", strerror(errno
));
293 struct sockaddr_in addr_in
;
294 addr_in
.sin_port
= 0;
295 socklen_t addr_in_size
= sizeof(addr_in
);
296 if (getsockname(c
->fd
, (struct sockaddr
*)&addr_in
, &addr_in_size
) == 0)
297 LOG_INFO("Listening on port %hu for %s connections",
298 ntohs(addr_in
.sin_port
), c
->name
);
299 } else if (c
->type
== CONNECTION_STDINOUT
) {
300 c
->fd
= fileno(stdin
);
303 /* for win32 set stdin/stdout to binary mode */
304 if (_setmode(_fileno(stdout
), _O_BINARY
) < 0)
305 LOG_WARNING("cannot change stdout mode to binary");
306 if (_setmode(_fileno(stdin
), _O_BINARY
) < 0)
307 LOG_WARNING("cannot change stdin mode to binary");
308 if (_setmode(_fileno(stderr
), _O_BINARY
) < 0)
309 LOG_WARNING("cannot change stderr mode to binary");
311 socket_nonblock(c
->fd
);
313 } else if (c
->type
== CONNECTION_PIPE
) {
315 /* we currently do not support named pipes under win32
316 * so exit openocd for now */
317 LOG_ERROR("Named pipes currently not supported under this os");
321 /* Pipe we're reading from */
322 c
->fd
= open(c
->port
, O_RDONLY
| O_NONBLOCK
);
324 LOG_ERROR("could not open %s", c
->port
);
331 /* add to the end of linked list */
332 for (p
= &services
; *p
; p
= &(*p
)->next
)
339 static void remove_connections(struct service
*service
)
341 struct connection
*connection
;
343 connection
= service
->connections
;
346 struct connection
*tmp
;
348 tmp
= connection
->next
;
349 remove_connection(service
, connection
);
354 int remove_service(const char *name
, const char *port
)
357 struct service
*prev
;
361 for (tmp
= services
; tmp
; prev
= tmp
, tmp
= tmp
->next
) {
362 if (!strcmp(tmp
->name
, name
) && !strcmp(tmp
->port
, port
)) {
363 remove_connections(tmp
);
366 services
= tmp
->next
;
368 prev
->next
= tmp
->next
;
370 if (tmp
->type
!= CONNECTION_STDINOUT
)
371 close_socket(tmp
->fd
);
383 static int remove_services(void)
385 struct service
*c
= services
;
389 struct service
*next
= c
->next
;
391 remove_connections(c
);
395 if (c
->type
== CONNECTION_PIPE
) {
404 /* remember the last service for unlinking */
413 void server_keep_clients_alive(void)
415 for (struct service
*s
= services
; s
; s
= s
->next
)
416 if (s
->keep_client_alive
)
417 for (struct connection
*c
= s
->connections
; c
; c
= c
->next
)
418 s
->keep_client_alive(c
);
421 int server_loop(struct command_context
*command_context
)
423 struct service
*service
;
427 /* used in select() */
431 /* used in accept() */
434 int64_t next_event
= timeval_ms() + polling_period
;
437 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
438 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
441 while (shutdown_openocd
== CONTINUE_MAIN_LOOP
) {
442 /* monitor sockets for activity */
446 /* add service and connection fds to read_fds */
447 for (service
= services
; service
; service
= service
->next
) {
448 if (service
->fd
!= -1) {
449 /* listen for new connections */
450 FD_SET(service
->fd
, &read_fds
);
452 if (service
->fd
> fd_max
)
453 fd_max
= service
->fd
;
456 if (service
->connections
) {
457 struct connection
*c
;
459 for (c
= service
->connections
; c
; c
= c
->next
) {
460 /* check for activity on the connection */
461 FD_SET(c
->fd
, &read_fds
);
471 /* we're just polling this iteration, this is faster on embedded
474 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
476 /* Every 100ms, can be changed with "poll_period" command */
477 int timeout_ms
= next_event
- timeval_ms();
480 else if (timeout_ms
> polling_period
)
481 timeout_ms
= polling_period
;
482 tv
.tv_usec
= timeout_ms
* 1000;
483 /* Only while we're sleeping we'll let others run */
484 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
490 errno
= WSAGetLastError();
492 if (errno
== WSAEINTR
)
495 LOG_ERROR("error during select: %s", strerror(errno
));
503 LOG_ERROR("error during select: %s", strerror(errno
));
510 /* We only execute these callbacks when there was nothing to do or we timed
512 target_call_timer_callbacks_now();
513 next_event
= target_timer_next_event();
514 process_jim_events(command_context
);
516 FD_ZERO(&read_fds
); /* eCos leaves read_fds unchanged in this case! */
518 /* We timed out/there was nothing to do, timeout rather than poll next time
522 /* There was something to do, next time we'll just poll */
526 /* This is a simple back-off algorithm where we immediately
527 * re-poll if we did something this time around.
529 * This greatly improves performance of DCC.
531 poll_ok
= poll_ok
|| target_got_message();
533 for (service
= services
; service
; service
= service
->next
) {
534 /* handle new connections on listeners */
535 if ((service
->fd
!= -1)
536 && (FD_ISSET(service
->fd
, &read_fds
))) {
537 if (service
->max_connections
!= 0)
538 add_connection(service
, command_context
);
540 if (service
->type
== CONNECTION_TCP
) {
541 struct sockaddr_in sin
;
542 socklen_t address_size
= sizeof(sin
);
544 tmp_fd
= accept(service
->fd
,
545 (struct sockaddr
*)&service
->sin
,
547 close_socket(tmp_fd
);
550 "rejected '%s' connection, no more connections allowed",
555 /* handle activity on connections */
556 if (service
->connections
) {
557 struct connection
*c
;
559 for (c
= service
->connections
; c
; ) {
560 if ((c
->fd
>= 0 && FD_ISSET(c
->fd
, &read_fds
)) || c
->input_pending
) {
561 retval
= service
->input(c
);
562 if (retval
!= ERROR_OK
) {
563 struct connection
*next
= c
->next
;
564 if (service
->type
== CONNECTION_PIPE
||
565 service
->type
== CONNECTION_STDINOUT
) {
566 /* if connection uses a pipe then
567 * shutdown openocd on error */
568 shutdown_openocd
= SHUTDOWN_REQUESTED
;
570 remove_connection(service
, c
);
571 LOG_INFO("dropped '%s' connection",
584 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
585 if (msg
.message
== WM_QUIT
)
586 shutdown_openocd
= SHUTDOWN_WITH_SIGNAL_CODE
;
591 /* when quit for signal or CTRL-C, run (eventually user implemented) "shutdown" */
592 if (shutdown_openocd
== SHUTDOWN_WITH_SIGNAL_CODE
)
593 command_run_line(command_context
, "shutdown");
595 return shutdown_openocd
== SHUTDOWN_WITH_ERROR_CODE
? ERROR_FAIL
: ERROR_OK
;
598 static void sig_handler(int sig
)
600 /* store only first signal that hits us */
601 if (shutdown_openocd
== CONTINUE_MAIN_LOOP
) {
602 shutdown_openocd
= SHUTDOWN_WITH_SIGNAL_CODE
;
604 LOG_DEBUG("Terminating on Signal %d", sig
);
606 LOG_DEBUG("Ignored extra Signal %d", sig
);
611 BOOL WINAPI
control_handler(DWORD ctrl_type
)
613 shutdown_openocd
= SHUTDOWN_WITH_SIGNAL_CODE
;
617 static void sigkey_handler(int sig
)
619 /* ignore keystroke generated signals if not in foreground process group */
621 if (tcgetpgrp(STDIN_FILENO
) > 0)
624 LOG_DEBUG("Ignored Signal %d", sig
);
629 int server_host_os_entry(void)
631 /* this currently only calls WSAStartup on native win32 systems
632 * before any socket operations are performed.
633 * This is an issue if you call init in your config script */
636 WORD version_requested
;
639 version_requested
= MAKEWORD(2, 2);
641 if (WSAStartup(version_requested
, &wsadata
) != 0) {
642 LOG_ERROR("Failed to Open Winsock");
649 int server_host_os_close(void)
657 int server_preinit(void)
660 /* register ctrl-c handler */
661 SetConsoleCtrlHandler(control_handler
, TRUE
);
663 signal(SIGBREAK
, sig_handler
);
664 signal(SIGINT
, sig_handler
);
666 signal(SIGHUP
, sig_handler
);
667 signal(SIGPIPE
, sig_handler
);
668 signal(SIGQUIT
, sigkey_handler
);
669 signal(SIGINT
, sigkey_handler
);
671 signal(SIGTERM
, sig_handler
);
672 signal(SIGABRT
, sig_handler
);
677 int server_init(struct command_context
*cmd_ctx
)
679 int ret
= tcl_init();
684 ret
= telnet_init("Open On-Chip Debugger");
686 if (ret
!= ERROR_OK
) {
694 int server_quit(void)
700 SetConsoleCtrlHandler(control_handler
, FALSE
);
705 /* return signal number so we can kill ourselves */
709 void server_free(void)
712 telnet_service_free();
718 void exit_on_signal(int sig
)
721 /* bring back default system handler and kill yourself */
722 signal(sig
, SIG_DFL
);
727 int connection_write(struct connection
*connection
, const void *data
, int len
)
730 /* successful no-op. Sockets and pipes behave differently here... */
733 if (connection
->service
->type
== CONNECTION_TCP
)
734 return write_socket(connection
->fd_out
, data
, len
);
736 return write(connection
->fd_out
, data
, len
);
739 int connection_read(struct connection
*connection
, void *data
, int len
)
741 if (connection
->service
->type
== CONNECTION_TCP
)
742 return read_socket(connection
->fd
, data
, len
);
744 return read(connection
->fd
, data
, len
);
747 /* tell the server we want to shut down */
748 COMMAND_HANDLER(handle_shutdown_command
)
750 LOG_USER("shutdown command invoked");
752 shutdown_openocd
= SHUTDOWN_REQUESTED
;
754 command_run_line(CMD_CTX
, "_run_pre_shutdown_commands");
757 if (!strcmp(CMD_ARGV
[0], "error")) {
758 shutdown_openocd
= SHUTDOWN_WITH_ERROR_CODE
;
763 return ERROR_COMMAND_CLOSE_CONNECTION
;
766 COMMAND_HANDLER(handle_poll_period_command
)
769 LOG_WARNING("You need to set a period value");
771 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], polling_period
);
773 LOG_INFO("set servers polling period to %ums", polling_period
);
778 COMMAND_HANDLER(handle_bindto_command
)
782 command_print(CMD
, "bindto name: %s", bindto_name
);
786 bindto_name
= strdup(CMD_ARGV
[0]);
789 return ERROR_COMMAND_SYNTAX_ERROR
;
794 static const struct command_registration server_command_handlers
[] = {
797 .handler
= &handle_shutdown_command
,
800 .help
= "shut the server down",
803 .name
= "poll_period",
804 .handler
= &handle_poll_period_command
,
807 .help
= "set the servers polling period",
811 .handler
= &handle_bindto_command
,
812 .mode
= COMMAND_CONFIG
,
814 .help
= "Specify address by name on which to listen for "
815 "incoming TCP/IP connections",
817 COMMAND_REGISTRATION_DONE
820 int server_register_commands(struct command_context
*cmd_ctx
)
822 int retval
= telnet_register_commands(cmd_ctx
);
823 if (retval
!= ERROR_OK
)
826 retval
= tcl_register_commands(cmd_ctx
);
827 if (retval
!= ERROR_OK
)
830 retval
= jsp_register_commands(cmd_ctx
);
831 if (retval
!= ERROR_OK
)
834 return register_commands(cmd_ctx
, NULL
, server_command_handlers
);
837 COMMAND_HELPER(server_port_command
, unsigned short *out
)
841 command_print(CMD
, "%d", *out
);
846 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], port
);
851 return ERROR_COMMAND_SYNTAX_ERROR
;
856 COMMAND_HELPER(server_pipe_command
, char **out
)
860 command_print(CMD
, "%s", *out
);
864 if (CMD_CTX
->mode
== COMMAND_EXEC
) {
865 LOG_WARNING("unable to change server port after init");
866 return ERROR_COMMAND_ARGUMENT_INVALID
;
869 *out
= strdup(CMD_ARGV
[0]);
873 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)