]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsumount.c
umount.nfs: make _nfsumount() static
[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 /*
125  * Returns 1 if everything went well, else 0.
126  */
127 static int do_nfs_umount(const char *spec, char *opts)
128 {
129         char *hostname;
130         char *dirname;
131         clnt_addr_t mnt_server = { &hostname, };
132         struct mntent mnt = { .mnt_opts = opts };
133         struct pmap *pmap = &mnt_server.pmap;
134         char *p;
135
136         if (spec == NULL || (p = strchr(spec,':')) == NULL)
137                 goto out_bad;
138         hostname = xstrndup(spec, p-spec);
139         dirname = xstrdup(p+1);
140 #ifdef NFS_MOUNT_DEBUG
141         printf(_("host: %s, directory: %s\n"), hostname, dirname);
142 #endif
143
144         if (opts && (p = strstr(opts, "addr="))) {
145                 char *q;
146
147                 free(hostname);
148                 p += 5;
149                 q = p;
150                 while (*q && *q != ',') q++;
151                 hostname = xstrndup(p,q-p);
152         }
153
154         if (opts && (p = strstr(opts, "mounthost="))) {
155                 char *q;
156
157                 free(hostname);
158                 p += 10;
159                 q = p;
160                 while (*q && *q != ',') q++;
161                 hostname = xstrndup(p,q-p);
162         }
163
164         pmap->pm_prog = MOUNTPROG;
165         pmap->pm_vers = MOUNTVERS_NFSV3;
166         if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
167                 pmap->pm_prog = atoi(p+10);
168         if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
169                 pmap->pm_port = atoi(p+10);
170         if (opts && hasmntopt(&mnt, "v2"))
171                 pmap->pm_vers = nfsvers_to_mnt(2);
172         if (opts && hasmntopt(&mnt, "v3"))
173                 pmap->pm_vers = nfsvers_to_mnt(3);
174         if (opts && hasmntopt(&mnt, "v4"))
175                 pmap->pm_vers = nfsvers_to_mnt(4);
176         if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
177                 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
178         if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
179                 pmap->pm_vers = atoi(p+10);
180         if (opts && (hasmntopt(&mnt, "udp") || hasmntopt(&mnt, "proto=udp")))
181                 pmap->pm_prot = IPPROTO_UDP;
182
183         if (!nfs_gethostbyname(hostname, &mnt_server.saddr))
184                 goto out_bad;
185         return nfs_call_umount(&mnt_server, &dirname);
186  out_bad:
187         nfs_error(_("%s: %s: not found or not mounted"), progname, spec);
188         return 0;
189 }
190
191 static struct option umount_longopts[] =
192 {
193   { "force", 0, 0, 'f' },
194   { "help", 0, 0, 'h' },
195   { "no-mtab", 0, 0, 'n' },
196   { "verbose", 0, 0, 'v' },
197   { "read-only", 0, 0, 'r' },
198   { NULL, 0, 0, 0 }
199 };
200
201 static void umount_usage(void)
202 {
203         printf("usage: %s dir [-fvnrlh]\n", progname);
204         printf("options:\n\t-f\t\tforce unmount\n");
205         printf("\t-v\t\tverbose\n");
206         printf("\t-n\t\tDo not update /etc/mtab\n");
207         printf("\t-r\t\tremount\n");
208         printf("\t-l\t\tlazy unmount\n");
209         printf("\t-h\t\tprint this help\n\n");
210 }
211
212 int nfsumount(int argc, char *argv[])
213 {
214         int c, ret;
215         char *spec;
216         struct mntentchn *mc;
217
218         if (argc < 2) {
219                 umount_usage();
220                 return EX_USAGE;
221         }
222
223         spec = argv[1];
224
225         argv += 1;
226         argc -= 1;
227
228         argv[0] = argv[-1]; /* So that getopt error messages are correct */
229         while ((c = getopt_long (argc, argv, "fvnrlh",
230                                 umount_longopts, NULL)) != -1) {
231
232                 switch (c) {
233                 case 'f':
234                         ++force;
235                         break;
236                 case 'v':
237                         ++verbose;
238                         break;
239                 case 'n':
240                         ++nomtab;
241                         break;
242                 case 'r':
243                         ++remount;
244                         break;
245                 case 'l':
246                         ++lazy;
247                         break;
248                 case 'h':
249                 default:
250                         umount_usage();
251                         return EX_USAGE;
252                 }
253         }
254         if (optind != argc) {
255                 umount_usage();
256                 return EX_USAGE;
257         }
258         
259         if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
260                 nfs_error(_("%s: %s: not found\n"), progname, spec);
261                 return EX_USAGE;
262         }
263
264         if (*spec == '/')
265                 mc = getmntdirbackward(spec, NULL);
266         else
267                 mc = getmntdevbackward(spec, NULL);
268         if (!mc && verbose)
269                 printf(_("Could not find %s in mtab\n"), spec);
270
271         if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
272             strcmp(mc->m.mnt_type, "nfs4") != 0) {
273                 nfs_error(_("%s: %s on %s it not an nfs filesystem"),
274                                 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
275                 return EX_USAGE;
276         }
277
278         if (getuid() != 0) {
279                 /* only permitted if "user=" or "users" is in mount options */
280                 if (!mc) {
281                         /* umount might call us twice.  The second time there will
282                          * be no entry in mtab and we should just exit quietly
283                          */
284                         return 0;
285
286                 only_root:
287                         nfs_error(_("%s: You are not permitted to unmount %s"),
288                                         progname, spec);
289                         return EX_USAGE;
290                 }
291                 if (hasmntopt(&mc->m, "users") == NULL) {
292                         char *opt = hasmntopt(&mc->m, "user");
293                         struct passwd *pw;
294                         char *comma;
295                         int len;
296                         if (!opt)
297                                 goto only_root;
298                         if (opt[4] != '=')
299                                 goto only_root;
300                         comma = strchr(opt, ',');
301                         if (comma)
302                                 len = comma - (opt + 5);
303                         else
304                                 len = strlen(opt+5);
305                         pw = getpwuid(getuid());
306                         if (pw == NULL || strlen(pw->pw_name) != len
307                             || strncmp(pw->pw_name, opt+5, len) != 0)
308                                 goto only_root;
309                 }
310         }
311
312         ret = 0;
313         if (mc) {
314                 if (!lazy)
315                         do_nfs_umount(mc->m.mnt_fsname, mc->m.mnt_opts);
316                 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
317         } else if (*spec != '/') {
318                 if (!lazy)
319                         do_nfs_umount(spec, "tcp,v3");
320         } else
321                 ret = del_mtab(NULL, spec);
322
323         return ret;
324 }