]> git.sur5r.net Git - openocd/blob - src/server/server.c
committed bugfix from Michael Bruck
[openocd] / src / server / server.c
1 /***************************************************************************\r
2  *   Copyright (C) 2005 by Dominic Rath                                    *\r
3  *   Dominic.Rath@gmx.de                                                   *\r
4  *                                                                         *\r
5  *   This program is free software; you can redistribute it and/or modify  *\r
6  *   it under the terms of the GNU General Public License as published by  *\r
7  *   the Free Software Foundation; either version 2 of the License, or     *\r
8  *   (at your option) any later version.                                   *\r
9  *                                                                         *\r
10  *   This program is distributed in the hope that it will be useful,       *\r
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *\r
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *\r
13  *   GNU General Public License for more details.                          *\r
14  *                                                                         *\r
15  *   You should have received a copy of the GNU General Public License     *\r
16  *   along with this program; if not, write to the                         *\r
17  *   Free Software Foundation, Inc.,                                       *\r
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *\r
19  ***************************************************************************/\r
20 #ifdef HAVE_CONFIG_H\r
21 #include "config.h"\r
22 #endif\r
23 \r
24 #include "replacements.h"\r
25 \r
26 #include "server.h"\r
27 \r
28 #include "log.h"\r
29 #include "telnet_server.h"\r
30 #include "target.h"\r
31 \r
32 #include <command.h>\r
33 #include <string.h>\r
34 #include <stdlib.h>\r
35 #include <errno.h>\r
36 #include <unistd.h>\r
37 #include <sys/types.h>\r
38 #include <fcntl.h>\r
39 #include <signal.h>\r
40 \r
41 service_t *services = NULL;\r
42 \r
43 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */\r
44 static int shutdown_openocd = 0;\r
45 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);\r
46 \r
47 int add_connection(service_t *service, command_context_t *cmd_ctx)\r
48 {\r
49         unsigned int address_size;\r
50         connection_t *c, **p;\r
51         int retval;\r
52         \r
53         c = malloc(sizeof(connection_t));\r
54         c->fd = -1;\r
55         memset(&c->sin, 0, sizeof(c->sin));\r
56         c->cmd_ctx = copy_command_context(cmd_ctx);\r
57         c->service = service;\r
58         c->input_pending = 0;\r
59         c->priv = NULL;\r
60         c->next = NULL;\r
61 \r
62         address_size = sizeof(c->sin);\r
63         c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);\r
64                                 \r
65         if ((retval = service->new_connection(c)) == ERROR_OK)\r
66         {\r
67                 INFO("accepted '%s' connection from %i", service->name, c->sin.sin_port);\r
68         }\r
69         else\r
70         {\r
71                 close_socket(c->fd);\r
72                 INFO("attempted '%s' connection rejected", service->name);\r
73                 free(c);\r
74         }\r
75         \r
76         /* add to the end of linked list */\r
77         for (p = &service->connections; *p; p = &(*p)->next);\r
78         *p = c;\r
79         \r
80         service->max_connections--;\r
81         \r
82         return ERROR_OK;\r
83 }\r
84 \r
85 int remove_connection(service_t *service, connection_t *connection)\r
86 {\r
87         connection_t **p = &service->connections;\r
88         connection_t *c;\r
89         \r
90         /* find connection */\r
91         while(c = *p)\r
92         {               \r
93                 if (c->fd == connection->fd)\r
94                 {       \r
95                         service->connection_closed(c);\r
96                         close_socket(c->fd);\r
97                         command_done(c->cmd_ctx);\r
98                         \r
99                         /* delete connection */\r
100                         *p = c->next;\r
101                         free(c);\r
102                         \r
103                         service->max_connections++;\r
104                         break;\r
105                 }\r
106                 \r
107                 /* redirect p to next list pointer */\r
108                 p = &(*p)->next;                \r
109         }\r
110         \r
111         return ERROR_OK;\r
112 }\r
113 \r
114 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)\r
115 {\r
116         service_t *c, **p;\r
117         int so_reuseaddr_option = 1;\r
118         \r
119         c = malloc(sizeof(service_t));\r
120         \r
121         c->name = strdup(name);\r
122         c->type = type;\r
123         c->port = port;\r
124         c->max_connections = max_connections;\r
125         c->fd = -1;\r
126         c->connections = NULL;\r
127         c->new_connection = new_connection_handler;\r
128         c->input = input_handler;\r
129         c->connection_closed = connection_closed_handler;\r
130         c->priv = priv;\r
131         c->next = NULL;\r
132         \r
133         if ((c->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)\r
134         {\r
135                 ERROR("error creating socket: %s", strerror(errno));\r
136                 exit(-1);\r
137         }\r
138         \r
139         setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));\r
140         \r
141         socket_nonblock(c->fd);\r
142         \r
143         memset(&c->sin, 0, sizeof(c->sin));\r
144         c->sin.sin_family = AF_INET;\r
145         c->sin.sin_addr.s_addr = INADDR_ANY;\r
146         c->sin.sin_port = htons(port);\r
147         \r
148         if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1)\r
149         {\r
150                 ERROR("couldn't bind to socket: %s", strerror(errno));\r
151                 exit(-1);\r
152         }\r
153         \r
154         if (listen(c->fd, 1) == -1)\r
155         {\r
156                 ERROR("couldn't listen on socket: %s", strerror(errno));\r
157                 exit(-1);\r
158         }\r
159         \r
160         /* add to the end of linked list */\r
161         for (p = &services; *p; p = &(*p)->next);\r
162         *p = c;\r
163         \r
164         return ERROR_OK;\r
165 }\r
166 \r
167 int remove_service(unsigned short port)\r
168 {\r
169         service_t **p = &services;\r
170         service_t *c;\r
171         \r
172         /* find service */\r
173         while(c = *p)\r
174         {               \r
175                 if (c->port == port)\r
176                 {       \r
177                         if (c->name)\r
178                                 free(c->name);\r
179                         \r
180                         if (c->priv)\r
181                                 free(c->priv);\r
182                         \r
183                         /* delete service */\r
184                         *p = c->next;\r
185                         free(c);\r
186                 }\r
187 \r
188                 /* redirect p to next list pointer */\r
189                 p = &(*p)->next;\r
190         }\r
191         \r
192         return ERROR_OK;\r
193 }\r
194 \r
195 int remove_services()\r
196 {\r
197         service_t *c = services;\r
198 \r
199         /* loop service */\r
200         while(c)\r
201         {\r
202                 service_t *next = c->next;\r
203 \r
204                 if (c->name)\r
205                         free(c->name);\r
206 \r
207                 if (c->priv)\r
208                         free(c->priv);\r
209 \r
210                 /* delete service */\r
211                 free(c);\r
212 \r
213                 /* remember the last service for unlinking */\r
214                 c = next;\r
215         }\r
216 \r
217         services = NULL;\r
218         \r
219         return ERROR_OK;\r
220 }\r
221 \r
222 int server_loop(command_context_t *command_context)\r
223 {\r
224         service_t *service;\r
225 \r
226         /* used in select() */\r
227         fd_set read_fds;\r
228         struct timeval tv;\r
229         int fd_max;\r
230         \r
231         /* used in accept() */\r
232         int retval;\r
233         \r
234 #ifndef _WIN32\r
235         if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)\r
236                 ERROR("couldn't set SIGPIPE to SIG_IGN");\r
237 #endif\r
238         \r
239         /* do regular tasks after at most 10ms */\r
240         tv.tv_sec = 0;\r
241         tv.tv_usec = 10000;\r
242         \r
243         while(!shutdown_openocd)\r
244         {\r
245                 /* monitor sockets for acitvity */\r
246                 fd_max = 0;\r
247                 FD_ZERO(&read_fds);\r
248 \r
249                 /* add service and connection fds to read_fds */\r
250                 for (service = services; service; service = service->next)\r
251                 {\r
252                         if (service->fd != -1)\r
253                         {\r
254                                 /* listen for new connections */\r
255                                 FD_SET(service->fd, &read_fds);\r
256 \r
257                                 if (service->fd > fd_max)\r
258                                         fd_max = service->fd;\r
259                         }\r
260                         \r
261                         if (service->connections)\r
262                         {\r
263                                 connection_t *c;\r
264                                 \r
265                                 for (c = service->connections; c; c = c->next)\r
266                                 {\r
267                                         /* check for activity on the connection */\r
268                                         FD_SET(c->fd, &read_fds);\r
269                                         if (c->fd > fd_max)\r
270                                                 fd_max = c->fd;\r
271                                 }\r
272                         }\r
273                 }\r
274                 \r
275 #ifndef _WIN32\r
276                 /* add STDIN to read_fds */\r
277                 FD_SET(fileno(stdin), &read_fds);\r
278 #endif\r
279 \r
280                 retval = select(fd_max + 1, &read_fds, NULL, NULL, &tv);\r
281                 \r
282                 if (retval == -1)\r
283                 {\r
284 #ifdef _WIN32\r
285 \r
286                         errno = WSAGetLastError();\r
287 \r
288                         if (errno == WSAEINTR)\r
289                                 FD_ZERO(&read_fds);\r
290                         else\r
291                         {\r
292                                 ERROR("error during select: %s", strerror(errno));\r
293                                 exit(-1);\r
294                         }\r
295 #else\r
296 \r
297                         if (errno == EINTR)\r
298                         {\r
299                                 FD_ZERO(&read_fds);\r
300                         }\r
301                         else\r
302                         {\r
303                                 ERROR("error during select: %s", strerror(errno));\r
304                                 exit(-1);\r
305                         }\r
306 #endif\r
307                 }\r
308                 \r
309                 target_call_timer_callbacks();\r
310 \r
311                 if (retval == 0)\r
312                 {\r
313                         /* do regular tasks after at most 100ms */\r
314                         tv.tv_sec = 0;\r
315                         tv.tv_usec = 10000;\r
316                         FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case!  */\r
317                 }\r
318                 \r
319                 for (service = services; service; service = service->next)\r
320                 {\r
321                         /* handle new connections on listeners */\r
322                         if ((service->fd != -1) \r
323                                 && (FD_ISSET(service->fd, &read_fds))) \r
324                         {\r
325                                 if (service->max_connections > 0)\r
326                                 {\r
327                                         add_connection(service, command_context);\r
328                                 }\r
329                                 else\r
330                                 {\r
331                                         struct sockaddr_in sin;\r
332                                         unsigned int address_size = sizeof(sin);\r
333                                         int tmp_fd;\r
334                                         tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);\r
335                                         close_socket(tmp_fd);\r
336                                         INFO("rejected '%s' connection, no more connections allowed", service->name);\r
337                                 }\r
338                         }\r
339                         \r
340                         /* handle activity on connections */\r
341                         if (service->connections)\r
342                         {\r
343                                 connection_t *c;\r
344                                 \r
345                                 for (c = service->connections; c;)\r
346                                 {\r
347                                         if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending)\r
348                                         {\r
349                                                 if (service->input(c) != ERROR_OK)\r
350                                                 {\r
351                                                         connection_t *next = c->next;\r
352                                                         remove_connection(service, c);\r
353                                                         INFO("dropped '%s' connection", service->name);\r
354                                                         c = next;\r
355                                                         continue;\r
356                                                 }\r
357                                         }\r
358                                         c = c->next;\r
359                                 }\r
360                         }\r
361                 }\r
362                 \r
363 #ifndef _WIN32\r
364                 if (FD_ISSET(fileno(stdin), &read_fds))\r
365                 {\r
366                         if (getc(stdin) == 'x')\r
367                         {\r
368                                 shutdown_openocd = 1;\r
369                         }\r
370                 }\r
371 #else\r
372                 MSG msg;\r
373                 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))\r
374                 {\r
375                         if (msg.message == WM_QUIT)\r
376                                 shutdown_openocd = 1;\r
377                 }\r
378 #endif\r
379         }\r
380         \r
381         return ERROR_OK;\r
382 }\r
383 \r
384 #ifdef _WIN32\r
385 BOOL WINAPI ControlHandler(DWORD dwCtrlType)\r
386 {\r
387     shutdown_openocd = 1;\r
388     return TRUE;\r
389 }\r
390 \r
391 void sig_handler(int sig) {\r
392     shutdown_openocd = 1;\r
393 }\r
394 #endif\r
395 \r
396 int server_init()\r
397 {\r
398 #ifdef _WIN32\r
399         WORD wVersionRequested;\r
400         WSADATA wsaData;\r
401 \r
402         wVersionRequested = MAKEWORD( 2, 2 );\r
403 \r
404         if (WSAStartup(wVersionRequested, &wsaData) != 0)\r
405         {\r
406                 ERROR("Failed to Open Winsock");\r
407                 exit(-1);\r
408         }\r
409 \r
410         SetConsoleCtrlHandler( ControlHandler, TRUE );\r
411 \r
412         signal(SIGINT, sig_handler);\r
413         signal(SIGTERM, sig_handler);\r
414         signal(SIGBREAK, sig_handler);\r
415         signal(SIGABRT, sig_handler);\r
416 #endif\r
417 \r
418         \r
419         return ERROR_OK;\r
420 }\r
421 \r
422 int server_quit()\r
423 {\r
424         remove_services();\r
425 \r
426 #ifdef _WIN32\r
427         WSACleanup();\r
428         SetConsoleCtrlHandler( ControlHandler, FALSE );\r
429 #endif\r
430 \r
431         return ERROR_OK;\r
432 }\r
433 \r
434 int server_register_commands(command_context_t *context)\r
435 {\r
436         register_command(context, NULL, "shutdown", handle_shutdown_command,\r
437                                          COMMAND_ANY, "shut the server down");\r
438         \r
439         return ERROR_OK;\r
440 }\r
441 \r
442 /* tell the server we want to shut down */\r
443 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)\r
444 {\r
445         shutdown_openocd = 1;\r
446 \r
447         return ERROR_COMMAND_CLOSE_CONNECTION;\r
448 }\r
449 \r
450 \r