Ensure the next pointer is correct
[deb_libnfs.git] / lib / init.c
CommitLineData
84004dbf
RS
1/*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
2606f9bb 4
84004dbf
RS
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*/
00748f36
RS
14#ifdef HAVE_CONFIG_H
15#include "config.h"
16#endif
84004dbf 17
108c622a
RS
18#ifdef AROS
19#include "aros_compat.h"
20#endif
21
a8a1b858
M
22#ifdef WIN32
23#include "win32_compat.h"
bff8fe46
RS
24#endif
25
a8a1b858 26#define _GNU_SOURCE
eecdc4f3 27
00748f36
RS
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
31
bff8fe46
RS
32#ifdef HAVE_STRINGS_H
33#include <strings.h>
34#endif
35
84004dbf
RS
36#include <stdio.h>
37#include <stdarg.h>
84004dbf 38#include <string.h>
98f5fee8 39#include <stdlib.h>
f3a75078 40#include <assert.h>
a2756193 41#include <time.h>
84004dbf 42#include "slist.h"
763cd6e3 43#include "libnfs-zdr.h"
84004dbf
RS
44#include "libnfs.h"
45#include "libnfs-raw.h"
46#include "libnfs-private.h"
47
48struct rpc_context *rpc_init_context(void)
49{
50 struct rpc_context *rpc;
a2756193 51 static uint32_t salt = 0;
84004dbf
RS
52
53 rpc = malloc(sizeof(struct rpc_context));
54 if (rpc == NULL) {
84004dbf
RS
55 return NULL;
56 }
ea98629a 57 memset(rpc, 0, sizeof(struct rpc_context));
84004dbf 58
f3a75078 59 rpc->magic = RPC_CONTEXT_MAGIC;
35280fd7
RS
60
61 /* Allow 1M of data (for writes) and some */
62 rpc->encodebuflen = 1024 * 1024 + 4096;
84004dbf
RS
63 rpc->encodebuf = malloc(rpc->encodebuflen);
64 if (rpc->encodebuf == NULL) {
84004dbf
RS
65 free(rpc);
66 return NULL;
67 }
68
eecdc4f3 69 rpc->auth = authunix_create_default();
84004dbf 70 if (rpc->auth == NULL) {
84004dbf
RS
71 free(rpc->encodebuf);
72 free(rpc);
73 return NULL;
74 }
6fda9871 75 rpc->xid = salt + time(NULL) + getpid() << 16;
a2756193 76 salt += 0x01000000;
84004dbf 77 rpc->fd = -1;
1c8b4547 78 rpc->tcp_syncnt = RPC_PARAM_UNDEFINED;
3af0c022 79#if defined(WIN32) || defined(ANDROID)
9126c9c0
PL
80 rpc->uid = 65534;
81 rpc->gid = 65534;
82#else
83 rpc->uid = getuid();
84 rpc->gid = getgid();
85#endif
aec45c62
MH
86 rpc_reset_queue(&rpc->outqueue);
87 rpc_reset_queue(&rpc->waitpdu);
84004dbf
RS
88
89 return rpc;
90}
91
92
9e00b8c6
RS
93struct rpc_context *rpc_init_udp_context(void)
94{
95 struct rpc_context *rpc;
96
97 rpc = rpc_init_context();
98 if (rpc != NULL) {
99 rpc->is_udp = 1;
100 }
101
102 return rpc;
103}
104
67ba2239 105void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
84004dbf 106{
f3a75078
RS
107 assert(rpc->magic == RPC_CONTEXT_MAGIC);
108
84004dbf
RS
109 if (rpc->auth != NULL) {
110 auth_destroy(rpc->auth);
111 }
8704724f 112 rpc->auth = auth;
84004dbf
RS
113}
114
9126c9c0
PL
115static void rpc_set_uid_gid(struct rpc_context *rpc, int uid, int gid) {
116 if (uid != rpc->uid || gid != rpc->gid) {
117 struct AUTH *auth = libnfs_authunix_create("libnfs", uid, gid, 0, NULL);
118 if (auth != NULL) {
119 rpc_set_auth(rpc, auth);
120 rpc->uid = uid;
121 rpc->gid = gid;
122 }
123 }
124}
125
126void rpc_set_uid(struct rpc_context *rpc, int uid) {
127 rpc_set_uid_gid(rpc, uid, rpc->gid);
128}
129
130void rpc_set_gid(struct rpc_context *rpc, int gid) {
131 rpc_set_uid_gid(rpc, rpc->uid, gid);
132}
84004dbf
RS
133
134void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
135{
136 va_list ap;
f0cb8042 137 char *old_error_string = rpc->error_string;
84004dbf 138
f3a75078
RS
139 assert(rpc->magic == RPC_CONTEXT_MAGIC);
140
84004dbf 141 va_start(ap, error_string);
b85c7de2
RS
142 rpc->error_string = malloc(1024);
143 vsnprintf(rpc->error_string, 1024, error_string, ap);
84004dbf 144 va_end(ap);
f0cb8042
RS
145
146 if (old_error_string != NULL) {
147 free(old_error_string);
148 }
84004dbf
RS
149}
150
151char *rpc_get_error(struct rpc_context *rpc)
152{
f3a75078
RS
153 assert(rpc->magic == RPC_CONTEXT_MAGIC);
154
84004dbf
RS
155 return rpc->error_string;
156}
157
158void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
159{
aec45c62 160 struct rpc_pdu *pdu, *next;
84004dbf 161
f3a75078
RS
162 assert(rpc->magic == RPC_CONTEXT_MAGIC);
163
aec45c62 164 while ((pdu = rpc->outqueue.head) != NULL) {
84004dbf 165 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
aec45c62 166 rpc->outqueue.head = pdu->next;
84004dbf
RS
167 rpc_free_pdu(rpc, pdu);
168 }
aec45c62
MH
169 rpc->outqueue.tail = NULL;
170
171 while((pdu = rpc->waitpdu.head) != NULL) {
84004dbf 172 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
aec45c62 173 rpc->waitpdu.head = pdu->next;
84004dbf
RS
174 rpc_free_pdu(rpc, pdu);
175 }
aec45c62 176 rpc->waitpdu.tail = NULL;
84004dbf
RS
177}
178
d678b73e
RS
179static void rpc_free_fragment(struct rpc_fragment *fragment)
180{
181 if (fragment->data != NULL) {
182 free(fragment->data);
183 }
184 free(fragment);
185}
186
187void rpc_free_all_fragments(struct rpc_context *rpc)
188{
f3a75078
RS
189 assert(rpc->magic == RPC_CONTEXT_MAGIC);
190
d678b73e
RS
191 while (rpc->fragments != NULL) {
192 struct rpc_fragment *fragment = rpc->fragments;
193
aec45c62 194 rpc->fragments = fragment->next;
d678b73e
RS
195 rpc_free_fragment(fragment);
196 }
197}
198
183451cf 199int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
d678b73e
RS
200{
201 struct rpc_fragment *fragment;
202
f3a75078
RS
203 assert(rpc->magic == RPC_CONTEXT_MAGIC);
204
d678b73e
RS
205 fragment = malloc(sizeof(struct rpc_fragment));
206 if (fragment == NULL) {
207 return -1;
208 }
209
210 fragment->size = size;
211 fragment->data = malloc(fragment->size);
212 if(fragment->data == NULL) {
213 free(fragment);
214 return -1;
215 }
216
217 memcpy(fragment->data, data, fragment->size);
218 SLIST_ADD_END(&rpc->fragments, fragment);
219 return 0;
220}
84004dbf
RS
221
222void rpc_destroy_context(struct rpc_context *rpc)
223{
224 struct rpc_pdu *pdu;
225
f3a75078
RS
226 assert(rpc->magic == RPC_CONTEXT_MAGIC);
227
aec45c62 228 while((pdu = rpc->outqueue.head) != NULL) {
84004dbf 229 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
aec45c62 230 rpc->outqueue.head = pdu->next;
84004dbf
RS
231 rpc_free_pdu(rpc, pdu);
232 }
aec45c62 233 while((pdu = rpc->waitpdu.head) != NULL) {
84004dbf 234 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
aec45c62 235 rpc->outqueue.head = pdu->next;
84004dbf
RS
236 rpc_free_pdu(rpc, pdu);
237 }
238
d678b73e
RS
239 rpc_free_all_fragments(rpc);
240
84004dbf
RS
241 auth_destroy(rpc->auth);
242 rpc->auth =NULL;
243
244 if (rpc->fd != -1) {
eecdc4f3 245 close(rpc->fd);
84004dbf
RS
246 }
247
248 if (rpc->encodebuf != NULL) {
249 free(rpc->encodebuf);
250 rpc->encodebuf = NULL;
251 }
252
253 if (rpc->error_string != NULL) {
254 free(rpc->error_string);
255 rpc->error_string = NULL;
256 }
257
7ff2f3a0
RS
258 if (rpc->udp_dest != NULL) {
259 free(rpc->udp_dest);
260 rpc->udp_dest = NULL;
261 }
262
f3a75078 263 rpc->magic = 0;
84004dbf
RS
264 free(rpc);
265}
266
267