Add myself for packaging copyright and some minor tweaks
[deb_libnfs.git] / lib / init.c
1 /*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
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.
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 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #ifdef AROS
22 #include "aros_compat.h"
23 #endif
24
25 #ifdef WIN32
26 #include "win32_compat.h"
27 #endif
28
29 #define _GNU_SOURCE
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #ifdef HAVE_STRINGS_H
36 #include <strings.h>
37 #endif
38
39 #include <stdio.h>
40 #include <stdarg.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <assert.h>
44 #include <time.h>
45 #include "slist.h"
46 #include "libnfs-zdr.h"
47 #include "libnfs.h"
48 #include "libnfs-raw.h"
49 #include "libnfs-private.h"
50
51 struct rpc_context *rpc_init_context(void)
52 {
53 struct rpc_context *rpc;
54 static uint32_t salt = 0;
55 unsigned int i;
56
57 rpc = malloc(sizeof(struct rpc_context));
58 if (rpc == NULL) {
59 return NULL;
60 }
61 memset(rpc, 0, sizeof(struct rpc_context));
62
63 rpc->magic = RPC_CONTEXT_MAGIC;
64
65 /* Allow 1M of data (for writes) and some */
66 rpc->encodebuflen = 1024 * 1024 + 4096;
67 rpc->encodebuf = malloc(rpc->encodebuflen);
68 if (rpc->encodebuf == NULL) {
69 free(rpc);
70 return NULL;
71 }
72
73 rpc->auth = authunix_create_default();
74 if (rpc->auth == NULL) {
75 free(rpc->encodebuf);
76 free(rpc);
77 return NULL;
78 }
79 rpc->xid = salt + time(NULL) + getpid() << 16;
80 salt += 0x01000000;
81 rpc->fd = -1;
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
90 rpc_reset_queue(&rpc->outqueue);
91 for (i = 0; i < HASHES; i++)
92 rpc_reset_queue(&rpc->waitpdu[i]);
93
94 return rpc;
95 }
96
97 void rpc_set_readahead(struct rpc_context *rpc, uint32_t v)
98 {
99 assert(rpc->magic == RPC_CONTEXT_MAGIC);
100
101 rpc->readahead = v;
102 }
103
104 struct 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
116 void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
117 {
118 assert(rpc->magic == RPC_CONTEXT_MAGIC);
119
120 if (rpc->auth != NULL) {
121 auth_destroy(rpc->auth);
122 }
123 rpc->auth = auth;
124 }
125
126 static 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
137 void rpc_set_uid(struct rpc_context *rpc, int uid) {
138 rpc_set_uid_gid(rpc, uid, rpc->gid);
139 }
140
141 void rpc_set_gid(struct rpc_context *rpc, int gid) {
142 rpc_set_uid_gid(rpc, rpc->uid, gid);
143 }
144
145 void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
146 {
147 va_list ap;
148 char *old_error_string = rpc->error_string;
149
150 assert(rpc->magic == RPC_CONTEXT_MAGIC);
151
152 va_start(ap, error_string);
153 rpc->error_string = malloc(1024);
154 vsnprintf(rpc->error_string, 1024, error_string, ap);
155 va_end(ap);
156
157 if (old_error_string != NULL) {
158 free(old_error_string);
159 }
160 }
161
162 char *rpc_get_error(struct rpc_context *rpc)
163 {
164 assert(rpc->magic == RPC_CONTEXT_MAGIC);
165
166 return rpc->error_string;
167 }
168
169 void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
170 {
171 struct rpc_pdu *pdu, *next;
172 unsigned int i;
173
174 assert(rpc->magic == RPC_CONTEXT_MAGIC);
175
176 while ((pdu = rpc->outqueue.head) != NULL) {
177 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
178 rpc->outqueue.head = pdu->next;
179 rpc_free_pdu(rpc, pdu);
180 }
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;
192 }
193 }
194
195 static void rpc_free_fragment(struct rpc_fragment *fragment)
196 {
197 if (fragment->data != NULL) {
198 free(fragment->data);
199 }
200 free(fragment);
201 }
202
203 void rpc_free_all_fragments(struct rpc_context *rpc)
204 {
205 assert(rpc->magic == RPC_CONTEXT_MAGIC);
206
207 while (rpc->fragments != NULL) {
208 struct rpc_fragment *fragment = rpc->fragments;
209
210 rpc->fragments = fragment->next;
211 rpc_free_fragment(fragment);
212 }
213 }
214
215 int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
216 {
217 struct rpc_fragment *fragment;
218
219 assert(rpc->magic == RPC_CONTEXT_MAGIC);
220
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);
234 LIBNFS_LIST_ADD_END(&rpc->fragments, fragment);
235 return 0;
236 }
237
238 void rpc_destroy_context(struct rpc_context *rpc)
239 {
240 struct rpc_pdu *pdu;
241 unsigned int i;
242
243 assert(rpc->magic == RPC_CONTEXT_MAGIC);
244
245 while((pdu = rpc->outqueue.head) != NULL) {
246 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
247 LIBNFS_LIST_REMOVE(&rpc->outqueue.head, pdu);
248 rpc_free_pdu(rpc, pdu);
249 }
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 }
259 }
260
261 rpc_free_all_fragments(rpc);
262
263 auth_destroy(rpc->auth);
264 rpc->auth =NULL;
265
266 if (rpc->fd != -1) {
267 close(rpc->fd);
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
285 rpc->magic = 0;
286 free(rpc);
287 }
288
289