OpenOCD
server.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007-2010 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  * *
10  * Copyright (C) 2008 by Spencer Oliver *
11  * spen@spen-soft.co.uk *
12  ***************************************************************************/
13 
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 
18 #include "server.h"
19 #include <helper/time_support.h>
20 #include <target/target.h>
21 #include <target/target_request.h>
23 #include "openocd.h"
24 #include "tcl_server.h"
25 #include "telnet_server.h"
26 #include "ipdbg.h"
27 
28 #include <signal.h>
29 
30 #ifdef HAVE_NETDB_H
31 #include <netdb.h>
32 #endif
33 
34 #ifndef _WIN32
35 #include <netinet/tcp.h>
36 #endif
37 
38 static struct service *services;
39 
41  CONTINUE_MAIN_LOOP, /* stay in main event loop */
42  SHUTDOWN_REQUESTED, /* set by shutdown command; exit the event loop and quit the debugger */
43  SHUTDOWN_WITH_ERROR_CODE, /* set by shutdown command; quit with non-zero return code */
44  SHUTDOWN_WITH_SIGNAL_CODE /* set by sig_handler; exec shutdown then exit with signal as return code */
45 };
46 
47 static volatile sig_atomic_t shutdown_openocd = CONTINUE_MAIN_LOOP;
48 /* store received signal to exit application by killing ourselves */
49 static volatile sig_atomic_t last_signal;
50 
51 /* set the polling period to 100ms */
52 static int polling_period = 100;
53 
54 /* address by name on which to listen for incoming TCP/IP connections */
55 static char *bindto_name;
56 
57 static int add_connection(struct service *service, struct command_context *cmd_ctx)
58 {
59  socklen_t address_size;
60  struct connection *c, **p;
61  int retval;
62  int flag = 1;
63 
64  c = malloc(sizeof(struct connection));
65  c->fd = -1;
66  c->fd_out = -1;
67  memset(&c->sin, 0, sizeof(c->sin));
69  c->service = service;
70  c->input_pending = false;
71  c->priv = NULL;
72  c->next = NULL;
73 
74  if (service->type == CONNECTION_TCP) {
75  address_size = sizeof(c->sin);
76 
77  c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
78  c->fd_out = c->fd;
79 
80  /* This increases performance dramatically for e.g. GDB load which
81  * does not have a sliding window protocol.
82  *
83  * Ignore errors from this fn as it probably just means less performance
84  */
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 */
90 
91  LOG_INFO("accepting '%s' connection on tcp/%s", service->name, service->port);
92  retval = service->new_connection(c);
93  if (retval != ERROR_OK) {
94  close_socket(c->fd);
95  LOG_ERROR("attempted '%s' connection rejected", service->name);
97  free(c);
98  return retval;
99  }
100  } else if (service->type == CONNECTION_STDINOUT) {
101  c->fd = service->fd;
102  c->fd_out = fileno(stdout);
103 
104 #ifdef _WIN32
105  /* we are using stdin/out so ignore ctrl-c under windoze */
106  SetConsoleCtrlHandler(NULL, TRUE);
107 #endif
108 
109  /* do not check for new connections again on stdin */
110  service->fd = -1;
111 
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);
117  free(c);
118  return retval;
119  }
120  } else if (service->type == CONNECTION_PIPE) {
121  c->fd = service->fd;
122  /* do not check for new connections again on stdin */
123  service->fd = -1;
124 
125  char *out_file = alloc_printf("%so", service->port);
126  c->fd_out = open(out_file, O_WRONLY);
127  free(out_file);
128  if (c->fd_out == -1) {
129  LOG_ERROR("could not open %s", service->port);
130  command_done(c->cmd_ctx);
131  free(c);
132  return ERROR_FAIL;
133  }
134 
135  LOG_INFO("accepting '%s' connection from pipe %s", service->name, service->port);
136  retval = service->new_connection(c);
137  if (retval != ERROR_OK) {
138  LOG_ERROR("attempted '%s' connection rejected", service->name);
139  command_done(c->cmd_ctx);
140  free(c);
141  return retval;
142  }
143  }
144 
145  /* add to the end of linked list */
146  for (p = &service->connections; *p; p = &(*p)->next)
147  ;
148  *p = c;
149 
152 
153  return ERROR_OK;
154 }
155 
157 {
158  struct connection **p = &service->connections;
159  struct connection *c;
160 
161  /* find connection */
162  while ((c = *p)) {
163  if (c->fd == connection->fd) {
166  if (service->type == CONNECTION_TCP)
167  close_socket(c->fd);
168  else if (service->type == CONNECTION_PIPE) {
169  /* The service will listen to the pipe again */
170  c->service->fd = c->fd;
171  }
172 
173  command_done(c->cmd_ctx);
174 
175  /* delete connection */
176  *p = c->next;
177  free(c);
178 
181 
182  break;
183  }
184 
185  /* redirect p to next list pointer */
186  p = &(*p)->next;
187  }
188 
189  return ERROR_OK;
190 }
191 
192 static void free_service(struct service *c)
193 {
194  if (c->type == CONNECTION_PIPE && c->fd != -1)
195  close(c->fd);
196  if (c->type == CONNECTION_TCP && c->fd != -1)
197  close_socket(c->fd);
198  if (c->service_dtor)
199  c->service_dtor(c);
200  free(c->name);
201  free(c->port);
202  free(c->priv);
203  free(c);
204 }
205 
206 int add_service(const struct service_driver *driver, const char *port,
207  int max_connections, void *priv)
208 {
209  struct service *c, **p;
210  struct hostent *hp;
211  int so_reuseaddr_option = 1;
212 
213  c = calloc(1, sizeof(*c));
214  if (!c) {
215  LOG_ERROR("Out of memory");
216  return ERROR_FAIL;
217  }
218 
219  c->name = strdup(driver->name);
220  c->port = strdup(port);
221  c->max_connections = 1; /* Only TCP/IP ports can support more than one connection */
222  c->fd = -1;
223  c->connections = NULL;
226  c->input = driver->input_handler;
229  c->service_dtor = driver->service_dtor_handler;
230  c->priv = priv;
231  c->next = NULL;
232 
233  if (!c->name || !c->port) {
234  LOG_ERROR("Out of memory");
235  goto error;
236  }
237 
238  long portnumber;
239  if (strcmp(c->port, "pipe") == 0)
241  else {
242  char *end;
243  portnumber = strtol(c->port, &end, 0);
244  if (!*end && (parse_long(c->port, &portnumber) == ERROR_OK)) {
245  c->portnumber = portnumber;
246  c->type = CONNECTION_TCP;
247  } else
248  c->type = CONNECTION_PIPE;
249  }
250 
251  if (c->type == CONNECTION_TCP) {
252  c->max_connections = max_connections;
253 
254  c->fd = socket(AF_INET, SOCK_STREAM, 0);
255  if (c->fd == -1) {
256  LOG_ERROR("error creating socket: %s", strerror(errno));
257  goto error;
258  }
259 
260  setsockopt(c->fd,
261  SOL_SOCKET,
262  SO_REUSEADDR,
263  (void *)&so_reuseaddr_option,
264  sizeof(int));
265 
266  socket_nonblock(c->fd);
267 
268  memset(&c->sin, 0, sizeof(c->sin));
269  c->sin.sin_family = AF_INET;
270 
271  if (!bindto_name)
272  c->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
273  else {
274  hp = gethostbyname(bindto_name);
275  if (!hp) {
276  LOG_ERROR("couldn't resolve bindto address: %s", bindto_name);
277  close_socket(c->fd);
278  goto error;
279  }
280  memcpy(&c->sin.sin_addr, hp->h_addr_list[0], hp->h_length);
281  }
282  c->sin.sin_port = htons(c->portnumber);
283 
284  if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1) {
285  LOG_ERROR("couldn't bind %s to socket on port %d: %s", c->name, c->portnumber, strerror(errno));
286  close_socket(c->fd);
287  goto error;
288  }
289 
290 #ifndef _WIN32
291  int segsize = 65536;
292  setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
293 #endif
294  int window_size = 128 * 1024;
295 
296  /* These setsockopt()s must happen before the listen() */
297 
298  setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
299  (char *)&window_size, sizeof(window_size));
300  setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
301  (char *)&window_size, sizeof(window_size));
302 
303  if (listen(c->fd, 1) == -1) {
304  LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
305  close_socket(c->fd);
306  goto error;
307  }
308 
309  struct sockaddr_in addr_in;
310  addr_in.sin_port = 0;
311  socklen_t addr_in_size = sizeof(addr_in);
312  if (getsockname(c->fd, (struct sockaddr *)&addr_in, &addr_in_size) == 0)
313  LOG_INFO("Listening on port %hu for %s connections",
314  ntohs(addr_in.sin_port), c->name);
315  } else if (c->type == CONNECTION_STDINOUT) {
316  c->fd = fileno(stdin);
317 
318 #ifdef _WIN32
319  /* for win32 set stdin/stdout to binary mode */
320  if (_setmode(_fileno(stdout), _O_BINARY) < 0)
321  LOG_WARNING("cannot change stdout mode to binary");
322  if (_setmode(_fileno(stdin), _O_BINARY) < 0)
323  LOG_WARNING("cannot change stdin mode to binary");
324  if (_setmode(_fileno(stderr), _O_BINARY) < 0)
325  LOG_WARNING("cannot change stderr mode to binary");
326 #else
327  socket_nonblock(c->fd);
328 #endif
329  } else if (c->type == CONNECTION_PIPE) {
330 #ifdef _WIN32
331  /* we currently do not support named pipes under win32
332  * so exit openocd for now */
333  LOG_ERROR("Named pipes currently not supported under this os");
334  goto error;
335 #else
336  /* Pipe we're reading from */
337  c->fd = open(c->port, O_RDONLY | O_NONBLOCK);
338  if (c->fd == -1) {
339  LOG_ERROR("could not open %s", c->port);
340  goto error;
341  }
342 #endif
343  }
344 
345  /* add to the end of linked list */
346  for (p = &services; *p; p = &(*p)->next)
347  ;
348  *p = c;
349 
350  return ERROR_OK;
351 
352 error:
353  // Only free() what has been locally allocated
354  free(c->port);
355  free(c->name);
356  free(c);
357 
358  return ERROR_FAIL;
359 }
360 
361 static void remove_connections(struct service *service)
362 {
363  struct connection *connection;
364 
366 
367  while (connection) {
368  struct connection *tmp;
369 
370  tmp = connection->next;
372  connection = tmp;
373  }
374 }
375 
376 int remove_service(const char *name, const char *port)
377 {
378  struct service *tmp;
379  struct service *prev;
380 
381  prev = services;
382 
383  for (tmp = services; tmp; prev = tmp, tmp = tmp->next) {
384  if (!strcmp(tmp->name, name) && !strcmp(tmp->port, port)) {
385  remove_connections(tmp);
386 
387  if (tmp == services)
388  services = tmp->next;
389  else
390  prev->next = tmp->next;
391 
392  if (tmp->type != CONNECTION_STDINOUT)
393  close_socket(tmp->fd);
394 
395  free_service(tmp);
396 
397  return ERROR_OK;
398  }
399  }
400 
401  return ERROR_OK;
402 }
403 
404 static int remove_services(void)
405 {
406  struct service *c = services;
407 
408  /* loop service */
409  while (c) {
410  struct service *next = c->next;
411 
413  free_service(c);
414  /* remember the last service for unlinking */
415  c = next;
416  }
417 
418  services = NULL;
419 
420  return ERROR_OK;
421 }
422 
424 {
425  for (struct service *s = services; s; s = s->next)
426  if (s->keep_client_alive)
427  for (struct connection *c = s->connections; c; c = c->next)
428  s->keep_client_alive(c);
429 }
430 
432 {
433  struct service *service;
434 
435  bool poll_ok = true;
436 
437  /* used in select() */
438  fd_set read_fds;
439  int fd_max;
440 
441  /* used in accept() */
442  int retval;
443 
444  int64_t next_event = timeval_ms() + polling_period;
445 
446 #ifndef _WIN32
447  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
448  LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
449 #endif
450 
452  /* monitor sockets for activity */
453  fd_max = 0;
454  FD_ZERO(&read_fds);
455 
456  /* add service and connection fds to read_fds */
457  for (service = services; service; service = service->next) {
458  if (service->fd != -1) {
459  /* listen for new connections */
460  FD_SET(service->fd, &read_fds);
461 
462  if (service->fd > fd_max)
463  fd_max = service->fd;
464  }
465 
466  if (service->connections) {
467  struct connection *c;
468 
469  for (c = service->connections; c; c = c->next) {
470  /* check for activity on the connection */
471  FD_SET(c->fd, &read_fds);
472  if (c->fd > fd_max)
473  fd_max = c->fd;
474  }
475  }
476  }
477 
478  struct timeval tv;
479  tv.tv_sec = 0;
480  if (poll_ok) {
481  /* we're just polling this iteration, this is faster on embedded
482  * hosts */
483  tv.tv_usec = 0;
484  retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
485  } else {
486  /* Timeout socket_select() when a target timer expires or every polling_period */
487  int timeout_ms = next_event - timeval_ms();
488  if (timeout_ms < 0)
489  timeout_ms = 0;
490  else if (timeout_ms > polling_period)
491  timeout_ms = polling_period;
492  tv.tv_usec = timeout_ms * 1000;
493  /* Only while we're sleeping we'll let others run */
494  retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
495  }
496 
497  if (retval == -1) {
498 #ifdef _WIN32
499 
500  errno = WSAGetLastError();
501 
502  if (errno == WSAEINTR)
503  FD_ZERO(&read_fds);
504  else {
505  LOG_ERROR("error during select: %s", strerror(errno));
506  return ERROR_FAIL;
507  }
508 #else
509 
510  if (errno == EINTR)
511  FD_ZERO(&read_fds);
512  else {
513  LOG_ERROR("error during select: %s", strerror(errno));
514  return ERROR_FAIL;
515  }
516 #endif
517  }
518 
519  if (retval == 0) {
520  /* Execute callbacks of expired timers when
521  * - there was nothing to do if poll_ok was true
522  * - socket_select() timed out if poll_ok was false, now one or more
523  * timers expired or the polling period elapsed
524  */
526  next_event = target_timer_next_event();
528 
529  FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
530 
531  /* We timed out/there was nothing to do, timeout rather than poll next time
532  **/
533  poll_ok = false;
534  } else {
535  /* There was something to do, next time we'll just poll */
536  poll_ok = true;
537  }
538 
539  /* This is a simple back-off algorithm where we immediately
540  * re-poll if we did something this time around.
541  *
542  * This greatly improves performance of DCC.
543  */
544  poll_ok = poll_ok || target_got_message();
545 
546  for (service = services; service; service = service->next) {
547  /* handle new connections on listeners */
548  if ((service->fd != -1)
549  && (FD_ISSET(service->fd, &read_fds))) {
550  if (service->max_connections != 0)
552  else {
553  if (service->type == CONNECTION_TCP) {
554  struct sockaddr_in sin;
555  socklen_t address_size = sizeof(sin);
556  int tmp_fd;
557  tmp_fd = accept(service->fd,
558  (struct sockaddr *)&service->sin,
559  &address_size);
560  close_socket(tmp_fd);
561  }
562  LOG_INFO(
563  "rejected '%s' connection, no more connections allowed",
564  service->name);
565  }
566  }
567 
568  /* handle activity on connections */
569  if (service->connections) {
570  struct connection *c;
571 
572  for (c = service->connections; c; ) {
573  if ((c->fd >= 0 && FD_ISSET(c->fd, &read_fds)) || c->input_pending) {
574  retval = service->input(c);
575  if (retval != ERROR_OK) {
576  struct connection *next = c->next;
577  if (service->type == CONNECTION_PIPE ||
579  /* if connection uses a pipe then
580  * shutdown openocd on error */
582  }
584  LOG_INFO("dropped '%s' connection",
585  service->name);
586  c = next;
587  continue;
588  }
589  }
590  c = c->next;
591  }
592  }
593  }
594 
595 #ifdef _WIN32
596  MSG msg;
597  while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
598  if (msg.message == WM_QUIT)
600  }
601 #endif
602  }
603 
604  /* when quit for signal or CTRL-C, run (eventually user implemented) "shutdown" */
606  command_run_line(command_context, "shutdown");
607 
609 }
610 
611 static void sig_handler(int sig)
612 {
613  /* store only first signal that hits us */
616  assert(sig >= SIG_ATOMIC_MIN && sig <= SIG_ATOMIC_MAX);
617  last_signal = sig;
618  LOG_DEBUG("Terminating on Signal %d", sig);
619  } else
620  LOG_DEBUG("Ignored extra Signal %d", sig);
621 }
622 
623 
624 #ifdef _WIN32
625 BOOL WINAPI control_handler(DWORD ctrl_type)
626 {
628  return TRUE;
629 }
630 #else
631 static void sigkey_handler(int sig)
632 {
633  /* ignore keystroke generated signals if not in foreground process group */
634 
635  if (tcgetpgrp(STDIN_FILENO) > 0)
636  sig_handler(sig);
637  else
638  LOG_DEBUG("Ignored Signal %d", sig);
639 }
640 #endif
641 
642 
644 {
645  /* this currently only calls WSAStartup on native win32 systems
646  * before any socket operations are performed.
647  * This is an issue if you call init in your config script */
648 
649 #ifdef _WIN32
650  WORD version_requested;
651  WSADATA wsadata;
652 
653  version_requested = MAKEWORD(2, 2);
654 
655  if (WSAStartup(version_requested, &wsadata) != 0) {
656  LOG_ERROR("Failed to Open Winsock");
657  return ERROR_FAIL;
658  }
659 #endif
660  return ERROR_OK;
661 }
662 
664 {
665 #ifdef _WIN32
666  WSACleanup();
667 #endif
668  return ERROR_OK;
669 }
670 
671 int server_preinit(void)
672 {
673 #ifdef _WIN32
674  /* register ctrl-c handler */
675  SetConsoleCtrlHandler(control_handler, TRUE);
676 
677  signal(SIGBREAK, sig_handler);
678  signal(SIGINT, sig_handler);
679 #else
680  signal(SIGHUP, sig_handler);
681  signal(SIGPIPE, sig_handler);
682  signal(SIGQUIT, sigkey_handler);
683  signal(SIGINT, sigkey_handler);
684 #endif
685  signal(SIGTERM, sig_handler);
686  signal(SIGABRT, sig_handler);
687 
688  return ERROR_OK;
689 }
690 
692 {
693  int ret = tcl_init();
694 
695  if (ret != ERROR_OK)
696  return ret;
697 
698  ret = telnet_init("Open On-Chip Debugger");
699 
700  if (ret != ERROR_OK) {
701  remove_services();
702  return ret;
703  }
704 
705  return ERROR_OK;
706 }
707 
708 int server_quit(void)
709 {
710  remove_services();
711  target_quit();
712 
713 #ifdef _WIN32
714  SetConsoleCtrlHandler(control_handler, FALSE);
715 
716  return ERROR_OK;
717 #endif
718 
719  /* return signal number so we can kill ourselves */
720  return last_signal;
721 }
722 
723 void server_free(void)
724 {
729 
730  free(bindto_name);
731 }
732 
733 void exit_on_signal(int sig)
734 {
735 #ifndef _WIN32
736  /* bring back default system handler and kill yourself */
737  signal(sig, SIG_DFL);
738  kill(getpid(), sig);
739 #endif
740 }
741 
742 int connection_write(struct connection *connection, const void *data, int len)
743 {
744  if (len == 0) {
745  /* successful no-op. Sockets and pipes behave differently here... */
746  return 0;
747  }
749  return write_socket(connection->fd_out, data, len);
750  else
751  return write(connection->fd_out, data, len);
752 }
753 
754 int connection_read(struct connection *connection, void *data, int len)
755 {
757  return read_socket(connection->fd, data, len);
758  else
759  return read(connection->fd, data, len);
760 }
761 
763 {
765 }
766 
767 /* tell the server we want to shut down */
768 COMMAND_HANDLER(handle_shutdown_command)
769 {
770  LOG_USER("shutdown command invoked");
771 
773 
774  command_run_line(CMD_CTX, "_run_pre_shutdown_commands");
775 
776  if (CMD_ARGC == 1) {
777  if (!strcmp(CMD_ARGV[0], "error")) {
779  return ERROR_FAIL;
780  }
781  }
782 
784 }
785 
786 COMMAND_HANDLER(handle_poll_period_command)
787 {
788  if (CMD_ARGC == 0)
789  LOG_WARNING("You need to set a period value");
790  else
792 
793  LOG_INFO("set servers polling period to %ums", polling_period);
794 
795  return ERROR_OK;
796 }
797 
798 COMMAND_HANDLER(handle_bindto_command)
799 {
800  switch (CMD_ARGC) {
801  case 0:
802  command_print(CMD, "bindto name: %s", bindto_name);
803  break;
804  case 1:
805  free(bindto_name);
806  bindto_name = strdup(CMD_ARGV[0]);
807  break;
808  default:
810  }
811  return ERROR_OK;
812 }
813 
814 static const struct command_registration server_command_handlers[] = {
815  {
816  .name = "shutdown",
817  .handler = &handle_shutdown_command,
818  .mode = COMMAND_ANY,
819  .usage = "",
820  .help = "shut the server down",
821  },
822  {
823  .name = "poll_period",
824  .handler = &handle_poll_period_command,
825  .mode = COMMAND_ANY,
826  .usage = "",
827  .help = "set the servers polling period",
828  },
829  {
830  .name = "bindto",
831  .handler = &handle_bindto_command,
832  .mode = COMMAND_CONFIG,
833  .usage = "[name]",
834  .help = "Specify address by name on which to listen for "
835  "incoming TCP/IP connections",
836  },
838 };
839 
841 {
842  int retval = telnet_register_commands(cmd_ctx);
843  if (retval != ERROR_OK)
844  return retval;
845 
846  retval = tcl_register_commands(cmd_ctx);
847  if (retval != ERROR_OK)
848  return retval;
849 
850  retval = jsp_register_commands(cmd_ctx);
851  if (retval != ERROR_OK)
852  return retval;
853 
855 }
856 
857 COMMAND_HELPER(server_port_command, unsigned short *out)
858 {
859  switch (CMD_ARGC) {
860  case 0:
861  command_print(CMD, "%d", *out);
862  break;
863  case 1:
864  {
865  uint16_t port;
866  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
867  *out = port;
868  break;
869  }
870  default:
872  }
873  return ERROR_OK;
874 }
875 
876 COMMAND_HELPER(server_pipe_command, char **out)
877 {
878  switch (CMD_ARGC) {
879  case 0:
880  command_print(CMD, "%s", *out);
881  break;
882  case 1:
883  {
884  if (CMD_CTX->mode == COMMAND_EXEC) {
885  LOG_WARNING("unable to change server port after init");
887  }
888  free(*out);
889  *out = strdup(CMD_ARGV[0]);
890  break;
891  }
892  default:
894  }
895  return ERROR_OK;
896 }
#define MSG
Definition: arm_tpiu_swo.c:43
const char * name
Definition: armv4_5.c:76
void command_done(struct command_context *cmd_ctx)
Frees the resources associated with a command context.
Definition: command.c:584
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
void process_jim_events(struct command_context *cmd_ctx)
Definition: command.c:1221
struct command_context * copy_command_context(struct command_context *context)
Creates a copy of an existing command context.
Definition: command.c:575
int command_run_line(struct command_context *context, char *line)
Definition: command.c:497
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define ERROR_COMMAND_CLOSE_CONNECTION
Definition: command.h:404
int parse_long(const char *str, long *ul)
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:407
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
Definition: command.h:277
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
int ipdbg_server_free(void)
Definition: ipdbg.c:780
void jsp_service_free(void)
Definition: jsp_server.c:236
int jsp_register_commands(struct command_context *cmd_ctx)
Definition: jsp_server.c:230
char * alloc_printf(const char *format,...)
Definition: log.c:377
#define LOG_USER(expr ...)
Definition: log.h:143
#define LOG_WARNING(expr ...)
Definition: log.h:137
#define ERROR_FAIL
Definition: log.h:181
#define LOG_ERROR(expr ...)
Definition: log.h:140
#define LOG_INFO(expr ...)
Definition: log.h:134
#define LOG_DEBUG(expr ...)
Definition: log.h:117
#define ERROR_OK
Definition: log.h:175
int flag
Definition: mips64.c:29
static int socket_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
Definition: replacements.h:215
static int read_socket(int handle, void *buffer, unsigned int count)
Definition: replacements.h:175
static int close_socket(int sock)
Definition: replacements.h:184
static int write_socket(int handle, const void *buffer, unsigned int count)
Definition: replacements.h:166
static void socket_nonblock(int fd)
Definition: replacements.h:204
void exit_on_signal(int sig)
Definition: server.c:733
int connection_write(struct connection *connection, const void *data, int len)
Definition: server.c:742
int server_preinit(void)
Definition: server.c:671
static volatile sig_atomic_t shutdown_openocd
Definition: server.c:47
static void sig_handler(int sig)
Definition: server.c:611
int connection_read(struct connection *connection, void *data, int len)
Definition: server.c:754
static void free_service(struct service *c)
Definition: server.c:192
int server_host_os_close(void)
Definition: server.c:663
static char * bindto_name
Definition: server.c:55
void server_free(void)
Definition: server.c:723
static const struct command_registration server_command_handlers[]
Definition: server.c:814
int server_host_os_entry(void)
Definition: server.c:643
COMMAND_HANDLER(handle_shutdown_command)
Definition: server.c:768
static void remove_connections(struct service *service)
Definition: server.c:361
static int remove_services(void)
Definition: server.c:404
static int remove_connection(struct service *service, struct connection *connection)
Definition: server.c:156
void server_keep_clients_alive(void)
Definition: server.c:423
shutdown_reason
Definition: server.c:40
@ SHUTDOWN_REQUESTED
Definition: server.c:42
@ SHUTDOWN_WITH_ERROR_CODE
Definition: server.c:43
@ CONTINUE_MAIN_LOOP
Definition: server.c:41
@ SHUTDOWN_WITH_SIGNAL_CODE
Definition: server.c:44
bool openocd_is_shutdown_pending(void)
Definition: server.c:762
int server_loop(struct command_context *command_context)
Definition: server.c:431
int remove_service(const char *name, const char *port)
Definition: server.c:376
int server_quit(void)
Definition: server.c:708
static int add_connection(struct service *service, struct command_context *cmd_ctx)
Definition: server.c:57
static volatile sig_atomic_t last_signal
Definition: server.c:49
int server_register_commands(struct command_context *cmd_ctx)
Definition: server.c:840
static struct service * services
Definition: server.c:38
COMMAND_HELPER(server_port_command, unsigned short *out)
Definition: server.c:857
int add_service(const struct service_driver *driver, const char *port, int max_connections, void *priv)
Definition: server.c:206
int server_init(struct command_context *cmd_ctx)
Definition: server.c:691
static void sigkey_handler(int sig)
Definition: server.c:631
static int polling_period
Definition: server.c:52
#define CONNECTION_LIMIT_UNLIMITED
Definition: server.h:34
@ CONNECTION_PIPE
Definition: server.h:30
@ CONNECTION_STDINOUT
Definition: server.h:31
@ CONNECTION_TCP
Definition: server.h:29
const char * name
Definition: command.h:239
struct sockaddr_in sin
Definition: server.h:39
struct command_context * cmd_ctx
Definition: server.h:40
void * priv
Definition: server.h:43
int fd_out
Definition: server.h:38
int fd
Definition: server.h:37
struct service * service
Definition: server.h:41
struct connection * next
Definition: server.h:44
bool input_pending
Definition: server.h:42
void(* service_dtor_handler)(struct service *service)
Definition: server.h:61
const char * name
the name of the server
Definition: server.h:49
int(* connection_closed_handler)(struct connection *connection)
callback to tear down the connection
Definition: server.h:63
void(* keep_client_alive_handler)(struct connection *connection)
called periodically to send keep-alive messages on the connection
Definition: server.h:65
int(* new_connection_handler)(struct connection *connection)
complete code to accept a new connection.
Definition: server.h:58
int(* new_connection_during_keep_alive_handler)(struct connection *connection)
optional minimal setup to accept a connection during keep-alive
Definition: server.h:51
int(* input_handler)(struct connection *connection)
callback to handle incoming data
Definition: server.h:60
Definition: server.h:68
struct service * next
Definition: server.h:84
int fd
Definition: server.h:73
void(* service_dtor)(struct service *service)
Definition: server.h:80
void * priv
Definition: server.h:83
int max_connections
Definition: server.h:75
int(* new_connection_during_keep_alive)(struct connection *connection)
Definition: server.h:77
struct sockaddr_in sin
Definition: server.h:74
struct connection * connections
Definition: server.h:76
int(* input)(struct connection *connection)
Definition: server.h:79
char * name
Definition: server.h:69
char * port
Definition: server.h:71
int(* connection_closed)(struct connection *connection)
Definition: server.h:81
unsigned short portnumber
Definition: server.h:72
void(* keep_client_alive)(struct connection *connection)
Definition: server.h:82
enum connection_type type
Definition: server.h:70
int(* new_connection)(struct connection *connection)
Definition: server.h:78
Definition: ftdi.c:138
long tv_sec
Definition: replacements.h:46
long tv_usec
Definition: replacements.h:47
int target_call_timer_callbacks(void)
Definition: target.c:1896
int64_t target_timer_next_event(void)
Returns when the next registered event will take place.
Definition: target.c:1907
void target_quit(void)
Free all the resources allocated by targets and the target layer.
Definition: target.c:2247
bool target_got_message(void)
Read and clear the flag as to whether we got a message.
int tcl_register_commands(struct command_context *cmd_ctx)
Definition: tcl_server.c:364
void tcl_service_free(void)
Definition: tcl_server.c:370
int tcl_init(void)
Definition: tcl_server.c:277
void telnet_service_free(void)
int telnet_register_commands(struct command_context *cmd_ctx)
int telnet_init(char *banner)
int64_t timeval_ms(void)
#define NULL
Definition: usb.h:16
#define WORD
Definition: x86_32_common.h:32
#define DWORD
Definition: x86_32_common.h:33