]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - utils/mount/stropts.c
text-based mount.nfs: Create helpers for invoking mount(2) system call
[nfs-utils.git] / utils / mount / stropts.c
index 8859bf584bb44eb0a504f0bfd68d3ab460a93bc7..c048be02713e1f634eee2e9b5b3597e2b2148bd9 100644 (file)
@@ -238,132 +238,77 @@ static int set_mandatory_options(const char *type,
 }
 
 /*
- * nfsmount_s - Mount an NFSv2 or v3 file system using C string options
+ * Attempt an NFSv2/3 mount via a mount(2) system call.
  *
- * @spec:      C string hostname:path specifying remoteshare to mount
- * @node:      C string pathname of local mounted on directory
- * @flags:     MS_ style flags
- * @extra_opts:        pointer to C string containing fs-specific mount options
- *             (possibly also a return argument)
- * @fake:      flag indicating whether to carry out the whole operation
- * @child:     one if this is a backgrounded mount
- *
- * XXX: need to handle bg, fg, and retry options.
+ * Returns 1 if successful.  Otherwise, returns zero.
+ * "errno" is set to reflect the individual error.
  */
-int nfsmount_s(const char *spec, const char *node, int flags,
-               char **extra_opts, int fake, int child)
+static int try_nfs23mount(const char *spec, const char *node,
+                         int flags, struct mount_options *options,
+                         int fake, char **extra_opts)
 {
-       struct mount_options *options = NULL;
-       struct sockaddr_in saddr;
-       char *hostname;
-       int err, retval = EX_FAIL;
-
-       if (!parse_devname(spec, &hostname))
-               goto out;
-       err = fill_ipv4_sockaddr(hostname, &saddr);
-       free(hostname);
-       if (!err)
-               goto out;
-
-       options = po_split(*extra_opts);
-       if (!options) {
-               nfs_error(_("%s: internal option parsing error"), progname);
-               goto out;
-       }
-
-       if (!append_addr_option(&saddr, options))
-               goto out;
-
-       if (!fix_mounthost_option(options))
-               goto out;
-
        if (po_join(options, extra_opts) == PO_FAILED) {
-               nfs_error(_("%s: internal option parsing error"), progname);
-               goto out;
+               errno = EIO;
+               return 0;
        }
 
        if (verbose)
                printf(_("%s: text-based options: '%s'\n"),
                        progname, *extra_opts);
 
-       if (!fake) {
-               if (mount(spec, node, "nfs",
-                               flags & ~(MS_USER|MS_USERS), *extra_opts)) {
-                       mount_error(spec, node, errno);
-                       goto out;
-               }
-       }
-
-       retval = EX_SUCCESS;
+       if (fake)
+               return 1;
 
-out:
-       po_destroy(options);
-       return retval;
+       if (!mount(spec, node, "nfs",
+                               flags & ~(MS_USER|MS_USERS), *extra_opts))
+               return 1;
+       return 0;
 }
 
 /*
- * nfs4mount_s - Mount an NFSv4 file system using C string options
- *
- * @spec:      C string hostname:path specifying remoteshare to mount
- * @node:      C string pathname of local mounted on directory
- * @flags:     MS_ style flags
- * @extra_opts:        pointer to C string containing fs-specific mount options
- *             (possibly also a return argument)
- * @fake:      flag indicating whether to carry out the whole operation
- * @child:     one if this is a backgrounded mount
- *
- * XXX: need to handle bg, fg, and retry options.
+ * Attempt an NFS v4 mount via a mount(2) system call.
  *
+ * Returns 1 if successful.  Otherwise, returns zero.
+ * "errno" is set to reflect the individual error.
  */
-int nfs4mount_s(const char *spec, const char *node, int flags,
-               char **extra_opts, int fake, int child)
+static int try_nfs4mount(const char *spec, const char *node,
+                        int flags, struct mount_options *options,
+                        int fake, char **extra_opts)
 {
-       struct mount_options *options = NULL;
-       struct sockaddr_in saddr;
-       char *hostname;
-       int err, retval = EX_FAIL;
-
-       if (!parse_devname(spec, &hostname))
-               goto out;
-       err = fill_ipv4_sockaddr(hostname, &saddr);
-       free(hostname);
-       if (!err)
-               goto out;
-
-       options = po_split(*extra_opts);
-       if (!options) {
-               nfs_error(_("%s: internal option parsing error"), progname);
-               goto out;
-       }
-
-       if (!append_addr_option(&saddr, options))
-               goto out;
-
-       if (!append_clientaddr_option(&saddr, options))
-               goto out;
-
        if (po_join(options, extra_opts) == PO_FAILED) {
-               nfs_error(_("%s: internal option parsing error"), progname);
-               goto out;
+               errno = EIO;
+               return 0;
        }
 
        if (verbose)
                printf(_("%s: text-based options: '%s'\n"),
                        progname, *extra_opts);
 
-       if (!fake) {
-               if (mount(spec, node, "nfs4",
-                               flags & ~(MS_USER|MS_USERS), *extra_opts)) {
-                       mount_error(spec, node, errno);
-                       goto out;
-               }
-       }
+       if (fake)
+               return 1;
 
-       retval = EX_SUCCESS;
+       if (!mount(spec, node, "nfs4",
+                               flags & ~(MS_USER|MS_USERS), *extra_opts))
+               return 1;
+       return 0;
+}
 
-out:
-       po_destroy(options);
-       return retval;
+/*
+ * Try the mount(2) system call.
+ *
+ * Returns 1 if successful.  Otherwise, returns zero.
+ * "errno" is set to reflect the individual error.
+ */
+static int try_mount(const char *spec, const char *node, const char *type,
+                    int flags, struct mount_options *options, int fake,
+                    char **extra_opts)
+{
+       if (strncmp(type, "nfs4", 4) == 0)
+               return try_nfs4mount(spec, node, flags,
+                                       options, fake, extra_opts);
+       else
+               return try_nfs23mount(spec, node, flags,
+                                       options, fake, extra_opts);
 }
 
 /**