2012-05-29 04:03:28 +01:00
|
|
|
/* FlexNBD server (C) Bytemark Hosting 2012
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** main() function for parsing and dispatching commands. Each mode has
|
|
|
|
* a corresponding structure which is filled in and passed to a do_ function
|
|
|
|
* elsewhere in the program.
|
|
|
|
*/
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
#include "flexnbd.h"
|
2012-06-06 10:45:07 +01:00
|
|
|
#include "serve.h"
|
2012-06-21 18:01:50 +01:00
|
|
|
#include "listen.h"
|
2012-05-17 20:14:22 +01:00
|
|
|
#include "util.h"
|
2012-06-27 15:45:33 +01:00
|
|
|
#include "control.h"
|
|
|
|
#include "status.h"
|
2012-05-15 02:42:03 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2012-05-17 20:14:22 +01:00
|
|
|
#include <sys/stat.h>
|
2012-06-27 15:45:33 +01:00
|
|
|
#include <sys/signalfd.h>
|
2012-05-17 20:14:22 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2012-05-27 14:40:16 +01:00
|
|
|
#include <signal.h>
|
2012-05-15 02:42:03 +01:00
|
|
|
|
2012-05-30 15:19:40 +01:00
|
|
|
#include <getopt.h>
|
2012-06-07 17:47:43 +01:00
|
|
|
|
|
|
|
#include "acl.h"
|
2012-05-30 15:19:40 +01:00
|
|
|
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
int flexnbd_build_signal_fd(void)
|
2012-05-30 15:19:40 +01:00
|
|
|
{
|
2012-06-27 15:45:33 +01:00
|
|
|
sigset_t mask;
|
|
|
|
int sfd;
|
2012-05-15 02:42:03 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
sigemptyset( &mask );
|
|
|
|
sigaddset( &mask, SIGTERM );
|
|
|
|
sigaddset( &mask, SIGQUIT );
|
|
|
|
sigaddset( &mask, SIGINT );
|
2012-05-15 02:42:03 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
FATAL_UNLESS( 0 == pthread_sigmask( SIG_BLOCK, &mask, NULL ),
|
|
|
|
"Signal blocking failed" );
|
2012-06-06 09:55:08 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
sfd = signalfd( -1, &mask, 0 );
|
|
|
|
FATAL_IF( -1 == sfd, "Failed to get a signal fd" );
|
2012-05-15 18:40:58 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
return sfd;
|
|
|
|
}
|
2012-05-17 20:14:22 +01:00
|
|
|
|
2012-06-01 14:48:34 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
void flexnbd_create_shared(
|
|
|
|
struct flexnbd * flexnbd,
|
|
|
|
const char * s_ctrl_sock)
|
2012-05-30 15:19:40 +01:00
|
|
|
{
|
2012-06-27 15:45:33 +01:00
|
|
|
NULLCHECK( flexnbd );
|
|
|
|
if ( s_ctrl_sock ){
|
|
|
|
flexnbd->control =
|
|
|
|
control_create( flexnbd, s_ctrl_sock );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
flexnbd->control = NULL;
|
2012-05-30 15:19:40 +01:00
|
|
|
}
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
flexnbd->signal_fd = flexnbd_build_signal_fd();
|
2012-05-30 15:19:40 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
pthread_mutex_init( &flexnbd->switch_mutex, NULL );
|
2012-06-21 18:01:50 +01:00
|
|
|
}
|
|
|
|
|
2012-05-30 15:19:40 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
struct flexnbd * flexnbd_create_serving(
|
|
|
|
char* s_ip_address,
|
|
|
|
char* s_port,
|
|
|
|
char* s_file,
|
|
|
|
char *s_ctrl_sock,
|
|
|
|
int default_deny,
|
|
|
|
int acl_entries,
|
|
|
|
char** s_acl_entries,
|
|
|
|
int max_nbd_clients)
|
2012-05-30 15:19:40 +01:00
|
|
|
{
|
2012-06-27 15:45:33 +01:00
|
|
|
struct flexnbd * flexnbd = xmalloc( sizeof( struct flexnbd ) );
|
|
|
|
flexnbd->serve = server_create(
|
|
|
|
flexnbd,
|
|
|
|
s_ip_address,
|
|
|
|
s_port,
|
|
|
|
s_file,
|
|
|
|
default_deny,
|
|
|
|
acl_entries,
|
|
|
|
s_acl_entries,
|
|
|
|
max_nbd_clients,
|
|
|
|
1);
|
|
|
|
flexnbd_create_shared( flexnbd, s_ctrl_sock );
|
|
|
|
return flexnbd;
|
2012-05-30 15:19:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
struct flexnbd * flexnbd_create_listening(
|
|
|
|
char* s_ip_address,
|
|
|
|
char* s_rebind_ip_address,
|
|
|
|
char* s_port,
|
|
|
|
char* s_rebind_port,
|
|
|
|
char* s_file,
|
|
|
|
char *s_ctrl_sock,
|
|
|
|
int default_deny,
|
|
|
|
int acl_entries,
|
|
|
|
char** s_acl_entries,
|
|
|
|
int max_nbd_clients )
|
2012-05-30 15:19:40 +01:00
|
|
|
{
|
2012-06-27 15:45:33 +01:00
|
|
|
struct flexnbd * flexnbd = xmalloc( sizeof( struct flexnbd ) );
|
|
|
|
flexnbd->listen = listen_create(
|
|
|
|
flexnbd,
|
|
|
|
s_ip_address,
|
|
|
|
s_rebind_ip_address,
|
|
|
|
s_port,
|
|
|
|
s_rebind_port,
|
|
|
|
s_file,
|
|
|
|
default_deny,
|
|
|
|
acl_entries,
|
|
|
|
s_acl_entries,
|
|
|
|
max_nbd_clients);
|
|
|
|
flexnbd->serve = flexnbd->listen->init_serve;
|
|
|
|
flexnbd_create_shared( flexnbd, s_ctrl_sock );
|
|
|
|
return flexnbd;
|
2012-05-30 15:19:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
void flexnbd_spawn_control(struct flexnbd * flexnbd )
|
2012-05-30 15:19:40 +01:00
|
|
|
{
|
2012-06-27 15:45:33 +01:00
|
|
|
NULLCHECK( flexnbd );
|
|
|
|
NULLCHECK( flexnbd->control );
|
2012-05-30 15:19:40 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
pthread_t * control_thread = &flexnbd->control->thread;
|
2012-06-01 14:48:34 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
FATAL_UNLESS( 0 == pthread_create(
|
|
|
|
control_thread,
|
|
|
|
NULL,
|
|
|
|
control_runner,
|
|
|
|
flexnbd->control ),
|
|
|
|
"Couldn't create the control thread" );
|
|
|
|
}
|
2012-05-30 15:19:40 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
void flexnbd_stop_control( struct flexnbd * flexnbd )
|
|
|
|
{
|
|
|
|
NULLCHECK( flexnbd );
|
|
|
|
NULLCHECK( flexnbd->control );
|
2012-05-30 15:19:40 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
control_signal_close( flexnbd->control );
|
|
|
|
FATAL_UNLESS( 0 == pthread_join( flexnbd->control->thread, NULL ),
|
|
|
|
"Failed joining the control thread" );
|
2012-05-30 15:19:40 +01:00
|
|
|
}
|
|
|
|
|
2012-06-21 18:01:50 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
int flexnbd_signal_fd( struct flexnbd * flexnbd )
|
2012-06-21 18:01:50 +01:00
|
|
|
{
|
2012-06-27 15:45:33 +01:00
|
|
|
NULLCHECK( flexnbd );
|
|
|
|
return flexnbd->signal_fd;
|
|
|
|
}
|
2012-06-21 18:01:50 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
void flexnbd_destroy( struct flexnbd * flexnbd )
|
|
|
|
{
|
|
|
|
NULLCHECK( flexnbd );
|
|
|
|
if ( flexnbd->control ) {
|
|
|
|
control_destroy( flexnbd->control );
|
2012-06-21 18:01:50 +01:00
|
|
|
}
|
2012-06-27 15:45:33 +01:00
|
|
|
if ( flexnbd->listen ) {
|
|
|
|
listen_destroy( flexnbd->listen );
|
2012-06-21 18:01:50 +01:00
|
|
|
}
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
close( flexnbd->signal_fd );
|
|
|
|
free( flexnbd );
|
2012-06-21 18:01:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
/* THOU SHALT NOT DEREFERENCE flexnbd->serve OUTSIDE A SWITCH LOCK
|
|
|
|
*/
|
|
|
|
void flexnbd_switch_lock( struct flexnbd * flexnbd )
|
2012-05-30 15:19:40 +01:00
|
|
|
{
|
2012-06-27 15:45:33 +01:00
|
|
|
NULLCHECK( flexnbd );
|
|
|
|
pthread_mutex_lock( &flexnbd->switch_mutex );
|
2012-05-30 15:19:40 +01:00
|
|
|
}
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
void flexnbd_switch_unlock( struct flexnbd * flexnbd )
|
2012-05-30 15:19:40 +01:00
|
|
|
{
|
2012-06-27 15:45:33 +01:00
|
|
|
NULLCHECK( flexnbd );
|
|
|
|
pthread_mutex_unlock( &flexnbd->switch_mutex );
|
2012-05-30 15:19:40 +01:00
|
|
|
}
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
struct server * flexnbd_server( struct flexnbd * flexnbd )
|
2012-05-15 02:42:03 +01:00
|
|
|
{
|
2012-06-27 15:45:33 +01:00
|
|
|
NULLCHECK( flexnbd );
|
|
|
|
return flexnbd->serve;
|
|
|
|
}
|
2012-05-30 15:19:40 +01:00
|
|
|
|
2012-05-31 13:31:22 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
void flexnbd_replace_acl( struct flexnbd * flexnbd, struct acl * acl )
|
|
|
|
{
|
|
|
|
NULLCHECK( flexnbd );
|
|
|
|
flexnbd_switch_lock( flexnbd );
|
|
|
|
{
|
|
|
|
server_replace_acl( flexnbd_server(flexnbd), acl );
|
2012-05-15 18:40:58 +01:00
|
|
|
}
|
2012-06-27 15:45:33 +01:00
|
|
|
flexnbd_switch_unlock( flexnbd );
|
2012-05-30 15:19:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
struct status * flexnbd_status_create( struct flexnbd * flexnbd )
|
2012-05-30 15:19:40 +01:00
|
|
|
{
|
2012-06-27 15:45:33 +01:00
|
|
|
NULLCHECK( flexnbd );
|
|
|
|
struct status * status;
|
2012-06-06 12:35:01 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
flexnbd_switch_lock( flexnbd );
|
|
|
|
{
|
|
|
|
status = status_create( flexnbd_server( flexnbd ) );
|
2012-06-11 14:34:17 +01:00
|
|
|
}
|
2012-06-27 15:45:33 +01:00
|
|
|
flexnbd_switch_unlock( flexnbd );
|
|
|
|
return status;
|
|
|
|
}
|
2012-05-30 15:19:40 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
/** THOU SHALT *ONLY* CALL THIS FROM INSIDE A SWITCH LOCK
|
|
|
|
*/
|
|
|
|
void flexnbd_set_server( struct flexnbd * flexnbd, struct server * serve )
|
|
|
|
{
|
|
|
|
NULLCHECK( flexnbd );
|
|
|
|
flexnbd->serve = serve;
|
2012-05-30 15:19:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
/* Calls the given callback to exchange server objects, then sets
|
|
|
|
* flexnbd->server so everything else can see it. */
|
|
|
|
void flexnbd_switch( struct flexnbd * flexnbd, struct server *(listen_cb)(struct listen *) )
|
2012-05-30 15:19:40 +01:00
|
|
|
{
|
2012-06-27 15:45:33 +01:00
|
|
|
NULLCHECK( flexnbd );
|
|
|
|
NULLCHECK( flexnbd->listen );
|
2012-05-31 13:31:22 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
flexnbd_switch_lock( flexnbd );
|
|
|
|
{
|
|
|
|
struct server * new_server = listen_cb( flexnbd->listen );
|
|
|
|
NULLCHECK( new_server );
|
|
|
|
flexnbd_set_server( flexnbd, new_server );
|
2012-05-30 15:19:40 +01:00
|
|
|
}
|
2012-06-27 15:45:33 +01:00
|
|
|
flexnbd_switch_unlock( flexnbd );
|
2012-05-30 15:19:40 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
/* Get the default_deny of the current server object. This takes the
|
|
|
|
* switch_lock to avoid nastiness if the server switches and gets freed
|
|
|
|
* in the dereference chain.
|
|
|
|
* This means that this function must not be called if the switch lock
|
|
|
|
* is already held.
|
|
|
|
*/
|
|
|
|
int flexnbd_default_deny( struct flexnbd * flexnbd )
|
2012-05-30 15:19:40 +01:00
|
|
|
{
|
2012-06-27 15:45:33 +01:00
|
|
|
int result;
|
2012-05-30 15:19:40 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
NULLCHECK( flexnbd );
|
|
|
|
flexnbd_switch_lock( flexnbd );
|
|
|
|
{
|
|
|
|
result = server_default_deny( flexnbd->serve );
|
|
|
|
}
|
|
|
|
flexnbd_switch_unlock( flexnbd );
|
|
|
|
return result;
|
2012-05-30 15:19:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
int flexnbd_serve( struct flexnbd * flexnbd )
|
2012-05-30 15:19:40 +01:00
|
|
|
{
|
2012-06-27 15:45:33 +01:00
|
|
|
NULLCHECK( flexnbd );
|
|
|
|
int success;
|
|
|
|
|
|
|
|
if ( flexnbd->control ){
|
|
|
|
debug( "Spawning control thread" );
|
|
|
|
flexnbd_spawn_control( flexnbd );
|
2012-05-30 15:19:40 +01:00
|
|
|
}
|
2012-06-27 15:45:33 +01:00
|
|
|
|
|
|
|
if ( flexnbd->listen ){
|
|
|
|
success = do_listen( flexnbd->listen );
|
2012-05-29 00:59:12 +01:00
|
|
|
}
|
2012-05-15 02:42:03 +01:00
|
|
|
else {
|
2012-06-27 15:45:33 +01:00
|
|
|
do_serve( flexnbd->serve );
|
|
|
|
/* We can't tell here what the intent was. We can
|
|
|
|
* legitimately exit either in control or not.
|
|
|
|
*/
|
|
|
|
success = 1;
|
2012-05-15 02:42:03 +01:00
|
|
|
}
|
2012-05-30 15:19:40 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
if ( flexnbd->control ) {
|
|
|
|
debug( "Stopping control thread" );
|
|
|
|
flexnbd_stop_control( flexnbd );
|
2012-05-30 15:19:40 +01:00
|
|
|
}
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
return success;
|
2012-05-15 02:42:03 +01:00
|
|
|
}
|
|
|
|
|