warnings: use more 'const' for char *
[openocd.git] / src / server / server.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
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. *
15 * *
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. *
20 * *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "server.h"
31 #include <target/target.h>
32 #include "openocd.h"
33 #include "tcl_server.h"
34 #include "telnet_server.h"
35
36 #include <signal.h>
37
38 #ifndef _WIN32
39 #include <netinet/tcp.h>
40 #endif
41
42
43 static struct service *services = NULL;
44
45 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
46 static int shutdown_openocd = 0;
47
48 static int add_connection(struct service *service, struct command_context *cmd_ctx)
49 {
50 socklen_t address_size;
51 struct connection *c, **p;
52 int retval;
53 int flag = 1;
54
55 c = malloc(sizeof(struct connection));
56 c->fd = -1;
57 c->fd_out = -1;
58 memset(&c->sin, 0, sizeof(c->sin));
59 c->cmd_ctx = copy_command_context(cmd_ctx);
60 c->service = service;
61 c->input_pending = 0;
62 c->priv = NULL;
63 c->next = NULL;
64
65 if (service->type == CONNECTION_TCP)
66 {
67 address_size = sizeof(c->sin);
68
69 c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
70 c->fd_out = c->fd;
71
72 /* This increases performance dramatically for e.g. GDB load which
73 * does not have a sliding window protocol. */
74 retval = setsockopt(c->fd, /* socket affected */
75 IPPROTO_TCP, /* set option at TCP level */
76 TCP_NODELAY, /* name of option */
77 (char *)&flag, /* the cast is historical cruft */
78 sizeof(int)); /* length of option value */
79
80 LOG_INFO("accepting '%s' connection from %s", service->name, service->port);
81 if ((retval = service->new_connection(c)) != ERROR_OK)
82 {
83 close_socket(c->fd);
84 LOG_ERROR("attempted '%s' connection rejected", service->name);
85 free(c);
86 return retval;
87 }
88 } else if (service->type == CONNECTION_STDINOUT)
89 {
90 c->fd = service->fd;
91 c->fd_out = fileno(stdout);
92
93 #ifdef _WIN32
94 /* we are using stdin/out so ignore ctrl-c under windoze */
95 SetConsoleCtrlHandler(NULL, TRUE);
96 #endif
97
98 /* do not check for new connections again on stdin */
99 service->fd = -1;
100
101 LOG_INFO("accepting '%s' connection from pipe", service->name);
102 if ((retval = service->new_connection(c)) != ERROR_OK)
103 {
104 LOG_ERROR("attempted '%s' connection rejected", service->name);
105 free(c);
106 return retval;
107 }
108 } else if (service->type == CONNECTION_PIPE)
109 {
110 c->fd = service->fd;
111 /* do not check for new connections again on stdin */
112 service->fd = -1;
113
114 char * out_file = alloc_printf("%so", service->port);
115 c->fd_out = open(out_file, O_WRONLY);
116 free(out_file);
117 if (c->fd_out == -1)
118 {
119 LOG_ERROR("could not open %s", service->port);
120 exit(1);
121 }
122
123 LOG_INFO("accepting '%s' connection from pipe %s", service->name, service->port);
124 if ((retval = service->new_connection(c)) != ERROR_OK)
125 {
126 LOG_ERROR("attempted '%s' connection rejected", service->name);
127 free(c);
128 return retval;
129 }
130 }
131
132 /* add to the end of linked list */
133 for (p = &service->connections; *p; p = &(*p)->next);
134 *p = c;
135
136 service->max_connections--;
137
138 return ERROR_OK;
139 }
140
141 static int remove_connection(struct service *service, struct connection *connection)
142 {
143 struct connection **p = &service->connections;
144 struct connection *c;
145
146 /* find connection */
147 while ((c = *p))
148 {
149 if (c->fd == connection->fd)
150 {
151 service->connection_closed(c);
152 if (service->type == CONNECTION_TCP)
153 {
154 close_socket(c->fd);
155 } else if (service->type == CONNECTION_PIPE)
156 {
157 /* The service will listen to the pipe again */
158 c->service->fd = c->fd;
159 }
160
161 command_done(c->cmd_ctx);
162
163 /* delete connection */
164 *p = c->next;
165 free(c);
166
167 service->max_connections++;
168 break;
169 }
170
171 /* redirect p to next list pointer */
172 p = &(*p)->next;
173 }
174
175 return ERROR_OK;
176 }
177
178 /* FIX! make service return error instead of invoking exit() */
179 int add_service(char *name, const char *port, int max_connections, new_connection_handler_t new_connection_handler, input_handler_t input_handler, connection_closed_handler_t connection_closed_handler, void *priv)
180 {
181 struct service *c, **p;
182 int so_reuseaddr_option = 1;
183
184 c = malloc(sizeof(struct service));
185
186 c->name = strdup(name);
187 c->port = strdup(port);
188 c->max_connections = 1; /* Only TCP/IP ports can support more than one connection */
189 c->fd = -1;
190 c->connections = NULL;
191 c->new_connection = new_connection_handler;
192 c->input = input_handler;
193 c->connection_closed = connection_closed_handler;
194 c->priv = priv;
195 c->next = NULL;
196 long portnumber;
197 if (strcmp(c->port, "pipe") == 0)
198 {
199 c->type = CONNECTION_STDINOUT;
200 } else
201 {
202 char *end;
203 portnumber = strtol(c->port, &end, 0);
204 if (!*end && (parse_long(c->port, &portnumber) == ERROR_OK))
205 {
206 c->portnumber = portnumber;
207 c->type = CONNECTION_TCP;
208 } else
209 {
210 c->type = CONNECTION_PIPE;
211 }
212 }
213
214 if (c->type == CONNECTION_TCP)
215 {
216 c->max_connections = max_connections;
217
218 if ((c->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
219 {
220 LOG_ERROR("error creating socket: %s", strerror(errno));
221 exit(-1);
222 }
223
224 setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
225
226 socket_nonblock(c->fd);
227
228 memset(&c->sin, 0, sizeof(c->sin));
229 c->sin.sin_family = AF_INET;
230 c->sin.sin_addr.s_addr = INADDR_ANY;
231 c->sin.sin_port = htons(c->portnumber);
232
233 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1)
234 {
235 LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
236 exit(-1);
237 }
238
239 #ifndef _WIN32
240 int segsize = 65536;
241 setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
242 #endif
243 int window_size = 128 * 1024;
244
245 /* These setsockopt()s must happen before the listen() */
246
247 setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
248 (char *)&window_size, sizeof(window_size));
249 setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
250 (char *)&window_size, sizeof(window_size));
251
252 if (listen(c->fd, 1) == -1)
253 {
254 LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
255 exit(-1);
256 }
257 }
258 else if (c->type == CONNECTION_STDINOUT)
259 {
260 c->fd = fileno(stdin);
261
262 #ifdef _WIN32
263 /* for win32 set stdin/stdout to binary mode */
264 if (_setmode(_fileno(stdout), _O_BINARY) < 0)
265 LOG_WARNING("cannot change stdout mode to binary");
266 if (_setmode(_fileno(stdin), _O_BINARY) < 0)
267 LOG_WARNING("cannot change stdin mode to binary");
268 if (_setmode(_fileno(stderr), _O_BINARY) < 0)
269 LOG_WARNING("cannot change stderr mode to binary");
270 #else
271 socket_nonblock(c->fd);
272 #endif
273 }
274 else if (c->type == CONNECTION_PIPE)
275 {
276 #ifdef _WIN32
277 /* we currenty do not support named pipes under win32
278 * so exit openocd for now */
279 LOG_ERROR("Named pipes currently not supported under this os");
280 exit(1);
281 #else
282 /* Pipe we're reading from */
283 c->fd = open(c->port, O_RDONLY | O_NONBLOCK);
284 if (c->fd == -1)
285 {
286 LOG_ERROR("could not open %s", c->port);
287 exit(1);
288 }
289 #endif
290 }
291
292 /* add to the end of linked list */
293 for (p = &services; *p; p = &(*p)->next);
294 *p = c;
295
296 return ERROR_OK;
297 }
298
299 static int remove_services(void)
300 {
301 struct service *c = services;
302
303 /* loop service */
304 while (c)
305 {
306 struct service *next = c->next;
307
308 if (c->name)
309 free((void *)c->name);
310
311 if (c->type == CONNECTION_PIPE)
312 {
313 if (c->fd != -1)
314 close(c->fd);
315 }
316 if (c->port)
317 free((void *)c->port);
318
319 if (c->priv)
320 free(c->priv);
321
322 /* delete service */
323 free(c);
324
325 /* remember the last service for unlinking */
326 c = next;
327 }
328
329 services = NULL;
330
331 return ERROR_OK;
332 }
333
334 int server_loop(struct command_context *command_context)
335 {
336 struct service *service;
337
338 bool poll_ok = true;
339
340 /* used in select() */
341 fd_set read_fds;
342 int fd_max;
343
344 /* used in accept() */
345 int retval;
346
347 #ifndef _WIN32
348 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
349 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
350 #endif
351
352 while (!shutdown_openocd)
353 {
354 /* monitor sockets for activity */
355 fd_max = 0;
356 FD_ZERO(&read_fds);
357
358 /* add service and connection fds to read_fds */
359 for (service = services; service; service = service->next)
360 {
361 if (service->fd != -1)
362 {
363 /* listen for new connections */
364 FD_SET(service->fd, &read_fds);
365
366 if (service->fd > fd_max)
367 fd_max = service->fd;
368 }
369
370 if (service->connections)
371 {
372 struct connection *c;
373
374 for (c = service->connections; c; c = c->next)
375 {
376 /* check for activity on the connection */
377 FD_SET(c->fd, &read_fds);
378 if (c->fd > fd_max)
379 fd_max = c->fd;
380 }
381 }
382 }
383
384 struct timeval tv;
385 tv.tv_sec = 0;
386 if (poll_ok)
387 {
388 /* we're just polling this iteration, this is faster on embedded
389 * hosts */
390 tv.tv_usec = 0;
391 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
392 } else
393 {
394 /* Every 100ms */
395 tv.tv_usec = 100000;
396 /* Only while we're sleeping we'll let others run */
397 openocd_sleep_prelude();
398 kept_alive();
399 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
400 openocd_sleep_postlude();
401 }
402
403 if (retval == -1)
404 {
405 #ifdef _WIN32
406
407 errno = WSAGetLastError();
408
409 if (errno == WSAEINTR)
410 FD_ZERO(&read_fds);
411 else
412 {
413 LOG_ERROR("error during select: %s", strerror(errno));
414 exit(-1);
415 }
416 #else
417
418 if (errno == EINTR)
419 {
420 FD_ZERO(&read_fds);
421 }
422 else
423 {
424 LOG_ERROR("error during select: %s", strerror(errno));
425 exit(-1);
426 }
427 #endif
428 }
429
430 if (retval == 0)
431 {
432 /* We only execute these callbacks when there was nothing to do or we timed out */
433 target_call_timer_callbacks();
434 process_jim_events(command_context);
435
436 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
437
438 /* We timed out/there was nothing to do, timeout rather than poll next time */
439 poll_ok = false;
440 } else
441 {
442 /* There was something to do, next time we'll just poll */
443 poll_ok = true;
444 }
445
446 for (service = services; service; service = service->next)
447 {
448 /* handle new connections on listeners */
449 if ((service->fd != -1)
450 && (FD_ISSET(service->fd, &read_fds)))
451 {
452 if (service->max_connections > 0)
453 {
454 add_connection(service, command_context);
455 }
456 else
457 {
458 if (service->type == CONNECTION_TCP)
459 {
460 struct sockaddr_in sin;
461 socklen_t address_size = sizeof(sin);
462 int tmp_fd;
463 tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
464 close_socket(tmp_fd);
465 }
466 LOG_INFO("rejected '%s' connection, no more connections allowed", service->name);
467 }
468 }
469
470 /* handle activity on connections */
471 if (service->connections)
472 {
473 struct connection *c;
474
475 for (c = service->connections; c;)
476 {
477 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending)
478 {
479 if ((retval = service->input(c)) != ERROR_OK)
480 {
481 struct connection *next = c->next;
482 if (service->type == CONNECTION_PIPE)
483 {
484 /* if connection uses a pipe then shutdown openocd on error */
485 shutdown_openocd = 1;
486 }
487 remove_connection(service, c);
488 LOG_INFO("dropped '%s' connection", service->name);
489 c = next;
490 continue;
491 }
492 }
493 c = c->next;
494 }
495 }
496 }
497
498 #ifdef _WIN32
499 MSG msg;
500 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
501 {
502 if (msg.message == WM_QUIT)
503 shutdown_openocd = 1;
504 }
505 #endif
506 }
507
508 return ERROR_OK;
509 }
510
511 #ifdef _WIN32
512 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
513 {
514 shutdown_openocd = 1;
515 return TRUE;
516 }
517
518 void sig_handler(int sig) {
519 shutdown_openocd = 1;
520 }
521 #endif
522
523 int server_preinit(void)
524 {
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 */
528
529 #ifdef _WIN32
530 WORD wVersionRequested;
531 WSADATA wsaData;
532
533 wVersionRequested = MAKEWORD(2, 2);
534
535 if (WSAStartup(wVersionRequested, &wsaData) != 0)
536 {
537 LOG_ERROR("Failed to Open Winsock");
538 exit(-1);
539 }
540
541 /* register ctrl-c handler */
542 SetConsoleCtrlHandler(ControlHandler, TRUE);
543
544 signal(SIGINT, sig_handler);
545 signal(SIGTERM, sig_handler);
546 signal(SIGBREAK, sig_handler);
547 signal(SIGABRT, sig_handler);
548 #endif
549
550 return ERROR_OK;
551 }
552
553 int server_init(struct command_context *cmd_ctx)
554 {
555 int ret = tcl_init();
556 if (ERROR_OK != ret)
557 return ret;
558
559 return telnet_init("Open On-Chip Debugger");
560 }
561
562 int server_quit(void)
563 {
564 remove_services();
565
566 #ifdef _WIN32
567 WSACleanup();
568 SetConsoleCtrlHandler(ControlHandler, FALSE);
569 #endif
570
571 return ERROR_OK;
572 }
573
574 int connection_write(struct connection *connection, const void *data, int len)
575 {
576 if (len == 0)
577 {
578 /* successful no-op. Sockets and pipes behave differently here... */
579 return 0;
580 }
581 if (connection->service->type == CONNECTION_TCP)
582 {
583 return write_socket(connection->fd_out, data, len);
584 } else
585 {
586 return write(connection->fd_out, data, len);
587 }
588 }
589
590 int connection_read(struct connection *connection, void *data, int len)
591 {
592 if (connection->service->type == CONNECTION_TCP)
593 {
594 return read_socket(connection->fd, data, len);
595 } else
596 {
597 return read(connection->fd, data, len);
598 }
599 }
600
601 /* tell the server we want to shut down */
602 COMMAND_HANDLER(handle_shutdown_command)
603 {
604 LOG_USER("shutdown command invoked");
605
606 shutdown_openocd = 1;
607
608 return ERROR_OK;
609 }
610
611 static const struct command_registration server_command_handlers[] = {
612 {
613 .name = "shutdown",
614 .handler = &handle_shutdown_command,
615 .mode = COMMAND_ANY,
616 .help = "shut the server down",
617 },
618 COMMAND_REGISTRATION_DONE
619 };
620
621 int server_register_commands(struct command_context *cmd_ctx)
622 {
623 int retval = telnet_register_commands(cmd_ctx);
624 if (ERROR_OK != retval)
625 return retval;
626
627 retval = tcl_register_commands(cmd_ctx);
628 if (ERROR_OK != retval)
629 return retval;
630
631 return register_commands(cmd_ctx, NULL, server_command_handlers);
632 }
633
634 SERVER_PORT_COMMAND()
635 {
636 switch (CMD_ARGC) {
637 case 0:
638 command_print(CMD_CTX, "%d", *out);
639 break;
640 case 1:
641 {
642 uint16_t port;
643 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
644 *out = port;
645 break;
646 }
647 default:
648 return ERROR_INVALID_ARGUMENTS;
649 }
650 return ERROR_OK;
651 }
652
653 SERVER_PIPE_COMMAND()
654 {
655 switch (CMD_ARGC) {
656 case 0:
657 command_print(CMD_CTX, "%s", *out);
658 break;
659 case 1:
660 {
661 const char * t = strdup(CMD_ARGV[0]);
662 free((void *)*out);
663 *out = t;
664 break;
665 }
666 default:
667 return ERROR_INVALID_ARGUMENTS;
668 }
669 return ERROR_OK;
670 }
671

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)