Permalink
Browse files

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

…een destroyed
  • Loading branch information...
sahlberg committed Nov 22, 2012
1 parent 0222cbf commit 4a2b08768a0d9ec9f5604da76f8e41263f20b821
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
@@ -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;
View
@@ -24,6 +24,7 @@
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
+#include <assert.h>
#include <rpc/rpc.h>
#include <rpc/xdr.h>
#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,13 +107,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);
@@ -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);
}
View
@@ -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>
@@ -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];
Oops, something went wrong.

0 comments on commit 4a2b087

Please sign in to comment.