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