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