]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsumount.c
umount.nfs: eliminate a nearly empty header file.
[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 "error.h"
37 #include "network.h"
38
39 #if !defined(MNT_FORCE)
40 /* dare not try to include <linux/mount.h> -- lots of errors */
41 #define MNT_FORCE 1
42 #endif
43
44 #if !defined(MNT_DETACH)
45 #define MNT_DETACH 2
46 #endif
47
48 extern char *progname;
49 extern int nomtab;
50 extern int verbose;
51 int force;
52 int lazy;
53 int remount;
54
55 static int del_mtab(const char *spec, const char *node)
56 {
57         int umnt_err, res;
58
59         umnt_err = 0;
60         if (lazy) {
61                 res = umount2 (node, MNT_DETACH);
62                 if (res < 0)
63                         umnt_err = errno;
64                 goto writemtab;
65         }
66
67         if (force) {
68                 res = umount2 (node, MNT_FORCE);
69                 if (res == -1) {
70                         int errsv = errno;
71                         perror("umount2");
72                         errno = errsv;
73                         if (errno == ENOSYS) {
74                                 if (verbose)
75                                         printf(_("no umount2, trying umount...\n"));
76                                 res = umount (node);
77                         }
78                 }
79         } else
80                 res = umount (node);
81
82         if (res < 0 && remount && errno == EBUSY && spec) {
83                 /* Umount failed - let us try a remount */
84                 res = mount(spec, node, NULL,
85                             MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
86                 if (res == 0) {
87                         struct mntent remnt;
88                         nfs_error(_("%s: %s busy - remounted read-only"),
89                                         progname, spec);
90                         remnt.mnt_type = remnt.mnt_fsname = NULL;
91                         remnt.mnt_dir = xstrdup(node);
92                         remnt.mnt_opts = xstrdup("ro");
93                         if (!nomtab)
94                                 update_mtab(node, &remnt);
95                         return 0;
96                 } else if (errno != EBUSY) {    /* hmm ... */
97                         perror("remount");
98                         nfs_error(_("%s: could not remount %s read-only"),
99                                         progname, spec);
100                 }
101         }
102
103         if (res >= 0) {
104                 /* Umount succeeded */
105                 if (verbose)
106                         printf (_("%s umounted\n"), spec ? spec : node);
107         }
108
109  writemtab:
110         if (!nomtab &&
111             (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
112                update_mtab(node, NULL);
113         }
114
115         if (res >= 0)
116                 return 0;
117
118         if (umnt_err)
119                 umount_error(umnt_err, node);
120         return EX_FILEIO;
121 }
122
123 static int do_nfs_umount(const char *spec, char *opts)
124 {
125         char *hostname;
126         char *dirname;
127         clnt_addr_t mnt_server = { &hostname, };
128         struct mntent mnt = { .mnt_opts = opts };
129         struct pmap *pmap = &mnt_server.pmap;
130         char *p;
131
132         if (spec == NULL) {
133                 nfs_error(_("%s: No NFS export name was provided"),
134                                 progname);
135                 return EX_USAGE;
136         }
137         
138         p = strchr(spec, ':');
139         if (p == NULL) {
140                 nfs_error(_("%s: '%s' is not a legal NFS export name"),
141                                 progname, spec);
142                 return EX_USAGE;
143         }
144         hostname = xstrndup(spec, p - spec);
145         dirname = xstrdup(p + 1);
146 #ifdef NFS_MOUNT_DEBUG
147         printf(_("host: %s, directory: %s\n"), hostname, dirname);
148 #endif
149
150         if (opts && (p = strstr(opts, "addr="))) {
151                 char *q;
152
153                 free(hostname);
154                 p += 5;
155                 q = p;
156                 while (*q && *q != ',') q++;
157                 hostname = xstrndup(p,q-p);
158         }
159
160         if (opts && (p = strstr(opts, "mounthost="))) {
161                 char *q;
162
163                 free(hostname);
164                 p += 10;
165                 q = p;
166                 while (*q && *q != ',') q++;
167                 hostname = xstrndup(p,q-p);
168         }
169
170         pmap->pm_prog = MOUNTPROG;
171         pmap->pm_vers = MOUNTVERS_NFSV3;
172         if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
173                 pmap->pm_prog = atoi(p+10);
174         if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
175                 pmap->pm_port = atoi(p+10);
176         if (opts && hasmntopt(&mnt, "v2"))
177                 pmap->pm_vers = nfsvers_to_mnt(2);
178         if (opts && hasmntopt(&mnt, "v3"))
179                 pmap->pm_vers = nfsvers_to_mnt(3);
180         if (opts && hasmntopt(&mnt, "v4"))
181                 pmap->pm_vers = nfsvers_to_mnt(4);
182         if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
183                 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
184         if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
185                 pmap->pm_vers = atoi(p+10);
186         if (opts && (hasmntopt(&mnt, "udp") || hasmntopt(&mnt, "proto=udp")))
187                 pmap->pm_prot = IPPROTO_UDP;
188
189         if (!nfs_gethostbyname(hostname, &mnt_server.saddr)) {
190                 nfs_error(_("%s: '%s' does not contain a recognized hostname"),
191                                 progname, spec);
192                 return EX_USAGE;
193         }
194
195         if (!nfs_call_umount(&mnt_server, &dirname)) {
196                 nfs_error(_("%s: Server failed to unmount '%s'"),
197                                 progname, spec);
198                 return EX_USAGE;
199         }
200
201         return 0;
202 }
203
204 static struct option umount_longopts[] =
205 {
206   { "force", 0, 0, 'f' },
207   { "help", 0, 0, 'h' },
208   { "no-mtab", 0, 0, 'n' },
209   { "verbose", 0, 0, 'v' },
210   { "read-only", 0, 0, 'r' },
211   { NULL, 0, 0, 0 }
212 };
213
214 static void umount_usage(void)
215 {
216         printf("usage: %s dir [-fvnrlh]\n", progname);
217         printf("options:\n\t-f\t\tforce unmount\n");
218         printf("\t-v\t\tverbose\n");
219         printf("\t-n\t\tDo not update /etc/mtab\n");
220         printf("\t-r\t\tremount\n");
221         printf("\t-l\t\tlazy unmount\n");
222         printf("\t-h\t\tprint this help\n\n");
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 it 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 0;
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                         int 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 = 0;
326         if (mc) {
327                 if (!lazy)
328                         ret = do_nfs_umount(mc->m.mnt_fsname, mc->m.mnt_opts);
329                 if (!ret || force)
330                         ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
331         } else if (*spec != '/') {
332                 if (!lazy)
333                         ret = do_nfs_umount(spec, "tcp,v3");
334         } else
335                 ret = del_mtab(NULL, spec);
336
337         return ret;
338 }