Deleted Added
full compact
umount.c (201135) umount.c (201252)
1/*-
2 * Copyright (c) 1980, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef lint
31static const char copyright[] =
32"@(#) Copyright (c) 1980, 1989, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
34#endif /* not lint */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)umount.c 8.8 (Berkeley) 5/8/95";
39#endif
40static const char rcsid[] =
1/*-
2 * Copyright (c) 1980, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef lint
31static const char copyright[] =
32"@(#) Copyright (c) 1980, 1989, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
34#endif /* not lint */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)umount.c 8.8 (Berkeley) 5/8/95";
39#endif
40static const char rcsid[] =
41 "$FreeBSD: head/sbin/umount/umount.c 201135 2009-12-28 17:57:37Z delphij $";
41 "$FreeBSD: head/sbin/umount/umount.c 201252 2009-12-30 06:36:42Z ed $";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/mount.h>
46#include <sys/socket.h>
47#include <sys/stat.h>
48
49#include <netdb.h>
50#include <rpc/rpc.h>
51#include <rpcsvc/mount.h>
52
53#include <ctype.h>
54#include <err.h>
55#include <errno.h>
56#include <fstab.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62#include "mounttab.h"
63
64typedef enum { FIND, REMOVE, CHECKUNIQUE } dowhat;
65
66struct addrinfo *nfshost_ai = NULL;
67int fflag, vflag;
68char *nfshost;
69
70struct statfs *checkmntlist(char *);
71int checkvfsname (const char *, char **);
72struct statfs *getmntentry(const char *fromname, const char *onname,
73 fsid_t *fsid, dowhat what);
74char **makevfslist (const char *);
75size_t mntinfo (struct statfs **);
76int namematch (struct addrinfo *);
77int parsehexfsid(const char *hex, fsid_t *fsid);
78int sacmp (void *, void *);
79int umountall (char **);
80int checkname (char *, char **);
81int umountfs(struct statfs *sfs);
82void usage (void);
83int xdr_dir (XDR *, char *);
84
85int
86main(int argc, char *argv[])
87{
88 int all, errs, ch, mntsize, error;
89 char **typelist = NULL;
90 struct statfs *mntbuf, *sfs;
91 struct addrinfo hints;
92
93 /* Start disks transferring immediately. */
94 sync();
95
96 all = errs = 0;
97 while ((ch = getopt(argc, argv, "AaF:fh:t:v")) != -1)
98 switch (ch) {
99 case 'A':
100 all = 2;
101 break;
102 case 'a':
103 all = 1;
104 break;
105 case 'F':
106 setfstab(optarg);
107 break;
108 case 'f':
109 fflag = MNT_FORCE;
110 break;
111 case 'h': /* -h implies -A. */
112 all = 2;
113 nfshost = optarg;
114 break;
115 case 't':
116 if (typelist != NULL)
117 err(1, "only one -t option may be specified");
118 typelist = makevfslist(optarg);
119 break;
120 case 'v':
121 vflag = 1;
122 break;
123 default:
124 usage();
125 /* NOTREACHED */
126 }
127 argc -= optind;
128 argv += optind;
129
130 if ((argc == 0 && !all) || (argc != 0 && all))
131 usage();
132
133 /* -h implies "-t nfs" if no -t flag. */
134 if ((nfshost != NULL) && (typelist == NULL))
135 typelist = makevfslist("nfs");
136
137 if (nfshost != NULL) {
138 memset(&hints, 0, sizeof hints);
139 error = getaddrinfo(nfshost, NULL, &hints, &nfshost_ai);
140 if (error)
141 errx(1, "%s: %s", nfshost, gai_strerror(error));
142 }
143
144 switch (all) {
145 case 2:
146 if ((mntsize = mntinfo(&mntbuf)) <= 0)
147 break;
148 /*
149 * We unmount the nfs-mounts in the reverse order
150 * that they were mounted.
151 */
152 for (errs = 0, mntsize--; mntsize > 0; mntsize--) {
153 sfs = &mntbuf[mntsize];
154 if (checkvfsname(sfs->f_fstypename, typelist))
155 continue;
156 if (umountfs(sfs) != 0)
157 errs = 1;
158 }
159 free(mntbuf);
160 break;
161 case 1:
162 if (setfsent() == 0)
163 err(1, "%s", getfstab());
164 errs = umountall(typelist);
165 break;
166 case 0:
167 for (errs = 0; *argv != NULL; ++argv)
168 if (checkname(*argv, typelist) != 0)
169 errs = 1;
170 break;
171 }
172 exit(errs);
173}
174
175int
176umountall(char **typelist)
177{
178 struct xvfsconf vfc;
179 struct fstab *fs;
180 int rval;
181 char *cp;
182 static int firstcall = 1;
183
184 if ((fs = getfsent()) != NULL)
185 firstcall = 0;
186 else if (firstcall)
187 errx(1, "fstab reading failure");
188 else
189 return (0);
190 do {
191 /* Ignore the root. */
192 if (strcmp(fs->fs_file, "/") == 0)
193 continue;
194 /*
195 * !!!
196 * Historic practice: ignore unknown FSTAB_* fields.
197 */
198 if (strcmp(fs->fs_type, FSTAB_RW) &&
199 strcmp(fs->fs_type, FSTAB_RO) &&
200 strcmp(fs->fs_type, FSTAB_RQ))
201 continue;
202 /* Ignore unknown file system types. */
203 if (getvfsbyname(fs->fs_vfstype, &vfc) == -1)
204 continue;
205 if (checkvfsname(fs->fs_vfstype, typelist))
206 continue;
207
208 /*
209 * We want to unmount the file systems in the reverse order
210 * that they were mounted. So, we save off the file name
211 * in some allocated memory, and then call recursively.
212 */
213 if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
214 err(1, "malloc failed");
215 (void)strcpy(cp, fs->fs_file);
216 rval = umountall(typelist);
217 rval = checkname(cp, typelist) || rval;
218 free(cp);
219 return (rval);
220 } while ((fs = getfsent()) != NULL);
221 return (0);
222}
223
224/*
225 * Do magic checks on mountpoint/device/fsid, and then call unmount(2).
226 */
227int
228checkname(char *mntname, char **typelist)
229{
230 char buf[MAXPATHLEN];
231 struct statfs sfsbuf;
232 struct stat sb;
233 struct statfs *sfs;
234 char *delimp;
235 dev_t dev;
236 int len;
237
238 /*
239 * 1. Check if the name exists in the mounttable.
240 */
241 sfs = checkmntlist(mntname);
242 /*
243 * 2. Remove trailing slashes if there are any. After that
244 * we look up the name in the mounttable again.
245 */
246 if (sfs == NULL) {
247 len = strlen(mntname);
248 while (len > 1 && mntname[len - 1] == '/')
249 mntname[--len] = '\0';
250 sfs = checkmntlist(mntname);
251 }
252 /*
253 * 3. Check if the deprecated NFS syntax with an '@' has been used
254 * and translate it to the ':' syntax. Look up the name in the
255 * mount table again.
256 */
257 if (sfs == NULL && (delimp = strrchr(mntname, '@')) != NULL) {
258 snprintf(buf, sizeof(buf), "%s:%.*s", delimp + 1,
259 (int)(delimp - mntname), mntname);
260 len = strlen(buf);
261 while (len > 1 && buf[len - 1] == '/')
262 buf[--len] = '\0';
263 sfs = checkmntlist(buf);
264 }
265 /*
266 * 4. Resort to a statfs(2) call. This is the last check so that
267 * hung NFS filesystems for example can be unmounted without
268 * potentially blocking forever in statfs() as long as the
269 * filesystem is specified unambiguously. This covers all the
270 * hard cases such as symlinks and mismatches between the
271 * mount list and reality.
272 * We also do this if an ambiguous mount point was specified.
273 */
274 if (sfs == NULL || (getmntentry(NULL, mntname, NULL, FIND) != NULL &&
275 getmntentry(NULL, mntname, NULL, CHECKUNIQUE) == NULL)) {
276 if (statfs(mntname, &sfsbuf) != 0) {
277 warn("%s: statfs", mntname);
278 } else if (stat(mntname, &sb) != 0) {
279 warn("%s: stat", mntname);
280 } else if (S_ISDIR(sb.st_mode)) {
281 /* Check that `mntname' is the root directory. */
282 dev = sb.st_dev;
283 snprintf(buf, sizeof(buf), "%s/..", mntname);
284 if (stat(buf, &sb) != 0) {
285 warn("%s: stat", buf);
286 } else if (sb.st_dev == dev) {
287 warnx("%s: not a file system root directory",
288 mntname);
289 return (1);
290 } else
291 sfs = &sfsbuf;
292 }
293 }
294 if (sfs == NULL) {
295 warnx("%s: unknown file system", mntname);
296 return (1);
297 }
298 if (checkvfsname(sfs->f_fstypename, typelist))
299 return (1);
300 return (umountfs(sfs));
301}
302
303/*
304 * NFS stuff and unmount(2) call
305 */
306int
307umountfs(struct statfs *sfs)
308{
309 char fsidbuf[64];
310 enum clnt_stat clnt_stat;
311 struct timeval try;
312 struct addrinfo *ai, hints;
313 int do_rpc;
314 CLIENT *clp;
315 char *nfsdirname, *orignfsdirname;
316 char *hostp, *delimp;
317
318 ai = NULL;
319 do_rpc = 0;
320 hostp = NULL;
321 nfsdirname = delimp = orignfsdirname = NULL;
322 memset(&hints, 0, sizeof hints);
323
324 if (strcmp(sfs->f_fstypename, "nfs") == 0) {
325 if ((nfsdirname = strdup(sfs->f_mntfromname)) == NULL)
326 err(1, "strdup");
327 orignfsdirname = nfsdirname;
328 if ((delimp = strrchr(nfsdirname, ':')) != NULL) {
329 *delimp = '\0';
330 hostp = nfsdirname;
331 getaddrinfo(hostp, NULL, &hints, &ai);
332 if (ai == NULL) {
333 warnx("can't get net id for host");
334 }
335 nfsdirname = delimp + 1;
336 }
337
338 /*
339 * Check if we have to start the rpc-call later.
340 * If there are still identical nfs-names mounted,
341 * we skip the rpc-call. Obviously this has to
342 * happen before unmount(2), but it should happen
343 * after the previous namecheck.
344 * A non-NULL return means that this is the last
345 * mount from mntfromname that is still mounted.
346 */
347 if (getmntentry(sfs->f_mntfromname, NULL, NULL,
348 CHECKUNIQUE) != NULL)
349 do_rpc = 1;
350 }
351
352 if (!namematch(ai))
353 return (1);
354 /* First try to unmount using the file system ID. */
355 snprintf(fsidbuf, sizeof(fsidbuf), "FSID:%d:%d", sfs->f_fsid.val[0],
356 sfs->f_fsid.val[1]);
357 if (unmount(fsidbuf, fflag | MNT_BYFSID) != 0) {
358 /* XXX, non-root users get a zero fsid, so don't warn. */
359 if (errno != ENOENT || sfs->f_fsid.val[0] != 0 ||
360 sfs->f_fsid.val[1] != 0)
361 warn("unmount of %s failed", sfs->f_mntonname);
362 if (errno != ENOENT)
363 return (1);
364 /* Compatibility for old kernels. */
365 if (sfs->f_fsid.val[0] != 0 || sfs->f_fsid.val[1] != 0)
366 warnx("retrying using path instead of file system ID");
367 if (unmount(sfs->f_mntonname, fflag) != 0) {
368 warn("unmount of %s failed", sfs->f_mntonname);
369 return (1);
370 }
371 }
372 /* Mark this this file system as unmounted. */
373 getmntentry(NULL, NULL, &sfs->f_fsid, REMOVE);
374 if (vflag)
375 (void)printf("%s: unmount from %s\n", sfs->f_mntfromname,
376 sfs->f_mntonname);
377 /*
378 * Report to mountd-server which nfsname
379 * has been unmounted.
380 */
381 if (ai != NULL && !(fflag & MNT_FORCE) && do_rpc) {
382 clp = clnt_create(hostp, MOUNTPROG, MOUNTVERS, "udp");
383 if (clp == NULL) {
384 warnx("%s: %s", hostp,
385 clnt_spcreateerror("MOUNTPROG"));
386 return (1);
387 }
388 clp->cl_auth = authsys_create_default();
389 try.tv_sec = 20;
390 try.tv_usec = 0;
391 clnt_stat = clnt_call(clp, MOUNTPROC_UMNT, (xdrproc_t)xdr_dir,
392 nfsdirname, (xdrproc_t)xdr_void, (caddr_t)0, try);
393 if (clnt_stat != RPC_SUCCESS) {
394 warnx("%s: %s", hostp,
395 clnt_sperror(clp, "RPCMNT_UMOUNT"));
396 return (1);
397 }
398 /*
399 * Remove the unmounted entry from /var/db/mounttab.
400 */
401 if (read_mtab()) {
402 clean_mtab(hostp, nfsdirname, vflag);
403 if(!write_mtab(vflag))
404 warnx("cannot remove mounttab entry %s:%s",
405 hostp, nfsdirname);
406 free_mtab();
407 }
408 free(orignfsdirname);
409 auth_destroy(clp->cl_auth);
410 clnt_destroy(clp);
411 }
412 return (0);
413}
414
415struct statfs *
416getmntentry(const char *fromname, const char *onname, fsid_t *fsid, dowhat what)
417{
418 static struct statfs *mntbuf;
419 static size_t mntsize = 0;
420 static char *mntcheck = NULL;
421 struct statfs *sfs, *foundsfs;
422 int i, count;
423
424 if (mntsize <= 0) {
425 if ((mntsize = mntinfo(&mntbuf)) <= 0)
426 return (NULL);
427 }
428 if (mntcheck == NULL) {
429 if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL)
430 err(1, "calloc");
431 }
432 /*
433 * We want to get the file systems in the reverse order
434 * that they were mounted. Unmounted file systems are marked
435 * in a table called 'mntcheck'.
436 */
437 count = 0;
438 foundsfs = NULL;
439 for (i = mntsize - 1; i >= 0; i--) {
440 if (mntcheck[i])
441 continue;
442 sfs = &mntbuf[i];
443 if (fromname != NULL && strcmp(sfs->f_mntfromname,
444 fromname) != 0)
445 continue;
446 if (onname != NULL && strcmp(sfs->f_mntonname, onname) != 0)
447 continue;
448 if (fsid != NULL && bcmp(&sfs->f_fsid, fsid,
449 sizeof(*fsid)) != 0)
450 continue;
451
452 switch (what) {
453 case CHECKUNIQUE:
454 foundsfs = sfs;
455 count++;
456 continue;
457 case REMOVE:
458 mntcheck[i] = 1;
459 break;
460 default:
461 break;
462 }
463 return (sfs);
464 }
465
466 if (what == CHECKUNIQUE && count == 1)
467 return (foundsfs);
468 return (NULL);
469}
470
471int
472sacmp(void *sa1, void *sa2)
473{
474 void *p1, *p2;
475 int len;
476
477 if (((struct sockaddr *)sa1)->sa_family !=
478 ((struct sockaddr *)sa2)->sa_family)
479 return (1);
480
481 switch (((struct sockaddr *)sa1)->sa_family) {
482 case AF_INET:
483 p1 = &((struct sockaddr_in *)sa1)->sin_addr;
484 p2 = &((struct sockaddr_in *)sa2)->sin_addr;
485 len = 4;
486 break;
487 case AF_INET6:
488 p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
489 p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
490 len = 16;
491 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
492 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
493 return (1);
494 break;
495 default:
496 return (1);
497 }
498
499 return memcmp(p1, p2, len);
500}
501
502int
503namematch(struct addrinfo *ai)
504{
505 struct addrinfo *aip;
506
507 if (nfshost == NULL || nfshost_ai == NULL)
508 return (1);
509
510 while (ai != NULL) {
511 aip = nfshost_ai;
512 while (aip != NULL) {
513 if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
514 return (1);
515 aip = aip->ai_next;
516 }
517 ai = ai->ai_next;
518 }
519
520 return (0);
521}
522
523struct statfs *
524checkmntlist(char *mntname)
525{
526 struct statfs *sfs;
527 fsid_t fsid;
528
529 sfs = NULL;
530 if (parsehexfsid(mntname, &fsid) == 0)
531 sfs = getmntentry(NULL, NULL, &fsid, FIND);
532 if (sfs == NULL)
533 sfs = getmntentry(NULL, mntname, NULL, FIND);
534 if (sfs == NULL)
535 sfs = getmntentry(mntname, NULL, NULL, FIND);
536 return (sfs);
537}
538
539size_t
540mntinfo(struct statfs **mntbuf)
541{
542 static struct statfs *origbuf;
543 size_t bufsize;
544 int mntsize;
545
546 mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
547 if (mntsize <= 0)
548 return (0);
549 bufsize = (mntsize + 1) * sizeof(struct statfs);
550 if ((origbuf = malloc(bufsize)) == NULL)
551 err(1, "malloc");
552 mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT);
553 *mntbuf = origbuf;
554 return (mntsize);
555}
556
557/*
558 * Convert a hexadecimal filesystem ID to an fsid_t.
559 * Returns 0 on success.
560 */
561int
562parsehexfsid(const char *hex, fsid_t *fsid)
563{
564 char hexbuf[3];
565 int i;
566
567 if (strlen(hex) != sizeof(*fsid) * 2)
568 return (-1);
569 hexbuf[2] = '\0';
570 for (i = 0; i < (int)sizeof(*fsid); i++) {
571 hexbuf[0] = hex[i * 2];
572 hexbuf[1] = hex[i * 2 + 1];
573 if (!isxdigit(hexbuf[0]) || !isxdigit(hexbuf[1]))
574 return (-1);
575 ((u_char *)fsid)[i] = strtol(hexbuf, NULL, 16);
576 }
577 return (0);
578}
579
580/*
581 * xdr routines for mount rpc's
582 */
583int
584xdr_dir(XDR *xdrsp, char *dirp)
585{
586
587 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
588}
589
590void
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/mount.h>
46#include <sys/socket.h>
47#include <sys/stat.h>
48
49#include <netdb.h>
50#include <rpc/rpc.h>
51#include <rpcsvc/mount.h>
52
53#include <ctype.h>
54#include <err.h>
55#include <errno.h>
56#include <fstab.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62#include "mounttab.h"
63
64typedef enum { FIND, REMOVE, CHECKUNIQUE } dowhat;
65
66struct addrinfo *nfshost_ai = NULL;
67int fflag, vflag;
68char *nfshost;
69
70struct statfs *checkmntlist(char *);
71int checkvfsname (const char *, char **);
72struct statfs *getmntentry(const char *fromname, const char *onname,
73 fsid_t *fsid, dowhat what);
74char **makevfslist (const char *);
75size_t mntinfo (struct statfs **);
76int namematch (struct addrinfo *);
77int parsehexfsid(const char *hex, fsid_t *fsid);
78int sacmp (void *, void *);
79int umountall (char **);
80int checkname (char *, char **);
81int umountfs(struct statfs *sfs);
82void usage (void);
83int xdr_dir (XDR *, char *);
84
85int
86main(int argc, char *argv[])
87{
88 int all, errs, ch, mntsize, error;
89 char **typelist = NULL;
90 struct statfs *mntbuf, *sfs;
91 struct addrinfo hints;
92
93 /* Start disks transferring immediately. */
94 sync();
95
96 all = errs = 0;
97 while ((ch = getopt(argc, argv, "AaF:fh:t:v")) != -1)
98 switch (ch) {
99 case 'A':
100 all = 2;
101 break;
102 case 'a':
103 all = 1;
104 break;
105 case 'F':
106 setfstab(optarg);
107 break;
108 case 'f':
109 fflag = MNT_FORCE;
110 break;
111 case 'h': /* -h implies -A. */
112 all = 2;
113 nfshost = optarg;
114 break;
115 case 't':
116 if (typelist != NULL)
117 err(1, "only one -t option may be specified");
118 typelist = makevfslist(optarg);
119 break;
120 case 'v':
121 vflag = 1;
122 break;
123 default:
124 usage();
125 /* NOTREACHED */
126 }
127 argc -= optind;
128 argv += optind;
129
130 if ((argc == 0 && !all) || (argc != 0 && all))
131 usage();
132
133 /* -h implies "-t nfs" if no -t flag. */
134 if ((nfshost != NULL) && (typelist == NULL))
135 typelist = makevfslist("nfs");
136
137 if (nfshost != NULL) {
138 memset(&hints, 0, sizeof hints);
139 error = getaddrinfo(nfshost, NULL, &hints, &nfshost_ai);
140 if (error)
141 errx(1, "%s: %s", nfshost, gai_strerror(error));
142 }
143
144 switch (all) {
145 case 2:
146 if ((mntsize = mntinfo(&mntbuf)) <= 0)
147 break;
148 /*
149 * We unmount the nfs-mounts in the reverse order
150 * that they were mounted.
151 */
152 for (errs = 0, mntsize--; mntsize > 0; mntsize--) {
153 sfs = &mntbuf[mntsize];
154 if (checkvfsname(sfs->f_fstypename, typelist))
155 continue;
156 if (umountfs(sfs) != 0)
157 errs = 1;
158 }
159 free(mntbuf);
160 break;
161 case 1:
162 if (setfsent() == 0)
163 err(1, "%s", getfstab());
164 errs = umountall(typelist);
165 break;
166 case 0:
167 for (errs = 0; *argv != NULL; ++argv)
168 if (checkname(*argv, typelist) != 0)
169 errs = 1;
170 break;
171 }
172 exit(errs);
173}
174
175int
176umountall(char **typelist)
177{
178 struct xvfsconf vfc;
179 struct fstab *fs;
180 int rval;
181 char *cp;
182 static int firstcall = 1;
183
184 if ((fs = getfsent()) != NULL)
185 firstcall = 0;
186 else if (firstcall)
187 errx(1, "fstab reading failure");
188 else
189 return (0);
190 do {
191 /* Ignore the root. */
192 if (strcmp(fs->fs_file, "/") == 0)
193 continue;
194 /*
195 * !!!
196 * Historic practice: ignore unknown FSTAB_* fields.
197 */
198 if (strcmp(fs->fs_type, FSTAB_RW) &&
199 strcmp(fs->fs_type, FSTAB_RO) &&
200 strcmp(fs->fs_type, FSTAB_RQ))
201 continue;
202 /* Ignore unknown file system types. */
203 if (getvfsbyname(fs->fs_vfstype, &vfc) == -1)
204 continue;
205 if (checkvfsname(fs->fs_vfstype, typelist))
206 continue;
207
208 /*
209 * We want to unmount the file systems in the reverse order
210 * that they were mounted. So, we save off the file name
211 * in some allocated memory, and then call recursively.
212 */
213 if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
214 err(1, "malloc failed");
215 (void)strcpy(cp, fs->fs_file);
216 rval = umountall(typelist);
217 rval = checkname(cp, typelist) || rval;
218 free(cp);
219 return (rval);
220 } while ((fs = getfsent()) != NULL);
221 return (0);
222}
223
224/*
225 * Do magic checks on mountpoint/device/fsid, and then call unmount(2).
226 */
227int
228checkname(char *mntname, char **typelist)
229{
230 char buf[MAXPATHLEN];
231 struct statfs sfsbuf;
232 struct stat sb;
233 struct statfs *sfs;
234 char *delimp;
235 dev_t dev;
236 int len;
237
238 /*
239 * 1. Check if the name exists in the mounttable.
240 */
241 sfs = checkmntlist(mntname);
242 /*
243 * 2. Remove trailing slashes if there are any. After that
244 * we look up the name in the mounttable again.
245 */
246 if (sfs == NULL) {
247 len = strlen(mntname);
248 while (len > 1 && mntname[len - 1] == '/')
249 mntname[--len] = '\0';
250 sfs = checkmntlist(mntname);
251 }
252 /*
253 * 3. Check if the deprecated NFS syntax with an '@' has been used
254 * and translate it to the ':' syntax. Look up the name in the
255 * mount table again.
256 */
257 if (sfs == NULL && (delimp = strrchr(mntname, '@')) != NULL) {
258 snprintf(buf, sizeof(buf), "%s:%.*s", delimp + 1,
259 (int)(delimp - mntname), mntname);
260 len = strlen(buf);
261 while (len > 1 && buf[len - 1] == '/')
262 buf[--len] = '\0';
263 sfs = checkmntlist(buf);
264 }
265 /*
266 * 4. Resort to a statfs(2) call. This is the last check so that
267 * hung NFS filesystems for example can be unmounted without
268 * potentially blocking forever in statfs() as long as the
269 * filesystem is specified unambiguously. This covers all the
270 * hard cases such as symlinks and mismatches between the
271 * mount list and reality.
272 * We also do this if an ambiguous mount point was specified.
273 */
274 if (sfs == NULL || (getmntentry(NULL, mntname, NULL, FIND) != NULL &&
275 getmntentry(NULL, mntname, NULL, CHECKUNIQUE) == NULL)) {
276 if (statfs(mntname, &sfsbuf) != 0) {
277 warn("%s: statfs", mntname);
278 } else if (stat(mntname, &sb) != 0) {
279 warn("%s: stat", mntname);
280 } else if (S_ISDIR(sb.st_mode)) {
281 /* Check that `mntname' is the root directory. */
282 dev = sb.st_dev;
283 snprintf(buf, sizeof(buf), "%s/..", mntname);
284 if (stat(buf, &sb) != 0) {
285 warn("%s: stat", buf);
286 } else if (sb.st_dev == dev) {
287 warnx("%s: not a file system root directory",
288 mntname);
289 return (1);
290 } else
291 sfs = &sfsbuf;
292 }
293 }
294 if (sfs == NULL) {
295 warnx("%s: unknown file system", mntname);
296 return (1);
297 }
298 if (checkvfsname(sfs->f_fstypename, typelist))
299 return (1);
300 return (umountfs(sfs));
301}
302
303/*
304 * NFS stuff and unmount(2) call
305 */
306int
307umountfs(struct statfs *sfs)
308{
309 char fsidbuf[64];
310 enum clnt_stat clnt_stat;
311 struct timeval try;
312 struct addrinfo *ai, hints;
313 int do_rpc;
314 CLIENT *clp;
315 char *nfsdirname, *orignfsdirname;
316 char *hostp, *delimp;
317
318 ai = NULL;
319 do_rpc = 0;
320 hostp = NULL;
321 nfsdirname = delimp = orignfsdirname = NULL;
322 memset(&hints, 0, sizeof hints);
323
324 if (strcmp(sfs->f_fstypename, "nfs") == 0) {
325 if ((nfsdirname = strdup(sfs->f_mntfromname)) == NULL)
326 err(1, "strdup");
327 orignfsdirname = nfsdirname;
328 if ((delimp = strrchr(nfsdirname, ':')) != NULL) {
329 *delimp = '\0';
330 hostp = nfsdirname;
331 getaddrinfo(hostp, NULL, &hints, &ai);
332 if (ai == NULL) {
333 warnx("can't get net id for host");
334 }
335 nfsdirname = delimp + 1;
336 }
337
338 /*
339 * Check if we have to start the rpc-call later.
340 * If there are still identical nfs-names mounted,
341 * we skip the rpc-call. Obviously this has to
342 * happen before unmount(2), but it should happen
343 * after the previous namecheck.
344 * A non-NULL return means that this is the last
345 * mount from mntfromname that is still mounted.
346 */
347 if (getmntentry(sfs->f_mntfromname, NULL, NULL,
348 CHECKUNIQUE) != NULL)
349 do_rpc = 1;
350 }
351
352 if (!namematch(ai))
353 return (1);
354 /* First try to unmount using the file system ID. */
355 snprintf(fsidbuf, sizeof(fsidbuf), "FSID:%d:%d", sfs->f_fsid.val[0],
356 sfs->f_fsid.val[1]);
357 if (unmount(fsidbuf, fflag | MNT_BYFSID) != 0) {
358 /* XXX, non-root users get a zero fsid, so don't warn. */
359 if (errno != ENOENT || sfs->f_fsid.val[0] != 0 ||
360 sfs->f_fsid.val[1] != 0)
361 warn("unmount of %s failed", sfs->f_mntonname);
362 if (errno != ENOENT)
363 return (1);
364 /* Compatibility for old kernels. */
365 if (sfs->f_fsid.val[0] != 0 || sfs->f_fsid.val[1] != 0)
366 warnx("retrying using path instead of file system ID");
367 if (unmount(sfs->f_mntonname, fflag) != 0) {
368 warn("unmount of %s failed", sfs->f_mntonname);
369 return (1);
370 }
371 }
372 /* Mark this this file system as unmounted. */
373 getmntentry(NULL, NULL, &sfs->f_fsid, REMOVE);
374 if (vflag)
375 (void)printf("%s: unmount from %s\n", sfs->f_mntfromname,
376 sfs->f_mntonname);
377 /*
378 * Report to mountd-server which nfsname
379 * has been unmounted.
380 */
381 if (ai != NULL && !(fflag & MNT_FORCE) && do_rpc) {
382 clp = clnt_create(hostp, MOUNTPROG, MOUNTVERS, "udp");
383 if (clp == NULL) {
384 warnx("%s: %s", hostp,
385 clnt_spcreateerror("MOUNTPROG"));
386 return (1);
387 }
388 clp->cl_auth = authsys_create_default();
389 try.tv_sec = 20;
390 try.tv_usec = 0;
391 clnt_stat = clnt_call(clp, MOUNTPROC_UMNT, (xdrproc_t)xdr_dir,
392 nfsdirname, (xdrproc_t)xdr_void, (caddr_t)0, try);
393 if (clnt_stat != RPC_SUCCESS) {
394 warnx("%s: %s", hostp,
395 clnt_sperror(clp, "RPCMNT_UMOUNT"));
396 return (1);
397 }
398 /*
399 * Remove the unmounted entry from /var/db/mounttab.
400 */
401 if (read_mtab()) {
402 clean_mtab(hostp, nfsdirname, vflag);
403 if(!write_mtab(vflag))
404 warnx("cannot remove mounttab entry %s:%s",
405 hostp, nfsdirname);
406 free_mtab();
407 }
408 free(orignfsdirname);
409 auth_destroy(clp->cl_auth);
410 clnt_destroy(clp);
411 }
412 return (0);
413}
414
415struct statfs *
416getmntentry(const char *fromname, const char *onname, fsid_t *fsid, dowhat what)
417{
418 static struct statfs *mntbuf;
419 static size_t mntsize = 0;
420 static char *mntcheck = NULL;
421 struct statfs *sfs, *foundsfs;
422 int i, count;
423
424 if (mntsize <= 0) {
425 if ((mntsize = mntinfo(&mntbuf)) <= 0)
426 return (NULL);
427 }
428 if (mntcheck == NULL) {
429 if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL)
430 err(1, "calloc");
431 }
432 /*
433 * We want to get the file systems in the reverse order
434 * that they were mounted. Unmounted file systems are marked
435 * in a table called 'mntcheck'.
436 */
437 count = 0;
438 foundsfs = NULL;
439 for (i = mntsize - 1; i >= 0; i--) {
440 if (mntcheck[i])
441 continue;
442 sfs = &mntbuf[i];
443 if (fromname != NULL && strcmp(sfs->f_mntfromname,
444 fromname) != 0)
445 continue;
446 if (onname != NULL && strcmp(sfs->f_mntonname, onname) != 0)
447 continue;
448 if (fsid != NULL && bcmp(&sfs->f_fsid, fsid,
449 sizeof(*fsid)) != 0)
450 continue;
451
452 switch (what) {
453 case CHECKUNIQUE:
454 foundsfs = sfs;
455 count++;
456 continue;
457 case REMOVE:
458 mntcheck[i] = 1;
459 break;
460 default:
461 break;
462 }
463 return (sfs);
464 }
465
466 if (what == CHECKUNIQUE && count == 1)
467 return (foundsfs);
468 return (NULL);
469}
470
471int
472sacmp(void *sa1, void *sa2)
473{
474 void *p1, *p2;
475 int len;
476
477 if (((struct sockaddr *)sa1)->sa_family !=
478 ((struct sockaddr *)sa2)->sa_family)
479 return (1);
480
481 switch (((struct sockaddr *)sa1)->sa_family) {
482 case AF_INET:
483 p1 = &((struct sockaddr_in *)sa1)->sin_addr;
484 p2 = &((struct sockaddr_in *)sa2)->sin_addr;
485 len = 4;
486 break;
487 case AF_INET6:
488 p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
489 p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
490 len = 16;
491 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
492 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
493 return (1);
494 break;
495 default:
496 return (1);
497 }
498
499 return memcmp(p1, p2, len);
500}
501
502int
503namematch(struct addrinfo *ai)
504{
505 struct addrinfo *aip;
506
507 if (nfshost == NULL || nfshost_ai == NULL)
508 return (1);
509
510 while (ai != NULL) {
511 aip = nfshost_ai;
512 while (aip != NULL) {
513 if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
514 return (1);
515 aip = aip->ai_next;
516 }
517 ai = ai->ai_next;
518 }
519
520 return (0);
521}
522
523struct statfs *
524checkmntlist(char *mntname)
525{
526 struct statfs *sfs;
527 fsid_t fsid;
528
529 sfs = NULL;
530 if (parsehexfsid(mntname, &fsid) == 0)
531 sfs = getmntentry(NULL, NULL, &fsid, FIND);
532 if (sfs == NULL)
533 sfs = getmntentry(NULL, mntname, NULL, FIND);
534 if (sfs == NULL)
535 sfs = getmntentry(mntname, NULL, NULL, FIND);
536 return (sfs);
537}
538
539size_t
540mntinfo(struct statfs **mntbuf)
541{
542 static struct statfs *origbuf;
543 size_t bufsize;
544 int mntsize;
545
546 mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
547 if (mntsize <= 0)
548 return (0);
549 bufsize = (mntsize + 1) * sizeof(struct statfs);
550 if ((origbuf = malloc(bufsize)) == NULL)
551 err(1, "malloc");
552 mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT);
553 *mntbuf = origbuf;
554 return (mntsize);
555}
556
557/*
558 * Convert a hexadecimal filesystem ID to an fsid_t.
559 * Returns 0 on success.
560 */
561int
562parsehexfsid(const char *hex, fsid_t *fsid)
563{
564 char hexbuf[3];
565 int i;
566
567 if (strlen(hex) != sizeof(*fsid) * 2)
568 return (-1);
569 hexbuf[2] = '\0';
570 for (i = 0; i < (int)sizeof(*fsid); i++) {
571 hexbuf[0] = hex[i * 2];
572 hexbuf[1] = hex[i * 2 + 1];
573 if (!isxdigit(hexbuf[0]) || !isxdigit(hexbuf[1]))
574 return (-1);
575 ((u_char *)fsid)[i] = strtol(hexbuf, NULL, 16);
576 }
577 return (0);
578}
579
580/*
581 * xdr routines for mount rpc's
582 */
583int
584xdr_dir(XDR *xdrsp, char *dirp)
585{
586
587 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
588}
589
590void
591usage()
591usage(void)
592{
593
594 (void)fprintf(stderr, "%s\n%s\n",
595 "usage: umount [-fv] special | node | fsid",
596 " umount -a | -A [-F fstab] [-fv] [-h host] [-t type]");
597 exit(1);
598}
592{
593
594 (void)fprintf(stderr, "%s\n%s\n",
595 "usage: umount [-fv] special | node | fsid",
596 " umount -a | -A [-F fstab] [-fv] [-h host] [-t type]");
597 exit(1);
598}