- Replace 'while(' with 'while ('.
[openocd.git] / src / server / server.c
blobbca9774f56176fe0352d43164c8028558cd7e6f9
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
30 #include "server.h"
31 #include "target.h"
33 #include <signal.h>
35 #ifndef _WIN32
36 #include <netinet/tcp.h>
37 #endif
40 service_t *services = NULL;
42 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
43 static int shutdown_openocd = 0;
44 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
46 /* set when using pipes rather than tcp */
47 int server_use_pipes = 0;
49 int add_connection(service_t *service, command_context_t *cmd_ctx)
51 socklen_t address_size;
52 connection_t *c, **p;
53 int retval;
54 int flag=1;
56 c = malloc(sizeof(connection_t));
57 c->fd = -1;
58 memset(&c->sin, 0, sizeof(c->sin));
59 c->cmd_ctx = copy_command_context(cmd_ctx);
60 c->service = service;
61 c->input_pending = 0;
62 c->priv = NULL;
63 c->next = NULL;
65 if (service->type == CONNECTION_TCP)
67 address_size = sizeof(c->sin);
69 c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
71 /* This increases performance dramatically for e.g. GDB load which
72 * does not have a sliding window protocol. */
73 retval=setsockopt(c->fd, /* socket affected */
74 IPPROTO_TCP, /* set option at TCP level */
75 TCP_NODELAY, /* name of option */
76 (char *)&flag, /* the cast is historical cruft */
77 sizeof(int)); /* length of option value */
79 LOG_INFO("accepting '%s' connection from %i", service->name, c->sin.sin_port);
80 if ((retval = service->new_connection(c)) != ERROR_OK)
82 close_socket(c->fd);
83 LOG_ERROR("attempted '%s' connection rejected", service->name);
84 free(c);
85 return retval;
88 else if (service->type == CONNECTION_PIPE)
90 c->fd = service->fd;
92 /* do not check for new connections again on stdin */
93 service->fd = -1;
95 LOG_INFO("accepting '%s' connection from pipe", service->name);
96 if ((retval = service->new_connection(c)) != ERROR_OK)
98 LOG_ERROR("attempted '%s' connection rejected", service->name);
99 free(c);
100 return retval;
104 /* add to the end of linked list */
105 for (p = &service->connections; *p; p = &(*p)->next);
106 *p = c;
108 service->max_connections--;
110 return ERROR_OK;
113 int remove_connection(service_t *service, connection_t *connection)
115 connection_t **p = &service->connections;
116 connection_t *c;
118 /* find connection */
119 while ((c = *p))
121 if (c->fd == connection->fd)
123 service->connection_closed(c);
124 if (service->type == CONNECTION_TCP)
125 close_socket(c->fd);
126 command_done(c->cmd_ctx);
128 /* delete connection */
129 *p = c->next;
130 free(c);
132 service->max_connections++;
133 break;
136 /* redirect p to next list pointer */
137 p = &(*p)->next;
140 return ERROR_OK;
143 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)
145 service_t *c, **p;
146 int so_reuseaddr_option = 1;
148 c = malloc(sizeof(service_t));
150 c->name = strdup(name);
151 c->type = type;
152 c->port = port;
153 c->max_connections = max_connections;
154 c->fd = -1;
155 c->connections = NULL;
156 c->new_connection = new_connection_handler;
157 c->input = input_handler;
158 c->connection_closed = connection_closed_handler;
159 c->priv = priv;
160 c->next = NULL;
162 if (type == CONNECTION_TCP)
164 if ((c->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
166 LOG_ERROR("error creating socket: %s", strerror(errno));
167 exit(-1);
170 setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
172 socket_nonblock(c->fd);
174 memset(&c->sin, 0, sizeof(c->sin));
175 c->sin.sin_family = AF_INET;
176 c->sin.sin_addr.s_addr = INADDR_ANY;
177 c->sin.sin_port = htons(port);
179 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1)
181 LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
182 exit(-1);
185 #ifndef _WIN32
186 int segsize=65536;
187 setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
188 #endif
189 int window_size = 128 * 1024;
191 /* These setsockopt()s must happen before the listen() */
193 setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
194 (char *)&window_size, sizeof(window_size));
195 setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
196 (char *)&window_size, sizeof(window_size));
198 if (listen(c->fd, 1) == -1)
200 LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
201 exit(-1);
204 else if (type == CONNECTION_PIPE)
206 /* use stdin */
207 c->fd = STDIN_FILENO;
209 #ifdef _WIN32
210 /* for win32 set stdin/stdout to binary mode */
211 if (_setmode(_fileno(stdout), _O_BINARY) < 0)
212 LOG_WARNING("cannot change stdout mode to binary");
213 if (_setmode(_fileno(stdin), _O_BINARY) < 0)
214 LOG_WARNING("cannot change stdin mode to binary");
215 if (_setmode(_fileno(stderr), _O_BINARY) < 0)
216 LOG_WARNING("cannot change stderr mode to binary");
217 #else
218 socket_nonblock(c->fd);
219 #endif
221 else
223 LOG_ERROR("unknown connection type: %d", type);
224 exit(1);
227 /* add to the end of linked list */
228 for (p = &services; *p; p = &(*p)->next);
229 *p = c;
231 return ERROR_OK;
234 int remove_service(unsigned short port)
236 service_t **p = &services;
237 service_t *c;
239 /* find service */
240 while ((c = *p))
242 if (c->port == port)
244 if (c->name)
245 free(c->name);
247 if (c->priv)
248 free(c->priv);
250 /* delete service */
251 *p = c->next;
252 free(c);
255 /* redirect p to next list pointer */
256 p = &(*p)->next;
259 return ERROR_OK;
262 int remove_services(void)
264 service_t *c = services;
266 /* loop service */
267 while (c)
269 service_t *next = c->next;
271 if (c->name)
272 free(c->name);
274 if (c->priv)
275 free(c->priv);
277 /* delete service */
278 free(c);
280 /* remember the last service for unlinking */
281 c = next;
284 services = NULL;
286 return ERROR_OK;
289 extern void openocd_sleep_prelude(void);
290 extern void openocd_sleep_postlude(void);
292 int server_loop(command_context_t *command_context)
294 service_t *service;
296 /* used in select() */
297 fd_set read_fds;
298 struct timeval tv;
299 int fd_max;
301 /* used in accept() */
302 int retval;
304 #ifndef _WIN32
305 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
306 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
307 #endif
309 /* do regular tasks after at most 10ms */
310 tv.tv_sec = 0;
311 tv.tv_usec = 10000;
313 while (!shutdown_openocd)
315 /* monitor sockets for acitvity */
316 fd_max = 0;
317 FD_ZERO(&read_fds);
319 /* add service and connection fds to read_fds */
320 for (service = services; service; service = service->next)
322 if (service->fd != -1)
324 /* listen for new connections */
325 FD_SET(service->fd, &read_fds);
327 if (service->fd > fd_max)
328 fd_max = service->fd;
331 if (service->connections)
333 connection_t *c;
335 for (c = service->connections; c; c = c->next)
337 /* check for activity on the connection */
338 FD_SET(c->fd, &read_fds);
339 if (c->fd > fd_max)
340 fd_max = c->fd;
345 #ifndef _WIN32
346 #if BUILD_ECOSBOARD == 0
347 if (server_use_pipes == 0)
349 /* add STDIN to read_fds */
350 FD_SET(fileno(stdin), &read_fds);
352 #endif
353 #endif
355 openocd_sleep_prelude();
356 kept_alive();
358 /* Only while we're sleeping we'll let others run */
359 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
360 openocd_sleep_postlude();
362 if (retval == -1)
364 #ifdef _WIN32
366 errno = WSAGetLastError();
368 if (errno == WSAEINTR)
369 FD_ZERO(&read_fds);
370 else
372 LOG_ERROR("error during select: %s", strerror(errno));
373 exit(-1);
375 #else
377 if (errno == EINTR)
379 FD_ZERO(&read_fds);
381 else
383 LOG_ERROR("error during select: %s", strerror(errno));
384 exit(-1);
386 #endif
389 target_call_timer_callbacks();
390 process_jim_events ();
392 if (retval == 0)
394 /* do regular tasks after at most 100ms */
395 tv.tv_sec = 0;
396 tv.tv_usec = 10000;
397 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
400 for (service = services; service; service = service->next)
402 /* handle new connections on listeners */
403 if ((service->fd != -1)
404 && (FD_ISSET(service->fd, &read_fds)))
406 if (service->max_connections > 0)
408 add_connection(service, command_context);
410 else
412 if (service->type != CONNECTION_PIPE)
414 struct sockaddr_in sin;
415 socklen_t address_size = sizeof(sin);
416 int tmp_fd;
417 tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
418 close_socket(tmp_fd);
420 LOG_INFO("rejected '%s' connection, no more connections allowed", service->name);
424 /* handle activity on connections */
425 if (service->connections)
427 connection_t *c;
429 for (c = service->connections; c;)
431 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending)
433 if ((retval = service->input(c)) != ERROR_OK)
435 connection_t *next = c->next;
436 if (service->type == CONNECTION_PIPE)
438 /* if connection uses a pipe then shutdown openocd on error */
439 shutdown_openocd = 1;
441 remove_connection(service, c);
442 LOG_INFO("dropped '%s' connection - error %d", service->name, retval);
443 c = next;
444 continue;
447 c = c->next;
452 #ifndef _WIN32
453 #if BUILD_ECOSBOARD == 0
454 /* check for data on stdin if not using pipes */
455 if (server_use_pipes == 0)
457 if (FD_ISSET(fileno(stdin), &read_fds))
459 if (getc(stdin) == 'x')
461 shutdown_openocd = 1;
465 #endif
466 #else
467 MSG msg;
468 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
470 if (msg.message == WM_QUIT)
471 shutdown_openocd = 1;
473 #endif
476 return ERROR_OK;
479 #ifdef _WIN32
480 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
482 shutdown_openocd = 1;
483 return TRUE;
486 void sig_handler(int sig) {
487 shutdown_openocd = 1;
489 #endif
491 int server_init(void)
493 #ifdef _WIN32
494 WORD wVersionRequested;
495 WSADATA wsaData;
497 wVersionRequested = MAKEWORD(2, 2);
499 if (WSAStartup(wVersionRequested, &wsaData) != 0)
501 LOG_ERROR("Failed to Open Winsock");
502 exit(-1);
505 if (server_use_pipes == 0)
507 /* register ctrl-c handler */
508 SetConsoleCtrlHandler(ControlHandler, TRUE);
510 else
512 /* we are using pipes so ignore ctrl-c */
513 SetConsoleCtrlHandler(NULL, TRUE);
516 signal(SIGINT, sig_handler);
517 signal(SIGTERM, sig_handler);
518 signal(SIGBREAK, sig_handler);
519 signal(SIGABRT, sig_handler);
520 #endif
522 return ERROR_OK;
525 int server_quit(void)
527 remove_services();
529 #ifdef _WIN32
530 WSACleanup();
531 SetConsoleCtrlHandler( ControlHandler, FALSE );
532 #endif
534 return ERROR_OK;
537 int server_register_commands(command_context_t *context)
539 register_command(context, NULL, "shutdown", handle_shutdown_command,
540 COMMAND_ANY, "shut the server down");
542 return ERROR_OK;
545 /* tell the server we want to shut down */
546 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
548 shutdown_openocd = 1;
550 return ERROR_COMMAND_CLOSE_CONNECTION;