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 };
47 
48 /* store received signal to exit application by killing ourselves */
49 static int 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) {
165  if (service->type == CONNECTION_TCP)
166  close_socket(c->fd);
167  else if (service->type == CONNECTION_PIPE) {
168  /* The service will listen to the pipe again */
169  c->service->fd = c->fd;
170  }
171 
172  command_done(c->cmd_ctx);
173 
174  /* delete connection */
175  *p = c->next;
176  free(c);
177 
180 
181  break;
182  }
183 
184  /* redirect p to next list pointer */
185  p = &(*p)->next;
186  }
187 
188  return ERROR_OK;
189 }
190 
191 static void free_service(struct service *c)
192 {
193  free(c->name);
194  free(c->port);
195  free(c);
196 }
197 
198 int add_service(const struct service_driver *driver, const char *port,
199  int max_connections, void *priv)
200 {
201  struct service *c, **p;
202  struct hostent *hp;
203  int so_reuseaddr_option = 1;
204 
205  c = malloc(sizeof(struct service));
206 
207  c->name = strdup(driver->name);
208  c->port = strdup(port);
209  c->max_connections = 1; /* Only TCP/IP ports can support more than one connection */
210  c->fd = -1;
211  c->connections = NULL;
214  c->input = driver->input_handler;
217  c->priv = priv;
218  c->next = NULL;
219  long portnumber;
220  if (strcmp(c->port, "pipe") == 0)
222  else {
223  char *end;
224  portnumber = strtol(c->port, &end, 0);
225  if (!*end && (parse_long(c->port, &portnumber) == ERROR_OK)) {
226  c->portnumber = portnumber;
227  c->type = CONNECTION_TCP;
228  } else
229  c->type = CONNECTION_PIPE;
230  }
231 
232  if (c->type == CONNECTION_TCP) {
233  c->max_connections = max_connections;
234 
235  c->fd = socket(AF_INET, SOCK_STREAM, 0);
236  if (c->fd == -1) {
237  LOG_ERROR("error creating socket: %s", strerror(errno));
238  free_service(c);
239  return ERROR_FAIL;
240  }
241 
242  setsockopt(c->fd,
243  SOL_SOCKET,
244  SO_REUSEADDR,
245  (void *)&so_reuseaddr_option,
246  sizeof(int));
247 
248  socket_nonblock(c->fd);
249 
250  memset(&c->sin, 0, sizeof(c->sin));
251  c->sin.sin_family = AF_INET;
252 
253  if (!bindto_name)
254  c->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
255  else {
256  hp = gethostbyname(bindto_name);
257  if (!hp) {
258  LOG_ERROR("couldn't resolve bindto address: %s", bindto_name);
259  close_socket(c->fd);
260  free_service(c);
261  return ERROR_FAIL;
262  }
263  memcpy(&c->sin.sin_addr, hp->h_addr_list[0], hp->h_length);
264  }
265  c->sin.sin_port = htons(c->portnumber);
266 
267  if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1) {
268  LOG_ERROR("couldn't bind %s to socket on port %d: %s", c->name, c->portnumber, strerror(errno));
269  close_socket(c->fd);
270  free_service(c);
271  return ERROR_FAIL;
272  }
273 
274 #ifndef _WIN32
275  int segsize = 65536;
276  setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
277 #endif
278  int window_size = 128 * 1024;
279 
280  /* These setsockopt()s must happen before the listen() */
281 
282  setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
283  (char *)&window_size, sizeof(window_size));
284  setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
285  (char *)&window_size, sizeof(window_size));
286 
287  if (listen(c->fd, 1) == -1) {
288  LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
289  close_socket(c->fd);
290  free_service(c);
291  return ERROR_FAIL;
292  }
293 
294  struct sockaddr_in addr_in;
295  addr_in.sin_port = 0;
296  socklen_t addr_in_size = sizeof(addr_in);
297  if (getsockname(c->fd, (struct sockaddr *)&addr_in, &addr_in_size) == 0)
298  LOG_INFO("Listening on port %hu for %s connections",
299  ntohs(addr_in.sin_port), c->name);
300  } else if (c->type == CONNECTION_STDINOUT) {
301  c->fd = fileno(stdin);
302 
303 #ifdef _WIN32
304  /* for win32 set stdin/stdout to binary mode */
305  if (_setmode(_fileno(stdout), _O_BINARY) < 0)
306  LOG_WARNING("cannot change stdout mode to binary");
307  if (_setmode(_fileno(stdin), _O_BINARY) < 0)
308  LOG_WARNING("cannot change stdin mode to binary");
309  if (_setmode(_fileno(stderr), _O_BINARY) < 0)
310  LOG_WARNING("cannot change stderr mode to binary");
311 #else
312  socket_nonblock(c->fd);
313 #endif
314  } else if (c->type == CONNECTION_PIPE) {
315 #ifdef _WIN32
316  /* we currently do not support named pipes under win32
317  * so exit openocd for now */
318  LOG_ERROR("Named pipes currently not supported under this os");
319  free_service(c);
320  return ERROR_FAIL;
321 #else
322  /* Pipe we're reading from */
323  c->fd = open(c->port, O_RDONLY | O_NONBLOCK);
324  if (c->fd == -1) {
325  LOG_ERROR("could not open %s", c->port);
326  free_service(c);
327  return ERROR_FAIL;
328  }
329 #endif
330  }
331 
332  /* add to the end of linked list */
333  for (p = &services; *p; p = &(*p)->next)
334  ;
335  *p = c;
336 
337  return ERROR_OK;
338 }
339 
340 static void remove_connections(struct service *service)
341 {
342  struct connection *connection;
343 
345 
346  while (connection) {
347  struct connection *tmp;
348 
349  tmp = connection->next;
351  connection = tmp;
352  }
353 }
354 
355 int remove_service(const char *name, const char *port)
356 {
357  struct service *tmp;
358  struct service *prev;
359 
360  prev = services;
361 
362  for (tmp = services; tmp; prev = tmp, tmp = tmp->next) {
363  if (!strcmp(tmp->name, name) && !strcmp(tmp->port, port)) {
364  remove_connections(tmp);
365 
366  if (tmp == services)
367  services = tmp->next;
368  else
369  prev->next = tmp->next;
370 
371  if (tmp->type != CONNECTION_STDINOUT)
372  close_socket(tmp->fd);
373 
374  free(tmp->priv);
375  free_service(tmp);
376 
377  return ERROR_OK;
378  }
379  }
380 
381  return ERROR_OK;
382 }
383 
384 static int remove_services(void)
385 {
386  struct service *c = services;
387 
388  /* loop service */
389  while (c) {
390  struct service *next = c->next;
391 
393 
394  free(c->name);
395 
396  if (c->type == CONNECTION_PIPE) {
397  if (c->fd != -1)
398  close(c->fd);
399  }
400  free(c->port);
401  free(c->priv);
402  /* delete service */
403  free(c);
404 
405  /* remember the last service for unlinking */
406  c = next;
407  }
408 
409  services = NULL;
410 
411  return ERROR_OK;
412 }
413 
415 {
416  for (struct service *s = services; s; s = s->next)
417  if (s->keep_client_alive)
418  for (struct connection *c = s->connections; c; c = c->next)
419  s->keep_client_alive(c);
420 }
421 
423 {
424  struct service *service;
425 
426  bool poll_ok = true;
427 
428  /* used in select() */
429  fd_set read_fds;
430  int fd_max;
431 
432  /* used in accept() */
433  int retval;
434 
435  int64_t next_event = timeval_ms() + polling_period;
436 
437 #ifndef _WIN32
438  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
439  LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
440 #endif
441 
443  /* monitor sockets for activity */
444  fd_max = 0;
445  FD_ZERO(&read_fds);
446 
447  /* add service and connection fds to read_fds */
448  for (service = services; service; service = service->next) {
449  if (service->fd != -1) {
450  /* listen for new connections */
451  FD_SET(service->fd, &read_fds);
452 
453  if (service->fd > fd_max)
454  fd_max = service->fd;
455  }
456 
457  if (service->connections) {
458  struct connection *c;
459 
460  for (c = service->connections; c; c = c->next) {
461  /* check for activity on the connection */
462  FD_SET(c->fd, &read_fds);
463  if (c->fd > fd_max)
464  fd_max = c->fd;
465  }
466  }
467  }
468 
469  struct timeval tv;
470  tv.tv_sec = 0;
471  if (poll_ok) {
472  /* we're just polling this iteration, this is faster on embedded
473  * hosts */
474  tv.tv_usec = 0;
475  retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
476  } else {
477  /* Timeout socket_select() when a target timer expires or every polling_period */
478  int timeout_ms = next_event - timeval_ms();
479  if (timeout_ms < 0)
480  timeout_ms = 0;
481  else if (timeout_ms > polling_period)
482  timeout_ms = polling_period;
483  tv.tv_usec = timeout_ms * 1000;
484  /* Only while we're sleeping we'll let others run */
485  retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
486  }
487 
488  if (retval == -1) {
489 #ifdef _WIN32
490 
491  errno = WSAGetLastError();
492 
493  if (errno == WSAEINTR)
494  FD_ZERO(&read_fds);
495  else {
496  LOG_ERROR("error during select: %s", strerror(errno));
497  return ERROR_FAIL;
498  }
499 #else
500 
501  if (errno == EINTR)
502  FD_ZERO(&read_fds);
503  else {
504  LOG_ERROR("error during select: %s", strerror(errno));
505  return ERROR_FAIL;
506  }
507 #endif
508  }
509 
510  if (retval == 0) {
511  /* Execute callbacks of expired timers when
512  * - there was nothing to do if poll_ok was true
513  * - socket_select() timed out if poll_ok was false, now one or more
514  * timers expired or the polling period elapsed
515  */
517  next_event = target_timer_next_event();
519 
520  FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
521 
522  /* We timed out/there was nothing to do, timeout rather than poll next time
523  **/
524  poll_ok = false;
525  } else {
526  /* There was something to do, next time we'll just poll */
527  poll_ok = true;
528  }
529 
530  /* This is a simple back-off algorithm where we immediately
531  * re-poll if we did something this time around.
532  *
533  * This greatly improves performance of DCC.
534  */
535  poll_ok = poll_ok || target_got_message();
536 
537  for (service = services; service; service = service->next) {
538  /* handle new connections on listeners */
539  if ((service->fd != -1)
540  && (FD_ISSET(service->fd, &read_fds))) {
541  if (service->max_connections != 0)
543  else {
544  if (service->type == CONNECTION_TCP) {
545  struct sockaddr_in sin;
546  socklen_t address_size = sizeof(sin);
547  int tmp_fd;
548  tmp_fd = accept(service->fd,
549  (struct sockaddr *)&service->sin,
550  &address_size);
551  close_socket(tmp_fd);
552  }
553  LOG_INFO(
554  "rejected '%s' connection, no more connections allowed",
555  service->name);
556  }
557  }
558 
559  /* handle activity on connections */
560  if (service->connections) {
561  struct connection *c;
562 
563  for (c = service->connections; c; ) {
564  if ((c->fd >= 0 && FD_ISSET(c->fd, &read_fds)) || c->input_pending) {
565  retval = service->input(c);
566  if (retval != ERROR_OK) {
567  struct connection *next = c->next;
568  if (service->type == CONNECTION_PIPE ||
570  /* if connection uses a pipe then
571  * shutdown openocd on error */
573  }
575  LOG_INFO("dropped '%s' connection",
576  service->name);
577  c = next;
578  continue;
579  }
580  }
581  c = c->next;
582  }
583  }
584  }
585 
586 #ifdef _WIN32
587  MSG msg;
588  while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
589  if (msg.message == WM_QUIT)
591  }
592 #endif
593  }
594 
595  /* when quit for signal or CTRL-C, run (eventually user implemented) "shutdown" */
597  command_run_line(command_context, "shutdown");
598 
600 }
601 
602 static void sig_handler(int sig)
603 {
604  /* store only first signal that hits us */
607  last_signal = sig;
608  LOG_DEBUG("Terminating on Signal %d", sig);
609  } else
610  LOG_DEBUG("Ignored extra Signal %d", sig);
611 }
612 
613 
614 #ifdef _WIN32
615 BOOL WINAPI control_handler(DWORD ctrl_type)
616 {
618  return TRUE;
619 }
620 #else
621 static void sigkey_handler(int sig)
622 {
623  /* ignore keystroke generated signals if not in foreground process group */
624 
625  if (tcgetpgrp(STDIN_FILENO) > 0)
626  sig_handler(sig);
627  else
628  LOG_DEBUG("Ignored Signal %d", sig);
629 }
630 #endif
631 
632 
634 {
635  /* this currently only calls WSAStartup on native win32 systems
636  * before any socket operations are performed.
637  * This is an issue if you call init in your config script */
638 
639 #ifdef _WIN32
640  WORD version_requested;
641  WSADATA wsadata;
642 
643  version_requested = MAKEWORD(2, 2);
644 
645  if (WSAStartup(version_requested, &wsadata) != 0) {
646  LOG_ERROR("Failed to Open Winsock");
647  return ERROR_FAIL;
648  }
649 #endif
650  return ERROR_OK;
651 }
652 
654 {
655 #ifdef _WIN32
656  WSACleanup();
657 #endif
658  return ERROR_OK;
659 }
660 
661 int server_preinit(void)
662 {
663 #ifdef _WIN32
664  /* register ctrl-c handler */
665  SetConsoleCtrlHandler(control_handler, TRUE);
666 
667  signal(SIGBREAK, sig_handler);
668  signal(SIGINT, sig_handler);
669 #else
670  signal(SIGHUP, sig_handler);
671  signal(SIGPIPE, sig_handler);
672  signal(SIGQUIT, sigkey_handler);
673  signal(SIGINT, sigkey_handler);
674 #endif
675  signal(SIGTERM, sig_handler);
676  signal(SIGABRT, sig_handler);
677 
678  return ERROR_OK;
679 }
680 
682 {
683  int ret = tcl_init();
684 
685  if (ret != ERROR_OK)
686  return ret;
687 
688  ret = telnet_init("Open On-Chip Debugger");
689 
690  if (ret != ERROR_OK) {
691  remove_services();
692  return ret;
693  }
694 
695  return ERROR_OK;
696 }
697 
698 int server_quit(void)
699 {
700  remove_services();
701  target_quit();
702 
703 #ifdef _WIN32
704  SetConsoleCtrlHandler(control_handler, FALSE);
705 
706  return ERROR_OK;
707 #endif
708 
709  /* return signal number so we can kill ourselves */
710  return last_signal;
711 }
712 
713 void server_free(void)
714 {
719 
720  free(bindto_name);
721 }
722 
723 void exit_on_signal(int sig)
724 {
725 #ifndef _WIN32
726  /* bring back default system handler and kill yourself */
727  signal(sig, SIG_DFL);
728  kill(getpid(), sig);
729 #endif
730 }
731 
732 int connection_write(struct connection *connection, const void *data, int len)
733 {
734  if (len == 0) {
735  /* successful no-op. Sockets and pipes behave differently here... */
736  return 0;
737  }
739  return write_socket(connection->fd_out, data, len);
740  else
741  return write(connection->fd_out, data, len);
742 }
743 
744 int connection_read(struct connection *connection, void *data, int len)
745 {
747  return read_socket(connection->fd, data, len);
748  else
749  return read(connection->fd, data, len);
750 }
751 
753 {
755 }
756 
757 /* tell the server we want to shut down */
758 COMMAND_HANDLER(handle_shutdown_command)
759 {
760  LOG_USER("shutdown command invoked");
761 
763 
764  command_run_line(CMD_CTX, "_run_pre_shutdown_commands");
765 
766  if (CMD_ARGC == 1) {
767  if (!strcmp(CMD_ARGV[0], "error")) {
769  return ERROR_FAIL;
770  }
771  }
772 
774 }
775 
776 COMMAND_HANDLER(handle_poll_period_command)
777 {
778  if (CMD_ARGC == 0)
779  LOG_WARNING("You need to set a period value");
780  else
782 
783  LOG_INFO("set servers polling period to %ums", polling_period);
784 
785  return ERROR_OK;
786 }
787 
788 COMMAND_HANDLER(handle_bindto_command)
789 {
790  switch (CMD_ARGC) {
791  case 0:
792  command_print(CMD, "bindto name: %s", bindto_name);
793  break;
794  case 1:
795  free(bindto_name);
796  bindto_name = strdup(CMD_ARGV[0]);
797  break;
798  default:
800  }
801  return ERROR_OK;
802 }
803 
804 static const struct command_registration server_command_handlers[] = {
805  {
806  .name = "shutdown",
807  .handler = &handle_shutdown_command,
808  .mode = COMMAND_ANY,
809  .usage = "",
810  .help = "shut the server down",
811  },
812  {
813  .name = "poll_period",
814  .handler = &handle_poll_period_command,
815  .mode = COMMAND_ANY,
816  .usage = "",
817  .help = "set the servers polling period",
818  },
819  {
820  .name = "bindto",
821  .handler = &handle_bindto_command,
822  .mode = COMMAND_CONFIG,
823  .usage = "[name]",
824  .help = "Specify address by name on which to listen for "
825  "incoming TCP/IP connections",
826  },
828 };
829 
831 {
832  int retval = telnet_register_commands(cmd_ctx);
833  if (retval != ERROR_OK)
834  return retval;
835 
836  retval = tcl_register_commands(cmd_ctx);
837  if (retval != ERROR_OK)
838  return retval;
839 
840  retval = jsp_register_commands(cmd_ctx);
841  if (retval != ERROR_OK)
842  return retval;
843 
845 }
846 
847 COMMAND_HELPER(server_port_command, unsigned short *out)
848 {
849  switch (CMD_ARGC) {
850  case 0:
851  command_print(CMD, "%d", *out);
852  break;
853  case 1:
854  {
855  uint16_t port;
856  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
857  *out = port;
858  break;
859  }
860  default:
862  }
863  return ERROR_OK;
864 }
865 
866 COMMAND_HELPER(server_pipe_command, char **out)
867 {
868  switch (CMD_ARGC) {
869  case 0:
870  command_print(CMD, "%s", *out);
871  break;
872  case 1:
873  {
874  if (CMD_CTX->mode == COMMAND_EXEC) {
875  LOG_WARNING("unable to change server port after init");
877  }
878  free(*out);
879  *out = strdup(CMD_ARGV[0]);
880  break;
881  }
882  default:
884  }
885  return ERROR_OK;
886 }
#define MSG
Definition: arm_tpiu_swo.c:44
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:644
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:443
void process_jim_events(struct command_context *cmd_ctx)
Definition: command.c:1263
struct command_context * copy_command_context(struct command_context *context)
Creates a copy of an existing command context.
Definition: command.c:635
int command_run_line(struct command_context *context, char *line)
Definition: command.c:547
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define ERROR_COMMAND_CLOSE_CONNECTION
Definition: command.h:401
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:151
#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:442
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:146
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:404
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:274
@ 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:776
void jsp_service_free(void)
Definition: jsp_server.c:228
int jsp_register_commands(struct command_context *cmd_ctx)
Definition: jsp_server.c:222
char * alloc_printf(const char *format,...)
Definition: log.c:364
#define LOG_USER(expr ...)
Definition: log.h:135
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_INFO(expr ...)
Definition: log.h:126
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
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:723
int connection_write(struct connection *connection, const void *data, int len)
Definition: server.c:732
int server_preinit(void)
Definition: server.c:661
static int last_signal
Definition: server.c:49
static void sig_handler(int sig)
Definition: server.c:602
int connection_read(struct connection *connection, void *data, int len)
Definition: server.c:744
static void free_service(struct service *c)
Definition: server.c:191
int server_host_os_close(void)
Definition: server.c:653
static char * bindto_name
Definition: server.c:55
void server_free(void)
Definition: server.c:713
static const struct command_registration server_command_handlers[]
Definition: server.c:804
int server_host_os_entry(void)
Definition: server.c:633
COMMAND_HANDLER(handle_shutdown_command)
Definition: server.c:758
static void remove_connections(struct service *service)
Definition: server.c:340
static enum shutdown_reason shutdown_openocd
Definition: server.c:46
static int remove_services(void)
Definition: server.c:384
static int remove_connection(struct service *service, struct connection *connection)
Definition: server.c:156
void server_keep_clients_alive(void)
Definition: server.c:414
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:752
int server_loop(struct command_context *command_context)
Definition: server.c:422
int remove_service(const char *name, const char *port)
Definition: server.c:355
int server_quit(void)
Definition: server.c:698
static int add_connection(struct service *service, struct command_context *cmd_ctx)
Definition: server.c:57
int server_register_commands(struct command_context *cmd_ctx)
Definition: server.c:830
static struct service * services
Definition: server.c:38
COMMAND_HELPER(server_port_command, unsigned short *out)
Definition: server.c:847
int add_service(const struct service_driver *driver, const char *port, int max_connections, void *priv)
Definition: server.c:198
int server_init(struct command_context *cmd_ctx)
Definition: server.c:681
static void sigkey_handler(int sig)
Definition: server.c:621
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:235
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
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:62
void(* keep_client_alive_handler)(struct connection *connection)
called periodically to send keep-alive messages on the connection
Definition: server.h:64
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:67
struct service * next
Definition: server.h:82
int fd
Definition: server.h:72
void * priv
Definition: server.h:81
int max_connections
Definition: server.h:74
int(* new_connection_during_keep_alive)(struct connection *connection)
Definition: server.h:76
struct sockaddr_in sin
Definition: server.h:73
struct connection * connections
Definition: server.h:75
int(* input)(struct connection *connection)
Definition: server.h:78
char * name
Definition: server.h:68
char * port
Definition: server.h:70
int(* connection_closed)(struct connection *connection)
Definition: server.h:79
unsigned short portnumber
Definition: server.h:71
void(* keep_client_alive)(struct connection *connection)
Definition: server.h:80
enum connection_type type
Definition: server.h:69
int(* new_connection)(struct connection *connection)
Definition: server.h:77
Definition: ftdi.c:95
long tv_sec
Definition: replacements.h:46
long tv_usec
Definition: replacements.h:47
int target_call_timer_callbacks(void)
Definition: target.c:1878
int64_t target_timer_next_event(void)
Returns when the next registered event will take place.
Definition: target.c:1889
void target_quit(void)
Free all the resources allocated by targets and the target layer.
Definition: target.c:2230
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:353
void tcl_service_free(void)
Definition: tcl_server.c:359
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