]> git.decadent.org.uk Git - nfs-utils.git/commitdiff
mount: move generic functions to utils.c and network.c
authorKarel Zak <kzak@redhat.com>
Wed, 6 Apr 2011 15:36:40 +0000 (11:36 -0400)
committerSteve Dickson <steved@redhat.com>
Wed, 6 Apr 2011 16:40:20 +0000 (12:40 -0400)
Move generic code that could be shared between standard mount.nfs and
libmount version to utils.c and network.c.

CC: Chuck Lever <chuck.lever@oracle.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Steve Dickson <steved@redhat.com>
utils/mount/Makefile.am
utils/mount/mount.c
utils/mount/network.c
utils/mount/network.h
utils/mount/nfsumount.c
utils/mount/utils.c [new file with mode: 0644]
utils/mount/utils.h [new file with mode: 0644]

index 299384a3aef1b73eef63971f0baad6070ddfc16c..a9553dc76dea46e3a40f04ee3f693758547bcf1d 100644 (file)
@@ -16,7 +16,7 @@ mount_nfs_SOURCES = mount.c error.c network.c fstab.c token.c \
                    mount_constants.h error.h network.h fstab.h token.h \
                    parse_opt.h parse_dev.h \
                    nfs4_mount.h nfs_mount4.h stropts.h version.h \
-                       mount_config.h
+                   mount_config.h utils.c utils.h
 
 if MOUNT_CONFIG
 mount_nfs_SOURCES += configfile.c
index a19af53ede893d92a5efb7b53b65268dba1c5769..f3f0a8310164a02a860aa48b9ae13cfc2a1da6af 100644 (file)
@@ -47,7 +47,7 @@
 #include "mount.h"
 #include "error.h"
 #include "stropts.h"
-#include "version.h"
+#include "utils.h"
 
 char *progname;
 int nfs_mount_data_version;
@@ -149,49 +149,6 @@ static const struct opt_map opt_map[] = {
 
 static void parse_opts(const char *options, int *flags, char **extra_opts);
 
-/*
- * Choose the version of the nfs_mount_data structure that is appropriate
- * for the kernel that is doing the mount.
- *
- * NFS_MOUNT_VERSION:          maximum version supported by these sources
- * nfs_mount_data_version:     maximum version supported by the running kernel
- */
-static void discover_nfs_mount_data_version(void)
-{
-       unsigned int kernel_version = linux_version_code();
-
-       if (kernel_version) {
-               if (kernel_version < MAKE_VERSION(2, 1, 32))
-                       nfs_mount_data_version = 1;
-               else if (kernel_version < MAKE_VERSION(2, 2, 18))
-                       nfs_mount_data_version = 3;
-               else if (kernel_version < MAKE_VERSION(2, 3, 0))
-                       nfs_mount_data_version = 4;
-               else if (kernel_version < MAKE_VERSION(2, 3, 99))
-                       nfs_mount_data_version = 3;
-               else if (kernel_version < MAKE_VERSION(2, 6, 3))
-                       nfs_mount_data_version = 4;
-               else
-                       nfs_mount_data_version = 6;
-       }
-       if (nfs_mount_data_version > NFS_MOUNT_VERSION)
-               nfs_mount_data_version = NFS_MOUNT_VERSION;
-       else
-               if (kernel_version > MAKE_VERSION(2, 6, 22))
-                       string++;
-}
-
-static void print_one(char *spec, char *node, char *type, char *opts)
-{
-       if (!verbose)
-               return;
-
-       if (opts)
-               printf(_("%s on %s type %s (%s)\n"), spec, node, type, opts);
-       else
-               printf(_("%s on %s type %s\n"), spec, node, type);
-}
-
 /*
  * Build a canonical mount option string for /etc/mtab.
  */
@@ -327,22 +284,6 @@ static int add_mtab(char *spec, char *mount_point, char *fstype,
        return result;
 }
 
-static void mount_usage(void)
-{
-       printf(_("usage: %s remotetarget dir [-rvVwfnsih] [-o nfsoptions]\n"),
-               progname);
-       printf(_("options:\n"));
-       printf(_("\t-r\t\tMount file system readonly\n"));
-       printf(_("\t-v\t\tVerbose\n"));
-       printf(_("\t-V\t\tPrint version\n"));
-       printf(_("\t-w\t\tMount file system read-write\n"));
-       printf(_("\t-f\t\tFake mount, do not actually mount\n"));
-       printf(_("\t-n\t\tDo not update /etc/mtab\n"));
-       printf(_("\t-s\t\tTolerate sloppy mount options rather than fail\n"));
-       printf(_("\t-h\t\tPrint this help\n"));
-       printf(_("\tnfsoptions\tRefer to mount.nfs(8) or nfs(5)\n\n"));
-}
-
 static void parse_opt(const char *opt, int *mask, char *extra_opts, size_t len)
 {
        const struct opt_map *om;
@@ -403,26 +344,6 @@ static void parse_opts(const char *options, int *flags, char **extra_opts)
        }
 }
 
-static int chk_mountpoint(char *mount_point)
-{
-       struct stat sb;
-
-       if (stat(mount_point, &sb) < 0){
-               mount_error(NULL, mount_point, errno);
-               return 1;
-       }
-       if (S_ISDIR(sb.st_mode) == 0){
-               mount_error(NULL, mount_point, ENOTDIR);
-               return 1;
-       }
-       if (access(mount_point, X_OK) < 0) {
-               mount_error(NULL, mount_point, errno);
-               return 1;
-       }
-
-       return 0;
-}
-
 static int try_mount(char *spec, char *mount_point, int flags,
                        char *fs_type, char **extra_opts, char *mount_opts,
                        int fake, int bg)
@@ -459,7 +380,7 @@ int main(int argc, char *argv[])
 
        progname = basename(argv[0]);
 
-       discover_nfs_mount_data_version();
+       nfs_mount_data_version = discover_nfs_mount_data_version(&string);
 
        if(!strncmp(progname, "umount", strlen("umount")))
                exit(nfsumount(argc, argv));
index 52a538940ad2f33d1115bec1f6c34a021ee50fe8..d1f91dc0b32c9c501d9253cbe6a576363f7e00f0 100644 (file)
@@ -1624,3 +1624,71 @@ int nfs_options2pmap(struct mount_options *options,
 
        return 1;
 }
+
+/*
+ * Discover mount server's hostname/address by examining mount options
+ *
+ * Returns a pointer to a string that the caller must free, on
+ * success; otherwise NULL is returned.
+ */
+static char *nfs_umount_hostname(struct mount_options *options,
+                                char *hostname)
+{
+       char *option;
+
+       option = po_get(options, "mountaddr");
+       if (option)
+               goto out;
+       option = po_get(options, "mounthost");
+       if (option)
+               goto out;
+       option = po_get(options, "addr");
+       if (option)
+               goto out;
+
+       return hostname;
+
+out:
+       free(hostname);
+       return strdup(option);
+}
+
+
+/*
+ * Returns EX_SUCCESS if mount options and device name have been
+ * parsed successfully; otherwise EX_FAIL.
+ */
+int nfs_umount_do_umnt(struct mount_options *options,
+                      char **hostname, char **dirname)
+{
+       union nfs_sockaddr address;
+       struct sockaddr *sap = &address.sa;
+       socklen_t salen = sizeof(address);
+       struct pmap nfs_pmap, mnt_pmap;
+       sa_family_t family;
+
+       if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap))
+               return EX_FAIL;
+
+       /* Skip UMNT call for vers=4 mounts */
+       if (nfs_pmap.pm_vers == 4)
+               return EX_SUCCESS;
+
+       *hostname = nfs_umount_hostname(options, *hostname);
+       if (!*hostname) {
+               nfs_error(_("%s: out of memory"), progname);
+               return EX_FAIL;
+       }
+
+       if (!nfs_mount_proto_family(options, &family))
+               return 0;
+       if (!nfs_lookup(*hostname, family, sap, &salen))
+               /* nfs_lookup reports any errors */
+               return EX_FAIL;
+
+       if (nfs_advise_umount(sap, salen, &mnt_pmap, dirname) == 0)
+               /* nfs_advise_umount reports any errors */
+               return EX_FAIL;
+
+       return EX_SUCCESS;
+}
index 2a3a1105498df483adbd5722ae226e32763fb1dd..81c6f22590eba3e39f152deb79293bc5b705992e 100644 (file)
@@ -75,4 +75,7 @@ int nfs_advise_umount(const struct sockaddr *, const socklen_t,
 CLIENT *mnt_openclnt(clnt_addr_t *, int *);
 void mnt_closeclnt(CLIENT *, int);
 
+int nfs_umount_do_umnt(struct mount_options *options,
+                      char **hostname, char **dirname);
+
 #endif /* _NFS_UTILS_MOUNT_NETWORK_H */
index 02d40ff9d2bcb32457f1414b073cbec16cb4f0b9..8cd28528b1a01d4c4b86b3306189088b6a927d2a 100644 (file)
@@ -37,6 +37,7 @@
 #include "network.h"
 #include "parse_opt.h"
 #include "parse_dev.h"
+#include "utils.h"
 
 #define MOUNTSFILE     "/proc/mounts"
 #define LINELEN                (4096)
@@ -138,113 +139,6 @@ static int del_mtab(const char *spec, const char *node)
        return EX_FILEIO;
 }
 
-/*
- * Discover mount server's hostname/address by examining mount options
- *
- * Returns a pointer to a string that the caller must free, on
- * success; otherwise NULL is returned.
- */
-static char *nfs_umount_hostname(struct mount_options *options,
-                                char *hostname)
-{
-       char *option;
-
-       option = po_get(options, "mountaddr");
-       if (option)
-               goto out;
-       option = po_get(options, "mounthost");
-       if (option)
-               goto out;
-       option = po_get(options, "addr");
-       if (option)
-               goto out;
-
-       return hostname;
-
-out:
-       free(hostname);
-       return strdup(option);
-}
-
-/*
- * Returns EX_SUCCESS if mount options and device name have been
- * parsed successfully; otherwise EX_FAIL.
- */
-static int nfs_umount_do_umnt(struct mount_options *options,
-                             char **hostname, char **dirname)
-{
-       union {
-               struct sockaddr         sa;
-               struct sockaddr_in      s4;
-               struct sockaddr_in6     s6;
-       } address;
-       struct sockaddr *sap = &address.sa;
-       socklen_t salen = sizeof(address);
-       struct pmap nfs_pmap, mnt_pmap;
-       sa_family_t family;
-
-       if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap))
-               return EX_FAIL;
-
-       /* Skip UMNT call for vers=4 mounts */
-       if (nfs_pmap.pm_vers == 4)
-               return EX_SUCCESS;
-
-       *hostname = nfs_umount_hostname(options, *hostname);
-       if (!*hostname) {
-               nfs_error(_("%s: out of memory"), progname);
-               return EX_FAIL;
-       }
-
-       if (!nfs_mount_proto_family(options, &family))
-               return 0;
-       if (!nfs_lookup(*hostname, family, sap, &salen))
-               /* nfs_lookup reports any errors */
-               return EX_FAIL;
-
-       if (nfs_advise_umount(sap, salen, &mnt_pmap, dirname) == 0)
-               /* nfs_advise_umount reports any errors */
-               return EX_FAIL;
-
-       return EX_SUCCESS;
-}
-
-/*
- * Pick up certain mount options used during the original mount
- * from /etc/mtab.  The basics include the server's IP address and
- * the server pathname of the share to unregister.
- *
- * These options might also describe the mount port, mount protocol
- * version, and transport protocol used to punch through a firewall.
- * We will need this information to get through the firewall again
- * to do the umount.
- *
- * Note that option parsing failures won't necessarily cause the
- * umount request to fail.  Those values will be left zero in the
- * pmap tuple.  If the GETPORT call later fails to disambiguate them,
- * then we fail.
- */
-static int nfs_umount23(const char *devname, char *string)
-{
-       char *hostname, *dirname;
-       struct mount_options *options;
-       int result = EX_FAIL;
-
-       if (!nfs_parse_devname(devname, &hostname, &dirname))
-               return EX_USAGE;
-
-       options = po_split(string);
-       if (options) {
-               result = nfs_umount_do_umnt(options, &hostname, &dirname);
-               po_destroy(options);
-       } else
-               nfs_error(_("%s: option parsing error"), progname);
-
-       free(hostname);
-       free(dirname);
-       return result;
-}
-
 /*
  * Detect NFSv4 mounts.
  *
@@ -340,17 +234,6 @@ static struct option umount_longopts[] =
   { NULL, 0, 0, 0 }
 };
 
-static void umount_usage(void)
-{
-       printf(_("usage: %s dir [-fvnrlh]\n"), progname);
-       printf(_("options:\n\t-f\t\tforce unmount\n"));
-       printf(_("\t-v\tverbose\n"));
-       printf(_("\t-n\tDo not update /etc/mtab\n"));
-       printf(_("\t-r\tremount\n"));
-       printf(_("\t-l\tlazy unmount\n"));
-       printf(_("\t-h\tprint this help\n\n"));
-}
-
 int nfsumount(int argc, char *argv[])
 {
        int c, ret;
diff --git a/utils/mount/utils.c b/utils/mount/utils.c
new file mode 100644 (file)
index 0000000..298db39
--- /dev/null
@@ -0,0 +1,175 @@
+/*
+ * Copyright (C) 2010 Karel Zak <kzak@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "sockaddr.h"
+#include "nfs_mount.h"
+#include "nls.h"
+#include "xcommon.h"
+#include "version.h"
+#include "error.h"
+#include "utils.h"
+#include "mount.h"
+#include "network.h"
+#include "parse_dev.h"
+
+extern int verbose;
+extern char *progname;
+
+/*
+ * Choose the version of the nfs_mount_data structure that is appropriate
+ * for the kernel that is doing the mount.
+ *
+ * NFS_MOUNT_VERSION:          maximum version supported by these sources
+ * nfs_mount_data_version:     maximum version supported by the running kernel
+ */
+int discover_nfs_mount_data_version(int *string_ver)
+{
+       unsigned int kernel_version = linux_version_code();
+       int ver = 0;
+
+       *string_ver = 0;
+
+       if (kernel_version) {
+               if (kernel_version < MAKE_VERSION(2, 1, 32))
+                       ver = 1;
+               else if (kernel_version < MAKE_VERSION(2, 2, 18))
+                       ver = 3;
+               else if (kernel_version < MAKE_VERSION(2, 3, 0))
+                       ver = 4;
+               else if (kernel_version < MAKE_VERSION(2, 3, 99))
+                       ver = 3;
+               else if (kernel_version < MAKE_VERSION(2, 6, 3))
+                       ver = 4;
+               else
+                       ver = 6;
+       }
+       if (ver > NFS_MOUNT_VERSION)
+               ver = NFS_MOUNT_VERSION;
+       else
+               if (kernel_version > MAKE_VERSION(2, 6, 22))
+                       (*string_ver)++;
+
+       return ver;
+}
+
+void print_one(char *spec, char *node, char *type, char *opts)
+{
+       if (!verbose)
+               return;
+
+       if (opts)
+               printf(_("%s on %s type %s (%s)\n"), spec, node, type, opts);
+       else
+               printf(_("%s on %s type %s\n"), spec, node, type);
+}
+
+void mount_usage(void)
+{
+       printf(_("usage: %s remotetarget dir [-rvVwfnsih] [-o nfsoptions]\n"),
+               progname);
+       printf(_("options:\n"));
+       printf(_("\t-r\t\tMount file system readonly\n"));
+       printf(_("\t-v\t\tVerbose\n"));
+       printf(_("\t-V\t\tPrint version\n"));
+       printf(_("\t-w\t\tMount file system read-write\n"));
+       printf(_("\t-f\t\tFake mount, do not actually mount\n"));
+       printf(_("\t-n\t\tDo not update /etc/mtab\n"));
+       printf(_("\t-s\t\tTolerate sloppy mount options rather than fail\n"));
+       printf(_("\t-h\t\tPrint this help\n"));
+       printf(_("\tnfsoptions\tRefer to mount.nfs(8) or nfs(5)\n\n"));
+}
+
+void umount_usage(void)
+{
+       printf(_("usage: %s dir [-fvnrlh]\n"), progname);
+       printf(_("options:\n\t-f\t\tforce unmount\n"));
+       printf(_("\t-v\tverbose\n"));
+       printf(_("\t-n\tDo not update /etc/mtab\n"));
+       printf(_("\t-r\tremount\n"));
+       printf(_("\t-l\tlazy unmount\n"));
+       printf(_("\t-h\tprint this help\n\n"));
+}
+
+int chk_mountpoint(const char *mount_point)
+{
+       struct stat sb;
+
+       if (stat(mount_point, &sb) < 0){
+               mount_error(NULL, mount_point, errno);
+               return 1;
+       }
+       if (S_ISDIR(sb.st_mode) == 0){
+               mount_error(NULL, mount_point, ENOTDIR);
+               return 1;
+       }
+       if (access(mount_point, X_OK) < 0) {
+               mount_error(NULL, mount_point, errno);
+               return 1;
+       }
+
+       return 0;
+}
+
+/*
+ * Pick up certain mount options used during the original mount
+ * from /etc/mtab.  The basics include the server's IP address and
+ * the server pathname of the share to unregister.
+ *
+ * These options might also describe the mount port, mount protocol
+ * version, and transport protocol used to punch through a firewall.
+ * We will need this information to get through the firewall again
+ * to do the umount.
+ *
+ * Note that option parsing failures won't necessarily cause the
+ * umount request to fail.  Those values will be left zero in the
+ * pmap tuple.  If the GETPORT call later fails to disambiguate them,
+ * then we fail.
+ */
+int nfs_umount23(const char *devname, char *string)
+{
+       char *hostname = NULL, *dirname = NULL;
+       struct mount_options *options;
+       int result = EX_FAIL;
+
+       if (!nfs_parse_devname(devname, &hostname, &dirname))
+               return EX_USAGE;
+
+       options = po_split(string);
+       if (options) {
+               result = nfs_umount_do_umnt(options, &hostname, &dirname);
+               po_destroy(options);
+       } else
+               nfs_error(_("%s: option parsing error"), progname);
+
+       free(hostname);
+       free(dirname);
+       return result;
+}
diff --git a/utils/mount/utils.h b/utils/mount/utils.h
new file mode 100644 (file)
index 0000000..3fcd504
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * utils.h -- misc utils for mount and umount
+ *
+ * Copyright (C) 2010 Karel Zak <kzak@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ *
+ */
+
+#ifndef _NFS_UTILS_MOUNT_UTILS_H
+#define _NFS_UTILS_MOUNT_UTILS_H
+
+#include "parse_opt.h"
+
+int discover_nfs_mount_data_version(int *string_ver);
+void print_one(char *spec, char *node, char *type, char *opts);
+void mount_usage(void);
+void umount_usage(void);
+int chk_mountpoint(const char *mount_point);
+
+int nfs_umount23(const char *devname, char *string);
+
+#endif /* !_NFS_UTILS_MOUNT_UTILS_H */