1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
/*
(c) Copyright 2012-2013 DirectFB integrated media GmbH
(c) Copyright 2001-2013 The world wide DirectFB Open Source Community (directfb.org)
(c) Copyright 2000-2004 Convergence (integrated media) GmbH
All rights reserved.
Written by Denis Oliver Kropp <dok@directfb.org>,
Andreas Shimokawa <andi@directfb.org>,
Marek Pikarski <mass@directfb.org>,
Sven Neumann <neo@directfb.org>,
Ville Syrjälä <syrjala@sci.fi> and
Claudio Ciccani <klan@users.sf.net>.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <semaphore.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <direct/clock.h>
#include <direct/debug.h>
#include <direct/interface.h>
#include <direct/list.h>
#include <direct/mem.h>
#include <direct/messages.h>
#include <direct/thread.h>
#include <direct/util.h>
#include <voodoo/conf.h>
#include <voodoo/internal.h>
#include <voodoo/link.h>
#include <voodoo/manager.h>
#include <voodoo/server.h>
#ifndef htons
#if WORDS_BIGENDIAN
#define htons(x) (x)
#else
#define htons(x) ((((x) >> 8) & 0x00ff) | (((x) << 8) & 0xff00))
#endif
#endif
typedef struct {
sem_t sem;
pid_t single_pid;
} ServerShared;
typedef struct {
DirectLink link;
VoodooLink vl;
VoodooManager *manager;
} Connection;
typedef struct {
const char *name;
VoodooSuperConstruct func;
void *ctx;
IAny *interface;
} Super;
#define MAX_SUPER 8
struct __V_VoodooServer {
int fd;
bool fork;
bool quit;
DirectLink *connections;
int num_super;
Super supers[MAX_SUPER];
ServerShared *shared;
};
D_DEBUG_DOMAIN( Voodoo_Server, "Voodoo/Server", "Voodoo Server" );
/**********************************************************************************************************************/
static DirectResult accept_connection( VoodooServer *server, int fd );
/**********************************************************************************************************************/
static const int one = 1;
/**********************************************************************************************************************/
DirectResult
voodoo_server_create( const char *_addr,
int port,
bool fork,
VoodooServer **ret_server )
{
DirectResult ret;
struct sockaddr_in addr;
int fd = -1;
VoodooServer *server = NULL;
D_ASSERT( ret_server != NULL );
/* Create the server socket. */
fd = socket( PF_INET, SOCK_STREAM, 0 );
if (fd < 0) {
ret = errno2result( errno );
D_PERROR( "Voodoo/Server: Could not create the socket via socket()!\n" );
goto error;
}
/* Allow reuse of local address. */
if (setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one) ) < 0)
D_PERROR( "Voodoo/Server: Could not set SO_REUSEADDR!\n" );
/* Bind the socket to the local port. */
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr( _addr ?: "0.0.0.0" );
addr.sin_port = htons( port ?: 2323 );
if (bind( fd, (struct sockaddr*)&addr, sizeof(addr) )) {
ret = errno2result( errno );
D_PERROR( "Voodoo/Server: Could not bind() the socket!\n" );
goto error;
}
/* Start listening. */
if (listen( fd, 4 )) {
ret = errno2result( errno );
D_PERROR( "Voodoo/Server: Could not listen() to the socket!\n" );
goto error;
}
/* Allocate server structure. */
server = D_CALLOC( 1, sizeof(VoodooServer) );
if (!server) {
ret = D_OOM();
D_WARN( "out of memory" );
goto error;
}
/* Initialize server structure. */
server->fd = fd;
server->fork = fork;
{
int zfd;
zfd = open( "/dev/zero", O_RDWR );
if (zfd < 0) {
ret = errno2result( errno );
D_PERROR( "Voodoo/Server: Failed to open /dev/zero!\n" );
goto error;
}
server->shared = mmap( NULL, sizeof(ServerShared), PROT_READ | PROT_WRITE, MAP_SHARED, zfd, 0 );
close( zfd );
if (server->shared == MAP_FAILED) {
ret = errno2result( errno );
D_PERROR( "Voodoo/Server: Failed to mmap /dev/zero!\n" );
server->shared = NULL;
goto error;
}
if (sem_init( &server->shared->sem, 1, 1 )) {
ret = errno2result( errno );
D_PERROR( "Voodoo/Server: Failed to create process shared semaphore!\n" );
goto error;
}
}
/* Return the new server. */
*ret_server = server;
return DR_OK;
error:
if (server) {
if (server->shared)
munmap( server->shared, sizeof(ServerShared) );
D_FREE( server );
}
if (fd >= 0)
close( fd );
return ret;
}
DirectResult
voodoo_server_register( VoodooServer *server,
const char *name,
VoodooSuperConstruct func,
void *ctx )
{
Super *super;
D_ASSERT( server != NULL );
D_ASSERT( name != NULL );
D_ASSERT( func != NULL );
if (server->num_super == MAX_SUPER)
return DR_LIMITEXCEEDED;
super = &server->supers[server->num_super++];
super->name = name;
super->func = func;
super->ctx = ctx;
return DR_OK;
}
static inline Super *
lookup_super( VoodooServer *server,
const char *name )
{
int i;
D_ASSERT( server != NULL );
D_ASSERT( name != NULL );
for (i=0; i<server->num_super; i++) {
Super *super = &server->supers[i];
if (! strcmp( name, super->name ))
return super;
}
return NULL;
}
DirectResult
voodoo_server_construct( VoodooServer *server,
VoodooManager *manager,
const char *name,
VoodooInstanceID *ret_instance )
{
DirectResult ret;
Super *super;
VoodooInstanceID instance;
D_ASSERT( server != NULL );
D_ASSERT( manager != NULL );
D_ASSERT( name != NULL );
D_ASSERT( ret_instance != NULL );
super = lookup_super( server, name );
if (!super) {
D_ERROR( "Voodoo/Server: Super interface '%s' is not available!\n", name );
return DR_UNSUPPORTED;
}
if (voodoo_config->server_single && !strcmp( name, voodoo_config->server_single )) {
sem_wait( &server->shared->sem );
if (server->shared->single_pid) {
D_INFO( "Voodoo/Server: Killing previous single process with pid %d\n", server->shared->single_pid );
kill( server->shared->single_pid, SIGTERM );
}
server->shared->single_pid = getpid();
D_INFO( "Voodoo/Server: New single process has pid %d\n", server->shared->single_pid );
sem_post( &server->shared->sem );
}
ret = super->func( server, manager, name, super->ctx, &instance );
if (ret) {
D_ERROR( "Voodoo/Server: "
"Creating super interface '%s' failed (%s)!\n", name, DirectResultString(ret) );
return ret;
}
*ret_instance = instance;
return DR_OK;
}
DirectResult
voodoo_server_run( VoodooServer *server )
{
DirectLink *l, *n;
struct pollfd pf;
bool listener = true;
D_ASSERT( server != NULL );
while (!server->quit) {
/* Cleanup dead connections. */
direct_list_foreach_safe (l, n, server->connections) {
Connection *connection = (Connection*) l;
if (voodoo_manager_is_closed( connection->manager )) {
D_DEBUG_AT( Voodoo_Server, " -> closed manager\n" );
sem_wait( &server->shared->sem );
if (server->shared->single_pid == getpid()) {
D_INFO( "Voodoo/Server: Closing single process with pid %d\n", server->shared->single_pid );
server->shared->single_pid = 0;
}
sem_post( &server->shared->sem );
voodoo_manager_destroy( connection->manager );
//connection->vl.Close( &connection->vl );
direct_list_remove( &server->connections, l );
D_INFO( "Voodoo/Server: Closed connection.\n" );
D_FREE( connection );
//if (!server->connections)
// return DR_OK;
}
}
if (listener) {
int fd;
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
char buf[100];
pf.fd = server->fd;
pf.events = POLLIN;
switch (poll( &pf, 1, 100 )) {
default:
fd = accept( server->fd, (struct sockaddr*)&addr, &addrlen );
if (fd < 0) {
D_PERROR( "Voodoo/Server: Could not accept() incoming connection!\n" );
break;
}
inet_ntop( AF_INET, &addr.sin_addr, buf, sizeof(buf) );
D_INFO( "Voodoo/Server: Accepted connection from '%s'\n", buf );
if (server->fork) {
pid_t pid;
D_DEBUG_AT( Voodoo_Server, " -> forking...\n" );
pid = fork();
switch (pid) {
case 0:
D_DEBUG_AT( Voodoo_Server, " ===> In child process\n" );
listener = false;
close( server->fd );
accept_connection( server, fd );
break;
case -1:
D_PERROR( "Voodoo/Server: Could not fork()!\n" );
break;
default:
D_DEBUG_AT( Voodoo_Server, " => Child process has pid %d\n", pid );
close( fd );
break;
}
}
else {
D_DEBUG_AT( Voodoo_Server, " -> running threaded only...\n" );
accept_connection( server, fd );
}
break;
case 0:
waitpid( -1, NULL, WNOHANG );
//D_DEBUG_AT( Voodoo_Server, " -> Timeout during poll()\n" );
break;
case -1:
if (errno != EINTR) {
D_PERROR( "Voodoo/Server: Could not poll() the socket!\n" );
server->quit = true;
}
break;
}
}
else {
if (server->connections)
usleep( 200000 );
else
return DR_OK;
}
}
return DR_OK;
}
DirectResult
voodoo_server_destroy( VoodooServer *server )
{
DirectLink *l;
D_ASSERT( server != NULL );
close( server->fd );
/* Close all connections. */
direct_list_foreach (l, server->connections) {
Connection *connection = (Connection*) l;
voodoo_manager_destroy( connection->manager );
//connection->vl.Close( &connection->vl );
D_FREE( connection );
}
if (server->shared)
munmap( server->shared, sizeof(ServerShared) );
D_FREE( server );
return DR_OK;
}
/**********************************************************************************************************************/
static DirectResult
accept_connection( VoodooServer *server, int fd )
{
DirectResult ret;
int fds[2] = { fd, fd };
Connection *connection;
connection = D_CALLOC( 1, sizeof(Connection) );
if (!connection) {
D_WARN( "out of memory" );
return DR_NOLOCALMEMORY;
}
ret = voodoo_link_init_fd( &connection->vl, fds );
if (ret) {
D_PERROR( "Voodoo/Server: accept_connection: failed to init fd!\n" );
return ret;
}
ret = voodoo_manager_create( &connection->vl, NULL, server, &connection->manager );
if (ret) {
connection->vl.Close( &connection->vl );
D_FREE( connection );
return ret;
}
direct_list_prepend( &server->connections, &connection->link );
return DR_OK;
}
|