]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsumount.c
nfsumount: Squelch compiler warning
[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 #include "utils.h"
41
42 #define MOUNTSFILE      "/proc/mounts"
43 #define LINELEN         (4096)
44
45 #if !defined(MNT_FORCE)
46 /* dare not try to include <linux/mount.h> -- lots of errors */
47 #define MNT_FORCE 1
48 #endif
49
50 #if !defined(MNT_DETACH)
51 #define MNT_DETACH 2
52 #endif
53
54 extern char *progname;
55 extern int nomtab;
56 extern int verbose;
57 int force;
58 int lazy;
59 int remount;
60
61
62 static int try_remount(const char *spec, const char *node)
63 {
64         int res;
65
66         res = mount(spec, node, NULL,
67                     MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
68         if (res == 0) {
69                 struct mntent remnt;
70                 nfs_error(_("%s: %s busy - remounted read-only"),
71                                 progname, spec);
72                 remnt.mnt_type = remnt.mnt_fsname = NULL;
73                 remnt.mnt_dir = xstrdup(node);
74                 remnt.mnt_opts = xstrdup("ro");
75                 if (!nomtab)
76                         update_mtab(node, &remnt);
77         } else if (errno != EBUSY) {    /* hmm ... */
78                 perror(_("remount"));
79                 nfs_error(_("%s: could not remount %s read-only"),
80                                 progname, spec);
81         }
82         return res;
83 }
84
85 static int del_mtab(const char *spec, const char *node)
86 {
87         int umnt_err, res;
88
89         umnt_err = 0;
90         if (lazy) {
91                 res = umount2 (node, MNT_DETACH);
92                 if (res < 0)
93                         umnt_err = errno;
94                 goto writemtab;
95         }
96
97         if (force) {
98                 res = umount2 (node, MNT_FORCE);
99                 if (res == -1) {
100                         int errsv = errno;
101                         perror(_("umount2"));
102                         errno = errsv;
103                         if (errno == ENOSYS) {
104                                 if (verbose)
105                                         printf(_("no umount2, trying umount...\n"));
106                                 res = umount (node);
107                         }
108                 }
109         } else
110                 res = umount (node);
111
112         if (res < 0) {
113                 if (remount && errno == EBUSY && spec) {
114                         res = try_remount(spec, node);
115                         if (res)
116                                 goto writemtab;
117                         return EX_SUCCESS;
118                 } else
119                         umnt_err = errno;
120         }
121
122         if (res >= 0) {
123                 /* Umount succeeded */
124                 if (verbose)
125                         printf(_("%s umounted\n"), spec ? spec : node);
126         }
127
128  writemtab:
129         if (!nomtab &&
130             (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
131                 update_mtab(node, NULL);
132         }
133
134         if (res >= 0)
135                 return EX_SUCCESS;
136
137         if (umnt_err)
138                 umount_error(umnt_err, node);
139         return EX_FILEIO;
140 }
141
142 /*
143  * Detect NFSv4 mounts.
144  *
145  * Consult /proc/mounts to determine if the mount point
146  * is an NFSv4 mount.  The kernel is authoritative about
147  * what type of mount this is.
148  *
149  * Returns 1 if "mc" is an NFSv4 mount, zero if not, and
150  * -1 if some error occurred.
151  */
152 static int nfs_umount_is_vers4(const struct mntentchn *mc)
153 {
154         struct mntentchn *pmc;
155         struct mount_options *options;
156         int retval;
157
158         retval = -1;
159         pmc = getprocmntdirbackward(mc->m.mnt_dir, NULL);
160         if (!pmc)
161                 goto not_found;
162
163         do {
164                 size_t nlen = strlen(pmc->m.mnt_fsname);
165
166                 /*
167                  * It's possible the mount location string in /proc/mounts
168                  * ends with a '/'. In this case, if the entry came from
169                  * /etc/mtab, it won't have the trailing '/' so deal with
170                  * it.
171                  */
172                 while (pmc->m.mnt_fsname[nlen - 1] == '/')
173                         nlen--;
174                 if (strncmp(pmc->m.mnt_fsname, mc->m.mnt_fsname, nlen) != 0)
175                         continue;
176
177                 if (strcmp(pmc->m.mnt_type, "nfs4") == 0)
178                         goto out_nfs4;
179
180                 options = po_split(pmc->m.mnt_opts);
181                 if (options != NULL) {
182                         unsigned long version;
183                         int rc = nfs_nfs_version(options, &version);
184                         po_destroy(options);
185                         if (rc && version == 4)
186                                 goto out_nfs4;
187                 }
188
189                 if (strcmp(pmc->m.mnt_type, "nfs") == 0)
190                         goto out_nfs;
191         } while ((pmc = getprocmntdirbackward(mc->m.mnt_dir, pmc)) != NULL);
192
193         if (retval == -1) {
194 not_found:
195                 fprintf(stderr, "%s was not found in %s\n",
196                         mc->m.mnt_dir, MOUNTSFILE);
197                 goto out;
198         }
199
200 out_nfs4:
201         if (verbose)
202                 fprintf(stderr, "NFSv4 mount point detected\n");
203         retval = 1;
204         goto out;
205
206 out_nfs:
207         if (verbose)
208                 fprintf(stderr, "Legacy NFS mount point detected\n");
209         retval = 0;
210
211 out:
212         return retval;
213 }
214
215 static struct option umount_longopts[] =
216 {
217   { "force", 0, 0, 'f' },
218   { "help", 0, 0, 'h' },
219   { "no-mtab", 0, 0, 'n' },
220   { "verbose", 0, 0, 'v' },
221   { "read-only", 0, 0, 'r' },
222   { NULL, 0, 0, 0 }
223 };
224
225 int nfsumount(int argc, char *argv[])
226 {
227         int c, ret;
228         char *spec;
229         struct mntentchn *mc;
230
231         if (argc < 2) {
232                 umount_usage();
233                 return EX_USAGE;
234         }
235
236         spec = argv[1];
237
238         argv += 1;
239         argc -= 1;
240
241         argv[0] = argv[-1]; /* So that getopt error messages are correct */
242         while ((c = getopt_long (argc, argv, "fvnrlh",
243                                 umount_longopts, NULL)) != -1) {
244
245                 switch (c) {
246                 case 'f':
247                         ++force;
248                         break;
249                 case 'v':
250                         ++verbose;
251                         break;
252                 case 'n':
253                         ++nomtab;
254                         break;
255                 case 'r':
256                         ++remount;
257                         break;
258                 case 'l':
259                         ++lazy;
260                         break;
261                 case 'h':
262                 default:
263                         umount_usage();
264                         return EX_USAGE;
265                 }
266         }
267         if (optind != argc) {
268                 umount_usage();
269                 return EX_USAGE;
270         }
271         
272         if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
273                 nfs_error(_("%s: %s: not found\n"), progname, spec);
274                 return EX_USAGE;
275         }
276
277         if (*spec == '/')
278                 mc = getmntdirbackward(spec, NULL);
279         else
280                 mc = getmntdevbackward(spec, NULL);
281         if (!mc && verbose)
282                 printf(_("Could not find %s in mtab\n"), spec);
283
284         if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
285             strcmp(mc->m.mnt_type, "nfs4") != 0) {
286                 nfs_error(_("%s: %s on %s is not an NFS filesystem"),
287                                 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
288                 return EX_USAGE;
289         }
290
291         if (getuid() != 0) {
292                 /* only permitted if "user=" or "users" is in mount options */
293                 if (!mc) {
294                         /* umount might call us twice.  The second time there will
295                          * be no entry in mtab and we should just exit quietly
296                          */
297                         return EX_SUCCESS;
298
299                 only_root:
300                         nfs_error(_("%s: You are not permitted to unmount %s"),
301                                         progname, spec);
302                         return EX_USAGE;
303                 }
304                 if (hasmntopt(&mc->m, "users") == NULL) {
305                         char *opt = hasmntopt(&mc->m, "user");
306                         struct passwd *pw;
307                         char *comma;
308                         size_t len;
309                         if (!opt)
310                                 goto only_root;
311                         if (opt[4] != '=')
312                                 goto only_root;
313                         comma = strchr(opt, ',');
314                         if (comma)
315                                 len = comma - (opt + 5);
316                         else
317                                 len = strlen(opt+5);
318                         pw = getpwuid(getuid());
319                         if (pw == NULL || strlen(pw->pw_name) != len
320                             || strncmp(pw->pw_name, opt+5, len) != 0)
321                                 goto only_root;
322                 }
323         }
324
325         ret = EX_SUCCESS;
326         if (mc) {
327                 if (!lazy) {
328                         switch (nfs_umount_is_vers4(mc)) {
329                         case 0:
330                                 /* We ignore the error from nfs_umount23.
331                                  * If the actual umount succeeds (in del_mtab),
332                                  * we don't want to signal an error, as that
333                                  * could cause /sbin/mount to retry!
334                                  */
335                                 nfs_umount23(mc->m.mnt_fsname, mc->m.mnt_opts);
336                                 break;
337                         case 1:
338                                 break;
339                         default:
340                                 return EX_FAIL;
341                         }
342                 }
343                 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
344         } else if (*spec != '/') {
345                 if (!lazy)
346                         ret = nfs_umount23(spec, "tcp,v3");
347         } else
348                 ret = del_mtab(NULL, spec);
349
350         return ret;
351 }