Modernize DEBUG macros, and make code -Wall -Werror clean
authorWouter Verhelst <w@uter.be>
Fri, 27 May 2011 15:23:45 +0000 (17:23 +0200)
committerWouter Verhelst <w@uter.be>
Fri, 27 May 2011 15:23:45 +0000 (17:23 +0200)
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
nbd-client.c
nbd-server.c
nbd-tester-client.c

index 51b1a9e..09cb3d6 100644 (file)
--- 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__))
 #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
 #else
 #define G_GNUC_NORETURN
+#define G_GNUC_UNUSED
 #endif
 #endif
 
 #endif
 #endif
 
index 78e62f9..b1e2a3c 100644 (file)
@@ -320,7 +320,7 @@ int main(int argc, char *argv[]) {
        int cont=0;
        int timeout=0;
        int sdp=0;
        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;
        u64 size64;
        u32 flags;
        int c;
index cd67e44..3343d9d 100644 (file)
@@ -133,17 +133,9 @@ int dontfork = 0;
 /* Debugging macros */
 //#define DODBG
 #ifdef DODBG
 /* 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
 #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 ""
 #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 {
                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);
                }
        }
                        g_hash_table_remove(children, &pid);
                }
        }
@@ -992,7 +984,7 @@ off_t size_autodetect(int fhandle) {
        if (es > ((off_t)0)) {
                return es;
         } else {
        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");
         }
 
        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;
 
        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);
 
        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;
 
        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);
 
        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));
 
        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;
 
 
        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=(0<DIFFPAGESIZE-offset && len<(size_t)(DIFFPAGESIZE-offset)) ?
                        len : (size_t)DIFFPAGESIZE-offset;
                if (client->difmap[mapcnt]!=(u32)(-1)) { /* the block is already there */
                rdlen=(0<DIFFPAGESIZE-offset && len<(size_t)(DIFFPAGESIZE-offset)) ?
                        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);
                        if (read(client->difffile, buf, rdlen) != rdlen) return -1;
                } else { /* the block is not there */
                               (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;
                }
                               (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)); 
 
        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 ;
 
 
        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 */
                        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);
                               (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++;
                } 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 ;
                               (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 expflush(CLIENT *client) {
-       int fhandle;
-       off_t foffset;
-       size_t maxbytes;
        gint i;
 
         if (client->server->flags & F_COPYONWRITE) {
        gint i;
 
         if (client->server->flags & F_COPYONWRITE) {
@@ -1492,11 +1481,9 @@ int mainloop(CLIENT *client) {
                } else {
                        currlen = len;
                }
                } 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,
                                "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)) {
                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);
                }
                } 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 */
                fi.fhandle = open(tmpname, mode);
                if(fi.fhandle == -1 && mode == O_RDWR) {
                        /* Try again because maybe media was read-only */
index 662c6f7..b5801ee 100644 (file)
 #include <unistd.h>
 #include "config.h"
 #include "lfs.h"
 #include <unistd.h>
 #include "config.h"
 #include "lfs.h"
-#define MY_NAME "nbd-tester-client"
-#include "cliserv.h"
-
 #include <netinet/in.h>
 #include <glib.h>
 
 #include <netinet/in.h>
 #include <glib.h>
 
+#define MY_NAME "nbd-tester-client"
+#include "cliserv.h"
+
 static gchar errstr[1024];
 const static int errstr_len=1024;
 
 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 retval=0;
        struct nbd_request req;
        struct nbd_reply rep;
-       int request=0;
        int i=0;
        int serverflags = 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;
 
        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;
 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;
        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;
        char speedchar[2] = { '\0', '\0' };
        int retval=0;
        int serverflags = 0;
-       size_t tmp;
        signed int do_write=TRUE;
        pid_t mypid = getpid();
 
        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) {
 
 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;
        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;
        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;
        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;
                int ret;
 
                uint32_t magic;
-                uint64_t hand;
                 uint32_t command;
                 uint64_t from;
                 uint32_t len;
                 uint32_t command;
                 uint64_t from;
                 uint32_t len;