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