Browse files

Add an assert to track if we try to use an rpc_context after it has b…

…een destroyed
  • Loading branch information...
1 parent 31a2602 commit f3a75078066dd8022e1e52d9cce2256b4fc323d2 @sahlberg committed Nov 24, 2012
Showing with 200 additions and 39 deletions.
  1. +3 −0 include/libnfs-private.h
  2. +17 −0 lib/init.c
  3. +13 −0 lib/libnfs-sync.c
  4. +120 −31 lib/libnfs.c
  5. +11 −4 lib/pdu.c
  6. +36 −4 lib/socket.c
View
3 include/libnfs-private.h
@@ -24,7 +24,10 @@ struct rpc_fragment {
char *data;
};
+#define RPC_CONTEXT_MAGIC 0xc6e46435
+
struct rpc_context {
+ uint32_t magic;
int fd;
int is_connected;
View
17 lib/init.c
@@ -24,6 +24,7 @@
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
+#include <assert.h>
#include "slist.h"
#include "libnfs-zdr.h"
#include "libnfs.h"
@@ -40,6 +41,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) {
@@ -78,6 +80,8 @@ struct rpc_context *rpc_init_udp_context(void)
void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
{
+ assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
if (rpc->auth != NULL) {
auth_destroy(rpc->auth);
}
@@ -89,6 +93,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);
}
@@ -100,13 +106,17 @@ 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;
}
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);
@@ -129,6 +139,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;
@@ -141,6 +153,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;
@@ -162,6 +176,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);
@@ -201,6 +217,7 @@ void rpc_destroy_context(struct rpc_context *rpc)
rpc->udp_dest = NULL;
}
+ rpc->magic = 0;
free(rpc);
}
View
13 lib/libnfs-sync.c
@@ -38,6 +38,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
@@ -67,6 +68,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);
@@ -135,6 +138,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) {
@@ -1149,6 +1154,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;
@@ -1230,6 +1237,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;
}
@@ -1283,6 +1292,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;
@@ -1413,6 +1424,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];
View
151 lib/libnfs.c
@@ -33,6 +33,7 @@
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
+#include <assert.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -218,11 +219,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);
@@ -244,6 +247,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);
@@ -270,6 +275,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);
@@ -294,6 +301,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;
@@ -322,6 +331,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);
@@ -367,6 +378,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);
@@ -390,6 +403,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;
@@ -417,6 +432,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);
@@ -450,6 +467,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);
@@ -473,6 +492,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;
@@ -537,12 +558,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);
@@ -656,13 +679,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);
@@ -736,14 +761,16 @@ 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;
struct nfs_context *nfs = data->nfs;
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);
@@ -849,12 +876,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);
@@ -879,13 +908,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) {
@@ -1026,12 +1057,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);
@@ -1056,13 +1089,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) {
@@ -1244,12 +1279,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);
@@ -1302,12 +1339,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);
@@ -1403,13 +1442,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) {
@@ -1492,13 +1533,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) {
@@ -1572,14 +1615,16 @@ 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;
struct nfs_context *nfs = data->nfs;
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);
@@ -1620,13 +1665,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);
@@ -1714,13 +1761,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) {
@@ -1807,13 +1856,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) {
@@ -1911,7 +1962,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;
@@ -1921,6 +1972,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--;
@@ -1965,7 +2018,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;
@@ -1976,6 +2029,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);
@@ -2080,7 +2135,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;
@@ -2089,6 +2144,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;
@@ -2250,12 +2306,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);
@@ -2321,13 +2379,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);
@@ -2390,12 +2450,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);
@@ -2452,12 +2514,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);
@@ -2550,12 +2614,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);
@@ -2686,12 +2752,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);
@@ -2802,13 +2870,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);
@@ -2915,13 +2985,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);
@@ -3050,13 +3122,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);
@@ -3206,13 +3280,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);
@@ -3387,7 +3463,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;
@@ -3409,6 +3487,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;
@@ -3435,7 +3515,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;
@@ -3466,6 +3548,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);
@@ -3488,6 +3572,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;
@@ -3513,6 +3599,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;
@@ -3535,6 +3623,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;
}
View
15 lib/pdu.c
@@ -28,6 +28,7 @@
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
+#include <assert.h>
#include <errno.h>
#include "slist.h"
#include "libnfs-zdr.h"
@@ -40,9 +41,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) {
@@ -81,8 +80,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;
@@ -104,6 +105,8 @@ int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
{
int size, recordmarker;
+ assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
size = zdr_getpos(&pdu->zdr);
/* for udp we dont queue, we just send it straight away */
@@ -150,6 +153,8 @@ static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, ZDR *
{
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->zdr_decode_bufsize > 0) {
@@ -215,6 +220,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(&zdr, 0, sizeof(ZDR));
zdrmem_create(&zdr, buf, size, ZDR_DECODE);
View
40 lib/socket.c
@@ -30,6 +30,7 @@
#endif
#include <stdio.h>
#include <stdlib.h>
+#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
@@ -69,12 +70,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 */
@@ -91,9 +98,8 @@ static int rpc_write_to_socket(struct rpc_context *rpc)
{
int32_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;
@@ -135,6 +141,8 @@ static int rpc_read_from_socket(struct rpc_context *rpc)
int pdu_size;
int32_t count;
+ assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
#if defined(WIN32)
if (ioctlsocket(rpc->fd, FIONREAD, &available) != 0) {
#else
@@ -257,6 +265,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;
@@ -335,18 +345,24 @@ 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;
}
static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_storage *s)
{
int socksize;
+ assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
switch (s->ss_family) {
case AF_INET:
socksize = sizeof(struct sockaddr_in);
@@ -423,6 +439,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;
@@ -463,6 +481,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) {
@@ -483,6 +503,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;
@@ -497,6 +519,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);
@@ -536,6 +560,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;
@@ -579,6 +605,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;
@@ -612,6 +640,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;
}
@@ -620,6 +650,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++;
}

0 comments on commit f3a7507

Please sign in to comment.