fix implicit decl warnings
[deb_libnfs.git] / lib / init.c
CommitLineData
84004dbf
RS
1/*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
f0888d12
RS
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
84004dbf
RS
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
16*/
00748f36
RS
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
84004dbf 20
108c622a
RS
21#ifdef AROS
22#include "aros_compat.h"
23#endif
24
a8a1b858
M
25#ifdef WIN32
26#include "win32_compat.h"
bff8fe46
RS
27#endif
28
a8a1b858 29#define _GNU_SOURCE
eecdc4f3 30
00748f36
RS
31#ifdef HAVE_UNISTD_H
32#include <unistd.h>
33#endif
34
bff8fe46
RS
35#ifdef HAVE_STRINGS_H
36#include <strings.h>
37#endif
38
84004dbf
RS
39#include <stdio.h>
40#include <stdarg.h>
84004dbf 41#include <string.h>
98f5fee8 42#include <stdlib.h>
f3a75078 43#include <assert.h>
a2756193 44#include <time.h>
84004dbf 45#include "slist.h"
763cd6e3 46#include "libnfs-zdr.h"
84004dbf
RS
47#include "libnfs.h"
48#include "libnfs-raw.h"
49#include "libnfs-private.h"
50
51struct rpc_context *rpc_init_context(void)
52{
53 struct rpc_context *rpc;
a2756193 54 static uint32_t salt = 0;
63f36a09 55 unsigned int i;
84004dbf
RS
56
57 rpc = malloc(sizeof(struct rpc_context));
58 if (rpc == NULL) {
84004dbf
RS
59 return NULL;
60 }
ea98629a 61 memset(rpc, 0, sizeof(struct rpc_context));
84004dbf 62
f3a75078 63 rpc->magic = RPC_CONTEXT_MAGIC;
35280fd7
RS
64
65 /* Allow 1M of data (for writes) and some */
66 rpc->encodebuflen = 1024 * 1024 + 4096;
84004dbf
RS
67 rpc->encodebuf = malloc(rpc->encodebuflen);
68 if (rpc->encodebuf == NULL) {
84004dbf
RS
69 free(rpc);
70 return NULL;
71 }
72
eecdc4f3 73 rpc->auth = authunix_create_default();
84004dbf 74 if (rpc->auth == NULL) {
84004dbf
RS
75 free(rpc->encodebuf);
76 free(rpc);
77 return NULL;
78 }
6fda9871 79 rpc->xid = salt + time(NULL) + getpid() << 16;
a2756193 80 salt += 0x01000000;
84004dbf 81 rpc->fd = -1;
1c8b4547 82 rpc->tcp_syncnt = RPC_PARAM_UNDEFINED;
3af0c022 83#if defined(WIN32) || defined(ANDROID)
9126c9c0
PL
84 rpc->uid = 65534;
85 rpc->gid = 65534;
86#else
87 rpc->uid = getuid();
88 rpc->gid = getgid();
89#endif
aec45c62 90 rpc_reset_queue(&rpc->outqueue);
63f36a09
MH
91 for (i = 0; i < HASHES; i++)
92 rpc_reset_queue(&rpc->waitpdu[i]);
84004dbf
RS
93
94 return rpc;
95}
96
3ca2aac9
PL
97void rpc_set_readahead(struct rpc_context *rpc, uint32_t v)
98{
99 assert(rpc->magic == RPC_CONTEXT_MAGIC);
100
101 rpc->readahead = v;
102}
84004dbf 103
9e00b8c6
RS
104struct rpc_context *rpc_init_udp_context(void)
105{
106 struct rpc_context *rpc;
107
108 rpc = rpc_init_context();
109 if (rpc != NULL) {
110 rpc->is_udp = 1;
111 }
112
113 return rpc;
114}
115
67ba2239 116void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
84004dbf 117{
f3a75078
RS
118 assert(rpc->magic == RPC_CONTEXT_MAGIC);
119
84004dbf
RS
120 if (rpc->auth != NULL) {
121 auth_destroy(rpc->auth);
122 }
8704724f 123 rpc->auth = auth;
84004dbf
RS
124}
125
9126c9c0
PL
126static void rpc_set_uid_gid(struct rpc_context *rpc, int uid, int gid) {
127 if (uid != rpc->uid || gid != rpc->gid) {
128 struct AUTH *auth = libnfs_authunix_create("libnfs", uid, gid, 0, NULL);
129 if (auth != NULL) {
130 rpc_set_auth(rpc, auth);
131 rpc->uid = uid;
132 rpc->gid = gid;
133 }
134 }
135}
136
137void rpc_set_uid(struct rpc_context *rpc, int uid) {
138 rpc_set_uid_gid(rpc, uid, rpc->gid);
139}
140
141void rpc_set_gid(struct rpc_context *rpc, int gid) {
142 rpc_set_uid_gid(rpc, rpc->uid, gid);
143}
84004dbf
RS
144
145void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
146{
147 va_list ap;
f0cb8042 148 char *old_error_string = rpc->error_string;
84004dbf 149
f3a75078
RS
150 assert(rpc->magic == RPC_CONTEXT_MAGIC);
151
84004dbf 152 va_start(ap, error_string);
b85c7de2
RS
153 rpc->error_string = malloc(1024);
154 vsnprintf(rpc->error_string, 1024, error_string, ap);
84004dbf 155 va_end(ap);
f0cb8042
RS
156
157 if (old_error_string != NULL) {
158 free(old_error_string);
159 }
84004dbf
RS
160}
161
162char *rpc_get_error(struct rpc_context *rpc)
163{
f3a75078
RS
164 assert(rpc->magic == RPC_CONTEXT_MAGIC);
165
84004dbf
RS
166 return rpc->error_string;
167}
168
169void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
170{
aec45c62 171 struct rpc_pdu *pdu, *next;
63f36a09 172 unsigned int i;
84004dbf 173
f3a75078
RS
174 assert(rpc->magic == RPC_CONTEXT_MAGIC);
175
aec45c62 176 while ((pdu = rpc->outqueue.head) != NULL) {
84004dbf 177 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
aec45c62 178 rpc->outqueue.head = pdu->next;
84004dbf
RS
179 rpc_free_pdu(rpc, pdu);
180 }
aec45c62
MH
181 rpc->outqueue.tail = NULL;
182
63f36a09
MH
183 for (i = 0; i < HASHES; i++) {
184 struct rpc_queue *q = &rpc->waitpdu[i];
185
186 while((pdu = q->head) != NULL) {
187 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
188 q->head = pdu->next;
189 rpc_free_pdu(rpc, pdu);
190 }
191 q->tail = NULL;
84004dbf
RS
192 }
193}
194
d678b73e
RS
195static void rpc_free_fragment(struct rpc_fragment *fragment)
196{
197 if (fragment->data != NULL) {
198 free(fragment->data);
199 }
200 free(fragment);
201}
202
203void rpc_free_all_fragments(struct rpc_context *rpc)
204{
f3a75078
RS
205 assert(rpc->magic == RPC_CONTEXT_MAGIC);
206
d678b73e
RS
207 while (rpc->fragments != NULL) {
208 struct rpc_fragment *fragment = rpc->fragments;
209
aec45c62 210 rpc->fragments = fragment->next;
d678b73e
RS
211 rpc_free_fragment(fragment);
212 }
213}
214
183451cf 215int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
d678b73e
RS
216{
217 struct rpc_fragment *fragment;
218
f3a75078
RS
219 assert(rpc->magic == RPC_CONTEXT_MAGIC);
220
d678b73e
RS
221 fragment = malloc(sizeof(struct rpc_fragment));
222 if (fragment == NULL) {
223 return -1;
224 }
225
226 fragment->size = size;
227 fragment->data = malloc(fragment->size);
228 if(fragment->data == NULL) {
229 free(fragment);
230 return -1;
231 }
232
233 memcpy(fragment->data, data, fragment->size);
8a52596b 234 LIBNFS_LIST_ADD_END(&rpc->fragments, fragment);
d678b73e
RS
235 return 0;
236}
84004dbf
RS
237
238void rpc_destroy_context(struct rpc_context *rpc)
239{
240 struct rpc_pdu *pdu;
63f36a09 241 unsigned int i;
84004dbf 242
f3a75078
RS
243 assert(rpc->magic == RPC_CONTEXT_MAGIC);
244
aec45c62 245 while((pdu = rpc->outqueue.head) != NULL) {
84004dbf 246 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
1a6ec3ee 247 LIBNFS_LIST_REMOVE(&rpc->outqueue.head, pdu);
84004dbf
RS
248 rpc_free_pdu(rpc, pdu);
249 }
63f36a09
MH
250
251 for (i = 0; i < HASHES; i++) {
252 struct rpc_queue *q = &rpc->waitpdu[i];
253
254 while((pdu = q->head) != NULL) {
255 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
1a6ec3ee 256 LIBNFS_LIST_REMOVE(&q->head, pdu);
63f36a09
MH
257 rpc_free_pdu(rpc, pdu);
258 }
84004dbf
RS
259 }
260
d678b73e
RS
261 rpc_free_all_fragments(rpc);
262
84004dbf
RS
263 auth_destroy(rpc->auth);
264 rpc->auth =NULL;
265
266 if (rpc->fd != -1) {
eecdc4f3 267 close(rpc->fd);
84004dbf
RS
268 }
269
270 if (rpc->encodebuf != NULL) {
271 free(rpc->encodebuf);
272 rpc->encodebuf = NULL;
273 }
274
275 if (rpc->error_string != NULL) {
276 free(rpc->error_string);
277 rpc->error_string = NULL;
278 }
279
7ff2f3a0
RS
280 if (rpc->udp_dest != NULL) {
281 free(rpc->udp_dest);
282 rpc->udp_dest = NULL;
283 }
284
f3a75078 285 rpc->magic = 0;
84004dbf
RS
286 free(rpc);
287}
288
289