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"
40 #include <netinet/tcp.h>
43 static struct service
*services
;
45 /* shutdown_openocd == 1: exit the main event loop, and quit the
46 * debugger; 2: quit with non-zero return code */
47 static int shutdown_openocd
;
49 /* store received signal to exit application by killing ourselves */
50 static int last_signal
;
52 /* set the polling period to 100ms */
53 static int polling_period
= 100;
55 static int add_connection(struct service
*service
, struct command_context
*cmd_ctx
)
57 socklen_t address_size
;
58 struct connection
*c
, **p
;
62 c
= malloc(sizeof(struct connection
));
65 memset(&c
->sin
, 0, sizeof(c
->sin
));
66 c
->cmd_ctx
= copy_command_context(cmd_ctx
);
72 if (service
->type
== CONNECTION_TCP
) {
73 address_size
= sizeof(c
->sin
);
75 c
->fd
= accept(service
->fd
, (struct sockaddr
*)&service
->sin
, &address_size
);
78 /* This increases performance dramatically for e.g. GDB load which
79 * does not have a sliding window protocol.
81 * Ignore errors from this fn as it probably just means less performance
83 setsockopt(c
->fd
, /* socket affected */
84 IPPROTO_TCP
, /* set option at TCP level */
85 TCP_NODELAY
, /* name of option */
86 (char *)&flag
, /* the cast is historical cruft */
87 sizeof(int)); /* length of option value */
89 LOG_INFO("accepting '%s' connection on tcp/%s", service
->name
, service
->port
);
90 retval
= service
->new_connection(c
);
91 if (retval
!= ERROR_OK
) {
93 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
94 command_done(c
->cmd_ctx
);
98 } else if (service
->type
== CONNECTION_STDINOUT
) {
100 c
->fd_out
= fileno(stdout
);
103 /* we are using stdin/out so ignore ctrl-c under windoze */
104 SetConsoleCtrlHandler(NULL
, TRUE
);
107 /* do not check for new connections again on stdin */
110 LOG_INFO("accepting '%s' connection from pipe", service
->name
);
111 retval
= service
->new_connection(c
);
112 if (retval
!= ERROR_OK
) {
113 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
114 command_done(c
->cmd_ctx
);
118 } else if (service
->type
== CONNECTION_PIPE
) {
120 /* do not check for new connections again on stdin */
123 char *out_file
= alloc_printf("%so", service
->port
);
124 c
->fd_out
= open(out_file
, O_WRONLY
);
126 if (c
->fd_out
== -1) {
127 LOG_ERROR("could not open %s", service
->port
);
131 LOG_INFO("accepting '%s' connection from pipe %s", service
->name
, service
->port
);
132 retval
= service
->new_connection(c
);
133 if (retval
!= ERROR_OK
) {
134 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
135 command_done(c
->cmd_ctx
);
141 /* add to the end of linked list */
142 for (p
= &service
->connections
; *p
; p
= &(*p
)->next
)
146 if (service
->max_connections
!= CONNECTION_LIMIT_UNLIMITED
)
147 service
->max_connections
--;
152 static int remove_connection(struct service
*service
, struct connection
*connection
)
154 struct connection
**p
= &service
->connections
;
155 struct connection
*c
;
157 /* find connection */
159 if (c
->fd
== connection
->fd
) {
160 service
->connection_closed(c
);
161 if (service
->type
== CONNECTION_TCP
)
163 else if (service
->type
== CONNECTION_PIPE
) {
164 /* The service will listen to the pipe again */
165 c
->service
->fd
= c
->fd
;
168 command_done(c
->cmd_ctx
);
170 /* delete connection */
174 if (service
->max_connections
!= CONNECTION_LIMIT_UNLIMITED
)
175 service
->max_connections
++;
180 /* redirect p to next list pointer */
187 /* FIX! make service return error instead of invoking exit() */
188 int add_service(char *name
,
191 new_connection_handler_t new_connection_handler
,
192 input_handler_t input_handler
,
193 connection_closed_handler_t connection_closed_handler
,
196 struct service
*c
, **p
;
197 int so_reuseaddr_option
= 1;
199 c
= malloc(sizeof(struct service
));
201 c
->name
= strdup(name
);
202 c
->port
= strdup(port
);
203 c
->max_connections
= 1; /* Only TCP/IP ports can support more than one connection */
205 c
->connections
= NULL
;
206 c
->new_connection
= new_connection_handler
;
207 c
->input
= input_handler
;
208 c
->connection_closed
= connection_closed_handler
;
212 if (strcmp(c
->port
, "pipe") == 0)
213 c
->type
= CONNECTION_STDINOUT
;
216 portnumber
= strtol(c
->port
, &end
, 0);
217 if (!*end
&& (parse_long(c
->port
, &portnumber
) == ERROR_OK
)) {
218 c
->portnumber
= portnumber
;
219 c
->type
= CONNECTION_TCP
;
221 c
->type
= CONNECTION_PIPE
;
224 if (c
->type
== CONNECTION_TCP
) {
225 c
->max_connections
= max_connections
;
227 c
->fd
= socket(AF_INET
, SOCK_STREAM
, 0);
229 LOG_ERROR("error creating socket: %s", strerror(errno
));
236 (void *)&so_reuseaddr_option
,
239 socket_nonblock(c
->fd
);
241 memset(&c
->sin
, 0, sizeof(c
->sin
));
242 c
->sin
.sin_family
= AF_INET
;
243 c
->sin
.sin_addr
.s_addr
= INADDR_ANY
;
244 c
->sin
.sin_port
= htons(c
->portnumber
);
246 if (bind(c
->fd
, (struct sockaddr
*)&c
->sin
, sizeof(c
->sin
)) == -1) {
247 LOG_ERROR("couldn't bind to socket: %s", strerror(errno
));
253 setsockopt(c
->fd
, IPPROTO_TCP
, TCP_MAXSEG
, &segsize
, sizeof(int));
255 int window_size
= 128 * 1024;
257 /* These setsockopt()s must happen before the listen() */
259 setsockopt(c
->fd
, SOL_SOCKET
, SO_SNDBUF
,
260 (char *)&window_size
, sizeof(window_size
));
261 setsockopt(c
->fd
, SOL_SOCKET
, SO_RCVBUF
,
262 (char *)&window_size
, sizeof(window_size
));
264 if (listen(c
->fd
, 1) == -1) {
265 LOG_ERROR("couldn't listen on socket: %s", strerror(errno
));
268 } else if (c
->type
== CONNECTION_STDINOUT
) {
269 c
->fd
= fileno(stdin
);
272 /* for win32 set stdin/stdout to binary mode */
273 if (_setmode(_fileno(stdout
), _O_BINARY
) < 0)
274 LOG_WARNING("cannot change stdout mode to binary");
275 if (_setmode(_fileno(stdin
), _O_BINARY
) < 0)
276 LOG_WARNING("cannot change stdin mode to binary");
277 if (_setmode(_fileno(stderr
), _O_BINARY
) < 0)
278 LOG_WARNING("cannot change stderr mode to binary");
280 socket_nonblock(c
->fd
);
282 } else if (c
->type
== CONNECTION_PIPE
) {
284 /* we currenty do not support named pipes under win32
285 * so exit openocd for now */
286 LOG_ERROR("Named pipes currently not supported under this os");
289 /* Pipe we're reading from */
290 c
->fd
= open(c
->port
, O_RDONLY
| O_NONBLOCK
);
292 LOG_ERROR("could not open %s", c
->port
);
298 /* add to the end of linked list */
299 for (p
= &services
; *p
; p
= &(*p
)->next
)
306 static int remove_services(void)
308 struct service
*c
= services
;
312 struct service
*next
= c
->next
;
317 if (c
->type
== CONNECTION_PIPE
) {
330 /* remember the last service for unlinking */
339 int server_loop(struct command_context
*command_context
)
341 struct service
*service
;
345 /* used in select() */
349 /* used in accept() */
353 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
354 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
357 while (!shutdown_openocd
) {
358 /* monitor sockets for activity */
362 /* add service and connection fds to read_fds */
363 for (service
= services
; service
; service
= service
->next
) {
364 if (service
->fd
!= -1) {
365 /* listen for new connections */
366 FD_SET(service
->fd
, &read_fds
);
368 if (service
->fd
> fd_max
)
369 fd_max
= service
->fd
;
372 if (service
->connections
) {
373 struct connection
*c
;
375 for (c
= service
->connections
; c
; c
= c
->next
) {
376 /* check for activity on the connection */
377 FD_SET(c
->fd
, &read_fds
);
387 /* we're just polling this iteration, this is faster on embedded
390 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
392 /* Every 100ms, can be changed with "poll_period" command */
393 tv
.tv_usec
= polling_period
* 1000;
394 /* Only while we're sleeping we'll let others run */
395 openocd_sleep_prelude();
397 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
398 openocd_sleep_postlude();
404 errno
= WSAGetLastError();
406 if (errno
== WSAEINTR
)
409 LOG_ERROR("error during select: %s", strerror(errno
));
417 LOG_ERROR("error during select: %s", strerror(errno
));
424 /* We only execute these callbacks when there was nothing to do or we timed
426 target_call_timer_callbacks();
427 process_jim_events(command_context
);
429 FD_ZERO(&read_fds
); /* eCos leaves read_fds unchanged in this case! */
431 /* We timed out/there was nothing to do, timeout rather than poll next time
435 /* There was something to do, next time we'll just poll */
439 /* This is a simple back-off algorithm where we immediately
440 * re-poll if we did something this time around.
442 * This greatly improves performance of DCC.
444 poll_ok
= poll_ok
|| target_got_message();
446 for (service
= services
; service
; service
= service
->next
) {
447 /* handle new connections on listeners */
448 if ((service
->fd
!= -1)
449 && (FD_ISSET(service
->fd
, &read_fds
))) {
450 if (service
->max_connections
!= 0)
451 add_connection(service
, command_context
);
453 if (service
->type
== CONNECTION_TCP
) {
454 struct sockaddr_in sin
;
455 socklen_t address_size
= sizeof(sin
);
457 tmp_fd
= accept(service
->fd
,
458 (struct sockaddr
*)&service
->sin
,
460 close_socket(tmp_fd
);
463 "rejected '%s' connection, no more connections allowed",
468 /* handle activity on connections */
469 if (service
->connections
) {
470 struct connection
*c
;
472 for (c
= service
->connections
; c
; ) {
473 if ((FD_ISSET(c
->fd
, &read_fds
)) || c
->input_pending
) {
474 retval
= service
->input(c
);
475 if (retval
!= ERROR_OK
) {
476 struct connection
*next
= c
->next
;
477 if (service
->type
== CONNECTION_PIPE
||
478 service
->type
== CONNECTION_STDINOUT
) {
479 /* if connection uses a pipe then
480 * shutdown openocd on error */
481 shutdown_openocd
= 1;
483 remove_connection(service
, c
);
484 LOG_INFO("dropped '%s' connection",
497 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
498 if (msg
.message
== WM_QUIT
)
499 shutdown_openocd
= 1;
504 return shutdown_openocd
!= 2 ? ERROR_OK
: ERROR_FAIL
;
508 BOOL WINAPI
ControlHandler(DWORD dwCtrlType
)
510 shutdown_openocd
= 1;
515 void sig_handler(int sig
)
517 /* store only first signal that hits us */
520 shutdown_openocd
= 1;
523 int server_preinit(void)
525 /* this currently only calls WSAStartup on native win32 systems
526 * before any socket operations are performed.
527 * This is an issue if you call init in your config script */
530 WORD wVersionRequested
;
533 wVersionRequested
= MAKEWORD(2, 2);
535 if (WSAStartup(wVersionRequested
, &wsaData
) != 0) {
536 LOG_ERROR("Failed to Open Winsock");
540 /* register ctrl-c handler */
541 SetConsoleCtrlHandler(ControlHandler
, TRUE
);
543 signal(SIGBREAK
, sig_handler
);
545 signal(SIGINT
, sig_handler
);
546 signal(SIGTERM
, sig_handler
);
547 signal(SIGABRT
, sig_handler
);
552 int server_init(struct command_context
*cmd_ctx
)
554 int ret
= tcl_init();
558 return telnet_init("Open On-Chip Debugger");
561 int server_quit(void)
568 SetConsoleCtrlHandler(ControlHandler
, FALSE
);
573 /* return signal number so we can kill ourselves */
577 void exit_on_signal(int sig
)
580 /* bring back default system handler and kill yourself */
581 signal(sig
, SIG_DFL
);
586 int connection_write(struct connection
*connection
, const void *data
, int len
)
589 /* successful no-op. Sockets and pipes behave differently here... */
592 if (connection
->service
->type
== CONNECTION_TCP
)
593 return write_socket(connection
->fd_out
, data
, len
);
595 return write(connection
->fd_out
, data
, len
);
598 int connection_read(struct connection
*connection
, void *data
, int len
)
600 if (connection
->service
->type
== CONNECTION_TCP
)
601 return read_socket(connection
->fd
, data
, len
);
603 return read(connection
->fd
, data
, len
);
606 /* tell the server we want to shut down */
607 COMMAND_HANDLER(handle_shutdown_command
)
609 LOG_USER("shutdown command invoked");
611 shutdown_openocd
= 1;
614 if (!strcmp(CMD_ARGV
[0], "error")) {
615 shutdown_openocd
= 2;
620 return ERROR_COMMAND_CLOSE_CONNECTION
;
623 COMMAND_HANDLER(handle_poll_period_command
)
626 LOG_WARNING("You need to set a period value");
628 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], polling_period
);
630 LOG_INFO("set servers polling period to %ums", polling_period
);
635 static const struct command_registration server_command_handlers
[] = {
638 .handler
= &handle_shutdown_command
,
641 .help
= "shut the server down",
644 .name
= "poll_period",
645 .handler
= &handle_poll_period_command
,
648 .help
= "set the servers polling period",
650 COMMAND_REGISTRATION_DONE
653 int server_register_commands(struct command_context
*cmd_ctx
)
655 int retval
= telnet_register_commands(cmd_ctx
);
656 if (ERROR_OK
!= retval
)
659 retval
= tcl_register_commands(cmd_ctx
);
660 if (ERROR_OK
!= retval
)
663 retval
= jsp_register_commands(cmd_ctx
);
664 if (ERROR_OK
!= retval
)
667 return register_commands(cmd_ctx
, NULL
, server_command_handlers
);
670 COMMAND_HELPER(server_port_command
, unsigned short *out
)
674 command_print(CMD_CTX
, "%d", *out
);
679 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], port
);
684 return ERROR_COMMAND_SYNTAX_ERROR
;
689 COMMAND_HELPER(server_pipe_command
, char **out
)
693 command_print(CMD_CTX
, "%s", *out
);
697 if (CMD_CTX
->mode
== COMMAND_EXEC
) {
698 LOG_WARNING("unable to change server port after init");
699 return ERROR_COMMAND_ARGUMENT_INVALID
;
702 *out
= strdup(CMD_ARGV
[0]);
706 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)