]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsumount.c
Updated rpc.mountd man page
[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 "nfs_mount.h"
35 #include "mount.h"
36 #include "error.h"
37 #include "network.h"
38 #include "parse_opt.h"
39 #include "parse_dev.h"
40
41 #define MOUNTSFILE      "/proc/mounts"
42 #define LINELEN         (4096)
43
44 #if !defined(MNT_FORCE)
45 /* dare not try to include <linux/mount.h> -- lots of errors */
46 #define MNT_FORCE 1
47 #endif
48
49 #if !defined(MNT_DETACH)
50 #define MNT_DETACH 2
51 #endif
52
53 extern char *progname;
54 extern int nomtab;
55 extern int verbose;
56 int force;
57 int lazy;
58 int remount;
59
60
61 static int try_remount(const char *spec, const char *node)
62 {
63         int res;
64
65         res = mount(spec, node, NULL,
66                     MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
67         if (res == 0) {
68                 struct mntent remnt;
69                 nfs_error(_("%s: %s busy - remounted read-only"),
70                                 progname, spec);
71                 remnt.mnt_type = remnt.mnt_fsname = NULL;
72                 remnt.mnt_dir = xstrdup(node);
73                 remnt.mnt_opts = xstrdup("ro");
74                 if (!nomtab)
75                         update_mtab(node, &remnt);
76         } else if (errno != EBUSY) {    /* hmm ... */
77                 perror(_("remount"));
78                 nfs_error(_("%s: could not remount %s read-only"),
79                                 progname, spec);
80         }
81         return res;
82 }
83
84 static int del_mtab(const char *spec, const char *node)
85 {
86         int umnt_err, res;
87
88         umnt_err = 0;
89         if (lazy) {
90                 res = umount2 (node, MNT_DETACH);
91                 if (res < 0)
92                         umnt_err = errno;
93                 goto writemtab;
94         }
95
96         if (force) {
97                 res = umount2 (node, MNT_FORCE);
98                 if (res == -1) {
99                         int errsv = errno;
100                         perror(_("umount2"));
101                         errno = errsv;
102                         if (errno == ENOSYS) {
103                                 if (verbose)
104                                         printf(_("no umount2, trying umount...\n"));
105                                 res = umount (node);
106                         }
107                 }
108         } else
109                 res = umount (node);
110
111         if (res < 0) {
112                 if (remount && errno == EBUSY && spec) {
113                         res = try_remount(spec, node);
114                         if (res)
115                                 goto writemtab;
116                         return EX_SUCCESS;
117                 } else
118                         umnt_err = errno;
119         }
120
121         if (res >= 0) {
122                 /* Umount succeeded */
123                 if (verbose)
124                         printf(_("%s umounted\n"), spec ? spec : node);
125         }
126
127  writemtab:
128         if (!nomtab &&
129             (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
130                 update_mtab(node, NULL);
131         }
132
133         if (res >= 0)
134                 return EX_SUCCESS;
135
136         if (umnt_err)
137                 umount_error(umnt_err, node);
138         return EX_FILEIO;
139 }
140
141 /*
142  * Discover mount server's hostname/address by examining mount options
143  *
144  * Returns a pointer to a string that the caller must free, on
145  * success; otherwise NULL is returned.
146  */
147 static char *nfs_umount_hostname(struct mount_options *options,
148                                  char *hostname)
149 {
150         char *option;
151
152         option = po_get(options, "mountaddr");
153         if (option)
154                 goto out;
155         option = po_get(options, "mounthost");
156         if (option)
157                 goto out;
158         option = po_get(options, "addr");
159         if (option)
160                 goto out;
161
162         return hostname;
163
164 out:
165         free(hostname);
166         return strdup(option);
167 }
168
169 /*
170  * Returns EX_SUCCESS if mount options and device name have been
171  * parsed successfully; otherwise EX_FAIL.
172  */
173 static int nfs_umount_do_umnt(struct mount_options *options,
174                               char **hostname, char **dirname)
175 {
176         union {
177                 struct sockaddr         sa;
178                 struct sockaddr_in      s4;
179                 struct sockaddr_in6     s6;
180         } address;
181         struct sockaddr *sap = &address.sa;
182         socklen_t salen = sizeof(address);
183         struct pmap nfs_pmap, mnt_pmap;
184         sa_family_t family;
185
186         if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap))
187                 return EX_FAIL;
188
189         /* Skip UMNT call for vers=4 mounts */
190         if (nfs_pmap.pm_vers == 4)
191                 return EX_SUCCESS;
192
193         *hostname = nfs_umount_hostname(options, *hostname);
194         if (!*hostname) {
195                 nfs_error(_("%s: out of memory"), progname);
196                 return EX_FAIL;
197         }
198
199         if (!nfs_mount_proto_family(options, &family))
200                 return 0;
201         if (!nfs_lookup(*hostname, family, sap, &salen))
202                 /* nfs_lookup reports any errors */
203                 return EX_FAIL;
204
205         if (nfs_advise_umount(sap, salen, &mnt_pmap, dirname) == 0)
206                 /* nfs_advise_umount reports any errors */
207                 return EX_FAIL;
208
209         return EX_SUCCESS;
210 }
211
212 /*
213  * Pick up certain mount options used during the original mount
214  * from /etc/mtab.  The basics include the server's IP address and
215  * the server pathname of the share to unregister.
216  *
217  * These options might also describe the mount port, mount protocol
218  * version, and transport protocol used to punch through a firewall.
219  * We will need this information to get through the firewall again
220  * to do the umount.
221  *
222  * Note that option parsing failures won't necessarily cause the
223  * umount request to fail.  Those values will be left zero in the
224  * pmap tuple.  If the GETPORT call later fails to disambiguate them,
225  * then we fail.
226  */
227 static int nfs_umount23(const char *devname, char *string)
228 {
229         char *hostname, *dirname;
230         struct mount_options *options;
231         int result = EX_FAIL;
232
233         if (!nfs_parse_devname(devname, &hostname, &dirname))
234                 return EX_USAGE;
235
236         options = po_split(string);
237         if (options) {
238                 result = nfs_umount_do_umnt(options, &hostname, &dirname);
239                 po_destroy(options);
240         } else
241                 nfs_error(_("%s: option parsing error"), progname);
242
243         free(hostname);
244         free(dirname);
245         return result;
246 }
247
248 /*
249  * Detect NFSv4 mounts.
250  *
251  * Consult /proc/mounts to determine if the mount point
252  * is an NFSv4 mount.  The kernel is authoritative about
253  * what type of mount this is.
254  *
255  * Returns 1 if "mc" is an NFSv4 mount, zero if not, and
256  * -1 if some error occurred.
257  */
258 static int nfs_umount_is_vers4(const struct mntentchn *mc)
259 {
260         char buffer[LINELEN], *next;
261         int retval;
262         FILE *f;
263
264         if ((f = fopen(MOUNTSFILE, "r")) == NULL) {
265                 fprintf(stderr, "%s: %s\n",
266                         MOUNTSFILE, strerror(errno));
267                 return -1;
268         }
269
270         retval = -1;
271         while (fgets(buffer, sizeof(buffer), f) != NULL) {
272                 char *device, *mntdir, *type, *flags;
273                 struct mount_options *options;
274                 char *line = buffer;
275
276                 next = strchr(line, '\n');
277                 if (next != NULL)
278                         *next = '\0';
279
280                 device = strtok(line, " \t");
281                 if (device == NULL)
282                         continue;
283                 mntdir = strtok(NULL, " \t");
284                 if (mntdir == NULL)
285                         continue;
286                 if (strcmp(device, mc->m.mnt_fsname) != 0 &&
287                     strcmp(mntdir, mc->m.mnt_dir) != 0)
288                         continue;
289
290                 type = strtok(NULL, " \t");
291                 if (type == NULL)
292                         continue;
293                 if (strcmp(type, "nfs4") == 0)
294                         goto out_nfs4;
295
296                 flags = strtok(NULL, " \t");
297                 if (flags == NULL)
298                         continue;
299                 options = po_split(flags);
300                 if (options != NULL) {
301                         unsigned long version;
302                         int rc;
303
304                         rc = nfs_nfs_version(options, &version);
305                         po_destroy(options);
306                         if (rc && version == 4)
307                                 goto out_nfs4;
308                 }
309
310                 goto out_nfs;
311         }
312         if (retval == -1)
313                 fprintf(stderr, "%s was not found in %s\n",
314                         mc->m.mnt_dir, MOUNTSFILE);
315
316 out:
317         fclose(f);
318         return retval;
319
320 out_nfs4:
321         if (verbose)
322                 fprintf(stderr, "NFSv4 mount point detected\n");
323         retval = 1;
324         goto out;
325
326 out_nfs:
327         if (verbose)
328                 fprintf(stderr, "Legacy NFS mount point detected\n");
329         retval = 0;
330         goto out;
331 }
332
333 static struct option umount_longopts[] =
334 {
335   { "force", 0, 0, 'f' },
336   { "help", 0, 0, 'h' },
337   { "no-mtab", 0, 0, 'n' },
338   { "verbose", 0, 0, 'v' },
339   { "read-only", 0, 0, 'r' },
340   { NULL, 0, 0, 0 }
341 };
342
343 static void umount_usage(void)
344 {
345         printf(_("usage: %s dir [-fvnrlh]\n"), progname);
346         printf(_("options:\n\t-f\t\tforce unmount\n"));
347         printf(_("\t-v\tverbose\n"));
348         printf(_("\t-n\tDo not update /etc/mtab\n"));
349         printf(_("\t-r\tremount\n"));
350         printf(_("\t-l\tlazy unmount\n"));
351         printf(_("\t-h\tprint this help\n\n"));
352 }
353
354 int nfsumount(int argc, char *argv[])
355 {
356         int c, ret;
357         char *spec;
358         struct mntentchn *mc;
359
360         if (argc < 2) {
361                 umount_usage();
362                 return EX_USAGE;
363         }
364
365         spec = argv[1];
366
367         argv += 1;
368         argc -= 1;
369
370         argv[0] = argv[-1]; /* So that getopt error messages are correct */
371         while ((c = getopt_long (argc, argv, "fvnrlh",
372                                 umount_longopts, NULL)) != -1) {
373
374                 switch (c) {
375                 case 'f':
376                         ++force;
377                         break;
378                 case 'v':
379                         ++verbose;
380                         break;
381                 case 'n':
382                         ++nomtab;
383                         break;
384                 case 'r':
385                         ++remount;
386                         break;
387                 case 'l':
388                         ++lazy;
389                         break;
390                 case 'h':
391                 default:
392                         umount_usage();
393                         return EX_USAGE;
394                 }
395         }
396         if (optind != argc) {
397                 umount_usage();
398                 return EX_USAGE;
399         }
400         
401         if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
402                 nfs_error(_("%s: %s: not found\n"), progname, spec);
403                 return EX_USAGE;
404         }
405
406         if (*spec == '/')
407                 mc = getmntdirbackward(spec, NULL);
408         else
409                 mc = getmntdevbackward(spec, NULL);
410         if (!mc && verbose)
411                 printf(_("Could not find %s in mtab\n"), spec);
412
413         if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
414             strcmp(mc->m.mnt_type, "nfs4") != 0) {
415                 nfs_error(_("%s: %s on %s is not an NFS filesystem"),
416                                 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
417                 return EX_USAGE;
418         }
419
420         if (getuid() != 0) {
421                 /* only permitted if "user=" or "users" is in mount options */
422                 if (!mc) {
423                         /* umount might call us twice.  The second time there will
424                          * be no entry in mtab and we should just exit quietly
425                          */
426                         return EX_SUCCESS;
427
428                 only_root:
429                         nfs_error(_("%s: You are not permitted to unmount %s"),
430                                         progname, spec);
431                         return EX_USAGE;
432                 }
433                 if (hasmntopt(&mc->m, "users") == NULL) {
434                         char *opt = hasmntopt(&mc->m, "user");
435                         struct passwd *pw;
436                         char *comma;
437                         size_t len;
438                         if (!opt)
439                                 goto only_root;
440                         if (opt[4] != '=')
441                                 goto only_root;
442                         comma = strchr(opt, ',');
443                         if (comma)
444                                 len = comma - (opt + 5);
445                         else
446                                 len = strlen(opt+5);
447                         pw = getpwuid(getuid());
448                         if (pw == NULL || strlen(pw->pw_name) != len
449                             || strncmp(pw->pw_name, opt+5, len) != 0)
450                                 goto only_root;
451                 }
452         }
453
454         ret = EX_SUCCESS;
455         if (mc) {
456                 if (!lazy) {
457                         switch (nfs_umount_is_vers4(mc)) {
458                         case 0:
459                                 /* We ignore the error from nfs_umount23.
460                                  * If the actual umount succeeds (in del_mtab),
461                                  * we don't want to signal an error, as that
462                                  * could cause /sbin/mount to retry!
463                                  */
464                                 nfs_umount23(mc->m.mnt_fsname, mc->m.mnt_opts);
465                                 break;
466                         case 1:
467                                 break;
468                         default:
469                                 return EX_FAIL;
470                         }
471                 }
472                 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
473         } else if (*spec != '/') {
474                 if (!lazy)
475                         ret = nfs_umount23(spec, "tcp,v3");
476         } else
477                 ret = del_mtab(NULL, spec);
478
479         return ret;
480 }