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