BSD 4_3_Net_2 release
[unix-history] / usr / src / sbin / umount / umount.c
index 9361ae2..750cb43 100644 (file)
@@ -1,29 +1,50 @@
-/*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+/*-
+ * Copyright (c) 1980, 1989 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
 char copyright[] =
  */
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+"@(#) Copyright (c) 1980, 1989 The Regents of the University of California.\n\
  All rights reserved.\n";
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)umount.c   5.7 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)umount.c   5.16 (Berkeley) 6/3/91";
+#endif /* not lint */
 
 
-/*
- * umount
- */
 #include <sys/param.h>
 #include <sys/param.h>
-#include <stdio.h>
-#include <fstab.h>
 #include <sys/stat.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
 #include <sys/mount.h>
-#include <sys/vnode.h>
-#include <ufs/inode.h>
+
 #ifdef NFS
 #include <sys/time.h>
 #include <sys/socket.h>
 #ifdef NFS
 #include <sys/time.h>
 #include <sys/socket.h>
@@ -35,61 +56,111 @@ static char sccsid[] = "@(#)umount.c       5.7 (Berkeley) %G%";
 #include <nfs/rpcv2.h>
 #endif
 
 #include <nfs/rpcv2.h>
 #endif
 
+#include <fstab.h>
+#include <stdio.h>
+#include <string.h>
+
 #ifdef NFS
 #ifdef NFS
-extern int errno;
 int xdr_dir();
 int xdr_dir();
-char   *index();
+char *nfshost;
 #endif
 #endif
-int    vflag, all, errs;
-char   *rindex(), *getmntinfo();
-#define MNTON  1
-#define MNTFROM        2
+
+int    vflag, all, errs, fake;
+int    fflag = MNT_NOFORCE;
+char   *getmntname();
+
+#define        MNTON   1
+#define        MNTFROM 2
+#define        MNTTYPE 3
+
+int *typelist, *maketypelist();
 
 main(argc, argv)
        int argc;
        char **argv;
 {
 
 main(argc, argv)
        int argc;
        char **argv;
 {
+       extern char *optarg;
+       extern int optind;
+       int ch;
 
 
-       argc--, argv++;
        sync();
        sync();
-again:
-       if (argc > 0 && !strcmp(*argv, "-v")) {
-               vflag++;
-               argc--, argv++;
-               goto again;
-       }
-       if (argc > 0 && !strcmp(*argv, "-a")) {
-               all++;
-               argc--, argv++;
-               goto again;
-       }
-       if (argc == 0 && !all) {
-               fprintf(stderr, "Usage: umount [ -a ] [ -v ] [ dev ... ]\n");
-               exit(1);
-       }
+       while ((ch = getopt(argc, argv, "afFh:t:v")) != EOF)
+               switch((char)ch) {
+               case 'v':
+                       vflag++;
+                       break;
+               case 'f':
+                       fflag = MNT_FORCE;
+                       break;
+               case 'F':
+                       fake++;
+                       break;
+               case 'a':
+                       all++;
+                       break;
+               case 't':
+                       typelist = maketypelist(optarg);
+                       break;
+#ifdef NFS
+               case 'h':
+                       /* -h flag implies -a, and "-t nfs" if no -t flag */
+                       nfshost = optarg;
+                       all++;
+                       if (typelist == NULL)
+                               typelist = maketypelist("nfs");
+                       break;
+#endif /* NFS */
+               case '?':
+               default:
+                       usage();
+                       /* NOTREACHED */
+               }
+       argc -= optind;
+       argv += optind;
+
+       if (argc == 0 && !all)
+               usage();
        if (all) {
        if (all) {
+               if (argc > 0)
+                       usage();
                if (setfsent() == 0)
                        perror(FSTAB), exit(1);
                if (setfsent() == 0)
                        perror(FSTAB), exit(1);
-               umountall();
+               umountall(typelist);
                exit(0);
        } else
                setfsent();
        while (argc > 0) {
                exit(0);
        } else
                setfsent();
        while (argc > 0) {
-               if (umountfs(*argv++) == 0)
+               if (umountfs(*argv++, 0) == 0)
                        errs++;
                argc--;
        }
        exit(errs);
 }
 
                        errs++;
                argc--;
        }
        exit(errs);
 }
 
-umountall()
+usage()
 {
 {
-       struct fstab *fs, *allocfsent();
+       fprintf(stderr,
+               "%s\n%s\n",
+               "Usage: umount [-fv] special | node",
+#ifndef        NFS
+               "    or umount -a[fv] [-t fstypelist]"
+#else
+               "    or umount -a[fv] [-h host] [-t fstypelist]"
+#endif
+             );
+       exit(1);
+}
+
+umountall(typelist)
+       char **typelist;
+{
+       register struct fstab *fs;
+       struct fstab *allocfsent();
 
        if ((fs = getfsent()) == (struct fstab *)0)
                return;
        fs = allocfsent(fs);
 
        if ((fs = getfsent()) == (struct fstab *)0)
                return;
        fs = allocfsent(fs);
-       umountall();
+       umountall(typelist);
        if (strcmp(fs->fs_file, "/") == 0) {
                freefsent(fs);
                return;
        if (strcmp(fs->fs_file, "/") == 0) {
                freefsent(fs);
                return;
@@ -100,7 +171,7 @@ umountall()
                freefsent(fs);
                return;
        }
                freefsent(fs);
                return;
        }
-       (void) umountfs(fs->fs_file);
+       (void) umountfs(fs->fs_file, typelist);
        freefsent(fs);
 }
 
        freefsent(fs);
 }
 
@@ -110,16 +181,15 @@ allocfsent(fs)
 {
        register struct fstab *new;
        register char *cp;
 {
        register struct fstab *new;
        register char *cp;
-       char *malloc();
 
        new = (struct fstab *)malloc((unsigned)sizeof (*fs));
 
        new = (struct fstab *)malloc((unsigned)sizeof (*fs));
-       cp = malloc((unsigned)strlen(fs->fs_file) + 1);
+       cp = (char *)malloc((unsigned)strlen(fs->fs_file) + 1);
        strcpy(cp, fs->fs_file);
        new->fs_file = cp;
        strcpy(cp, fs->fs_file);
        new->fs_file = cp;
-       cp = malloc((unsigned)strlen(fs->fs_type) + 1);
+       cp = (char *)malloc((unsigned)strlen(fs->fs_type) + 1);
        strcpy(cp, fs->fs_type);
        new->fs_type = cp;
        strcpy(cp, fs->fs_type);
        new->fs_type = cp;
-       cp = malloc((unsigned)strlen(fs->fs_spec) + 1);
+       cp = (char *)malloc((unsigned)strlen(fs->fs_spec) + 1);
        strcpy(cp, fs->fs_spec);
        new->fs_spec = cp;
        new->fs_passno = fs->fs_passno;
        strcpy(cp, fs->fs_spec);
        new->fs_spec = cp;
        new->fs_passno = fs->fs_passno;
@@ -140,50 +210,76 @@ freefsent(fs)
        free((char *)fs);
 }
 
        free((char *)fs);
 }
 
-umountfs(name)
+umountfs(name, typelist)
        char *name;
        char *name;
+       int *typelist;
 {
        char *mntpt;
        struct stat stbuf;
 {
        char *mntpt;
        struct stat stbuf;
+       int type;
 #ifdef NFS
        register CLIENT *clp;
 #ifdef NFS
        register CLIENT *clp;
-       struct hostent *hp;
+       struct hostent *hp = 0;
        struct sockaddr_in saddr;
        struct timeval pertry, try;
        enum clnt_stat clnt_stat;
        int so = RPC_ANYSOCK;
        char *hostp, *delimp;
        struct sockaddr_in saddr;
        struct timeval pertry, try;
        enum clnt_stat clnt_stat;
        int so = RPC_ANYSOCK;
        char *hostp, *delimp;
-#endif
+#endif /* NFS */
 
        if (stat(name, &stbuf) < 0) {
 
        if (stat(name, &stbuf) < 0) {
-               if ((mntpt = getmntinfo(name, MNTON)) == 0)
+               if (getmntname(name, MNTFROM, &type) != 0)
+                       mntpt = name;
+               else if ((mntpt = getmntname(name, MNTON, &type)) == 0) {
+                       fprintf(stderr, "%s: not currently mounted\n", name);
                        return (0);
                        return (0);
-       } else if ((stbuf.st_mode & IFMT) == IFBLK) {
-               if ((mntpt = getmntinfo(name, MNTON)) == 0)
+               }
+       } else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
+               if ((mntpt = getmntname(name, MNTON, &type)) == 0) {
+                       fprintf(stderr, "%s: not currently mounted\n", name);
                        return (0);
                        return (0);
-       } else if ((stbuf.st_mode & IFMT) == IFDIR) {
+               }
+       } else if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
                mntpt = name;
                mntpt = name;
-               if ((name = getmntinfo(mntpt, MNTFROM)) == 0)
+               if (getmntname(mntpt, MNTFROM, &type) == 0) {
+                       fprintf(stderr, "%s: not currently mounted\n", name);
                        return (0);
                        return (0);
+               }
        } else {
                fprintf(stderr, "%s: not a directory or special device\n",
                        name);
                return (0);
        }
        } else {
                fprintf(stderr, "%s: not a directory or special device\n",
                        name);
                return (0);
        }
-       if (umount(mntpt, MNT_NOFORCE) < 0) {
+
+       if (badtype(type, typelist))
+               return(1);
+#ifdef NFS
+       if ((delimp = index(name, '@')) != NULL) {
+               hostp = delimp + 1;
+               *delimp = '\0';
+               hp = gethostbyname(hostp);
+               *delimp = '@';
+       } else if ((delimp = index(name, ':')) != NULL) {
+               *delimp = '\0';
+               hostp = name;
+               hp = gethostbyname(hostp);
+               name = delimp+1;
+               *delimp = ':';
+       }
+
+       if (!namematch(hp, nfshost))
+               return(1);
+#endif /* NFS */
+       if (!fake && unmount(mntpt, fflag) < 0) {
                perror(mntpt);
                return (0);
        }
        if (vflag)
                fprintf(stderr, "%s: Unmounted from %s\n", name, mntpt);
                perror(mntpt);
                return (0);
        }
        if (vflag)
                fprintf(stderr, "%s: Unmounted from %s\n", name, mntpt);
-#ifdef NFS
-       if ((delimp = index(name, '@')) != NULL) {
-               hostp = delimp + 1;
+
+#ifdef NFS
+       if (!fake && hp != NULL && (fflag & MNT_FORCE) == 0) {
                *delimp = '\0';
                *delimp = '\0';
-       } else {
-               return (1);
-       }
-       if ((hp = gethostbyname(hostp)) != NULL) {
                bcopy(hp->h_addr,(caddr_t)&saddr.sin_addr,hp->h_length);
                saddr.sin_family = AF_INET;
                saddr.sin_port = 0;
                bcopy(hp->h_addr,(caddr_t)&saddr.sin_addr,hp->h_length);
                saddr.sin_family = AF_INET;
                saddr.sin_port = 0;
@@ -206,48 +302,119 @@ umountfs(name)
                auth_destroy(clp->cl_auth);
                clnt_destroy(clp);
        }
                auth_destroy(clp->cl_auth);
                clnt_destroy(clp);
        }
-#endif NFS
+#endif /* NFS */
        return (1);
 }
 
 char *
        return (1);
 }
 
 char *
-getmntinfo(name, what)
+getmntname(name, what, type)
        char *name;
        int what;
        char *name;
        int what;
+       int *type;
 {
        int mntsize, i;
        struct statfs *mntbuf;
 
 {
        int mntsize, i;
        struct statfs *mntbuf;
 
-       if ((mntsize = getfsstat((struct statfs *)0, 0)) < 0) {
+       if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
                perror("umount");
                return (0);
        }
                perror("umount");
                return (0);
        }
-       mntbuf = 0;
-       do {
-               if (mntbuf)
-                       free((char *)mntbuf);
-               i = (mntsize + 1) * sizeof(struct statfs);
-               if ((mntbuf = (struct statfs *)malloc((unsigned)i)) == 0) {
-                       fprintf(stderr,
-                               "no space for umount table buffer\n");
-                       return (0);
-               }
-               if ((mntsize = getfsstat(mntbuf, i)) < 0) {
-                       perror("umount");
-                       return (0);
-               }
-       } while (i == mntsize * sizeof(struct statfs));
        for (i = 0; i < mntsize; i++) {
        for (i = 0; i < mntsize; i++) {
-               if (what == MNTON && !strcmp(mntbuf[i].f_mntfromname, name))
+               if (what == MNTON && !strcmp(mntbuf[i].f_mntfromname, name)) {
+                       if (type)
+                               *type = mntbuf[i].f_type;
                        return (mntbuf[i].f_mntonname);
                        return (mntbuf[i].f_mntonname);
-               if (what == MNTFROM && !strcmp(mntbuf[i].f_mntonname, name))
+               }
+               if (what == MNTFROM && !strcmp(mntbuf[i].f_mntonname, name)) {
+                       if (type)
+                               *type = mntbuf[i].f_type;
                        return (mntbuf[i].f_mntfromname);
                        return (mntbuf[i].f_mntfromname);
+               }
        }
        }
-       fprintf(stderr, "%s: not currently mounted\n", name);
        return (0);
 }
 
        return (0);
 }
 
-#ifdef NFS
+static int skipvfs;
+
+badtype(type, typelist)
+       int type;
+       int *typelist;
+{
+       if (typelist == 0)
+               return(0);
+       while (*typelist) {
+               if (type == *typelist)
+                       return(skipvfs);
+               typelist++;
+       }
+       return(!skipvfs);
+}
+
+int *
+maketypelist(fslist)
+       char *fslist;
+{
+       register char *nextcp;
+       register int *av, i;
+
+       if (fslist == NULL)
+               return(NULL);
+       if (fslist[0] == 'n' && fslist[1] == 'o') {
+               fslist += 2;
+               skipvfs = 1;
+       } else
+               skipvfs = 0;
+       for (i = 0, nextcp = fslist; *nextcp; nextcp++)
+               if (*nextcp == ',')
+                       i++;
+       av = (int *)malloc((i+2) * sizeof(int));
+       if (av == NULL)
+               return(NULL);
+       for (i = 0; fslist; fslist = nextcp) {
+               if (nextcp = index(fslist, ','))
+                       *nextcp++ = '\0';
+               if (strcmp(fslist, "ufs") == 0)
+                       av[i++] = MOUNT_UFS;
+               else if (strcmp(fslist, "nfs") == 0)
+                       av[i++] = MOUNT_NFS;
+               else if (strcmp(fslist, "mfs") == 0)
+                       av[i++] = MOUNT_MFS;
+               else if (strcmp(fslist, "pc") == 0)
+                       av[i++] = MOUNT_PC;
+       }
+       av[i++] = 0;
+       return(av);
+}
+
+#ifdef NFS
+namematch(hp, nfshost)
+       struct hostent *hp;
+       char *nfshost;
+{
+       register char *cp;
+       register char **np;
+
+       if (hp == NULL || nfshost == NULL)
+               return(1);
+       if (strcasecmp(nfshost, hp->h_name) == 0)
+               return(1);
+       if (cp = index(hp->h_name, '.')) {
+               *cp = '\0';
+               if (strcasecmp(nfshost, hp->h_name) == 0)
+                       return(1);
+       }
+       for (np = hp->h_aliases; *np; np++) {
+               if (strcasecmp(nfshost, *np) == 0)
+                       return(1);
+               if (cp = index(*np, '.')) {
+                       *cp = '\0';
+                       if (strcasecmp(nfshost, *np) == 0)
+                               return(1);
+               }
+       }
+       return(0);
+}
+
 /*
  * xdr routines for mount rpc's
  */
 /*
  * xdr routines for mount rpc's
  */
@@ -257,4 +424,4 @@ xdr_dir(xdrsp, dirp)
 {
        return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
 }
 {
        return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
 }
-#endif NFS
+#endif /* NFS */