diff --git a/Rakefile b/Rakefile index da25c4b..cc9f40f 100644 --- a/Rakefile +++ b/Rakefile @@ -1,8 +1,6 @@ require 'rake_utils/debian' include RakeUtils::DSL -DEBUG = true - ALL_SOURCES =FileList['src/*'] SOURCES = ALL_SOURCES.select { |c| c =~ /\.c$/ } OBJECTS = SOURCES.pathmap( "%{^src,build}X.o" ) @@ -14,7 +12,7 @@ LIBCHECK = "/usr/lib/libcheck.a" TEST_MODULES = Dir["tests/check_*.c"].map { |n| n[12..-3] } -if DEBUG +if ENV['DEBUG'] LDFLAGS << ["-g"] CCFLAGS << ["-g -DDEBUG"] end diff --git a/src/bitset.h b/src/bitset.h index 8fb1673..9c081af 100644 --- a/src/bitset.h +++ b/src/bitset.h @@ -5,7 +5,6 @@ #include #include -#include "util.h" static inline char char_with_bit_set(int num) { return 1<<(num%8); } diff --git a/src/flexnbd.c b/src/flexnbd.c index 1acb911..23de499 100644 --- a/src/flexnbd.c +++ b/src/flexnbd.c @@ -43,9 +43,9 @@ void exit_err( char *msg ) } void params_serve( - struct mode_serve_params* out, - char* s_ip_address, - char* s_port, + struct mode_serve_params* out, + char* s_ip_address, + char* s_port, char* s_file, char *s_ctrl_sock, int acl_entries, @@ -53,16 +53,16 @@ void params_serve( ) { int parsed; - + out->tcp_backlog = 10; /* does this need to be settable? */ - + if (s_ip_address == NULL) SERVER_ERROR("No IP address supplied"); if (s_port == NULL) SERVER_ERROR("No port number supplied"); if (s_file == NULL) SERVER_ERROR("No filename supplied"); - + if (parse_ip_to_sockaddr(&out->bind_to.generic, s_ip_address) == 0) SERVER_ERROR("Couldn't parse server address '%s' (use 0 if " "you want to bind to all IPs)", s_ip_address); @@ -75,12 +75,12 @@ void params_serve( parsed = parse_acl(&out->acl, acl_entries, s_acl_entries); if (parsed != acl_entries) SERVER_ERROR("Bad ACL entry '%s'", s_acl_entries[parsed]); - + out->bind_to.v4.sin_port = atoi(s_port); if (out->bind_to.v4.sin_port < 0 || out->bind_to.v4.sin_port > 65535) SERVER_ERROR("Port number must be >= 0 and <= 65535"); out->bind_to.v4.sin_port = htobe16(out->bind_to.v4.sin_port); - + out->filename = s_file; out->filename_incomplete = xmalloc(strlen(s_file)+11); strcpy(out->filename_incomplete, s_file); @@ -104,7 +104,7 @@ void params_serve( void params_readwrite( int write_not_read, struct mode_readwrite_params* out, - char* s_ip_address, + char* s_ip_address, char* s_port, char* s_from, char* s_length_or_filename @@ -118,11 +118,11 @@ void params_readwrite( SERVER_ERROR("No from supplied"); if (s_length_or_filename == NULL) SERVER_ERROR("No length supplied"); - + if (parse_ip_to_sockaddr(&out->connect_to.generic, s_ip_address) == 0) - SERVER_ERROR("Couldn't parse connection address '%s'", + SERVER_ERROR("Couldn't parse connection address '%s'", s_ip_address); - + /* FIXME: duplicated from above */ out->connect_to.v4.sin_port = atoi(s_port); if (out->connect_to.v4.sin_port < 0 || out->connect_to.v4.sin_port > 65535) @@ -130,7 +130,7 @@ void params_readwrite( out->connect_to.v4.sin_port = htobe16(out->connect_to.v4.sin_port); out->from = atol(s_from); - + if (write_not_read) { if (s_length_or_filename[0]-48 < 10) { out->len = atol(s_length_or_filename); @@ -180,6 +180,9 @@ void read_serve_param( int c, char **ip_addr, char **ip_port, char **file, char case 's': *sock = optarg; break; + case 'd': + set_debug(1); + break; default: exit_err( serve_help_text ); break; @@ -206,6 +209,9 @@ void read_readwrite_param( int c, char **ip_addr, char **ip_port, char **from, c case 'S': *size = optarg; break; + case 'd': + set_debug(1); + break; default: exit_err( read_help_text ); break; @@ -222,6 +228,9 @@ void read_sock_param( int c, char **sock, char *help_text ) case 's': *sock = optarg; break; + case 'd': + set_debug(1); + break; default: exit_err( help_text ); break; @@ -249,6 +258,9 @@ void read_mirror_param( int c, char **sock, char **ip_addr, char **ip_port ) case 'p': *ip_port = optarg; break; + case 'd': + set_debug(1); + break; default: exit_err( mirror_help_text ); break; @@ -370,7 +382,7 @@ int mode_acl( int argc, char *argv[] ) if ( c == -1 ) break; read_acl_param( c, &sock ); } - + if ( NULL == sock ){ fprintf( stderr, "--sock is required.\n" ); exit_err( acl_help_text ); @@ -418,13 +430,13 @@ int mode_status( int argc, char *argv[] ) { int c; char *sock = NULL; - + while (1) { c = getopt_long( argc, argv, status_short_options, status_options, NULL ); if ( -1 == c ) break; read_status_param( c, &sock ); } - + if ( NULL == sock ){ fprintf( stderr, "--sock is required.\n" ); exit_err( acl_help_text ); @@ -500,6 +512,7 @@ int main(int argc, char** argv) { signal(SIGPIPE, SIG_IGN); /* calls to splice() unhelpfully throw this */ error_init(); + set_debug(0); if (argc < 2) { exit_err( help_help_text ); diff --git a/src/options.h b/src/options.h index 6dee399..2a7e489 100644 --- a/src/options.h +++ b/src/options.h @@ -32,23 +32,36 @@ #define GETOPT_FROM GETOPT_ARG( OPT_FROM, 'F' ) #define GETOPT_SIZE GETOPT_ARG( OPT_SIZE, 'S' ) +#ifdef DEBUG +# define OPT_DEBUG "debug" +# define GETOPT_DEBUG GETOPT_FLAG( OPT_DEBUG, 'd' ) +# define DEBUG_LINE \ + "\t--" OPT_DEBUG ",-d\tOutput debug information.\n" +#else +# define GETOPT_DEBUG {0} +# define DEBUG_LINE "" +#endif + + static struct option serve_options[] = { GETOPT_HELP, GETOPT_ADDR, GETOPT_PORT, GETOPT_FILE, GETOPT_SOCK, + GETOPT_DEBUG, {0} }; static char serve_short_options[] = "hl:p:f:s:"; -static char serve_help_text[] = +static char serve_help_text[] = "Usage: flexnbd " CMD_SERVE " [*]\n\n" "Serve FILE from ADDR:PORT, with an optional control socket at SOCK.\n\n" "\t--" OPT_HELP ",-h\tThis text.\n" "\t--" OPT_ADDR ",-l \tThe address to serve on.\n" "\t--" OPT_PORT ",-p \tThe port to serve on.\n" "\t--" OPT_FILE ",-f \tThe file to serve.\n" - "\t--" OPT_SOCK ",-s \tPath to the control socket to open.\n"; + "\t--" OPT_SOCK ",-s \tPath to the control socket to open.\n" + DEBUG_LINE; static struct option read_options[] = { GETOPT_HELP, @@ -56,6 +69,7 @@ static struct option read_options[] = { GETOPT_PORT, GETOPT_FROM, GETOPT_SIZE, + GETOPT_DEBUG, {0} }; static char read_short_options[] = "hl:p:F:S:"; @@ -66,7 +80,8 @@ static char read_help_text[] = "\t--" OPT_ADDR ",-l \tThe address to read from.\n" "\t--" OPT_PORT ",-p \tThe port to read from.\n" "\t--" OPT_FROM ",-F \tByte offset to read from.\n" - "\t--" OPT_SIZE ",-S \tBytes to read.\n"; + "\t--" OPT_SIZE ",-S \tBytes to read.\n" + DEBUG_LINE; static struct option *write_options = read_options; @@ -78,11 +93,13 @@ static char write_help_text[] = "\t--" OPT_ADDR ",-l \tThe address to write to.\n" "\t--" OPT_PORT ",-p \tThe port to write to.\n" "\t--" OPT_FROM ",-F \tByte offset to write from.\n" - "\t--" OPT_SIZE ",-S \tBytes to write.\n"; + "\t--" OPT_SIZE ",-S \tBytes to write.\n" + DEBUG_LINE; struct option acl_options[] = { GETOPT_HELP, GETOPT_SOCK, + GETOPT_DEBUG, {0} }; static char acl_short_options[] = "hs:"; @@ -90,13 +107,15 @@ static char acl_help_text[] = "Usage: flexnbd " CMD_ACL " [+]\n\n" "Set the access control list for a server with control socket SOCK.\n\n" "\t--" OPT_HELP ",-h\tThis text.\n" - "\t--" OPT_SOCK ",-s \tPath to the control socket.\n"; + "\t--" OPT_SOCK ",-s \tPath to the control socket.\n" + DEBUG_LINE; struct option mirror_options[] = { GETOPT_HELP, GETOPT_SOCK, GETOPT_ADDR, GETOPT_PORT, + GETOPT_DEBUG, {0} }; static char mirror_short_options[] = "hs:l:p:"; @@ -106,12 +125,14 @@ static char mirror_help_text[] = "\t--" OPT_HELP ",-h\tThis text.\n" "\t--" OPT_SOCK ",-s \tPath to the control socket.\n" "\t--" OPT_ADDR ",-l \tThe address to mirror to.\n" - "\t--" OPT_PORT ",-p \tThe port to mirror to.\n"; - + "\t--" OPT_PORT ",-p \tThe port to mirror to.\n" + DEBUG_LINE; + struct option status_options[] = { GETOPT_HELP, GETOPT_SOCK, + GETOPT_DEBUG, {0} }; static char status_short_options[] = "hs:"; @@ -119,9 +140,10 @@ static char status_help_text[] = "Usage: flexnbd " CMD_STATUS " \n\n" "Get the status for a server with control socket SOCK.\n\n" "\t--" OPT_HELP ",-h\tThis text.\n" - "\t--" OPT_SOCK ",-s \tPath to the control socket.\n"; + "\t--" OPT_SOCK ",-s \tPath to the control socket.\n" + DEBUG_LINE; -static char help_help_text[] = +static char help_help_text[] = "Usage: flexnbd [cmd options]\n\n" "Commands:\n" "\tflexnbd serve\n" diff --git a/src/util.c b/src/util.c index 0f7e2f1..05be379 100644 --- a/src/util.c +++ b/src/util.c @@ -10,6 +10,7 @@ #include "util.h" static pthread_t main_thread; +static int global_debug; void error_init() { @@ -59,3 +60,23 @@ void* xmalloc(size_t size) return p; } + +void set_debug(int value) { + global_debug = value; +} + +#ifdef DEBUG +# include +# include + +void debug(const char *msg, ...) { + va_list argp; + + if ( global_debug ) { + fprintf(stderr, "%08x %4d: ", (int) pthread_self(), (int) clock() ); + fprintf(stderr, msg, argp); + fprintf(stderr, "\n"); + } +} +#endif + diff --git a/src/util.h b/src/util.h index 932f97d..24a4994 100644 --- a/src/util.h +++ b/src/util.h @@ -12,12 +12,9 @@ void* xrealloc(void* ptr, size_t size); void* xmalloc(size_t size); -#ifndef DEBUG -# define debug(msg, ...) -#else -# include -# define debug(msg, ...) fprintf(stderr, "%08x %4d: " msg "\n" , \ - (int) pthread_self(), (int) clock(), ##__VA_ARGS__) +void set_debug(int value); +#ifdef DEBUG +void debug(const char*msg, ...); #endif #define CLIENT_ERROR(msg, ...) \