]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsumount.c
umount.nfs: Pass return codes from do_nfs_umount back
[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 #include "conn.h"
33
34 #include "mount_constants.h"
35 #include "mount.h"
36 #include "nfsumount.h"
37 #include "error.h"
38 #include "network.h"
39
40 #if !defined(MNT_FORCE)
41 /* dare not try to include <linux/mount.h> -- lots of errors */
42 #define MNT_FORCE 1
43 #endif
44
45 #if !defined(MNT_DETACH)
46 #define MNT_DETACH 2
47 #endif
48
49 extern char *progname;
50 extern int nomtab;
51 extern int verbose;
52 int force;
53 int lazy;
54 int remount;
55
56 static int del_mtab(const char *spec, const char *node)
57 {
58         int umnt_err, res;
59
60         umnt_err = 0;
61         if (lazy) {
62                 res = umount2 (node, MNT_DETACH);
63                 if (res < 0)
64                         umnt_err = errno;
65                 goto writemtab;
66         }
67
68         if (force) {
69                 res = umount2 (node, MNT_FORCE);
70                 if (res == -1) {
71                         int errsv = errno;
72                         perror("umount2");
73                         errno = errsv;
74                         if (errno == ENOSYS) {
75                                 if (verbose)
76                                         printf(_("no umount2, trying umount...\n"));
77                                 res = umount (node);
78                         }
79                 }
80         } else
81                 res = umount (node);
82
83         if (res < 0 && remount && errno == EBUSY && spec) {
84                 /* Umount failed - let us try a remount */
85                 res = mount(spec, node, NULL,
86                             MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
87                 if (res == 0) {
88                         struct mntent remnt;
89                         nfs_error(_("%s: %s busy - remounted read-only"),
90                                         progname, spec);
91                         remnt.mnt_type = remnt.mnt_fsname = NULL;
92                         remnt.mnt_dir = xstrdup(node);
93                         remnt.mnt_opts = xstrdup("ro");
94                         if (!nomtab)
95                                 update_mtab(node, &remnt);
96                         return 0;
97                 } else if (errno != EBUSY) {    /* hmm ... */
98                         perror("remount");
99                         nfs_error(_("%s: could not remount %s read-only"),
100                                         progname, spec);
101                 }
102         }
103
104         if (res >= 0) {
105                 /* Umount succeeded */
106                 if (verbose)
107                         printf (_("%s umounted\n"), spec ? spec : node);
108         }
109
110  writemtab:
111         if (!nomtab &&
112             (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
113                update_mtab(node, NULL);
114         }
115
116         if (res >= 0)
117                 return 0;
118
119         if (umnt_err)
120                 umount_error(umnt_err, node);
121         return EX_FILEIO;
122 }
123
124 static int do_nfs_umount(const char *spec, char *opts)
125 {
126         char *hostname;
127         char *dirname;
128         clnt_addr_t mnt_server = { &hostname, };
129         struct mntent mnt = { .mnt_opts = opts };
130         struct pmap *pmap = &mnt_server.pmap;
131         char *p;
132
133         if (spec == NULL) {
134                 nfs_error(_("%s: No NFS export name was provided"),
135                                 progname);
136                 return EX_USAGE;
137         }
138         
139         p = strchr(spec, ':');
140         if (p == NULL) {
141                 nfs_error(_("%s: '%s' is not a legal NFS export name"),
142                                 progname, spec);
143                 return EX_USAGE;
144         }
145         hostname = xstrndup(spec, p - spec);
146         dirname = xstrdup(p + 1);
147 #ifdef NFS_MOUNT_DEBUG
148         printf(_("host: %s, directory: %s\n"), hostname, dirname);
149 #endif
150
151         if (opts && (p = strstr(opts, "addr="))) {
152                 char *q;
153
154                 free(hostname);
155                 p += 5;
156                 q = p;
157                 while (*q && *q != ',') q++;
158                 hostname = xstrndup(p,q-p);
159         }
160
161         if (opts && (p = strstr(opts, "mounthost="))) {
162                 char *q;
163
164                 free(hostname);
165                 p += 10;
166                 q = p;
167                 while (*q && *q != ',') q++;
168                 hostname = xstrndup(p,q-p);
169         }
170
171         pmap->pm_prog = MOUNTPROG;
172         pmap->pm_vers = MOUNTVERS_NFSV3;
173         if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
174                 pmap->pm_prog = atoi(p+10);
175         if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
176                 pmap->pm_port = atoi(p+10);
177         if (opts && hasmntopt(&mnt, "v2"))
178                 pmap->pm_vers = nfsvers_to_mnt(2);
179         if (opts && hasmntopt(&mnt, "v3"))
180                 pmap->pm_vers = nfsvers_to_mnt(3);
181         if (opts && hasmntopt(&mnt, "v4"))
182                 pmap->pm_vers = nfsvers_to_mnt(4);
183         if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
184                 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
185         if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
186                 pmap->pm_vers = atoi(p+10);
187         if (opts && (hasmntopt(&mnt, "udp") || hasmntopt(&mnt, "proto=udp")))
188                 pmap->pm_prot = IPPROTO_UDP;
189
190         if (!nfs_gethostbyname(hostname, &mnt_server.saddr)) {
191                 nfs_error(_("%s: '%s' does not contain a recognized hostname"),
192                                 progname, spec);
193                 return EX_USAGE;
194         }
195
196         if (!nfs_call_umount(&mnt_server, &dirname)) {
197                 nfs_error(_("%s: Server failed to unmount '%s'"),
198                                 progname, spec);
199                 return EX_USAGE;
200         }
201
202         return 0;
203 }
204
205 static struct option umount_longopts[] =
206 {
207   { "force", 0, 0, 'f' },
208   { "help", 0, 0, 'h' },
209   { "no-mtab", 0, 0, 'n' },
210   { "verbose", 0, 0, 'v' },
211   { "read-only", 0, 0, 'r' },
212   { NULL, 0, 0, 0 }
213 };
214
215 static void umount_usage(void)
216 {
217         printf("usage: %s dir [-fvnrlh]\n", progname);
218         printf("options:\n\t-f\t\tforce unmount\n");
219         printf("\t-v\t\tverbose\n");
220         printf("\t-n\t\tDo not update /etc/mtab\n");
221         printf("\t-r\t\tremount\n");
222         printf("\t-l\t\tlazy unmount\n");
223         printf("\t-h\t\tprint this help\n\n");
224 }
225
226 int nfsumount(int argc, char *argv[])
227 {
228         int c, ret;
229         char *spec;
230         struct mntentchn *mc;
231
232         if (argc < 2) {
233                 umount_usage();
234                 return EX_USAGE;
235         }
236
237         spec = argv[1];
238
239         argv += 1;
240         argc -= 1;
241
242         argv[0] = argv[-1]; /* So that getopt error messages are correct */
243         while ((c = getopt_long (argc, argv, "fvnrlh",
244                                 umount_longopts, NULL)) != -1) {
245
246                 switch (c) {
247                 case 'f':
248                         ++force;
249                         break;
250                 case 'v':
251                         ++verbose;
252                         break;
253                 case 'n':
254                         ++nomtab;
255                         break;
256                 case 'r':
257                         ++remount;
258                         break;
259                 case 'l':
260                         ++lazy;
261                         break;
262                 case 'h':
263                 default:
264                         umount_usage();
265                         return EX_USAGE;
266                 }
267         }
268         if (optind != argc) {
269                 umount_usage();
270                 return EX_USAGE;
271         }
272         
273         if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
274                 nfs_error(_("%s: %s: not found\n"), progname, spec);
275                 return EX_USAGE;
276         }
277
278         if (*spec == '/')
279                 mc = getmntdirbackward(spec, NULL);
280         else
281                 mc = getmntdevbackward(spec, NULL);
282         if (!mc && verbose)
283                 printf(_("Could not find %s in mtab\n"), spec);
284
285         if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
286             strcmp(mc->m.mnt_type, "nfs4") != 0) {
287                 nfs_error(_("%s: %s on %s it not an nfs filesystem"),
288                                 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
289                 return EX_USAGE;
290         }
291
292         if (getuid() != 0) {
293                 /* only permitted if "user=" or "users" is in mount options */
294                 if (!mc) {
295                         /* umount might call us twice.  The second time there will
296                          * be no entry in mtab and we should just exit quietly
297                          */
298                         return 0;
299
300                 only_root:
301                         nfs_error(_("%s: You are not permitted to unmount %s"),
302                                         progname, spec);
303                         return EX_USAGE;
304                 }
305                 if (hasmntopt(&mc->m, "users") == NULL) {
306                         char *opt = hasmntopt(&mc->m, "user");
307                         struct passwd *pw;
308                         char *comma;
309                         int len;
310                         if (!opt)
311                                 goto only_root;
312                         if (opt[4] != '=')
313                                 goto only_root;
314                         comma = strchr(opt, ',');
315                         if (comma)
316                                 len = comma - (opt + 5);
317                         else
318                                 len = strlen(opt+5);
319                         pw = getpwuid(getuid());
320                         if (pw == NULL || strlen(pw->pw_name) != len
321                             || strncmp(pw->pw_name, opt+5, len) != 0)
322                                 goto only_root;
323                 }
324         }
325
326         ret = 0;
327         if (mc) {
328                 if (!lazy)
329                         ret = do_nfs_umount(mc->m.mnt_fsname, mc->m.mnt_opts);
330                 if (!ret || force)
331                         ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
332         } else if (*spec != '/') {
333                 if (!lazy)
334                         ret = do_nfs_umount(spec, "tcp,v3");
335         } else
336                 ret = del_mtab(NULL, spec);
337
338         return ret;
339 }