From a7334bd4c1a3f5118ae364b7bcb98a9cc0bb2db6 Mon Sep 17 00:00:00 2001 From: Wouter Verhelst Date: Fri, 27 May 2011 17:23:45 +0200 Subject: [PATCH] Modernize DEBUG macros, and make code -Wall -Werror clean This involved several things: - Remove unused variables - Mark variables that may or may not be used when specific macros are defined, but which we still want to define for the other cases without having to #ifdef too much, as such by way of G_GNUC_UNUSED. - use varargs macros instead of the DEBUG[2-5] ones (which we can now do since we require a C99 compiler). - cast integers to what we request in the format string, and standardize on assuming that size_t is an unsigned int everywhere. This may not always be correct (depending on whether we do LFS support), but debugging statements that are not 100% correct (as long as they contain a value that maps one-on-one to the right bit pattern) isn't a huge problem. - Move the "include cliserv.h" to below the "include glib.h", so we don't define macros twice. --- cliserv.h | 2 ++ nbd-client.c | 2 +- nbd-server.c | 43 +++++++++++++++---------------------------- nbd-tester-client.c | 15 +++++---------- 4 files changed, 23 insertions(+), 39 deletions(-) diff --git a/cliserv.h b/cliserv.h index 51b1a9e..09cb3d6 100644 --- a/cliserv.h +++ b/cliserv.h @@ -76,8 +76,10 @@ void setmysockopt(int sock) { #ifndef G_GNUC_NORETURN #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) #define G_GNUC_NORETURN __attribute__((__noreturn__)) +#define G_GNUC_UNUSED __attribute__((unused)) #else #define G_GNUC_NORETURN +#define G_GNUC_UNUSED #endif #endif diff --git a/nbd-client.c b/nbd-client.c index 78e62f9..b1e2a3c 100644 --- a/nbd-client.c +++ b/nbd-client.c @@ -320,7 +320,7 @@ int main(int argc, char *argv[]) { int cont=0; int timeout=0; int sdp=0; - int nofork=0; + int G_GNUC_UNUSED nofork=0; // if -dNOFORK u64 size64; u32 flags; int c; diff --git a/nbd-server.c b/nbd-server.c index cd67e44..3343d9d 100644 --- a/nbd-server.c +++ b/nbd-server.c @@ -133,17 +133,9 @@ int dontfork = 0; /* Debugging macros */ //#define DODBG #ifdef DODBG -#define DEBUG( a ) printf( a ) -#define DEBUG2( a,b ) printf( a,b ) -#define DEBUG3( a,b,c ) printf( a,b,c ) -#define DEBUG4( a,b,c,d ) printf( a,b,c,d ) -#define DEBUG5( a,b,c,d,e ) printf( a,b,c,d,e ) +#define DEBUG(...) printf(__VA_ARGS__) #else -#define DEBUG( a ) -#define DEBUG2( a,b ) -#define DEBUG3( a,b,c ) -#define DEBUG4( a,b,c,d ) -#define DEBUG5( a,b,c,d,e ) +#define DEBUG(...) #endif #ifndef PACKAGE_VERSION #define PACKAGE_VERSION "" @@ -914,7 +906,7 @@ void sigchld_handler(int s) { if(!i) { msg3(LOG_INFO, "SIGCHLD received for an unknown child with PID %ld", (long)pid); } else { - DEBUG2("Removing %d from the list of children", pid); + DEBUG("Removing %d from the list of children", pid); g_hash_table_remove(children, &pid); } } @@ -992,7 +984,7 @@ off_t size_autodetect(int fhandle) { if (es > ((off_t)0)) { return es; } else { - DEBUG2("lseek failed: %d", errno==EBADF?1:(errno==ESPIPE?2:(errno==EINVAL?3:4))); + DEBUG("lseek failed: %d", errno==EBADF?1:(errno==ESPIPE?2:(errno==EINVAL?3:4))); } err("Could not find size of exported block device: %m"); @@ -1082,7 +1074,7 @@ ssize_t rawexpwrite(off_t a, char *buf, size_t len, CLIENT *client, int fua) { if(maxbytes && len > maxbytes) len = maxbytes; - DEBUG5("(WRITE to fd %d offset %llu len %u fua %d), ", fhandle, foffset, len, fua); + DEBUG("(WRITE to fd %d offset %llu len %u fua %d), ", fhandle, (long long unsigned)foffset, (unsigned int)len, fua); myseek(fhandle, foffset); retval = write(fhandle, buf, len); @@ -1167,7 +1159,7 @@ ssize_t rawexpread(off_t a, char *buf, size_t len, CLIENT *client) { if(maxbytes && len > maxbytes) len = maxbytes; - DEBUG4("(READ from fd %d offset %llu len %u), ", fhandle, foffset, len); + DEBUG("(READ from fd %d offset %llu len %u), ", fhandle, (long long unsigned int)foffset, (unsigned int)len); myseek(fhandle, foffset); return read(fhandle, buf, len); @@ -1204,7 +1196,7 @@ int expread(off_t a, char *buf, size_t len, CLIENT *client) { if (!(client->server->flags & F_COPYONWRITE)) return(rawexpread_fully(a, buf, len, client)); - DEBUG3("Asked to read %d bytes at %llu.\n", len, (unsigned long long)a); + DEBUG("Asked to read %u bytes at %llu.\n", (unsigned int)len, (unsigned long long)a); mapl=a/DIFFPAGESIZE; maph=(a+len-1)/DIFFPAGESIZE; @@ -1214,12 +1206,12 @@ int expread(off_t a, char *buf, size_t len, CLIENT *client) { rdlen=(0difmap[mapcnt]!=(u32)(-1)) { /* the block is already there */ - DEBUG3("Page %llu is at %lu\n", (unsigned long long)mapcnt, + DEBUG("Page %llu is at %lu\n", (unsigned long long)mapcnt, (unsigned long)(client->difmap[mapcnt])); myseek(client->difffile, client->difmap[mapcnt]*DIFFPAGESIZE+offset); if (read(client->difffile, buf, rdlen) != rdlen) return -1; } else { /* the block is not there */ - DEBUG2("Page %llu is not here, we read the original one\n", + DEBUG("Page %llu is not here, we read the original one\n", (unsigned long long)mapcnt); if(rawexpread_fully(a, buf, rdlen, client)) return -1; } @@ -1248,7 +1240,7 @@ int expwrite(off_t a, char *buf, size_t len, CLIENT *client, int fua) { if (!(client->server->flags & F_COPYONWRITE)) return(rawexpwrite_fully(a, buf, len, client, fua)); - DEBUG3("Asked to write %d bytes at %llu.\n", len, (unsigned long long)a); + DEBUG("Asked to write %u bytes at %llu.\n", (unsigned int)len, (unsigned long long)a); mapl=a/DIFFPAGESIZE ; maph=(a+len-1)/DIFFPAGESIZE ; @@ -1259,7 +1251,7 @@ int expwrite(off_t a, char *buf, size_t len, CLIENT *client, int fua) { len : (size_t)DIFFPAGESIZE-offset; if (client->difmap[mapcnt]!=(u32)(-1)) { /* the block is already there */ - DEBUG3("Page %llu is at %lu\n", (unsigned long long)mapcnt, + DEBUG("Page %llu is at %lu\n", (unsigned long long)mapcnt, (unsigned long)(client->difmap[mapcnt])) ; myseek(client->difffile, client->difmap[mapcnt]*DIFFPAGESIZE+offset); @@ -1267,7 +1259,7 @@ int expwrite(off_t a, char *buf, size_t len, CLIENT *client, int fua) { } else { /* the block is not there */ myseek(client->difffile,client->difffilelen*DIFFPAGESIZE) ; client->difmap[mapcnt]=(client->server->flags&F_SPARSE)?mapcnt:client->difffilelen++; - DEBUG3("Page %llu is not here, we put it at %lu\n", + DEBUG("Page %llu is not here, we put it at %lu\n", (unsigned long long)mapcnt, (unsigned long)(client->difmap[mapcnt])); rdlen=DIFFPAGESIZE ; @@ -1292,9 +1284,6 @@ int expwrite(off_t a, char *buf, size_t len, CLIENT *client, int fua) { } int expflush(CLIENT *client) { - int fhandle; - off_t foffset; - size_t maxbytes; gint i; if (client->server->flags & F_COPYONWRITE) { @@ -1492,11 +1481,9 @@ int mainloop(CLIENT *client) { } else { currlen = len; } -#ifdef DODBG - printf("%s from %llu (%llu) len %d, ", command ? "WRITE" : + DEBUG("%s from %llu (%llu) len %d, ", command ? "WRITE" : "READ", (unsigned long long)request.from, - (unsigned long long)request.from / 512, len); -#endif + (unsigned long long)request.from / 512, (unsigned int)len); memcpy(reply.handle, request.handle, sizeof(reply.handle)); if ((command==NBD_CMD_WRITE) || (command==NBD_CMD_READ)) { @@ -1607,7 +1594,7 @@ void setupexport(CLIENT* client) { } else { tmpname=g_strdup(client->exportname); } - DEBUG2( "Opening %s\n", tmpname ); + DEBUG( "Opening %s\n", tmpname ); fi.fhandle = open(tmpname, mode); if(fi.fhandle == -1 && mode == O_RDWR) { /* Try again because maybe media was read-only */ diff --git a/nbd-tester-client.c b/nbd-tester-client.c index 662c6f7..b5801ee 100644 --- a/nbd-tester-client.c +++ b/nbd-tester-client.c @@ -35,12 +35,12 @@ #include #include "config.h" #include "lfs.h" -#define MY_NAME "nbd-tester-client" -#include "cliserv.h" - #include #include +#define MY_NAME "nbd-tester-client" +#include "cliserv.h" + static gchar errstr[1024]; const static int errstr_len=1024; @@ -333,10 +333,9 @@ int oversize_test(gchar* hostname, int port, char* name, int sock, int retval=0; struct nbd_request req; struct nbd_reply rep; - int request=0; int i=0; int serverflags = 0; - pid_t mypid = getpid(); + pid_t G_GNUC_UNUSED mypid = getpid(); char buf[((1024*1024)+sizeof(struct nbd_request)/2)<<1]; bool got_err; @@ -401,7 +400,6 @@ int oversize_test(gchar* hostname, int port, char* name, int sock, int throughput_test(gchar* hostname, int port, char* name, int sock, char sock_is_open, char close_sock, int testflags) { long long int i; - char buf[1024]; char writebuf[1024]; struct nbd_request req; int requests=0; @@ -414,7 +412,6 @@ int throughput_test(gchar* hostname, int port, char* name, int sock, char speedchar[2] = { '\0', '\0' }; int retval=0; int serverflags = 0; - size_t tmp; signed int do_write=TRUE; pid_t mypid = getpid(); @@ -609,7 +606,6 @@ static inline void dumpcommand(char * text, uint32_t command) int integrity_test(gchar* hostname, int port, char* name, int sock, char sock_is_open, char close_sock, int testflags) { - struct nbd_request req; struct nbd_reply rep; fd_set rset; fd_set wset; @@ -621,7 +617,7 @@ int integrity_test(gchar* hostname, int port, char* name, int sock, char speedchar[2] = { '\0', '\0' }; int retval=0; int serverflags = 0; - pid_t mypid = getpid(); + pid_t G_GNUC_UNUSED mypid = getpid(); int blkhashfd = -1; char *blkhashname=NULL; uint32_t *blkhash = NULL; @@ -710,7 +706,6 @@ int integrity_test(gchar* hostname, int port, char* name, int sock, int ret; uint32_t magic; - uint64_t hand; uint32_t command; uint64_t from; uint32_t len; -- 1.7.10.4