]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsmount.c
nfs-utils: NFSv4: fix backgrounding
[nfs-utils.git] / utils / mount / nfsmount.c
1 /*
2  * nfsmount.c -- Linux NFS mount
3  * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * Wed Feb  8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
16  * numbers to be specified on the command line.
17  *
18  * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
19  * Omit the call to connect() for Linux version 1.3.11 or later.
20  *
21  * Wed Oct  1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
22  * Implemented the "bg", "fg" and "retry" mount options for NFS.
23  *
24  * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
25  * - added Native Language Support
26  *
27  * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
28  * plus NFSv3 stuff.
29  *
30  * 2006-06-06 Amit Gud <agud@redhat.com>
31  * - Moved with modifcations to nfs-utils/utils/mount from util-linux/mount.
32  */
33
34 /*
35  * nfsmount.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <ctype.h>
43 #include <unistd.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <errno.h>
48 #include <netdb.h>
49 #include <time.h>
50 #include <rpc/rpc.h>
51 #include <rpc/pmap_prot.h>
52 #include <rpc/pmap_clnt.h>
53 #include <sys/socket.h>
54 #include <sys/time.h>
55 #include <sys/stat.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
58 #include <mntent.h>
59 #include <sys/mount.h>
60 #include <paths.h>
61 #include <syslog.h>
62
63 #include "xcommon.h"
64 #include "mount.h"
65 #include "nfs_mount.h"
66 #include "mount_constants.h"
67 #include "nls.h"
68 #include "error.h"
69 #include "network.h"
70 #include "version.h"
71
72 #ifdef HAVE_RPCSVC_NFS_PROT_H
73 #include <rpcsvc/nfs_prot.h>
74 #else
75 #include <linux/nfs.h>
76 #define nfsstat nfs_stat
77 #endif
78
79 #ifndef NFS_PORT
80 #define NFS_PORT 2049
81 #endif
82 #ifndef NFS_FHSIZE
83 #define NFS_FHSIZE 32
84 #endif
85
86 #ifndef HAVE_INET_ATON
87 #define inet_aton(a,b) (0)
88 #endif
89
90 typedef dirpath mnt2arg_t;
91 typedef dirpath mnt3arg_t;
92 typedef dirpath mntarg_t;
93
94 typedef struct fhstatus  mnt2res_t;
95 typedef struct mountres3 mnt3res_t;
96 typedef union {
97         mnt2res_t nfsv2;
98         mnt3res_t nfsv3;
99 } mntres_t;
100
101 extern int nfs_mount_data_version;
102 extern char *progname;
103 extern int verbose;
104 extern int sloppy;
105
106 static inline enum clnt_stat
107 nfs3_mount(CLIENT *clnt, mnt3arg_t *mnt3arg, mnt3res_t *mnt3res)
108 {
109         return clnt_call(clnt, MOUNTPROC3_MNT,
110                          (xdrproc_t) xdr_dirpath, (caddr_t) mnt3arg,
111                          (xdrproc_t) xdr_mountres3, (caddr_t) mnt3res,
112                          TIMEOUT);
113 }
114
115 static inline enum clnt_stat
116 nfs2_mount(CLIENT *clnt, mnt2arg_t *mnt2arg, mnt2res_t *mnt2res)
117 {
118         return clnt_call(clnt, MOUNTPROC_MNT,
119                          (xdrproc_t) xdr_dirpath, (caddr_t) mnt2arg,
120                          (xdrproc_t) xdr_fhstatus, (caddr_t) mnt2res,
121                          TIMEOUT);
122 }
123
124 static int
125 nfs_call_mount(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server,
126                mntarg_t *mntarg, mntres_t *mntres)
127 {
128         CLIENT *clnt;
129         enum clnt_stat stat;
130         int msock;
131
132         if (!probe_bothports(mnt_server, nfs_server))
133                 goto out_bad;
134
135         clnt = mnt_openclnt(mnt_server, &msock);
136         if (!clnt)
137                 goto out_bad;
138         /* make pointers in xdr_mountres3 NULL so
139          * that xdr_array allocates memory for us
140          */
141         memset(mntres, 0, sizeof(*mntres));
142         switch (mnt_server->pmap.pm_vers) {
143         case 3:
144                 stat = nfs3_mount(clnt, mntarg, &mntres->nfsv3);
145                 break;
146         case 2:
147         case 1:
148                 stat = nfs2_mount(clnt, mntarg, &mntres->nfsv2);
149                 break;
150         default:
151                 goto out_bad;
152         }
153         if (stat != RPC_SUCCESS) {
154                 clnt_geterr(clnt, &rpc_createerr.cf_error);
155                 rpc_createerr.cf_stat = stat;
156         }
157         mnt_closeclnt(clnt, msock);
158         if (stat == RPC_SUCCESS)
159                 return 1;
160  out_bad:
161         return 0;
162 }
163
164 static int
165 parse_options(char *old_opts, struct nfs_mount_data *data,
166               int *bg, int *retry, clnt_addr_t *mnt_server,
167               clnt_addr_t *nfs_server, char *new_opts, const int opt_size)
168 {
169         struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
170         struct pmap *mnt_pmap = &mnt_server->pmap;
171         struct pmap *nfs_pmap = &nfs_server->pmap;
172         int len;
173         char *opt, *opteq, *p, *opt_b, *tmp_opts;
174         char *mounthost = NULL;
175         char cbuf[128];
176         int open_quote = 0;
177
178         data->flags = 0;
179         *bg = 0;
180
181         len = strlen(new_opts);
182         tmp_opts = xstrdup(old_opts);
183         for (p=tmp_opts, opt_b=NULL; p && *p; p++) {
184                 if (!opt_b)
185                         opt_b = p;              /* begin of the option item */
186                 if (*p == '"')
187                         open_quote ^= 1;        /* reverse the status */
188                 if (open_quote)
189                         continue;               /* still in a quoted block */
190                 if (*p == ',')
191                         *p = '\0';              /* terminate the option item */
192                 if (*p == '\0' || *(p+1) == '\0') {
193                         opt = opt_b;            /* opt is useful now */
194                         opt_b = NULL;
195                 }
196                 else
197                         continue;               /* still somewhere in the option item */
198
199                 if (strlen(opt) >= sizeof(cbuf))
200                         goto bad_parameter;
201                 if ((opteq = strchr(opt, '=')) && isdigit(opteq[1])) {
202                         int val = atoi(opteq + 1);      
203                         *opteq = '\0';
204                         if (!strcmp(opt, "rsize"))
205                                 data->rsize = val;
206                         else if (!strcmp(opt, "wsize"))
207                                 data->wsize = val;
208                         else if (!strcmp(opt, "timeo"))
209                                 data->timeo = val;
210                         else if (!strcmp(opt, "retrans"))
211                                 data->retrans = val;
212                         else if (!strcmp(opt, "acregmin"))
213                                 data->acregmin = val;
214                         else if (!strcmp(opt, "acregmax"))
215                                 data->acregmax = val;
216                         else if (!strcmp(opt, "acdirmin"))
217                                 data->acdirmin = val;
218                         else if (!strcmp(opt, "acdirmax"))
219                                 data->acdirmax = val;
220                         else if (!strcmp(opt, "actimeo")) {
221                                 data->acregmin = val;
222                                 data->acregmax = val;
223                                 data->acdirmin = val;
224                                 data->acdirmax = val;
225                         }
226                         else if (!strcmp(opt, "retry"))
227                                 *retry = val;
228                         else if (!strcmp(opt, "port"))
229                                 nfs_pmap->pm_port = val;
230                         else if (!strcmp(opt, "mountport"))
231                                 mnt_pmap->pm_port = val;
232                         else if (!strcmp(opt, "mountprog"))
233                                 mnt_pmap->pm_prog = val;
234                         else if (!strcmp(opt, "mountvers"))
235                                 mnt_pmap->pm_vers = val;
236                         else if (!strcmp(opt, "mounthost"))
237                                 mounthost=xstrndup(opteq+1, strcspn(opteq+1," \t\n\r,"));
238                         else if (!strcmp(opt, "nfsprog"))
239                                 nfs_pmap->pm_prog = val;
240                         else if (!strcmp(opt, "nfsvers") ||
241                                  !strcmp(opt, "vers")) {
242                                 nfs_pmap->pm_vers = val;
243                                 opt = "nfsvers";
244 #if NFS_MOUNT_VERSION >= 2
245                         } else if (!strcmp(opt, "namlen")) {
246                                 if (nfs_mount_data_version >= 2)
247                                         data->namlen = val;
248                                 else if (sloppy)
249                                         continue;
250                                 else
251                                         goto bad_parameter;
252 #endif
253                         } else if (!strcmp(opt, "addr")) {
254                                 /* ignore */;
255                                 continue;
256                         } else if (sloppy)
257                                 continue;
258                         else
259                                 goto bad_parameter;
260                         sprintf(cbuf, "%s=%s,", opt, opteq+1);
261                 } else if (opteq) {
262                         *opteq = '\0';
263                         if (!strcmp(opt, "proto")) {
264                                 if (!strcmp(opteq+1, "udp")) {
265                                         nfs_pmap->pm_prot = IPPROTO_UDP;
266                                         mnt_pmap->pm_prot = IPPROTO_UDP;
267 #if NFS_MOUNT_VERSION >= 2
268                                         data->flags &= ~NFS_MOUNT_TCP;
269                                 } else if (!strcmp(opteq+1, "tcp") &&
270                                            nfs_mount_data_version > 2) {
271                                         nfs_pmap->pm_prot = IPPROTO_TCP;
272                                         mnt_pmap->pm_prot = IPPROTO_TCP;
273                                         data->flags |= NFS_MOUNT_TCP;
274 #endif
275                                 } else if (sloppy)
276                                         continue;
277                                 else
278                                         goto bad_parameter;
279 #if NFS_MOUNT_VERSION >= 5
280                         } else if (!strcmp(opt, "sec")) {
281                                 char *secflavor = opteq+1;
282                                 /* see RFC 2623 */
283                                 if (nfs_mount_data_version < 5) {
284                                         printf(_("Warning: ignoring sec=%s option\n"),
285                                                         secflavor);
286                                         continue;
287                                 } else if (!strcmp(secflavor, "none"))
288                                         data->pseudoflavor = AUTH_NONE;
289                                 else if (!strcmp(secflavor, "sys"))
290                                         data->pseudoflavor = AUTH_SYS;
291                                 else if (!strcmp(secflavor, "krb5"))
292                                         data->pseudoflavor = AUTH_GSS_KRB5;
293                                 else if (!strcmp(secflavor, "krb5i"))
294                                         data->pseudoflavor = AUTH_GSS_KRB5I;
295                                 else if (!strcmp(secflavor, "krb5p"))
296                                         data->pseudoflavor = AUTH_GSS_KRB5P;
297                                 else if (!strcmp(secflavor, "lipkey"))
298                                         data->pseudoflavor = AUTH_GSS_LKEY;
299                                 else if (!strcmp(secflavor, "lipkey-i"))
300                                         data->pseudoflavor = AUTH_GSS_LKEYI;
301                                 else if (!strcmp(secflavor, "lipkey-p"))
302                                         data->pseudoflavor = AUTH_GSS_LKEYP;
303                                 else if (!strcmp(secflavor, "spkm3"))
304                                         data->pseudoflavor = AUTH_GSS_SPKM;
305                                 else if (!strcmp(secflavor, "spkm3i"))
306                                         data->pseudoflavor = AUTH_GSS_SPKMI;
307                                 else if (!strcmp(secflavor, "spkm3p"))
308                                         data->pseudoflavor = AUTH_GSS_SPKMP;
309                                 else if (sloppy)
310                                         continue;
311                                 else {
312                                         printf(_("Warning: Unrecognized security flavor %s.\n"),
313                                                 secflavor);
314                                         goto bad_parameter;
315                                 }
316                                 data->flags |= NFS_MOUNT_SECFLAVOUR;
317 #endif
318                         } else if (!strcmp(opt, "mounthost"))
319                                 mounthost=xstrndup(opteq+1,
320                                                    strcspn(opteq+1," \t\n\r,"));
321                          else if (!strcmp(opt, "context")) {
322                                 char *context = opteq + 1;
323                                 int ctxlen = strlen(context);
324
325                                 if (ctxlen > NFS_MAX_CONTEXT_LEN) {
326                                         nfs_error(_("context parameter exceeds"
327                                                         " limit of %d"),
328                                                         NFS_MAX_CONTEXT_LEN);
329                                         goto bad_parameter;
330                                 }
331                                 /* The context string is in the format of
332                                  * "system_u:object_r:...".  We only want
333                                  * the context str between the quotes.
334                                  */
335                                 if (*context == '"')
336                                         strncpy(data->context, context+1,
337                                                         ctxlen-2);
338                                 else
339                                         strncpy(data->context, context,
340                                                         NFS_MAX_CONTEXT_LEN);
341                         } else if (sloppy)
342                                 continue;
343                         else
344                                 goto bad_parameter;
345                         sprintf(cbuf, "%s=%s,", opt, opteq+1);
346                 } else {
347                         int val = 1;
348                         if (!strncmp(opt, "no", 2)) {
349                                 val = 0;
350                                 opt += 2;
351                         }
352                         if (!strcmp(opt, "bg"))
353                                 *bg = val;
354                         else if (!strcmp(opt, "fg"))
355                                 *bg = !val;
356                         else if (!strcmp(opt, "soft")) {
357                                 data->flags &= ~NFS_MOUNT_SOFT;
358                                 if (val)
359                                         data->flags |= NFS_MOUNT_SOFT;
360                         } else if (!strcmp(opt, "hard")) {
361                                 data->flags &= ~NFS_MOUNT_SOFT;
362                                 if (!val)
363                                         data->flags |= NFS_MOUNT_SOFT;
364                         } else if (!strcmp(opt, "intr")) {
365                                 data->flags &= ~NFS_MOUNT_INTR;
366                                 if (val)
367                                         data->flags |= NFS_MOUNT_INTR;
368                         } else if (!strcmp(opt, "posix")) {
369                                 data->flags &= ~NFS_MOUNT_POSIX;
370                                 if (val)
371                                         data->flags |= NFS_MOUNT_POSIX;
372                         } else if (!strcmp(opt, "cto")) {
373                                 data->flags &= ~NFS_MOUNT_NOCTO;
374                                 if (!val)
375                                         data->flags |= NFS_MOUNT_NOCTO;
376                         } else if (!strcmp(opt, "ac")) {
377                                 data->flags &= ~NFS_MOUNT_NOAC;
378                                 if (!val)
379                                         data->flags |= NFS_MOUNT_NOAC;
380 #if NFS_MOUNT_VERSION >= 2
381                         } else if (!strcmp(opt, "tcp")) {
382                                 data->flags &= ~NFS_MOUNT_TCP;
383                                 if (val) {
384                                         if (nfs_mount_data_version < 2)
385                                                 goto bad_option;
386                                         nfs_pmap->pm_prot = IPPROTO_TCP;
387                                         mnt_pmap->pm_prot = IPPROTO_TCP;
388                                         data->flags |= NFS_MOUNT_TCP;
389                                 } else {
390                                         mnt_pmap->pm_prot = IPPROTO_UDP;
391                                         nfs_pmap->pm_prot = IPPROTO_UDP;
392                                 }
393                         } else if (!strcmp(opt, "udp")) {
394                                 data->flags &= ~NFS_MOUNT_TCP;
395                                 if (!val) {
396                                         if (nfs_mount_data_version < 2)
397                                                 goto bad_option;
398                                         nfs_pmap->pm_prot = IPPROTO_TCP;
399                                         mnt_pmap->pm_prot = IPPROTO_TCP;
400                                         data->flags |= NFS_MOUNT_TCP;
401                                 } else {
402                                         nfs_pmap->pm_prot = IPPROTO_UDP;
403                                         mnt_pmap->pm_prot = IPPROTO_UDP;
404                                 }
405 #endif
406 #if NFS_MOUNT_VERSION >= 3
407                         } else if (!strcmp(opt, "lock")) {
408                                 data->flags &= ~NFS_MOUNT_NONLM;
409                                 if (!val) {
410                                         if (nfs_mount_data_version < 3)
411                                                 goto bad_option;
412                                         data->flags |= NFS_MOUNT_NONLM;
413                                 }
414 #endif
415 #if NFS_MOUNT_VERSION >= 4
416                         } else if (!strcmp(opt, "broken_suid")) {
417                                 data->flags &= ~NFS_MOUNT_BROKEN_SUID;
418                                 if (val) {
419                                         if (nfs_mount_data_version < 4)
420                                                 goto bad_option;
421                                         data->flags |= NFS_MOUNT_BROKEN_SUID;
422                                 }
423                         } else if (!strcmp(opt, "acl")) {
424                                 data->flags &= ~NFS_MOUNT_NOACL;
425                                 if (!val)
426                                         data->flags |= NFS_MOUNT_NOACL;
427                         } else if (!strcmp(opt, "rdirplus")) {
428                                 data->flags &= ~NFS_MOUNT_NORDIRPLUS;
429                                 if (!val)
430                                         data->flags |= NFS_MOUNT_NORDIRPLUS;
431                         } else if (!strcmp(opt, "sharecache")) {
432                                 data->flags &= ~NFS_MOUNT_UNSHARED;
433                                 if (!val)
434                                         data->flags |= NFS_MOUNT_UNSHARED;
435 #endif
436                         } else {
437                         bad_option:
438                                 if (sloppy)
439                                         continue;
440                                 nfs_error(_("%s: Unsupported nfs mount option:"
441                                                 " %s%s"), progname,
442                                                 val ? "" : "no", opt);
443                                 goto out_bad;
444                         }
445                         sprintf(cbuf, val ? "%s," : "no%s,", opt);
446                 }
447                 len += strlen(cbuf);
448                 if (len >= opt_size) {
449                         nfs_error(_("%s: excessively long option argument"),
450                                         progname);
451                         goto out_bad;
452                 }
453                 strcat(new_opts, cbuf);
454         }
455         /* See if the nfs host = mount host. */
456         if (mounthost) {
457                 if (!nfs_gethostbyname(mounthost, mnt_saddr))
458                         goto out_bad;
459                 *mnt_server->hostname = mounthost;
460         }
461         free(tmp_opts);
462         return 1;
463  bad_parameter:
464         nfs_error(_("%s: Bad nfs mount parameter: %s\n"), progname, opt);
465  out_bad:
466         free(tmp_opts);
467         return 0;
468 }
469
470 static int nfsmnt_check_compat(const struct pmap *nfs_pmap,
471                                 const struct pmap *mnt_pmap)
472 {
473         unsigned int max_nfs_vers = (nfs_mount_data_version >= 4) ? 3 : 2;
474         unsigned int max_mnt_vers = (nfs_mount_data_version >= 4) ? 3 : 2;
475
476         if (nfs_pmap->pm_vers == 4) {
477                 nfs_error(_("%s: Please use '-t nfs4' "
478                                 "instead of '-o vers=4'"), progname);
479                 goto out_bad;
480         }
481
482         if (nfs_pmap->pm_vers) {
483                 if (nfs_pmap->pm_vers > max_nfs_vers || nfs_pmap->pm_vers < 2) {
484                         nfs_error(_("%s: NFS version %ld is not supported"),
485                                         progname, nfs_pmap->pm_vers);
486                         goto out_bad;
487                 }
488         }
489
490         if (mnt_pmap->pm_vers > max_mnt_vers) {
491                 nfs_error(_("%s: NFS mount version %ld is not supported"),
492                                 progname, mnt_pmap->pm_vers);
493                 goto out_bad;
494         }
495
496         return 1;
497
498 out_bad:
499         return 0;
500 }
501
502 int
503 nfsmount(const char *spec, const char *node, int flags,
504          char **extra_opts, int fake, int running_bg)
505 {
506         char hostdir[1024];
507         char *hostname, *dirname, *old_opts, *mounthost = NULL;
508         char new_opts[1024], cbuf[1024];
509         static struct nfs_mount_data data;
510         int val;
511         static int doonce = 0;
512
513         clnt_addr_t mnt_server = { &mounthost, };
514         clnt_addr_t nfs_server = { &hostname, };
515         struct sockaddr_in *nfs_saddr = &nfs_server.saddr;
516         struct pmap  *mnt_pmap = &mnt_server.pmap,
517                      *nfs_pmap = &nfs_server.pmap;
518         struct pmap  save_mnt, save_nfs;
519
520         int fsock = -1;
521
522         mntres_t mntres;
523
524         struct stat statbuf;
525         char *s;
526         int bg, retry;
527         int retval = EX_FAIL;
528         time_t t;
529         time_t prevt;
530         time_t timeout;
531
532         if (strlen(spec) >= sizeof(hostdir)) {
533                 nfs_error(_("%s: excessively long host:dir argument"),
534                                 progname);
535                 goto fail;
536         }
537         strcpy(hostdir, spec);
538         if ((s = strchr(hostdir, ':'))) {
539                 hostname = hostdir;
540                 dirname = s + 1;
541                 *s = '\0';
542                 /* Ignore all but first hostname in replicated mounts
543                    until they can be fully supported. (mack@sgi.com) */
544                 if ((s = strchr(hostdir, ','))) {
545                         *s = '\0';
546                         nfs_error(_("%s: warning: "
547                                   "multiple hostnames not supported"),
548                                         progname);
549                 }
550         } else {
551                 nfs_error(_("%s: directory to mount not in host:dir format"),
552                                 progname);
553                 goto fail;
554         }
555
556         if (!nfs_gethostbyname(hostname, nfs_saddr))
557                 goto fail;
558         mounthost = hostname;
559         memcpy (&mnt_server.saddr, nfs_saddr, sizeof (mnt_server.saddr));
560
561         /* add IP address to mtab options for use when unmounting */
562
563         s = inet_ntoa(nfs_saddr->sin_addr);
564         old_opts = *extra_opts;
565         if (!old_opts)
566                 old_opts = "";
567
568         /* Set default options.
569          * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
570          * let the kernel decide.
571          * timeo is filled in after we know whether it'll be TCP or UDP. */
572         memset(&data, 0, sizeof(data));
573         data.acregmin   = 3;
574         data.acregmax   = 60;
575         data.acdirmin   = 30;
576         data.acdirmax   = 60;
577 #if NFS_MOUNT_VERSION >= 2
578         data.namlen     = NAME_MAX;
579 #endif
580
581         bg = 0;
582         retry = -1;
583
584         memset(mnt_pmap, 0, sizeof(*mnt_pmap));
585         mnt_pmap->pm_prog = MOUNTPROG;
586         memset(nfs_pmap, 0, sizeof(*nfs_pmap));
587         nfs_pmap->pm_prog = NFS_PROGRAM;
588
589         /* parse options */
590         new_opts[0] = 0;
591         if (!parse_options(old_opts, &data, &bg, &retry, &mnt_server, &nfs_server,
592                            new_opts, sizeof(new_opts)))
593                 goto fail;
594         if (!nfsmnt_check_compat(nfs_pmap, mnt_pmap))
595                 goto fail;
596
597         if (retry == -1) {
598                 if (bg)
599                         retry = 10000;  /* 10000 mins == ~1 week*/
600                 else
601                         retry = 2;      /* 2 min default on fg mounts */
602         }
603
604 #ifdef NFS_MOUNT_DEBUG
605         printf(_("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n"),
606                data.rsize, data.wsize, data.timeo, data.retrans);
607         printf(_("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n"),
608                data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
609         printf(_("port = %lu, bg = %d, retry = %d, flags = %.8x\n"),
610                nfs_pmap->pm_port, bg, retry, data.flags);
611         printf(_("mountprog = %lu, mountvers = %lu, nfsprog = %lu, nfsvers = %lu\n"),
612                mnt_pmap->pm_prog, mnt_pmap->pm_vers,
613                nfs_pmap->pm_prog, nfs_pmap->pm_vers);
614         printf(_("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d"),
615                (data.flags & NFS_MOUNT_SOFT) != 0,
616                (data.flags & NFS_MOUNT_INTR) != 0,
617                (data.flags & NFS_MOUNT_POSIX) != 0,
618                (data.flags & NFS_MOUNT_NOCTO) != 0,
619                (data.flags & NFS_MOUNT_NOAC) != 0);
620 #if NFS_MOUNT_VERSION >= 2
621         printf(_(", tcp = %d"),
622                (data.flags & NFS_MOUNT_TCP) != 0);
623 #endif
624 #if NFS_MOUNT_VERSION >= 4
625         printf(_(", noacl = %d"), (data.flags & NFS_MOUNT_NOACL) != 0);
626 #endif
627 #if NFS_MOUNT_VERSION >= 5
628         printf(_(", sec = %u"), data.pseudoflavor);
629         printf(_(", readdirplus = %d"), (data.flags & NFS_MOUNT_NORDIRPLUS) != 0);
630 #endif
631         printf("\n");
632 #endif
633
634         data.version = nfs_mount_data_version;
635
636         if (flags & MS_REMOUNT)
637                 goto out_ok;
638
639         /* create mount deamon client */
640
641         /*
642          * The following loop implements the mount retries. On the first
643          * call, "running_bg" is 0. When the mount times out, and the
644          * "bg" option is set, the exit status EX_BG will be returned.
645          * For a backgrounded mount, there will be a second call by the
646          * child process with "running_bg" set to 1.
647          *
648          * The case where the mount point is not present and the "bg"
649          * option is set, is treated as a timeout. This is done to
650          * support nested mounts.
651          *
652          * The "retry" count specified by the user is the number of
653          * minutes to retry before giving up.
654          *
655          * Only the first error message will be displayed.
656          */
657         timeout = time(NULL) + 60 * retry;
658         prevt = 0;
659         t = 30;
660         val = 1;
661
662         memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
663         memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
664         for (;;) {
665                 if (bg && stat(node, &statbuf) == -1) {
666                         /* no mount point yet - sleep */
667                         if (running_bg) {
668                                 sleep(val);     /* 1, 2, 4, 8, 16, 30, ... */
669                                 val *= 2;
670                                 if (val > 30)
671                                         val = 30;
672                         }
673                 } else {
674                         int stat;
675                         /* be careful not to use too many CPU cycles */
676                         if (t - prevt < 30)
677                                 sleep(30);
678
679                         stat = nfs_call_mount(&mnt_server, &nfs_server,
680                                               &dirname, &mntres);
681                         if (stat)
682                                 break;
683                         memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
684                         memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
685                         prevt = t;
686                 }
687                 if (!bg) {
688                         switch(rpc_createerr.cf_stat){
689                         case RPC_TIMEDOUT:
690                                 break;
691                         case RPC_SYSTEMERROR:
692                                 if (errno == ETIMEDOUT)
693                                         break;
694                         default:
695                                 rpc_mount_errors(*nfs_server.hostname, 0, bg);
696                         goto fail;
697                         }
698                         t = time(NULL);
699                         if (t >= timeout) {
700                                 rpc_mount_errors(*nfs_server.hostname, 0, bg);
701                                 goto fail;
702                         }
703                         rpc_mount_errors(*nfs_server.hostname, 1, bg);
704                         continue;
705                 }
706                 if (!running_bg) {
707                         if (retry > 0)
708                                 retval = EX_BG;
709                         goto fail;
710                 }
711                 t = time(NULL);
712                 if (t >= timeout) {
713                         rpc_mount_errors(*nfs_server.hostname, 0, bg);
714                         goto fail;
715                 }
716                 if (doonce++ < 1)
717                         rpc_mount_errors(*nfs_server.hostname, 1, bg);
718         }
719
720         if (mnt_pmap->pm_vers <= 2) {
721                 if (mntres.nfsv2.fhs_status != 0) {
722                         nfs_error(_("%s: %s:%s failed, reason given by server: %s"),
723                                         progname, hostname, dirname,
724                                         nfs_strerror(mntres.nfsv2.fhs_status));
725                         goto fail;
726                 }
727                 memcpy(data.root.data,
728                        (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
729                        NFS_FHSIZE);
730 #if NFS_MOUNT_VERSION >= 4
731                 data.root.size = NFS_FHSIZE;
732                 memcpy(data.old_root.data,
733                        (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
734                        NFS_FHSIZE);
735 #endif
736         } else {
737 #if NFS_MOUNT_VERSION >= 4
738                 mountres3_ok *mountres;
739                 fhandle3 *fhandle;
740                 int i,  n_flavors, *flavor, yum = 0;
741                 if (mntres.nfsv3.fhs_status != 0) {
742                         nfs_error(_("%s: %s:%s failed, reason given by server: %s"),
743                                         progname, hostname, dirname,
744                                         nfs_strerror(mntres.nfsv3.fhs_status));
745                         goto fail;
746                 }
747 #if NFS_MOUNT_VERSION >= 5
748                 mountres = &mntres.nfsv3.mountres3_u.mountinfo;
749                 n_flavors = mountres->auth_flavors.auth_flavors_len;
750                 if (n_flavors <= 0)
751                         goto noauth_flavors;
752
753                 flavor = mountres->auth_flavors.auth_flavors_val;
754                 for (i = 0; i < n_flavors; ++i) {
755                         /*
756                          * Per RFC2623, section 2.7, we should prefer the
757                          * flavour listed first.
758                          * If no flavour requested, use the first simple
759                          * flavour that is offered.
760                          */
761                         if (! (data.flags & NFS_MOUNT_SECFLAVOUR) &&
762                             (flavor[i] == AUTH_SYS ||
763                              flavor[i] == AUTH_NONE)) {
764                                 data.pseudoflavor = flavor[i];
765                                 data.flags |= NFS_MOUNT_SECFLAVOUR;
766                         }
767                         if (flavor[i] == data.pseudoflavor)
768                                 yum = 1;
769 #ifdef NFS_MOUNT_DEBUG
770                         printf(_("auth flavor %d: %d\n"), i, flavor[i]);
771 #endif
772                 }
773                 if (!yum) {
774                         nfs_error(_("%s: %s:%s failed, security flavor "
775                                         "not supported"),
776                                         progname, hostname, dirname);
777                         /* server has registered us in rmtab, send umount */
778                         nfs_call_umount(&mnt_server, &dirname);
779                         goto fail;
780                 }
781 noauth_flavors:
782 #endif
783                 fhandle = &mntres.nfsv3.mountres3_u.mountinfo.fhandle;
784                 memset(data.old_root.data, 0, NFS_FHSIZE);
785                 memset(&data.root, 0, sizeof(data.root));
786                 data.root.size = fhandle->fhandle3_len;
787                 memcpy(data.root.data,
788                        (char *) fhandle->fhandle3_val,
789                        fhandle->fhandle3_len);
790
791                 data.flags |= NFS_MOUNT_VER3;
792 #endif
793         }
794
795         if (nfs_mount_data_version == 1) {
796                 /* create nfs socket for kernel */
797                 if (nfs_pmap->pm_prot == IPPROTO_TCP)
798                         fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
799                 else
800                         fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
801                 if (fsock < 0) {
802                         perror(_("nfs socket"));
803                         goto fail;
804                 }
805                 if (bindresvport(fsock, 0) < 0) {
806                         perror(_("nfs bindresvport"));
807                         goto fail;
808                 }
809         }
810
811 #ifdef NFS_MOUNT_DEBUG
812         printf(_("using port %lu for nfs deamon\n"), nfs_pmap->pm_port);
813 #endif
814         nfs_saddr->sin_port = htons(nfs_pmap->pm_port);
815         /*
816          * connect() the socket for kernels 1.3.10 and below only,
817          * to avoid problems with multihomed hosts.
818          * --Swen
819          */
820         if (linux_version_code() <= MAKE_VERSION(1, 3, 10) && fsock != -1
821             && connect(fsock, (struct sockaddr *) nfs_saddr,
822                        sizeof (*nfs_saddr)) < 0) {
823                 perror(_("nfs connect"));
824                 goto fail;
825         }
826
827 #if NFS_MOUNT_VERSION >= 2
828         if (nfs_pmap->pm_prot == IPPROTO_TCP)
829                 data.flags |= NFS_MOUNT_TCP;
830         else
831                 data.flags &= ~NFS_MOUNT_TCP;
832 #endif
833
834         /* prepare data structure for kernel */
835
836         data.fd = fsock;
837         memcpy((char *) &data.addr, (char *) nfs_saddr, sizeof(data.addr));
838         strncpy(data.hostname, hostname, sizeof(data.hostname));
839
840  out_ok:
841         /* Ensure we have enough padding for the following strcat()s */
842         if (strlen(new_opts) + strlen(s) + 30 >= sizeof(new_opts)) {
843                 nfs_error(_("%s: excessively long option argument"),
844                                 progname);
845                 goto fail;
846         }
847
848         snprintf(cbuf, sizeof(cbuf)-1, "addr=%s", s);
849         strcat(new_opts, cbuf);
850
851         *extra_opts = xstrdup(new_opts);
852
853         if (!fake && !(data.flags & NFS_MOUNT_NONLM)) {
854                 if (!start_statd()) {
855                         nfs_error(_("%s: rpc.statd is not running but is "
856                                 "required for remote locking.\n"
857                                 "   Either use '-o nolock' to keep "
858                                 "locks local, or start statd."),
859                                         progname);
860                         goto fail;
861                 }
862         }
863
864         if (!fake) {
865                 if (mount(spec, node, "nfs",
866                                 flags & ~(MS_USER|MS_USERS), &data)) {
867                         mount_error(spec, node, errno);
868                         goto fail;
869                 }
870         }
871
872         return EX_SUCCESS;
873
874         /* abort */
875  fail:
876         if (fsock != -1)
877                 close(fsock);
878         return retval;
879 }