]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsumount.c
Don't fail an unmount just because we couldn't contact the NFS server.
[nfs-utils.git] / utils / mount / nfsumount.c
1 /*
2  * nfsumount.c -- Linux NFS umount
3  * Copyright (C) 2006 Amit Gud <agud@redhat.com>
4  *
5  * - Basic code and wrapper around NFS umount code originally
6  *   in util-linux/mount/nfsmount.c
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <errno.h>
23 #include <getopt.h>
24 #include <mntent.h>
25 #include <sys/mount.h>
26 #include <ctype.h>
27 #include <pwd.h>
28
29 #include "xcommon.h"
30 #include "fstab.h"
31 #include "nls.h"
32
33 #include "mount_constants.h"
34 #include "mount.h"
35 #include "error.h"
36 #include "network.h"
37
38 #if !defined(MNT_FORCE)
39 /* dare not try to include <linux/mount.h> -- lots of errors */
40 #define MNT_FORCE 1
41 #endif
42
43 #if !defined(MNT_DETACH)
44 #define MNT_DETACH 2
45 #endif
46
47 extern char *progname;
48 extern int nomtab;
49 extern int verbose;
50 int force;
51 int lazy;
52 int remount;
53
54
55 static int try_remount(const char *spec, const char *node)
56 {
57         int res;
58
59         res = mount(spec, node, NULL,
60                     MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
61         if (res == 0) {
62                 struct mntent remnt;
63                 nfs_error(_("%s: %s busy - remounted read-only"),
64                                 progname, spec);
65                 remnt.mnt_type = remnt.mnt_fsname = NULL;
66                 remnt.mnt_dir = xstrdup(node);
67                 remnt.mnt_opts = xstrdup("ro");
68                 if (!nomtab)
69                         update_mtab(node, &remnt);
70         } else if (errno != EBUSY) {    /* hmm ... */
71                 perror(_("remount"));
72                 nfs_error(_("%s: could not remount %s read-only"),
73                                 progname, spec);
74         }
75         return res;
76 }
77
78 static int del_mtab(const char *spec, const char *node)
79 {
80         int umnt_err, res;
81
82         umnt_err = 0;
83         if (lazy) {
84                 res = umount2 (node, MNT_DETACH);
85                 if (res < 0)
86                         umnt_err = errno;
87                 goto writemtab;
88         }
89
90         if (force) {
91                 res = umount2 (node, MNT_FORCE);
92                 if (res == -1) {
93                         int errsv = errno;
94                         perror(_("umount2"));
95                         errno = errsv;
96                         if (errno == ENOSYS) {
97                                 if (verbose)
98                                         printf(_("no umount2, trying umount...\n"));
99                                 res = umount (node);
100                         }
101                 }
102         } else
103                 res = umount (node);
104
105         if (res < 0) {
106                 if (remount && errno == EBUSY && spec) {
107                         res = try_remount(spec, node);
108                         if (res)
109                                 goto writemtab;
110                         return 0;
111                 } else
112                         umnt_err = errno;
113         }
114
115         if (res >= 0) {
116                 /* Umount succeeded */
117                 if (verbose)
118                         printf(_("%s umounted\n"), spec ? spec : node);
119         }
120
121  writemtab:
122         if (!nomtab &&
123             (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
124                 update_mtab(node, NULL);
125         }
126
127         if (res >= 0)
128                 return 0;
129
130         if (umnt_err)
131                 umount_error(umnt_err, node);
132         return EX_FILEIO;
133 }
134
135 /*
136  * Pick up certain mount options used during the original mount
137  * from /etc/mtab.  The basics include the server's IP address and
138  * the server pathname of the share to unregister.
139  *
140  * These options might also describe the mount port, mount protocol
141  * version, and transport protocol used to punch through a firewall.
142  * We will need this information to get through the firewall again
143  * to do the umount.
144  */
145 static int do_nfs_umount(const char *spec, char *opts)
146 {
147         char *hostname;
148         char *dirname;
149         clnt_addr_t mnt_server = { &hostname, };
150         struct mntent mnt = { .mnt_opts = opts };
151         struct pmap *pmap = &mnt_server.pmap;
152         char *p;
153
154         if (spec == NULL) {
155                 nfs_error(_("%s: No NFS export name was provided"),
156                                 progname);
157                 return EX_USAGE;
158         }
159         
160         p = strchr(spec, ':');
161         if (p == NULL) {
162                 nfs_error(_("%s: '%s' is not a legal NFS export name"),
163                                 progname, spec);
164                 return EX_USAGE;
165         }
166         hostname = xstrndup(spec, p - spec);
167         dirname = xstrdup(p + 1);
168 #ifdef NFS_MOUNT_DEBUG
169         printf(_("host: %s, directory: %s\n"), hostname, dirname);
170 #endif
171
172         if (opts && (p = strstr(opts, "addr="))) {
173                 char *q;
174
175                 free(hostname);
176                 p += 5;
177                 q = p;
178                 while (*q && *q != ',') q++;
179                 hostname = xstrndup(p,q-p);
180         }
181
182         if (opts && (p = strstr(opts, "mounthost="))) {
183                 char *q;
184
185                 free(hostname);
186                 p += 10;
187                 q = p;
188                 while (*q && *q != ',') q++;
189                 hostname = xstrndup(p,q-p);
190         }
191
192         pmap->pm_prog = MOUNTPROG;
193         pmap->pm_vers = MOUNTVERS_NFSV3;
194         if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
195                 pmap->pm_prog = atoi(p+10);
196         if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
197                 pmap->pm_port = atoi(p+10);
198         if (opts && hasmntopt(&mnt, "v2"))
199                 pmap->pm_vers = nfsvers_to_mnt(2);
200         if (opts && hasmntopt(&mnt, "v3"))
201                 pmap->pm_vers = nfsvers_to_mnt(3);
202         if (opts && hasmntopt(&mnt, "v4"))
203                 pmap->pm_vers = nfsvers_to_mnt(4);
204         if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
205                 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
206         if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
207                 pmap->pm_vers = atoi(p+10);
208         if (opts && (hasmntopt(&mnt, "udp") || hasmntopt(&mnt, "proto=udp")))
209                 pmap->pm_prot = IPPROTO_UDP;
210         if (opts && (hasmntopt(&mnt, "tcp") || hasmntopt(&mnt, "proto=tcp")))
211                 pmap->pm_prot = IPPROTO_TCP;
212
213         if (!nfs_gethostbyname(hostname, &mnt_server.saddr)) {
214                 nfs_error(_("%s: '%s' does not contain a recognized hostname"),
215                                 progname, spec);
216                 return EX_USAGE;
217         }
218
219         if (!nfs_call_umount(&mnt_server, &dirname)) {
220                 nfs_error(_("%s: Server failed to unmount '%s'"),
221                                 progname, spec);
222                 return EX_USAGE;
223         }
224
225         return EX_SUCCESS;
226 }
227
228 static struct option umount_longopts[] =
229 {
230   { "force", 0, 0, 'f' },
231   { "help", 0, 0, 'h' },
232   { "no-mtab", 0, 0, 'n' },
233   { "verbose", 0, 0, 'v' },
234   { "read-only", 0, 0, 'r' },
235   { NULL, 0, 0, 0 }
236 };
237
238 static void umount_usage(void)
239 {
240         printf(_("usage: %s dir [-fvnrlh]\n"), progname);
241         printf(_("options:\n\t-f\t\tforce unmount\n"));
242         printf(_("\t-v\tverbose\n"));
243         printf(_("\t-n\tDo not update /etc/mtab\n"));
244         printf(_("\t-r\tremount\n"));
245         printf(_("\t-l\tlazy unmount\n"));
246         printf(_("\t-h\tprint this help\n\n"));
247 }
248
249 int nfsumount(int argc, char *argv[])
250 {
251         int c, ret;
252         char *spec;
253         struct mntentchn *mc;
254
255         if (argc < 2) {
256                 umount_usage();
257                 return EX_USAGE;
258         }
259
260         spec = argv[1];
261
262         argv += 1;
263         argc -= 1;
264
265         argv[0] = argv[-1]; /* So that getopt error messages are correct */
266         while ((c = getopt_long (argc, argv, "fvnrlh",
267                                 umount_longopts, NULL)) != -1) {
268
269                 switch (c) {
270                 case 'f':
271                         ++force;
272                         break;
273                 case 'v':
274                         ++verbose;
275                         break;
276                 case 'n':
277                         ++nomtab;
278                         break;
279                 case 'r':
280                         ++remount;
281                         break;
282                 case 'l':
283                         ++lazy;
284                         break;
285                 case 'h':
286                 default:
287                         umount_usage();
288                         return EX_USAGE;
289                 }
290         }
291         if (optind != argc) {
292                 umount_usage();
293                 return EX_USAGE;
294         }
295         
296         if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
297                 nfs_error(_("%s: %s: not found\n"), progname, spec);
298                 return EX_USAGE;
299         }
300
301         if (*spec == '/')
302                 mc = getmntdirbackward(spec, NULL);
303         else
304                 mc = getmntdevbackward(spec, NULL);
305         if (!mc && verbose)
306                 printf(_("Could not find %s in mtab\n"), spec);
307
308         if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
309             strcmp(mc->m.mnt_type, "nfs4") != 0) {
310                 nfs_error(_("%s: %s on %s is not an NFS filesystem"),
311                                 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
312                 return EX_USAGE;
313         }
314
315         if (getuid() != 0) {
316                 /* only permitted if "user=" or "users" is in mount options */
317                 if (!mc) {
318                         /* umount might call us twice.  The second time there will
319                          * be no entry in mtab and we should just exit quietly
320                          */
321                         return EX_SUCCESS;
322
323                 only_root:
324                         nfs_error(_("%s: You are not permitted to unmount %s"),
325                                         progname, spec);
326                         return EX_USAGE;
327                 }
328                 if (hasmntopt(&mc->m, "users") == NULL) {
329                         char *opt = hasmntopt(&mc->m, "user");
330                         struct passwd *pw;
331                         char *comma;
332                         int len;
333                         if (!opt)
334                                 goto only_root;
335                         if (opt[4] != '=')
336                                 goto only_root;
337                         comma = strchr(opt, ',');
338                         if (comma)
339                                 len = comma - (opt + 5);
340                         else
341                                 len = strlen(opt+5);
342                         pw = getpwuid(getuid());
343                         if (pw == NULL || strlen(pw->pw_name) != len
344                             || strncmp(pw->pw_name, opt+5, len) != 0)
345                                 goto only_root;
346                 }
347         }
348
349         ret = 0;
350         if (mc) {
351                 if (!lazy && strcmp(mc->m.mnt_type, "nfs4") != 0)
352                         ret = do_nfs_umount(mc->m.mnt_fsname, mc->m.mnt_opts);
353                 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir) ?: ret;
354         } else if (*spec != '/') {
355                 if (!lazy)
356                         ret = do_nfs_umount(spec, "tcp,v3");
357         } else
358                 ret = del_mtab(NULL, spec);
359
360         return ret;
361 }