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