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 int server_loop(struct command_context
*command_context
)
426 struct service
*service
;
430 /* used in select() */
434 /* used in accept() */
437 int64_t next_event
= timeval_ms() + polling_period
;
440 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
441 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
444 while (shutdown_openocd
== CONTINUE_MAIN_LOOP
) {
445 /* monitor sockets for activity */
449 /* add service and connection fds to read_fds */
450 for (service
= services
; service
; service
= service
->next
) {
451 if (service
->fd
!= -1) {
452 /* listen for new connections */
453 FD_SET(service
->fd
, &read_fds
);
455 if (service
->fd
> fd_max
)
456 fd_max
= service
->fd
;
459 if (service
->connections
) {
460 struct connection
*c
;
462 for (c
= service
->connections
; c
; c
= c
->next
) {
463 /* check for activity on the connection */
464 FD_SET(c
->fd
, &read_fds
);
474 /* we're just polling this iteration, this is faster on embedded
477 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
479 /* Every 100ms, can be changed with "poll_period" command */
480 int timeout_ms
= next_event
- timeval_ms();
483 else if (timeout_ms
> polling_period
)
484 timeout_ms
= polling_period
;
485 tv
.tv_usec
= timeout_ms
* 1000;
486 /* Only while we're sleeping we'll let others run */
487 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
493 errno
= WSAGetLastError();
495 if (errno
== WSAEINTR
)
498 LOG_ERROR("error during select: %s", strerror(errno
));
506 LOG_ERROR("error during select: %s", strerror(errno
));
513 /* We only execute these callbacks when there was nothing to do or we timed
515 target_call_timer_callbacks_now();
516 next_event
= target_timer_next_event();
517 process_jim_events(command_context
);
519 FD_ZERO(&read_fds
); /* eCos leaves read_fds unchanged in this case! */
521 /* We timed out/there was nothing to do, timeout rather than poll next time
525 /* There was something to do, next time we'll just poll */
529 /* This is a simple back-off algorithm where we immediately
530 * re-poll if we did something this time around.
532 * This greatly improves performance of DCC.
534 poll_ok
= poll_ok
|| target_got_message();
536 for (service
= services
; service
; service
= service
->next
) {
537 /* handle new connections on listeners */
538 if ((service
->fd
!= -1)
539 && (FD_ISSET(service
->fd
, &read_fds
))) {
540 if (service
->max_connections
!= 0)
541 add_connection(service
, command_context
);
543 if (service
->type
== CONNECTION_TCP
) {
544 struct sockaddr_in sin
;
545 socklen_t address_size
= sizeof(sin
);
547 tmp_fd
= accept(service
->fd
,
548 (struct sockaddr
*)&service
->sin
,
550 close_socket(tmp_fd
);
553 "rejected '%s' connection, no more connections allowed",
558 /* handle activity on connections */
559 if (service
->connections
) {
560 struct connection
*c
;
562 for (c
= service
->connections
; c
; ) {
563 if ((c
->fd
>= 0 && FD_ISSET(c
->fd
, &read_fds
)) || c
->input_pending
) {
564 retval
= service
->input(c
);
565 if (retval
!= ERROR_OK
) {
566 struct connection
*next
= c
->next
;
567 if (service
->type
== CONNECTION_PIPE
||
568 service
->type
== CONNECTION_STDINOUT
) {
569 /* if connection uses a pipe then
570 * shutdown openocd on error */
571 shutdown_openocd
= SHUTDOWN_REQUESTED
;
573 remove_connection(service
, c
);
574 LOG_INFO("dropped '%s' connection",
587 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
588 if (msg
.message
== WM_QUIT
)
589 shutdown_openocd
= SHUTDOWN_WITH_SIGNAL_CODE
;
594 /* when quit for signal or CTRL-C, run (eventually user implemented) "shutdown" */
595 if (shutdown_openocd
== SHUTDOWN_WITH_SIGNAL_CODE
)
596 command_run_line(command_context
, "shutdown");
598 return shutdown_openocd
== SHUTDOWN_WITH_ERROR_CODE
? ERROR_FAIL
: ERROR_OK
;
601 static void sig_handler(int sig
)
603 /* store only first signal that hits us */
604 if (shutdown_openocd
== CONTINUE_MAIN_LOOP
) {
605 shutdown_openocd
= SHUTDOWN_WITH_SIGNAL_CODE
;
607 LOG_DEBUG("Terminating on Signal %d", sig
);
609 LOG_DEBUG("Ignored extra Signal %d", sig
);
614 BOOL WINAPI
control_handler(DWORD ctrl_type
)
616 shutdown_openocd
= SHUTDOWN_WITH_SIGNAL_CODE
;
620 static void sigkey_handler(int sig
)
622 /* ignore keystroke generated signals if not in foreground process group */
624 if (tcgetpgrp(STDIN_FILENO
) > 0)
627 LOG_DEBUG("Ignored Signal %d", sig
);
632 int server_host_os_entry(void)
634 /* this currently only calls WSAStartup on native win32 systems
635 * before any socket operations are performed.
636 * This is an issue if you call init in your config script */
639 WORD version_requested
;
642 version_requested
= MAKEWORD(2, 2);
644 if (WSAStartup(version_requested
, &wsadata
) != 0) {
645 LOG_ERROR("Failed to Open Winsock");
652 int server_host_os_close(void)
660 int server_preinit(void)
663 /* register ctrl-c handler */
664 SetConsoleCtrlHandler(control_handler
, TRUE
);
666 signal(SIGBREAK
, sig_handler
);
667 signal(SIGINT
, sig_handler
);
669 signal(SIGHUP
, sig_handler
);
670 signal(SIGPIPE
, sig_handler
);
671 signal(SIGQUIT
, sigkey_handler
);
672 signal(SIGINT
, sigkey_handler
);
674 signal(SIGTERM
, sig_handler
);
675 signal(SIGABRT
, sig_handler
);
680 int server_init(struct command_context
*cmd_ctx
)
682 int ret
= tcl_init();
687 ret
= telnet_init("Open On-Chip Debugger");
689 if (ret
!= ERROR_OK
) {
697 int server_quit(void)
703 SetConsoleCtrlHandler(control_handler
, FALSE
);
708 /* return signal number so we can kill ourselves */
712 void server_free(void)
715 telnet_service_free();
721 void exit_on_signal(int sig
)
724 /* bring back default system handler and kill yourself */
725 signal(sig
, SIG_DFL
);
730 int connection_write(struct connection
*connection
, const void *data
, int len
)
733 /* successful no-op. Sockets and pipes behave differently here... */
736 if (connection
->service
->type
== CONNECTION_TCP
)
737 return write_socket(connection
->fd_out
, data
, len
);
739 return write(connection
->fd_out
, data
, len
);
742 int connection_read(struct connection
*connection
, void *data
, int len
)
744 if (connection
->service
->type
== CONNECTION_TCP
)
745 return read_socket(connection
->fd
, data
, len
);
747 return read(connection
->fd
, data
, len
);
750 /* tell the server we want to shut down */
751 COMMAND_HANDLER(handle_shutdown_command
)
753 LOG_USER("shutdown command invoked");
755 shutdown_openocd
= SHUTDOWN_REQUESTED
;
758 if (!strcmp(CMD_ARGV
[0], "error")) {
759 shutdown_openocd
= SHUTDOWN_WITH_ERROR_CODE
;
764 return ERROR_COMMAND_CLOSE_CONNECTION
;
767 COMMAND_HANDLER(handle_poll_period_command
)
770 LOG_WARNING("You need to set a period value");
772 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], polling_period
);
774 LOG_INFO("set servers polling period to %ums", polling_period
);
779 COMMAND_HANDLER(handle_bindto_command
)
783 command_print(CMD
, "bindto name: %s", bindto_name
);
787 bindto_name
= strdup(CMD_ARGV
[0]);
790 return ERROR_COMMAND_SYNTAX_ERROR
;
795 static const struct command_registration server_command_handlers
[] = {
798 .handler
= &handle_shutdown_command
,
801 .help
= "shut the server down",
804 .name
= "poll_period",
805 .handler
= &handle_poll_period_command
,
808 .help
= "set the servers polling period",
812 .handler
= &handle_bindto_command
,
813 .mode
= COMMAND_CONFIG
,
815 .help
= "Specify address by name on which to listen for "
816 "incoming TCP/IP connections",
818 COMMAND_REGISTRATION_DONE
821 int server_register_commands(struct command_context
*cmd_ctx
)
823 int retval
= telnet_register_commands(cmd_ctx
);
824 if (retval
!= ERROR_OK
)
827 retval
= tcl_register_commands(cmd_ctx
);
828 if (retval
!= ERROR_OK
)
831 retval
= jsp_register_commands(cmd_ctx
);
832 if (retval
!= ERROR_OK
)
835 return register_commands(cmd_ctx
, NULL
, server_command_handlers
);
838 COMMAND_HELPER(server_port_command
, unsigned short *out
)
842 command_print(CMD
, "%d", *out
);
847 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], port
);
852 return ERROR_COMMAND_SYNTAX_ERROR
;
857 COMMAND_HELPER(server_pipe_command
, char **out
)
861 command_print(CMD
, "%s", *out
);
865 if (CMD_CTX
->mode
== COMMAND_EXEC
) {
866 LOG_WARNING("unable to change server port after init");
867 return ERROR_COMMAND_ARGUMENT_INVALID
;
870 *out
= strdup(CMD_ARGV
[0]);
874 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)