diff --git a/buffer.c b/buffer.c index 7009615d..913f1d1b 100644 --- a/buffer.c +++ b/buffer.c @@ -27,6 +27,8 @@ OTHER DEALINGS IN THE SOFTWARE. #include #include "buffer.h" +#include + struct buffer { diff --git a/buffer.h b/buffer.h index c92b9a68..328abef0 100644 --- a/buffer.h +++ b/buffer.h @@ -25,7 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE. #ifndef __TLSPROXY_BUFFERS_H #define __TLSPROXY_BUFFERS_H -#include +//#include #include typedef struct buffer buffer_t; diff --git a/cliserv.c b/cliserv.c index 651145f8..795a7560 100644 --- a/cliserv.c +++ b/cliserv.c @@ -1,4 +1,5 @@ #include +#include #include #include #include diff --git a/cliserv.h b/cliserv.h index 9e2d8c01..bec0b5ca 100644 --- a/cliserv.h +++ b/cliserv.h @@ -9,14 +9,14 @@ Send 128 bytes of zeros (reserved for future use) */ -#include +// #include #include #include #include #include #include -#include "nbd.h" +//#include "nbd.h" #ifndef HAVE_FDATASYNC #define fdatasync(arg) fsync(arg) diff --git a/nbd-client.c b/nbd-client.c index 2e98ba53..f08140b7 100644 --- a/nbd-client.c +++ b/nbd-client.c @@ -19,17 +19,18 @@ */ #include "config.h" -#include "lfs.h" +// #include "lfs.h" +#include "nbd.h" #include #include #include #include #include -#include +// #include #include #include -#include "netdb-compat.h" +// #include "netdb-compat.h" #include #include #include @@ -39,12 +40,12 @@ #include #include #include -#include +// #include #include #include #include -#include +// #include #if HAVE_NETLINK #include "nbd-netlink.h" diff --git a/nbd-debug.h b/nbd-debug.h index f2b1af57..3343ff02 100644 --- a/nbd-debug.h +++ b/nbd-debug.h @@ -5,7 +5,7 @@ #ifdef DODBG #define DEBUG(...) printf(__VA_ARGS__) #else -#define DEBUG(...) +#define DEBUG(...) do{}while(0) #endif #ifndef PACKAGE_VERSION #define PACKAGE_VERSION "" diff --git a/nbd-server.c b/nbd-server.c index 6a87d445..8836ca6b 100644 --- a/nbd-server.c +++ b/nbd-server.c @@ -78,11 +78,11 @@ #ifdef HAVE_SYS_UIO_H #include #endif -#include +// #include #include #include #include -#include +// #include #include #include #include @@ -98,8 +98,8 @@ #if HAVE_BLKDISCARD #include #endif -#include -#include +// #include +// #include #include #ifdef HAVE_SYS_DIR_H #include @@ -111,7 +111,7 @@ #include #include #include -#include +// #include #include #include @@ -120,7 +120,7 @@ #define MY_NAME "nbd_server" #include "cliserv.h" #include "nbd-debug.h" -#include "netdb-compat.h" +// #include "netdb-compat.h" #include "backend.h" #include "treefiles.h" #include "nbd-helper.h" @@ -226,30 +226,25 @@ struct work_package { void* data; /**< for write requests */ }; -static volatile sig_atomic_t is_sigchld_caught; /**< Flag set by - SIGCHLD handler - to mark a child - exit */ - -static volatile sig_atomic_t is_sigterm_caught; /**< Flag set by - SIGTERM handler - to mark a exit - request */ - -static volatile sig_atomic_t is_sighup_caught; /**< Flag set by SIGHUP - handler to mark a - reconfiguration - request */ - -GArray* modernsocks; /**< Sockets for the modern handler. Not used - if a client was only specified on the - command line; only port used if - oldstyle is set to false (and then the - command-line client isn't used, gna gna). - This may be more than one socket on - systems that don't support serving IPv4 - and IPv6 from the same socket (like, - e.g., FreeBSD) */ +/// Flag set by SIGCHLD handler to mark a child exit +static volatile sig_atomic_t is_sigchld_caught; + +/// Flag set by SIGTERM handler to mark an exit request +static volatile sig_atomic_t is_sigterm_caught; + +/// Flag set by SIGHUP handler to mark a reconfiguration request +static volatile sig_atomic_t is_sighup_caught; + +/** + * Sockets for the modern handler. + * Not used if a client was only specified on the command line; + * only port used if oldstyle is set to false (and then the command-line + * client isn't used, gna gna). + * This may be more than one socket on systems that don't support serving + * IPv4 and IPv6 from the same socket (like, e.g., FreeBSD) + */ +GArray* modernsocks; + GArray* childsocks; /**< parent-side sockets for communication with children */ int commsocket; /**< child-side socket for communication with parent */ static sem_t file_wait_sem; @@ -262,7 +257,7 @@ bool logged_oversized=false; /**< whether we logged oversized requests already typedef enum { PARAM_INT, /**< This parameter is an integer */ PARAM_INT64, /**< This parameter is an integer */ - PARAM_STRING, /**< This parameter is a string */ + PARAM_STRING, /**< This parameter is a string */ PARAM_BOOL, /**< This parameter is a boolean */ } PARAM_TYPE; @@ -270,34 +265,31 @@ typedef enum { * Configuration file values **/ typedef struct { - gchar *paramname; /**< Name of the parameter, as it appears in - the config file */ - gboolean required; /**< Whether this is a required (as opposed to - optional) parameter */ + gchar *paramname; /**< Name of the parameter, as it appears in the config file */ + gboolean required; /**< Whether this is a required (as opposed to optional) parameter */ PARAM_TYPE ptype; /**< Type of the parameter. */ gpointer target; /**< Pointer to where the data of this parameter should be written. If ptype is PARAM_BOOL, the data is or'ed rather than overwritten. */ - gint flagval; /**< Flag mask for this parameter in case ptype - is PARAM_BOOL. */ + gint flagval; /**< Flag mask for this parameter in case ptype is PARAM_BOOL. */ } PARAM; /** * Configuration file values of the "generic" section **/ struct generic_conf { - gchar *user; /**< user we run the server as */ - gchar *group; /**< group we run running as */ - gchar *modernaddr; /**< address of the modern socket */ - gchar *modernport; /**< port of the modern socket */ - gchar *unixsock; /**< file name of the unix domain socket */ - gchar *certfile; /**< certificate file */ - gchar *keyfile; /**< key file */ - gchar *cacertfile; /**< CA certificate file */ - gchar *tlsprio; /**< TLS priority string */ - gint flags; /**< global flags */ - gint threads; /**< maximum number of parallel threads we want to run */ + gchar *user; /**< user we run the server as */ + gchar *group; /**< group we run running as */ + gchar *modernaddr; /**< address of the modern socket */ + gchar *modernport; /**< port of the modern socket */ + gchar *unixsock; /**< file name of the unix domain socket */ + gchar *certfile; /**< certificate file */ + gchar *keyfile; /**< key file */ + gchar *cacertfile; /**< CA certificate file */ + gchar *tlsprio; /**< TLS priority string */ + gint flags; /**< global flags */ + gint threads; /**< maximum number of parallel threads we want to run */ }; #if HAVE_GNUTLS @@ -379,9 +371,8 @@ static void socket_read(CLIENT* client, void *buf, size_t len) { * @param bufsiz the size of the buffer **/ static inline void consume(CLIENT* c, size_t len, void * buf, size_t bufsiz) { - size_t curlen; while (len>0) { - curlen = (len>bufsiz)?bufsiz:len; + const size_t curlen = (len>bufsiz)?bufsiz:len; socket_read(c, buf, curlen); len -= curlen; } @@ -555,7 +546,7 @@ SERVER* cmdline(int argc, char *argv[], struct generic_conf *genconf) { int i=0; int nonspecial=0; int c; - struct option long_options[] = { + const struct option long_options[] = { {"read-only", no_argument, NULL, 'r'}, {"multi-file", no_argument, NULL, 'm'}, {"copy-on-write", no_argument, NULL, 'c'}, @@ -1199,7 +1190,7 @@ int get_filepos(CLIENT *client, off_t a, int* fhandle, off_t* foffset, size_t* m * @param fua Flag to indicate 'Force Unit Access' * @return The number of bytes actually written, or -1 in case of an error **/ -ssize_t rawexpwrite(off_t a, char *buf, size_t len, CLIENT *client, int fua) { +ssize_t rawexpwrite(off_t a, const char *buf, size_t len, CLIENT *client, int fua) { int fhandle; off_t foffset; size_t maxbytes; @@ -1272,7 +1263,7 @@ ssize_t rawexpwrite(off_t a, char *buf, size_t len, CLIENT *client, int fua) { * @param fua Flag to indicate 'Force Unit Access' * @return 0 on success, nonzero on failure **/ -int rawexpwrite_fully(off_t a, char *buf, size_t len, CLIENT *client, int fua) { +int rawexpwrite_fully(off_t a, const char *buf, size_t len, CLIENT *client, int fua) { ssize_t ret=0; while(len > 0 && (ret=rawexpwrite(a, buf, len, client, fua)) > 0 ) { diff --git a/nbd-trdump.c b/nbd-trdump.c index eb02245d..6a6d8513 100644 --- a/nbd-trdump.c +++ b/nbd-trdump.c @@ -8,7 +8,7 @@ #include #include #include -#include +// #include #include #include #include diff --git a/nbd-trplay.c b/nbd-trplay.c index deeea517..8c5f181f 100644 --- a/nbd-trplay.c +++ b/nbd-trplay.c @@ -9,10 +9,11 @@ */ #include +#include #include #include #include -#include +// #include #include #include #include diff --git a/nbd.h b/nbd.h index 049e7455..348aca7c 100644 --- a/nbd.h +++ b/nbd.h @@ -15,7 +15,7 @@ #ifndef LINUX_NBD_H #define LINUX_NBD_H -//#include +#include #define NBD_SET_SOCK _IO( 0xab, 0 ) #define NBD_SET_BLKSIZE _IO( 0xab, 1 ) diff --git a/nbdclt.h b/nbdclt.h index 51e7ccb6..e16cddee 100644 --- a/nbdclt.h +++ b/nbdclt.h @@ -1,6 +1,9 @@ #ifndef NBDCLT_H #define NBDCLT_H +#include +#include + typedef struct { char *name; char *dev; @@ -23,7 +26,7 @@ typedef struct { bool preinit; bool force_ro; bool tls; - bool persist_mode; + bool persist_mode; char *priority; int dead_conn_timeout; } CLIENT; diff --git a/nbdsrv.c b/nbdsrv.c index 6b4182ec..9967b8a8 100644 --- a/nbdsrv.c +++ b/nbdsrv.c @@ -1,12 +1,13 @@ #include "config.h" #include "nbd-debug.h" -#include +#include "nbdsrv.h" #include #include #include #include +#include #include #include #include @@ -18,16 +19,17 @@ #include #include #include "backend.h" + #ifdef HAVE_SYS_IOCTL_H #include #endif + #ifdef HAVE_SYS_MOUNT_H #include #endif -#define LINELEN 256 /**< Size of static buffer used to read the - authorization file (yuck) */ -#include +#define LINELEN 256 /**< Size of static buffer used to read the authorization file (yuck) */ +#include "cliserv.h" bool address_matches(const char* mask, const struct sockaddr* addr, GError** err) { struct addrinfo *res, *aitmp, hints; @@ -227,14 +229,12 @@ SERVER* dup_serve(const SERVER *const s) { } uint64_t size_autodetect(int fhandle) { - off_t es; - uint64_t bytes __attribute__((unused)); struct stat stat_buf; - int error; #ifdef HAVE_SYS_MOUNT_H #ifdef HAVE_SYS_IOCTL_H #ifdef BLKGETSIZE64 + uint64_t bytes=0; DEBUG("looking for export size with ioctl BLKGETSIZE64\n"); if (!ioctl(fhandle, BLKGETSIZE64, &bytes) && bytes) { return bytes; @@ -245,7 +245,7 @@ uint64_t size_autodetect(int fhandle) { DEBUG("looking for fhandle size with fstat\n"); stat_buf.st_size = 0; - error = fstat(fhandle, &stat_buf); + const int error = fstat(fhandle, &stat_buf); if (!error) { /* always believe stat if a regular file as it might really * be zero length */ @@ -256,9 +256,9 @@ uint64_t size_autodetect(int fhandle) { } DEBUG("looking for fhandle size with lseek SEEK_END\n"); - es = lseek(fhandle, (off_t)0, SEEK_END); - if (es > ((off_t)0)) { - return (uint64_t)es; + const off_t es = lseek(fhandle, (off_t)0, SEEK_END); + if (es > 0) { + return es; } else { DEBUG("lseek failed: %d", errno==EBADF?1:(errno==ESPIPE?2:(errno==EINVAL?3:4))); } @@ -271,8 +271,8 @@ int exptrim(struct nbd_request* req, CLIENT* client) { /* Caller did range checking */ assert(!(client->server->flags & F_READONLY)); assert(req->from + req->len <= client->exportsize); - /* For copy-on-write, we should trim on the diff file. Not yet - * implemented. */ + + /* For copy-on-write, we should trim on the diff file. Not yet implemented. */ if(client->server->flags & F_COPYONWRITE) { DEBUG("TRIM not supported yet on copy-on-write exports"); return 0; @@ -281,9 +281,9 @@ int exptrim(struct nbd_request* req, CLIENT* client) { /* start address of first block to be trimmed */ off_t min = ( ( req->from + TREEPAGESIZE - 1 ) / TREEPAGESIZE) * TREEPAGESIZE; /* start address of first block NOT to be trimmed */ - off_t max = ( ( req->from + req->len ) / TREEPAGESIZE) * TREEPAGESIZE; + const off_t max = ( ( req->from + req->len ) / TREEPAGESIZE) * TREEPAGESIZE; while (minexportname,client->exportsize,min); + delete_treefile(client->exportname, client->exportsize, min); min+=TREEPAGESIZE; } DEBUG("Performed TRIM request on TREE structure from %llu to %llu", (unsigned long long) req->from, (unsigned long long) req->len); diff --git a/nbdsrv.h b/nbdsrv.h index 4590c862..2e0df63f 100644 --- a/nbdsrv.h +++ b/nbdsrv.h @@ -1,11 +1,12 @@ #ifndef NBDSRV_H #define NBDSRV_H -#include "lfs.h" +// #include "lfs.h" #include #include #include +#include #include #include diff --git a/treefiles.c b/treefiles.c index a5552226..ac401411 100644 --- a/treefiles.c +++ b/treefiles.c @@ -1,16 +1,19 @@ +#include #include #include // for PATH_MAX #include #include #include #include -#include +// #include #include "config.h" #include "cliserv.h" #include "treefiles.h" #include "nbd-debug.h" +#include + /** * Tree structure helper functions */ @@ -31,18 +34,18 @@ void construct_path(char* name, int lenmax, off_t size, off_t pos, off_t* ppos) } } -void delete_treefile(char* name, off_t size, off_t pos) { +void delete_treefile(const char* export_name, off_t export_size, off_t pos) { char filename[PATH_MAX]; off_t ppos; - strncpy(filename,name,PATH_MAX-1); + strncpy(filename, export_name, PATH_MAX-1); filename[PATH_MAX-1] = '\0'; - construct_path(filename+strlen(name),PATH_MAX-strlen(name)-1,size,pos,&ppos); + construct_path(filename+strlen(export_name), PATH_MAX-strlen(export_name)-1, export_size, pos, &ppos); - DEBUG("Deleting treefile: %s",filename); + DEBUG("Deleting treefile: %s", filename); if (unlink(filename)==-1) - DEBUG("Deleting failed : %s",strerror(errno)); + DEBUG("Deleting failed : %s", strerror(errno)); } void mkdir_path(char* path) { @@ -58,13 +61,13 @@ void mkdir_path(char* path) { } } -int open_treefile(char* name, mode_t mode, off_t size, off_t pos, pthread_mutex_t* mutex) { +int open_treefile(const char* export_name, mode_t mode, off_t export_size, off_t pos, pthread_mutex_t* mutex) { char filename[PATH_MAX]; off_t ppos; - strncpy(filename,name,PATH_MAX-1); + strncpy(filename, export_name, PATH_MAX-1); filename[PATH_MAX - 1] = '\0'; - construct_path(filename+strlen(name),PATH_MAX-strlen(name)-1,size,pos,&ppos); + construct_path(filename+strlen(export_name), PATH_MAX-strlen(export_name)-1, export_size, pos, &ppos); DEBUG("Accessing treefile %s (offset %llu of %llu)",filename,(unsigned long long)pos,(unsigned long long)size); diff --git a/treefiles.h b/treefiles.h index 04466084..efca4dd9 100644 --- a/treefiles.h +++ b/treefiles.h @@ -7,9 +7,12 @@ #define TREEDIRSIZE 1024 /**< number of files per subdirectory (or subdirs per subdirectory) */ #define TREEPAGESIZE 4096 /**< tree (block) files uses those chunks */ -void construct_path(char *name, int lenmax, off_t size, off_t pos, off_t *ppos); -void delete_treefile(char *name, off_t size, off_t pos); +void construct_path(char *name, int lenmax, off_t export_size, off_t pos, off_t *ppos); + +void delete_treefile(const char *base_name, off_t export_size, off_t pos); + void mkdir_path(char *path); -int open_treefile(char *name, mode_t mode, off_t size, off_t pos, pthread_mutex_t *mutex); + +int open_treefile(const char *export_name, mode_t mode, off_t size, off_t pos, pthread_mutex_t *mutex); #endif