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