]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsumount.c
mount.nfs - Tidy up option parsing.
[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
38 #if !defined(MNT_FORCE)
39 /* dare not try to include <linux/mount.h> -- lots of errors */
40 #define MNT_FORCE 1
41 #endif
42
43 #if !defined(MNT_DETACH)
44 #define MNT_DETACH 2
45 #endif
46
47 extern char *progname;
48 extern int nfs_mount_version;
49 extern int nomtab;
50 extern int verbose;
51 int force;
52 int lazy;
53 int remount;
54
55 extern int find_kernel_nfs_mount_version(void);
56 extern int probe_mntport(clnt_addr_t *);
57 extern int nfs_gethostbyname(const char *, struct sockaddr_in *);
58
59 static inline enum clnt_stat
60 nfs_umount(dirpath *argp, CLIENT *clnt)
61 {
62         return clnt_call(clnt, MOUNTPROC_UMNT,
63                          (xdrproc_t) xdr_dirpath, (caddr_t)argp,
64                          (xdrproc_t) xdr_void, NULL,
65                          TIMEOUT);
66 }
67
68 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
69 {
70         CLIENT *clnt;
71         enum clnt_stat res = 0;
72         int msock;
73
74         switch (mnt_server->pmap.pm_vers) {
75         case 3:
76         case 2:
77         case 1:
78                 if (!probe_mntport(mnt_server))
79                         goto out_bad;
80                 clnt = mnt_openclnt(mnt_server, &msock);
81                 if (!clnt)
82                         goto out_bad;
83                 res = nfs_umount(argp, clnt);
84                 mnt_closeclnt(clnt, msock);
85                 if (res == RPC_SUCCESS)
86                         return 1;
87                 break;
88         default:
89                 res = 1;
90                 break;
91         }
92  out_bad:
93         return res;
94 }
95
96 /* complain about a failed umount */
97 static void complain(int err, const char *dev) {
98   switch (err) {
99     case ENXIO:
100       nfs_error (_("umount: %s: invalid block device"), dev); break;
101     case EINVAL:
102       nfs_error (_("umount: %s: not mounted"), dev); break;
103     case EIO:
104       nfs_error (_("umount: %s: can't write superblock"), dev); break;
105     case EBUSY:
106      /* Let us hope fstab has a line "proc /proc ..."
107         and not "none /proc ..."*/
108       nfs_error (_("umount: %s: device is busy"), dev); break;
109     case ENOENT:
110       nfs_error (_("umount: %s: not found"), dev); break;
111     case EPERM:
112       nfs_error (_("umount: %s: must be superuser to umount"), dev); break;
113     case EACCES:
114       nfs_error (_("umount: %s: block devices not permitted on fs"), dev); break;
115     default:
116       nfs_error (_("umount: %s: %s"), dev, strerror (err)); break;
117   }
118 }
119
120 int del_mtab(const char *spec, const char *node)
121 {
122         int umnt_err, res;
123
124         umnt_err = 0;
125         if (lazy) {
126                 res = umount2 (node, MNT_DETACH);
127                 if (res < 0)
128                         umnt_err = errno;
129                 goto writemtab;
130         }
131
132         if (force) {
133                 res = umount2 (node, MNT_FORCE);
134                 if (res == -1) {
135                         int errsv = errno;
136                         perror("umount2");
137                         errno = errsv;
138                         if (errno == ENOSYS) {
139                                 if (verbose)
140                                         printf(_("no umount2, trying umount...\n"));
141                                 res = umount (node);
142                         }
143                 }
144         } else
145                 res = umount (node);
146
147         if (res < 0 && remount && errno == EBUSY && spec) {
148                 /* Umount failed - let us try a remount */
149                 res = mount(spec, node, NULL,
150                             MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
151                 if (res == 0) {
152                         struct mntent remnt;
153                         fprintf(stderr,
154                                 _("umount: %s busy - remounted read-only\n"),
155                                 spec);
156                         remnt.mnt_type = remnt.mnt_fsname = NULL;
157                         remnt.mnt_dir = xstrdup(node);
158                         remnt.mnt_opts = xstrdup("ro");
159                         if (!nomtab)
160                                 update_mtab(node, &remnt);
161                         return 0;
162                 } else if (errno != EBUSY) {    /* hmm ... */
163                         perror("remount");
164                         fprintf(stderr,
165                                 _("umount: could not remount %s read-only\n"),
166                                 spec);
167                 }
168         }
169
170         if (res >= 0) {
171                 /* Umount succeeded */
172                 if (verbose)
173                         printf (_("%s umounted\n"), spec ? spec : node);
174         }
175
176  writemtab:
177         if (!nomtab &&
178             (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
179                update_mtab(node, NULL);
180         }
181
182         if (res >= 0)
183                 return 0;
184
185         if (umnt_err)
186                 complain(umnt_err, node);
187         return 1;
188 }
189
190 /*
191  * Returns 1 if everything went well, else 0.
192  */
193 int _nfsumount(const char *spec, char *opts)
194 {
195         char *hostname;
196         char *dirname;
197         clnt_addr_t mnt_server = { &hostname, };
198         struct mntent mnt = { .mnt_opts = opts };
199         struct pmap *pmap = &mnt_server.pmap;
200         char *p;
201
202         nfs_mount_version = find_kernel_nfs_mount_version();
203         if (spec == NULL || (p = strchr(spec,':')) == NULL)
204                 goto out_bad;
205         hostname = xstrndup(spec, p-spec);
206         dirname = xstrdup(p+1);
207 #ifdef NFS_MOUNT_DEBUG
208         printf(_("host: %s, directory: %s\n"), hostname, dirname);
209 #endif
210
211         if (opts && (p = strstr(opts, "addr="))) {
212                 char *q;
213
214                 free(hostname);
215                 p += 5;
216                 q = p;
217                 while (*q && *q != ',') q++;
218                 hostname = xstrndup(p,q-p);
219         }
220
221         if (opts && (p = strstr(opts, "mounthost="))) {
222                 char *q;
223
224                 free(hostname);
225                 p += 10;
226                 q = p;
227                 while (*q && *q != ',') q++;
228                 hostname = xstrndup(p,q-p);
229         }
230
231         pmap->pm_prog = MOUNTPROG;
232         pmap->pm_vers = MOUNTVERS_NFSV3;
233         pmap->pm_prot = IPPROTO_TCP;
234         if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
235                 pmap->pm_prog = atoi(p+10);
236         if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
237                 pmap->pm_port = atoi(p+10);
238         if (opts && hasmntopt(&mnt, "v2"))
239                 pmap->pm_vers = nfsvers_to_mnt(2);
240         if (opts && hasmntopt(&mnt, "v3"))
241                 pmap->pm_vers = nfsvers_to_mnt(3);
242         if (opts && hasmntopt(&mnt, "v4"))
243                 pmap->pm_vers = nfsvers_to_mnt(4);
244         if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
245                 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
246         if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
247                 pmap->pm_vers = atoi(p+10);
248         if (opts && (hasmntopt(&mnt, "udp") || hasmntopt(&mnt, "proto=udp")))
249                 pmap->pm_prot = IPPROTO_UDP;
250
251         if (!nfs_gethostbyname(hostname, &mnt_server.saddr))
252                 goto out_bad;
253         return nfs_call_umount(&mnt_server, &dirname);
254  out_bad:
255         fprintf(stderr, "%s: %s: not found or not mounted\n", progname, spec);
256         return 0;
257 }
258
259 static struct option umount_longopts[] =
260 {
261   { "force", 0, 0, 'f' },
262   { "help", 0, 0, 'h' },
263   { "no-mtab", 0, 0, 'n' },
264   { "verbose", 0, 0, 'v' },
265   { "read-only", 0, 0, 'r' },
266   { NULL, 0, 0, 0 }
267 };
268
269 void umount_usage()
270 {
271         printf("usage: %s dir [-fvnrlh]\n", progname);
272         printf("options:\n\t-f\t\tforce unmount\n");
273         printf("\t-v\t\tverbose\n");
274         printf("\t-n\t\tDo not update /etc/mtab\n");
275         printf("\t-r\t\tremount\n");
276         printf("\t-l\t\tlazy unmount\n");
277         printf("\t-h\t\tprint this help\n\n");
278 }
279
280 int nfsumount(int argc, char *argv[])
281 {
282         int c, ret;
283         char *spec;
284         struct mntentchn *mc;
285
286         spec = argv[1];
287
288         argv += 1;
289         argc -= 1;
290
291         while ((c = getopt_long (argc, argv, "fvnrlh",
292                                 umount_longopts, NULL)) != -1) {
293
294                 switch (c) {
295                 case 'f':
296                         ++force;
297                         break;
298                 case 'v':
299                         ++verbose;
300                         break;
301                 case 'n':
302                         ++nomtab;
303                         break;
304                 case 'r':
305                         ++remount;
306                         break;
307                 case 'l':
308                         ++lazy;
309                         break;
310                 case 'h':
311                 default:
312                         umount_usage();
313                         return 0;
314                 }
315         }
316         
317         if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
318                 printf(_("umount: %s: not found\n"), spec);
319                 return 0;
320         }
321
322         if (*spec == '/')
323                 mc = getmntdirbackward(spec, NULL);
324         else
325                 mc = getmntdevbackward(spec, NULL);
326         if (!mc && verbose)
327                 printf(_("Could not find %s in mtab\n"), spec);
328
329         if (getuid() != 0) {
330                 /* only permitted if "user=" or "users" is in mount options */
331                 if (!mc) {
332                 only_root:
333                         fprintf(stderr,"%s: You are not permitted to unmount %s\n",
334                                 progname, spec);
335                         return 0;
336                 }
337                 if (hasmntopt(&mc->m, "users") == NULL) {
338                         char *opt = hasmntopt(&mc->m, "user");
339                         struct passwd *pw;
340                         char *comma;
341                         int len;
342                         if (!opt)
343                                 goto only_root;
344                         if (opt[5] != '=')
345                                 goto only_root;
346                         comma = strchr(opt, ',');
347                         if (comma)
348                                 len = comma - (opt + 5);
349                         else
350                                 len = strlen(opt+5);
351                         pw = getpwuid(getuid());
352                         if (pw == NULL || strlen(pw->pw_name) != len
353                             || strncmp(pw->pw_name, opt+5, len) != 0)
354                                 goto only_root;
355                 }
356         }
357
358         ret = 0;
359         if (mc) {
360                 if (!lazy)
361                         ret = _nfsumount(mc->m.mnt_fsname, mc->m.mnt_opts);
362                 if(ret)
363                         ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
364         } else if (*spec != '/') {
365                 if (!lazy)
366                         ret = _nfsumount(spec, "tcp,v3");
367         } else
368                 ret = del_mtab(NULL, spec);
369
370         return(ret);
371 }
372