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