782a7ea3edbac2da46c4c557d2de37a945ca24cb
[deb_libnfs.git] / lib / init.c
1 /*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
4
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 */
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17
18 #ifdef AROS
19 #include "aros_compat.h"
20 #endif
21
22 #ifdef WIN32
23 #include "win32_compat.h"
24 #endif
25
26 #define _GNU_SOURCE
27
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31
32 #ifdef HAVE_STRINGS_H
33 #include <strings.h>
34 #endif
35
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <assert.h>
41 #include <time.h>
42 #include "slist.h"
43 #include "libnfs-zdr.h"
44 #include "libnfs.h"
45 #include "libnfs-raw.h"
46 #include "libnfs-private.h"
47
48 struct rpc_context *rpc_init_context(void)
49 {
50 struct rpc_context *rpc;
51 static uint32_t salt = 0;
52 unsigned int i;
53
54 rpc = malloc(sizeof(struct rpc_context));
55 if (rpc == NULL) {
56 return NULL;
57 }
58 memset(rpc, 0, sizeof(struct rpc_context));
59
60 rpc->magic = RPC_CONTEXT_MAGIC;
61
62 /* Allow 1M of data (for writes) and some */
63 rpc->encodebuflen = 1024 * 1024 + 4096;
64 rpc->encodebuf = malloc(rpc->encodebuflen);
65 if (rpc->encodebuf == NULL) {
66 free(rpc);
67 return NULL;
68 }
69
70 rpc->auth = authunix_create_default();
71 if (rpc->auth == NULL) {
72 free(rpc->encodebuf);
73 free(rpc);
74 return NULL;
75 }
76 rpc->xid = salt + time(NULL) + getpid() << 16;
77 salt += 0x01000000;
78 rpc->fd = -1;
79 rpc->tcp_syncnt = RPC_PARAM_UNDEFINED;
80 #if defined(WIN32) || defined(ANDROID)
81 rpc->uid = 65534;
82 rpc->gid = 65534;
83 #else
84 rpc->uid = getuid();
85 rpc->gid = getgid();
86 #endif
87 rpc_reset_queue(&rpc->outqueue);
88 for (i = 0; i < HASHES; i++)
89 rpc_reset_queue(&rpc->waitpdu[i]);
90
91 return rpc;
92 }
93
94
95 struct 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
107 void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
108 {
109 assert(rpc->magic == RPC_CONTEXT_MAGIC);
110
111 if (rpc->auth != NULL) {
112 auth_destroy(rpc->auth);
113 }
114 rpc->auth = auth;
115 }
116
117 static 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
128 void rpc_set_uid(struct rpc_context *rpc, int uid) {
129 rpc_set_uid_gid(rpc, uid, rpc->gid);
130 }
131
132 void rpc_set_gid(struct rpc_context *rpc, int gid) {
133 rpc_set_uid_gid(rpc, rpc->uid, gid);
134 }
135
136 void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
137 {
138 va_list ap;
139 char *old_error_string = rpc->error_string;
140
141 assert(rpc->magic == RPC_CONTEXT_MAGIC);
142
143 va_start(ap, error_string);
144 rpc->error_string = malloc(1024);
145 vsnprintf(rpc->error_string, 1024, error_string, ap);
146 va_end(ap);
147
148 if (old_error_string != NULL) {
149 free(old_error_string);
150 }
151 }
152
153 char *rpc_get_error(struct rpc_context *rpc)
154 {
155 assert(rpc->magic == RPC_CONTEXT_MAGIC);
156
157 return rpc->error_string;
158 }
159
160 void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
161 {
162 struct rpc_pdu *pdu, *next;
163 unsigned int i;
164
165 assert(rpc->magic == RPC_CONTEXT_MAGIC);
166
167 while ((pdu = rpc->outqueue.head) != NULL) {
168 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
169 rpc->outqueue.head = pdu->next;
170 rpc_free_pdu(rpc, pdu);
171 }
172 rpc->outqueue.tail = NULL;
173
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;
183 }
184 }
185
186 static void rpc_free_fragment(struct rpc_fragment *fragment)
187 {
188 if (fragment->data != NULL) {
189 free(fragment->data);
190 }
191 free(fragment);
192 }
193
194 void rpc_free_all_fragments(struct rpc_context *rpc)
195 {
196 assert(rpc->magic == RPC_CONTEXT_MAGIC);
197
198 while (rpc->fragments != NULL) {
199 struct rpc_fragment *fragment = rpc->fragments;
200
201 rpc->fragments = fragment->next;
202 rpc_free_fragment(fragment);
203 }
204 }
205
206 int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
207 {
208 struct rpc_fragment *fragment;
209
210 assert(rpc->magic == RPC_CONTEXT_MAGIC);
211
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);
225 LIBNFS_LIST_ADD_END(&rpc->fragments, fragment);
226 return 0;
227 }
228
229 void rpc_destroy_context(struct rpc_context *rpc)
230 {
231 struct rpc_pdu *pdu;
232 unsigned int i;
233
234 assert(rpc->magic == RPC_CONTEXT_MAGIC);
235
236 while((pdu = rpc->outqueue.head) != NULL) {
237 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
238 rpc->outqueue.head = pdu->next;
239 rpc_free_pdu(rpc, pdu);
240 }
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 }
250 }
251
252 rpc_free_all_fragments(rpc);
253
254 auth_destroy(rpc->auth);
255 rpc->auth =NULL;
256
257 if (rpc->fd != -1) {
258 close(rpc->fd);
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
271 if (rpc->udp_dest != NULL) {
272 free(rpc->udp_dest);
273 rpc->udp_dest = NULL;
274 }
275
276 rpc->magic = 0;
277 free(rpc);
278 }
279
280