]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsumount.c
mount: move generic functions to utils.c and network.c
[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         char buffer[LINELEN], *next;
155         int retval;
156         FILE *f;
157
158         if ((f = fopen(MOUNTSFILE, "r")) == NULL) {
159                 fprintf(stderr, "%s: %s\n",
160                         MOUNTSFILE, strerror(errno));
161                 return -1;
162         }
163
164         retval = -1;
165         while (fgets(buffer, sizeof(buffer), f) != NULL) {
166                 char *device, *mntdir, *type, *flags;
167                 struct mount_options *options;
168                 char *line = buffer;
169
170                 next = strchr(line, '\n');
171                 if (next != NULL)
172                         *next = '\0';
173
174                 device = strtok(line, " \t");
175                 if (device == NULL)
176                         continue;
177                 mntdir = strtok(NULL, " \t");
178                 if (mntdir == NULL)
179                         continue;
180                 if (strcmp(device, mc->m.mnt_fsname) != 0 &&
181                     strcmp(mntdir, mc->m.mnt_dir) != 0)
182                         continue;
183
184                 type = strtok(NULL, " \t");
185                 if (type == NULL)
186                         continue;
187                 if (strcmp(type, "nfs4") == 0)
188                         goto out_nfs4;
189
190                 flags = strtok(NULL, " \t");
191                 if (flags == NULL)
192                         continue;
193                 options = po_split(flags);
194                 if (options != NULL) {
195                         unsigned long version;
196                         int rc;
197
198                         rc = nfs_nfs_version(options, &version);
199                         po_destroy(options);
200                         if (rc && version == 4)
201                                 goto out_nfs4;
202                 }
203
204                 goto out_nfs;
205         }
206         if (retval == -1)
207                 fprintf(stderr, "%s was not found in %s\n",
208                         mc->m.mnt_dir, MOUNTSFILE);
209
210 out:
211         fclose(f);
212         return retval;
213
214 out_nfs4:
215         if (verbose)
216                 fprintf(stderr, "NFSv4 mount point detected\n");
217         retval = 1;
218         goto out;
219
220 out_nfs:
221         if (verbose)
222                 fprintf(stderr, "Legacy NFS mount point detected\n");
223         retval = 0;
224         goto out;
225 }
226
227 static struct option umount_longopts[] =
228 {
229   { "force", 0, 0, 'f' },
230   { "help", 0, 0, 'h' },
231   { "no-mtab", 0, 0, 'n' },
232   { "verbose", 0, 0, 'v' },
233   { "read-only", 0, 0, 'r' },
234   { NULL, 0, 0, 0 }
235 };
236
237 int nfsumount(int argc, char *argv[])
238 {
239         int c, ret;
240         char *spec;
241         struct mntentchn *mc;
242
243         if (argc < 2) {
244                 umount_usage();
245                 return EX_USAGE;
246         }
247
248         spec = argv[1];
249
250         argv += 1;
251         argc -= 1;
252
253         argv[0] = argv[-1]; /* So that getopt error messages are correct */
254         while ((c = getopt_long (argc, argv, "fvnrlh",
255                                 umount_longopts, NULL)) != -1) {
256
257                 switch (c) {
258                 case 'f':
259                         ++force;
260                         break;
261                 case 'v':
262                         ++verbose;
263                         break;
264                 case 'n':
265                         ++nomtab;
266                         break;
267                 case 'r':
268                         ++remount;
269                         break;
270                 case 'l':
271                         ++lazy;
272                         break;
273                 case 'h':
274                 default:
275                         umount_usage();
276                         return EX_USAGE;
277                 }
278         }
279         if (optind != argc) {
280                 umount_usage();
281                 return EX_USAGE;
282         }
283         
284         if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
285                 nfs_error(_("%s: %s: not found\n"), progname, spec);
286                 return EX_USAGE;
287         }
288
289         if (*spec == '/')
290                 mc = getmntdirbackward(spec, NULL);
291         else
292                 mc = getmntdevbackward(spec, NULL);
293         if (!mc && verbose)
294                 printf(_("Could not find %s in mtab\n"), spec);
295
296         if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
297             strcmp(mc->m.mnt_type, "nfs4") != 0) {
298                 nfs_error(_("%s: %s on %s is not an NFS filesystem"),
299                                 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
300                 return EX_USAGE;
301         }
302
303         if (getuid() != 0) {
304                 /* only permitted if "user=" or "users" is in mount options */
305                 if (!mc) {
306                         /* umount might call us twice.  The second time there will
307                          * be no entry in mtab and we should just exit quietly
308                          */
309                         return EX_SUCCESS;
310
311                 only_root:
312                         nfs_error(_("%s: You are not permitted to unmount %s"),
313                                         progname, spec);
314                         return EX_USAGE;
315                 }
316                 if (hasmntopt(&mc->m, "users") == NULL) {
317                         char *opt = hasmntopt(&mc->m, "user");
318                         struct passwd *pw;
319                         char *comma;
320                         size_t len;
321                         if (!opt)
322                                 goto only_root;
323                         if (opt[4] != '=')
324                                 goto only_root;
325                         comma = strchr(opt, ',');
326                         if (comma)
327                                 len = comma - (opt + 5);
328                         else
329                                 len = strlen(opt+5);
330                         pw = getpwuid(getuid());
331                         if (pw == NULL || strlen(pw->pw_name) != len
332                             || strncmp(pw->pw_name, opt+5, len) != 0)
333                                 goto only_root;
334                 }
335         }
336
337         ret = EX_SUCCESS;
338         if (mc) {
339                 if (!lazy) {
340                         switch (nfs_umount_is_vers4(mc)) {
341                         case 0:
342                                 /* We ignore the error from nfs_umount23.
343                                  * If the actual umount succeeds (in del_mtab),
344                                  * we don't want to signal an error, as that
345                                  * could cause /sbin/mount to retry!
346                                  */
347                                 nfs_umount23(mc->m.mnt_fsname, mc->m.mnt_opts);
348                                 break;
349                         case 1:
350                                 break;
351                         default:
352                                 return EX_FAIL;
353                         }
354                 }
355                 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
356         } else if (*spec != '/') {
357                 if (!lazy)
358                         ret = nfs_umount23(spec, "tcp,v3");
359         } else
360                 ret = del_mtab(NULL, spec);
361
362         return ret;
363 }