init.c: fixup the mangled lgplv2.1 boilerplate
[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
98 struct rpc_context *rpc_init_udp_context(void)
99 {
100 struct rpc_context *rpc;
101
102 rpc = rpc_init_context();
103 if (rpc != NULL) {
104 rpc->is_udp = 1;
105 }
106
107 return rpc;
108 }
109
110 void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
111 {
112 assert(rpc->magic == RPC_CONTEXT_MAGIC);
113
114 if (rpc->auth != NULL) {
115 auth_destroy(rpc->auth);
116 }
117 rpc->auth = auth;
118 }
119
120 static void rpc_set_uid_gid(struct rpc_context *rpc, int uid, int gid) {
121 if (uid != rpc->uid || gid != rpc->gid) {
122 struct AUTH *auth = libnfs_authunix_create("libnfs", uid, gid, 0, NULL);
123 if (auth != NULL) {
124 rpc_set_auth(rpc, auth);
125 rpc->uid = uid;
126 rpc->gid = gid;
127 }
128 }
129 }
130
131 void rpc_set_uid(struct rpc_context *rpc, int uid) {
132 rpc_set_uid_gid(rpc, uid, rpc->gid);
133 }
134
135 void rpc_set_gid(struct rpc_context *rpc, int gid) {
136 rpc_set_uid_gid(rpc, rpc->uid, gid);
137 }
138
139 void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
140 {
141 va_list ap;
142 char *old_error_string = rpc->error_string;
143
144 assert(rpc->magic == RPC_CONTEXT_MAGIC);
145
146 va_start(ap, error_string);
147 rpc->error_string = malloc(1024);
148 vsnprintf(rpc->error_string, 1024, error_string, ap);
149 va_end(ap);
150
151 if (old_error_string != NULL) {
152 free(old_error_string);
153 }
154 }
155
156 char *rpc_get_error(struct rpc_context *rpc)
157 {
158 assert(rpc->magic == RPC_CONTEXT_MAGIC);
159
160 return rpc->error_string;
161 }
162
163 void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
164 {
165 struct rpc_pdu *pdu, *next;
166 unsigned int i;
167
168 assert(rpc->magic == RPC_CONTEXT_MAGIC);
169
170 while ((pdu = rpc->outqueue.head) != NULL) {
171 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
172 rpc->outqueue.head = pdu->next;
173 rpc_free_pdu(rpc, pdu);
174 }
175 rpc->outqueue.tail = NULL;
176
177 for (i = 0; i < HASHES; i++) {
178 struct rpc_queue *q = &rpc->waitpdu[i];
179
180 while((pdu = q->head) != NULL) {
181 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
182 q->head = pdu->next;
183 rpc_free_pdu(rpc, pdu);
184 }
185 q->tail = NULL;
186 }
187 }
188
189 static void rpc_free_fragment(struct rpc_fragment *fragment)
190 {
191 if (fragment->data != NULL) {
192 free(fragment->data);
193 }
194 free(fragment);
195 }
196
197 void rpc_free_all_fragments(struct rpc_context *rpc)
198 {
199 assert(rpc->magic == RPC_CONTEXT_MAGIC);
200
201 while (rpc->fragments != NULL) {
202 struct rpc_fragment *fragment = rpc->fragments;
203
204 rpc->fragments = fragment->next;
205 rpc_free_fragment(fragment);
206 }
207 }
208
209 int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
210 {
211 struct rpc_fragment *fragment;
212
213 assert(rpc->magic == RPC_CONTEXT_MAGIC);
214
215 fragment = malloc(sizeof(struct rpc_fragment));
216 if (fragment == NULL) {
217 return -1;
218 }
219
220 fragment->size = size;
221 fragment->data = malloc(fragment->size);
222 if(fragment->data == NULL) {
223 free(fragment);
224 return -1;
225 }
226
227 memcpy(fragment->data, data, fragment->size);
228 LIBNFS_LIST_ADD_END(&rpc->fragments, fragment);
229 return 0;
230 }
231
232 void rpc_destroy_context(struct rpc_context *rpc)
233 {
234 struct rpc_pdu *pdu;
235 unsigned int i;
236
237 assert(rpc->magic == RPC_CONTEXT_MAGIC);
238
239 while((pdu = rpc->outqueue.head) != NULL) {
240 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
241 LIBNFS_LIST_REMOVE(&rpc->outqueue.head, pdu);
242 rpc_free_pdu(rpc, pdu);
243 }
244
245 for (i = 0; i < HASHES; i++) {
246 struct rpc_queue *q = &rpc->waitpdu[i];
247
248 while((pdu = q->head) != NULL) {
249 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
250 LIBNFS_LIST_REMOVE(&q->head, pdu);
251 rpc_free_pdu(rpc, pdu);
252 }
253 }
254
255 rpc_free_all_fragments(rpc);
256
257 auth_destroy(rpc->auth);
258 rpc->auth =NULL;
259
260 if (rpc->fd != -1) {
261 close(rpc->fd);
262 }
263
264 if (rpc->encodebuf != NULL) {
265 free(rpc->encodebuf);
266 rpc->encodebuf = NULL;
267 }
268
269 if (rpc->error_string != NULL) {
270 free(rpc->error_string);
271 rpc->error_string = NULL;
272 }
273
274 if (rpc->udp_dest != NULL) {
275 free(rpc->udp_dest);
276 rpc->udp_dest = NULL;
277 }
278
279 rpc->magic = 0;
280 free(rpc);
281 }
282
283