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