slist.h: Change SLIST_* to LIBNFS_LIST_* to avoid name clash on *BSD
[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;
63f36a09 52 unsigned int i;
84004dbf
RS
53
54 rpc = malloc(sizeof(struct rpc_context));
55 if (rpc == NULL) {
84004dbf
RS
56 return NULL;
57 }
ea98629a 58 memset(rpc, 0, sizeof(struct rpc_context));
84004dbf 59
f3a75078 60 rpc->magic = RPC_CONTEXT_MAGIC;
35280fd7
RS
61
62 /* Allow 1M of data (for writes) and some */
63 rpc->encodebuflen = 1024 * 1024 + 4096;
84004dbf
RS
64 rpc->encodebuf = malloc(rpc->encodebuflen);
65 if (rpc->encodebuf == NULL) {
84004dbf
RS
66 free(rpc);
67 return NULL;
68 }
69
eecdc4f3 70 rpc->auth = authunix_create_default();
84004dbf 71 if (rpc->auth == NULL) {
84004dbf
RS
72 free(rpc->encodebuf);
73 free(rpc);
74 return NULL;
75 }
6fda9871 76 rpc->xid = salt + time(NULL) + getpid() << 16;
a2756193 77 salt += 0x01000000;
84004dbf 78 rpc->fd = -1;
1c8b4547 79 rpc->tcp_syncnt = RPC_PARAM_UNDEFINED;
3af0c022 80#if defined(WIN32) || defined(ANDROID)
9126c9c0
PL
81 rpc->uid = 65534;
82 rpc->gid = 65534;
83#else
84 rpc->uid = getuid();
85 rpc->gid = getgid();
86#endif
aec45c62 87 rpc_reset_queue(&rpc->outqueue);
63f36a09
MH
88 for (i = 0; i < HASHES; i++)
89 rpc_reset_queue(&rpc->waitpdu[i]);
84004dbf
RS
90
91 return rpc;
92}
93
94
9e00b8c6
RS
95struct rpc_context *rpc_init_udp_context(void)
96{
97 struct rpc_context *rpc;
98
99 rpc = rpc_init_context();
100 if (rpc != NULL) {
101 rpc->is_udp = 1;
102 }
103
104 return rpc;
105}
106
67ba2239 107void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
84004dbf 108{
f3a75078
RS
109 assert(rpc->magic == RPC_CONTEXT_MAGIC);
110
84004dbf
RS
111 if (rpc->auth != NULL) {
112 auth_destroy(rpc->auth);
113 }
8704724f 114 rpc->auth = auth;
84004dbf
RS
115}
116
9126c9c0
PL
117static void rpc_set_uid_gid(struct rpc_context *rpc, int uid, int gid) {
118 if (uid != rpc->uid || gid != rpc->gid) {
119 struct AUTH *auth = libnfs_authunix_create("libnfs", uid, gid, 0, NULL);
120 if (auth != NULL) {
121 rpc_set_auth(rpc, auth);
122 rpc->uid = uid;
123 rpc->gid = gid;
124 }
125 }
126}
127
128void rpc_set_uid(struct rpc_context *rpc, int uid) {
129 rpc_set_uid_gid(rpc, uid, rpc->gid);
130}
131
132void rpc_set_gid(struct rpc_context *rpc, int gid) {
133 rpc_set_uid_gid(rpc, rpc->uid, gid);
134}
84004dbf
RS
135
136void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
137{
138 va_list ap;
f0cb8042 139 char *old_error_string = rpc->error_string;
84004dbf 140
f3a75078
RS
141 assert(rpc->magic == RPC_CONTEXT_MAGIC);
142
84004dbf 143 va_start(ap, error_string);
b85c7de2
RS
144 rpc->error_string = malloc(1024);
145 vsnprintf(rpc->error_string, 1024, error_string, ap);
84004dbf 146 va_end(ap);
f0cb8042
RS
147
148 if (old_error_string != NULL) {
149 free(old_error_string);
150 }
84004dbf
RS
151}
152
153char *rpc_get_error(struct rpc_context *rpc)
154{
f3a75078
RS
155 assert(rpc->magic == RPC_CONTEXT_MAGIC);
156
84004dbf
RS
157 return rpc->error_string;
158}
159
160void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
161{
aec45c62 162 struct rpc_pdu *pdu, *next;
63f36a09 163 unsigned int i;
84004dbf 164
f3a75078
RS
165 assert(rpc->magic == RPC_CONTEXT_MAGIC);
166
aec45c62 167 while ((pdu = rpc->outqueue.head) != NULL) {
84004dbf 168 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
aec45c62 169 rpc->outqueue.head = pdu->next;
84004dbf
RS
170 rpc_free_pdu(rpc, pdu);
171 }
aec45c62
MH
172 rpc->outqueue.tail = NULL;
173
63f36a09
MH
174 for (i = 0; i < HASHES; i++) {
175 struct rpc_queue *q = &rpc->waitpdu[i];
176
177 while((pdu = q->head) != NULL) {
178 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
179 q->head = pdu->next;
180 rpc_free_pdu(rpc, pdu);
181 }
182 q->tail = NULL;
84004dbf
RS
183 }
184}
185
d678b73e
RS
186static void rpc_free_fragment(struct rpc_fragment *fragment)
187{
188 if (fragment->data != NULL) {
189 free(fragment->data);
190 }
191 free(fragment);
192}
193
194void rpc_free_all_fragments(struct rpc_context *rpc)
195{
f3a75078
RS
196 assert(rpc->magic == RPC_CONTEXT_MAGIC);
197
d678b73e
RS
198 while (rpc->fragments != NULL) {
199 struct rpc_fragment *fragment = rpc->fragments;
200
aec45c62 201 rpc->fragments = fragment->next;
d678b73e
RS
202 rpc_free_fragment(fragment);
203 }
204}
205
183451cf 206int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
d678b73e
RS
207{
208 struct rpc_fragment *fragment;
209
f3a75078
RS
210 assert(rpc->magic == RPC_CONTEXT_MAGIC);
211
d678b73e
RS
212 fragment = malloc(sizeof(struct rpc_fragment));
213 if (fragment == NULL) {
214 return -1;
215 }
216
217 fragment->size = size;
218 fragment->data = malloc(fragment->size);
219 if(fragment->data == NULL) {
220 free(fragment);
221 return -1;
222 }
223
224 memcpy(fragment->data, data, fragment->size);
8a52596b 225 LIBNFS_LIST_ADD_END(&rpc->fragments, fragment);
d678b73e
RS
226 return 0;
227}
84004dbf
RS
228
229void rpc_destroy_context(struct rpc_context *rpc)
230{
231 struct rpc_pdu *pdu;
63f36a09 232 unsigned int i;
84004dbf 233
f3a75078
RS
234 assert(rpc->magic == RPC_CONTEXT_MAGIC);
235
aec45c62 236 while((pdu = rpc->outqueue.head) != NULL) {
84004dbf 237 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
aec45c62 238 rpc->outqueue.head = pdu->next;
84004dbf
RS
239 rpc_free_pdu(rpc, pdu);
240 }
63f36a09
MH
241
242 for (i = 0; i < HASHES; i++) {
243 struct rpc_queue *q = &rpc->waitpdu[i];
244
245 while((pdu = q->head) != NULL) {
246 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
247 rpc->outqueue.head = pdu->next;
248 rpc_free_pdu(rpc, pdu);
249 }
84004dbf
RS
250 }
251
d678b73e
RS
252 rpc_free_all_fragments(rpc);
253
84004dbf
RS
254 auth_destroy(rpc->auth);
255 rpc->auth =NULL;
256
257 if (rpc->fd != -1) {
eecdc4f3 258 close(rpc->fd);
84004dbf
RS
259 }
260
261 if (rpc->encodebuf != NULL) {
262 free(rpc->encodebuf);
263 rpc->encodebuf = NULL;
264 }
265
266 if (rpc->error_string != NULL) {
267 free(rpc->error_string);
268 rpc->error_string = NULL;
269 }
270
7ff2f3a0
RS
271 if (rpc->udp_dest != NULL) {
272 free(rpc->udp_dest);
273 rpc->udp_dest = NULL;
274 }
275
f3a75078 276 rpc->magic = 0;
84004dbf
RS
277 free(rpc);
278}
279
280