2012-06-06 10:45:07 +01:00
|
|
|
#include "serve.h"
|
2012-06-06 11:27:52 +01:00
|
|
|
#include "client.h"
|
2012-05-17 20:14:22 +01:00
|
|
|
#include "nbdtypes.h"
|
|
|
|
#include "ioutil.h"
|
|
|
|
#include "util.h"
|
2012-05-18 13:24:35 +01:00
|
|
|
#include "bitset.h"
|
2012-05-23 00:42:14 +01:00
|
|
|
#include "control.h"
|
2012-06-06 12:41:03 +01:00
|
|
|
#include "self_pipe.h"
|
2012-05-17 20:14:22 +01:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
2012-05-18 18:44:34 +01:00
|
|
|
#include <sys/un.h>
|
2012-05-17 20:14:22 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2012-05-31 11:33:31 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
|
2012-06-06 10:45:07 +01:00
|
|
|
static inline void* sockaddr_address_data(struct sockaddr* sockaddr)
|
|
|
|
{
|
2012-06-07 11:44:19 +01:00
|
|
|
NULLCHECK( sockaddr );
|
|
|
|
|
2012-06-06 10:45:07 +01:00
|
|
|
struct sockaddr_in* in = (struct sockaddr_in*) sockaddr;
|
|
|
|
struct sockaddr_in6* in6 = (struct sockaddr_in6*) sockaddr;
|
|
|
|
|
|
|
|
if (sockaddr->sa_family == AF_INET)
|
|
|
|
return &in->sin_addr;
|
|
|
|
if (sockaddr->sa_family == AF_INET6)
|
|
|
|
return &in6->sin6_addr;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-06 11:27:52 +01:00
|
|
|
void server_dirty(struct server *serve, off64_t from, int len)
|
2012-05-21 04:03:17 +01:00
|
|
|
{
|
2012-06-07 11:44:19 +01:00
|
|
|
NULLCHECK( serve );
|
|
|
|
|
2012-05-21 04:03:17 +01:00
|
|
|
if (serve->mirror)
|
|
|
|
bitset_set_range(serve->mirror->dirty_map, from, len);
|
|
|
|
}
|
|
|
|
|
2012-06-06 13:29:13 +01:00
|
|
|
int server_lock_io( struct server * serve)
|
|
|
|
{
|
2012-06-07 11:44:19 +01:00
|
|
|
NULLCHECK( serve );
|
|
|
|
|
2012-06-06 13:29:13 +01:00
|
|
|
SERVER_ERROR_ON_FAILURE(
|
|
|
|
pthread_mutex_lock(&serve->l_io),
|
|
|
|
"Problem with I/O lock"
|
|
|
|
);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void server_unlock_io( struct server* serve )
|
|
|
|
{
|
2012-06-07 11:44:19 +01:00
|
|
|
NULLCHECK( serve );
|
|
|
|
|
2012-06-06 13:29:13 +01:00
|
|
|
SERVER_ERROR_ON_FAILURE(
|
|
|
|
pthread_mutex_unlock(&serve->l_io),
|
|
|
|
"Problem with I/O unlock"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Prepares a listening socket for the NBD server, binding etc. */
|
2012-06-06 10:35:50 +01:00
|
|
|
void serve_open_server_socket(struct server* params)
|
2012-05-17 20:14:22 +01:00
|
|
|
{
|
2012-06-07 11:44:19 +01:00
|
|
|
NULLCHECK( params );
|
|
|
|
|
2012-05-29 00:59:12 +01:00
|
|
|
int optval=1;
|
|
|
|
|
2012-06-06 10:19:45 +01:00
|
|
|
params->server_fd= socket(params->bind_to.generic.sa_family == AF_INET ?
|
2012-05-27 14:40:16 +01:00
|
|
|
PF_INET : PF_INET6, SOCK_STREAM, 0);
|
2012-05-17 20:14:22 +01:00
|
|
|
|
2012-06-06 10:19:45 +01:00
|
|
|
SERVER_ERROR_ON_FAILURE(params->server_fd,
|
2012-05-17 20:14:22 +01:00
|
|
|
"Couldn't create server socket");
|
2012-05-27 14:40:16 +01:00
|
|
|
|
2012-05-29 00:59:12 +01:00
|
|
|
SERVER_ERROR_ON_FAILURE(
|
2012-06-06 10:19:45 +01:00
|
|
|
setsockopt(params->server_fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)),
|
2012-05-29 00:59:12 +01:00
|
|
|
"Couldn't set SO_REUSEADDR"
|
|
|
|
);
|
|
|
|
|
2012-05-17 20:14:22 +01:00
|
|
|
SERVER_ERROR_ON_FAILURE(
|
2012-06-06 10:19:45 +01:00
|
|
|
setsockopt(params->server_fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(optval)),
|
2012-05-31 11:33:31 +01:00
|
|
|
"Couldn't set TCP_NODELAY"
|
|
|
|
);
|
|
|
|
|
|
|
|
SERVER_ERROR_ON_FAILURE(
|
2012-06-06 10:19:45 +01:00
|
|
|
bind(params->server_fd, ¶ms->bind_to.generic,
|
2012-05-27 14:40:16 +01:00
|
|
|
sizeof(params->bind_to)),
|
2012-05-17 20:14:22 +01:00
|
|
|
"Couldn't bind server to IP address"
|
|
|
|
);
|
|
|
|
|
|
|
|
SERVER_ERROR_ON_FAILURE(
|
2012-06-06 10:19:45 +01:00
|
|
|
listen(params->server_fd, params->tcp_backlog),
|
2012-05-17 20:14:22 +01:00
|
|
|
"Couldn't listen on server socket"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-06-07 14:25:30 +01:00
|
|
|
int tryjoin_client_thread( struct client_tbl_entry *entry, int (*joinfunc)(pthread_t, void **) )
|
2012-06-07 11:44:19 +01:00
|
|
|
{
|
2012-06-07 14:25:30 +01:00
|
|
|
|
2012-06-07 11:44:19 +01:00
|
|
|
NULLCHECK( entry );
|
2012-06-07 14:25:30 +01:00
|
|
|
NULLCHECK( joinfunc );
|
2012-06-07 11:44:19 +01:00
|
|
|
|
|
|
|
int was_closed = 0;
|
|
|
|
void * status;
|
|
|
|
|
|
|
|
if (entry->thread != 0) {
|
|
|
|
char s_client_address[64];
|
|
|
|
|
|
|
|
memset(s_client_address, 0, 64);
|
|
|
|
strcpy(s_client_address, "???");
|
|
|
|
inet_ntop( entry->address.generic.sa_family,
|
|
|
|
sockaddr_address_data(&entry->address.generic),
|
|
|
|
s_client_address,
|
|
|
|
64 );
|
|
|
|
|
2012-06-07 14:25:30 +01:00
|
|
|
if (joinfunc(entry->thread, &status) != 0) {
|
2012-06-07 11:44:19 +01:00
|
|
|
if (errno != EBUSY)
|
|
|
|
SERVER_ERROR_ON_FAILURE(-1, "Problem with joining thread");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
debug("nbd thread %p exited (%s) with status %ld",
|
|
|
|
(int) entry->thread,
|
|
|
|
s_client_address,
|
|
|
|
(uint64_t)status);
|
|
|
|
client_destroy( entry->client );
|
|
|
|
entry->thread = 0;
|
|
|
|
was_closed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return was_closed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-07 14:25:30 +01:00
|
|
|
/**
|
|
|
|
* Check to see if a client thread has finished, and if so, tidy up
|
|
|
|
* after it.
|
|
|
|
* Returns 1 if the thread was cleaned up and the slot freed, 0
|
|
|
|
* otherwise.
|
|
|
|
*
|
|
|
|
* It's important that client_destroy gets called in the same thread
|
|
|
|
* which signals the client threads to stop. This avoids the
|
|
|
|
* possibility of sending a stop signal via a signal which has already
|
|
|
|
* been destroyed. However, it means that stopped client threads,
|
|
|
|
* including their signal pipes, won't be cleaned up until the next new
|
|
|
|
* client connection attempt.
|
|
|
|
*/
|
|
|
|
int cleanup_client_thread( struct client_tbl_entry * entry )
|
|
|
|
{
|
|
|
|
return tryjoin_client_thread( entry, pthread_tryjoin_np );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Join a client thread after having sent a stop signal to it.
|
|
|
|
* This function will not return until pthread_join has returned, so
|
|
|
|
* ensures that the client thread is dead.
|
|
|
|
*/
|
|
|
|
int join_client_thread( struct client_tbl_entry *entry )
|
|
|
|
{
|
|
|
|
return tryjoin_client_thread( entry, pthread_join );
|
|
|
|
}
|
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
/** We can only accommodate MAX_NBD_CLIENTS connections at once. This function
|
|
|
|
* goes through the current list, waits for any threads that have finished
|
|
|
|
* and returns the next slot free (or -1 if there are none).
|
|
|
|
*/
|
2012-06-06 10:35:50 +01:00
|
|
|
int cleanup_and_find_client_slot(struct server* params)
|
2012-05-27 14:40:16 +01:00
|
|
|
{
|
2012-06-07 11:44:19 +01:00
|
|
|
NULLCHECK( params );
|
|
|
|
|
|
|
|
int slot=-1, i,j;
|
|
|
|
|
|
|
|
for ( i = 0; i < MAX_NBD_CLIENTS; i++ ) {
|
|
|
|
cleanup_client_thread( ¶ms->nbd_client[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( j = 0; j < MAX_NBD_CLIENTS; j++ ) {
|
|
|
|
if( params->nbd_client[j].thread == 0 && slot == -1 ){
|
|
|
|
slot = j;
|
|
|
|
break;
|
2012-05-27 14:40:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 11:44:19 +01:00
|
|
|
if ( -1 == slot ) { debug( "No client slot found." ); }
|
|
|
|
|
2012-05-27 14:40:16 +01:00
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2012-06-07 11:44:19 +01:00
|
|
|
|
|
|
|
int server_acl_accepts( struct server *params, union mysockaddr * client_address )
|
|
|
|
{
|
|
|
|
NULLCHECK( params );
|
|
|
|
NULLCHECK( client_address );
|
|
|
|
|
|
|
|
if (params->acl) {
|
2012-06-07 17:47:43 +01:00
|
|
|
return acl_includes( params->acl, client_address );
|
2012-06-07 11:44:19 +01:00
|
|
|
}
|
2012-06-07 17:47:43 +01:00
|
|
|
|
|
|
|
return 1;
|
2012-06-07 11:44:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int server_should_accept_client(
|
|
|
|
struct server * params,
|
|
|
|
int client_fd,
|
|
|
|
union mysockaddr * client_address,
|
|
|
|
char *s_client_address,
|
|
|
|
size_t s_client_address_len )
|
|
|
|
{
|
|
|
|
NULLCHECK( params );
|
|
|
|
NULLCHECK( client_address );
|
|
|
|
NULLCHECK( s_client_address );
|
|
|
|
|
|
|
|
if (inet_ntop(client_address->generic.sa_family,
|
|
|
|
sockaddr_address_data(&client_address->generic),
|
|
|
|
s_client_address, s_client_address_len ) == NULL) {
|
|
|
|
debug( "Rejecting client %s: Bad client_address", s_client_address );
|
|
|
|
write(client_fd, "Bad client_address", 18);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !server_acl_accepts( params, client_address ) ) {
|
|
|
|
debug( "Rejecting client %s: Access control error", s_client_address );
|
|
|
|
debug( "We %s have an acl, and default_deny is %s",
|
|
|
|
(params->acl ? "do" : "do not"),
|
|
|
|
(params->default_deny ? "true" : "false") );
|
|
|
|
write(client_fd, "Access control error", 20);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Dispatch function for accepting an NBD connection and starting a thread
|
|
|
|
* to handle it. Rejects the connection if there is an ACL, and the far end's
|
|
|
|
* address doesn't match, or if there are too many clients already connected.
|
|
|
|
*/
|
2012-06-01 16:24:50 +01:00
|
|
|
void accept_nbd_client(
|
2012-06-06 10:35:50 +01:00
|
|
|
struct server* params,
|
2012-06-01 16:24:50 +01:00
|
|
|
int client_fd,
|
|
|
|
union mysockaddr* client_address)
|
2012-05-18 23:39:16 +01:00
|
|
|
{
|
2012-06-07 11:44:19 +01:00
|
|
|
NULLCHECK(params);
|
|
|
|
NULLCHECK(client_address);
|
|
|
|
|
2012-06-06 11:33:17 +01:00
|
|
|
struct client* client_params;
|
2012-06-07 14:25:30 +01:00
|
|
|
int slot;
|
2012-06-07 11:44:19 +01:00
|
|
|
char s_client_address[64] = {0};
|
2012-06-01 14:48:34 +01:00
|
|
|
|
|
|
|
|
2012-06-07 11:44:19 +01:00
|
|
|
if ( !server_should_accept_client( params, client_fd, client_address, s_client_address, 64 ) ) {
|
|
|
|
close( client_fd );
|
|
|
|
return;
|
2012-06-01 14:48:34 +01:00
|
|
|
}
|
|
|
|
|
2012-06-07 14:25:30 +01:00
|
|
|
slot = cleanup_and_find_client_slot(params);
|
2012-05-27 14:40:16 +01:00
|
|
|
if (slot < 0) {
|
|
|
|
write(client_fd, "Too many clients", 16);
|
|
|
|
close(client_fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-07 11:44:19 +01:00
|
|
|
debug( "Client %s accepted.", s_client_address );
|
|
|
|
client_params = client_create( params, client_fd );
|
2012-06-07 14:25:30 +01:00
|
|
|
|
2012-06-07 11:44:19 +01:00
|
|
|
params->nbd_client[slot].client = client_params;
|
2012-06-07 14:25:30 +01:00
|
|
|
memcpy(¶ms->nbd_client[slot].address, client_address,
|
|
|
|
sizeof(union mysockaddr));
|
2012-05-18 23:39:16 +01:00
|
|
|
|
2012-05-27 14:40:16 +01:00
|
|
|
if (pthread_create(¶ms->nbd_client[slot].thread, NULL, client_serve, client_params) < 0) {
|
2012-06-07 11:44:19 +01:00
|
|
|
debug( "Thread creation problem." );
|
2012-05-27 14:40:16 +01:00
|
|
|
write(client_fd, "Thread creation problem", 23);
|
2012-06-07 11:44:19 +01:00
|
|
|
client_destroy( client_params );
|
2012-05-27 14:40:16 +01:00
|
|
|
close(client_fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("nbd thread %d started (%s)", (int) params->nbd_client[slot].thread, s_client_address);
|
2012-05-18 23:39:16 +01:00
|
|
|
}
|
|
|
|
|
2012-06-06 11:27:52 +01:00
|
|
|
|
2012-06-06 14:25:35 +01:00
|
|
|
int server_is_closed(struct server* serve)
|
2012-06-06 11:27:52 +01:00
|
|
|
{
|
2012-06-07 11:44:19 +01:00
|
|
|
NULLCHECK( serve );
|
|
|
|
return fd_is_closed( serve->server_fd );
|
2012-06-06 11:27:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-07 14:25:30 +01:00
|
|
|
void server_close_clients( struct server *params )
|
|
|
|
{
|
|
|
|
NULLCHECK(params);
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
struct client_tbl_entry *entry;
|
|
|
|
|
|
|
|
for( i = 0; i < MAX_NBD_CLIENTS; i++ ) {
|
|
|
|
entry = ¶ms->nbd_client[i];
|
|
|
|
|
|
|
|
if ( entry->thread != 0 ) {
|
|
|
|
client_signal_stop( entry->client );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for( j = 0; j < MAX_NBD_CLIENTS; j++ ) {
|
|
|
|
join_client_thread( ¶ms->nbd_client[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Accept either an NBD or control socket connection, dispatch appropriately */
|
2012-06-06 10:35:50 +01:00
|
|
|
void serve_accept_loop(struct server* params)
|
2012-05-17 20:14:22 +01:00
|
|
|
{
|
2012-06-07 11:44:19 +01:00
|
|
|
NULLCHECK( params );
|
2012-05-17 20:14:22 +01:00
|
|
|
while (1) {
|
2012-05-30 20:13:56 +01:00
|
|
|
int activity_fd, client_fd;
|
|
|
|
union mysockaddr client_address;
|
|
|
|
fd_set fds;
|
|
|
|
socklen_t socklen=sizeof(client_address);
|
2012-05-17 20:14:22 +01:00
|
|
|
|
2012-05-18 23:39:16 +01:00
|
|
|
FD_ZERO(&fds);
|
2012-06-06 10:19:45 +01:00
|
|
|
FD_SET(params->server_fd, &fds);
|
2012-06-06 12:41:03 +01:00
|
|
|
self_pipe_fd_set( params->close_signal, &fds );
|
2012-05-21 04:03:17 +01:00
|
|
|
if (params->control_socket_name)
|
2012-06-06 10:35:50 +01:00
|
|
|
FD_SET(params->control_fd, &fds);
|
2012-05-17 20:14:22 +01:00
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
SERVER_ERROR_ON_FAILURE(select(FD_SETSIZE, &fds,
|
|
|
|
NULL, NULL, NULL), "select() failed");
|
|
|
|
|
2012-06-07 14:25:30 +01:00
|
|
|
if ( self_pipe_fd_isset( params->close_signal, &fds ) ){
|
|
|
|
server_close_clients( params );
|
2012-05-29 04:03:28 +01:00
|
|
|
return;
|
2012-06-07 14:25:30 +01:00
|
|
|
}
|
2012-05-17 20:14:22 +01:00
|
|
|
|
2012-06-06 10:19:45 +01:00
|
|
|
activity_fd = FD_ISSET(params->server_fd, &fds) ? params->server_fd:
|
2012-06-06 10:35:50 +01:00
|
|
|
params->control_fd;
|
2012-05-30 20:13:56 +01:00
|
|
|
client_fd = accept(activity_fd, &client_address.generic, &socklen);
|
2012-05-17 20:14:22 +01:00
|
|
|
|
2012-06-07 11:44:19 +01:00
|
|
|
if (activity_fd == params->server_fd) {
|
2012-06-07 17:47:43 +01:00
|
|
|
debug("Accepted nbd client socket");
|
2012-05-18 23:39:16 +01:00
|
|
|
accept_nbd_client(params, client_fd, &client_address);
|
2012-06-07 11:44:19 +01:00
|
|
|
}
|
|
|
|
if (activity_fd == params->control_fd) {
|
2012-06-07 17:47:43 +01:00
|
|
|
debug("Accepted control client socket");
|
2012-05-18 23:39:16 +01:00
|
|
|
accept_control_connection(params, client_fd, &client_address);
|
2012-06-07 11:44:19 +01:00
|
|
|
}
|
2012-05-29 00:59:12 +01:00
|
|
|
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Initialisation function that sets up the initial allocation map, i.e. so
|
|
|
|
* we know which blocks of the file are allocated.
|
|
|
|
*/
|
2012-06-06 10:35:50 +01:00
|
|
|
void serve_init_allocation_map(struct server* params)
|
2012-05-18 13:24:35 +01:00
|
|
|
{
|
2012-06-07 11:44:19 +01:00
|
|
|
NULLCHECK( params );
|
|
|
|
|
2012-05-18 13:24:35 +01:00
|
|
|
int fd = open(params->filename, O_RDONLY);
|
|
|
|
off64_t size;
|
2012-06-07 11:44:19 +01:00
|
|
|
|
2012-05-18 13:24:35 +01:00
|
|
|
SERVER_ERROR_ON_FAILURE(fd, "Couldn't open %s", params->filename);
|
|
|
|
size = lseek64(fd, 0, SEEK_END);
|
2012-05-21 04:03:17 +01:00
|
|
|
params->size = size;
|
2012-05-18 13:24:35 +01:00
|
|
|
SERVER_ERROR_ON_FAILURE(size, "Couldn't find size of %s",
|
|
|
|
params->filename);
|
2012-06-07 11:17:02 +01:00
|
|
|
params->allocation_map =
|
2012-05-18 13:24:35 +01:00
|
|
|
build_allocation_map(fd, size, block_allocation_resolution);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2012-06-06 12:41:03 +01:00
|
|
|
|
|
|
|
/* Tell the server to close all the things. */
|
|
|
|
void serve_signal_close( struct server * serve )
|
|
|
|
{
|
2012-06-07 11:44:19 +01:00
|
|
|
NULLCHECK( serve );
|
2012-06-06 12:41:03 +01:00
|
|
|
self_pipe_signal( serve->close_signal );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Closes sockets, frees memory and waits for all client threads to finish */
|
2012-06-06 10:35:50 +01:00
|
|
|
void serve_cleanup(struct server* params)
|
2012-05-29 04:03:28 +01:00
|
|
|
{
|
2012-06-07 11:44:19 +01:00
|
|
|
NULLCHECK( params );
|
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
int i;
|
|
|
|
|
2012-06-06 10:19:45 +01:00
|
|
|
close(params->server_fd);
|
2012-06-06 10:35:50 +01:00
|
|
|
close(params->control_fd);
|
2012-05-29 04:03:28 +01:00
|
|
|
if (params->acl)
|
|
|
|
free(params->acl);
|
|
|
|
//free(params->filename);
|
|
|
|
if (params->control_socket_name)
|
|
|
|
//free(params->control_socket_name);
|
|
|
|
pthread_mutex_destroy(¶ms->l_io);
|
|
|
|
if (params->proxy_fd);
|
|
|
|
close(params->proxy_fd);
|
2012-06-06 12:41:03 +01:00
|
|
|
|
|
|
|
self_pipe_destroy( params->close_signal );
|
|
|
|
|
2012-06-07 11:17:02 +01:00
|
|
|
free(params->allocation_map);
|
2012-05-29 04:03:28 +01:00
|
|
|
|
|
|
|
if (params->mirror)
|
|
|
|
debug("mirror thread running! this should not happen!");
|
|
|
|
|
|
|
|
for (i=0; i < MAX_NBD_CLIENTS; i++) {
|
|
|
|
void* status;
|
|
|
|
|
|
|
|
if (params->nbd_client[i].thread != 0) {
|
|
|
|
debug("joining thread %d", i);
|
|
|
|
pthread_join(params->nbd_client[i].thread, &status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Full lifecycle of the server */
|
2012-06-06 10:35:50 +01:00
|
|
|
void do_serve(struct server* params)
|
2012-05-17 20:14:22 +01:00
|
|
|
{
|
2012-06-07 11:44:19 +01:00
|
|
|
NULLCHECK( params );
|
|
|
|
|
2012-05-29 00:59:12 +01:00
|
|
|
pthread_mutex_init(¶ms->l_io, NULL);
|
2012-06-06 12:41:03 +01:00
|
|
|
|
|
|
|
params->close_signal = self_pipe_create();
|
2012-06-06 13:19:24 +01:00
|
|
|
if ( NULL == params->close_signal) {
|
|
|
|
SERVER_ERROR( "close signal creation failed" );
|
|
|
|
}
|
2012-05-29 00:59:12 +01:00
|
|
|
|
2012-05-18 18:44:34 +01:00
|
|
|
serve_open_server_socket(params);
|
|
|
|
serve_open_control_socket(params);
|
2012-05-18 13:24:35 +01:00
|
|
|
serve_init_allocation_map(params);
|
2012-05-17 20:14:22 +01:00
|
|
|
serve_accept_loop(params);
|
2012-05-29 04:03:28 +01:00
|
|
|
serve_cleanup(params);
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
|
|
|
|