Add the pid to the status output

This will be needed if we daemonise flexnbd.
This commit is contained in:
Alex Young
2012-07-16 11:50:59 +01:00
parent 8814894874
commit 2e20e7197a
3 changed files with 49 additions and 3 deletions

View File

@@ -8,6 +8,7 @@ struct status * status_create( struct server * serve )
struct status * status; struct status * status;
status = xmalloc( sizeof( struct status ) ); status = xmalloc( sizeof( struct status ) );
status->pid = getpid();
status->has_control = serve->has_control; status->has_control = serve->has_control;
status->is_mirroring = NULL != serve->mirror; status->is_mirroring = NULL != serve->mirror;
return status; return status;
@@ -15,13 +16,16 @@ struct status * status_create( struct server * serve )
} }
#define BOOL_S(var) (var ? "true" : "false" ) #define BOOL_S(var) (var ? "true" : "false" )
#define PRINT_FIELD( var ) \ #define PRINT_BOOL( var ) \
do{dprintf( fd, #var "=%s ", BOOL_S( status->var ) );}while(0) do{dprintf( fd, #var "=%s ", BOOL_S( status->var ) );}while(0)
#define PRINT_INT( var ) \
do{dprintf( fd, #var "=%d ", status->var );}while(0)
int status_write( struct status * status, int fd ) int status_write( struct status * status, int fd )
{ {
PRINT_FIELD( is_mirroring ); PRINT_INT( pid );
PRINT_FIELD( has_control ); PRINT_BOOL( is_mirroring );
PRINT_BOOL( has_control );
dprintf(fd, "\n"); dprintf(fd, "\n");
return 1; return 1;
} }

View File

@@ -17,6 +17,9 @@
* *
* The following status fields are defined: * The following status fields are defined:
* *
* pid:
* The current process ID.
*
* has_control: * has_control:
* This will be false when the server is listening for an incoming * This will be false when the server is listening for an incoming
* migration. It will switch to true when the end-of-migration * migration. It will switch to true when the end-of-migration
@@ -36,7 +39,11 @@
#include "serve.h" #include "serve.h"
#include <sys/types.h>
#include <unistd.h>
struct status { struct status {
pid_t pid;
int has_control; int has_control;
int is_mirroring; int is_mirroring;
}; };

View File

@@ -49,6 +49,21 @@ START_TEST( test_gets_is_mirroring )
END_TEST END_TEST
START_TEST( test_gets_pid )
{
struct server server;
struct status * status;
server.mirror = NULL;
status = status_create( &server );
fail_unless( getpid() == status->pid, "Pid wasn't gathered" );
status_destroy( status );
}
END_TEST
START_TEST( test_renders_has_control ) START_TEST( test_renders_has_control )
{ {
struct status status; struct status status;
@@ -105,6 +120,24 @@ START_TEST( test_renders_is_mirroring )
END_TEST END_TEST
START_TEST( test_renders_pid )
{
struct status status;
int fds[2];
pipe(fds);
char buf[1024] = {0};
status.pid = 42;
status_write( &status, fds[1] );
fail_unless( read_until_newline( fds[0], buf, 1024 ) > 0,
"Couldn't read the result" );
char *found = strstr( buf, "pid=42" );
fail_if( NULL == found, "pid=42 not found" );
}
END_TEST
Suite *status_suite(void) Suite *status_suite(void)
{ {
Suite *s = suite_create("status"); Suite *s = suite_create("status");
@@ -114,9 +147,11 @@ Suite *status_suite(void)
tcase_add_test(tc_create, test_status_create); tcase_add_test(tc_create, test_status_create);
tcase_add_test(tc_create, test_gets_has_control); tcase_add_test(tc_create, test_gets_has_control);
tcase_add_test(tc_create, test_gets_is_mirroring); tcase_add_test(tc_create, test_gets_is_mirroring);
tcase_add_test(tc_create, test_gets_pid);
tcase_add_test(tc_render, test_renders_has_control); tcase_add_test(tc_render, test_renders_has_control);
tcase_add_test(tc_render, test_renders_is_mirroring); tcase_add_test(tc_render, test_renders_is_mirroring);
tcase_add_test(tc_render, test_renders_pid);
suite_add_tcase(s, tc_create); suite_add_tcase(s, tc_create);
suite_add_tcase(s, tc_render); suite_add_tcase(s, tc_render);