From: Ronnie Sahlberg Date: Thu, 22 Nov 2012 01:10:05 +0000 (-0800) Subject: Add an assert to track if we try to use an rpc_context after it has been destroyed X-Git-Tag: upstream/1.9.6^2~244 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=4a2b08768a0d9ec9f5604da76f8e41263f20b821;p=deb_libnfs.git Add an assert to track if we try to use an rpc_context after it has been destroyed --- diff --git a/include/libnfs-private.h b/include/libnfs-private.h index a083b37..e32a706 100644 --- a/include/libnfs-private.h +++ b/include/libnfs-private.h @@ -23,7 +23,10 @@ struct rpc_fragment { char *data; }; +#define RPC_CONTEXT_MAGIC 0xc6e46435 + struct rpc_context { + uint32_t magic; int fd; int is_connected; diff --git a/lib/init.c b/lib/init.c index 4c8f84f..ee6e474 100644 --- a/lib/init.c +++ b/lib/init.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include "slist.h" @@ -41,6 +42,7 @@ struct rpc_context *rpc_init_context(void) } memset(rpc, 0, sizeof(struct rpc_context)); + rpc->magic = RPC_CONTEXT_MAGIC; rpc->encodebuflen = 65536; rpc->encodebuf = malloc(rpc->encodebuflen); if (rpc->encodebuf == NULL) { @@ -79,6 +81,8 @@ struct rpc_context *rpc_init_udp_context(void) void rpc_set_auth(struct rpc_context *rpc, AUTH *auth) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (rpc->auth != NULL) { auth_destroy(rpc->auth); } @@ -90,6 +94,8 @@ void rpc_set_error(struct rpc_context *rpc, char *error_string, ...) { va_list ap; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (rpc->error_string != NULL) { free(rpc->error_string); } @@ -101,6 +107,8 @@ void rpc_set_error(struct rpc_context *rpc, char *error_string, ...) char *rpc_get_error(struct rpc_context *rpc) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + return rpc->error_string; } @@ -108,6 +116,8 @@ void rpc_error_all_pdus(struct rpc_context *rpc, char *error) { struct rpc_pdu *pdu; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + while((pdu = rpc->outqueue) != NULL) { pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data); SLIST_REMOVE(&rpc->outqueue, pdu); @@ -130,6 +140,8 @@ static void rpc_free_fragment(struct rpc_fragment *fragment) void rpc_free_all_fragments(struct rpc_context *rpc) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + while (rpc->fragments != NULL) { struct rpc_fragment *fragment = rpc->fragments; @@ -142,6 +154,8 @@ int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size) { struct rpc_fragment *fragment; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + fragment = malloc(sizeof(struct rpc_fragment)); if (fragment == NULL) { return -1; @@ -163,6 +177,8 @@ void rpc_destroy_context(struct rpc_context *rpc) { struct rpc_pdu *pdu; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + while((pdu = rpc->outqueue) != NULL) { pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data); SLIST_REMOVE(&rpc->outqueue, pdu); @@ -202,6 +218,7 @@ void rpc_destroy_context(struct rpc_context *rpc) rpc->udp_dest = NULL; } + rpc->magic = 0; free(rpc); } diff --git a/lib/libnfs-sync.c b/lib/libnfs-sync.c index 5f044dd..3547557 100644 --- a/lib/libnfs-sync.c +++ b/lib/libnfs-sync.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -66,6 +67,8 @@ static void wait_for_reply(struct rpc_context *rpc, struct sync_cb_data *cb_data { struct pollfd pfd; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + while (!cb_data->is_finished) { pfd.fd = rpc_get_fd(rpc); @@ -134,6 +137,8 @@ int nfs_mount(struct nfs_context *nfs, const char *server, const char *export) struct sync_cb_data cb_data; struct rpc_context *rpc = nfs_get_rpc_context(nfs); + assert(rpc->magic == RPC_CONTEXT_MAGIC); + cb_data.is_finished = 0; if (nfs_mount_async(nfs, server, export, mount_cb, &cb_data) != 0) { @@ -1148,6 +1153,8 @@ void mount_getexports_cb(struct rpc_context *mount_context, int status, void *da struct sync_cb_data *cb_data = private_data; exports export; + assert(mount_context->magic == RPC_CONTEXT_MAGIC); + cb_data->is_finished = 1; cb_data->status = status; cb_data->return_data = NULL; @@ -1229,6 +1236,8 @@ void callit_cb(struct rpc_context *rpc, int status, void *data _U_, void *privat char hostdd[16]; struct nfs_server_list *srvr; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_CANCEL) { return; } @@ -1282,6 +1291,8 @@ static int send_nfsd_probes(struct rpc_context *rpc, INTERFACE_INFO *InterfaceLi { int i=0; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + for(i = 0; i < numIfs; i++) { SOCKADDR *pAddress; @@ -1412,6 +1423,8 @@ static int send_nfsd_probes(struct rpc_context *rpc, struct ifconf *ifc, struct { char *ptr; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + for (ptr =(char *)(ifc->ifc_buf); ptr < (char *)(ifc->ifc_buf) + ifc->ifc_len; ) { struct ifreq *ifr; char bcdd[16]; diff --git a/lib/libnfs.c b/lib/libnfs.c index 86ff7c2..2f47ba8 100644 --- a/lib/libnfs.c +++ b/lib/libnfs.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -216,11 +217,13 @@ void free_nfs_cb_data(struct nfs_cb_data *data) -static void nfs_mount_10_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_mount_10_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -242,6 +245,8 @@ static void nfs_mount_9_cb(struct rpc_context *rpc, int status, void *command_da struct nfs_context *nfs = data->nfs; FSINFO3res *res = command_data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -268,6 +273,8 @@ static void nfs_mount_8_cb(struct rpc_context *rpc, int status, void *command_da struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -292,6 +299,8 @@ static void nfs_mount_7_cb(struct rpc_context *rpc, int status, void *command_da struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + /* Dont want any more callbacks even if the socket is closed */ rpc->connect_cb = NULL; @@ -320,6 +329,8 @@ static void nfs_mount_6_cb(struct rpc_context *rpc, int status, void *command_da struct nfs_context *nfs = data->nfs; mountres3 *res; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -365,6 +376,8 @@ static void nfs_mount_5_cb(struct rpc_context *rpc, int status, void *command_da struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -388,6 +401,8 @@ static void nfs_mount_4_cb(struct rpc_context *rpc, int status, void *command_da struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + /* Dont want any more callbacks even if the socket is closed */ rpc->connect_cb = NULL; @@ -415,6 +430,8 @@ static void nfs_mount_3_cb(struct rpc_context *rpc, int status, void *command_da struct nfs_context *nfs = data->nfs; uint32_t mount_port; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -448,6 +465,8 @@ static void nfs_mount_2_cb(struct rpc_context *rpc, int status, void *command_da struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -471,6 +490,8 @@ static void nfs_mount_1_cb(struct rpc_context *rpc, int status, void *command_da struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + /* Dont want any more callbacks even if the socket is closed */ rpc->connect_cb = NULL; @@ -535,12 +556,14 @@ int nfs_mount_async(struct nfs_context *nfs, const char *server, const char *exp * Functions to first look up a path, component by component, and then finally call a specific function once * the filehandle for the final component is found. */ -static void nfs_lookup_path_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_lookup_path_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; LOOKUP3res *res; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -654,13 +677,15 @@ static int nfs_lookuppath_async(struct nfs_context *nfs, const char *path, nfs_c /* * Async stat() */ -static void nfs_stat_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_stat_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { GETATTR3res *res; struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; struct stat st; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -734,7 +759,7 @@ int nfs_stat_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *p /* * Async open() */ -static void nfs_open_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_open_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { ACCESS3res *res; struct nfs_cb_data *data = private_data; @@ -742,6 +767,8 @@ static void nfs_open_cb(struct rpc_context *rpc _U_, int status, void *command_d struct nfsfh *nfsfh; unsigned int nfsmode = 0; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -847,12 +874,14 @@ int nfs_open_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb c /* * Async pread() */ -static void nfs_pread_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_pread_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; READ3res *res; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -877,13 +906,15 @@ static void nfs_pread_cb(struct rpc_context *rpc _U_, int status, void *command_ free_nfs_cb_data(data); } -static void nfs_pread_mcb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_pread_mcb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { struct nfs_mcb_data *mdata = private_data; struct nfs_cb_data *data = mdata->data; struct nfs_context *nfs = data->nfs; READ3res *res; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + data->num_calls--; if (status == RPC_STATUS_ERROR) { @@ -1024,12 +1055,14 @@ int nfs_read_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count, /* * Async pwrite() */ -static void nfs_pwrite_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_pwrite_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; WRITE3res *res; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -1054,13 +1087,15 @@ static void nfs_pwrite_cb(struct rpc_context *rpc _U_, int status, void *command free_nfs_cb_data(data); } -static void nfs_pwrite_mcb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_pwrite_mcb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { struct nfs_mcb_data *mdata = private_data; struct nfs_cb_data *data = mdata->data; struct nfs_context *nfs = data->nfs; WRITE3res *res; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + data->num_calls--; if (status == RPC_STATUS_ERROR) { @@ -1242,12 +1277,14 @@ int nfs_fstat_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, voi /* * Async fsync() */ -static void nfs_fsync_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_fsync_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; COMMIT3res *res; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -1300,12 +1337,14 @@ int nfs_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, voi /* * Async ftruncate() */ -static void nfs_ftruncate_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_ftruncate_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; SETATTR3res *res; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -1401,13 +1440,15 @@ int nfs_truncate_async(struct nfs_context *nfs, const char *path, uint64_t lengt /* * Async mkdir() */ -static void nfs_mkdir_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_mkdir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { MKDIR3res *res; struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; char *str = data->continue_data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + str = &str[strlen(str) + 1]; if (status == RPC_STATUS_ERROR) { @@ -1490,13 +1531,15 @@ int nfs_mkdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void * /* * Async rmdir() */ -static void nfs_rmdir_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_rmdir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { RMDIR3res *res; struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; char *str = data->continue_data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + str = &str[strlen(str) + 1]; if (status == RPC_STATUS_ERROR) { @@ -1570,7 +1613,7 @@ int nfs_rmdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void * /* * Async creat() */ -static void nfs_create_2_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_create_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { LOOKUP3res *res; struct nfs_cb_data *data = private_data; @@ -1578,6 +1621,8 @@ static void nfs_create_2_cb(struct rpc_context *rpc _U_, int status, void *comma struct nfsfh *nfsfh; char *str = data->continue_data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -1618,13 +1663,15 @@ static void nfs_create_2_cb(struct rpc_context *rpc _U_, int status, void *comma -static void nfs_creat_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_creat_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { CREATE3res *res; struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; char *str = data->continue_data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -1712,13 +1759,15 @@ int nfs_creat_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb /* * Async unlink() */ -static void nfs_unlink_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_unlink_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { REMOVE3res *res; struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; char *str = data->continue_data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + str = &str[strlen(str) + 1]; if (status == RPC_STATUS_ERROR) { @@ -1805,13 +1854,15 @@ static void free_mknod_cb_data(void *ptr) free(data); } -static void nfs_mknod_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_mknod_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { MKNOD3res *res; struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; char *str = data->continue_data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + str = &str[strlen(str) + 1]; if (status == RPC_STATUS_ERROR) { @@ -1909,7 +1960,7 @@ struct rdpe_lookup_cb_data { }; /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */ -static void nfs_opendir3_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_opendir3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { LOOKUP3res *res = command_data; struct rdpe_lookup_cb_data *rdpe_lookup_cb_data = private_data; @@ -1919,6 +1970,8 @@ static void nfs_opendir3_cb(struct rpc_context *rpc _U_, int status, void *comma struct nfs_context *nfs = data->nfs; struct nfsdirent *nfsdirent = rdpe_lookup_cb_data->nfsdirent; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + free(rdpe_lookup_cb_data); rdpe_cb_data->getattrcount--; @@ -1963,7 +2016,7 @@ static void nfs_opendir3_cb(struct rpc_context *rpc _U_, int status, void *comma } } -static void nfs_opendir2_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_opendir2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { READDIR3res *res = command_data; struct nfs_cb_data *data = private_data; @@ -1974,6 +2027,8 @@ static void nfs_opendir2_cb(struct rpc_context *rpc _U_, int status, void *comma uint64_t cookie; struct rdpe_cb_data *rdpe_cb_data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); nfs_free_nfsdir(nfsdir); @@ -2078,7 +2133,7 @@ static void nfs_opendir2_cb(struct rpc_context *rpc _U_, int status, void *comma } -static void nfs_opendir_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_opendir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { READDIRPLUS3res *res = command_data; struct nfs_cb_data *data = private_data; @@ -2087,6 +2142,7 @@ static void nfs_opendir_cb(struct rpc_context *rpc _U_, int status, void *comman struct entryplus3 *entry; uint64_t cookie; + assert(rpc->magic == RPC_CONTEXT_MAGIC); if (status == RPC_STATUS_ERROR || (status == RPC_STATUS_SUCCESS && res->status == NFS3ERR_NOTSUPP) ){ cookieverf3 cv; @@ -2248,12 +2304,14 @@ struct lseek_cb_data { void *private_data; }; -static void nfs_lseek_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_lseek_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { GETATTR3res *res; struct lseek_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free(data); @@ -2319,13 +2377,15 @@ int nfs_lseek_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offse /* * Async statvfs() */ -static void nfs_statvfs_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_statvfs_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { FSSTAT3res *res; struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; struct statvfs svfs; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -2388,12 +2448,14 @@ int nfs_statvfs_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void /* * Async readlink() */ -static void nfs_readlink_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_readlink_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { READLINK3res *res; struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -2450,12 +2512,14 @@ int nfs_readlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, voi /* * Async chmod() */ -static void nfs_chmod_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_chmod_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; SETATTR3res *res; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -2548,12 +2612,14 @@ int nfs_fchmod_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode, nfs /* * Async chown() */ -static void nfs_chown_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_chown_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; SETATTR3res *res; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -2684,12 +2750,14 @@ int nfs_fchown_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int uid, int /* * Async utimes() */ -static void nfs_utimes_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_utimes_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; SETATTR3res *res; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -2800,13 +2868,15 @@ int nfs_utime_async(struct nfs_context *nfs, const char *path, struct utimbuf *t /* * Async access() */ -static void nfs_access_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_access_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { ACCESS3res *res; struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; unsigned int nfsmode = 0; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -2913,13 +2983,15 @@ static void free_nfs_symlink_data(void *mem) free(data); } -static void nfs_symlink_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_symlink_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { SYMLINK3res *res; struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; struct nfs_symlink_data *symlink_data = data->continue_data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -3048,13 +3120,15 @@ static void free_nfs_rename_data(void *mem) free(data); } -static void nfs_rename_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_rename_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { RENAME3res *res; struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; struct nfs_rename_data *rename_data = data->continue_data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -3204,13 +3278,15 @@ static void free_nfs_link_data(void *mem) free(data); } -static void nfs_link_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) +static void nfs_link_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) { LINK3res *res; struct nfs_cb_data *data = private_data; struct nfs_context *nfs = data->nfs; struct nfs_link_data *link_data = data->continue_data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(-EFAULT, nfs, command_data, data->private_data); free_nfs_cb_data(data); @@ -3385,7 +3461,9 @@ static void mount_export_5_cb(struct rpc_context *rpc, int status, void *command { struct mount_cb_data *data = private_data; - if (status == RPC_STATUS_ERROR) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + + if (status == RPC_STATUS_ERROR) { data->cb(rpc, -EFAULT, command_data, data->private_data); free_mount_cb_data(data); return; @@ -3407,6 +3485,8 @@ static void mount_export_4_cb(struct rpc_context *rpc, int status, void *command { struct mount_cb_data *data = private_data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + /* Dont want any more callbacks even if the socket is closed */ rpc->connect_cb = NULL; @@ -3433,7 +3513,9 @@ static void mount_export_3_cb(struct rpc_context *rpc, int status, void *command struct mount_cb_data *data = private_data; uint32_t mount_port; - if (status == RPC_STATUS_ERROR) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + + if (status == RPC_STATUS_ERROR) { data->cb(rpc, -EFAULT, command_data, data->private_data); free_mount_cb_data(data); return; @@ -3464,6 +3546,8 @@ static void mount_export_2_cb(struct rpc_context *rpc, int status, void *command { struct mount_cb_data *data = private_data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status == RPC_STATUS_ERROR) { data->cb(rpc, -EFAULT, command_data, data->private_data); free_mount_cb_data(data); @@ -3486,6 +3570,8 @@ static void mount_export_1_cb(struct rpc_context *rpc, int status, void *command { struct mount_cb_data *data = private_data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + /* Dont want any more callbacks even if the socket is closed */ rpc->connect_cb = NULL; @@ -3511,6 +3597,8 @@ int mount_getexports_async(struct rpc_context *rpc, const char *server, rpc_cb c { struct mount_cb_data *data; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + data = malloc(sizeof(struct mount_cb_data)); if (data == NULL) { return -1; @@ -3533,6 +3621,7 @@ int mount_getexports_async(struct rpc_context *rpc, const char *server, rpc_cb c struct rpc_context *nfs_get_rpc_context(struct nfs_context *nfs) { + assert(nfs->rpc->magic == RPC_CONTEXT_MAGIC); return nfs->rpc; } diff --git a/lib/pdu.c b/lib/pdu.c index 276515e..abb19f2 100644 --- a/lib/pdu.c +++ b/lib/pdu.c @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -39,9 +40,7 @@ struct rpc_pdu *rpc_allocate_pdu(struct rpc_context *rpc, int program, int versi struct rpc_pdu *pdu; struct rpc_msg msg; - if (rpc == NULL) { - return NULL; - } + assert(rpc->magic == RPC_CONTEXT_MAGIC); pdu = malloc(sizeof(struct rpc_pdu)); if (pdu == NULL) { @@ -80,8 +79,10 @@ struct rpc_pdu *rpc_allocate_pdu(struct rpc_context *rpc, int program, int versi return pdu; } -void rpc_free_pdu(struct rpc_context *rpc _U_, struct rpc_pdu *pdu) +void rpc_free_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (pdu->outdata.data != NULL) { free(pdu->outdata.data); pdu->outdata.data = NULL; @@ -103,6 +104,8 @@ int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu) { int size, recordmarker; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + size = xdr_getpos(&pdu->xdr); /* for udp we dont queue, we just send it straight away */ @@ -148,6 +151,8 @@ static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, XDR * { struct rpc_msg msg; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + memset(&msg, 0, sizeof(struct rpc_msg)); msg.acpted_rply.ar_verf = _null_auth; if (pdu->xdr_decode_bufsize > 0) { @@ -213,6 +218,8 @@ int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size) unsigned int xid; char *reasbuf = NULL; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + memset(&xdr, 0, sizeof(XDR)); xdrmem_create(&xdr, buf, size, XDR_DECODE); diff --git a/lib/socket.c b/lib/socket.c index 8ad5e4e..413ebbe 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -30,6 +30,7 @@ #endif #include #include +#include #include #include #include @@ -70,12 +71,18 @@ static void set_nonblocking(int fd) int rpc_get_fd(struct rpc_context *rpc) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + return rpc->fd; } int rpc_which_events(struct rpc_context *rpc) { - int events = rpc->is_connected ? POLLIN : POLLOUT; + int events; + + assert(rpc->magic == RPC_CONTEXT_MAGIC); + + events = rpc->is_connected ? POLLIN : POLLOUT; if (rpc->is_udp != 0) { /* for udp sockets we only wait for pollin */ @@ -92,9 +99,8 @@ static int rpc_write_to_socket(struct rpc_context *rpc) { int64_t count; - if (rpc == NULL) { - return -1; - } + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (rpc->fd == -1) { rpc_set_error(rpc, "trying to write but not connected"); return -1; @@ -136,6 +142,8 @@ static int rpc_read_from_socket(struct rpc_context *rpc) int pdu_size; int64_t count; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + #if defined(WIN32) if (ioctlsocket(rpc->fd, FIONREAD, &available) != 0) { #else @@ -258,6 +266,8 @@ static int rpc_read_from_socket(struct rpc_context *rpc) int rpc_service(struct rpc_context *rpc, int revents) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (revents & POLLERR) { #ifdef WIN32 char err = 0; @@ -336,11 +346,15 @@ int rpc_service(struct rpc_context *rpc, int revents) void rpc_set_autoreconnect(struct rpc_context *rpc) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + rpc->auto_reconnect = 1; } void rpc_unset_autoreconnect(struct rpc_context *rpc) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + rpc->auto_reconnect = 0; } @@ -348,6 +362,8 @@ static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_s { int socksize; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + switch (s->ss_family) { case AF_INET: socksize = sizeof(struct sockaddr_in); @@ -424,6 +440,8 @@ int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc { struct sockaddr_in *sin = (struct sockaddr_in *)&rpc->s; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (rpc->fd != -1) { rpc_set_error(rpc, "Trying to connect while already connected"); return -1; @@ -464,6 +482,8 @@ int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc int rpc_disconnect(struct rpc_context *rpc, char *error) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + rpc_unset_autoreconnect(rpc); if (rpc->fd != -1) { @@ -484,6 +504,8 @@ int rpc_disconnect(struct rpc_context *rpc, char *error) static void reconnect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (status != RPC_STATUS_SUCCESS) { rpc_error_all_pdus(rpc, "RPC ERROR: Failed to reconnect async"); return; @@ -498,6 +520,8 @@ static int rpc_reconnect_requeue(struct rpc_context *rpc) { struct rpc_pdu *pdu; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (rpc->fd != -1) { #if defined(WIN32) closesocket(rpc->fd); @@ -537,6 +561,8 @@ int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port) struct addrinfo *ai = NULL; char service[6]; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (rpc->is_udp == 0) { rpc_set_error(rpc, "Cant not bind UDP. Not UDP context"); return -1; @@ -580,6 +606,8 @@ int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int i struct addrinfo *ai = NULL; char service[6]; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (rpc->is_udp == 0) { rpc_set_error(rpc, "Can not set destination sockaddr. Not UDP context"); return -1; @@ -613,6 +641,8 @@ int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int i struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + return (struct sockaddr *)&rpc->udp_src; } @@ -621,6 +651,8 @@ int rpc_queue_length(struct rpc_context *rpc) int i=0; struct rpc_pdu *pdu; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + for(pdu = rpc->outqueue; pdu; pdu = pdu->next) { i++; }