/
init.c
613 lines (507 loc) · 14.6 KB
/
init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
/*
Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef AROS
#include "aros_compat.h"
#endif
#ifdef PS2_EE
#include "ps2_compat.h"
#endif
#ifdef PS3_PPU
#include "ps3_compat.h"
#endif
#ifdef WIN32
#include <win32/win32_compat.h>
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <fcntl.h>
#include <time.h>
#include "slist.h"
#include "libnfs-zdr.h"
#include "libnfs.h"
#include "libnfs-raw.h"
#include "libnfs-private.h"
#ifdef HAVE_LIBKRB5
#include "krb5-wrapper.h"
#endif
#ifndef discard_const
#define discard_const(ptr) ((void *)((intptr_t)(ptr)))
#endif
static const char *oom = "out of memory";
uint64_t rpc_current_time(void)
{
#ifdef HAVE_CLOCK_GETTIME
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC_COARSE, &tp);
return (uint64_t)tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
#else
return (uint64_t)time(NULL) * 1000;
#endif
}
int rpc_set_hash_size(struct rpc_context *rpc, int hashes)
{
uint32_t i;
#ifdef HAVE_MULTITHREADING
if (rpc->multithreading_enabled) {
nfs_mt_mutex_lock(&rpc->rpc_mutex);
}
#endif /* HAVE_MULTITHREADING */
rpc->num_hashes = hashes;
free(rpc->waitpdu);
rpc->waitpdu = malloc(sizeof(struct rpc_queue) * rpc->num_hashes);
if (rpc->waitpdu == NULL) {
return -1;
}
for (i = 0; i < rpc->num_hashes; i++)
rpc_reset_queue(&rpc->waitpdu[i]);
#ifdef HAVE_MULTITHREADING
if (rpc->multithreading_enabled) {
nfs_mt_mutex_unlock(&rpc->rpc_mutex);
}
#endif /* HAVE_MULTITHREADING */
return 0;
}
int nfs_set_hash_size(struct nfs_context *nfs, int hashes)
{
return rpc_set_hash_size(nfs->rpc, hashes);
}
struct rpc_context *rpc_init_context(void)
{
struct rpc_context *rpc;
static uint32_t salt = 0;
rpc = malloc(sizeof(struct rpc_context));
if (rpc == NULL) {
return NULL;
}
memset(rpc, 0, sizeof(struct rpc_context));
if (rpc_set_hash_size(rpc, DEFAULT_HASHES)) {
free(rpc);
return NULL;
}
rpc->magic = RPC_CONTEXT_MAGIC;
rpc->inpos = 0;
rpc->state = READ_RM;
#ifdef HAVE_MULTITHREADING
nfs_mt_mutex_init(&rpc->rpc_mutex);
#ifndef HAVE_STDATOMIC_H
nfs_mt_mutex_init(&rpc->atomic_int_mutex);
#endif
#endif /* HAVE_MULTITHREADING */
rpc->auth = authunix_create_default();
if (rpc->auth == NULL) {
free(rpc->waitpdu);
free(rpc);
return NULL;
}
// Add PID to rpc->xid for easier debugging, making sure to cast
// pid to 32-bit type to avoid invalid left-shifts.
rpc->xid = salt + (uint32_t)rpc_current_time() + ((uint32_t)getpid() << 16);
salt += 0x01000000;
rpc->fd = -1;
rpc->tcp_syncnt = RPC_PARAM_UNDEFINED;
#if defined(WIN32) || defined(ANDROID) || defined(PS3_PPU)
rpc->uid = 65534;
rpc->gid = 65534;
#else
rpc->uid = getuid();
rpc->gid = getgid();
#endif
rpc_reset_queue(&rpc->outqueue);
/* Default is no limit */
rpc->max_waitpdu_len = 0;
/*
* Default RPC timeout is 60 secs, but it will later be updated if user
* has passed the timeo=<int> mount option. Another way to set the RPC
* timeout is by calling the nfs_set_timeout()/rpc_set_timeout()
* function but the mount option should be prefered by new users.
*/
rpc->timeout = 60 * 1000;
/*
* Default value of retrans starts as 0, i.e., no retries.
* Only after mount completes successfully and the rpc_context is used
* for NFS requests, do we set rpc->retrans to the value set by retrans=<int>
* mount parameter. See rpc_set_resiliency().
*/
rpc->retrans = 0;
/* Default is to timeout after 100ms of poll(2) */
rpc->poll_timeout = 100;
return rpc;
}
static int
is_nonblocking(int s)
{
#if defined(WIN32)
return 0;
#else
int v;
v = fcntl(s, F_GETFL, 0);
return (v & O_NONBLOCK) != 0;
#endif
}
struct rpc_context *rpc_init_server_context(int s)
{
struct rpc_context *rpc;
rpc = malloc(sizeof(struct rpc_context));
if (rpc == NULL) {
return NULL;
}
memset(rpc, 0, sizeof(struct rpc_context));
rpc->magic = RPC_CONTEXT_MAGIC;
rpc->is_server_context = 1;
rpc->fd = s;
rpc->is_connected = 1;
rpc->is_nonblocking = is_nonblocking(s);
rpc->is_udp = rpc_is_udp_socket(rpc);
rpc_reset_queue(&rpc->outqueue);
#ifdef HAVE_MULTITHREADING
nfs_mt_mutex_init(&rpc->rpc_mutex);
#ifndef HAVE_STDATOMIC_H
nfs_mt_mutex_init(&rpc->atomic_int_mutex);
#endif
#endif /* HAVE_MULTITHREADING */
return rpc;
}
#ifdef HAVE_SO_BINDTODEVICE
void rpc_set_interface(struct rpc_context *rpc, const char *ifname)
{
/*
* This only copies the interface information into the RPC
* structure. It doesn't stop whatever interface is being used. The
* connection needs to be restarted for that happen. In other words,
* set this before you connect.
*/
assert(rpc->magic == RPC_CONTEXT_MAGIC);
if (ifname) {
/*
* Allow at one-less character just-in-case IFNAMSIZ for
* the defined platform does not include the NUL-terminator.
*/
strncpy(rpc->ifname, ifname, sizeof(rpc->ifname) - 1);
}
}
#endif
void rpc_set_debug(struct rpc_context *rpc, int level)
{
assert(rpc->magic == RPC_CONTEXT_MAGIC);
rpc->debug = level;
}
struct rpc_context *rpc_init_udp_context(void)
{
struct rpc_context *rpc;
rpc = rpc_init_context();
if (rpc != NULL) {
rpc->is_udp = 1;
}
return rpc;
}
int rpc_set_username(struct rpc_context *rpc, const char *username)
{
#ifdef HAVE_LIBKRB5
free(discard_const(rpc->username));
rpc->username = strdup(username);
#endif
return 0;
}
void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
{
assert(rpc->magic == RPC_CONTEXT_MAGIC);
if (rpc->auth != NULL) {
auth_destroy(rpc->auth);
}
rpc->auth = auth;
}
static void rpc_set_uid_gid(struct rpc_context *rpc, int uid, int gid) {
if (uid != rpc->uid || gid != rpc->gid) {
struct AUTH *auth = libnfs_authunix_create("libnfs", uid, gid, 0, NULL);
if (auth != NULL) {
rpc_set_auth(rpc, auth);
rpc->uid = uid;
rpc->gid = gid;
}
}
}
void rpc_set_uid(struct rpc_context *rpc, int uid) {
rpc_set_uid_gid(rpc, uid, rpc->gid);
}
void rpc_set_gid(struct rpc_context *rpc, int gid) {
rpc_set_uid_gid(rpc, rpc->uid, gid);
}
void rpc_set_auxiliary_gids(struct rpc_context *rpc, uint32_t len, uint32_t* gids) {
struct AUTH *auth = libnfs_authunix_create("libnfs", rpc->uid, rpc->gid, len, gids);
if (auth != NULL) {
rpc_set_auth(rpc, auth);
}
}
void rpc_set_error(struct rpc_context *rpc, const char *error_string, ...)
{
va_list ap;
char *old_error_string = NULL;
#ifdef HAVE_MULTITHREADING
if (rpc->multithreading_enabled) {
nfs_mt_mutex_lock(&rpc->rpc_mutex);
}
#endif /* HAVE_MULTITHREADING */
old_error_string = rpc->error_string;
va_start(ap, error_string);
rpc->error_string = malloc(1024);
if (rpc->error_string == NULL) {
rpc->error_string = discard_const(oom);
goto finished;
}
vsnprintf(rpc->error_string, 1024, error_string, ap);
va_end(ap);
RPC_LOG(rpc, 1, "error: %s", rpc->error_string);
finished:
if (old_error_string && old_error_string != oom) {
free(old_error_string);
}
#ifdef HAVE_MULTITHREADING
if (rpc->multithreading_enabled) {
nfs_mt_mutex_unlock(&rpc->rpc_mutex);
}
#endif /* HAVE_MULTITHREADING */
}
void rpc_set_error_locked(struct rpc_context *rpc, const char *error_string, ...)
{
va_list ap;
char *old_error_string = rpc->error_string;
va_start(ap, error_string);
rpc->error_string = malloc(1024);
if (rpc->error_string == NULL) {
free(old_error_string);
rpc->error_string = discard_const(oom);
return;
}
vsnprintf(rpc->error_string, 1024, error_string, ap);
va_end(ap);
RPC_LOG(rpc, 1, "error: %s", rpc->error_string);
if (old_error_string && old_error_string != oom) {
free(old_error_string);
}
}
char *rpc_get_error(struct rpc_context *rpc)
{
assert(rpc->magic == RPC_CONTEXT_MAGIC);
return rpc->error_string ? rpc->error_string : "";
}
static void rpc_purge_all_pdus(struct rpc_context *rpc, int status, const char *error)
{
struct rpc_queue outqueue;
struct rpc_pdu *pdu;
int i;
assert(rpc->magic == RPC_CONTEXT_MAGIC);
/* Remove all entries from each queue before cancellation to prevent
* the callbacks manipulating entries that are about to be removed.
*
* This code assumes that the callbacks will not enqueue any new
* pdus when called.
*/
#ifdef HAVE_MULTITHREADING
if (rpc->multithreading_enabled) {
nfs_mt_mutex_lock(&rpc->rpc_mutex);
}
#endif /* HAVE_MULTITHREADING */
outqueue = rpc->outqueue;
rpc_reset_queue(&rpc->outqueue);
while ((pdu = outqueue.head) != NULL) {
outqueue.head = pdu->next;
pdu->next = NULL;
pdu->cb(rpc, status, (void *) error, pdu->private_data);
rpc_free_pdu(rpc, pdu);
}
#ifdef HAVE_MULTITHREADING
if (rpc->multithreading_enabled) {
nfs_mt_mutex_unlock(&rpc->rpc_mutex);
}
#endif /* HAVE_MULTITHREADING */
for (i = 0; i < rpc->num_hashes; i++) {
struct rpc_queue waitqueue;
#ifdef HAVE_MULTITHREADING
if (rpc->multithreading_enabled) {
nfs_mt_mutex_lock(&rpc->rpc_mutex);
}
#endif /* HAVE_MULTITHREADING */
waitqueue = rpc->waitpdu[i];
rpc_reset_queue(&rpc->waitpdu[i]);
#ifdef HAVE_MULTITHREADING
if (rpc->multithreading_enabled) {
nfs_mt_mutex_unlock(&rpc->rpc_mutex);
}
#endif /* HAVE_MULTITHREADING */
while((pdu = waitqueue.head) != NULL) {
waitqueue.head = pdu->next;
pdu->next = NULL;
pdu->cb(rpc, status, (void *) error, pdu->private_data);
rpc_free_pdu(rpc, pdu);
}
}
assert(!rpc->outqueue.head);
}
void rpc_error_all_pdus(struct rpc_context *rpc, const char *error)
{
rpc_purge_all_pdus(rpc, RPC_STATUS_ERROR, error);
}
static void rpc_free_fragment(struct rpc_fragment *fragment)
{
if (fragment->data != NULL) {
free(fragment->data);
}
free(fragment);
}
void rpc_free_all_fragments(struct rpc_context *rpc)
{
assert(rpc->magic == RPC_CONTEXT_MAGIC);
while (rpc->fragments != NULL) {
struct rpc_fragment *fragment = rpc->fragments;
rpc->fragments = fragment->next;
rpc_free_fragment(fragment);
}
}
int rpc_add_fragment(struct rpc_context *rpc, char *data, uint32_t size)
{
struct rpc_fragment *fragment;
assert(rpc->magic == RPC_CONTEXT_MAGIC);
fragment = malloc(sizeof(struct rpc_fragment));
if (fragment == NULL) {
return -1;
}
fragment->size = size;
fragment->data = malloc(fragment->size);
if(fragment->data == NULL) {
free(fragment);
return -1;
}
memcpy(fragment->data, data, fragment->size);
LIBNFS_LIST_ADD_END(&rpc->fragments, fragment);
return 0;
}
void rpc_destroy_context(struct rpc_context *rpc)
{
assert(rpc->magic == RPC_CONTEXT_MAGIC);
rpc_purge_all_pdus(rpc, RPC_STATUS_CANCEL, NULL);
rpc_free_all_fragments(rpc);
if (rpc->auth) {
auth_destroy(rpc->auth);
rpc->auth =NULL;
}
if (rpc->fd != -1) {
close(rpc->fd);
}
if (rpc->error_string && rpc->error_string != oom) {
free(rpc->error_string);
rpc->error_string = NULL;
}
free(rpc->waitpdu);
rpc->waitpdu = NULL;
free(rpc->inbuf);
rpc->inbuf = NULL;
rpc->magic = 0;
#ifdef HAVE_MULTITHREADING
nfs_mt_mutex_destroy(&rpc->rpc_mutex);
#ifndef HAVE_STDATOMIC_H
nfs_mt_mutex_destroy(&rpc->atomic_int_mutex);
#endif
#endif /* HAVE_MULTITHREADING */
#ifdef HAVE_LIBKRB5
if (rpc->auth_data) {
krb5_free_auth_data(rpc->auth_data);
}
free(discard_const(rpc->username));
free(rpc->context);
#endif /* HAVE_LIBKRB5 */
free(rpc->server);
free(rpc);
}
void rpc_set_poll_timeout(struct rpc_context *rpc, int poll_timeout)
{
assert(rpc->magic == RPC_CONTEXT_MAGIC);
rpc->poll_timeout = poll_timeout;
}
int rpc_get_poll_timeout(struct rpc_context *rpc)
{
assert(rpc->magic == RPC_CONTEXT_MAGIC);
return rpc->poll_timeout;
}
void rpc_set_timeout(struct rpc_context *rpc, int timeout_msecs)
{
assert(rpc->magic == RPC_CONTEXT_MAGIC);
rpc->timeout = timeout_msecs;
}
int rpc_get_timeout(struct rpc_context *rpc)
{
assert(rpc->magic == RPC_CONTEXT_MAGIC);
return rpc->timeout;
}
int rpc_register_service(struct rpc_context *rpc, int program, int version,
struct service_proc *procs, int num_procs)
{
struct rpc_endpoint *endpoint;
assert(rpc->magic == RPC_CONTEXT_MAGIC);
if (!rpc->is_server_context) {
rpc_set_error(rpc, "Not a server context.");
return -1;
}
endpoint = malloc(sizeof(*endpoint));
if (endpoint == NULL) {
rpc_set_error(rpc, "Out of memory: Failed to allocate endpoint "
"structure");
return -1;
}
endpoint->program = program;
endpoint->version = version;
endpoint->procs = procs;
endpoint->num_procs = num_procs;
endpoint->next = rpc->endpoints;
rpc->endpoints = endpoint;
return 0;
}
void rpc_free_iovector(struct rpc_context *rpc, struct rpc_io_vectors *v)
{
int i;
for (i = 0; i < v->niov; i++) {
if (v->iov[i].free) {
v->iov[i].free(v->iov[i].buf);
}
}
v->niov = 0;
}
int rpc_add_iovector(struct rpc_context *rpc, struct rpc_io_vectors *v,
char *buf, int len, void (*free)(void *))
{
if (v->niov >= RPC_MAX_VECTORS) {
rpc_set_error(rpc, "Too many io vectors");
return -1;
}
v->iov[v->niov].buf = buf;
v->iov[v->niov].len = len;
v->iov[v->niov].free = free;
v->niov++;
return 0;
}