DEVTAB - Add support in umount, fsck, and hammer, and add sample /etc/devtab.
[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         /* 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         char none[] = "none";
254
255         len = 0;
256         mntfromname = mntonname = delimp = hostp = NULL;
257
258         /*
259          * 1. Check if the name exists in the mounttable.
260          */
261         checkmntlist(name, &mntfromname, &mntonname, &type);
262         if (mntfromname == NULL && mntonname == NULL) {
263                 checkmntlist(getdevpath(name, 0), &mntfromname,
264                              &mntonname, &type);
265         }
266
267         /*
268          * 2. Remove trailing slashes if there are any. After that
269          * we look up the name in the mounttable again.
270          */
271         if (mntfromname == NULL && mntonname == NULL) {
272                 speclen = strlen(name);
273                 for (speclen = strlen(name); 
274                     speclen > 1 && name[speclen - 1] == '/';
275                     speclen--)
276                         name[speclen - 1] = '\0';
277                 checkmntlist(name, &mntfromname, &mntonname, &type);
278                 resolved = name;
279                 /* Save off original name in origname */
280                 if ((origname = strdup(name)) == NULL)
281                         err(1, "strdup");
282                 /*
283                  * 3. Check if the deprecated nfs-syntax with an '@'
284                  * has been used and translate it to the ':' syntax.
285                  * Look up the name in the mounttable again.
286                  */
287                 if (mntfromname == NULL && mntonname == NULL) {
288                         if ((delimp = strrchr(name, '@')) != NULL) {
289                                 hostp = delimp + 1;
290                                 if (*hostp != '\0') {
291                                         /*
292                                          * Make both '@' and ':'
293                                          * notations equal 
294                                          */
295                                         char *host = strdup(hostp);
296                                         len = strlen(hostp);
297                                         if (host == NULL)
298                                                 err(1, "strdup");
299                                         memmove(name + len + 1, name,
300                                             (size_t)(delimp - name));
301                                         name[len] = ':';
302                                         memmove(name, host, len);
303                                         free(host);
304                                 }
305                                 for (speclen = strlen(name); 
306                                     speclen > 1 && name[speclen - 1] == '/';
307                                     speclen--)
308                                         name[speclen - 1] = '\0';
309                                 name[len + speclen + 1] = '\0';
310                                 checkmntlist(name, &mntfromname,
311                                     &mntonname, &type);
312                                 resolved = name;
313                         }
314                         /*
315                          * 4. Check if a relative mountpoint has been
316                          * specified. This should happen as last check,
317                          * the order is important. To prevent possible
318                          * nfs-hangs, we just call realpath(3) on the
319                          * basedir of mountpoint and add the dirname again.
320                          * Check the name in mounttable one last time.
321                          */
322                         if (mntfromname == NULL && mntonname == NULL) {
323                                 strcpy(name, origname);
324                                 if ((getrealname(name, realname)) != NULL) {
325                                         checkmntlist(realname,
326                                             &mntfromname, &mntonname, &type);
327                                         resolved = realname;
328                                 }
329                                 /*
330                                  * 5. All tests failed, just hand over the
331                                  * mountpoint to the kernel, maybe the statfs
332                                  * structure has been truncated or is not
333                                  * useful anymore because of a chroot(2).
334                                  * Please note that nfs will not be able to
335                                  * notify the nfs-server about unmounting.
336                                  * These things can change in future when the
337                                  * fstat structure get's more reliable,
338                                  * but at the moment we cannot thrust it.
339                                  */
340                                 if (mntfromname == NULL && mntonname == NULL) {
341                                         strcpy(name, origname);
342                                         if (umountfs(NULL, origname,
343                                             none) == 0) {;
344                                                 warnx("%s not found in "
345                                                     "mount table, "
346                                                     "unmounted it anyway",
347                                                     origname);
348                                                 free(origname);
349                                                 return (0);
350                                         } else
351                                                 free(origname);
352                                                 return (1);
353                                 }
354                         }
355                 }
356                 free(origname);
357         } else
358                 resolved = name;
359
360         if (checkvfsname(type, typelist))
361                 return (1);
362
363         /*
364          * Check if the reverse entrys of the mounttable are really the
365          * same as the normal ones.
366          */
367         if ((mntfromnamerev = strdup(getmntname(getmntname(mntfromname,
368             NULL, MNTON, &type, NAME), NULL, MNTFROM, &type, NAME))) == NULL)
369                 err(1, "strdup");
370         /*
371          * Mark the uppermost mount as unmounted.
372          */
373         getmntname(mntfromname, mntonname, NOTHING, &type, MARK);
374         /*
375          * If several equal mounts are in the mounttable, check the order
376          * and warn the user if necessary.
377          */
378         if (strcmp(mntfromnamerev, mntfromname ) != 0 &&
379             strcmp(resolved, mntonname) != 0) {
380                 warnx("cannot umount %s, %s\n        "
381                     "is mounted there, umount it first",
382                     mntonname, mntfromnamerev);
383
384                 /* call getmntname again to set mntcheck[i] to 0 */
385                 getmntname(mntfromname, mntonname,
386                     NOTHING, &type, UNMARK);
387                 return (1);
388         }
389         free(mntfromnamerev);
390         return (umountfs(mntfromname, mntonname, type));
391 }
392
393 /*
394  * NFS stuff and unmount(2) call
395  */
396 int
397 umountfs(char *mntfromname, char *mntonname, char *type)
398 {
399         enum clnt_stat clnt_stat;
400         struct timeval try;
401         struct addrinfo *ai, hints;
402         int do_rpc;
403         CLIENT *clp;
404         char *nfsdirname, *orignfsdirname;
405         char *hostp, *delimp;
406
407         ai = NULL;
408         do_rpc = 0;
409         hostp = NULL;
410         nfsdirname = delimp = orignfsdirname = NULL;
411         memset(&hints, 0, sizeof hints);
412
413         if (strcmp(type, "nfs") == 0) {
414                 if ((nfsdirname = strdup(mntfromname)) == NULL)
415                         err(1, "strdup");
416                 orignfsdirname = nfsdirname;
417                 if ((delimp = strrchr(nfsdirname, ':')) != NULL) {
418                         *delimp = '\0';
419                         hostp = nfsdirname;
420                         getaddrinfo(hostp, NULL, &hints, &ai);
421                         if (ai == NULL) {
422                                 warnx("can't get net id for host");
423                         }
424                         nfsdirname = delimp + 1;
425                 }
426
427                 /*
428                  * Check if we have to start the rpc-call later.
429                  * If there are still identical nfs-names mounted,
430                  * we skip the rpc-call. Obviously this has to
431                  * happen before unmount(2), but it should happen
432                  * after the previous namecheck.
433                  * A non-NULL return means that this is the last
434                  * mount from mntfromname that is still mounted.
435                  */
436                 if (getmntname(mntfromname, NULL, NOTHING, &type, COUNT)
437                      != NULL)
438                         do_rpc = 1;
439         }
440
441         if (!namematch(ai))
442                 return (1);
443         if (unmount(mntonname, fflag) != 0 ) {
444                 warn("unmount of %s failed", mntonname);
445                 return (1);
446         }
447         if (vflag)
448                 printf("%s: unmount from %s\n", mntfromname, mntonname);
449         /*
450          * Report to mountd-server which nfsname
451          * has been unmounted.
452          */
453         if (ai != NULL && !(fflag & MNT_FORCE) && do_rpc) {
454                 clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1, "udp");
455                 if (clp  == NULL) {
456                         warnx("%s: %s", hostp,
457                             clnt_spcreateerror("RPCPROG_MNT"));
458                         return (1);
459                 }
460                 clp->cl_auth = authsys_create_default();
461                 try.tv_sec = 20;
462                 try.tv_usec = 0;
463                 clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, (xdrproc_t)xdr_dir,
464                     nfsdirname, (xdrproc_t)xdr_void, (caddr_t)0, try);
465                 if (clnt_stat != RPC_SUCCESS) {
466                         warnx("%s: %s", hostp,
467                             clnt_sperror(clp, "RPCMNT_UMOUNT"));
468                         return (1);
469                 }
470                 /*
471                  * Remove the unmounted entry from /var/db/mounttab.
472                  */
473                 if (read_mtab()) {
474                         clean_mtab(hostp, nfsdirname, vflag);
475                         if(!write_mtab(vflag))
476                                 warnx("cannot remove mounttab entry %s:%s",
477                                     hostp, nfsdirname);
478                         free_mtab();
479                 }
480                 free(orignfsdirname);
481                 auth_destroy(clp->cl_auth);
482                 clnt_destroy(clp);
483         }
484         return (0);
485 }
486
487 char *
488 getmntname(const char *fromname, const char *onname,
489     mntwhat what, char **type, dowhat mark)
490 {
491         static struct statfs *mntbuf;
492         static int mntsize = 0;
493         static char *mntcheck = NULL;
494         static char *mntcount = NULL;
495         int i, count;
496
497         if (mntsize <= 0) {
498                 if ((mntsize = mntinfo(&mntbuf)) <= 0)
499                         return (NULL);
500         }
501         if (mntcheck == NULL) {
502                 if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL ||
503                     (mntcount = calloc(mntsize + 1, sizeof(int))) == NULL)
504                         err(1, "calloc");
505         }
506         /*
507          * We want to get the file systems in the reverse order
508          * that they were mounted. Mounted and unmounted filesystems
509          * are marked or unmarked in a table called 'mntcheck'.
510          * Unmount(const char *dir, int flags) does only take the
511          * mountpoint as argument, not the destination. If we don't pay
512          * attention to the order, it can happen that a overlaying
513          * filesystem get's unmounted instead of the one the user
514          * has choosen.
515          */
516         switch (mark) {
517         case NAME:
518                 /* Return only the specific name */
519                 for (i = mntsize - 1; i >= 0; i--) {
520                         if (fromname != NULL && what == MNTON &&
521                             !strcmp(mntbuf[i].f_mntfromname, fromname) &&
522                             mntcheck[i] != 1) {
523                                 if (type)
524                                         *type = mntbuf[i].f_fstypename;
525                                 return (mntbuf[i].f_mntonname);
526                         }
527                         if (fromname != NULL && what == MNTFROM &&
528                             !strcmp(mntbuf[i].f_mntonname, fromname) &&
529                             mntcheck[i] != 1) {
530                                 if (type)
531                                         *type = mntbuf[i].f_fstypename;
532                                 return (mntbuf[i].f_mntfromname);
533                         }
534                 }
535                 return (NULL);
536         case MARK:
537                 /* Mark current mount with '1' and return name */
538                 for (i = mntsize - 1; i >= 0; i--) {
539                         if (mntcheck[i] == 0 &&
540                             (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
541                             (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
542                                 mntcheck[i] = 1;
543                                 return (mntbuf[i].f_mntonname);
544                         }
545                 }
546                 return (NULL);
547         case UNMARK:
548                 /* Unmark current mount with '0' and return name */
549                 for (i = 0; i < mntsize; i++) {
550                         if (mntcheck[i] == 1 &&
551                             (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
552                             (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
553                                 mntcheck[i] = 0;
554                                 return (mntbuf[i].f_mntonname);
555                         }
556                 }
557                 return (NULL);
558         case COUNT:
559                 /* Count the equal mntfromnames */
560                 count = 0;
561                 for (i = mntsize - 1; i >= 0; i--) {
562                         if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)
563                                 count++;
564                 }
565                 /* Mark the already unmounted mounts and return
566                  * mntfromname if count <= 1. Else return NULL.
567                  */
568                 for (i = mntsize - 1; i >= 0; i--) {
569                         if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0) {
570                                 if (mntcount[i] == 1)
571                                         count--;
572                                 else {
573                                         mntcount[i] = 1;
574                                         break;
575                                 }
576                         }
577                 }
578                 if (count <= 1)
579                         return (mntbuf[i].f_mntonname);
580                 else
581                         return (NULL);
582         case FREE:
583                 free(mntbuf);
584                 free(mntcheck);
585                 free(mntcount);
586                 return (NULL);
587         default:
588                 return (NULL);
589         }
590 }
591
592 int
593 sacmp(struct sockaddr *sa1, struct sockaddr *sa2)
594 {
595         void *p1, *p2;
596         int len;
597
598         if (sa1->sa_family != sa2->sa_family)
599                 return (1);
600
601         switch (sa1->sa_family) {
602         case AF_INET:
603                 p1 = &((struct sockaddr_in *)sa1)->sin_addr;
604                 p2 = &((struct sockaddr_in *)sa2)->sin_addr;
605                 len = 4;
606                 break;
607         case AF_INET6:
608                 p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
609                 p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
610                 len = 16;
611                 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
612                     ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
613                         return (1);
614                 break;
615         default:
616                 return (1);
617         }
618
619         return memcmp(p1, p2, len);
620 }
621
622 int
623 namematch(struct addrinfo *ai)
624 {
625         struct addrinfo *aip;
626
627         if (nfshost == NULL || nfshost_ai == NULL)
628                 return (1);
629
630         while (ai != NULL) {
631                 aip = nfshost_ai;
632                 while (aip != NULL) {
633                         if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
634                                 return (1);
635                         aip = aip->ai_next;
636                 }
637                 ai = ai->ai_next;
638         }
639
640         return (0);
641 }
642
643 void
644 checkmntlist(char *name, char **fromname, char **onname, char **type)
645 {
646
647         *fromname = getmntname(name, NULL, MNTFROM, type, NAME);
648         if (*fromname == NULL) {
649                 *onname = getmntname(name, NULL, MNTON, type, NAME);
650                 if (*onname != NULL)
651                         *fromname = name;
652         } else
653                 *onname = name;
654 }
655
656 int
657 mntinfo(struct statfs **mntbuf)
658 {
659         static struct statfs *origbuf;
660         size_t bufsize;
661         int mntsize;
662
663         mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
664         if (mntsize <= 0)
665                 return (0);
666         bufsize = (mntsize + 1) * sizeof(struct statfs);
667         if ((origbuf = malloc(bufsize)) == NULL)
668                 err(1, "malloc");
669         mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT);
670         *mntbuf = origbuf;
671         return (mntsize);
672 }
673
674 char *
675 getrealname(char *name, char *realname)
676 {
677         char *dirname;
678         int havedir;
679         size_t baselen;
680         size_t dirlen;
681         
682         dirname = '\0';
683         havedir = 0;
684         if (*name == '/') {
685                 if (ISDOT(name + 1) || ISDOTDOT(name + 1))
686                         strcpy(realname, "/");
687                 else {
688                         if ((dirname = strrchr(name + 1, '/')) == NULL)
689                                 snprintf(realname, MAXPATHLEN, "%s", name);
690                         else
691                                 havedir = 1;
692                 }
693         } else {
694                 if (ISDOT(name) || ISDOTDOT(name))
695                         realpath(name, realname);
696                 else {
697                         if ((dirname = strrchr(name, '/')) == NULL) {
698                                 if ((realpath(name, realname)) == NULL)
699                                         return (NULL);
700                         } else 
701                                 havedir = 1;
702                 }
703         }
704         if (havedir) {
705                 *dirname++ = '\0';
706                 if (ISDOT(dirname)) {
707                         *dirname = '\0';
708                         if ((realpath(name, realname)) == NULL)
709                                 return (NULL);
710                 } else if (ISDOTDOT(dirname)) {
711                         *--dirname = '/';
712                         if ((realpath(name, realname)) == NULL)
713                                 return (NULL);
714                 } else {
715                         if ((realpath(name, realname)) == NULL)
716                                 return (NULL);
717                         baselen = strlen(realname);
718                         dirlen = strlen(dirname);
719                         if (baselen + dirlen + 1 > MAXPATHLEN)
720                                 return (NULL);
721                         if (realname[1] == '\0') {
722                                 memmove(realname + 1, dirname, dirlen);
723                                 realname[dirlen + 1] = '\0';
724                         } else {
725                                 realname[baselen] = '/';
726                                 memmove(realname + baselen + 1,
727                                     dirname, dirlen);
728                                 realname[baselen + dirlen + 1] = '\0';
729                         }
730                 }
731         }
732         return (realname);
733 }
734
735 /*
736  * xdr routines for mount rpc's
737  */
738 int
739 xdr_dir(XDR *xdrsp, char *dirp)
740 {
741
742         return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
743 }
744
745 void
746 usage(void)
747 {
748
749         fprintf(stderr, "%s\n%s\n",
750             "usage: umount [-fv] special | node",
751             "       umount -a | -A [-fv] [-h host] [-t type]");
752         exit(1);
753 }