Move updating the acl object into serve.c

* * *
Replacing the server acl sends an acl_updated signal
This commit is contained in:
Alex Young
2012-06-08 10:32:33 +01:00
parent 5fb0cd4cca
commit f7e1a098b1
5 changed files with 109 additions and 10 deletions

View File

@@ -96,8 +96,23 @@ file check("acl") =>
gcc_link t.name, t.prerequisites + [LIBCHECK] gcc_link t.name, t.prerequisites + [LIBCHECK]
end end
file check("serve") =>
%w{tests/check_serve.c
build/self_pipe.o
build/nbdtypes.o
build/control.o
build/readwrite.o
build/parse.o
build/client.o
build/serve.o
build/acl.o
build/ioutil.o
build/util.o} do |t|
gcc_link t.name, t.prerequisites + [LIBCHECK]
end
(TEST_MODULES- %w{acl client}).each do |m|
(TEST_MODULES- %w{acl client serve}).each do |m|
deps = ["tests/check_#{m}.c", "build/ioutil.o", "build/util.o"] deps = ["tests/check_#{m}.c", "build/ioutil.o", "build/util.o"]
maybe_obj_name = "build/#{m}.o" maybe_obj_name = "build/#{m}.o"

View File

@@ -273,8 +273,7 @@ int control_acl(struct control_params* client, int linesc, char** lines)
acl_destroy( new_acl ); acl_destroy( new_acl );
} }
else { else {
client->serve->acl = new_acl; server_replace_acl( client->serve, new_acl );
acl_destroy( old_acl );
write_socket("0: updated"); write_socket("0: updated");
} }

View File

@@ -314,6 +314,20 @@ void server_close_clients( struct server *params )
} }
void server_replace_acl( struct server *serve, struct acl * new_acl )
{
NULLCHECK(serve);
NULLCHECK(new_acl);
struct acl * old_acl = serve->acl;
serve->acl = new_acl;
if ( old_acl ) { acl_destroy( old_acl ); }
self_pipe_signal( serve->acl_updated_signal );
}
/** Accept either an NBD or control socket connection, dispatch appropriately */ /** Accept either an NBD or control socket connection, dispatch appropriately */
void serve_accept_loop(struct server* params) void serve_accept_loop(struct server* params)
{ {
@@ -402,6 +416,7 @@ void serve_cleanup(struct server* params)
close(params->proxy_fd); close(params->proxy_fd);
self_pipe_destroy( params->close_signal ); self_pipe_destroy( params->close_signal );
self_pipe_destroy( params->acl_updated_signal );
free(params->allocation_map); free(params->allocation_map);
@@ -426,9 +441,9 @@ void do_serve(struct server* params)
pthread_mutex_init(&params->l_io, NULL); pthread_mutex_init(&params->l_io, NULL);
params->close_signal = self_pipe_create(); params->close_signal = self_pipe_create();
if ( NULL == params->close_signal) { NULLCHECK( params->close_signal );
SERVER_ERROR( "close signal creation failed" ); params->acl_updated_signal = self_pipe_create();
} NULLCHECK( params->acl_updated_signal );
serve_open_server_socket(params); serve_open_server_socket(params);
serve_open_control_socket(params); serve_open_control_socket(params);

View File

@@ -3,14 +3,14 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#ifndef _LARGEFILE64_SOURCE
#define _LARGEFILE64_SOURCE #define _LARGEFILE64_SOURCE
#endif
#include <sys/types.h>
#include <unistd.h>
#include "parse.h" #include "parse.h"
#include "acl.h" #include "acl.h"
#include <sys/types.h>
static const int block_allocation_resolution = 4096;//128<<10; static const int block_allocation_resolution = 4096;//128<<10;
@@ -69,6 +69,11 @@ struct server {
/** to interrupt accept loop and clients, write() to close_signal[1] */ /** to interrupt accept loop and clients, write() to close_signal[1] */
struct self_pipe * close_signal; struct self_pipe * close_signal;
/** acl_updated_signal will be signalled after the acl struct
* has been replaced
*/
struct self_pipe * acl_updated_signal;
struct mirror_status* mirror; struct mirror_status* mirror;
int server_fd; int server_fd;
int control_fd; int control_fd;

65
tests/check_serve.c Normal file
View File

@@ -0,0 +1,65 @@
#include "serve.h"
#include "self_pipe.h"
#include <check.h>
#include <stdio.h>
START_TEST( test_replaces_acl )
{
struct server s;
s.acl_updated_signal = self_pipe_create();
struct acl * acl = acl_create( 0, NULL, 0 );
server_replace_acl( &s, acl );
fail_unless( s.acl == acl, "ACL wasn't replaced." );
self_pipe_destroy( s.acl_updated_signal );
}
END_TEST
START_TEST( test_signals_acl_updated )
{
struct server s;
struct acl * new_acl = acl_create( 0, NULL, 0 );
s.acl_updated_signal = self_pipe_create();
s.acl = acl_create( 0, NULL, 0);
server_replace_acl( &s, new_acl );
fail_unless( 1 == self_pipe_signal_clear( s.acl_updated_signal ),
"No signal sent." );
self_pipe_destroy( s.acl_updated_signal );
}
END_TEST
Suite* serve_suite()
{
Suite *s = suite_create("serve");
TCase *tc_acl_update = tcase_create("acl_update");
tcase_add_test(tc_acl_update, test_replaces_acl);
tcase_add_test(tc_acl_update, test_signals_acl_updated);
suite_add_tcase(s, tc_acl_update);
return s;
}
int main(void)
{
set_debug(1);
int number_failed;
Suite *s = serve_suite();
SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}