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
48 * debugger; 2: quit with non-zero return code */
49 static int shutdown_openocd
;
51 /* store received signal to exit application by killing ourselves */
52 static int last_signal
;
54 /* set the polling period to 100ms */
55 static int polling_period
= 100;
57 static int add_connection(struct service
*service
, struct command_context
*cmd_ctx
)
59 socklen_t address_size
;
60 struct connection
*c
, **p
;
64 c
= malloc(sizeof(struct connection
));
67 memset(&c
->sin
, 0, sizeof(c
->sin
));
68 c
->cmd_ctx
= copy_command_context(cmd_ctx
);
74 if (service
->type
== CONNECTION_TCP
) {
75 address_size
= sizeof(c
->sin
);
77 c
->fd
= accept(service
->fd
, (struct sockaddr
*)&service
->sin
, &address_size
);
80 /* This increases performance dramatically for e.g. GDB load which
81 * does not have a sliding window protocol.
83 * Ignore errors from this fn as it probably just means less performance
85 setsockopt(c
->fd
, /* socket affected */
86 IPPROTO_TCP
, /* set option at TCP level */
87 TCP_NODELAY
, /* name of option */
88 (char *)&flag
, /* the cast is historical cruft */
89 sizeof(int)); /* length of option value */
91 LOG_INFO("accepting '%s' connection on tcp/%s", service
->name
, service
->port
);
92 retval
= service
->new_connection(c
);
93 if (retval
!= ERROR_OK
) {
95 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
96 command_done(c
->cmd_ctx
);
100 } else if (service
->type
== CONNECTION_STDINOUT
) {
102 c
->fd_out
= fileno(stdout
);
105 /* we are using stdin/out so ignore ctrl-c under windoze */
106 SetConsoleCtrlHandler(NULL
, TRUE
);
109 /* do not check for new connections again on stdin */
112 LOG_INFO("accepting '%s' connection from pipe", service
->name
);
113 retval
= service
->new_connection(c
);
114 if (retval
!= ERROR_OK
) {
115 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
116 command_done(c
->cmd_ctx
);
120 } else if (service
->type
== CONNECTION_PIPE
) {
122 /* do not check for new connections again on stdin */
125 char *out_file
= alloc_printf("%so", service
->port
);
126 c
->fd_out
= open(out_file
, O_WRONLY
);
128 if (c
->fd_out
== -1) {
129 LOG_ERROR("could not open %s", service
->port
);
133 LOG_INFO("accepting '%s' connection from pipe %s", service
->name
, service
->port
);
134 retval
= service
->new_connection(c
);
135 if (retval
!= ERROR_OK
) {
136 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
137 command_done(c
->cmd_ctx
);
143 /* add to the end of linked list */
144 for (p
= &service
->connections
; *p
; p
= &(*p
)->next
)
148 if (service
->max_connections
!= CONNECTION_LIMIT_UNLIMITED
)
149 service
->max_connections
--;
154 static int remove_connection(struct service
*service
, struct connection
*connection
)
156 struct connection
**p
= &service
->connections
;
157 struct connection
*c
;
159 /* find connection */
161 if (c
->fd
== connection
->fd
) {
162 service
->connection_closed(c
);
163 if (service
->type
== CONNECTION_TCP
)
165 else if (service
->type
== CONNECTION_PIPE
) {
166 /* The service will listen to the pipe again */
167 c
->service
->fd
= c
->fd
;
170 command_done(c
->cmd_ctx
);
172 /* delete connection */
176 if (service
->max_connections
!= CONNECTION_LIMIT_UNLIMITED
)
177 service
->max_connections
++;
182 /* redirect p to next list pointer */
189 /* FIX! make service return error instead of invoking exit() */
190 int add_service(char *name
,
193 new_connection_handler_t new_connection_handler
,
194 input_handler_t input_handler
,
195 connection_closed_handler_t connection_closed_handler
,
198 struct service
*c
, **p
;
199 int so_reuseaddr_option
= 1;
201 c
= malloc(sizeof(struct service
));
203 c
->name
= strdup(name
);
204 c
->port
= strdup(port
);
205 c
->max_connections
= 1; /* Only TCP/IP ports can support more than one connection */
207 c
->connections
= NULL
;
208 c
->new_connection
= new_connection_handler
;
209 c
->input
= input_handler
;
210 c
->connection_closed
= connection_closed_handler
;
214 if (strcmp(c
->port
, "pipe") == 0)
215 c
->type
= CONNECTION_STDINOUT
;
218 portnumber
= strtol(c
->port
, &end
, 0);
219 if (!*end
&& (parse_long(c
->port
, &portnumber
) == ERROR_OK
)) {
220 c
->portnumber
= portnumber
;
221 c
->type
= CONNECTION_TCP
;
223 c
->type
= CONNECTION_PIPE
;
226 if (c
->type
== CONNECTION_TCP
) {
227 c
->max_connections
= max_connections
;
229 c
->fd
= socket(AF_INET
, SOCK_STREAM
, 0);
231 LOG_ERROR("error creating socket: %s", strerror(errno
));
238 (void *)&so_reuseaddr_option
,
241 socket_nonblock(c
->fd
);
243 memset(&c
->sin
, 0, sizeof(c
->sin
));
244 c
->sin
.sin_family
= AF_INET
;
245 c
->sin
.sin_addr
.s_addr
= INADDR_ANY
;
246 c
->sin
.sin_port
= htons(c
->portnumber
);
248 if (bind(c
->fd
, (struct sockaddr
*)&c
->sin
, sizeof(c
->sin
)) == -1) {
249 LOG_ERROR("couldn't bind to socket: %s", strerror(errno
));
255 setsockopt(c
->fd
, IPPROTO_TCP
, TCP_MAXSEG
, &segsize
, sizeof(int));
257 int window_size
= 128 * 1024;
259 /* These setsockopt()s must happen before the listen() */
261 setsockopt(c
->fd
, SOL_SOCKET
, SO_SNDBUF
,
262 (char *)&window_size
, sizeof(window_size
));
263 setsockopt(c
->fd
, SOL_SOCKET
, SO_RCVBUF
,
264 (char *)&window_size
, sizeof(window_size
));
266 if (listen(c
->fd
, 1) == -1) {
267 LOG_ERROR("couldn't listen on socket: %s", strerror(errno
));
270 } else if (c
->type
== CONNECTION_STDINOUT
) {
271 c
->fd
= fileno(stdin
);
274 /* for win32 set stdin/stdout to binary mode */
275 if (_setmode(_fileno(stdout
), _O_BINARY
) < 0)
276 LOG_WARNING("cannot change stdout mode to binary");
277 if (_setmode(_fileno(stdin
), _O_BINARY
) < 0)
278 LOG_WARNING("cannot change stdin mode to binary");
279 if (_setmode(_fileno(stderr
), _O_BINARY
) < 0)
280 LOG_WARNING("cannot change stderr mode to binary");
282 socket_nonblock(c
->fd
);
284 } else if (c
->type
== CONNECTION_PIPE
) {
286 /* we currenty do not support named pipes under win32
287 * so exit openocd for now */
288 LOG_ERROR("Named pipes currently not supported under this os");
291 /* Pipe we're reading from */
292 c
->fd
= open(c
->port
, O_RDONLY
| O_NONBLOCK
);
294 LOG_ERROR("could not open %s", c
->port
);
300 /* add to the end of linked list */
301 for (p
= &services
; *p
; p
= &(*p
)->next
)
308 static int remove_services(void)
310 struct service
*c
= services
;
314 struct service
*next
= c
->next
;
319 if (c
->type
== CONNECTION_PIPE
) {
332 /* remember the last service for unlinking */
341 int server_loop(struct command_context
*command_context
)
343 struct service
*service
;
347 /* used in select() */
351 /* used in accept() */
355 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
356 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
359 while (!shutdown_openocd
) {
360 /* monitor sockets for activity */
364 /* add service and connection fds to read_fds */
365 for (service
= services
; service
; service
= service
->next
) {
366 if (service
->fd
!= -1) {
367 /* listen for new connections */
368 FD_SET(service
->fd
, &read_fds
);
370 if (service
->fd
> fd_max
)
371 fd_max
= service
->fd
;
374 if (service
->connections
) {
375 struct connection
*c
;
377 for (c
= service
->connections
; c
; c
= c
->next
) {
378 /* check for activity on the connection */
379 FD_SET(c
->fd
, &read_fds
);
389 /* we're just polling this iteration, this is faster on embedded
392 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
394 /* Every 100ms, can be changed with "poll_period" command */
395 tv
.tv_usec
= polling_period
* 1000;
396 /* Only while we're sleeping we'll let others run */
397 openocd_sleep_prelude();
399 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
400 openocd_sleep_postlude();
406 errno
= WSAGetLastError();
408 if (errno
== WSAEINTR
)
411 LOG_ERROR("error during select: %s", strerror(errno
));
419 LOG_ERROR("error during select: %s", strerror(errno
));
426 /* We only execute these callbacks when there was nothing to do or we timed
428 target_call_timer_callbacks();
429 process_jim_events(command_context
);
431 FD_ZERO(&read_fds
); /* eCos leaves read_fds unchanged in this case! */
433 /* We timed out/there was nothing to do, timeout rather than poll next time
437 /* There was something to do, next time we'll just poll */
441 /* This is a simple back-off algorithm where we immediately
442 * re-poll if we did something this time around.
444 * This greatly improves performance of DCC.
446 poll_ok
= poll_ok
|| target_got_message();
448 for (service
= services
; service
; service
= service
->next
) {
449 /* handle new connections on listeners */
450 if ((service
->fd
!= -1)
451 && (FD_ISSET(service
->fd
, &read_fds
))) {
452 if (service
->max_connections
!= 0)
453 add_connection(service
, command_context
);
455 if (service
->type
== CONNECTION_TCP
) {
456 struct sockaddr_in sin
;
457 socklen_t address_size
= sizeof(sin
);
459 tmp_fd
= accept(service
->fd
,
460 (struct sockaddr
*)&service
->sin
,
462 close_socket(tmp_fd
);
465 "rejected '%s' connection, no more connections allowed",
470 /* handle activity on connections */
471 if (service
->connections
) {
472 struct connection
*c
;
474 for (c
= service
->connections
; c
; ) {
475 if ((FD_ISSET(c
->fd
, &read_fds
)) || c
->input_pending
) {
476 retval
= service
->input(c
);
477 if (retval
!= ERROR_OK
) {
478 struct connection
*next
= c
->next
;
479 if (service
->type
== CONNECTION_PIPE
||
480 service
->type
== CONNECTION_STDINOUT
) {
481 /* if connection uses a pipe then
482 * shutdown openocd on error */
483 shutdown_openocd
= 1;
485 remove_connection(service
, c
);
486 LOG_INFO("dropped '%s' connection",
499 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
500 if (msg
.message
== WM_QUIT
)
501 shutdown_openocd
= 1;
506 return shutdown_openocd
!= 2 ? ERROR_OK
: ERROR_FAIL
;
510 BOOL WINAPI
ControlHandler(DWORD dwCtrlType
)
512 shutdown_openocd
= 1;
517 void sig_handler(int sig
)
519 /* store only first signal that hits us */
522 shutdown_openocd
= 1;
525 int server_preinit(void)
527 /* this currently only calls WSAStartup on native win32 systems
528 * before any socket operations are performed.
529 * This is an issue if you call init in your config script */
532 WORD wVersionRequested
;
535 wVersionRequested
= MAKEWORD(2, 2);
537 if (WSAStartup(wVersionRequested
, &wsaData
) != 0) {
538 LOG_ERROR("Failed to Open Winsock");
542 /* register ctrl-c handler */
543 SetConsoleCtrlHandler(ControlHandler
, TRUE
);
545 signal(SIGBREAK
, sig_handler
);
547 signal(SIGINT
, sig_handler
);
548 signal(SIGTERM
, sig_handler
);
549 signal(SIGABRT
, sig_handler
);
554 int server_init(struct command_context
*cmd_ctx
)
556 int ret
= tcl_init();
560 return telnet_init("Open On-Chip Debugger");
563 int server_quit(void)
570 SetConsoleCtrlHandler(ControlHandler
, FALSE
);
575 /* return signal number so we can kill ourselves */
579 void exit_on_signal(int sig
)
582 /* bring back default system handler and kill yourself */
583 signal(sig
, SIG_DFL
);
588 int connection_write(struct connection
*connection
, const void *data
, int len
)
591 /* successful no-op. Sockets and pipes behave differently here... */
594 if (connection
->service
->type
== CONNECTION_TCP
)
595 return write_socket(connection
->fd_out
, data
, len
);
597 return write(connection
->fd_out
, data
, len
);
600 int connection_read(struct connection
*connection
, void *data
, int len
)
602 if (connection
->service
->type
== CONNECTION_TCP
)
603 return read_socket(connection
->fd
, data
, len
);
605 return read(connection
->fd
, data
, len
);
608 /* tell the server we want to shut down */
609 COMMAND_HANDLER(handle_shutdown_command
)
611 LOG_USER("shutdown command invoked");
613 shutdown_openocd
= 1;
616 if (!strcmp(CMD_ARGV
[0], "error")) {
617 shutdown_openocd
= 2;
622 return ERROR_COMMAND_CLOSE_CONNECTION
;
625 COMMAND_HANDLER(handle_poll_period_command
)
628 LOG_WARNING("You need to set a period value");
630 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], polling_period
);
632 LOG_INFO("set servers polling period to %ums", polling_period
);
637 static const struct command_registration server_command_handlers
[] = {
640 .handler
= &handle_shutdown_command
,
643 .help
= "shut the server down",
646 .name
= "poll_period",
647 .handler
= &handle_poll_period_command
,
650 .help
= "set the servers polling period",
652 COMMAND_REGISTRATION_DONE
655 int server_register_commands(struct command_context
*cmd_ctx
)
657 int retval
= telnet_register_commands(cmd_ctx
);
658 if (ERROR_OK
!= retval
)
661 retval
= tcl_register_commands(cmd_ctx
);
662 if (ERROR_OK
!= retval
)
665 retval
= jsp_register_commands(cmd_ctx
);
666 if (ERROR_OK
!= retval
)
669 return register_commands(cmd_ctx
, NULL
, server_command_handlers
);
672 COMMAND_HELPER(server_port_command
, unsigned short *out
)
676 command_print(CMD_CTX
, "%d", *out
);
681 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], port
);
686 return ERROR_COMMAND_SYNTAX_ERROR
;
691 COMMAND_HELPER(server_pipe_command
, char **out
)
695 command_print(CMD_CTX
, "%s", *out
);
699 if (CMD_CTX
->mode
== COMMAND_EXEC
) {
700 LOG_WARNING("unable to change server port after init");
701 return ERROR_COMMAND_ARGUMENT_INVALID
;
704 *out
= strdup(CMD_ARGV
[0]);
708 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)