]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/utils.c
mount: move generic functions to utils.c and network.c
[nfs-utils.git] / utils / mount / utils.c
1 /*
2  * Copyright (C) 2010 Karel Zak <kzak@redhat.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 021110-1307, USA.
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31
32 #include "sockaddr.h"
33 #include "nfs_mount.h"
34 #include "nls.h"
35 #include "xcommon.h"
36 #include "version.h"
37 #include "error.h"
38 #include "utils.h"
39 #include "mount.h"
40 #include "network.h"
41 #include "parse_dev.h"
42
43 extern int verbose;
44 extern char *progname;
45
46 /*
47  * Choose the version of the nfs_mount_data structure that is appropriate
48  * for the kernel that is doing the mount.
49  *
50  * NFS_MOUNT_VERSION:           maximum version supported by these sources
51  * nfs_mount_data_version:      maximum version supported by the running kernel
52  */
53 int discover_nfs_mount_data_version(int *string_ver)
54 {
55         unsigned int kernel_version = linux_version_code();
56         int ver = 0;
57
58         *string_ver = 0;
59
60         if (kernel_version) {
61                 if (kernel_version < MAKE_VERSION(2, 1, 32))
62                         ver = 1;
63                 else if (kernel_version < MAKE_VERSION(2, 2, 18))
64                         ver = 3;
65                 else if (kernel_version < MAKE_VERSION(2, 3, 0))
66                         ver = 4;
67                 else if (kernel_version < MAKE_VERSION(2, 3, 99))
68                         ver = 3;
69                 else if (kernel_version < MAKE_VERSION(2, 6, 3))
70                         ver = 4;
71                 else
72                         ver = 6;
73         }
74         if (ver > NFS_MOUNT_VERSION)
75                 ver = NFS_MOUNT_VERSION;
76         else
77                 if (kernel_version > MAKE_VERSION(2, 6, 22))
78                         (*string_ver)++;
79
80         return ver;
81 }
82
83 void print_one(char *spec, char *node, char *type, char *opts)
84 {
85         if (!verbose)
86                 return;
87
88         if (opts)
89                 printf(_("%s on %s type %s (%s)\n"), spec, node, type, opts);
90         else
91                 printf(_("%s on %s type %s\n"), spec, node, type);
92 }
93
94 void mount_usage(void)
95 {
96         printf(_("usage: %s remotetarget dir [-rvVwfnsih] [-o nfsoptions]\n"),
97                 progname);
98         printf(_("options:\n"));
99         printf(_("\t-r\t\tMount file system readonly\n"));
100         printf(_("\t-v\t\tVerbose\n"));
101         printf(_("\t-V\t\tPrint version\n"));
102         printf(_("\t-w\t\tMount file system read-write\n"));
103         printf(_("\t-f\t\tFake mount, do not actually mount\n"));
104         printf(_("\t-n\t\tDo not update /etc/mtab\n"));
105         printf(_("\t-s\t\tTolerate sloppy mount options rather than fail\n"));
106         printf(_("\t-h\t\tPrint this help\n"));
107         printf(_("\tnfsoptions\tRefer to mount.nfs(8) or nfs(5)\n\n"));
108 }
109
110 void umount_usage(void)
111 {
112         printf(_("usage: %s dir [-fvnrlh]\n"), progname);
113         printf(_("options:\n\t-f\t\tforce unmount\n"));
114         printf(_("\t-v\tverbose\n"));
115         printf(_("\t-n\tDo not update /etc/mtab\n"));
116         printf(_("\t-r\tremount\n"));
117         printf(_("\t-l\tlazy unmount\n"));
118         printf(_("\t-h\tprint this help\n\n"));
119 }
120
121 int chk_mountpoint(const char *mount_point)
122 {
123         struct stat sb;
124
125         if (stat(mount_point, &sb) < 0){
126                 mount_error(NULL, mount_point, errno);
127                 return 1;
128         }
129         if (S_ISDIR(sb.st_mode) == 0){
130                 mount_error(NULL, mount_point, ENOTDIR);
131                 return 1;
132         }
133         if (access(mount_point, X_OK) < 0) {
134                 mount_error(NULL, mount_point, errno);
135                 return 1;
136         }
137
138         return 0;
139 }
140
141 /*
142  * Pick up certain mount options used during the original mount
143  * from /etc/mtab.  The basics include the server's IP address and
144  * the server pathname of the share to unregister.
145  *
146  * These options might also describe the mount port, mount protocol
147  * version, and transport protocol used to punch through a firewall.
148  * We will need this information to get through the firewall again
149  * to do the umount.
150  *
151  * Note that option parsing failures won't necessarily cause the
152  * umount request to fail.  Those values will be left zero in the
153  * pmap tuple.  If the GETPORT call later fails to disambiguate them,
154  * then we fail.
155  */
156 int nfs_umount23(const char *devname, char *string)
157 {
158         char *hostname = NULL, *dirname = NULL;
159         struct mount_options *options;
160         int result = EX_FAIL;
161
162         if (!nfs_parse_devname(devname, &hostname, &dirname))
163                 return EX_USAGE;
164
165         options = po_split(string);
166         if (options) {
167                 result = nfs_umount_do_umnt(options, &hostname, &dirname);
168                 po_destroy(options);
169         } else
170                 nfs_error(_("%s: option parsing error"), progname);
171
172         free(hostname);
173         free(dirname);
174         return result;
175 }