Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nfs-utils / utils / mount / nfsmount.c
blob6355681d4b520eeb3bdeacb26ed81cf797ea33cc
1 /*
2 * nfsmount.c -- Linux NFS mount
3 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
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.
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.
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.
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.
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.
24 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
25 * - added Native Language Support
27 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
28 * plus NFSv3 stuff.
30 * 2006-06-06 Amit Gud <agud@redhat.com>
31 * - Moved with modifcations to nfs-utils/utils/mount from util-linux/mount.
35 * nfsmount.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
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>
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"
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
79 #ifndef NFS_PORT
80 #define NFS_PORT 2049
81 #endif
82 #ifndef NFS_FHSIZE
83 #define NFS_FHSIZE 32
84 #endif
86 #ifndef HAVE_INET_ATON
87 #define inet_aton(a,b) (0)
88 #endif
90 typedef dirpath mnt2arg_t;
91 typedef dirpath mnt3arg_t;
92 typedef dirpath mntarg_t;
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;
101 extern int nfs_mount_data_version;
102 extern char *progname;
103 extern int verbose;
104 extern int sloppy;
106 static inline enum clnt_stat
107 nfs3_mount(CLIENT *clnt, mnt3arg_t *mnt3arg, mnt3res_t *mnt3res)
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);
115 static inline enum clnt_stat
116 nfs2_mount(CLIENT *clnt, mnt2arg_t *mnt2arg, mnt2res_t *mnt2res)
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);
124 static int
125 nfs_call_mount(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server,
126 mntarg_t *mntarg, mntres_t *mntres)
128 CLIENT *clnt;
129 enum clnt_stat stat;
130 int msock;
132 if (!probe_bothports(mnt_server, nfs_server))
133 goto out_bad;
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
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;
153 if (stat != RPC_SUCCESS) {
154 clnt_geterr(clnt, &rpc_createerr.cf_error);
155 rpc_createerr.cf_stat = stat;
157 mnt_closeclnt(clnt, msock);
158 if (stat == RPC_SUCCESS)
159 return 1;
160 out_bad:
161 return 0;
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)
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;
174 char *mounthost = NULL;
175 char cbuf[128];
176 int open_quote = 0;
178 data->flags = 0;
179 *bg = 0;
181 len = strlen(new_opts);
182 for (p=old_opts, opt_b=NULL; p && *p; p++) {
183 if (!opt_b)
184 opt_b = p; /* begin of the option item */
185 if (*p == '"')
186 open_quote ^= 1; /* reverse the status */
187 if (open_quote)
188 continue; /* still in a quoted block */
189 if (*p == ',')
190 *p = '\0'; /* terminate the option item */
191 if (*p == '\0' || *(p+1) == '\0') {
192 opt = opt_b; /* opt is useful now */
193 opt_b = NULL;
195 else
196 continue; /* still somewhere in the option item */
198 if (strlen(opt) >= sizeof(cbuf))
199 goto bad_parameter;
200 if ((opteq = strchr(opt, '=')) && isdigit(opteq[1])) {
201 int val = atoi(opteq + 1);
202 *opteq = '\0';
203 if (!strcmp(opt, "rsize"))
204 data->rsize = val;
205 else if (!strcmp(opt, "wsize"))
206 data->wsize = val;
207 else if (!strcmp(opt, "timeo"))
208 data->timeo = val;
209 else if (!strcmp(opt, "retrans"))
210 data->retrans = val;
211 else if (!strcmp(opt, "acregmin"))
212 data->acregmin = val;
213 else if (!strcmp(opt, "acregmax"))
214 data->acregmax = val;
215 else if (!strcmp(opt, "acdirmin"))
216 data->acdirmin = val;
217 else if (!strcmp(opt, "acdirmax"))
218 data->acdirmax = val;
219 else if (!strcmp(opt, "actimeo")) {
220 data->acregmin = val;
221 data->acregmax = val;
222 data->acdirmin = val;
223 data->acdirmax = val;
225 else if (!strcmp(opt, "retry"))
226 *retry = val;
227 else if (!strcmp(opt, "port"))
228 nfs_pmap->pm_port = val;
229 else if (!strcmp(opt, "mountport"))
230 mnt_pmap->pm_port = val;
231 else if (!strcmp(opt, "mountprog"))
232 mnt_pmap->pm_prog = val;
233 else if (!strcmp(opt, "mountvers"))
234 mnt_pmap->pm_vers = val;
235 else if (!strcmp(opt, "mounthost"))
236 mounthost=xstrndup(opteq+1, strcspn(opteq+1," \t\n\r,"));
237 else if (!strcmp(opt, "nfsprog"))
238 nfs_pmap->pm_prog = val;
239 else if (!strcmp(opt, "nfsvers") ||
240 !strcmp(opt, "vers")) {
241 nfs_pmap->pm_vers = val;
242 opt = "nfsvers";
243 #if NFS_MOUNT_VERSION >= 2
244 } else if (!strcmp(opt, "namlen")) {
245 if (nfs_mount_data_version >= 2)
246 data->namlen = val;
247 else if (sloppy)
248 continue;
249 else
250 goto bad_parameter;
251 #endif
252 } else if (!strcmp(opt, "addr")) {
253 /* ignore */;
254 continue;
255 } else if (sloppy)
256 continue;
257 else
258 goto bad_parameter;
259 sprintf(cbuf, "%s=%s,", opt, opteq+1);
260 } else if (opteq) {
261 *opteq = '\0';
262 if (!strcmp(opt, "proto")) {
263 if (!strcmp(opteq+1, "udp")) {
264 nfs_pmap->pm_prot = IPPROTO_UDP;
265 mnt_pmap->pm_prot = IPPROTO_UDP;
266 #if NFS_MOUNT_VERSION >= 2
267 data->flags &= ~NFS_MOUNT_TCP;
268 } else if (!strcmp(opteq+1, "tcp") &&
269 nfs_mount_data_version > 2) {
270 nfs_pmap->pm_prot = IPPROTO_TCP;
271 mnt_pmap->pm_prot = IPPROTO_TCP;
272 data->flags |= NFS_MOUNT_TCP;
273 #endif
274 } else if (sloppy)
275 continue;
276 else
277 goto bad_parameter;
278 #if NFS_MOUNT_VERSION >= 5
279 } else if (!strcmp(opt, "sec")) {
280 char *secflavor = opteq+1;
281 /* see RFC 2623 */
282 if (nfs_mount_data_version < 5) {
283 printf(_("Warning: ignoring sec=%s option\n"),
284 secflavor);
285 continue;
286 } else if (!strcmp(secflavor, "none"))
287 data->pseudoflavor = AUTH_NONE;
288 else if (!strcmp(secflavor, "sys"))
289 data->pseudoflavor = AUTH_SYS;
290 else if (!strcmp(secflavor, "krb5"))
291 data->pseudoflavor = AUTH_GSS_KRB5;
292 else if (!strcmp(secflavor, "krb5i"))
293 data->pseudoflavor = AUTH_GSS_KRB5I;
294 else if (!strcmp(secflavor, "krb5p"))
295 data->pseudoflavor = AUTH_GSS_KRB5P;
296 else if (!strcmp(secflavor, "lipkey"))
297 data->pseudoflavor = AUTH_GSS_LKEY;
298 else if (!strcmp(secflavor, "lipkey-i"))
299 data->pseudoflavor = AUTH_GSS_LKEYI;
300 else if (!strcmp(secflavor, "lipkey-p"))
301 data->pseudoflavor = AUTH_GSS_LKEYP;
302 else if (!strcmp(secflavor, "spkm3"))
303 data->pseudoflavor = AUTH_GSS_SPKM;
304 else if (!strcmp(secflavor, "spkm3i"))
305 data->pseudoflavor = AUTH_GSS_SPKMI;
306 else if (!strcmp(secflavor, "spkm3p"))
307 data->pseudoflavor = AUTH_GSS_SPKMP;
308 else if (sloppy)
309 continue;
310 else {
311 printf(_("Warning: Unrecognized security flavor %s.\n"),
312 secflavor);
313 goto bad_parameter;
315 data->flags |= NFS_MOUNT_SECFLAVOUR;
316 #endif
317 } else if (!strcmp(opt, "mounthost"))
318 mounthost=xstrndup(opteq+1,
319 strcspn(opteq+1," \t\n\r,"));
320 else if (!strcmp(opt, "context")) {
321 char *context = opteq + 1;
322 int ctxlen = strlen(context);
324 if (ctxlen > NFS_MAX_CONTEXT_LEN) {
325 nfs_error(_("context parameter exceeds"
326 " limit of %d"),
327 NFS_MAX_CONTEXT_LEN);
328 goto bad_parameter;
330 /* The context string is in the format of
331 * "system_u:object_r:...". We only want
332 * the context str between the quotes.
334 if (*context == '"')
335 strncpy(data->context, context+1,
336 ctxlen-2);
337 else
338 strncpy(data->context, context,
339 NFS_MAX_CONTEXT_LEN);
340 } else if (sloppy)
341 continue;
342 else
343 goto bad_parameter;
344 sprintf(cbuf, "%s=%s,", opt, opteq+1);
345 } else {
346 int val = 1;
347 if (!strncmp(opt, "no", 2)) {
348 val = 0;
349 opt += 2;
351 if (!strcmp(opt, "bg"))
352 *bg = val;
353 else if (!strcmp(opt, "fg"))
354 *bg = !val;
355 else if (!strcmp(opt, "soft")) {
356 data->flags &= ~NFS_MOUNT_SOFT;
357 if (val)
358 data->flags |= NFS_MOUNT_SOFT;
359 } else if (!strcmp(opt, "hard")) {
360 data->flags &= ~NFS_MOUNT_SOFT;
361 if (!val)
362 data->flags |= NFS_MOUNT_SOFT;
363 } else if (!strcmp(opt, "intr")) {
364 data->flags &= ~NFS_MOUNT_INTR;
365 if (val)
366 data->flags |= NFS_MOUNT_INTR;
367 } else if (!strcmp(opt, "posix")) {
368 data->flags &= ~NFS_MOUNT_POSIX;
369 if (val)
370 data->flags |= NFS_MOUNT_POSIX;
371 } else if (!strcmp(opt, "cto")) {
372 data->flags &= ~NFS_MOUNT_NOCTO;
373 if (!val)
374 data->flags |= NFS_MOUNT_NOCTO;
375 } else if (!strcmp(opt, "ac")) {
376 data->flags &= ~NFS_MOUNT_NOAC;
377 if (!val)
378 data->flags |= NFS_MOUNT_NOAC;
379 #if NFS_MOUNT_VERSION >= 2
380 } else if (!strcmp(opt, "tcp")) {
381 data->flags &= ~NFS_MOUNT_TCP;
382 if (val) {
383 if (nfs_mount_data_version < 2)
384 goto bad_option;
385 nfs_pmap->pm_prot = IPPROTO_TCP;
386 mnt_pmap->pm_prot = IPPROTO_TCP;
387 data->flags |= NFS_MOUNT_TCP;
388 } else {
389 mnt_pmap->pm_prot = IPPROTO_UDP;
390 nfs_pmap->pm_prot = IPPROTO_UDP;
392 } else if (!strcmp(opt, "udp")) {
393 data->flags &= ~NFS_MOUNT_TCP;
394 if (!val) {
395 if (nfs_mount_data_version < 2)
396 goto bad_option;
397 nfs_pmap->pm_prot = IPPROTO_TCP;
398 mnt_pmap->pm_prot = IPPROTO_TCP;
399 data->flags |= NFS_MOUNT_TCP;
400 } else {
401 nfs_pmap->pm_prot = IPPROTO_UDP;
402 mnt_pmap->pm_prot = IPPROTO_UDP;
404 #endif
405 #if NFS_MOUNT_VERSION >= 3
406 } else if (!strcmp(opt, "lock")) {
407 data->flags &= ~NFS_MOUNT_NONLM;
408 if (!val) {
409 if (nfs_mount_data_version < 3)
410 goto bad_option;
411 data->flags |= NFS_MOUNT_NONLM;
413 #endif
414 #if NFS_MOUNT_VERSION >= 4
415 } else if (!strcmp(opt, "broken_suid")) {
416 data->flags &= ~NFS_MOUNT_BROKEN_SUID;
417 if (val) {
418 if (nfs_mount_data_version < 4)
419 goto bad_option;
420 data->flags |= NFS_MOUNT_BROKEN_SUID;
422 } else if (!strcmp(opt, "acl")) {
423 data->flags &= ~NFS_MOUNT_NOACL;
424 if (!val)
425 data->flags |= NFS_MOUNT_NOACL;
426 } else if (!strcmp(opt, "rdirplus")) {
427 data->flags &= ~NFS_MOUNT_NORDIRPLUS;
428 if (!val)
429 data->flags |= NFS_MOUNT_NORDIRPLUS;
430 } else if (!strcmp(opt, "sharecache")) {
431 data->flags &= ~NFS_MOUNT_UNSHARED;
432 if (!val)
433 data->flags |= NFS_MOUNT_UNSHARED;
434 #endif
435 } else {
436 bad_option:
437 if (sloppy)
438 continue;
439 nfs_error(_("%s: Unsupported nfs mount option:"
440 " %s%s"), progname,
441 val ? "" : "no", opt);
442 goto out_bad;
444 sprintf(cbuf, val ? "%s," : "no%s,", opt);
446 len += strlen(cbuf);
447 if (len >= opt_size) {
448 nfs_error(_("%s: excessively long option argument"),
449 progname);
450 goto out_bad;
452 strcat(new_opts, cbuf);
454 /* See if the nfs host = mount host. */
455 if (mounthost) {
456 if (!nfs_gethostbyname(mounthost, mnt_saddr))
457 goto out_bad;
458 *mnt_server->hostname = mounthost;
460 return 1;
461 bad_parameter:
462 nfs_error(_("%s: Bad nfs mount parameter: %s\n"), progname, opt);
463 out_bad:
464 return 0;
467 static int nfsmnt_check_compat(const struct pmap *nfs_pmap,
468 const struct pmap *mnt_pmap)
470 unsigned int max_nfs_vers = (nfs_mount_data_version >= 4) ? 3 : 2;
471 unsigned int max_mnt_vers = (nfs_mount_data_version >= 4) ? 3 : 2;
473 if (nfs_pmap->pm_vers == 4) {
474 nfs_error(_("%s: Please use '-t nfs4' "
475 "instead of '-o vers=4'"), progname);
476 goto out_bad;
479 if (nfs_pmap->pm_vers) {
480 if (nfs_pmap->pm_vers > max_nfs_vers || nfs_pmap->pm_vers < 2) {
481 nfs_error(_("%s: NFS version %ld is not supported"),
482 progname, nfs_pmap->pm_vers);
483 goto out_bad;
487 if (mnt_pmap->pm_vers > max_mnt_vers) {
488 nfs_error(_("%s: NFS mount version %ld is not supported"),
489 progname, mnt_pmap->pm_vers);
490 goto out_bad;
493 return 1;
495 out_bad:
496 return 0;
500 nfsmount(const char *spec, const char *node, int flags,
501 char **extra_opts, int fake, int running_bg)
503 char hostdir[1024];
504 char *hostname, *dirname, *old_opts, *mounthost = NULL;
505 char new_opts[1024], cbuf[1024];
506 static struct nfs_mount_data data;
507 int val;
508 static int doonce = 0;
510 clnt_addr_t mnt_server = { &mounthost, };
511 clnt_addr_t nfs_server = { &hostname, };
512 struct sockaddr_in *nfs_saddr = &nfs_server.saddr;
513 struct pmap *mnt_pmap = &mnt_server.pmap,
514 *nfs_pmap = &nfs_server.pmap;
515 struct pmap save_mnt, save_nfs;
517 int fsock = -1;
519 mntres_t mntres;
521 struct stat statbuf;
522 char *s;
523 int bg, retry;
524 int retval = EX_FAIL;
525 time_t t;
526 time_t prevt;
527 time_t timeout;
529 if (strlen(spec) >= sizeof(hostdir)) {
530 nfs_error(_("%s: excessively long host:dir argument"),
531 progname);
532 goto fail;
534 strcpy(hostdir, spec);
535 if ((s = strchr(hostdir, ':'))) {
536 hostname = hostdir;
537 dirname = s + 1;
538 *s = '\0';
539 /* Ignore all but first hostname in replicated mounts
540 until they can be fully supported. (mack@sgi.com) */
541 if ((s = strchr(hostdir, ','))) {
542 *s = '\0';
543 nfs_error(_("%s: warning: "
544 "multiple hostnames not supported"),
545 progname);
547 } else {
548 nfs_error(_("%s: directory to mount not in host:dir format"),
549 progname);
550 goto fail;
553 if (!nfs_gethostbyname(hostname, nfs_saddr))
554 goto fail;
555 mounthost = hostname;
556 memcpy (&mnt_server.saddr, nfs_saddr, sizeof (mnt_server.saddr));
558 /* add IP address to mtab options for use when unmounting */
560 s = inet_ntoa(nfs_saddr->sin_addr);
561 old_opts = *extra_opts;
562 if (!old_opts)
563 old_opts = "";
565 /* Set default options.
566 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
567 * let the kernel decide.
568 * timeo is filled in after we know whether it'll be TCP or UDP. */
569 memset(&data, 0, sizeof(data));
570 data.acregmin = 3;
571 data.acregmax = 60;
572 data.acdirmin = 30;
573 data.acdirmax = 60;
574 #if NFS_MOUNT_VERSION >= 2
575 data.namlen = NAME_MAX;
576 #endif
578 bg = 0;
579 retry = -1;
581 memset(mnt_pmap, 0, sizeof(*mnt_pmap));
582 mnt_pmap->pm_prog = MOUNTPROG;
583 memset(nfs_pmap, 0, sizeof(*nfs_pmap));
584 nfs_pmap->pm_prog = NFS_PROGRAM;
586 /* parse options */
587 new_opts[0] = 0;
588 if (!parse_options(old_opts, &data, &bg, &retry, &mnt_server, &nfs_server,
589 new_opts, sizeof(new_opts)))
590 goto fail;
591 if (!nfsmnt_check_compat(nfs_pmap, mnt_pmap))
592 goto fail;
594 if (retry == -1) {
595 if (bg)
596 retry = 10000; /* 10000 mins == ~1 week*/
597 else
598 retry = 2; /* 2 min default on fg mounts */
601 #ifdef NFS_MOUNT_DEBUG
602 printf(_("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n"),
603 data.rsize, data.wsize, data.timeo, data.retrans);
604 printf(_("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n"),
605 data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
606 printf(_("port = %lu, bg = %d, retry = %d, flags = %.8x\n"),
607 nfs_pmap->pm_port, bg, retry, data.flags);
608 printf(_("mountprog = %lu, mountvers = %lu, nfsprog = %lu, nfsvers = %lu\n"),
609 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
610 nfs_pmap->pm_prog, nfs_pmap->pm_vers);
611 printf(_("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d"),
612 (data.flags & NFS_MOUNT_SOFT) != 0,
613 (data.flags & NFS_MOUNT_INTR) != 0,
614 (data.flags & NFS_MOUNT_POSIX) != 0,
615 (data.flags & NFS_MOUNT_NOCTO) != 0,
616 (data.flags & NFS_MOUNT_NOAC) != 0);
617 #if NFS_MOUNT_VERSION >= 2
618 printf(_(", tcp = %d"),
619 (data.flags & NFS_MOUNT_TCP) != 0);
620 #endif
621 #if NFS_MOUNT_VERSION >= 4
622 printf(_(", noacl = %d"), (data.flags & NFS_MOUNT_NOACL) != 0);
623 #endif
624 #if NFS_MOUNT_VERSION >= 5
625 printf(_(", sec = %u"), data.pseudoflavor);
626 printf(_(", readdirplus = %d"), (data.flags & NFS_MOUNT_NORDIRPLUS) != 0);
627 #endif
628 printf("\n");
629 #endif
631 data.version = nfs_mount_data_version;
633 if (flags & MS_REMOUNT)
634 goto out_ok;
636 /* create mount deamon client */
639 * The following loop implements the mount retries. On the first
640 * call, "running_bg" is 0. When the mount times out, and the
641 * "bg" option is set, the exit status EX_BG will be returned.
642 * For a backgrounded mount, there will be a second call by the
643 * child process with "running_bg" set to 1.
645 * The case where the mount point is not present and the "bg"
646 * option is set, is treated as a timeout. This is done to
647 * support nested mounts.
649 * The "retry" count specified by the user is the number of
650 * minutes to retry before giving up.
652 * Only the first error message will be displayed.
654 timeout = time(NULL) + 60 * retry;
655 prevt = 0;
656 t = 30;
657 val = 1;
659 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
660 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
661 for (;;) {
662 if (bg && stat(node, &statbuf) == -1) {
663 /* no mount point yet - sleep */
664 if (running_bg) {
665 sleep(val); /* 1, 2, 4, 8, 16, 30, ... */
666 val *= 2;
667 if (val > 30)
668 val = 30;
670 } else {
671 int stat;
672 /* be careful not to use too many CPU cycles */
673 if (t - prevt < 30)
674 sleep(30);
676 stat = nfs_call_mount(&mnt_server, &nfs_server,
677 &dirname, &mntres);
678 if (stat)
679 break;
680 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
681 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
682 prevt = t;
684 if (!bg) {
685 switch(rpc_createerr.cf_stat){
686 case RPC_TIMEDOUT:
687 break;
688 case RPC_SYSTEMERROR:
689 if (errno == ETIMEDOUT)
690 break;
691 default:
692 rpc_mount_errors(*nfs_server.hostname, 0, bg);
693 goto fail;
695 t = time(NULL);
696 if (t >= timeout) {
697 rpc_mount_errors(*nfs_server.hostname, 0, bg);
698 goto fail;
700 rpc_mount_errors(*nfs_server.hostname, 1, bg);
701 continue;
703 if (!running_bg) {
704 if (retry > 0)
705 retval = EX_BG;
706 goto fail;
708 t = time(NULL);
709 if (t >= timeout) {
710 rpc_mount_errors(*nfs_server.hostname, 0, bg);
711 goto fail;
713 if (doonce++ < 1)
714 rpc_mount_errors(*nfs_server.hostname, 1, bg);
717 if (mnt_pmap->pm_vers <= 2) {
718 if (mntres.nfsv2.fhs_status != 0) {
719 nfs_error(_("%s: %s:%s failed, reason given by server: %s"),
720 progname, hostname, dirname,
721 nfs_strerror(mntres.nfsv2.fhs_status));
722 goto fail;
724 memcpy(data.root.data,
725 (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
726 NFS_FHSIZE);
727 #if NFS_MOUNT_VERSION >= 4
728 data.root.size = NFS_FHSIZE;
729 memcpy(data.old_root.data,
730 (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
731 NFS_FHSIZE);
732 #endif
733 } else {
734 #if NFS_MOUNT_VERSION >= 4
735 mountres3_ok *mountres;
736 fhandle3 *fhandle;
737 int i, n_flavors, *flavor, yum = 0;
738 if (mntres.nfsv3.fhs_status != 0) {
739 nfs_error(_("%s: %s:%s failed, reason given by server: %s"),
740 progname, hostname, dirname,
741 nfs_strerror(mntres.nfsv3.fhs_status));
742 goto fail;
744 #if NFS_MOUNT_VERSION >= 5
745 mountres = &mntres.nfsv3.mountres3_u.mountinfo;
746 n_flavors = mountres->auth_flavors.auth_flavors_len;
747 if (n_flavors <= 0)
748 goto noauth_flavors;
750 flavor = mountres->auth_flavors.auth_flavors_val;
751 for (i = 0; i < n_flavors; ++i) {
753 * Per RFC2623, section 2.7, we should prefer the
754 * flavour listed first.
755 * If no flavour requested, use the first simple
756 * flavour that is offered.
758 if (! (data.flags & NFS_MOUNT_SECFLAVOUR) &&
759 (flavor[i] == AUTH_SYS ||
760 flavor[i] == AUTH_NONE)) {
761 data.pseudoflavor = flavor[i];
762 data.flags |= NFS_MOUNT_SECFLAVOUR;
764 if (flavor[i] == data.pseudoflavor)
765 yum = 1;
766 #ifdef NFS_MOUNT_DEBUG
767 printf(_("auth flavor %d: %d\n"), i, flavor[i]);
768 #endif
770 if (!yum) {
771 nfs_error(_("%s: %s:%s failed, security flavor "
772 "not supported"),
773 progname, hostname, dirname);
774 /* server has registered us in rmtab, send umount */
775 nfs_call_umount(&mnt_server, &dirname);
776 goto fail;
778 noauth_flavors:
779 #endif
780 fhandle = &mntres.nfsv3.mountres3_u.mountinfo.fhandle;
781 memset(data.old_root.data, 0, NFS_FHSIZE);
782 memset(&data.root, 0, sizeof(data.root));
783 data.root.size = fhandle->fhandle3_len;
784 memcpy(data.root.data,
785 (char *) fhandle->fhandle3_val,
786 fhandle->fhandle3_len);
788 data.flags |= NFS_MOUNT_VER3;
789 #endif
792 if (nfs_mount_data_version == 1) {
793 /* create nfs socket for kernel */
794 if (nfs_pmap->pm_prot == IPPROTO_TCP)
795 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
796 else
797 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
798 if (fsock < 0) {
799 perror(_("nfs socket"));
800 goto fail;
802 if (bindresvport(fsock, 0) < 0) {
803 perror(_("nfs bindresvport"));
804 goto fail;
808 #ifdef NFS_MOUNT_DEBUG
809 printf(_("using port %lu for nfs deamon\n"), nfs_pmap->pm_port);
810 #endif
811 nfs_saddr->sin_port = htons(nfs_pmap->pm_port);
813 * connect() the socket for kernels 1.3.10 and below only,
814 * to avoid problems with multihomed hosts.
815 * --Swen
817 if (linux_version_code() <= MAKE_VERSION(1, 3, 10) && fsock != -1
818 && connect(fsock, (struct sockaddr *) nfs_saddr,
819 sizeof (*nfs_saddr)) < 0) {
820 perror(_("nfs connect"));
821 goto fail;
824 #if NFS_MOUNT_VERSION >= 2
825 if (nfs_pmap->pm_prot == IPPROTO_TCP)
826 data.flags |= NFS_MOUNT_TCP;
827 else
828 data.flags &= ~NFS_MOUNT_TCP;
829 #endif
831 /* prepare data structure for kernel */
833 data.fd = fsock;
834 memcpy((char *) &data.addr, (char *) nfs_saddr, sizeof(data.addr));
835 strncpy(data.hostname, hostname, sizeof(data.hostname));
837 out_ok:
838 /* Ensure we have enough padding for the following strcat()s */
839 if (strlen(new_opts) + strlen(s) + 30 >= sizeof(new_opts)) {
840 nfs_error(_("%s: excessively long option argument"),
841 progname);
842 goto fail;
845 snprintf(cbuf, sizeof(cbuf)-1, "addr=%s", s);
846 strcat(new_opts, cbuf);
848 *extra_opts = xstrdup(new_opts);
850 if (!fake && !(data.flags & NFS_MOUNT_NONLM)) {
851 if (!start_statd()) {
852 nfs_error(_("%s: rpc.statd is not running but is "
853 "required for remote locking.\n"
854 " Either use '-o nolock' to keep "
855 "locks local, or start statd."),
856 progname);
857 goto fail;
861 if (!fake) {
862 if (mount(spec, node, "nfs",
863 flags & ~(MS_USER|MS_USERS), &data)) {
864 mount_error(spec, node, errno);
865 goto fail;
869 return EX_SUCCESS;
871 /* abort */
872 fail:
873 if (fsock != -1)
874 close(fsock);
875 return retval;