You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

614 lines
14 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 √ėyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2008 by Spencer Oliver *
  9. * spen@spen-soft.co.uk *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "server.h"
  30. #include <target/target.h>
  31. #include "openocd.h"
  32. #include "tcl_server.h"
  33. #include "telnet_server.h"
  34. #include <signal.h>
  35. #ifndef _WIN32
  36. #include <netinet/tcp.h>
  37. #endif
  38. struct service *services = NULL;
  39. /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
  40. static int shutdown_openocd = 0;
  41. /* set when using pipes rather than tcp */
  42. int server_use_pipes = 0;
  43. int add_connection(struct service *service, struct command_context *cmd_ctx)
  44. {
  45. socklen_t address_size;
  46. struct connection *c, **p;
  47. int retval;
  48. int flag = 1;
  49. c = malloc(sizeof(struct connection));
  50. c->fd = -1;
  51. memset(&c->sin, 0, sizeof(c->sin));
  52. c->cmd_ctx = copy_command_context(cmd_ctx);
  53. c->service = service;
  54. c->input_pending = 0;
  55. c->priv = NULL;
  56. c->next = NULL;
  57. if (service->type == CONNECTION_TCP)
  58. {
  59. address_size = sizeof(c->sin);
  60. c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
  61. /* This increases performance dramatically for e.g. GDB load which
  62. * does not have a sliding window protocol. */
  63. retval = setsockopt(c->fd, /* socket affected */
  64. IPPROTO_TCP, /* set option at TCP level */
  65. TCP_NODELAY, /* name of option */
  66. (char *)&flag, /* the cast is historical cruft */
  67. sizeof(int)); /* length of option value */
  68. LOG_INFO("accepting '%s' connection from %i", service->name, c->sin.sin_port);
  69. if ((retval = service->new_connection(c)) != ERROR_OK)
  70. {
  71. close_socket(c->fd);
  72. LOG_ERROR("attempted '%s' connection rejected", service->name);
  73. free(c);
  74. return retval;
  75. }
  76. }
  77. else if (service->type == CONNECTION_PIPE)
  78. {
  79. c->fd = service->fd;
  80. /* do not check for new connections again on stdin */
  81. service->fd = -1;
  82. LOG_INFO("accepting '%s' connection from pipe", service->name);
  83. if ((retval = service->new_connection(c)) != ERROR_OK)
  84. {
  85. LOG_ERROR("attempted '%s' connection rejected", service->name);
  86. free(c);
  87. return retval;
  88. }
  89. }
  90. /* add to the end of linked list */
  91. for (p = &service->connections; *p; p = &(*p)->next);
  92. *p = c;
  93. service->max_connections--;
  94. return ERROR_OK;
  95. }
  96. int remove_connection(struct service *service, struct connection *connection)
  97. {
  98. struct connection **p = &service->connections;
  99. struct connection *c;
  100. /* find connection */
  101. while ((c = *p))
  102. {
  103. if (c->fd == connection->fd)
  104. {
  105. service->connection_closed(c);
  106. if (service->type == CONNECTION_TCP)
  107. close_socket(c->fd);
  108. command_done(c->cmd_ctx);
  109. /* delete connection */
  110. *p = c->next;
  111. free(c);
  112. service->max_connections++;
  113. break;
  114. }
  115. /* redirect p to next list pointer */
  116. p = &(*p)->next;
  117. }
  118. return ERROR_OK;
  119. }
  120. int add_service(char *name, enum connection_type type, unsigned short port, int max_connections, new_connection_handler_t new_connection_handler, input_handler_t input_handler, connection_closed_handler_t connection_closed_handler, void *priv)
  121. {
  122. struct service *c, **p;
  123. int so_reuseaddr_option = 1;
  124. c = malloc(sizeof(struct service));
  125. c->name = strdup(name);
  126. c->type = type;
  127. c->port = port;
  128. c->max_connections = max_connections;
  129. c->fd = -1;
  130. c->connections = NULL;
  131. c->new_connection = new_connection_handler;
  132. c->input = input_handler;
  133. c->connection_closed = connection_closed_handler;
  134. c->priv = priv;
  135. c->next = NULL;
  136. if (type == CONNECTION_TCP)
  137. {
  138. if ((c->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  139. {
  140. LOG_ERROR("error creating socket: %s", strerror(errno));
  141. exit(-1);
  142. }
  143. setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
  144. socket_nonblock(c->fd);
  145. memset(&c->sin, 0, sizeof(c->sin));
  146. c->sin.sin_family = AF_INET;
  147. c->sin.sin_addr.s_addr = INADDR_ANY;
  148. c->sin.sin_port = htons(port);
  149. if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1)
  150. {
  151. LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
  152. exit(-1);
  153. }
  154. #ifndef _WIN32
  155. int segsize = 65536;
  156. setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
  157. #endif
  158. int window_size = 128 * 1024;
  159. /* These setsockopt()s must happen before the listen() */
  160. setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
  161. (char *)&window_size, sizeof(window_size));
  162. setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
  163. (char *)&window_size, sizeof(window_size));
  164. if (listen(c->fd, 1) == -1)
  165. {
  166. LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
  167. exit(-1);
  168. }
  169. }
  170. else if (type == CONNECTION_PIPE)
  171. {
  172. /* use stdin */
  173. c->fd = STDIN_FILENO;
  174. #ifdef _WIN32
  175. /* for win32 set stdin/stdout to binary mode */
  176. if (_setmode(_fileno(stdout), _O_BINARY) < 0)
  177. LOG_WARNING("cannot change stdout mode to binary");
  178. if (_setmode(_fileno(stdin), _O_BINARY) < 0)
  179. LOG_WARNING("cannot change stdin mode to binary");
  180. if (_setmode(_fileno(stderr), _O_BINARY) < 0)
  181. LOG_WARNING("cannot change stderr mode to binary");
  182. #else
  183. socket_nonblock(c->fd);
  184. #endif
  185. }
  186. else
  187. {
  188. LOG_ERROR("unknown connection type: %d", type);
  189. exit(1);
  190. }
  191. /* add to the end of linked list */
  192. for (p = &services; *p; p = &(*p)->next);
  193. *p = c;
  194. return ERROR_OK;
  195. }
  196. int remove_service(unsigned short port)
  197. {
  198. struct service **p = &services;
  199. struct service *c;
  200. /* find service */
  201. while ((c = *p))
  202. {
  203. if (c->port == port)
  204. {
  205. if (c->name)
  206. free(c->name);
  207. if (c->priv)
  208. free(c->priv);
  209. /* delete service */
  210. *p = c->next;
  211. free(c);
  212. }
  213. /* redirect p to next list pointer */
  214. p = &(*p)->next;
  215. }
  216. return ERROR_OK;
  217. }
  218. int remove_services(void)
  219. {
  220. struct service *c = services;
  221. /* loop service */
  222. while (c)
  223. {
  224. struct service *next = c->next;
  225. if (c->name)
  226. free(c->name);
  227. if (c->priv)
  228. free(c->priv);
  229. /* delete service */
  230. free(c);
  231. /* remember the last service for unlinking */
  232. c = next;
  233. }
  234. services = NULL;
  235. return ERROR_OK;
  236. }
  237. int server_loop(struct command_context *command_context)
  238. {
  239. struct service *service;
  240. bool poll = true;
  241. /* used in select() */
  242. fd_set read_fds;
  243. int fd_max;
  244. /* used in accept() */
  245. int retval;
  246. #ifndef _WIN32
  247. if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
  248. LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
  249. #endif
  250. while (!shutdown_openocd)
  251. {
  252. /* monitor sockets for activity */
  253. fd_max = 0;
  254. FD_ZERO(&read_fds);
  255. /* add service and connection fds to read_fds */
  256. for (service = services; service; service = service->next)
  257. {
  258. if (service->fd != -1)
  259. {
  260. /* listen for new connections */
  261. FD_SET(service->fd, &read_fds);
  262. if (service->fd > fd_max)
  263. fd_max = service->fd;
  264. }
  265. if (service->connections)
  266. {
  267. struct connection *c;
  268. for (c = service->connections; c; c = c->next)
  269. {
  270. /* check for activity on the connection */
  271. FD_SET(c->fd, &read_fds);
  272. if (c->fd > fd_max)
  273. fd_max = c->fd;
  274. }
  275. }
  276. }
  277. #ifndef _WIN32
  278. #if BUILD_ECOSBOARD == 0
  279. if (server_use_pipes == 0)
  280. {
  281. /* add STDIN to read_fds */
  282. FD_SET(fileno(stdin), &read_fds);
  283. }
  284. #endif
  285. #endif
  286. struct timeval tv;
  287. tv.tv_sec = 0;
  288. if (poll)
  289. {
  290. /* we're just polling this iteration, this is faster on embedded
  291. * hosts */
  292. tv.tv_usec = 0;
  293. retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
  294. } else
  295. {
  296. /* Every 100ms */
  297. tv.tv_usec = 100000;
  298. /* Only while we're sleeping we'll let others run */
  299. openocd_sleep_prelude();
  300. kept_alive();
  301. retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
  302. openocd_sleep_postlude();
  303. }
  304. if (retval == -1)
  305. {
  306. #ifdef _WIN32
  307. errno = WSAGetLastError();
  308. if (errno == WSAEINTR)
  309. FD_ZERO(&read_fds);
  310. else
  311. {
  312. LOG_ERROR("error during select: %s", strerror(errno));
  313. exit(-1);
  314. }
  315. #else
  316. if (errno == EINTR)
  317. {
  318. FD_ZERO(&read_fds);
  319. }
  320. else
  321. {
  322. LOG_ERROR("error during select: %s", strerror(errno));
  323. exit(-1);
  324. }
  325. #endif
  326. }
  327. if (retval == 0)
  328. {
  329. /* We only execute these callbacks when there was nothing to do or we timed out */
  330. target_call_timer_callbacks();
  331. process_jim_events(command_context);
  332. FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
  333. /* We timed out/there was nothing to do, timeout rather than poll next time */
  334. poll = false;
  335. } else
  336. {
  337. /* There was something to do, next time we'll just poll */
  338. poll = true;
  339. }
  340. for (service = services; service; service = service->next)
  341. {
  342. /* handle new connections on listeners */
  343. if ((service->fd != -1)
  344. && (FD_ISSET(service->fd, &read_fds)))
  345. {
  346. if (service->max_connections > 0)
  347. {
  348. add_connection(service, command_context);
  349. }
  350. else
  351. {
  352. if (service->type != CONNECTION_PIPE)
  353. {
  354. struct sockaddr_in sin;
  355. socklen_t address_size = sizeof(sin);
  356. int tmp_fd;
  357. tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
  358. close_socket(tmp_fd);
  359. }
  360. LOG_INFO("rejected '%s' connection, no more connections allowed", service->name);
  361. }
  362. }
  363. /* handle activity on connections */
  364. if (service->connections)
  365. {
  366. struct connection *c;
  367. for (c = service->connections; c;)
  368. {
  369. if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending)
  370. {
  371. if ((retval = service->input(c)) != ERROR_OK)
  372. {
  373. struct connection *next = c->next;
  374. if (service->type == CONNECTION_PIPE)
  375. {
  376. /* if connection uses a pipe then shutdown openocd on error */
  377. shutdown_openocd = 1;
  378. }
  379. remove_connection(service, c);
  380. LOG_INFO("dropped '%s' connection - error %d", service->name, retval);
  381. c = next;
  382. continue;
  383. }
  384. }
  385. c = c->next;
  386. }
  387. }
  388. }
  389. #ifndef _WIN32
  390. #if BUILD_ECOSBOARD == 0
  391. /* check for data on stdin if not using pipes */
  392. if (server_use_pipes == 0)
  393. {
  394. if (FD_ISSET(fileno(stdin), &read_fds))
  395. {
  396. if (getc(stdin) == 'x')
  397. {
  398. shutdown_openocd = 1;
  399. }
  400. }
  401. }
  402. #endif
  403. #else
  404. MSG msg;
  405. while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  406. {
  407. if (msg.message == WM_QUIT)
  408. shutdown_openocd = 1;
  409. }
  410. #endif
  411. }
  412. return ERROR_OK;
  413. }
  414. #ifdef _WIN32
  415. BOOL WINAPI ControlHandler(DWORD dwCtrlType)
  416. {
  417. shutdown_openocd = 1;
  418. return TRUE;
  419. }
  420. void sig_handler(int sig) {
  421. shutdown_openocd = 1;
  422. }
  423. #endif
  424. int server_preinit(void)
  425. {
  426. /* this currently only calls WSAStartup on native win32 systems
  427. * before any socket operations are performed.
  428. * This is an issue if you call init in your config script */
  429. #ifdef _WIN32
  430. WORD wVersionRequested;
  431. WSADATA wsaData;
  432. wVersionRequested = MAKEWORD(2, 2);
  433. if (WSAStartup(wVersionRequested, &wsaData) != 0)
  434. {
  435. LOG_ERROR("Failed to Open Winsock");
  436. exit(-1);
  437. }
  438. if (server_use_pipes == 0)
  439. {
  440. /* register ctrl-c handler */
  441. SetConsoleCtrlHandler(ControlHandler, TRUE);
  442. }
  443. else
  444. {
  445. /* we are using pipes so ignore ctrl-c */
  446. SetConsoleCtrlHandler(NULL, TRUE);
  447. }
  448. signal(SIGINT, sig_handler);
  449. signal(SIGTERM, sig_handler);
  450. signal(SIGBREAK, sig_handler);
  451. signal(SIGABRT, sig_handler);
  452. #endif
  453. return ERROR_OK;
  454. }
  455. int server_init(struct command_context *cmd_ctx)
  456. {
  457. int ret = tcl_init(cmd_ctx);
  458. if (ERROR_OK != ret)
  459. return ret;
  460. return telnet_init("Open On-Chip Debugger");
  461. }
  462. int server_quit(void)
  463. {
  464. remove_services();
  465. #ifdef _WIN32
  466. WSACleanup();
  467. SetConsoleCtrlHandler(ControlHandler, FALSE);
  468. #endif
  469. return ERROR_OK;
  470. }
  471. /* tell the server we want to shut down */
  472. COMMAND_HANDLER(handle_shutdown_command)
  473. {
  474. LOG_USER("shutdown command invoked");
  475. shutdown_openocd = 1;
  476. return ERROR_OK;
  477. }
  478. static const struct command_registration server_command_handlers[] = {
  479. {
  480. .name = "shutdown",
  481. .handler = &handle_shutdown_command,
  482. .mode = COMMAND_ANY,
  483. .help = "shut the server down",
  484. },
  485. COMMAND_REGISTRATION_DONE
  486. };
  487. int server_register_commands(struct command_context *cmd_ctx)
  488. {
  489. int retval = telnet_register_commands(cmd_ctx);
  490. if (ERROR_OK != retval)
  491. return retval;
  492. retval = tcl_register_commands(cmd_ctx);
  493. if (ERROR_OK != retval)
  494. return retval;
  495. return register_commands(cmd_ctx, NULL, server_command_handlers);
  496. }
  497. SERVER_PORT_COMMAND()
  498. {
  499. switch (CMD_ARGC) {
  500. case 0:
  501. command_print(CMD_CTX, "%d", *out);
  502. break;
  503. case 1:
  504. {
  505. uint16_t port;
  506. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
  507. *out = port;
  508. break;
  509. }
  510. default:
  511. return ERROR_INVALID_ARGUMENTS;
  512. }
  513. return ERROR_OK;
  514. }