aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-09-14 15:46:33 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-09-14 15:46:33 +0000
commit25098f7fd517367e9f55adb3aa49d3030187404f (patch)
treec572d17c1ca601fec7dd8c165ad85c3dbb391d9d
parent6eb2f8edceaa09ff46153d64df13b1dc918e8139 (diff)
downloadbusybox-w32-25098f7fd517367e9f55adb3aa49d3030187404f.tar.gz
busybox-w32-25098f7fd517367e9f55adb3aa49d3030187404f.tar.bz2
busybox-w32-25098f7fd517367e9f55adb3aa49d3030187404f.zip
mount: move code from nfsmount.c into mount.c
-rw-r--r--include/libbb.h3
-rw-r--r--util-linux/Makefile.in1
-rw-r--r--util-linux/mount.c1023
-rw-r--r--util-linux/nfsmount.c1022
4 files changed, 1018 insertions, 1031 deletions
diff --git a/include/libbb.h b/include/libbb.h
index dfb7a70bd..10309cff2 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -303,9 +303,6 @@ extern int set_loop(char **device, const char *file, int offset);
303extern int vdprintf(int d, const char *format, va_list ap); 303extern int vdprintf(int d, const char *format, va_list ap);
304#endif 304#endif
305 305
306int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts);
307int nfsmount(struct mntent *mp, int vfsflags, char *filteropts);
308
309/* Include our own copy of struct sysinfo to avoid binary compatibility 306/* Include our own copy of struct sysinfo to avoid binary compatibility
310 * problems with Linux 2.4, which changed things. Grumble, grumble. */ 307 * problems with Linux 2.4, which changed things. Grumble, grumble. */
311struct sysinfo { 308struct sysinfo {
diff --git a/util-linux/Makefile.in b/util-linux/Makefile.in
index d12f9f24f..3d6c96a35 100644
--- a/util-linux/Makefile.in
+++ b/util-linux/Makefile.in
@@ -29,7 +29,6 @@ UTILLINUX-$(CONFIG_MKFS_MINIX) +=mkfs_minix.o
29UTILLINUX-$(CONFIG_MKSWAP) +=mkswap.o 29UTILLINUX-$(CONFIG_MKSWAP) +=mkswap.o
30UTILLINUX-$(CONFIG_MORE) +=more.o 30UTILLINUX-$(CONFIG_MORE) +=more.o
31UTILLINUX-$(CONFIG_MOUNT) +=mount.o 31UTILLINUX-$(CONFIG_MOUNT) +=mount.o
32UTILLINUX-$(CONFIG_FEATURE_MOUNT_NFS) +=nfsmount.o
33UTILLINUX-$(CONFIG_PIVOT_ROOT) +=pivot_root.o 32UTILLINUX-$(CONFIG_PIVOT_ROOT) +=pivot_root.o
34UTILLINUX-$(CONFIG_RDATE) +=rdate.o 33UTILLINUX-$(CONFIG_RDATE) +=rdate.o
35UTILLINUX-$(CONFIG_READPROFILE) +=readprofile.o 34UTILLINUX-$(CONFIG_READPROFILE) +=readprofile.o
diff --git a/util-linux/mount.c b/util-linux/mount.c
index 4ed000e88..5592a7a5f 100644
--- a/util-linux/mount.c
+++ b/util-linux/mount.c
@@ -84,10 +84,10 @@ struct {
84 {"ro", MS_RDONLY}, // vfs flag 84 {"ro", MS_RDONLY}, // vfs flag
85 {"rw", ~MS_RDONLY}, // vfs flag 85 {"rw", ~MS_RDONLY}, // vfs flag
86 {"remount", MS_REMOUNT}, // action flag 86 {"remount", MS_REMOUNT}, // action flag
87
88
89}; 87};
90 88
89static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts);
90
91/* Append mount options to string */ 91/* Append mount options to string */
92static void append_mount_options(char **oldopts, char *newopts) 92static void append_mount_options(char **oldopts, char *newopts)
93{ 93{
@@ -196,7 +196,7 @@ static int fakeIt;
196#endif 196#endif
197 197
198// Perform actual mount of specific filesystem at specific location. 198// Perform actual mount of specific filesystem at specific location.
199int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts) 199static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
200{ 200{
201 int rc; 201 int rc;
202 202
@@ -234,7 +234,7 @@ int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
234 // Add vfs string flags 234 // Add vfs string flags
235 235
236 for(i=0; mount_options[i].flags != MS_REMOUNT; i++) 236 for(i=0; mount_options[i].flags != MS_REMOUNT; i++)
237 if (mount_options[i].flags > 0 && (mount_options[i].flags&vfsflags)) 237 if (mount_options[i].flags > 0 && (mount_options[i].flags & vfsflags))
238 append_mount_options(&(mp->mnt_opts), mount_options[i].name); 238 append_mount_options(&(mp->mnt_opts), mount_options[i].name);
239 239
240 // Remove trailing / (if any) from directory we mounted on 240 // Remove trailing / (if any) from directory we mounted on
@@ -260,8 +260,8 @@ int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
260 mp->mnt_fsname = srcbuf; 260 mp->mnt_fsname = srcbuf;
261 } 261 }
262 mp->mnt_type = "none"; 262 mp->mnt_type = "none";
263 mp->mnt_freq = mp->mnt_passno = 0;
264 } 263 }
264 mp->mnt_freq = mp->mnt_passno = 0;
265 265
266 // Write and close. 266 // Write and close.
267 267
@@ -613,3 +613,1016 @@ clean_up:
613 613
614 return rc; 614 return rc;
615} 615}
616
617
618#if ENABLE_FEATURE_MOUNT_NFS
619
620/*
621 * Linux NFS mount
622 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
623 *
624 * Licensed under GPLv2, see file LICENSE in this tarball for details.
625 *
626 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
627 * numbers to be specified on the command line.
628 *
629 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
630 * Omit the call to connect() for Linux version 1.3.11 or later.
631 *
632 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
633 * Implemented the "bg", "fg" and "retry" mount options for NFS.
634 *
635 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
636 * - added Native Language Support
637 *
638 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
639 * plus NFSv3 stuff.
640 */
641
642#include <syslog.h>
643#include <sys/utsname.h>
644#undef TRUE
645#undef FALSE
646#include <rpc/rpc.h>
647#include <rpc/pmap_prot.h>
648#include <rpc/pmap_clnt.h>
649
650/* This is just a warning of a common mistake. Possibly this should be a
651 * uclibc faq entry rather than in busybox... */
652#if ENABLE_FEATURE_MOUNT_NFS && defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
653#error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support."
654#endif
655
656#define MOUNTPORT 635
657#define MNTPATHLEN 1024
658#define MNTNAMLEN 255
659#define FHSIZE 32
660#define FHSIZE3 64
661
662typedef char fhandle[FHSIZE];
663
664typedef struct {
665 unsigned int fhandle3_len;
666 char *fhandle3_val;
667} fhandle3;
668
669enum mountstat3 {
670 MNT_OK = 0,
671 MNT3ERR_PERM = 1,
672 MNT3ERR_NOENT = 2,
673 MNT3ERR_IO = 5,
674 MNT3ERR_ACCES = 13,
675 MNT3ERR_NOTDIR = 20,
676 MNT3ERR_INVAL = 22,
677 MNT3ERR_NAMETOOLONG = 63,
678 MNT3ERR_NOTSUPP = 10004,
679 MNT3ERR_SERVERFAULT = 10006,
680};
681typedef enum mountstat3 mountstat3;
682
683struct fhstatus {
684 unsigned int fhs_status;
685 union {
686 fhandle fhs_fhandle;
687 } fhstatus_u;
688};
689typedef struct fhstatus fhstatus;
690
691struct mountres3_ok {
692 fhandle3 fhandle;
693 struct {
694 unsigned int auth_flavours_len;
695 char *auth_flavours_val;
696 } auth_flavours;
697};
698typedef struct mountres3_ok mountres3_ok;
699
700struct mountres3 {
701 mountstat3 fhs_status;
702 union {
703 mountres3_ok mountinfo;
704 } mountres3_u;
705};
706typedef struct mountres3 mountres3;
707
708typedef char *dirpath;
709
710typedef char *name;
711
712typedef struct mountbody *mountlist;
713
714struct mountbody {
715 name ml_hostname;
716 dirpath ml_directory;
717 mountlist ml_next;
718};
719typedef struct mountbody mountbody;
720
721typedef struct groupnode *groups;
722
723struct groupnode {
724 name gr_name;
725 groups gr_next;
726};
727typedef struct groupnode groupnode;
728
729typedef struct exportnode *exports;
730
731struct exportnode {
732 dirpath ex_dir;
733 groups ex_groups;
734 exports ex_next;
735};
736typedef struct exportnode exportnode;
737
738struct ppathcnf {
739 int pc_link_max;
740 short pc_max_canon;
741 short pc_max_input;
742 short pc_name_max;
743 short pc_path_max;
744 short pc_pipe_buf;
745 u_char pc_vdisable;
746 char pc_xxx;
747 short pc_mask[2];
748};
749typedef struct ppathcnf ppathcnf;
750
751#define MOUNTPROG 100005
752#define MOUNTVERS 1
753
754#define MOUNTPROC_NULL 0
755#define MOUNTPROC_MNT 1
756#define MOUNTPROC_DUMP 2
757#define MOUNTPROC_UMNT 3
758#define MOUNTPROC_UMNTALL 4
759#define MOUNTPROC_EXPORT 5
760#define MOUNTPROC_EXPORTALL 6
761
762#define MOUNTVERS_POSIX 2
763
764#define MOUNTPROC_PATHCONF 7
765
766#define MOUNT_V3 3
767
768#define MOUNTPROC3_NULL 0
769#define MOUNTPROC3_MNT 1
770#define MOUNTPROC3_DUMP 2
771#define MOUNTPROC3_UMNT 3
772#define MOUNTPROC3_UMNTALL 4
773#define MOUNTPROC3_EXPORT 5
774
775enum {
776#ifndef NFS_FHSIZE
777 NFS_FHSIZE = 32,
778#endif
779#ifndef NFS_PORT
780 NFS_PORT = 2049
781#endif
782};
783
784
785/*
786 * We want to be able to compile mount on old kernels in such a way
787 * that the binary will work well on more recent kernels.
788 * Thus, if necessary we teach nfsmount.c the structure of new fields
789 * that will come later.
790 *
791 * Moreover, the new kernel includes conflict with glibc includes
792 * so it is easiest to ignore the kernel altogether (at compile time).
793 */
794
795struct nfs2_fh {
796 char data[32];
797};
798struct nfs3_fh {
799 unsigned short size;
800 unsigned char data[64];
801};
802
803struct nfs_mount_data {
804 int version; /* 1 */
805 int fd; /* 1 */
806 struct nfs2_fh old_root; /* 1 */
807 int flags; /* 1 */
808 int rsize; /* 1 */
809 int wsize; /* 1 */
810 int timeo; /* 1 */
811 int retrans; /* 1 */
812 int acregmin; /* 1 */
813 int acregmax; /* 1 */
814 int acdirmin; /* 1 */
815 int acdirmax; /* 1 */
816 struct sockaddr_in addr; /* 1 */
817 char hostname[256]; /* 1 */
818 int namlen; /* 2 */
819 unsigned int bsize; /* 3 */
820 struct nfs3_fh root; /* 4 */
821};
822
823/* bits in the flags field */
824enum {
825 NFS_MOUNT_SOFT = 0x0001, /* 1 */
826 NFS_MOUNT_INTR = 0x0002, /* 1 */
827 NFS_MOUNT_SECURE = 0x0004, /* 1 */
828 NFS_MOUNT_POSIX = 0x0008, /* 1 */
829 NFS_MOUNT_NOCTO = 0x0010, /* 1 */
830 NFS_MOUNT_NOAC = 0x0020, /* 1 */
831 NFS_MOUNT_TCP = 0x0040, /* 2 */
832 NFS_MOUNT_VER3 = 0x0080, /* 3 */
833 NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
834 NFS_MOUNT_NONLM = 0x0200 /* 3 */
835};
836
837
838/*
839 * We need to translate between nfs status return values and
840 * the local errno values which may not be the same.
841 *
842 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
843 * "after #include <errno.h> the symbol errno is reserved for any use,
844 * it cannot even be used as a struct tag or field name".
845 */
846
847#ifndef EDQUOT
848#define EDQUOT ENOSPC
849#endif
850
851// Convert each NFSERR_BLAH into EBLAH
852
853static const struct {
854 int stat;
855 int errnum;
856} nfs_errtbl[] = {
857 {0,0}, {1,EPERM}, {2,ENOENT}, {5,EIO}, {6,ENXIO}, {13,EACCES}, {17,EEXIST},
858 {19,ENODEV}, {20,ENOTDIR}, {21,EISDIR}, {22,EINVAL}, {27,EFBIG},
859 {28,ENOSPC}, {30,EROFS}, {63,ENAMETOOLONG}, {66,ENOTEMPTY}, {69,EDQUOT},
860 {70,ESTALE}, {71,EREMOTE}, {-1,EIO}
861};
862
863static char *nfs_strerror(int status)
864{
865 int i;
866 static char buf[256];
867
868 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
869 if (nfs_errtbl[i].stat == status)
870 return strerror(nfs_errtbl[i].errnum);
871 }
872 sprintf(buf, "unknown nfs status return value: %d", status);
873 return buf;
874}
875
876static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
877{
878 if (!xdr_opaque(xdrs, objp, FHSIZE))
879 return FALSE;
880 return TRUE;
881}
882
883static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
884{
885 if (!xdr_u_int(xdrs, &objp->fhs_status))
886 return FALSE;
887 switch (objp->fhs_status) {
888 case 0:
889 if (!xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle))
890 return FALSE;
891 break;
892 default:
893 break;
894 }
895 return TRUE;
896}
897
898static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp)
899{
900 if (!xdr_string(xdrs, objp, MNTPATHLEN))
901 return FALSE;
902 return TRUE;
903}
904
905static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
906{
907 if (!xdr_bytes(xdrs, (char **)&objp->fhandle3_val, (unsigned int *) &objp->fhandle3_len, FHSIZE3))
908 return FALSE;
909 return TRUE;
910}
911
912static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
913{
914 if (!xdr_fhandle3(xdrs, &objp->fhandle))
915 return FALSE;
916 if (!xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val), &(objp->auth_flavours.auth_flavours_len), ~0,
917 sizeof (int), (xdrproc_t) xdr_int))
918 return FALSE;
919 return TRUE;
920}
921
922static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
923{
924 if (!xdr_enum(xdrs, (enum_t *) objp))
925 return FALSE;
926 return TRUE;
927}
928
929static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
930{
931 if (!xdr_mountstat3(xdrs, &objp->fhs_status))
932 return FALSE;
933 switch (objp->fhs_status) {
934 case MNT_OK:
935 if (!xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo))
936 return FALSE;
937 break;
938 default:
939 break;
940 }
941 return TRUE;
942}
943
944#define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
945
946/*
947 * nfs_mount_version according to the sources seen at compile time.
948 */
949static int nfs_mount_version;
950static int kernel_version;
951
952/*
953 * Unfortunately, the kernel prints annoying console messages
954 * in case of an unexpected nfs mount version (instead of
955 * just returning some error). Therefore we'll have to try
956 * and figure out what version the kernel expects.
957 *
958 * Variables:
959 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
960 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
961 * nfs_mount_version: version this source and running kernel can handle
962 */
963static void
964find_kernel_nfs_mount_version(void)
965{
966 if (kernel_version)
967 return;
968
969 nfs_mount_version = 4; /* default */
970
971 kernel_version = get_linux_version_code();
972 if (kernel_version) {
973 if (kernel_version < KERNEL_VERSION(2,1,32))
974 nfs_mount_version = 1;
975 else if (kernel_version < KERNEL_VERSION(2,2,18) ||
976 (kernel_version >= KERNEL_VERSION(2,3,0) &&
977 kernel_version < KERNEL_VERSION(2,3,99)))
978 nfs_mount_version = 3;
979 /* else v4 since 2.3.99pre4 */
980 }
981}
982
983static struct pmap *
984get_mountport(struct sockaddr_in *server_addr,
985 long unsigned prog,
986 long unsigned version,
987 long unsigned proto,
988 long unsigned port)
989{
990 struct pmaplist *pmap;
991 static struct pmap p = {0, 0, 0, 0};
992
993 server_addr->sin_port = PMAPPORT;
994 pmap = pmap_getmaps(server_addr);
995
996 if (version > MAX_NFSPROT)
997 version = MAX_NFSPROT;
998 if (!prog)
999 prog = MOUNTPROG;
1000 p.pm_prog = prog;
1001 p.pm_vers = version;
1002 p.pm_prot = proto;
1003 p.pm_port = port;
1004
1005 while (pmap) {
1006 if (pmap->pml_map.pm_prog != prog)
1007 goto next;
1008 if (!version && p.pm_vers > pmap->pml_map.pm_vers)
1009 goto next;
1010 if (version > 2 && pmap->pml_map.pm_vers != version)
1011 goto next;
1012 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
1013 goto next;
1014 if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
1015 (proto && p.pm_prot && pmap->pml_map.pm_prot != proto) ||
1016 (port && pmap->pml_map.pm_port != port))
1017 goto next;
1018 memcpy(&p, &pmap->pml_map, sizeof(p));
1019next:
1020 pmap = pmap->pml_next;
1021 }
1022 if (!p.pm_vers)
1023 p.pm_vers = MOUNTVERS;
1024 if (!p.pm_port)
1025 p.pm_port = MOUNTPORT;
1026 if (!p.pm_prot)
1027 p.pm_prot = IPPROTO_TCP;
1028 return &p;
1029}
1030
1031static int daemonize(void)
1032{
1033 int fd;
1034 int pid = fork();
1035 if (pid < 0) /* error */
1036 return -errno;
1037 if (pid > 0) /* parent */
1038 return 0;
1039 /* child */
1040 fd = xopen(bb_dev_null, O_RDWR);
1041 dup2(fd, 0);
1042 dup2(fd, 1);
1043 dup2(fd, 2);
1044 if (fd > 2) close(fd);
1045 setsid();
1046 openlog(bb_applet_name, LOG_PID, LOG_DAEMON);
1047 logmode = LOGMODE_SYSLOG;
1048 return 1;
1049}
1050
1051// TODO
1052static inline int we_saw_this_host_before(const char *hostname)
1053{
1054 return 0;
1055}
1056
1057/* RPC strerror analogs are terminally idiotic:
1058 * *mandatory* prefix and \n at end.
1059 * This hopefully helps. Usage:
1060 * error_msg_rpc(clnt_*error*(" ")) */
1061static void error_msg_rpc(const char *msg)
1062{
1063 size_t len;
1064 while (msg[0] == ' ' || msg[0] == ':') msg++;
1065 len = strlen(msg);
1066 while (len && msg[len-1] == '\n') len--;
1067 bb_error_msg("%.*s", len, msg);
1068}
1069
1070static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
1071{
1072 CLIENT *mclient;
1073 char *hostname;
1074 char *pathname;
1075 char *mounthost;
1076 struct nfs_mount_data data;
1077 char *opt;
1078 struct hostent *hp;
1079 struct sockaddr_in server_addr;
1080 struct sockaddr_in mount_server_addr;
1081 int msock, fsock;
1082 union {
1083 struct fhstatus nfsv2;
1084 struct mountres3 nfsv3;
1085 } status;
1086 int daemonized;
1087 char *s;
1088 int port;
1089 int mountport;
1090 int proto;
1091 int bg;
1092 int soft;
1093 int intr;
1094 int posix;
1095 int nocto;
1096 int noac;
1097 int nolock;
1098 int retry;
1099 int tcp;
1100 int mountprog;
1101 int mountvers;
1102 int nfsprog;
1103 int nfsvers;
1104 int retval;
1105
1106 find_kernel_nfs_mount_version();
1107
1108 daemonized = 0;
1109 mounthost = NULL;
1110 retval = ETIMEDOUT;
1111 msock = fsock = -1;
1112 mclient = NULL;
1113
1114 /* NB: hostname, mounthost, filteropts must be free()d prior to return */
1115
1116 filteropts = xstrdup(filteropts); /* going to trash it later... */
1117
1118 hostname = xstrdup(mp->mnt_fsname);
1119 /* mount_main() guarantees that ':' is there */
1120 s = strchr(hostname, ':');
1121 pathname = s + 1;
1122 *s = '\0';
1123 /* Ignore all but first hostname in replicated mounts
1124 until they can be fully supported. (mack@sgi.com) */
1125 s = strchr(hostname, ',');
1126 if (s) {
1127 *s = '\0';
1128 bb_error_msg("warning: multiple hostnames not supported");
1129 }
1130
1131 server_addr.sin_family = AF_INET;
1132 if (!inet_aton(hostname, &server_addr.sin_addr)) {
1133 hp = gethostbyname(hostname);
1134 if (hp == NULL) {
1135 bb_herror_msg("%s", hostname);
1136 goto fail;
1137 }
1138 if (hp->h_length > sizeof(struct in_addr)) {
1139 bb_error_msg("got bad hp->h_length");
1140 hp->h_length = sizeof(struct in_addr);
1141 }
1142 memcpy(&server_addr.sin_addr,
1143 hp->h_addr, hp->h_length);
1144 }
1145
1146 memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr));
1147
1148 /* add IP address to mtab options for use when unmounting */
1149
1150 if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */
1151 mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr));
1152 } else {
1153 char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts,
1154 mp->mnt_opts[0] ? "," : "",
1155 inet_ntoa(server_addr.sin_addr));
1156 free(mp->mnt_opts);
1157 mp->mnt_opts = tmp;
1158 }
1159
1160 /* Set default options.
1161 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
1162 * let the kernel decide.
1163 * timeo is filled in after we know whether it'll be TCP or UDP. */
1164 memset(&data, 0, sizeof(data));
1165 data.retrans = 3;
1166 data.acregmin = 3;
1167 data.acregmax = 60;
1168 data.acdirmin = 30;
1169 data.acdirmax = 60;
1170 data.namlen = NAME_MAX;
1171
1172 bg = 0;
1173 soft = 0;
1174 intr = 0;
1175 posix = 0;
1176 nocto = 0;
1177 nolock = 0;
1178 noac = 0;
1179 retry = 10000; /* 10000 minutes ~ 1 week */
1180 tcp = 0;
1181
1182 mountprog = MOUNTPROG;
1183 mountvers = 0;
1184 port = 0;
1185 mountport = 0;
1186 nfsprog = 100003;
1187 nfsvers = 0;
1188
1189 /* parse options */
1190
1191 for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
1192 char *opteq = strchr(opt, '=');
1193 if (opteq) {
1194 int val = atoi(opteq + 1);
1195 *opteq = '\0';
1196 if (!strcmp(opt, "rsize"))
1197 data.rsize = val;
1198 else if (!strcmp(opt, "wsize"))
1199 data.wsize = val;
1200 else if (!strcmp(opt, "timeo"))
1201 data.timeo = val;
1202 else if (!strcmp(opt, "retrans"))
1203 data.retrans = val;
1204 else if (!strcmp(opt, "acregmin"))
1205 data.acregmin = val;
1206 else if (!strcmp(opt, "acregmax"))
1207 data.acregmax = val;
1208 else if (!strcmp(opt, "acdirmin"))
1209 data.acdirmin = val;
1210 else if (!strcmp(opt, "acdirmax"))
1211 data.acdirmax = val;
1212 else if (!strcmp(opt, "actimeo")) {
1213 data.acregmin = val;
1214 data.acregmax = val;
1215 data.acdirmin = val;
1216 data.acdirmax = val;
1217 }
1218 else if (!strcmp(opt, "retry"))
1219 retry = val;
1220 else if (!strcmp(opt, "port"))
1221 port = val;
1222 else if (!strcmp(opt, "mountport"))
1223 mountport = val;
1224 else if (!strcmp(opt, "mounthost"))
1225 mounthost = xstrndup(opteq+1,
1226 strcspn(opteq+1," \t\n\r,"));
1227 else if (!strcmp(opt, "mountprog"))
1228 mountprog = val;
1229 else if (!strcmp(opt, "mountvers"))
1230 mountvers = val;
1231 else if (!strcmp(opt, "nfsprog"))
1232 nfsprog = val;
1233 else if (!strcmp(opt, "nfsvers") ||
1234 !strcmp(opt, "vers"))
1235 nfsvers = val;
1236 else if (!strcmp(opt, "proto")) {
1237 if (!strncmp(opteq+1, "tcp", 3))
1238 tcp = 1;
1239 else if (!strncmp(opteq+1, "udp", 3))
1240 tcp = 0;
1241 else
1242 bb_error_msg("warning: unrecognized proto= option");
1243 } else if (!strcmp(opt, "namlen")) {
1244 if (nfs_mount_version >= 2)
1245 data.namlen = val;
1246 else
1247 bb_error_msg("warning: option namlen is not supported\n");
1248 } else if (!strcmp(opt, "addr"))
1249 /* ignore */;
1250 else {
1251 bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
1252 goto fail;
1253 }
1254 }
1255 else {
1256 int val = 1;
1257 if (!strncmp(opt, "no", 2)) {
1258 val = 0;
1259 opt += 2;
1260 }
1261 if (!strcmp(opt, "bg"))
1262 bg = val;
1263 else if (!strcmp(opt, "fg"))
1264 bg = !val;
1265 else if (!strcmp(opt, "soft"))
1266 soft = val;
1267 else if (!strcmp(opt, "hard"))
1268 soft = !val;
1269 else if (!strcmp(opt, "intr"))
1270 intr = val;
1271 else if (!strcmp(opt, "posix"))
1272 posix = val;
1273 else if (!strcmp(opt, "cto"))
1274 nocto = !val;
1275 else if (!strcmp(opt, "ac"))
1276 noac = !val;
1277 else if (!strcmp(opt, "tcp"))
1278 tcp = val;
1279 else if (!strcmp(opt, "udp"))
1280 tcp = !val;
1281 else if (!strcmp(opt, "lock")) {
1282 if (nfs_mount_version >= 3)
1283 nolock = !val;
1284 else
1285 bb_error_msg("warning: option nolock is not supported");
1286 } else {
1287 bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
1288 goto fail;
1289 }
1290 }
1291 }
1292 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
1293
1294 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
1295 | (intr ? NFS_MOUNT_INTR : 0)
1296 | (posix ? NFS_MOUNT_POSIX : 0)
1297 | (nocto ? NFS_MOUNT_NOCTO : 0)
1298 | (noac ? NFS_MOUNT_NOAC : 0);
1299 if (nfs_mount_version >= 2)
1300 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
1301 if (nfs_mount_version >= 3)
1302 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
1303 if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
1304 bb_error_msg("NFSv%d not supported", nfsvers);
1305 goto fail;
1306 }
1307 if (nfsvers && !mountvers)
1308 mountvers = (nfsvers < 3) ? 1 : nfsvers;
1309 if (nfsvers && nfsvers < mountvers) {
1310 mountvers = nfsvers;
1311 }
1312
1313 /* Adjust options if none specified */
1314 if (!data.timeo)
1315 data.timeo = tcp ? 70 : 7;
1316
1317#ifdef NFS_MOUNT_DEBUG
1318 printf("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
1319 data.rsize, data.wsize, data.timeo, data.retrans);
1320 printf("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n",
1321 data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
1322 printf("port = %d, bg = %d, retry = %d, flags = %.8x\n",
1323 port, bg, retry, data.flags);
1324 printf("mountprog = %d, mountvers = %d, nfsprog = %d, nfsvers = %d\n",
1325 mountprog, mountvers, nfsprog, nfsvers);
1326 printf("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d\n",
1327 (data.flags & NFS_MOUNT_SOFT) != 0,
1328 (data.flags & NFS_MOUNT_INTR) != 0,
1329 (data.flags & NFS_MOUNT_POSIX) != 0,
1330 (data.flags & NFS_MOUNT_NOCTO) != 0,
1331 (data.flags & NFS_MOUNT_NOAC) != 0);
1332 printf("tcp = %d\n",
1333 (data.flags & NFS_MOUNT_TCP) != 0);
1334#endif
1335
1336 data.version = nfs_mount_version;
1337
1338 if (vfsflags & MS_REMOUNT)
1339 goto do_mount;
1340
1341 /*
1342 * If the previous mount operation on the same host was
1343 * backgrounded, and the "bg" for this mount is also set,
1344 * give up immediately, to avoid the initial timeout.
1345 */
1346 if (bg && we_saw_this_host_before(hostname)) {
1347 daemonized = daemonize(); /* parent or error */
1348 if (daemonized <= 0) { /* parent or error */
1349 retval = -daemonized;
1350 goto ret;
1351 }
1352 }
1353
1354 /* create mount daemon client */
1355 /* See if the nfs host = mount host. */
1356 if (mounthost) {
1357 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
1358 mount_server_addr.sin_family = AF_INET;
1359 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
1360 } else {
1361 hp = gethostbyname(mounthost);
1362 if (hp == NULL) {
1363 bb_herror_msg("%s", mounthost);
1364 goto fail;
1365 } else {
1366 if (hp->h_length > sizeof(struct in_addr)) {
1367 bb_error_msg("got bad hp->h_length?");
1368 hp->h_length = sizeof(struct in_addr);
1369 }
1370 mount_server_addr.sin_family = AF_INET;
1371 memcpy(&mount_server_addr.sin_addr,
1372 hp->h_addr, hp->h_length);
1373 }
1374 }
1375 }
1376
1377 /*
1378 * The following loop implements the mount retries. When the mount
1379 * times out, and the "bg" option is set, we background ourself
1380 * and continue trying.
1381 *
1382 * The case where the mount point is not present and the "bg"
1383 * option is set, is treated as a timeout. This is done to
1384 * support nested mounts.
1385 *
1386 * The "retry" count specified by the user is the number of
1387 * minutes to retry before giving up.
1388 */
1389 {
1390 struct timeval total_timeout;
1391 struct timeval retry_timeout;
1392 struct pmap* pm_mnt;
1393 time_t t;
1394 time_t prevt;
1395 time_t timeout;
1396
1397 retry_timeout.tv_sec = 3;
1398 retry_timeout.tv_usec = 0;
1399 total_timeout.tv_sec = 20;
1400 total_timeout.tv_usec = 0;
1401 timeout = time(NULL) + 60 * retry;
1402 prevt = 0;
1403 t = 30;
1404retry:
1405 /* be careful not to use too many CPU cycles */
1406 if (t - prevt < 30)
1407 sleep(30);
1408
1409 pm_mnt = get_mountport(&mount_server_addr,
1410 mountprog,
1411 mountvers,
1412 proto,
1413 mountport);
1414 nfsvers = (pm_mnt->pm_vers < 2) ? 2 : pm_mnt->pm_vers;
1415
1416 /* contact the mount daemon via TCP */
1417 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
1418 msock = RPC_ANYSOCK;
1419
1420 switch (pm_mnt->pm_prot) {
1421 case IPPROTO_UDP:
1422 mclient = clntudp_create(&mount_server_addr,
1423 pm_mnt->pm_prog,
1424 pm_mnt->pm_vers,
1425 retry_timeout,
1426 &msock);
1427 if (mclient)
1428 break;
1429 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
1430 msock = RPC_ANYSOCK;
1431 case IPPROTO_TCP:
1432 mclient = clnttcp_create(&mount_server_addr,
1433 pm_mnt->pm_prog,
1434 pm_mnt->pm_vers,
1435 &msock, 0, 0);
1436 break;
1437 default:
1438 mclient = 0;
1439 }
1440 if (!mclient) {
1441 if (!daemonized && prevt == 0)
1442 error_msg_rpc(clnt_spcreateerror(" "));
1443 } else {
1444 enum clnt_stat clnt_stat;
1445 /* try to mount hostname:pathname */
1446 mclient->cl_auth = authunix_create_default();
1447
1448 /* make pointers in xdr_mountres3 NULL so
1449 * that xdr_array allocates memory for us
1450 */
1451 memset(&status, 0, sizeof(status));
1452
1453 if (pm_mnt->pm_vers == 3)
1454 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
1455 (xdrproc_t) xdr_dirpath,
1456 (caddr_t) &pathname,
1457 (xdrproc_t) xdr_mountres3,
1458 (caddr_t) &status,
1459 total_timeout);
1460 else
1461 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
1462 (xdrproc_t) xdr_dirpath,
1463 (caddr_t) &pathname,
1464 (xdrproc_t) xdr_fhstatus,
1465 (caddr_t) &status,
1466 total_timeout);
1467
1468 if (clnt_stat == RPC_SUCCESS)
1469 goto prepare_kernel_data; /* we're done */
1470 if (errno != ECONNREFUSED) {
1471 error_msg_rpc(clnt_sperror(mclient, " "));
1472 goto fail; /* don't retry */
1473 }
1474 /* Connection refused */
1475 if (!daemonized && prevt == 0) /* print just once */
1476 error_msg_rpc(clnt_sperror(mclient, " "));
1477 auth_destroy(mclient->cl_auth);
1478 clnt_destroy(mclient);
1479 mclient = 0;
1480 close(msock);
1481 }
1482
1483 /* Timeout. We are going to retry... maybe */
1484
1485 if (!bg)
1486 goto fail;
1487 if (!daemonized) {
1488 daemonized = daemonize();
1489 if (daemonized <= 0) { /* parent or error */
1490 retval = -daemonized;
1491 goto ret;
1492 }
1493 }
1494 prevt = t;
1495 t = time(NULL);
1496 if (t >= timeout)
1497 /* TODO error message */
1498 goto fail;
1499
1500 goto retry;
1501 }
1502
1503prepare_kernel_data:
1504
1505 if (nfsvers == 2) {
1506 if (status.nfsv2.fhs_status != 0) {
1507 bb_error_msg("%s:%s failed, reason given by server: %s",
1508 hostname, pathname,
1509 nfs_strerror(status.nfsv2.fhs_status));
1510 goto fail;
1511 }
1512 memcpy(data.root.data,
1513 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1514 NFS_FHSIZE);
1515 data.root.size = NFS_FHSIZE;
1516 memcpy(data.old_root.data,
1517 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1518 NFS_FHSIZE);
1519 } else {
1520 fhandle3 *my_fhandle;
1521 if (status.nfsv3.fhs_status != 0) {
1522 bb_error_msg("%s:%s failed, reason given by server: %s",
1523 hostname, pathname,
1524 nfs_strerror(status.nfsv3.fhs_status));
1525 goto fail;
1526 }
1527 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
1528 memset(data.old_root.data, 0, NFS_FHSIZE);
1529 memset(&data.root, 0, sizeof(data.root));
1530 data.root.size = my_fhandle->fhandle3_len;
1531 memcpy(data.root.data,
1532 (char *) my_fhandle->fhandle3_val,
1533 my_fhandle->fhandle3_len);
1534
1535 data.flags |= NFS_MOUNT_VER3;
1536 }
1537
1538 /* create nfs socket for kernel */
1539
1540 if (tcp) {
1541 if (nfs_mount_version < 3) {
1542 bb_error_msg("NFS over TCP is not supported");
1543 goto fail;
1544 }
1545 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1546 } else
1547 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1548 if (fsock < 0) {
1549 bb_perror_msg("nfs socket");
1550 goto fail;
1551 }
1552 if (bindresvport(fsock, 0) < 0) {
1553 bb_perror_msg("nfs bindresvport");
1554 goto fail;
1555 }
1556 if (port == 0) {
1557 server_addr.sin_port = PMAPPORT;
1558 port = pmap_getport(&server_addr, nfsprog, nfsvers,
1559 tcp ? IPPROTO_TCP : IPPROTO_UDP);
1560 if (port == 0)
1561 port = NFS_PORT;
1562#ifdef NFS_MOUNT_DEBUG
1563 else
1564 printf("used portmapper to find NFS port\n");
1565#endif
1566 }
1567#ifdef NFS_MOUNT_DEBUG
1568 printf("using port %d for nfs daemon\n", port);
1569#endif
1570 server_addr.sin_port = htons(port);
1571
1572 /* prepare data structure for kernel */
1573
1574 data.fd = fsock;
1575 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
1576 strncpy(data.hostname, hostname, sizeof(data.hostname));
1577
1578 /* clean up */
1579
1580 auth_destroy(mclient->cl_auth);
1581 clnt_destroy(mclient);
1582 close(msock);
1583
1584 if (bg) {
1585 /* We must wait until mount directory is available */
1586 struct stat statbuf;
1587 int delay = 1;
1588 while (stat(mp->mnt_dir, &statbuf) == -1) {
1589 if (!daemonized) {
1590 daemonized = daemonize();
1591 if (daemonized <= 0) { /* parent or error */
1592 retval = -daemonized;
1593 goto ret;
1594 }
1595 }
1596 sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */
1597 delay *= 2;
1598 if (delay > 30)
1599 delay = 30;
1600 }
1601 }
1602
1603do_mount: /* perform actual mount */
1604
1605 mp->mnt_type = "nfs";
1606 retval = mount_it_now(mp, vfsflags, (char*)&data);
1607 goto ret;
1608
1609fail: /* abort */
1610
1611 if (msock != -1) {
1612 if (mclient) {
1613 auth_destroy(mclient->cl_auth);
1614 clnt_destroy(mclient);
1615 }
1616 close(msock);
1617 }
1618 if (fsock != -1)
1619 close(fsock);
1620
1621ret:
1622 free(hostname);
1623 free(mounthost);
1624 free(filteropts);
1625 return retval;
1626}
1627
1628#endif /* ENABLE_FEATURE_MOUNT_NFS */
diff --git a/util-linux/nfsmount.c b/util-linux/nfsmount.c
deleted file mode 100644
index 4e523b5f3..000000000
--- a/util-linux/nfsmount.c
+++ /dev/null
@@ -1,1022 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * nfsmount.c -- Linux NFS mount
4 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
5 *
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7 *
8 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
9 * numbers to be specified on the command line.
10 *
11 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
12 * Omit the call to connect() for Linux version 1.3.11 or later.
13 *
14 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
15 * Implemented the "bg", "fg" and "retry" mount options for NFS.
16 *
17 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
18 * - added Native Language Support
19 *
20 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
21 * plus NFSv3 stuff.
22 */
23
24#include "busybox.h"
25#include <syslog.h>
26#include <mntent.h>
27#include <sys/utsname.h>
28#undef TRUE
29#undef FALSE
30#include <rpc/rpc.h>
31#include <rpc/pmap_prot.h>
32#include <rpc/pmap_clnt.h>
33
34/* This is just a warning of a common mistake. Possibly this should be a
35 * uclibc faq entry rather than in busybox... */
36#if ENABLE_FEATURE_MOUNT_NFS && defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
37#error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support."
38#endif
39
40/* former nfsmount.h */
41
42#define MOUNTPORT 635
43#define MNTPATHLEN 1024
44#define MNTNAMLEN 255
45#define FHSIZE 32
46#define FHSIZE3 64
47
48typedef char fhandle[FHSIZE];
49
50typedef struct {
51 unsigned int fhandle3_len;
52 char *fhandle3_val;
53} fhandle3;
54
55enum mountstat3 {
56 MNT_OK = 0,
57 MNT3ERR_PERM = 1,
58 MNT3ERR_NOENT = 2,
59 MNT3ERR_IO = 5,
60 MNT3ERR_ACCES = 13,
61 MNT3ERR_NOTDIR = 20,
62 MNT3ERR_INVAL = 22,
63 MNT3ERR_NAMETOOLONG = 63,
64 MNT3ERR_NOTSUPP = 10004,
65 MNT3ERR_SERVERFAULT = 10006,
66};
67typedef enum mountstat3 mountstat3;
68
69struct fhstatus {
70 unsigned int fhs_status;
71 union {
72 fhandle fhs_fhandle;
73 } fhstatus_u;
74};
75typedef struct fhstatus fhstatus;
76
77struct mountres3_ok {
78 fhandle3 fhandle;
79 struct {
80 unsigned int auth_flavours_len;
81 char *auth_flavours_val;
82 } auth_flavours;
83};
84typedef struct mountres3_ok mountres3_ok;
85
86struct mountres3 {
87 mountstat3 fhs_status;
88 union {
89 mountres3_ok mountinfo;
90 } mountres3_u;
91};
92typedef struct mountres3 mountres3;
93
94typedef char *dirpath;
95
96typedef char *name;
97
98typedef struct mountbody *mountlist;
99
100struct mountbody {
101 name ml_hostname;
102 dirpath ml_directory;
103 mountlist ml_next;
104};
105typedef struct mountbody mountbody;
106
107typedef struct groupnode *groups;
108
109struct groupnode {
110 name gr_name;
111 groups gr_next;
112};
113typedef struct groupnode groupnode;
114
115typedef struct exportnode *exports;
116
117struct exportnode {
118 dirpath ex_dir;
119 groups ex_groups;
120 exports ex_next;
121};
122typedef struct exportnode exportnode;
123
124struct ppathcnf {
125 int pc_link_max;
126 short pc_max_canon;
127 short pc_max_input;
128 short pc_name_max;
129 short pc_path_max;
130 short pc_pipe_buf;
131 u_char pc_vdisable;
132 char pc_xxx;
133 short pc_mask[2];
134};
135typedef struct ppathcnf ppathcnf;
136
137#define MOUNTPROG 100005
138#define MOUNTVERS 1
139
140#define MOUNTPROC_NULL 0
141#define MOUNTPROC_MNT 1
142#define MOUNTPROC_DUMP 2
143#define MOUNTPROC_UMNT 3
144#define MOUNTPROC_UMNTALL 4
145#define MOUNTPROC_EXPORT 5
146#define MOUNTPROC_EXPORTALL 6
147
148#define MOUNTVERS_POSIX 2
149
150#define MOUNTPROC_PATHCONF 7
151
152#define MOUNT_V3 3
153
154#define MOUNTPROC3_NULL 0
155#define MOUNTPROC3_MNT 1
156#define MOUNTPROC3_DUMP 2
157#define MOUNTPROC3_UMNT 3
158#define MOUNTPROC3_UMNTALL 4
159#define MOUNTPROC3_EXPORT 5
160
161/* former nfsmount.h ends */
162
163enum {
164#ifndef NFS_FHSIZE
165 NFS_FHSIZE = 32,
166#endif
167#ifndef NFS_PORT
168 NFS_PORT = 2049
169#endif
170};
171
172//enum {
173// S_QUOTA = 128, /* Quota initialized for file/directory/symlink */
174//};
175
176
177/*
178 * We want to be able to compile mount on old kernels in such a way
179 * that the binary will work well on more recent kernels.
180 * Thus, if necessary we teach nfsmount.c the structure of new fields
181 * that will come later.
182 *
183 * Moreover, the new kernel includes conflict with glibc includes
184 * so it is easiest to ignore the kernel altogether (at compile time).
185 */
186
187struct nfs2_fh {
188 char data[32];
189};
190struct nfs3_fh {
191 unsigned short size;
192 unsigned char data[64];
193};
194
195struct nfs_mount_data {
196 int version; /* 1 */
197 int fd; /* 1 */
198 struct nfs2_fh old_root; /* 1 */
199 int flags; /* 1 */
200 int rsize; /* 1 */
201 int wsize; /* 1 */
202 int timeo; /* 1 */
203 int retrans; /* 1 */
204 int acregmin; /* 1 */
205 int acregmax; /* 1 */
206 int acdirmin; /* 1 */
207 int acdirmax; /* 1 */
208 struct sockaddr_in addr; /* 1 */
209 char hostname[256]; /* 1 */
210 int namlen; /* 2 */
211 unsigned int bsize; /* 3 */
212 struct nfs3_fh root; /* 4 */
213};
214
215/* bits in the flags field */
216enum {
217 NFS_MOUNT_SOFT = 0x0001, /* 1 */
218 NFS_MOUNT_INTR = 0x0002, /* 1 */
219 NFS_MOUNT_SECURE = 0x0004, /* 1 */
220 NFS_MOUNT_POSIX = 0x0008, /* 1 */
221 NFS_MOUNT_NOCTO = 0x0010, /* 1 */
222 NFS_MOUNT_NOAC = 0x0020, /* 1 */
223 NFS_MOUNT_TCP = 0x0040, /* 2 */
224 NFS_MOUNT_VER3 = 0x0080, /* 3 */
225 NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
226 NFS_MOUNT_NONLM = 0x0200 /* 3 */
227};
228
229#define HAVE_inet_aton
230
231/*
232 * We need to translate between nfs status return values and
233 * the local errno values which may not be the same.
234 *
235 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
236 * "after #include <errno.h> the symbol errno is reserved for any use,
237 * it cannot even be used as a struct tag or field name".
238 */
239
240#ifndef EDQUOT
241#define EDQUOT ENOSPC
242#endif
243
244// Convert each NFSERR_BLAH into EBLAH
245
246static const struct {
247 int stat;
248 int errnum;
249} nfs_errtbl[] = {
250 {0,0}, {1,EPERM}, {2,ENOENT}, {5,EIO}, {6,ENXIO}, {13,EACCES}, {17,EEXIST},
251 {19,ENODEV}, {20,ENOTDIR}, {21,EISDIR}, {22,EINVAL}, {27,EFBIG},
252 {28,ENOSPC}, {30,EROFS}, {63,ENAMETOOLONG}, {66,ENOTEMPTY}, {69,EDQUOT},
253 {70,ESTALE}, {71,EREMOTE}, {-1,EIO}
254};
255
256static char *nfs_strerror(int status)
257{
258 int i;
259 static char buf[256];
260
261 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
262 if (nfs_errtbl[i].stat == status)
263 return strerror(nfs_errtbl[i].errnum);
264 }
265 sprintf(buf, "unknown nfs status return value: %d", status);
266 return buf;
267}
268
269static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
270{
271 if (!xdr_opaque(xdrs, objp, FHSIZE))
272 return FALSE;
273 return TRUE;
274}
275
276static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
277{
278 if (!xdr_u_int(xdrs, &objp->fhs_status))
279 return FALSE;
280 switch (objp->fhs_status) {
281 case 0:
282 if (!xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle))
283 return FALSE;
284 break;
285 default:
286 break;
287 }
288 return TRUE;
289}
290
291static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp)
292{
293 if (!xdr_string(xdrs, objp, MNTPATHLEN))
294 return FALSE;
295 return TRUE;
296}
297
298static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
299{
300 if (!xdr_bytes(xdrs, (char **)&objp->fhandle3_val, (unsigned int *) &objp->fhandle3_len, FHSIZE3))
301 return FALSE;
302 return TRUE;
303}
304
305static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
306{
307 if (!xdr_fhandle3(xdrs, &objp->fhandle))
308 return FALSE;
309 if (!xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val), &(objp->auth_flavours.auth_flavours_len), ~0,
310 sizeof (int), (xdrproc_t) xdr_int))
311 return FALSE;
312 return TRUE;
313}
314
315static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
316{
317 if (!xdr_enum(xdrs, (enum_t *) objp))
318 return FALSE;
319 return TRUE;
320}
321
322static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
323{
324 if (!xdr_mountstat3(xdrs, &objp->fhs_status))
325 return FALSE;
326 switch (objp->fhs_status) {
327 case MNT_OK:
328 if (!xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo))
329 return FALSE;
330 break;
331 default:
332 break;
333 }
334 return TRUE;
335}
336
337#define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
338
339/*
340 * nfs_mount_version according to the sources seen at compile time.
341 */
342static int nfs_mount_version;
343static int kernel_version;
344
345/*
346 * Unfortunately, the kernel prints annoying console messages
347 * in case of an unexpected nfs mount version (instead of
348 * just returning some error). Therefore we'll have to try
349 * and figure out what version the kernel expects.
350 *
351 * Variables:
352 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
353 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
354 * nfs_mount_version: version this source and running kernel can handle
355 */
356static void
357find_kernel_nfs_mount_version(void)
358{
359 if (kernel_version)
360 return;
361
362 nfs_mount_version = 4; /* default */
363
364 kernel_version = get_linux_version_code();
365 if (kernel_version) {
366 if (kernel_version < KERNEL_VERSION(2,1,32))
367 nfs_mount_version = 1;
368 else if (kernel_version < KERNEL_VERSION(2,2,18) ||
369 (kernel_version >= KERNEL_VERSION(2,3,0) &&
370 kernel_version < KERNEL_VERSION(2,3,99)))
371 nfs_mount_version = 3;
372 /* else v4 since 2.3.99pre4 */
373 }
374}
375
376static struct pmap *
377get_mountport(struct sockaddr_in *server_addr,
378 long unsigned prog,
379 long unsigned version,
380 long unsigned proto,
381 long unsigned port)
382{
383 struct pmaplist *pmap;
384 static struct pmap p = {0, 0, 0, 0};
385
386 server_addr->sin_port = PMAPPORT;
387 pmap = pmap_getmaps(server_addr);
388
389 if (version > MAX_NFSPROT)
390 version = MAX_NFSPROT;
391 if (!prog)
392 prog = MOUNTPROG;
393 p.pm_prog = prog;
394 p.pm_vers = version;
395 p.pm_prot = proto;
396 p.pm_port = port;
397
398 while (pmap) {
399 if (pmap->pml_map.pm_prog != prog)
400 goto next;
401 if (!version && p.pm_vers > pmap->pml_map.pm_vers)
402 goto next;
403 if (version > 2 && pmap->pml_map.pm_vers != version)
404 goto next;
405 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
406 goto next;
407 if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
408 (proto && p.pm_prot && pmap->pml_map.pm_prot != proto) ||
409 (port && pmap->pml_map.pm_port != port))
410 goto next;
411 memcpy(&p, &pmap->pml_map, sizeof(p));
412next:
413 pmap = pmap->pml_next;
414 }
415 if (!p.pm_vers)
416 p.pm_vers = MOUNTVERS;
417 if (!p.pm_port)
418 p.pm_port = MOUNTPORT;
419 if (!p.pm_prot)
420 p.pm_prot = IPPROTO_TCP;
421 return &p;
422}
423
424static int daemonize(void)
425{
426 int fd;
427 int pid = fork();
428 if (pid < 0) /* error */
429 return -errno;
430 if (pid > 0) /* parent */
431 return 0;
432 /* child */
433 fd = xopen(bb_dev_null, O_RDWR);
434 dup2(fd, 0);
435 dup2(fd, 1);
436 dup2(fd, 2);
437 if (fd > 2) close(fd);
438 setsid();
439 openlog(bb_applet_name, LOG_PID, LOG_DAEMON);
440 logmode = LOGMODE_SYSLOG;
441 return 1;
442}
443
444// TODO
445static inline int we_saw_this_host_before(const char *hostname)
446{
447 return 0;
448}
449
450/* RPC strerror analogs are terminally idiotic:
451 * *mandatory* prefix and \n at end.
452 * This hopefully helps. Usage:
453 * error_msg_rpc(clnt_*error*(" ")) */
454static void error_msg_rpc(const char *msg)
455{
456 size_t len;
457 while (msg[0] == ' ' || msg[0] == ':') msg++;
458 len = strlen(msg);
459 while (len && msg[len-1] == '\n') len--;
460 bb_error_msg("%.*s", len, msg);
461}
462
463int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
464{
465 CLIENT *mclient;
466 char *hostname;
467 char *pathname;
468 char *mounthost;
469 struct nfs_mount_data data;
470 char *opt;
471 struct hostent *hp;
472 struct sockaddr_in server_addr;
473 struct sockaddr_in mount_server_addr;
474 int msock, fsock;
475 union {
476 struct fhstatus nfsv2;
477 struct mountres3 nfsv3;
478 } status;
479 int daemonized;
480 char *s;
481 int port;
482 int mountport;
483 int proto;
484 int bg;
485 int soft;
486 int intr;
487 int posix;
488 int nocto;
489 int noac;
490 int nolock;
491 int retry;
492 int tcp;
493 int mountprog;
494 int mountvers;
495 int nfsprog;
496 int nfsvers;
497 int retval;
498
499 find_kernel_nfs_mount_version();
500
501 daemonized = 0;
502 mounthost = NULL;
503 retval = ETIMEDOUT;
504 msock = fsock = -1;
505 mclient = NULL;
506
507 /* NB: hostname, mounthost, filteropts must be free()d prior to return */
508
509 filteropts = xstrdup(filteropts); /* going to trash it later... */
510
511 hostname = xstrdup(mp->mnt_fsname);
512 /* mount_main() guarantees that ':' is there */
513 s = strchr(hostname, ':');
514 pathname = s + 1;
515 *s = '\0';
516 /* Ignore all but first hostname in replicated mounts
517 until they can be fully supported. (mack@sgi.com) */
518 s = strchr(hostname, ',');
519 if (s) {
520 *s = '\0';
521 bb_error_msg("warning: multiple hostnames not supported");
522 }
523
524 server_addr.sin_family = AF_INET;
525#ifdef HAVE_inet_aton
526 if (!inet_aton(hostname, &server_addr.sin_addr))
527#endif
528 {
529 hp = gethostbyname(hostname);
530 if (hp == NULL) {
531 bb_herror_msg("%s", hostname);
532 goto fail;
533 }
534 if (hp->h_length > sizeof(struct in_addr)) {
535 bb_error_msg("got bad hp->h_length");
536 hp->h_length = sizeof(struct in_addr);
537 }
538 memcpy(&server_addr.sin_addr,
539 hp->h_addr, hp->h_length);
540 }
541
542 memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr));
543
544 /* add IP address to mtab options for use when unmounting */
545
546 if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */
547 mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr));
548 } else {
549 char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts,
550 mp->mnt_opts[0] ? "," : "",
551 inet_ntoa(server_addr.sin_addr));
552 free(mp->mnt_opts);
553 mp->mnt_opts = tmp;
554 }
555
556 /* Set default options.
557 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
558 * let the kernel decide.
559 * timeo is filled in after we know whether it'll be TCP or UDP. */
560 memset(&data, 0, sizeof(data));
561 data.retrans = 3;
562 data.acregmin = 3;
563 data.acregmax = 60;
564 data.acdirmin = 30;
565 data.acdirmax = 60;
566 data.namlen = NAME_MAX;
567
568 bg = 0;
569 soft = 0;
570 intr = 0;
571 posix = 0;
572 nocto = 0;
573 nolock = 0;
574 noac = 0;
575 retry = 10000; /* 10000 minutes ~ 1 week */
576 tcp = 0;
577
578 mountprog = MOUNTPROG;
579 mountvers = 0;
580 port = 0;
581 mountport = 0;
582 nfsprog = 100003;
583 nfsvers = 0;
584
585 /* parse options */
586
587 for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
588 char *opteq = strchr(opt, '=');
589 if (opteq) {
590 int val = atoi(opteq + 1);
591 *opteq = '\0';
592 if (!strcmp(opt, "rsize"))
593 data.rsize = val;
594 else if (!strcmp(opt, "wsize"))
595 data.wsize = val;
596 else if (!strcmp(opt, "timeo"))
597 data.timeo = val;
598 else if (!strcmp(opt, "retrans"))
599 data.retrans = val;
600 else if (!strcmp(opt, "acregmin"))
601 data.acregmin = val;
602 else if (!strcmp(opt, "acregmax"))
603 data.acregmax = val;
604 else if (!strcmp(opt, "acdirmin"))
605 data.acdirmin = val;
606 else if (!strcmp(opt, "acdirmax"))
607 data.acdirmax = val;
608 else if (!strcmp(opt, "actimeo")) {
609 data.acregmin = val;
610 data.acregmax = val;
611 data.acdirmin = val;
612 data.acdirmax = val;
613 }
614 else if (!strcmp(opt, "retry"))
615 retry = val;
616 else if (!strcmp(opt, "port"))
617 port = val;
618 else if (!strcmp(opt, "mountport"))
619 mountport = val;
620 else if (!strcmp(opt, "mounthost"))
621 mounthost = xstrndup(opteq+1,
622 strcspn(opteq+1," \t\n\r,"));
623 else if (!strcmp(opt, "mountprog"))
624 mountprog = val;
625 else if (!strcmp(opt, "mountvers"))
626 mountvers = val;
627 else if (!strcmp(opt, "nfsprog"))
628 nfsprog = val;
629 else if (!strcmp(opt, "nfsvers") ||
630 !strcmp(opt, "vers"))
631 nfsvers = val;
632 else if (!strcmp(opt, "proto")) {
633 if (!strncmp(opteq+1, "tcp", 3))
634 tcp = 1;
635 else if (!strncmp(opteq+1, "udp", 3))
636 tcp = 0;
637 else
638 bb_error_msg("warning: unrecognized proto= option");
639 } else if (!strcmp(opt, "namlen")) {
640 if (nfs_mount_version >= 2)
641 data.namlen = val;
642 else
643 bb_error_msg("warning: option namlen is not supported\n");
644 } else if (!strcmp(opt, "addr"))
645 /* ignore */;
646 else {
647 bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
648 goto fail;
649 }
650 }
651 else {
652 int val = 1;
653 if (!strncmp(opt, "no", 2)) {
654 val = 0;
655 opt += 2;
656 }
657 if (!strcmp(opt, "bg"))
658 bg = val;
659 else if (!strcmp(opt, "fg"))
660 bg = !val;
661 else if (!strcmp(opt, "soft"))
662 soft = val;
663 else if (!strcmp(opt, "hard"))
664 soft = !val;
665 else if (!strcmp(opt, "intr"))
666 intr = val;
667 else if (!strcmp(opt, "posix"))
668 posix = val;
669 else if (!strcmp(opt, "cto"))
670 nocto = !val;
671 else if (!strcmp(opt, "ac"))
672 noac = !val;
673 else if (!strcmp(opt, "tcp"))
674 tcp = val;
675 else if (!strcmp(opt, "udp"))
676 tcp = !val;
677 else if (!strcmp(opt, "lock")) {
678 if (nfs_mount_version >= 3)
679 nolock = !val;
680 else
681 bb_error_msg("warning: option nolock is not supported");
682 } else {
683 bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
684 goto fail;
685 }
686 }
687 }
688 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
689
690 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
691 | (intr ? NFS_MOUNT_INTR : 0)
692 | (posix ? NFS_MOUNT_POSIX : 0)
693 | (nocto ? NFS_MOUNT_NOCTO : 0)
694 | (noac ? NFS_MOUNT_NOAC : 0);
695 if (nfs_mount_version >= 2)
696 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
697 if (nfs_mount_version >= 3)
698 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
699 if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
700 bb_error_msg("NFSv%d not supported", nfsvers);
701 goto fail;
702 }
703 if (nfsvers && !mountvers)
704 mountvers = (nfsvers < 3) ? 1 : nfsvers;
705 if (nfsvers && nfsvers < mountvers) {
706 mountvers = nfsvers;
707 }
708
709 /* Adjust options if none specified */
710 if (!data.timeo)
711 data.timeo = tcp ? 70 : 7;
712
713#ifdef NFS_MOUNT_DEBUG
714 printf("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
715 data.rsize, data.wsize, data.timeo, data.retrans);
716 printf("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n",
717 data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
718 printf("port = %d, bg = %d, retry = %d, flags = %.8x\n",
719 port, bg, retry, data.flags);
720 printf("mountprog = %d, mountvers = %d, nfsprog = %d, nfsvers = %d\n",
721 mountprog, mountvers, nfsprog, nfsvers);
722 printf("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d\n",
723 (data.flags & NFS_MOUNT_SOFT) != 0,
724 (data.flags & NFS_MOUNT_INTR) != 0,
725 (data.flags & NFS_MOUNT_POSIX) != 0,
726 (data.flags & NFS_MOUNT_NOCTO) != 0,
727 (data.flags & NFS_MOUNT_NOAC) != 0);
728 printf("tcp = %d\n",
729 (data.flags & NFS_MOUNT_TCP) != 0);
730#endif
731
732 data.version = nfs_mount_version;
733
734 if (vfsflags & MS_REMOUNT)
735 goto do_mount;
736
737 /*
738 * If the previous mount operation on the same host was
739 * backgrounded, and the "bg" for this mount is also set,
740 * give up immediately, to avoid the initial timeout.
741 */
742 if (bg && we_saw_this_host_before(hostname)) {
743 daemonized = daemonize(); /* parent or error */
744 if (daemonized <= 0) { /* parent or error */
745 retval = -daemonized;
746 goto ret;
747 }
748 }
749
750 /* create mount daemon client */
751 /* See if the nfs host = mount host. */
752 if (mounthost) {
753 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
754 mount_server_addr.sin_family = AF_INET;
755 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
756 } else {
757 hp = gethostbyname(mounthost);
758 if (hp == NULL) {
759 bb_herror_msg("%s", mounthost);
760 goto fail;
761 } else {
762 if (hp->h_length > sizeof(struct in_addr)) {
763 bb_error_msg("got bad hp->h_length?");
764 hp->h_length = sizeof(struct in_addr);
765 }
766 mount_server_addr.sin_family = AF_INET;
767 memcpy(&mount_server_addr.sin_addr,
768 hp->h_addr, hp->h_length);
769 }
770 }
771 }
772
773 /*
774 * The following loop implements the mount retries. When the mount
775 * times out, and the "bg" option is set, we background ourself
776 * and continue trying.
777 *
778 * The case where the mount point is not present and the "bg"
779 * option is set, is treated as a timeout. This is done to
780 * support nested mounts.
781 *
782 * The "retry" count specified by the user is the number of
783 * minutes to retry before giving up.
784 */
785 {
786 struct timeval total_timeout;
787 struct timeval retry_timeout;
788 struct pmap* pm_mnt;
789 time_t t;
790 time_t prevt;
791 time_t timeout;
792
793 retry_timeout.tv_sec = 3;
794 retry_timeout.tv_usec = 0;
795 total_timeout.tv_sec = 20;
796 total_timeout.tv_usec = 0;
797 timeout = time(NULL) + 60 * retry;
798 prevt = 0;
799 t = 30;
800retry:
801 /* be careful not to use too many CPU cycles */
802 if (t - prevt < 30)
803 sleep(30);
804
805 pm_mnt = get_mountport(&mount_server_addr,
806 mountprog,
807 mountvers,
808 proto,
809 mountport);
810 nfsvers = (pm_mnt->pm_vers < 2) ? 2 : pm_mnt->pm_vers;
811
812 /* contact the mount daemon via TCP */
813 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
814 msock = RPC_ANYSOCK;
815
816 switch (pm_mnt->pm_prot) {
817 case IPPROTO_UDP:
818 mclient = clntudp_create(&mount_server_addr,
819 pm_mnt->pm_prog,
820 pm_mnt->pm_vers,
821 retry_timeout,
822 &msock);
823 if (mclient)
824 break;
825 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
826 msock = RPC_ANYSOCK;
827 case IPPROTO_TCP:
828 mclient = clnttcp_create(&mount_server_addr,
829 pm_mnt->pm_prog,
830 pm_mnt->pm_vers,
831 &msock, 0, 0);
832 break;
833 default:
834 mclient = 0;
835 }
836 if (!mclient) {
837 if (!daemonized && prevt == 0)
838 error_msg_rpc(clnt_spcreateerror(" "));
839 } else {
840 enum clnt_stat clnt_stat;
841 /* try to mount hostname:pathname */
842 mclient->cl_auth = authunix_create_default();
843
844 /* make pointers in xdr_mountres3 NULL so
845 * that xdr_array allocates memory for us
846 */
847 memset(&status, 0, sizeof(status));
848
849 if (pm_mnt->pm_vers == 3)
850 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
851 (xdrproc_t) xdr_dirpath,
852 (caddr_t) &pathname,
853 (xdrproc_t) xdr_mountres3,
854 (caddr_t) &status,
855 total_timeout);
856 else
857 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
858 (xdrproc_t) xdr_dirpath,
859 (caddr_t) &pathname,
860 (xdrproc_t) xdr_fhstatus,
861 (caddr_t) &status,
862 total_timeout);
863
864 if (clnt_stat == RPC_SUCCESS)
865 goto prepare_kernel_data; /* we're done */
866 if (errno != ECONNREFUSED) {
867 error_msg_rpc(clnt_sperror(mclient, " "));
868 goto fail; /* don't retry */
869 }
870 /* Connection refused */
871 if (!daemonized && prevt == 0) /* print just once */
872 error_msg_rpc(clnt_sperror(mclient, " "));
873 auth_destroy(mclient->cl_auth);
874 clnt_destroy(mclient);
875 mclient = 0;
876 close(msock);
877 }
878
879 /* Timeout. We are going to retry... maybe */
880
881 if (!bg)
882 goto fail;
883 if (!daemonized) {
884 daemonized = daemonize();
885 if (daemonized <= 0) { /* parent or error */
886 retval = -daemonized;
887 goto ret;
888 }
889 }
890 prevt = t;
891 t = time(NULL);
892 if (t >= timeout)
893 /* TODO error message */
894 goto fail;
895
896 goto retry;
897 }
898
899prepare_kernel_data:
900
901 if (nfsvers == 2) {
902 if (status.nfsv2.fhs_status != 0) {
903 bb_error_msg("%s:%s failed, reason given by server: %s",
904 hostname, pathname,
905 nfs_strerror(status.nfsv2.fhs_status));
906 goto fail;
907 }
908 memcpy(data.root.data,
909 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
910 NFS_FHSIZE);
911 data.root.size = NFS_FHSIZE;
912 memcpy(data.old_root.data,
913 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
914 NFS_FHSIZE);
915 } else {
916 fhandle3 *my_fhandle;
917 if (status.nfsv3.fhs_status != 0) {
918 bb_error_msg("%s:%s failed, reason given by server: %s",
919 hostname, pathname,
920 nfs_strerror(status.nfsv3.fhs_status));
921 goto fail;
922 }
923 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
924 memset(data.old_root.data, 0, NFS_FHSIZE);
925 memset(&data.root, 0, sizeof(data.root));
926 data.root.size = my_fhandle->fhandle3_len;
927 memcpy(data.root.data,
928 (char *) my_fhandle->fhandle3_val,
929 my_fhandle->fhandle3_len);
930
931 data.flags |= NFS_MOUNT_VER3;
932 }
933
934 /* create nfs socket for kernel */
935
936 if (tcp) {
937 if (nfs_mount_version < 3) {
938 bb_error_msg("NFS over TCP is not supported");
939 goto fail;
940 }
941 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
942 } else
943 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
944 if (fsock < 0) {
945 bb_perror_msg("nfs socket");
946 goto fail;
947 }
948 if (bindresvport(fsock, 0) < 0) {
949 bb_perror_msg("nfs bindresvport");
950 goto fail;
951 }
952 if (port == 0) {
953 server_addr.sin_port = PMAPPORT;
954 port = pmap_getport(&server_addr, nfsprog, nfsvers,
955 tcp ? IPPROTO_TCP : IPPROTO_UDP);
956 if (port == 0)
957 port = NFS_PORT;
958#ifdef NFS_MOUNT_DEBUG
959 else
960 printf("used portmapper to find NFS port\n");
961#endif
962 }
963#ifdef NFS_MOUNT_DEBUG
964 printf("using port %d for nfs daemon\n", port);
965#endif
966 server_addr.sin_port = htons(port);
967
968 /* prepare data structure for kernel */
969
970 data.fd = fsock;
971 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
972 strncpy(data.hostname, hostname, sizeof(data.hostname));
973
974 /* clean up */
975
976 auth_destroy(mclient->cl_auth);
977 clnt_destroy(mclient);
978 close(msock);
979
980 if (bg) {
981 /* We must wait until mount directory is available */
982 struct stat statbuf;
983 int delay = 1;
984 while (stat(mp->mnt_dir, &statbuf) == -1) {
985 if (!daemonized) {
986 daemonized = daemonize();
987 if (daemonized <= 0) { /* parent or error */
988 retval = -daemonized;
989 goto ret;
990 }
991 }
992 sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */
993 delay *= 2;
994 if (delay > 30)
995 delay = 30;
996 }
997 }
998
999do_mount: /* perform actual mount */
1000
1001 mp->mnt_type = "nfs";
1002 retval = mount_it_now(mp, vfsflags, (char*)&data);
1003 goto ret;
1004
1005fail: /* abort */
1006
1007 if (msock != -1) {
1008 if (mclient) {
1009 auth_destroy(mclient->cl_auth);
1010 clnt_destroy(mclient);
1011 }
1012 close(msock);
1013 }
1014 if (fsock != -1)
1015 close(fsock);
1016
1017ret:
1018 free(hostname);
1019 free(mounthost);
1020 free(filteropts);
1021 return retval;
1022}