]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/umount/umount.c
This commit was generated by cvs2svn to compensate for changes in r74485,
[FreeBSD/FreeBSD.git] / sbin / umount / umount.c
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1989, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)umount.c    8.8 (Berkeley) 5/8/95";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47
48 #include <sys/param.h>
49 #include <sys/mount.h>
50 #include <sys/socket.h>
51
52 #include <netdb.h>
53 #include <rpc/rpc.h>
54 #include <nfs/rpcv2.h>
55
56 #include <err.h>
57 #include <fstab.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 #include "mounttab.h"
64
65 #define ISDOT(x)        ((x)[0] == '.' && (x)[1] == '\0')
66 #define ISDOTDOT(x)     ((x)[0] == '.' && (x)[1] == '.' && (x)[2] == '\0')
67
68 typedef enum { MNTON, MNTFROM, NOTHING } mntwhat;
69 typedef enum { MARK, UNMARK, NAME, COUNT, FREE } dowhat;
70
71 struct  mtablist *mtabhead;
72 struct  addrinfo *nfshost_ai = NULL;
73 int     fflag, vflag;
74 char   *nfshost;
75
76 void     checkmntlist (char *, char **, char **, char **);
77 int      checkvfsname (const char *, char **);
78 char    *getmntname (const char *, const char *,
79          mntwhat, char **, dowhat);
80 char    *getrealname(char *, char *resolved_path);
81 char   **makevfslist (const char *);
82 size_t   mntinfo (struct statfs **);
83 int      namematch (struct addrinfo *);
84 int      sacmp (struct sockaddr *, struct sockaddr *);
85 int      umountall (char **);
86 int      checkname (char *, char **);
87 int      umountfs (char *, char *, char *);
88 void     usage (void);
89 int      xdr_dir (XDR *, char *);
90
91 int
92 main(int argc, char *argv[])
93 {
94         int all, errs, ch, mntsize, error;
95         char **typelist = NULL, *mntonname, *mntfromname;
96         char *type, *mntfromnamerev, *mntonnamerev;
97         struct statfs *mntbuf;
98         struct addrinfo hints;
99
100         /* Start disks transferring immediately. */
101         sync();
102
103         all = errs = 0;
104         while ((ch = getopt(argc, argv, "Aafh:t:v")) != -1)
105                 switch (ch) {
106                 case 'A':
107                         all = 2;
108                         break;
109                 case 'a':
110                         all = 1;
111                         break;
112                 case 'f':
113                         fflag = MNT_FORCE;
114                         break;
115                 case 'h':       /* -h implies -A. */
116                         all = 2;
117                         nfshost = optarg;
118                         break;
119                 case 't':
120                         if (typelist != NULL)
121                                 err(1, "only one -t option may be specified");
122                         typelist = makevfslist(optarg);
123                         break;
124                 case 'v':
125                         vflag = 1;
126                         break;
127                 default:
128                         usage();
129                         /* NOTREACHED */
130                 }
131         argc -= optind;
132         argv += optind;
133
134         if ((argc == 0 && !all) || (argc != 0 && all))
135                 usage();
136
137         /* -h implies "-t nfs" if no -t flag. */
138         if ((nfshost != NULL) && (typelist == NULL))
139                 typelist = makevfslist("nfs");
140
141         if (nfshost != NULL) {
142                 memset(&hints, 0, sizeof hints);
143                 error = getaddrinfo(nfshost, NULL, &hints, &nfshost_ai);
144                 if (error) {
145                         fprintf(stderr, "ndp: %s: %s\n", nfshost,
146                             gai_strerror(error));
147                 }
148         }
149
150         switch (all) {
151         case 2:
152                 if ((mntsize = mntinfo(&mntbuf)) <= 0)
153                         break;
154                 /*
155                  * We unmount the nfs-mounts in the reverse order
156                  * that they were mounted.
157                  */
158                 for (errs = 0, mntsize--; mntsize > 0; mntsize--) {
159                         if (checkvfsname(mntbuf[mntsize].f_fstypename,
160                             typelist))
161                                 continue;
162                         /*
163                          * Check if a mountpoint is laid over by another mount.
164                          * A warning will be printed to stderr if this is
165                          * the case. The laid over mount remains unmounted.
166                          */
167                         mntonname = mntbuf[mntsize].f_mntonname;
168                         mntfromname = mntbuf[mntsize].f_mntfromname;
169                         mntonnamerev = getmntname(getmntname(mntonname,
170                             NULL, MNTFROM, &type, NAME), NULL,
171                             MNTON, &type, NAME);
172
173                         mntfromnamerev = getmntname(mntonnamerev,
174                             NULL, MNTFROM, &type, NAME);
175
176                         if (strcmp(mntonnamerev, mntonname) == 0 &&
177                             strcmp(mntfromnamerev, mntfromname ) != 0)
178                                 warnx("cannot umount %s, %s\n        "
179                                     "is mounted there, umount it first",
180                                     mntonname, mntfromnamerev);
181
182                         if (checkname(mntbuf[mntsize].f_mntonname,
183                             typelist) != 0)
184                                 errs = 1;
185                 }
186                 free(mntbuf);
187                 break;
188         case 1:
189                 if (setfsent() == 0)
190                         err(1, "%s", _PATH_FSTAB);
191                 errs = umountall(typelist);
192                 break;
193         case 0:
194                 for (errs = 0; *argv != NULL; ++argv)
195                         if (checkname(*argv, typelist) != 0)
196                                 errs = 1;
197                 break;
198         }
199         (void)getmntname(NULL, NULL, NOTHING, NULL, FREE);
200         exit(errs);
201 }
202
203 int
204 umountall(char **typelist)
205 {
206         struct vfsconf vfc;
207         struct fstab *fs;
208         int rval;
209         char *cp;
210         static int firstcall = 1;
211
212         if ((fs = getfsent()) != NULL)
213                 firstcall = 0;
214         else if (firstcall)
215                 errx(1, "fstab reading failure");
216         else
217                 return (0);
218         do {
219                 /* Ignore the root. */
220                 if (strcmp(fs->fs_file, "/") == 0)
221                         continue;
222                 /*
223                  * !!!
224                  * Historic practice: ignore unknown FSTAB_* fields.
225                  */
226                 if (strcmp(fs->fs_type, FSTAB_RW) &&
227                     strcmp(fs->fs_type, FSTAB_RO) &&
228                     strcmp(fs->fs_type, FSTAB_RQ))
229                         continue;
230                 /* Ignore unknown file system types. */
231                 if (getvfsbyname(fs->fs_vfstype, &vfc) == -1)
232                         continue;
233                 if (checkvfsname(fs->fs_vfstype, typelist))
234                         continue;
235
236                 /*
237                  * We want to unmount the file systems in the reverse order
238                  * that they were mounted.  So, we save off the file name
239                  * in some allocated memory, and then call recursively.
240                  */
241                 if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
242                         err(1, "malloc failed");
243                 (void)strcpy(cp, fs->fs_file);
244                 rval = umountall(typelist);
245                 rval = checkname(cp, typelist) || rval;
246                 free(cp);
247                 return (rval);
248         } while ((fs = getfsent()) != NULL);
249         return (0);
250 }
251
252 /*
253  * Do magic checks on mountpoint and device or hand over
254  * it to unmount(2) if everything fails.
255  */
256 int
257 checkname(char *name, char **typelist)
258 {
259         size_t len;
260         int speclen;
261         char *mntonname, *mntfromname;
262         char *mntfromnamerev;
263         char *resolved, realname[MAXPATHLEN];
264         char *type, *hostp, *delimp, *origname;
265
266         len = 0;
267         mntfromname = mntonname = delimp = hostp = NULL;
268
269         /*
270          * 1. Check if the name exists in the mounttable.
271          */
272         (void)checkmntlist(name, &mntfromname, &mntonname, &type);
273         /*
274          * 2. Remove trailing slashes if there are any. After that
275          * we look up the name in the mounttable again.
276          */
277         if (mntfromname == NULL && mntonname == NULL) {
278                 speclen = strlen(name);
279                 for (speclen = strlen(name); 
280                     speclen > 1 && name[speclen - 1] == '/';
281                     speclen--)
282                         name[speclen - 1] = '\0';
283                 (void)checkmntlist(name, &mntfromname, &mntonname, &type);
284                 resolved = name;
285                 /* Save off original name in origname */
286                 if ((origname = strdup(name)) == NULL)
287                         err(1, "strdup");
288                 /*
289                  * 3. Check if the deprecated nfs-syntax with an '@'
290                  * has been used and translate it to the ':' syntax.
291                  * Look up the name in the mounttable again.
292                  */
293                 if (mntfromname == NULL && mntonname == NULL) {
294                         if ((delimp = strrchr(name, '@')) != NULL) {
295                                 hostp = delimp + 1;
296                                 if (*hostp != '\0') {
297                                         /*
298                                          * Make both '@' and ':'
299                                          * notations equal 
300                                          */
301                                         char *host = strdup(hostp);
302                                         len = strlen(hostp);
303                                         if (host == NULL)
304                                                 err(1, "strdup");
305                                         memmove(name + len + 1, name,
306                                             (size_t)(delimp - name));
307                                         name[len] = ':';
308                                         memmove(name, host, len);
309                                         free(host);
310                                 }
311                                 for (speclen = strlen(name); 
312                                     speclen > 1 && name[speclen - 1] == '/';
313                                     speclen--)
314                                         name[speclen - 1] = '\0';
315                                 name[len + speclen + 1] = '\0';
316                                 (void)checkmntlist(name, &mntfromname,
317                                     &mntonname, &type);
318                                 resolved = name;
319                         }
320                         /*
321                          * 4. Check if a relative mountpoint has been
322                          * specified. This should happen as last check,
323                          * the order is important. To prevent possible
324                          * nfs-hangs, we just call realpath(3) on the
325                          * basedir of mountpoint and add the dirname again.
326                          * Check the name in mounttable one last time.
327                          */
328                         if (mntfromname == NULL && mntonname == NULL) {
329                                 (void)strcpy(name, origname);
330                                 if ((getrealname(name, realname)) != NULL) {
331                                         (void)checkmntlist(realname,
332                                             &mntfromname, &mntonname, &type);
333                                         resolved = realname;
334                                 }
335                                 /*
336                                  * 5. All tests failed, just hand over the
337                                  * mountpoint to the kernel, maybe the statfs
338                                  * structure has been truncated or is not
339                                  * useful anymore because of a chroot(2).
340                                  * Please note that nfs will not be able to
341                                  * notify the nfs-server about unmounting.
342                                  * These things can change in future when the 
343                                  * fstat structure get's more reliable,
344                                  * but at the moment we cannot thrust it.
345                                  */
346                                 if (mntfromname == NULL && mntonname == NULL) {
347                                         (void)strcpy(name, origname);
348                                         if (umountfs(NULL, origname,
349                                             "none") == 0) {;
350                                                 warnx("%s not found in "
351                                                     "mount table, "
352                                                     "unmounted it anyway",
353                                                     origname);
354                                                 free(origname);
355                                                 return (0);
356                                         } else
357                                                 free(origname);
358                                                 return (1);
359                                 }
360                         }
361                 }
362                 free(origname);
363         } else
364                 resolved = name;
365
366         if (checkvfsname(type, typelist))
367                 return (1);
368
369         /*
370          * Check if the reverse entrys of the mounttable are really the
371          * same as the normal ones.
372          */
373         if ((mntfromnamerev = strdup(getmntname(getmntname(mntfromname,
374             NULL, MNTON, &type, NAME), NULL, MNTFROM, &type, NAME))) == NULL)
375                 err(1, "strdup");
376         /*
377          * Mark the uppermost mount as unmounted.
378          */
379         (void)getmntname(mntfromname, mntonname, NOTHING, &type, MARK);
380         /*
381          * If several equal mounts are in the mounttable, check the order
382          * and warn the user if necessary.
383          */
384         if (strcmp(mntfromnamerev, mntfromname ) != 0 &&
385             strcmp(resolved, mntonname) != 0) {
386                 warnx("cannot umount %s, %s\n        "
387                     "is mounted there, umount it first",
388                     mntonname, mntfromnamerev);
389
390                 /* call getmntname again to set mntcheck[i] to 0 */
391                 (void)getmntname(mntfromname, mntonname,
392                     NOTHING, &type, UNMARK);
393                 return (1);
394         }
395         free(mntfromnamerev);
396         umountfs(mntfromname, mntonname, type);
397 }
398
399 /*
400  * NFS stuff and unmount(2) call
401  */
402 int
403 umountfs(char *mntfromname, char *mntonname, char *type)
404 {
405         enum clnt_stat clnt_stat;
406         struct timeval try;
407         struct mtablist *mtab;
408         struct addrinfo *ai, hints;
409         int do_rpc;
410         CLIENT *clp;
411         char *nfsdirname, *orignfsdirname;
412         char *hostp, *delimp;
413
414         mtab = NULL;
415         ai = NULL;
416         nfsdirname = delimp = orignfsdirname = NULL;
417         memset(&hints, 0, sizeof hints);
418
419         if (!strcmp(type, "nfs")) {
420                 if ((nfsdirname = strdup(mntfromname)) == NULL)
421                         err(1, "strdup");
422                 orignfsdirname = nfsdirname;
423                 if ((delimp = strrchr(nfsdirname, ':')) != NULL) {
424                         *delimp = '\0';
425                         hostp = nfsdirname;
426                         getaddrinfo(hostp, NULL, &hints, &ai);
427                         if (ai == NULL) {
428                                 warnx("can't get net id for host");
429                         }
430                         nfsdirname = delimp + 1;
431                 }
432         }
433         /*
434          * Check if we have to start the rpc-call later.
435          * If there are still identical nfs-names mounted,
436          * we skip the rpc-call. Obviously this has to
437          * happen before unmount(2), but it should happen
438          * after the previous namecheck.
439          */
440         if (strcmp(type, "nfs") == 0 && getmntname(mntfromname, NULL, NOTHING,
441             &type, COUNT) != NULL)
442                 do_rpc = 1;
443         else
444                 do_rpc = 0;
445
446         if (!namematch(ai))
447                 return (1);
448         if (unmount(mntonname, fflag) != 0 ) {
449                 warn("unmount of %s failed", mntonname);
450                 return (1);
451         }
452         if (vflag)
453                 (void)printf("%s: unmount from %s\n", mntfromname, mntonname);
454         /*
455          * Report to mountd-server which nfsname
456          * has been unmounted.
457          */
458         if (ai != NULL && !(fflag & MNT_FORCE) && do_rpc) {
459                 clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1, "udp");
460                 if (clp  == NULL) {
461                         clnt_pcreateerror("Cannot MNT PRC");
462                         return (1);
463                 }
464                 clp->cl_auth = authsys_create_default();
465                 try.tv_sec = 20;
466                 try.tv_usec = 0;
467                 clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir,
468                     nfsdirname, xdr_void, (caddr_t)0, try);
469                 if (clnt_stat != RPC_SUCCESS) {
470                         clnt_perror(clp, "Bad MNT RPC");
471                         return (1);
472                 }
473                 /*
474                  * Remove the unmounted entry from /var/db/mounttab.
475                  */
476                 if (read_mtab(mtab)) {
477                         mtab = mtabhead;
478                         clean_mtab(hostp, nfsdirname);
479                         if(!write_mtab())
480                                 warnx("cannot remove entry %s:%s",
481                                     hostp, nfsdirname);
482                         free_mtab();
483                 }
484                 free(orignfsdirname);
485                 auth_destroy(clp->cl_auth);
486                 clnt_destroy(clp);
487         }
488         return (0);
489 }
490
491 char *
492 getmntname(const char *fromname, const char *onname,
493     mntwhat what, char **type, dowhat mark)
494 {
495         static struct statfs *mntbuf;
496         static size_t mntsize = 0;
497         static char *mntcheck = NULL;
498         static char *mntcount = NULL;
499         int i, count;
500
501         if (mntsize <= 0) {
502                 if ((mntsize = mntinfo(&mntbuf)) <= 0)
503                         return (NULL);
504         }
505         if (mntcheck == NULL) {
506                 if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL ||
507                     (mntcount = calloc(mntsize + 1, sizeof(int))) == NULL)
508                         err(1, "calloc");
509         }
510         /*
511          * We want to get the file systems in the reverse order
512          * that they were mounted. Mounted and unmounted filesystems
513          * are marked or unmarked in a table called 'mntcheck'.
514          * Unmount(const char *dir, int flags) does only take the
515          * mountpoint as argument, not the destination. If we don't pay
516          * attention to the order, it can happen that a overlaying
517          * filesystem get's unmounted instead of the one the user
518          * has choosen.
519          */
520         switch (mark) {
521         case NAME:
522                 /* Return only the specific name */
523                 for (i = mntsize - 1; i >= 0; i--) {
524                         if (fromname != NULL && what == MNTON &&
525                             !strcmp(mntbuf[i].f_mntfromname, fromname) &&
526                             mntcheck[i] != 1) {
527                                 if (type)
528                                         *type = mntbuf[i].f_fstypename;
529                                 return (mntbuf[i].f_mntonname);
530                         }
531                         if (fromname != NULL && what == MNTFROM &&
532                             !strcmp(mntbuf[i].f_mntonname, fromname) &&
533                             mntcheck[i] != 1) {
534                                 if (type)
535                                         *type = mntbuf[i].f_fstypename;
536                                 return (mntbuf[i].f_mntfromname);
537                         }
538                 }
539                 return (NULL);
540         case MARK:
541                 /* Mark current mount with '1' and return name */
542                 for (i = mntsize - 1; i >= 0; i--) {
543                         if (mntcheck[i] == 0 &&
544                             (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
545                             (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
546                                 mntcheck[i] = 1;
547                                 return (mntbuf[i].f_mntonname);
548                         }
549                 }
550                 return (NULL);
551         case UNMARK:
552                 /* Unmark current mount with '0' and return name */
553                 for (i = 0; i < mntsize; i++) {
554                         if (mntcheck[i] == 1 &&
555                             (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
556                             (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
557                                 mntcheck[i] = 0;
558                                 return (mntbuf[i].f_mntonname);
559                         }
560                 }
561                 return (NULL);
562         case COUNT:
563                 /* Count the equal mntfromnames */
564                 count = 0;
565                 for (i = mntsize - 1; i >= 0; i--) {
566                         if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)
567                                 count++;
568                 }
569                 /* Mark the already unmounted mounts and return
570                  * mntfromname if count <= 1. Else return NULL.
571                  */
572                 for (i = mntsize - 1; i >= 0; i--) {
573                         if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0) {
574                                 if (mntcount[i] == 1)
575                                         count--;
576                                 else {
577                                         mntcount[i] = 1;
578                                         break;
579                                 }
580                         }
581                 }
582                 if (count <= 1)
583                         return (mntbuf[i].f_mntonname);
584                 else
585                         return (NULL);
586         case FREE:
587                 free(mntbuf);
588                 free(mntcheck);
589                 free(mntcount);
590                 return (NULL);
591         default:
592                 return (NULL);
593         }
594 }
595
596 int
597 sacmp(struct sockaddr *sa1, struct sockaddr *sa2)
598 {
599         void *p1, *p2;
600         int len;
601
602         if (sa1->sa_family != sa2->sa_family)
603                 return (1);
604
605         switch (sa1->sa_family) {
606         case AF_INET:
607                 p1 = &((struct sockaddr_in *)sa1)->sin_addr;
608                 p2 = &((struct sockaddr_in *)sa2)->sin_addr;
609                 len = 4;
610                 break;
611         case AF_INET6:
612                 p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
613                 p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
614                 len = 16;
615                 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
616                     ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
617                         return (1);
618                 break;
619         default:
620                 return (1);
621         }
622
623         return memcmp(p1, p2, len);
624 }
625
626 int
627 namematch(struct addrinfo *ai)
628 {
629         struct addrinfo *aip;
630
631         if (nfshost == NULL || nfshost_ai == NULL)
632                 return (1);
633
634         while (ai != NULL) {
635                 aip = nfshost_ai;
636                 while (aip != NULL) {
637                         if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
638                                 return (1);
639                         aip = aip->ai_next;
640                 }
641                 ai = ai->ai_next;
642         }
643
644         return (0);
645 }
646
647 void
648 checkmntlist(char *name, char **fromname, char **onname, char **type)
649 {
650
651         *fromname = getmntname(name, NULL, MNTFROM, type, NAME);
652         if (*fromname == NULL) {
653                 *onname = getmntname(name, NULL, MNTON, type, NAME);
654                 if (*onname != NULL)
655                         *fromname = name;
656         } else
657                 *onname = name;
658 }
659
660 size_t
661 mntinfo(struct statfs **mntbuf)
662 {
663         static struct statfs *origbuf;
664         size_t bufsize;
665         int mntsize;
666
667         mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
668         if (mntsize <= 0)
669                 return (0);
670         bufsize = (mntsize + 1) * sizeof(struct statfs);
671         if ((origbuf = malloc(bufsize)) == NULL)
672                 err(1, "malloc");
673         mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT);
674         *mntbuf = origbuf;
675         return (mntsize);
676 }
677
678 char *
679 getrealname(char *name, char *realname)
680 {
681         char *dirname;
682         int havedir;
683         size_t baselen;
684         size_t dirlen;
685         
686         dirname = '\0';
687         havedir = 0;
688         if (*name == '/') {
689                 if (ISDOT(name + 1) || ISDOTDOT(name + 1))
690                         strcpy(realname, "/");
691                 else {
692                         if ((dirname = strrchr(name + 1, '/')) == NULL)
693                                 snprintf(realname, MAXPATHLEN, "%s", name);
694                         else
695                                 havedir = 1;
696                 }
697         } else {
698                 if (ISDOT(name) || ISDOTDOT(name))
699                         (void)realpath(name, realname);
700                 else {
701                         if ((dirname = strrchr(name, '/')) == NULL) {
702                                 if ((realpath(name, realname)) == NULL)
703                                         return (NULL);
704                         } else 
705                                 havedir = 1;
706                 }
707         }
708         if (havedir) {
709                 *dirname++ = '\0';
710                 if (ISDOT(dirname)) {
711                         *dirname = '\0';
712                         if ((realpath(name, realname)) == NULL)
713                                 return (NULL);
714                 } else if (ISDOTDOT(dirname)) {
715                         *--dirname = '/';
716                         if ((realpath(name, realname)) == NULL)
717                                 return (NULL);
718                 } else {
719                         if ((realpath(name, realname)) == NULL)
720                                 return (NULL);
721                         baselen = strlen(realname);
722                         dirlen = strlen(dirname);
723                         if (baselen + dirlen + 1 > MAXPATHLEN)
724                                 return (NULL);
725                         if (realname[1] == '\0') {
726                                 memmove(realname + 1, dirname, dirlen);
727                                 realname[dirlen + 1] = '\0';
728                         } else {
729                                 realname[baselen] = '/';
730                                 memmove(realname + baselen + 1,
731                                     dirname, dirlen);
732                                 realname[baselen + dirlen + 1] = '\0';
733                         }
734                 }
735         }
736         return (realname);
737 }
738
739 /*
740  * xdr routines for mount rpc's
741  */
742 int
743 xdr_dir(XDR *xdrsp, char *dirp)
744 {
745
746         return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
747 }
748
749 void
750 usage()
751 {
752
753         (void)fprintf(stderr, "%s\n%s\n",
754             "usage: umount [-fv] special | node",
755             "       umount -a | -A [-fv] [-h host] [-t type]");
756         exit(1);
757 }