2 * nfsumount.c -- Linux NFS umount
3 * Copyright (C) 2006 Amit Gud <agud@redhat.com>
5 * - Basic code and wrapper around NFS umount code originally
6 * in util-linux/mount/nfsmount.c
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)
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.
25 #include <sys/mount.h>
33 #include "mount_constants.h"
37 #include "parse_opt.h"
38 #include "parse_dev.h"
40 #if !defined(MNT_FORCE)
41 /* dare not try to include <linux/mount.h> -- lots of errors */
45 #if !defined(MNT_DETACH)
49 extern char *progname;
57 static int try_remount(const char *spec, const char *node)
61 res = mount(spec, node, NULL,
62 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
65 nfs_error(_("%s: %s busy - remounted read-only"),
67 remnt.mnt_type = remnt.mnt_fsname = NULL;
68 remnt.mnt_dir = xstrdup(node);
69 remnt.mnt_opts = xstrdup("ro");
71 update_mtab(node, &remnt);
72 } else if (errno != EBUSY) { /* hmm ... */
74 nfs_error(_("%s: could not remount %s read-only"),
80 static int del_mtab(const char *spec, const char *node)
86 res = umount2 (node, MNT_DETACH);
93 res = umount2 (node, MNT_FORCE);
98 if (errno == ENOSYS) {
100 printf(_("no umount2, trying umount...\n"));
108 if (remount && errno == EBUSY && spec) {
109 res = try_remount(spec, node);
118 /* Umount succeeded */
120 printf(_("%s umounted\n"), spec ? spec : node);
125 (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
126 update_mtab(node, NULL);
133 umount_error(umnt_err, node);
138 * Discover mount server's hostname/address by examining mount options
140 * Returns a pointer to a string that the caller must free, on
141 * success; otherwise NULL is returned.
143 static char *nfs_umount_hostname(struct mount_options *options,
148 option = po_get(options, "mountaddr");
151 option = po_get(options, "mounthost");
154 option = po_get(options, "addr");
162 return strdup(option);
166 * Returns EX_SUCCESS if mount options and device name have been
167 * parsed successfully; otherwise EX_FAIL.
169 static int nfs_umount_do_umnt(struct mount_options *options,
170 char **hostname, char **dirname)
172 struct sockaddr_storage address;
173 struct sockaddr *sap = (struct sockaddr *)&address;
174 socklen_t salen = sizeof(address);
175 struct pmap nfs_pmap, mnt_pmap;
177 nfs_options2pmap(options, &nfs_pmap, &mnt_pmap);
179 *hostname = nfs_umount_hostname(options, *hostname);
181 nfs_error(_("%s: out of memory"), progname);
185 if (nfs_name_to_address(*hostname, sap, &salen) == 0)
186 /* nfs_name_to_address reports any errors */
189 if (nfs_advise_umount(sap, salen, &mnt_pmap, dirname) == 0)
190 /* nfs_advise_umount reports any errors */
197 * Pick up certain mount options used during the original mount
198 * from /etc/mtab. The basics include the server's IP address and
199 * the server pathname of the share to unregister.
201 * These options might also describe the mount port, mount protocol
202 * version, and transport protocol used to punch through a firewall.
203 * We will need this information to get through the firewall again
206 * Note that option parsing failures won't necessarily cause the
207 * umount request to fail. Those values will be left zero in the
208 * pmap tuple. If the GETPORT call later fails to disambiguate them,
211 static int nfs_umount23(const char *devname, char *string)
213 char *hostname, *dirname;
214 struct mount_options *options;
215 int result = EX_FAIL;
217 if (!nfs_parse_devname(devname, &hostname, &dirname))
220 options = po_split(string);
222 result = nfs_umount_do_umnt(options, &hostname, &dirname);
225 nfs_error(_("%s: option parsing error"), progname);
232 static struct option umount_longopts[] =
234 { "force", 0, 0, 'f' },
235 { "help", 0, 0, 'h' },
236 { "no-mtab", 0, 0, 'n' },
237 { "verbose", 0, 0, 'v' },
238 { "read-only", 0, 0, 'r' },
242 static void umount_usage(void)
244 printf(_("usage: %s dir [-fvnrlh]\n"), progname);
245 printf(_("options:\n\t-f\t\tforce unmount\n"));
246 printf(_("\t-v\tverbose\n"));
247 printf(_("\t-n\tDo not update /etc/mtab\n"));
248 printf(_("\t-r\tremount\n"));
249 printf(_("\t-l\tlazy unmount\n"));
250 printf(_("\t-h\tprint this help\n\n"));
253 int nfsumount(int argc, char *argv[])
257 struct mntentchn *mc;
269 argv[0] = argv[-1]; /* So that getopt error messages are correct */
270 while ((c = getopt_long (argc, argv, "fvnrlh",
271 umount_longopts, NULL)) != -1) {
295 if (optind != argc) {
300 if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
301 nfs_error(_("%s: %s: not found\n"), progname, spec);
306 mc = getmntdirbackward(spec, NULL);
308 mc = getmntdevbackward(spec, NULL);
310 printf(_("Could not find %s in mtab\n"), spec);
312 if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
313 strcmp(mc->m.mnt_type, "nfs4") != 0) {
314 nfs_error(_("%s: %s on %s is not an NFS filesystem"),
315 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
320 /* only permitted if "user=" or "users" is in mount options */
322 /* umount might call us twice. The second time there will
323 * be no entry in mtab and we should just exit quietly
328 nfs_error(_("%s: You are not permitted to unmount %s"),
332 if (hasmntopt(&mc->m, "users") == NULL) {
333 char *opt = hasmntopt(&mc->m, "user");
341 comma = strchr(opt, ',');
343 len = comma - (opt + 5);
346 pw = getpwuid(getuid());
347 if (pw == NULL || strlen(pw->pw_name) != len
348 || strncmp(pw->pw_name, opt+5, len) != 0)
355 if (!lazy && strcmp(mc->m.mnt_type, "nfs4") != 0)
356 /* We ignore the error from nfs_umount23.
357 * If the actual umount succeeds (in del_mtab),
358 * we don't want to signal an error, as that
359 * could cause /sbin/mount to retry!
361 nfs_umount23(mc->m.mnt_fsname, mc->m.mnt_opts);
362 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir) ?: ret;
363 } else if (*spec != '/') {
365 ret = nfs_umount23(spec, "tcp,v3");
367 ret = del_mtab(NULL, spec);