alphabetical order and fix mdev comments about echo>/sys vs echo>/proc/sys
[oweals/busybox.git] / util-linux / nfsmount.c
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  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * Wed Feb  8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
17  * numbers to be specified on the command line.
18  *
19  * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
20  * Omit the call to connect() for Linux version 1.3.11 or later.
21  *
22  * Wed Oct  1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
23  * Implemented the "bg", "fg" and "retry" mount options for NFS.
24  *
25  * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
26  * - added Native Language Support
27  *
28  * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
29  * plus NFSv3 stuff.
30  */
31
32 /*
33  * nfsmount.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
34  */
35
36 #include <unistd.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <netdb.h>
41 #include <sys/socket.h>
42 #include <time.h>
43 #include <sys/utsname.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <stdlib.h>
47 #include "busybox.h"
48 #undef TRUE
49 #undef FALSE
50 #include <rpc/rpc.h>
51 #include <rpc/pmap_prot.h>
52 #include <rpc/pmap_clnt.h>
53 #include "nfsmount.h"
54
55 /* This is just a warning of a common mistake.  Possibly this should be a
56  *  * uclibc faq entry rather than in busybox... */
57 #if ENABLE_FEATURE_MOUNT_NFS && defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
58 #error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support."
59 #endif
60
61
62 /*
63  * NFS stats. The good thing with these values is that NFSv3 errors are
64  * a superset of NFSv2 errors (with the exception of NFSERR_WFLUSH which
65  * no-one uses anyway), so we can happily mix code as long as we make sure
66  * no NFSv3 errors are returned to NFSv2 clients.
67  * Error codes that have a `--' in the v2 column are not part of the
68  * standard, but seem to be widely used nevertheless.
69  */
70 enum nfs_stat {
71         NFS_OK = 0,                     /* v2 v3 */
72         NFSERR_PERM = 1,                /* v2 v3 */
73         NFSERR_NOENT = 2,               /* v2 v3 */
74         NFSERR_IO = 5,                  /* v2 v3 */
75         NFSERR_NXIO = 6,                /* v2 v3 */
76         NFSERR_EAGAIN = 11,             /* v2 v3 */
77         NFSERR_ACCES = 13,              /* v2 v3 */
78         NFSERR_EXIST = 17,              /* v2 v3 */
79         NFSERR_XDEV = 18,               /*    v3 */
80         NFSERR_NODEV = 19,              /* v2 v3 */
81         NFSERR_NOTDIR = 20,             /* v2 v3 */
82         NFSERR_ISDIR = 21,              /* v2 v3 */
83         NFSERR_INVAL = 22,              /* v2 v3 that Sun forgot */
84         NFSERR_FBIG = 27,               /* v2 v3 */
85         NFSERR_NOSPC = 28,              /* v2 v3 */
86         NFSERR_ROFS = 30,               /* v2 v3 */
87         NFSERR_MLINK = 31,              /*    v3 */
88         NFSERR_OPNOTSUPP = 45,          /* v2 v3 */
89         NFSERR_NAMETOOLONG = 63,        /* v2 v3 */
90         NFSERR_NOTEMPTY = 66,           /* v2 v3 */
91         NFSERR_DQUOT = 69,              /* v2 v3 */
92         NFSERR_STALE = 70,              /* v2 v3 */
93         NFSERR_REMOTE = 71,             /* v2 v3 */
94         NFSERR_WFLUSH = 99,             /* v2    */
95         NFSERR_BADHANDLE = 10001,       /*    v3 */
96         NFSERR_NOT_SYNC = 10002,        /*    v3 */
97         NFSERR_BAD_COOKIE = 10003,      /*    v3 */
98         NFSERR_NOTSUPP = 10004,         /*    v3 */
99         NFSERR_TOOSMALL = 10005,        /*    v3 */
100         NFSERR_SERVERFAULT = 10006,     /*    v3 */
101         NFSERR_BADTYPE = 10007,         /*    v3 */
102         NFSERR_JUKEBOX = 10008          /*    v3 */
103 };
104
105 #define NFS_PROGRAM     100003
106
107
108
109 #ifndef NFS_FHSIZE
110 static const int NFS_FHSIZE = 32;
111 #endif
112 #ifndef NFS_PORT
113 static const int NFS_PORT = 2049;
114 #endif
115
116 /* Disable the nls stuff */
117 # undef bindtextdomain
118 # define bindtextdomain(Domain, Directory) /* empty */
119 # undef textdomain
120 # define textdomain(Domain) /* empty */
121 # define _(Text) (Text)
122 # define N_(Text) (Text)
123
124 static const int MS_MGC_VAL = 0xc0ed0000; /* Magic number indicatng "new" flags */
125 static const int MS_RDONLY = 1;      /* Mount read-only */
126 static const int MS_NOSUID = 2;      /* Ignore suid and sgid bits */
127 static const int MS_NODEV = 4;      /* Disallow access to device special files */
128 static const int MS_NOEXEC = 8;      /* Disallow program execution */
129 static const int MS_SYNCHRONOUS = 16;      /* Writes are synced at once */
130 static const int MS_REMOUNT = 32;      /* Alter flags of a mounted FS */
131 static const int MS_MANDLOCK = 64;      /* Allow mandatory locks on an FS */
132 static const int S_QUOTA = 128;     /* Quota initialized for file/directory/symlink */
133 static const int S_APPEND = 256;     /* Append-only file */
134 static const int S_IMMUTABLE = 512;     /* Immutable file */
135 static const int MS_NOATIME = 1024;    /* Do not update access times. */
136 static const int MS_NODIRATIME = 2048;    /* Do not update directory access times */
137
138
139 /*
140  * We want to be able to compile mount on old kernels in such a way
141  * that the binary will work well on more recent kernels.
142  * Thus, if necessary we teach nfsmount.c the structure of new fields
143  * that will come later.
144  *
145  * Moreover, the new kernel includes conflict with glibc includes
146  * so it is easiest to ignore the kernel altogether (at compile time).
147  */
148
149 /* NOTE: Do not make this into a 'static const int' because the pre-processor
150  * needs to test this value in some #if statements. */
151 #define NFS_MOUNT_VERSION 4
152
153 struct nfs2_fh {
154         char                    data[32];
155 };
156 struct nfs3_fh {
157         unsigned short          size;
158         unsigned char           data[64];
159 };
160
161 struct nfs_mount_data {
162         int             version;                /* 1 */
163         int             fd;                     /* 1 */
164         struct nfs2_fh  old_root;               /* 1 */
165         int             flags;                  /* 1 */
166         int             rsize;                  /* 1 */
167         int             wsize;                  /* 1 */
168         int             timeo;                  /* 1 */
169         int             retrans;                /* 1 */
170         int             acregmin;               /* 1 */
171         int             acregmax;               /* 1 */
172         int             acdirmin;               /* 1 */
173         int             acdirmax;               /* 1 */
174         struct sockaddr_in addr;                /* 1 */
175         char            hostname[256];          /* 1 */
176         int             namlen;                 /* 2 */
177         unsigned int    bsize;                  /* 3 */
178         struct nfs3_fh  root;                   /* 4 */
179 };
180
181 /* bits in the flags field */
182
183 static const int NFS_MOUNT_SOFT = 0x0001;       /* 1 */
184 static const int NFS_MOUNT_INTR = 0x0002;       /* 1 */
185 static const int NFS_MOUNT_SECURE = 0x0004;     /* 1 */
186 static const int NFS_MOUNT_POSIX = 0x0008;      /* 1 */
187 static const int NFS_MOUNT_NOCTO = 0x0010;      /* 1 */
188 static const int NFS_MOUNT_NOAC = 0x0020;       /* 1 */
189 static const int NFS_MOUNT_TCP = 0x0040;        /* 2 */
190 static const int NFS_MOUNT_VER3 = 0x0080;       /* 3 */
191 static const int NFS_MOUNT_KERBEROS = 0x0100;   /* 3 */
192 static const int NFS_MOUNT_NONLM = 0x0200;      /* 3 */
193
194
195 #define UTIL_LINUX_VERSION "2.10m"
196 #define util_linux_version "util-linux-2.10m"
197
198 #define HAVE_inet_aton
199 #define HAVE_scsi_h
200 #define HAVE_blkpg_h
201 #define HAVE_kd_h
202 #define HAVE_termcap
203 #define HAVE_locale_h
204 #define HAVE_libintl_h
205 #define ENABLE_NLS
206 #define HAVE_langinfo_h
207 #define HAVE_progname
208 #define HAVE_openpty
209 #define HAVE_nanosleep
210 #define HAVE_personality
211 #define HAVE_tm_gmtoff
212
213 static char *nfs_strerror(int status);
214
215 #define MAKE_VERSION(p,q,r)     (65536*(p) + 256*(q) + (r))
216 #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
217
218 static const int EX_FAIL = 32;       /* mount failure */
219 static const int EX_BG = 256;       /* retry in background (internal only) */
220
221
222 /*
223  * nfs_mount_version according to the sources seen at compile time.
224  */
225 static int nfs_mount_version;
226
227 /*
228  * Unfortunately, the kernel prints annoying console messages
229  * in case of an unexpected nfs mount version (instead of
230  * just returning some error).  Therefore we'll have to try
231  * and figure out what version the kernel expects.
232  *
233  * Variables:
234  *      KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
235  *      NFS_MOUNT_VERSION: these nfsmount sources at compile time
236  *      nfs_mount_version: version this source and running kernel can handle
237  */
238 static void
239 find_kernel_nfs_mount_version(void)
240 {
241         static int kernel_version = 0;
242
243         if (kernel_version)
244                 return;
245
246         nfs_mount_version = NFS_MOUNT_VERSION; /* default */
247
248         kernel_version = get_kernel_revision();
249         if (kernel_version) {
250                 if (kernel_version < MAKE_VERSION(2,1,32))
251                         nfs_mount_version = 1;
252                 else if (kernel_version < MAKE_VERSION(2,2,18) ||
253                                 (kernel_version >=   MAKE_VERSION(2,3,0) &&
254                                  kernel_version < MAKE_VERSION(2,3,99)))
255                         nfs_mount_version = 3;
256                 else
257                         nfs_mount_version = 4; /* since 2.3.99pre4 */
258         }
259         if (nfs_mount_version > NFS_MOUNT_VERSION)
260                 nfs_mount_version = NFS_MOUNT_VERSION;
261 }
262
263 static struct pmap *
264 get_mountport(struct sockaddr_in *server_addr,
265       long unsigned prog,
266       long unsigned version,
267       long unsigned proto,
268       long unsigned port)
269 {
270 struct pmaplist *pmap;
271 static struct pmap p = {0, 0, 0, 0};
272
273 server_addr->sin_port = PMAPPORT;
274 pmap = pmap_getmaps(server_addr);
275
276 if (version > MAX_NFSPROT)
277         version = MAX_NFSPROT;
278 if (!prog)
279         prog = MOUNTPROG;
280 p.pm_prog = prog;
281 p.pm_vers = version;
282 p.pm_prot = proto;
283 p.pm_port = port;
284
285 while (pmap) {
286         if (pmap->pml_map.pm_prog != prog)
287                 goto next;
288         if (!version && p.pm_vers > pmap->pml_map.pm_vers)
289                 goto next;
290         if (version > 2 && pmap->pml_map.pm_vers != version)
291                 goto next;
292         if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
293                 goto next;
294         if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
295             (proto && p.pm_prot && pmap->pml_map.pm_prot != proto) ||
296             (port && pmap->pml_map.pm_port != port))
297                 goto next;
298         memcpy(&p, &pmap->pml_map, sizeof(p));
299 next:
300         pmap = pmap->pml_next;
301 }
302 if (!p.pm_vers)
303         p.pm_vers = MOUNTVERS;
304 if (!p.pm_port)
305         p.pm_port = MOUNTPORT;
306 if (!p.pm_prot)
307         p.pm_prot = IPPROTO_TCP;
308 return &p;
309 }
310
311 int nfsmount(const char *spec, const char *node, int *flags,
312              char **mount_opts, int running_bg)
313 {
314         static char *prev_bg_host;
315         char hostdir[1024];
316         CLIENT *mclient;
317         char *hostname;
318         char *pathname;
319         char *old_opts;
320         char *mounthost=NULL;
321         char new_opts[1024];
322         struct timeval total_timeout;
323         enum clnt_stat clnt_stat;
324         struct nfs_mount_data data;
325         char *opt, *opteq;
326         int val;
327         struct hostent *hp;
328         struct sockaddr_in server_addr;
329         struct sockaddr_in mount_server_addr;
330         struct pmap* pm_mnt;
331         int msock, fsock;
332         struct timeval retry_timeout;
333         union {
334                 struct fhstatus nfsv2;
335                 struct mountres3 nfsv3;
336         } status;
337         struct stat statbuf;
338         char *s;
339         int port;
340         int mountport;
341         int proto;
342         int bg;
343         int soft;
344         int intr;
345         int posix;
346         int nocto;
347         int noac;
348         int nolock;
349         int retry;
350         int tcp;
351         int mountprog;
352         int mountvers;
353         int nfsprog;
354         int nfsvers;
355         int retval;
356         time_t t;
357         time_t prevt;
358         time_t timeout;
359
360         find_kernel_nfs_mount_version();
361
362         retval = EX_FAIL;
363         msock = fsock = -1;
364         mclient = NULL;
365         if (strlen(spec) >= sizeof(hostdir)) {
366                 bb_error_msg("excessively long host:dir argument");
367                 goto fail;
368         }
369         strcpy(hostdir, spec);
370         if ((s = strchr(hostdir, ':'))) {
371                 hostname = hostdir;
372                 pathname = s + 1;
373                 *s = '\0';
374                 /* Ignore all but first hostname in replicated mounts
375                    until they can be fully supported. (mack@sgi.com) */
376                 if ((s = strchr(hostdir, ','))) {
377                         *s = '\0';
378                         bb_error_msg("warning: multiple hostnames not supported");
379                 }
380         } else {
381                 bb_error_msg("directory to mount not in host:dir format");
382                 goto fail;
383         }
384
385         server_addr.sin_family = AF_INET;
386 #ifdef HAVE_inet_aton
387         if (!inet_aton(hostname, &server_addr.sin_addr))
388 #endif
389         {
390                 if ((hp = gethostbyname(hostname)) == NULL) {
391                         bb_herror_msg("%s", hostname);
392                         goto fail;
393                 } else {
394                         if (hp->h_length > sizeof(struct in_addr)) {
395                                 bb_error_msg("got bad hp->h_length");
396                                 hp->h_length = sizeof(struct in_addr);
397                         }
398                         memcpy(&server_addr.sin_addr,
399                                hp->h_addr, hp->h_length);
400                 }
401         }
402
403         memcpy (&mount_server_addr, &server_addr, sizeof (mount_server_addr));
404
405         /* add IP address to mtab options for use when unmounting */
406
407         s = inet_ntoa(server_addr.sin_addr);
408         old_opts = *mount_opts;
409         if (!old_opts)
410                 old_opts = "";
411         if (strlen(old_opts) + strlen(s) + 10 >= sizeof(new_opts)) {
412                 bb_error_msg("excessively long option argument");
413                 goto fail;
414         }
415         sprintf(new_opts, "%s%saddr=%s",
416                 old_opts, *old_opts ? "," : "", s);
417         *mount_opts = bb_xstrdup(new_opts);
418
419         /* Set default options.
420          * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
421          * let the kernel decide.
422          * timeo is filled in after we know whether it'll be TCP or UDP. */
423         memset(&data, 0, sizeof(data));
424         data.retrans    = 3;
425         data.acregmin   = 3;
426         data.acregmax   = 60;
427         data.acdirmin   = 30;
428         data.acdirmax   = 60;
429 #if NFS_MOUNT_VERSION >= 2
430         data.namlen     = NAME_MAX;
431 #endif
432
433         bg = 0;
434         soft = 0;
435         intr = 0;
436         posix = 0;
437         nocto = 0;
438         nolock = 0;
439         noac = 0;
440         retry = 10000;          /* 10000 minutes ~ 1 week */
441         tcp = 0;
442
443         mountprog = MOUNTPROG;
444         mountvers = 0;
445         port = 0;
446         mountport = 0;
447         nfsprog = NFS_PROGRAM;
448         nfsvers = 0;
449
450         /* parse options */
451
452         for (opt = strtok(old_opts, ","); opt; opt = strtok(NULL, ",")) {
453                 if ((opteq = strchr(opt, '='))) {
454                         val = atoi(opteq + 1);
455                         *opteq = '\0';
456                         if (!strcmp(opt, "rsize"))
457                                 data.rsize = val;
458                         else if (!strcmp(opt, "wsize"))
459                                 data.wsize = val;
460                         else if (!strcmp(opt, "timeo"))
461                                 data.timeo = val;
462                         else if (!strcmp(opt, "retrans"))
463                                 data.retrans = val;
464                         else if (!strcmp(opt, "acregmin"))
465                                 data.acregmin = val;
466                         else if (!strcmp(opt, "acregmax"))
467                                 data.acregmax = val;
468                         else if (!strcmp(opt, "acdirmin"))
469                                 data.acdirmin = val;
470                         else if (!strcmp(opt, "acdirmax"))
471                                 data.acdirmax = val;
472                         else if (!strcmp(opt, "actimeo")) {
473                                 data.acregmin = val;
474                                 data.acregmax = val;
475                                 data.acdirmin = val;
476                                 data.acdirmax = val;
477                         }
478                         else if (!strcmp(opt, "retry"))
479                                 retry = val;
480                         else if (!strcmp(opt, "port"))
481                                 port = val;
482                         else if (!strcmp(opt, "mountport"))
483                                 mountport = val;
484                         else if (!strcmp(opt, "mounthost"))
485                                 mounthost=bb_xstrndup(opteq+1,
486                                                   strcspn(opteq+1," \t\n\r,"));
487                         else if (!strcmp(opt, "mountprog"))
488                                 mountprog = val;
489                         else if (!strcmp(opt, "mountvers"))
490                                 mountvers = val;
491                         else if (!strcmp(opt, "nfsprog"))
492                                 nfsprog = val;
493                         else if (!strcmp(opt, "nfsvers") ||
494                                  !strcmp(opt, "vers"))
495                                 nfsvers = val;
496                         else if (!strcmp(opt, "proto")) {
497                                 if (!strncmp(opteq+1, "tcp", 3))
498                                         tcp = 1;
499                                 else if (!strncmp(opteq+1, "udp", 3))
500                                         tcp = 0;
501                                 else
502                                         printf(_("Warning: Unrecognized proto= option.\n"));
503                         } else if (!strcmp(opt, "namlen")) {
504 #if NFS_MOUNT_VERSION >= 2
505                                 if (nfs_mount_version >= 2)
506                                         data.namlen = val;
507                                 else
508 #endif
509                                 printf(_("Warning: Option namlen is not supported.\n"));
510                         } else if (!strcmp(opt, "addr"))
511                                 /* ignore */;
512                         else {
513                                 printf(_("unknown nfs mount parameter: "
514                                        "%s=%d\n"), opt, val);
515                                 goto fail;
516                         }
517                 }
518                 else {
519                         val = 1;
520                         if (!strncmp(opt, "no", 2)) {
521                                 val = 0;
522                                 opt += 2;
523                         }
524                         if (!strcmp(opt, "bg"))
525                                 bg = val;
526                         else if (!strcmp(opt, "fg"))
527                                 bg = !val;
528                         else if (!strcmp(opt, "soft"))
529                                 soft = val;
530                         else if (!strcmp(opt, "hard"))
531                                 soft = !val;
532                         else if (!strcmp(opt, "intr"))
533                                 intr = val;
534                         else if (!strcmp(opt, "posix"))
535                                 posix = val;
536                         else if (!strcmp(opt, "cto"))
537                                 nocto = !val;
538                         else if (!strcmp(opt, "ac"))
539                                 noac = !val;
540                         else if (!strcmp(opt, "tcp"))
541                                 tcp = val;
542                         else if (!strcmp(opt, "udp"))
543                                 tcp = !val;
544                         else if (!strcmp(opt, "lock")) {
545                                 if (nfs_mount_version >= 3)
546                                         nolock = !val;
547                                 else
548                                         printf(_("Warning: option nolock is not supported.\n"));
549                         } else {
550                                 printf(_("unknown nfs mount option: "
551                                            "%s%s\n"), val ? "" : "no", opt);
552                                 goto fail;
553                         }
554                 }
555         }
556         proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
557
558         data.flags = (soft ? NFS_MOUNT_SOFT : 0)
559                 | (intr ? NFS_MOUNT_INTR : 0)
560                 | (posix ? NFS_MOUNT_POSIX : 0)
561                 | (nocto ? NFS_MOUNT_NOCTO : 0)
562                 | (noac ? NFS_MOUNT_NOAC : 0);
563 #if NFS_MOUNT_VERSION >= 2
564         if (nfs_mount_version >= 2)
565                 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
566 #endif
567 #if NFS_MOUNT_VERSION >= 3
568         if (nfs_mount_version >= 3)
569                 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
570 #endif
571         if (nfsvers > MAX_NFSPROT) {
572                 bb_error_msg("NFSv%d not supported!", nfsvers);
573                 return 0;
574         }
575         if (mountvers > MAX_NFSPROT) {
576                 bb_error_msg("NFSv%d not supported!", nfsvers);
577                 return 0;
578         }
579         if (nfsvers && !mountvers)
580                 mountvers = (nfsvers < 3) ? 1 : nfsvers;
581         if (nfsvers && nfsvers < mountvers) {
582                 mountvers = nfsvers;
583         }
584
585         /* Adjust options if none specified */
586         if (!data.timeo)
587                 data.timeo = tcp ? 70 : 7;
588
589 #ifdef NFS_MOUNT_DEBUG
590         printf("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
591                 data.rsize, data.wsize, data.timeo, data.retrans);
592         printf("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n",
593                 data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
594         printf("port = %d, bg = %d, retry = %d, flags = %.8x\n",
595                 port, bg, retry, data.flags);
596         printf("mountprog = %d, mountvers = %d, nfsprog = %d, nfsvers = %d\n",
597                 mountprog, mountvers, nfsprog, nfsvers);
598         printf("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d\n",
599                 (data.flags & NFS_MOUNT_SOFT) != 0,
600                 (data.flags & NFS_MOUNT_INTR) != 0,
601                 (data.flags & NFS_MOUNT_POSIX) != 0,
602                 (data.flags & NFS_MOUNT_NOCTO) != 0,
603                 (data.flags & NFS_MOUNT_NOAC) != 0);
604 #if NFS_MOUNT_VERSION >= 2
605         printf("tcp = %d\n",
606                 (data.flags & NFS_MOUNT_TCP) != 0);
607 #endif
608 #endif
609
610         data.version = nfs_mount_version;
611
612         if (*flags & MS_REMOUNT)
613                 goto copy_data_and_return;
614
615         /*
616          * If the previous mount operation on the same host was
617          * backgrounded, and the "bg" for this mount is also set,
618          * give up immediately, to avoid the initial timeout.
619          */
620         if (bg && !running_bg &&
621             prev_bg_host && strcmp(hostname, prev_bg_host) == 0) {
622                 if (retry > 0)
623                         retval = EX_BG;
624                 return retval;
625         }
626
627         /* create mount deamon client */
628         /* See if the nfs host = mount host. */
629         if (mounthost) {
630           if (mounthost[0] >= '0' && mounthost[0] <= '9') {
631             mount_server_addr.sin_family = AF_INET;
632             mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
633           } else {
634                   if ((hp = gethostbyname(mounthost)) == NULL) {
635                           bb_herror_msg("%s", mounthost);
636                           goto fail;
637                   } else {
638                           if (hp->h_length > sizeof(struct in_addr)) {
639                                   bb_error_msg("got bad hp->h_length?");
640                                   hp->h_length = sizeof(struct in_addr);
641                           }
642                           mount_server_addr.sin_family = AF_INET;
643                           memcpy(&mount_server_addr.sin_addr,
644                                  hp->h_addr, hp->h_length);
645                   }
646           }
647         }
648
649         /*
650          * The following loop implements the mount retries. On the first
651          * call, "running_bg" is 0. When the mount times out, and the
652          * "bg" option is set, the exit status EX_BG will be returned.
653          * For a backgrounded mount, there will be a second call by the
654          * child process with "running_bg" set to 1.
655          *
656          * The case where the mount point is not present and the "bg"
657          * option is set, is treated as a timeout. This is done to
658          * support nested mounts.
659          *
660          * The "retry" count specified by the user is the number of
661          * minutes to retry before giving up.
662          *
663          * Only the first error message will be displayed.
664          */
665         retry_timeout.tv_sec = 3;
666         retry_timeout.tv_usec = 0;
667         total_timeout.tv_sec = 20;
668         total_timeout.tv_usec = 0;
669         timeout = time(NULL) + 60 * retry;
670         prevt = 0;
671         t = 30;
672         val = 1;
673         for (;;) {
674                 if (bg && stat(node, &statbuf) == -1) {
675                         if (running_bg) {
676                                 sleep(val);     /* 1, 2, 4, 8, 16, 30, ... */
677                                 val *= 2;
678                                 if (val > 30)
679                                         val = 30;
680                         }
681                 } else {
682                         /* be careful not to use too many CPU cycles */
683                         if (t - prevt < 30)
684                                 sleep(30);
685
686                         pm_mnt = get_mountport(&mount_server_addr,
687                                        mountprog,
688                                        mountvers,
689                                        proto,
690                                        mountport);
691
692                         /* contact the mount daemon via TCP */
693                         mount_server_addr.sin_port = htons(pm_mnt->pm_port);
694                         msock = RPC_ANYSOCK;
695
696                         switch (pm_mnt->pm_prot) {
697                         case IPPROTO_UDP:
698                                 mclient = clntudp_create(&mount_server_addr,
699                                                  pm_mnt->pm_prog,
700                                                  pm_mnt->pm_vers,
701                                                  retry_timeout,
702                                                  &msock);
703                   if (mclient)
704                           break;
705                   mount_server_addr.sin_port = htons(pm_mnt->pm_port);
706                   msock = RPC_ANYSOCK;
707                 case IPPROTO_TCP:
708                         mclient = clnttcp_create(&mount_server_addr,
709                                                  pm_mnt->pm_prog,
710                                                  pm_mnt->pm_vers,
711                                                  &msock, 0, 0);
712                         break;
713                 default:
714                         mclient = 0;
715                         }
716                         if (mclient) {
717                                 /* try to mount hostname:pathname */
718                                 mclient->cl_auth = authunix_create_default();
719
720                         /* make pointers in xdr_mountres3 NULL so
721                          * that xdr_array allocates memory for us
722                          */
723                         memset(&status, 0, sizeof(status));
724
725                         if (pm_mnt->pm_vers == 3)
726                                 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
727                                                       (xdrproc_t) xdr_dirpath,
728                                                       (caddr_t) &pathname,
729                                                       (xdrproc_t) xdr_mountres3,
730                                                       (caddr_t) &status,
731                                         total_timeout);
732                         else
733                                 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
734                                                       (xdrproc_t) xdr_dirpath,
735                                                       (caddr_t) &pathname,
736                                                       (xdrproc_t) xdr_fhstatus,
737                                                       (caddr_t) &status,
738                                                       total_timeout);
739
740                                 if (clnt_stat == RPC_SUCCESS)
741                                         break;          /* we're done */
742                                 if (errno != ECONNREFUSED) {
743                                         clnt_perror(mclient, "mount");
744                                         goto fail;      /* don't retry */
745                                 }
746                                 if (!running_bg && prevt == 0)
747                                         clnt_perror(mclient, "mount");
748                                 auth_destroy(mclient->cl_auth);
749                                 clnt_destroy(mclient);
750                                 mclient = 0;
751                                 close(msock);
752                         } else {
753                                 if (!running_bg && prevt == 0)
754                                         clnt_pcreateerror("mount");
755                         }
756                         prevt = t;
757                 }
758                 if (!bg)
759                         goto fail;
760                 if (!running_bg) {
761                         prev_bg_host = bb_xstrdup(hostname);
762                         if (retry > 0)
763                                 retval = EX_BG;
764                         goto fail;
765                 }
766                 t = time(NULL);
767                 if (t >= timeout)
768                         goto fail;
769         }
770         nfsvers = (pm_mnt->pm_vers < 2) ? 2 : pm_mnt->pm_vers;
771
772         if (nfsvers == 2) {
773                 if (status.nfsv2.fhs_status != 0) {
774                         bb_error_msg("%s:%s failed, reason given by server: %s",
775                                 hostname, pathname,
776                                 nfs_strerror(status.nfsv2.fhs_status));
777                         goto fail;
778                 }
779                 memcpy(data.root.data,
780                        (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
781                        NFS_FHSIZE);
782 #if NFS_MOUNT_VERSION >= 4
783                 data.root.size = NFS_FHSIZE;
784                 memcpy(data.old_root.data,
785                        (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
786                        NFS_FHSIZE);
787 #endif
788         } else {
789 #if NFS_MOUNT_VERSION >= 4
790                 fhandle3 *my_fhandle;
791                 if (status.nfsv3.fhs_status != 0) {
792                         bb_error_msg("%s:%s failed, reason given by server: %s",
793                                 hostname, pathname,
794                                 nfs_strerror(status.nfsv3.fhs_status));
795                         goto fail;
796                 }
797                 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
798                 memset(data.old_root.data, 0, NFS_FHSIZE);
799                 memset(&data.root, 0, sizeof(data.root));
800                 data.root.size = my_fhandle->fhandle3_len;
801                 memcpy(data.root.data,
802                        (char *) my_fhandle->fhandle3_val,
803                        my_fhandle->fhandle3_len);
804
805                 data.flags |= NFS_MOUNT_VER3;
806 #endif
807         }
808
809         /* create nfs socket for kernel */
810
811         if (tcp) {
812                 if (nfs_mount_version < 3) {
813                         printf(_("NFS over TCP is not supported.\n"));
814                         goto fail;
815                 }
816                 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
817         } else
818                 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
819         if (fsock < 0) {
820                 perror(_("nfs socket"));
821                 goto fail;
822         }
823         if (bindresvport(fsock, 0) < 0) {
824                 perror(_("nfs bindresvport"));
825                 goto fail;
826         }
827         if (port == 0) {
828                 server_addr.sin_port = PMAPPORT;
829                 port = pmap_getport(&server_addr, nfsprog, nfsvers,
830                         tcp ? IPPROTO_TCP : IPPROTO_UDP);
831                 if (port == 0)
832                         port = NFS_PORT;
833 #ifdef NFS_MOUNT_DEBUG
834                 else
835                         printf(_("used portmapper to find NFS port\n"));
836 #endif
837         }
838 #ifdef NFS_MOUNT_DEBUG
839         printf(_("using port %d for nfs deamon\n"), port);
840 #endif
841         server_addr.sin_port = htons(port);
842          /*
843           * connect() the socket for kernels 1.3.10 and below only,
844           * to avoid problems with multihomed hosts.
845           * --Swen
846           */
847         if (get_kernel_revision() <= 66314
848             && connect(fsock, (struct sockaddr *) &server_addr,
849                        sizeof (server_addr)) < 0) {
850                 perror(_("nfs connect"));
851                 goto fail;
852         }
853
854         /* prepare data structure for kernel */
855
856         data.fd = fsock;
857         memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
858         strncpy(data.hostname, hostname, sizeof(data.hostname));
859
860         /* clean up */
861
862         auth_destroy(mclient->cl_auth);
863         clnt_destroy(mclient);
864         close(msock);
865 copy_data_and_return:
866         *mount_opts = xrealloc(*mount_opts, sizeof(data));
867         memcpy(*mount_opts, &data, sizeof(data));
868         return 0;
869
870         /* abort */
871
872 fail:
873         if (msock != -1) {
874                 if (mclient) {
875                         auth_destroy(mclient->cl_auth);
876                         clnt_destroy(mclient);
877                 }
878                 close(msock);
879         }
880         if (fsock != -1)
881                 close(fsock);
882         return retval;
883 }
884
885 /*
886  * We need to translate between nfs status return values and
887  * the local errno values which may not be the same.
888  *
889  * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
890  * "after #include <errno.h> the symbol errno is reserved for any use,
891  *  it cannot even be used as a struct tag or field name".
892  */
893
894 #ifndef EDQUOT
895 #define EDQUOT  ENOSPC
896 #endif
897
898 static const struct {
899         enum nfs_stat stat;
900         int errnum;
901 } nfs_errtbl[] = {
902         { NFS_OK,               0               },
903         { NFSERR_PERM,          EPERM           },
904         { NFSERR_NOENT,         ENOENT          },
905         { NFSERR_IO,            EIO             },
906         { NFSERR_NXIO,          ENXIO           },
907         { NFSERR_ACCES,         EACCES          },
908         { NFSERR_EXIST,         EEXIST          },
909         { NFSERR_NODEV,         ENODEV          },
910         { NFSERR_NOTDIR,        ENOTDIR         },
911         { NFSERR_ISDIR,         EISDIR          },
912 #ifdef NFSERR_INVAL
913         { NFSERR_INVAL,         EINVAL          },      /* that Sun forgot */
914 #endif
915         { NFSERR_FBIG,          EFBIG           },
916         { NFSERR_NOSPC,         ENOSPC          },
917         { NFSERR_ROFS,          EROFS           },
918         { NFSERR_NAMETOOLONG,   ENAMETOOLONG    },
919         { NFSERR_NOTEMPTY,      ENOTEMPTY       },
920         { NFSERR_DQUOT,         EDQUOT          },
921         { NFSERR_STALE,         ESTALE          },
922 #ifdef EWFLUSH
923         { NFSERR_WFLUSH,        EWFLUSH         },
924 #endif
925         /* Throw in some NFSv3 values for even more fun (HP returns these) */
926         { 71,                   EREMOTE         },
927
928         { -1,                   EIO             }
929 };
930
931 static char *nfs_strerror(int status)
932 {
933         int i;
934         static char buf[256];
935
936         for (i = 0; nfs_errtbl[i].stat != -1; i++) {
937                 if (nfs_errtbl[i].stat == status)
938                         return strerror(nfs_errtbl[i].errnum);
939         }
940         sprintf(buf, _("unknown nfs status return value: %d"), status);
941         return buf;
942 }
943
944 static bool_t
945 xdr_fhandle (XDR *xdrs, fhandle objp)
946 {
947         //register int32_t *buf;
948
949          if (!xdr_opaque (xdrs, objp, FHSIZE))
950                  return FALSE;
951         return TRUE;
952 }
953
954 bool_t
955 xdr_fhstatus (XDR *xdrs, fhstatus *objp)
956 {
957         //register int32_t *buf;
958
959          if (!xdr_u_int (xdrs, &objp->fhs_status))
960                  return FALSE;
961         switch (objp->fhs_status) {
962         case 0:
963                  if (!xdr_fhandle (xdrs, objp->fhstatus_u.fhs_fhandle))
964                          return FALSE;
965                 break;
966         default:
967                 break;
968         }
969         return TRUE;
970 }
971
972 bool_t
973 xdr_dirpath (XDR *xdrs, dirpath *objp)
974 {
975         //register int32_t *buf;
976
977          if (!xdr_string (xdrs, objp, MNTPATHLEN))
978                  return FALSE;
979         return TRUE;
980 }
981
982 bool_t
983 xdr_fhandle3 (XDR *xdrs, fhandle3 *objp)
984 {
985         //register int32_t *buf;
986
987          if (!xdr_bytes (xdrs, (char **)&objp->fhandle3_val, (unsigned int *) &objp->fhandle3_len, FHSIZE3))
988                  return FALSE;
989         return TRUE;
990 }
991
992 bool_t
993 xdr_mountres3_ok (XDR *xdrs, mountres3_ok *objp)
994 {
995         //register int32_t *buf;
996
997          if (!xdr_fhandle3 (xdrs, &objp->fhandle))
998                  return FALSE;
999          if (!xdr_array (xdrs, (char **)&objp->auth_flavours.auth_flavours_val, (unsigned int *) &objp->auth_flavours.auth_flavours_len, ~0,
1000                 sizeof (int), (xdrproc_t) xdr_int))
1001                  return FALSE;
1002         return TRUE;
1003 }
1004
1005 bool_t
1006 xdr_mountstat3 (XDR *xdrs, mountstat3 *objp)
1007 {
1008         //register int32_t *buf;
1009
1010          if (!xdr_enum (xdrs, (enum_t *) objp))
1011                  return FALSE;
1012         return TRUE;
1013 }
1014
1015 bool_t
1016 xdr_mountres3 (XDR *xdrs, mountres3 *objp)
1017 {
1018         //register int32_t *buf;
1019
1020          if (!xdr_mountstat3 (xdrs, &objp->fhs_status))
1021                  return FALSE;
1022         switch (objp->fhs_status) {
1023         case MNT_OK:
1024                  if (!xdr_mountres3_ok (xdrs, &objp->mountres3_u.mountinfo))
1025                          return FALSE;
1026                 break;
1027         default:
1028                 break;
1029         }
1030         return TRUE;
1031 }
1032