libnfs.c: add nfs_create
[deb_libnfs.git] / lib / libnfs-sync.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 /*
18 * High level api to nfs filesystems
19 */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #ifdef AROS
25 #include "aros_compat.h"
26 #endif
27
28 #ifdef WIN32
29 #include "win32_compat.h"
30 #endif
31
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
34 #endif
35
36 #ifdef HAVE_NET_IF_H
37 #include <net/if.h>
38 #endif
39
40 #ifdef ANDROID
41 #define statvfs statfs
42 #endif
43
44 #ifdef HAVE_SYS_VFS_H
45 #include <sys/vfs.h>
46 #endif
47
48 #ifdef HAVE_SYS_STATVFS_H
49 #include <sys/statvfs.h>
50 #endif
51
52 #ifdef HAVE_SYS_IOCTL_H
53 #include <sys/ioctl.h>
54 #endif
55
56 #ifdef HAVE_POLL_H
57 #include <poll.h>
58 #endif
59
60 #ifdef HAVE_NETDB_H
61 #include <netdb.h>
62 #endif
63
64 #ifdef HAVE_UNISTD_H
65 #include <unistd.h>
66 #endif
67
68 #ifdef HAVE_NETINET_IN_H
69 #include <netinet/in.h>
70 #endif
71
72 #ifdef HAVE_STRINGS_H
73 #include <strings.h>
74 #endif
75
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <assert.h>
80 #include <sys/types.h>
81 #include <sys/stat.h>
82 #include <fcntl.h>
83 #include <errno.h>
84
85 #ifdef HAVE_SYS_SOCKIO_H
86 #include <sys/sockio.h>
87 #endif
88
89 #include "libnfs-zdr.h"
90 #include "libnfs.h"
91 #include "libnfs-raw.h"
92 #include "libnfs-raw-mount.h"
93 #include "libnfs-raw-nfs.h"
94 #include "libnfs-private.h"
95
96 struct sync_cb_data {
97 int is_finished;
98 int status;
99 uint64_t offset;
100 void *return_data;
101 int return_int;
102 const char *call;
103 };
104
105
106 static void wait_for_reply(struct rpc_context *rpc, struct sync_cb_data *cb_data)
107 {
108 struct pollfd pfd;
109
110 assert(rpc->magic == RPC_CONTEXT_MAGIC);
111
112 while (!cb_data->is_finished) {
113
114 pfd.fd = rpc_get_fd(rpc);
115 pfd.events = rpc_which_events(rpc);
116 if (poll(&pfd, 1, -1) < 0) {
117 rpc_set_error(rpc, "Poll failed");
118 cb_data->status = -EIO;
119 break;
120 }
121 if (rpc_service(rpc, pfd.revents) < 0) {
122 rpc_set_error(rpc, "rpc_service failed");
123 cb_data->status = -EIO;
124 break;
125 }
126 if (rpc_get_fd(rpc) == -1) {
127 rpc_set_error(rpc, "Socket closed\n");
128 break;
129 }
130 }
131 }
132
133 static void wait_for_nfs_reply(struct nfs_context *nfs, struct sync_cb_data *cb_data)
134 {
135 struct pollfd pfd;
136
137 while (!cb_data->is_finished) {
138
139 pfd.fd = nfs_get_fd(nfs);
140 pfd.events = nfs_which_events(nfs);
141 if (poll(&pfd, 1, -1) < 0) {
142 nfs_set_error(nfs, "Poll failed");
143 cb_data->status = -EIO;
144 break;
145 }
146 if (nfs_service(nfs, pfd.revents) < 0) {
147 nfs_set_error(nfs, "nfs_service failed");
148 cb_data->status = -EIO;
149 break;
150 }
151 }
152 }
153
154
155
156
157
158
159 /*
160 * connect to the server and mount the export
161 */
162 static void mount_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
163 {
164 struct sync_cb_data *cb_data = private_data;
165
166 cb_data->is_finished = 1;
167 cb_data->status = status;
168
169 if (status < 0) {
170 nfs_set_error(nfs, "mount/mnt call failed with \"%s\"", (char *)data);
171 return;
172 }
173 }
174
175 int nfs_mount(struct nfs_context *nfs, const char *server, const char *export)
176 {
177 struct sync_cb_data cb_data;
178 struct rpc_context *rpc = nfs_get_rpc_context(nfs);
179
180 assert(rpc->magic == RPC_CONTEXT_MAGIC);
181
182 cb_data.is_finished = 0;
183
184 if (nfs_mount_async(nfs, server, export, mount_cb, &cb_data) != 0) {
185 nfs_set_error(nfs, "nfs_mount_async failed");
186 return -1;
187 }
188
189 wait_for_nfs_reply(nfs, &cb_data);
190
191 /* Dont want any more callbacks even if the socket is closed */
192 rpc->connect_cb = NULL;
193
194 return cb_data.status;
195 }
196
197
198 /*
199 * stat()
200 */
201 static void stat_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
202 {
203 struct sync_cb_data *cb_data = private_data;
204
205 cb_data->is_finished = 1;
206 cb_data->status = status;
207
208 if (status < 0) {
209 nfs_set_error(nfs, "stat call failed with \"%s\"", (char *)data);
210 return;
211 }
212 #ifdef WIN32
213 memcpy(cb_data->return_data, data, sizeof(struct __stat64));
214 #else
215 memcpy(cb_data->return_data, data, sizeof(struct stat));
216 #endif
217 }
218
219 #ifdef WIN32
220 int nfs_stat(struct nfs_context *nfs, const char *path, struct __stat64 *st)
221 #else
222 int nfs_stat(struct nfs_context *nfs, const char *path, struct stat *st)
223 #endif
224 {
225 struct sync_cb_data cb_data;
226
227 cb_data.is_finished = 0;
228 cb_data.return_data = st;
229
230 if (nfs_stat_async(nfs, path, stat_cb, &cb_data) != 0) {
231 nfs_set_error(nfs, "nfs_stat_async failed");
232 return -1;
233 }
234
235 wait_for_nfs_reply(nfs, &cb_data);
236
237 return cb_data.status;
238 }
239
240 static void stat64_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
241 {
242 struct sync_cb_data *cb_data = private_data;
243
244 cb_data->is_finished = 1;
245 cb_data->status = status;
246
247 if (status < 0) {
248 nfs_set_error(nfs, "stat call failed with \"%s\"", (char *)data);
249 return;
250 }
251 memcpy(cb_data->return_data, data, sizeof(struct nfs_stat_64));
252 }
253
254 int nfs_stat64(struct nfs_context *nfs, const char *path, struct nfs_stat_64 *st)
255 {
256 struct sync_cb_data cb_data;
257
258 cb_data.is_finished = 0;
259 cb_data.return_data = st;
260
261 if (nfs_stat64_async(nfs, path, stat64_cb, &cb_data) != 0) {
262 nfs_set_error(nfs, "nfs_stat64_async failed");
263 return -1;
264 }
265
266 wait_for_nfs_reply(nfs, &cb_data);
267
268 return cb_data.status;
269 }
270
271 /*
272 * open()
273 */
274 static void open_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
275 {
276 struct sync_cb_data *cb_data = private_data;
277 struct nfsfh *fh, **nfsfh;
278
279 cb_data->is_finished = 1;
280 cb_data->status = status;
281
282 if (status < 0) {
283 nfs_set_error(nfs, "open call failed with \"%s\"", (char *)data);
284 return;
285 }
286
287 fh = data;
288 nfsfh = cb_data->return_data;
289 *nfsfh = fh;
290 }
291
292 int nfs_open(struct nfs_context *nfs, const char *path, int flags, struct nfsfh **nfsfh)
293 {
294 struct sync_cb_data cb_data;
295
296 cb_data.is_finished = 0;
297 cb_data.return_data = nfsfh;
298
299 if (nfs_open_async(nfs, path, flags, open_cb, &cb_data) != 0) {
300 nfs_set_error(nfs, "nfs_open_async failed");
301 return -1;
302 }
303
304 wait_for_nfs_reply(nfs, &cb_data);
305
306 return cb_data.status;
307 }
308
309 /*
310 * chdir()
311 */
312 static void chdir_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
313 {
314 struct sync_cb_data *cb_data = private_data;
315
316 cb_data->is_finished = 1;
317 cb_data->status = status;
318
319 if (status < 0) {
320 nfs_set_error(nfs, "chdir call failed with \"%s\"", (char *)data);
321 return;
322 }
323 }
324
325 int nfs_chdir(struct nfs_context *nfs, const char *path)
326 {
327 struct sync_cb_data cb_data;
328
329 cb_data.is_finished = 0;
330
331 if (nfs_chdir_async(nfs, path, chdir_cb, &cb_data) != 0) {
332 nfs_set_error(nfs, "nfs_chdir_async failed with %s",
333 nfs_get_error(nfs));
334 return -1;
335 }
336
337 wait_for_nfs_reply(nfs, &cb_data);
338
339 return cb_data.status;
340 }
341
342
343
344 /*
345 * pread()
346 */
347 static void pread_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
348 {
349 struct sync_cb_data *cb_data = private_data;
350 char *buffer;
351 cb_data->is_finished = 1;
352 cb_data->status = status;
353
354 if (status < 0) {
355 nfs_set_error(nfs, "%s call failed with \"%s\"", cb_data->call, (char *)data);
356 return;
357 }
358
359 buffer = cb_data->return_data;
360 memcpy(buffer, (char *)data, status);
361 }
362
363 int nfs_pread(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, uint64_t count, char *buffer)
364 {
365 struct sync_cb_data cb_data;
366
367 cb_data.is_finished = 0;
368 cb_data.return_data = buffer;
369 cb_data.call = "pread";
370
371 if (nfs_pread_async(nfs, nfsfh, offset, count, pread_cb, &cb_data) != 0) {
372 nfs_set_error(nfs, "nfs_pread_async failed");
373 return -1;
374 }
375
376 wait_for_nfs_reply(nfs, &cb_data);
377
378 return cb_data.status;
379 }
380
381 /*
382 * read()
383 */
384 int nfs_read(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count, char *buffer)
385 {
386 struct sync_cb_data cb_data;
387
388 cb_data.is_finished = 0;
389 cb_data.return_data = buffer;
390 cb_data.call = "read";
391
392 if (nfs_read_async(nfs, nfsfh, count, pread_cb, &cb_data) != 0) {
393 nfs_set_error(nfs, "nfs_read_async failed");
394 return -1;
395 }
396
397 wait_for_nfs_reply(nfs, &cb_data);
398
399 return cb_data.status;
400 }
401
402 /*
403 * close()
404 */
405 static void close_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
406 {
407 struct sync_cb_data *cb_data = private_data;
408 cb_data->is_finished = 1;
409 cb_data->status = status;
410
411 if (status < 0) {
412 nfs_set_error(nfs, "close call failed with \"%s\"", (char *)data);
413 return;
414 }
415 }
416
417 int nfs_close(struct nfs_context *nfs, struct nfsfh *nfsfh)
418 {
419 struct sync_cb_data cb_data;
420
421 cb_data.is_finished = 0;
422
423 if (nfs_close_async(nfs, nfsfh, close_cb, &cb_data) != 0) {
424 nfs_set_error(nfs, "nfs_close_async failed");
425 return -1;
426 }
427
428 wait_for_nfs_reply(nfs, &cb_data);
429
430 return cb_data.status;
431 }
432
433
434
435
436 /*
437 * fstat()
438 */
439 int nfs_fstat(struct nfs_context *nfs, struct nfsfh *nfsfh, struct stat *st)
440 {
441 struct sync_cb_data cb_data;
442
443 cb_data.is_finished = 0;
444 cb_data.return_data = st;
445
446 if (nfs_fstat_async(nfs, nfsfh, stat_cb, &cb_data) != 0) {
447 nfs_set_error(nfs, "nfs_fstat_async failed");
448 return -1;
449 }
450
451 wait_for_nfs_reply(nfs, &cb_data);
452
453 return cb_data.status;
454 }
455
456
457 /*
458 * pwrite()
459 */
460 static void pwrite_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
461 {
462 struct sync_cb_data *cb_data = private_data;
463 cb_data->is_finished = 1;
464 cb_data->status = status;
465
466 if (status < 0)
467 nfs_set_error(nfs, "%s call failed with \"%s\"", cb_data->call, (char *)data);
468 }
469
470 int nfs_pwrite(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, uint64_t count, char *buf)
471 {
472 struct sync_cb_data cb_data;
473
474 cb_data.is_finished = 0;
475 cb_data.call = "pwrite";
476
477 if (nfs_pwrite_async(nfs, nfsfh, offset, count, buf, pwrite_cb, &cb_data) != 0) {
478 nfs_set_error(nfs, "nfs_pwrite_async failed");
479 return -1;
480 }
481
482 wait_for_nfs_reply(nfs, &cb_data);
483
484 return cb_data.status;
485 }
486
487 /*
488 * write()
489 */
490 int nfs_write(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count, char *buf)
491 {
492 struct sync_cb_data cb_data;
493
494 cb_data.is_finished = 0;
495 cb_data.call = "write";
496
497 if (nfs_write_async(nfs, nfsfh, count, buf, pwrite_cb, &cb_data) != 0) {
498 nfs_set_error(nfs, "nfs_write_async failed");
499 return -1;
500 }
501
502 wait_for_nfs_reply(nfs, &cb_data);
503
504 return cb_data.status;
505 }
506
507
508 /*
509 * fsync()
510 */
511 static void fsync_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
512 {
513 struct sync_cb_data *cb_data = private_data;
514 cb_data->is_finished = 1;
515 cb_data->status = status;
516
517 if (status < 0) {
518 nfs_set_error(nfs, "fsync call failed with \"%s\"", (char *)data);
519 return;
520 }
521 }
522
523 int nfs_fsync(struct nfs_context *nfs, struct nfsfh *nfsfh)
524 {
525 struct sync_cb_data cb_data;
526
527 cb_data.is_finished = 0;
528
529 if (nfs_fsync_async(nfs, nfsfh, fsync_cb, &cb_data) != 0) {
530 nfs_set_error(nfs, "nfs_fsync_async failed");
531 return -1;
532 }
533
534 wait_for_nfs_reply(nfs, &cb_data);
535
536 return cb_data.status;
537 }
538
539
540
541
542 /*
543 * ftruncate()
544 */
545 static void ftruncate_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
546 {
547 struct sync_cb_data *cb_data = private_data;
548 cb_data->is_finished = 1;
549 cb_data->status = status;
550
551 if (status < 0) {
552 nfs_set_error(nfs, "ftruncate call failed with \"%s\"", (char *)data);
553 return;
554 }
555 }
556
557 int nfs_ftruncate(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t length)
558 {
559 struct sync_cb_data cb_data;
560
561 cb_data.is_finished = 0;
562
563 if (nfs_ftruncate_async(nfs, nfsfh, length, ftruncate_cb, &cb_data) != 0) {
564 nfs_set_error(nfs, "nfs_ftruncate_async failed");
565 return -1;
566 }
567
568 wait_for_nfs_reply(nfs, &cb_data);
569
570 return cb_data.status;
571 }
572
573
574
575 /*
576 * truncate()
577 */
578 static void truncate_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
579 {
580 struct sync_cb_data *cb_data = private_data;
581 cb_data->is_finished = 1;
582 cb_data->status = status;
583
584 if (status < 0) {
585 nfs_set_error(nfs, "truncate call failed with \"%s\"", (char *)data);
586 return;
587 }
588 }
589
590 int nfs_truncate(struct nfs_context *nfs, const char *path, uint64_t length)
591 {
592 struct sync_cb_data cb_data;
593
594 cb_data.is_finished = 0;
595
596 if (nfs_truncate_async(nfs, path, length, truncate_cb, &cb_data) != 0) {
597 nfs_set_error(nfs, "nfs_ftruncate_async failed");
598 return -1;
599 }
600
601 wait_for_nfs_reply(nfs, &cb_data);
602
603 return cb_data.status;
604 }
605
606
607
608
609
610 /*
611 * mkdir()
612 */
613 static void mkdir_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
614 {
615 struct sync_cb_data *cb_data = private_data;
616 cb_data->is_finished = 1;
617 cb_data->status = status;
618
619 if (status < 0) {
620 nfs_set_error(nfs, "mkdir call failed with \"%s\"", (char *)data);
621 return;
622 }
623 }
624
625 int nfs_mkdir(struct nfs_context *nfs, const char *path)
626 {
627 struct sync_cb_data cb_data;
628
629 cb_data.is_finished = 0;
630
631 if (nfs_mkdir_async(nfs, path, mkdir_cb, &cb_data) != 0) {
632 nfs_set_error(nfs, "nfs_mkdir_async failed");
633 return -1;
634 }
635
636 wait_for_nfs_reply(nfs, &cb_data);
637
638 return cb_data.status;
639 }
640
641
642
643
644
645 /*
646 * rmdir()
647 */
648 static void rmdir_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
649 {
650 struct sync_cb_data *cb_data = private_data;
651 cb_data->is_finished = 1;
652 cb_data->status = status;
653
654 if (status < 0) {
655 nfs_set_error(nfs, "rmdir call failed with \"%s\"", (char *)data);
656 return;
657 }
658 }
659
660 int nfs_rmdir(struct nfs_context *nfs, const char *path)
661 {
662 struct sync_cb_data cb_data;
663
664 cb_data.is_finished = 0;
665
666 if (nfs_rmdir_async(nfs, path, rmdir_cb, &cb_data) != 0) {
667 nfs_set_error(nfs, "nfs_rmdir_async failed");
668 return -1;
669 }
670
671 wait_for_nfs_reply(nfs, &cb_data);
672
673 return cb_data.status;
674 }
675
676
677
678 /*
679 * creat()
680 */
681 static void creat_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
682 {
683 struct sync_cb_data *cb_data = private_data;
684 struct nfsfh *fh, **nfsfh;
685
686 cb_data->is_finished = 1;
687 cb_data->status = status;
688
689 if (status < 0) {
690 nfs_set_error(nfs, "creat call failed with \"%s\"", (char *)data);
691 return;
692 }
693
694 fh = data;
695 nfsfh = cb_data->return_data;
696 *nfsfh = fh;
697 }
698
699 int nfs_create(struct nfs_context *nfs, const char *path, int flags, int mode, struct nfsfh **nfsfh)
700 {
701 struct sync_cb_data cb_data;
702
703 cb_data.is_finished = 0;
704 cb_data.return_data = nfsfh;
705
706 if (nfs_create_async(nfs, path, flags, mode, creat_cb, &cb_data) != 0) {
707 nfs_set_error(nfs, "nfs_create_async failed");
708 return -1;
709 }
710
711 wait_for_nfs_reply(nfs, &cb_data);
712
713 return cb_data.status;
714 }
715
716 int nfs_creat(struct nfs_context *nfs, const char *path, int mode, struct nfsfh **nfsfh)
717 {
718 return nfs_create(nfs, path, 0, mode, nfsfh);
719 }
720
721 /*
722 * mknod()
723 */
724 static void mknod_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
725 {
726 struct sync_cb_data *cb_data = private_data;
727
728 cb_data->is_finished = 1;
729 cb_data->status = status;
730
731 if (status < 0) {
732 nfs_set_error(nfs, "mknod call failed with \"%s\"", (char *)data);
733 return;
734 }
735 }
736
737 int nfs_mknod(struct nfs_context *nfs, const char *path, int mode, int dev)
738 {
739 struct sync_cb_data cb_data;
740
741 cb_data.is_finished = 0;
742
743 if (nfs_mknod_async(nfs, path, mode, dev, mknod_cb, &cb_data) != 0) {
744 nfs_set_error(nfs, "nfs_creat_async failed");
745 return -1;
746 }
747
748 wait_for_nfs_reply(nfs, &cb_data);
749
750 return cb_data.status;
751 }
752
753
754 /*
755 * unlink()
756 */
757 static void unlink_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
758 {
759 struct sync_cb_data *cb_data = private_data;
760
761 cb_data->is_finished = 1;
762 cb_data->status = status;
763
764 if (status < 0) {
765 nfs_set_error(nfs, "unlink call failed with \"%s\"", (char *)data);
766 return;
767 }
768 }
769
770 int nfs_unlink(struct nfs_context *nfs, const char *path)
771 {
772 struct sync_cb_data cb_data;
773
774 cb_data.is_finished = 0;
775
776 if (nfs_unlink_async(nfs, path, unlink_cb, &cb_data) != 0) {
777 nfs_set_error(nfs, "nfs_unlink_async failed");
778 return -1;
779 }
780
781 wait_for_nfs_reply(nfs, &cb_data);
782
783 return cb_data.status;
784 }
785
786
787
788 /*
789 * opendir()
790 */
791 static void opendir_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
792 {
793 struct sync_cb_data *cb_data = private_data;
794 struct nfsdir *dir, **nfsdir;
795
796 cb_data->is_finished = 1;
797 cb_data->status = status;
798
799 if (status < 0) {
800 nfs_set_error(nfs, "opendir call failed with \"%s\"", (char *)data);
801 return;
802 }
803
804 dir = data;
805 nfsdir = cb_data->return_data;
806 *nfsdir = dir;
807 }
808
809 int nfs_opendir(struct nfs_context *nfs, const char *path, struct nfsdir **nfsdir)
810 {
811 struct sync_cb_data cb_data;
812
813 cb_data.is_finished = 0;
814 cb_data.return_data = nfsdir;
815
816 if (nfs_opendir_async(nfs, path, opendir_cb, &cb_data) != 0) {
817 nfs_set_error(nfs, "nfs_opendir_async failed");
818 return -1;
819 }
820
821 wait_for_nfs_reply(nfs, &cb_data);
822
823 return cb_data.status;
824 }
825
826
827 /*
828 * lseek()
829 */
830 static void lseek_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
831 {
832 struct sync_cb_data *cb_data = private_data;
833
834 cb_data->is_finished = 1;
835 cb_data->status = status;
836
837 if (status < 0) {
838 nfs_set_error(nfs, "lseek call failed with \"%s\"", (char *)data);
839 return;
840 }
841
842 if (cb_data->return_data != NULL) {
843 memcpy(cb_data->return_data, data, sizeof(uint64_t));
844 }
845 }
846
847 int nfs_lseek(struct nfs_context *nfs, struct nfsfh *nfsfh, int64_t offset, int whence, uint64_t *current_offset)
848 {
849 struct sync_cb_data cb_data;
850
851 cb_data.is_finished = 0;
852 cb_data.return_data = current_offset;
853
854 if (nfs_lseek_async(nfs, nfsfh, offset, whence, lseek_cb, &cb_data) != 0) {
855 nfs_set_error(nfs, "nfs_lseek_async failed");
856 return -1;
857 }
858
859 wait_for_nfs_reply(nfs, &cb_data);
860
861 return cb_data.status;
862 }
863
864
865
866 /*
867 * statvfs()
868 */
869 static void statvfs_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
870 {
871 struct sync_cb_data *cb_data = private_data;
872
873 cb_data->is_finished = 1;
874 cb_data->status = status;
875
876 if (status < 0) {
877 nfs_set_error(nfs, "statvfs call failed with \"%s\"", (char *)data);
878 return;
879 }
880
881 memcpy(cb_data->return_data, data, sizeof(struct statvfs));
882 }
883
884 int nfs_statvfs(struct nfs_context *nfs, const char *path, struct statvfs *svfs)
885 {
886 struct sync_cb_data cb_data;
887
888 cb_data.is_finished = 0;
889 cb_data.return_data = svfs;
890
891 if (nfs_statvfs_async(nfs, path, statvfs_cb, &cb_data) != 0) {
892 nfs_set_error(nfs, "nfs_statvfs_async failed");
893 return -1;
894 }
895
896 wait_for_nfs_reply(nfs, &cb_data);
897
898 return cb_data.status;
899 }
900
901
902
903
904
905 /*
906 * readlink()
907 */
908 static void readlink_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
909 {
910 struct sync_cb_data *cb_data = private_data;
911
912 cb_data->is_finished = 1;
913 cb_data->status = status;
914
915 if (status < 0) {
916 nfs_set_error(nfs, "readlink call failed with \"%s\"", (char *)data);
917 return;
918 }
919
920 if (strlen(data) > (size_t)cb_data->return_int) {
921 nfs_set_error(nfs, "Too small buffer for readlink");
922 cb_data->status = -ENAMETOOLONG;
923 return;
924 }
925
926 memcpy(cb_data->return_data, data, strlen(data)+1);
927 }
928
929 int nfs_readlink(struct nfs_context *nfs, const char *path, char *buf, int bufsize)
930 {
931 struct sync_cb_data cb_data;
932
933 cb_data.is_finished = 0;
934 cb_data.return_data = buf;
935 cb_data.return_int = bufsize;
936
937 if (nfs_readlink_async(nfs, path, readlink_cb, &cb_data) != 0) {
938 nfs_set_error(nfs, "nfs_readlink_async failed");
939 return -1;
940 }
941
942 wait_for_nfs_reply(nfs, &cb_data);
943
944 return cb_data.status;
945 }
946
947
948
949 /*
950 * chmod()
951 */
952 static void chmod_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
953 {
954 struct sync_cb_data *cb_data = private_data;
955
956 cb_data->is_finished = 1;
957 cb_data->status = status;
958
959 if (status < 0) {
960 nfs_set_error(nfs, "chmod call failed with \"%s\"", (char *)data);
961 return;
962 }
963 }
964
965 int nfs_chmod(struct nfs_context *nfs, const char *path, int mode)
966 {
967 struct sync_cb_data cb_data;
968
969 cb_data.is_finished = 0;
970
971 if (nfs_chmod_async(nfs, path, mode, chmod_cb, &cb_data) != 0) {
972 nfs_set_error(nfs, "nfs_chmod_async failed");
973 return -1;
974 }
975
976 wait_for_nfs_reply(nfs, &cb_data);
977
978 return cb_data.status;
979 }
980
981
982
983
984 /*
985 * fchmod()
986 */
987 static void fchmod_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
988 {
989 struct sync_cb_data *cb_data = private_data;
990
991 cb_data->is_finished = 1;
992 cb_data->status = status;
993
994 if (status < 0) {
995 nfs_set_error(nfs, "fchmod call failed with \"%s\"", (char *)data);
996 return;
997 }
998 }
999
1000 int nfs_fchmod(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode)
1001 {
1002 struct sync_cb_data cb_data;
1003
1004 cb_data.is_finished = 0;
1005
1006 if (nfs_fchmod_async(nfs, nfsfh, mode, fchmod_cb, &cb_data) != 0) {
1007 nfs_set_error(nfs, "nfs_fchmod_async failed");
1008 return -1;
1009 }
1010
1011 wait_for_nfs_reply(nfs, &cb_data);
1012
1013 return cb_data.status;
1014 }
1015
1016
1017
1018
1019 /*
1020 * chown()
1021 */
1022 static void chown_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
1023 {
1024 struct sync_cb_data *cb_data = private_data;
1025
1026 cb_data->is_finished = 1;
1027 cb_data->status = status;
1028
1029 if (status < 0) {
1030 nfs_set_error(nfs, "chown call failed with \"%s\"", (char *)data);
1031 return;
1032 }
1033 }
1034
1035 int nfs_chown(struct nfs_context *nfs, const char *path, int uid, int gid)
1036 {
1037 struct sync_cb_data cb_data;
1038
1039 cb_data.is_finished = 0;
1040
1041 if (nfs_chown_async(nfs, path, uid, gid, chown_cb, &cb_data) != 0) {
1042 nfs_set_error(nfs, "nfs_chown_async failed");
1043 return -1;
1044 }
1045
1046 wait_for_nfs_reply(nfs, &cb_data);
1047
1048 return cb_data.status;
1049 }
1050
1051 /*
1052 * fchown()
1053 */
1054 static void fchown_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
1055 {
1056 struct sync_cb_data *cb_data = private_data;
1057
1058 cb_data->is_finished = 1;
1059 cb_data->status = status;
1060
1061 if (status < 0) {
1062 nfs_set_error(nfs, "fchown call failed with \"%s\"", (char *)data);
1063 return;
1064 }
1065 }
1066
1067 int nfs_fchown(struct nfs_context *nfs, struct nfsfh *nfsfh, int uid, int gid)
1068 {
1069 struct sync_cb_data cb_data;
1070
1071 cb_data.is_finished = 0;
1072
1073 if (nfs_fchown_async(nfs, nfsfh, uid, gid, fchown_cb, &cb_data) != 0) {
1074 nfs_set_error(nfs, "nfs_fchown_async failed");
1075 return -1;
1076 }
1077
1078 wait_for_nfs_reply(nfs, &cb_data);
1079
1080 return cb_data.status;
1081 }
1082
1083
1084
1085 /*
1086 * utimes()
1087 */
1088 static void utimes_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
1089 {
1090 struct sync_cb_data *cb_data = private_data;
1091
1092 cb_data->is_finished = 1;
1093 cb_data->status = status;
1094
1095 if (status < 0) {
1096 nfs_set_error(nfs, "utimes call failed with \"%s\"", (char *)data);
1097 return;
1098 }
1099 }
1100
1101 int nfs_utimes(struct nfs_context *nfs, const char *path, struct timeval *times)
1102 {
1103 struct sync_cb_data cb_data;
1104
1105 cb_data.is_finished = 0;
1106
1107 if (nfs_utimes_async(nfs, path, times, utimes_cb, &cb_data) != 0) {
1108 nfs_set_error(nfs, "nfs_utimes_async failed");
1109 return -1;
1110 }
1111
1112 wait_for_nfs_reply(nfs, &cb_data);
1113
1114 return cb_data.status;
1115 }
1116
1117
1118
1119 /*
1120 * utime()
1121 */
1122 static void utime_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
1123 {
1124 struct sync_cb_data *cb_data = private_data;
1125
1126 cb_data->is_finished = 1;
1127 cb_data->status = status;
1128
1129 if (status < 0) {
1130 nfs_set_error(nfs, "utime call failed with \"%s\"", (char *)data);
1131 return;
1132 }
1133 }
1134
1135 int nfs_utime(struct nfs_context *nfs, const char *path, struct utimbuf *times)
1136 {
1137 struct sync_cb_data cb_data;
1138
1139 cb_data.is_finished = 0;
1140
1141 if (nfs_utime_async(nfs, path, times, utime_cb, &cb_data) != 0) {
1142 nfs_set_error(nfs, "nfs_utimes_async failed");
1143 return -1;
1144 }
1145
1146 wait_for_nfs_reply(nfs, &cb_data);
1147
1148 return cb_data.status;
1149 }
1150
1151
1152
1153
1154 /*
1155 * access()
1156 */
1157 static void access_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
1158 {
1159 struct sync_cb_data *cb_data = private_data;
1160
1161 cb_data->is_finished = 1;
1162 cb_data->status = status;
1163
1164 if (status < 0) {
1165 nfs_set_error(nfs, "access call failed with \"%s\"", (char *)data);
1166 return;
1167 }
1168 }
1169
1170 int nfs_access(struct nfs_context *nfs, const char *path, int mode)
1171 {
1172 struct sync_cb_data cb_data;
1173
1174 cb_data.is_finished = 0;
1175
1176 if (nfs_access_async(nfs, path, mode, access_cb, &cb_data) != 0) {
1177 nfs_set_error(nfs, "nfs_access_async failed");
1178 return -1;
1179 }
1180
1181 wait_for_nfs_reply(nfs, &cb_data);
1182
1183 return cb_data.status;
1184 }
1185
1186
1187
1188 /*
1189 * symlink()
1190 */
1191 static void symlink_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
1192 {
1193 struct sync_cb_data *cb_data = private_data;
1194
1195 cb_data->is_finished = 1;
1196 cb_data->status = status;
1197
1198 if (status < 0) {
1199 nfs_set_error(nfs, "symlink call failed with \"%s\"", (char *)data);
1200 return;
1201 }
1202 }
1203
1204 int nfs_symlink(struct nfs_context *nfs, const char *oldpath, const char *newpath)
1205 {
1206 struct sync_cb_data cb_data;
1207
1208 cb_data.is_finished = 0;
1209
1210 if (nfs_symlink_async(nfs, oldpath, newpath, symlink_cb, &cb_data) != 0) {
1211 nfs_set_error(nfs, "nfs_symlink_async failed");
1212 return -1;
1213 }
1214
1215 wait_for_nfs_reply(nfs, &cb_data);
1216
1217 return cb_data.status;
1218 }
1219
1220
1221
1222 /*
1223 * rename()
1224 */
1225 static void rename_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
1226 {
1227 struct sync_cb_data *cb_data = private_data;
1228
1229 cb_data->is_finished = 1;
1230 cb_data->status = status;
1231
1232 if (status < 0) {
1233 nfs_set_error(nfs, "rename call failed with \"%s\"", (char *)data);
1234 return;
1235 }
1236 }
1237
1238 int nfs_rename(struct nfs_context *nfs, const char *oldpath, const char *newpath)
1239 {
1240 struct sync_cb_data cb_data;
1241
1242 cb_data.is_finished = 0;
1243
1244 if (nfs_rename_async(nfs, oldpath, newpath, rename_cb, &cb_data) != 0) {
1245 nfs_set_error(nfs, "nfs_rename_async failed");
1246 return -1;
1247 }
1248
1249 wait_for_nfs_reply(nfs, &cb_data);
1250
1251 return cb_data.status;
1252 }
1253
1254
1255
1256 /*
1257 * link()
1258 */
1259 static void link_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
1260 {
1261 struct sync_cb_data *cb_data = private_data;
1262
1263 cb_data->is_finished = 1;
1264 cb_data->status = status;
1265
1266 if (status < 0) {
1267 nfs_set_error(nfs, "link call failed with \"%s\"", (char *)data);
1268 return;
1269 }
1270 }
1271
1272 int nfs_link(struct nfs_context *nfs, const char *oldpath, const char *newpath)
1273 {
1274 struct sync_cb_data cb_data;
1275
1276 cb_data.is_finished = 0;
1277
1278 if (nfs_link_async(nfs, oldpath, newpath, link_cb, &cb_data) != 0) {
1279 nfs_set_error(nfs, "nfs_link_async failed");
1280 return -1;
1281 }
1282
1283 wait_for_nfs_reply(nfs, &cb_data);
1284
1285 return cb_data.status;
1286 }
1287
1288 void mount_getexports_cb(struct rpc_context *mount_context, int status, void *data, void *private_data)
1289 {
1290 struct sync_cb_data *cb_data = private_data;
1291 exports export;
1292
1293 assert(mount_context->magic == RPC_CONTEXT_MAGIC);
1294
1295 cb_data->is_finished = 1;
1296 cb_data->status = status;
1297 cb_data->return_data = NULL;
1298
1299 if (status != 0) {
1300 rpc_set_error(mount_context, "mount/export call failed with \"%s\"", (char *)data);
1301 return;
1302 }
1303
1304 export = *(exports *)data;
1305 while (export != NULL) {
1306 exports new_export;
1307
1308 new_export = malloc(sizeof(*new_export));
1309 memset(new_export, 0, sizeof(*new_export));
1310 new_export->ex_dir = strdup(export->ex_dir);
1311 new_export->ex_next = cb_data->return_data;
1312
1313 cb_data->return_data = new_export;
1314
1315 export = export->ex_next;
1316 }
1317 }
1318
1319 struct exportnode *mount_getexports(const char *server)
1320 {
1321 struct sync_cb_data cb_data;
1322 struct rpc_context *rpc;
1323
1324
1325 cb_data.is_finished = 0;
1326 cb_data.return_data = NULL;
1327
1328 rpc = rpc_init_context();
1329 if (mount_getexports_async(rpc, server, mount_getexports_cb, &cb_data) != 0) {
1330 rpc_destroy_context(rpc);
1331 return NULL;
1332 }
1333
1334 wait_for_reply(rpc, &cb_data);
1335 rpc_destroy_context(rpc);
1336
1337 return cb_data.return_data;
1338 }
1339
1340 void mount_free_export_list(struct exportnode *exports)
1341 {
1342 struct exportnode *tmp;
1343
1344 while ((tmp = exports)) {
1345 exports = exports->ex_next;
1346 free(tmp->ex_dir);
1347 free(tmp);
1348 }
1349 }
1350
1351
1352
1353 void free_nfs_srvr_list(struct nfs_server_list *srv)
1354 {
1355 while (srv != NULL) {
1356 struct nfs_server_list *next = srv->next;
1357
1358 free(srv->addr);
1359 free(srv);
1360 srv = next;
1361 }
1362 }
1363
1364 struct nfs_list_data {
1365 int status;
1366 struct nfs_server_list *srvrs;
1367 };
1368
1369 void callit_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
1370 {
1371 struct nfs_list_data *srv_data = private_data;
1372 struct sockaddr *sin;
1373 char hostdd[16];
1374 struct nfs_server_list *srvr;
1375
1376 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1377
1378 if (status == RPC_STATUS_CANCEL) {
1379 return;
1380 }
1381 if (status != 0) {
1382 srv_data->status = -1;
1383 return;
1384 }
1385
1386 sin = rpc_get_recv_sockaddr(rpc);
1387 if (sin == NULL) {
1388 rpc_set_error(rpc, "failed to get sockaddr in CALLIT callback");
1389 srv_data->status = -1;
1390 return;
1391 }
1392
1393 if (getnameinfo(sin, sizeof(struct sockaddr_in), &hostdd[0], sizeof(hostdd), NULL, 0, NI_NUMERICHOST) < 0) {
1394 rpc_set_error(rpc, "getnameinfo failed in CALLIT callback");
1395 srv_data->status = -1;
1396 return;
1397 }
1398
1399 /* check for dupes */
1400 for (srvr = srv_data->srvrs; srvr; srvr = srvr->next) {
1401 if (!strcmp(hostdd, srvr->addr)) {
1402 return;
1403 }
1404 }
1405
1406 srvr = malloc(sizeof(struct nfs_server_list));
1407 if (srvr == NULL) {
1408 rpc_set_error(rpc, "Malloc failed when allocating server structure");
1409 srv_data->status = -1;
1410 return;
1411 }
1412
1413 srvr->addr = strdup(hostdd);
1414 if (srvr->addr == NULL) {
1415 rpc_set_error(rpc, "Strdup failed when allocating server structure");
1416 free(srvr);
1417 srv_data->status = -1;
1418 return;
1419 }
1420
1421 srvr->next = srv_data->srvrs;
1422 srv_data->srvrs = srvr;
1423 }
1424
1425 #ifdef WIN32
1426
1427 static int send_nfsd_probes(struct rpc_context *rpc, INTERFACE_INFO *InterfaceList, int numIfs, struct nfs_list_data *data)
1428 {
1429 int i=0;
1430
1431 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1432
1433 for(i = 0; i < numIfs; i++)
1434 {
1435 SOCKADDR *pAddress;
1436 char bcdd[16];
1437 unsigned long nFlags = 0;
1438
1439 pAddress = (SOCKADDR *) & (InterfaceList[i].iiBroadcastAddress);
1440
1441 if(pAddress->sa_family != AF_INET)
1442 continue;
1443
1444 nFlags = InterfaceList[i].iiFlags;
1445
1446 if (!(nFlags & IFF_UP))
1447 {
1448 continue;
1449 }
1450
1451 if (nFlags & IFF_LOOPBACK)
1452 {
1453 continue;
1454 }
1455
1456 if (!(nFlags & IFF_BROADCAST))
1457 {
1458 continue;
1459 }
1460
1461 if (getnameinfo(pAddress, sizeof(struct sockaddr_in), &bcdd[0], sizeof(bcdd), NULL, 0, NI_NUMERICHOST) < 0)
1462 {
1463 continue;
1464 }
1465
1466 if (rpc_set_udp_destination(rpc, bcdd, 111, 1) < 0)
1467 {
1468 return -1;
1469 }
1470
1471 if (rpc_pmap2_callit_async(rpc, MOUNT_PROGRAM, 2, 0, NULL, 0, callit_cb, data) < 0)
1472 {
1473 return -1;
1474 }
1475 }
1476 return 0;
1477 }
1478
1479 struct nfs_server_list *nfs_find_local_servers(void)
1480 {
1481 struct rpc_context *rpc;
1482 struct nfs_list_data data = {0, NULL};
1483 struct timeval tv_start, tv_current;
1484 int loop;
1485 struct pollfd pfd;
1486 INTERFACE_INFO InterfaceList[20];
1487 unsigned long nBytesReturned;
1488 int nNumInterfaces = 0;
1489
1490 rpc = rpc_init_udp_context();
1491 if (rpc == NULL)
1492 {
1493 return NULL;
1494 }
1495
1496 if (rpc_bind_udp(rpc, "0.0.0.0", 0) < 0)
1497 {
1498 rpc_destroy_context(rpc);
1499 return NULL;
1500 }
1501
1502 if (WSAIoctl(rpc_get_fd(rpc), SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList, sizeof(InterfaceList), &nBytesReturned, 0, 0) == SOCKET_ERROR)
1503 {
1504 return NULL;
1505 }
1506
1507 nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO);
1508
1509 for (loop=0; loop<3; loop++)
1510 {
1511 if (send_nfsd_probes(rpc, InterfaceList, nNumInterfaces, &data) != 0)
1512 {
1513 rpc_destroy_context(rpc);
1514 return NULL;
1515 }
1516
1517 win32_gettimeofday(&tv_start, NULL);
1518 for(;;)
1519 {
1520 int mpt;
1521
1522 pfd.fd = rpc_get_fd(rpc);
1523 pfd.events = rpc_which_events(rpc);
1524
1525 win32_gettimeofday(&tv_current, NULL);
1526 mpt = 1000
1527 - (tv_current.tv_sec *1000 + tv_current.tv_usec / 1000)
1528 + (tv_start.tv_sec *1000 + tv_start.tv_usec / 1000);
1529
1530 if (poll(&pfd, 1, mpt) < 0)
1531 {
1532 free_nfs_srvr_list(data.srvrs);
1533 rpc_destroy_context(rpc);
1534 return NULL;
1535 }
1536 if (pfd.revents == 0)
1537 {
1538 break;
1539 }
1540
1541 if (rpc_service(rpc, pfd.revents) < 0)
1542 {
1543 break;
1544 }
1545 }
1546 }
1547
1548 rpc_destroy_context(rpc);
1549
1550 if (data.status != 0)
1551 {
1552 free_nfs_srvr_list(data.srvrs);
1553 return NULL;
1554 }
1555 return data.srvrs;
1556 }
1557 #else
1558
1559 static int send_nfsd_probes(struct rpc_context *rpc, struct ifconf *ifc, struct nfs_list_data *data)
1560 {
1561 char *ptr;
1562
1563 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1564
1565 for (ptr =(char *)(ifc->ifc_buf); ptr < (char *)(ifc->ifc_buf) + ifc->ifc_len; ) {
1566 struct ifreq *ifr;
1567 char bcdd[16];
1568
1569 ifr = (struct ifreq *)ptr;
1570 #ifdef HAVE_SOCKADDR_LEN
1571 if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)) {
1572 ptr += sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
1573 } else {
1574 ptr += sizeof(ifr->ifr_name) + sizeof(struct sockaddr);
1575 }
1576 #else
1577 ptr += sizeof(struct ifreq);
1578 #endif
1579
1580 if (ifr->ifr_addr.sa_family != AF_INET) {
1581 continue;
1582 }
1583 if (ioctl(rpc_get_fd(rpc), SIOCGIFFLAGS, ifr) < 0) {
1584 return -1;
1585 }
1586 if (!(ifr->ifr_flags & IFF_UP)) {
1587 continue;
1588 }
1589 if (ifr->ifr_flags & IFF_LOOPBACK) {
1590 continue;
1591 }
1592 if (!(ifr->ifr_flags & IFF_BROADCAST)) {
1593 continue;
1594 }
1595 if (ioctl(rpc_get_fd(rpc), SIOCGIFBRDADDR, ifr) < 0) {
1596 continue;
1597 }
1598 if (getnameinfo(&ifr->ifr_broadaddr, sizeof(struct sockaddr_in), &bcdd[0], sizeof(bcdd), NULL, 0, NI_NUMERICHOST) < 0) {
1599 continue;
1600 }
1601 if (rpc_set_udp_destination(rpc, bcdd, 111, 1) < 0) {
1602 return -1;
1603 }
1604
1605 if (rpc_pmap2_callit_async(rpc, MOUNT_PROGRAM, 2, 0, NULL, 0, callit_cb, data) < 0) {
1606 return -1;
1607 }
1608 }
1609
1610 return 0;
1611 }
1612
1613 struct nfs_server_list *nfs_find_local_servers(void)
1614 {
1615 struct rpc_context *rpc;
1616 struct nfs_list_data data = {0, NULL};
1617 struct timeval tv_start, tv_current;
1618 struct ifconf ifc;
1619 int size, loop;
1620 struct pollfd pfd;
1621
1622 rpc = rpc_init_udp_context();
1623 if (rpc == NULL) {
1624 return NULL;
1625 }
1626
1627 if (rpc_bind_udp(rpc, "0.0.0.0", 0) < 0) {
1628 rpc_destroy_context(rpc);
1629 return NULL;
1630 }
1631
1632
1633 /* get list of all interfaces */
1634 size = sizeof(struct ifreq);
1635 ifc.ifc_buf = NULL;
1636 ifc.ifc_len = size;
1637
1638 while(ifc.ifc_len > (size - sizeof(struct ifreq))) {
1639 size *= 2;
1640
1641 free(ifc.ifc_buf);
1642 ifc.ifc_len = size;
1643 ifc.ifc_buf = malloc(size);
1644 memset(ifc.ifc_buf, 0, size);
1645 if (ioctl(rpc_get_fd(rpc), SIOCGIFCONF, (caddr_t)&ifc) < 0) {
1646 rpc_destroy_context(rpc);
1647 free(ifc.ifc_buf);
1648 return NULL;
1649 }
1650 }
1651
1652 for (loop=0; loop<3; loop++) {
1653 if (send_nfsd_probes(rpc, &ifc, &data) != 0) {
1654 rpc_destroy_context(rpc);
1655 free(ifc.ifc_buf);
1656 return NULL;
1657 }
1658
1659 gettimeofday(&tv_start, NULL);
1660 for(;;) {
1661 int mpt;
1662
1663 pfd.fd = rpc_get_fd(rpc);
1664 pfd.events = rpc_which_events(rpc);
1665
1666 gettimeofday(&tv_current, NULL);
1667 mpt = 1000
1668 - (tv_current.tv_sec *1000 + tv_current.tv_usec / 1000)
1669 + (tv_start.tv_sec *1000 + tv_start.tv_usec / 1000);
1670
1671 if (poll(&pfd, 1, mpt) < 0) {
1672 free_nfs_srvr_list(data.srvrs);
1673 rpc_destroy_context(rpc);
1674 return NULL;
1675 }
1676 if (pfd.revents == 0) {
1677 break;
1678 }
1679
1680 if (rpc_service(rpc, pfd.revents) < 0) {
1681 break;
1682 }
1683 }
1684 }
1685
1686 free(ifc.ifc_buf);
1687 rpc_destroy_context(rpc);
1688
1689 if (data.status != 0) {
1690 free_nfs_srvr_list(data.srvrs);
1691 return NULL;
1692 }
1693 return data.srvrs;
1694 }
1695 #endif//WIN32