Imported Upstream version 1.9.4
[deb_libnfs.git] / lib / init.c
CommitLineData
dabf4152
AM
1/*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
f1f22dbf
RRS
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.
dabf4152
AM
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*/
ee872606
RRS
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#ifdef AROS
22#include "aros_compat.h"
23#endif
dabf4152 24
5670ec6e
AM
25#ifdef WIN32
26#include "win32_compat.h"
ee872606
RRS
27#endif
28
29#define _GNU_SOURCE
30
31#ifdef HAVE_UNISTD_H
5670ec6e 32#include <unistd.h>
ee872606
RRS
33#endif
34
35#ifdef HAVE_STRINGS_H
5670ec6e 36#include <strings.h>
ee872606 37#endif
5670ec6e 38
dabf4152
AM
39#include <stdio.h>
40#include <stdarg.h>
dabf4152 41#include <string.h>
dabf4152 42#include <stdlib.h>
ee872606
RRS
43#include <assert.h>
44#include <time.h>
dabf4152 45#include "slist.h"
ee872606 46#include "libnfs-zdr.h"
dabf4152
AM
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;
ee872606 54 static uint32_t salt = 0;
f1f22dbf 55 unsigned int i;
dabf4152
AM
56
57 rpc = malloc(sizeof(struct rpc_context));
58 if (rpc == NULL) {
59 return NULL;
60 }
5670ec6e 61 memset(rpc, 0, sizeof(struct rpc_context));
dabf4152 62
ee872606
RRS
63 rpc->magic = RPC_CONTEXT_MAGIC;
64
65 /* Allow 1M of data (for writes) and some */
66 rpc->encodebuflen = 1024 * 1024 + 4096;
dabf4152
AM
67 rpc->encodebuf = malloc(rpc->encodebuflen);
68 if (rpc->encodebuf == NULL) {
69 free(rpc);
70 return NULL;
71 }
72
5670ec6e 73 rpc->auth = authunix_create_default();
dabf4152
AM
74 if (rpc->auth == NULL) {
75 free(rpc->encodebuf);
76 free(rpc);
77 return NULL;
78 }
ee872606
RRS
79 rpc->xid = salt + time(NULL) + getpid() << 16;
80 salt += 0x01000000;
dabf4152 81 rpc->fd = -1;
ee872606
RRS
82 rpc->tcp_syncnt = RPC_PARAM_UNDEFINED;
83#if defined(WIN32) || defined(ANDROID)
84 rpc->uid = 65534;
85 rpc->gid = 65534;
86#else
87 rpc->uid = getuid();
88 rpc->gid = getgid();
89#endif
f1f22dbf
RRS
90 rpc_reset_queue(&rpc->outqueue);
91 for (i = 0; i < HASHES; i++)
92 rpc_reset_queue(&rpc->waitpdu[i]);
dabf4152
AM
93
94 return rpc;
95}
96
f1f22dbf
RRS
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}
dabf4152
AM
103
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
ee872606 116void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
dabf4152 117{
ee872606
RRS
118 assert(rpc->magic == RPC_CONTEXT_MAGIC);
119
dabf4152
AM
120 if (rpc->auth != NULL) {
121 auth_destroy(rpc->auth);
122 }
123 rpc->auth = auth;
124}
125
ee872606
RRS
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}
dabf4152
AM
144
145void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
146{
147 va_list ap;
ee872606
RRS
148 char *old_error_string = rpc->error_string;
149
150 assert(rpc->magic == RPC_CONTEXT_MAGIC);
dabf4152 151
dabf4152 152 va_start(ap, error_string);
5670ec6e
AM
153 rpc->error_string = malloc(1024);
154 vsnprintf(rpc->error_string, 1024, error_string, ap);
dabf4152 155 va_end(ap);
ee872606
RRS
156
157 if (old_error_string != NULL) {
158 free(old_error_string);
159 }
dabf4152
AM
160}
161
162char *rpc_get_error(struct rpc_context *rpc)
163{
ee872606
RRS
164 assert(rpc->magic == RPC_CONTEXT_MAGIC);
165
dabf4152
AM
166 return rpc->error_string;
167}
168
169void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
170{
f1f22dbf
RRS
171 struct rpc_pdu *pdu, *next;
172 unsigned int i;
dabf4152 173
ee872606
RRS
174 assert(rpc->magic == RPC_CONTEXT_MAGIC);
175
f1f22dbf 176 while ((pdu = rpc->outqueue.head) != NULL) {
dabf4152 177 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
f1f22dbf 178 rpc->outqueue.head = pdu->next;
dabf4152
AM
179 rpc_free_pdu(rpc, pdu);
180 }
f1f22dbf
RRS
181 rpc->outqueue.tail = NULL;
182
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;
dabf4152
AM
192 }
193}
194
5670ec6e
AM
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{
ee872606
RRS
205 assert(rpc->magic == RPC_CONTEXT_MAGIC);
206
5670ec6e
AM
207 while (rpc->fragments != NULL) {
208 struct rpc_fragment *fragment = rpc->fragments;
209
f1f22dbf 210 rpc->fragments = fragment->next;
5670ec6e
AM
211 rpc_free_fragment(fragment);
212 }
213}
214
215int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
216{
217 struct rpc_fragment *fragment;
218
ee872606
RRS
219 assert(rpc->magic == RPC_CONTEXT_MAGIC);
220
5670ec6e
AM
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);
f1f22dbf 234 LIBNFS_LIST_ADD_END(&rpc->fragments, fragment);
5670ec6e
AM
235 return 0;
236}
dabf4152
AM
237
238void rpc_destroy_context(struct rpc_context *rpc)
239{
240 struct rpc_pdu *pdu;
f1f22dbf 241 unsigned int i;
dabf4152 242
ee872606
RRS
243 assert(rpc->magic == RPC_CONTEXT_MAGIC);
244
f1f22dbf 245 while((pdu = rpc->outqueue.head) != NULL) {
dabf4152 246 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
f1f22dbf 247 LIBNFS_LIST_REMOVE(&rpc->outqueue.head, pdu);
dabf4152
AM
248 rpc_free_pdu(rpc, pdu);
249 }
f1f22dbf
RRS
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);
256 LIBNFS_LIST_REMOVE(&q->head, pdu);
257 rpc_free_pdu(rpc, pdu);
258 }
dabf4152
AM
259 }
260
5670ec6e
AM
261 rpc_free_all_fragments(rpc);
262
dabf4152
AM
263 auth_destroy(rpc->auth);
264 rpc->auth =NULL;
265
266 if (rpc->fd != -1) {
5670ec6e 267 close(rpc->fd);
dabf4152
AM
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
280 if (rpc->udp_dest != NULL) {
281 free(rpc->udp_dest);
282 rpc->udp_dest = NULL;
283 }
284
ee872606 285 rpc->magic = 0;
dabf4152
AM
286 free(rpc);
287}
288
289