Add an assert to track if we try to use an rpc_context after it has been destroyed
[deb_libnfs.git] / lib / init.c
1 /*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
4
5
6 This program is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 GNU Lesser General Public License for more details.
10
11 You should have received a copy of the GNU Lesser General Public License
12 along with this program; if not, see <http://www.gnu.org/licenses/>.
13 */
14
15 #ifdef WIN32
16 #include "win32_compat.h"
17 #else
18 #include <unistd.h>
19 #include <strings.h>
20 #endif/*WIN32*/
21 #define _GNU_SOURCE
22
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <rpc/rpc.h>
29 #include <rpc/xdr.h>
30 #include "slist.h"
31 #include "libnfs.h"
32 #include "libnfs-raw.h"
33 #include "libnfs-private.h"
34
35 struct rpc_context *rpc_init_context(void)
36 {
37 struct rpc_context *rpc;
38
39 rpc = malloc(sizeof(struct rpc_context));
40 if (rpc == NULL) {
41 return NULL;
42 }
43 memset(rpc, 0, sizeof(struct rpc_context));
44
45 rpc->magic = RPC_CONTEXT_MAGIC;
46 rpc->encodebuflen = 65536;
47 rpc->encodebuf = malloc(rpc->encodebuflen);
48 if (rpc->encodebuf == NULL) {
49 free(rpc);
50 return NULL;
51 }
52
53 #if defined(WIN32)
54 rpc->auth = authunix_create("LibNFS", 65535, 65535, 0, NULL);
55 #else
56 rpc->auth = authunix_create_default();
57 #endif
58 if (rpc->auth == NULL) {
59 free(rpc->encodebuf);
60 free(rpc);
61 return NULL;
62 }
63 rpc->xid = 1;
64 rpc->fd = -1;
65
66 return rpc;
67 }
68
69
70 struct rpc_context *rpc_init_udp_context(void)
71 {
72 struct rpc_context *rpc;
73
74 rpc = rpc_init_context();
75 if (rpc != NULL) {
76 rpc->is_udp = 1;
77 }
78
79 return rpc;
80 }
81
82 void rpc_set_auth(struct rpc_context *rpc, AUTH *auth)
83 {
84 assert(rpc->magic == RPC_CONTEXT_MAGIC);
85
86 if (rpc->auth != NULL) {
87 auth_destroy(rpc->auth);
88 }
89 rpc->auth = auth;
90 }
91
92
93 void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
94 {
95 va_list ap;
96
97 assert(rpc->magic == RPC_CONTEXT_MAGIC);
98
99 if (rpc->error_string != NULL) {
100 free(rpc->error_string);
101 }
102 va_start(ap, error_string);
103 rpc->error_string = malloc(1024);
104 vsnprintf(rpc->error_string, 1024, error_string, ap);
105 va_end(ap);
106 }
107
108 char *rpc_get_error(struct rpc_context *rpc)
109 {
110 assert(rpc->magic == RPC_CONTEXT_MAGIC);
111
112 return rpc->error_string;
113 }
114
115 void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
116 {
117 struct rpc_pdu *pdu;
118
119 assert(rpc->magic == RPC_CONTEXT_MAGIC);
120
121 while((pdu = rpc->outqueue) != NULL) {
122 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
123 SLIST_REMOVE(&rpc->outqueue, pdu);
124 rpc_free_pdu(rpc, pdu);
125 }
126 while((pdu = rpc->waitpdu) != NULL) {
127 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
128 SLIST_REMOVE(&rpc->waitpdu, pdu);
129 rpc_free_pdu(rpc, pdu);
130 }
131 }
132
133 static void rpc_free_fragment(struct rpc_fragment *fragment)
134 {
135 if (fragment->data != NULL) {
136 free(fragment->data);
137 }
138 free(fragment);
139 }
140
141 void rpc_free_all_fragments(struct rpc_context *rpc)
142 {
143 assert(rpc->magic == RPC_CONTEXT_MAGIC);
144
145 while (rpc->fragments != NULL) {
146 struct rpc_fragment *fragment = rpc->fragments;
147
148 SLIST_REMOVE(&rpc->fragments, fragment);
149 rpc_free_fragment(fragment);
150 }
151 }
152
153 int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
154 {
155 struct rpc_fragment *fragment;
156
157 assert(rpc->magic == RPC_CONTEXT_MAGIC);
158
159 fragment = malloc(sizeof(struct rpc_fragment));
160 if (fragment == NULL) {
161 return -1;
162 }
163
164 fragment->size = size;
165 fragment->data = malloc(fragment->size);
166 if(fragment->data == NULL) {
167 free(fragment);
168 return -1;
169 }
170
171 memcpy(fragment->data, data, fragment->size);
172 SLIST_ADD_END(&rpc->fragments, fragment);
173 return 0;
174 }
175
176 void rpc_destroy_context(struct rpc_context *rpc)
177 {
178 struct rpc_pdu *pdu;
179
180 assert(rpc->magic == RPC_CONTEXT_MAGIC);
181
182 while((pdu = rpc->outqueue) != NULL) {
183 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
184 SLIST_REMOVE(&rpc->outqueue, pdu);
185 rpc_free_pdu(rpc, pdu);
186 }
187 while((pdu = rpc->waitpdu) != NULL) {
188 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
189 SLIST_REMOVE(&rpc->waitpdu, pdu);
190 rpc_free_pdu(rpc, pdu);
191 }
192
193 rpc_free_all_fragments(rpc);
194
195 auth_destroy(rpc->auth);
196 rpc->auth =NULL;
197
198 if (rpc->fd != -1) {
199 #if defined(WIN32)
200 closesocket(rpc->fd);
201 #else
202 close(rpc->fd);
203 #endif
204 }
205
206 if (rpc->encodebuf != NULL) {
207 free(rpc->encodebuf);
208 rpc->encodebuf = NULL;
209 }
210
211 if (rpc->error_string != NULL) {
212 free(rpc->error_string);
213 rpc->error_string = NULL;
214 }
215
216 if (rpc->udp_dest != NULL) {
217 free(rpc->udp_dest);
218 rpc->udp_dest = NULL;
219 }
220
221 rpc->magic = 0;
222 free(rpc);
223 }
224
225