do not trim leading pathname from special device name
[unix-history] / usr / src / sbin / mount / mount.c
index 62a276f..262e57b 100644 (file)
@@ -22,7 +22,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)mount.c    5.15 (Berkeley) %G%";
+static char sccsid[] = "@(#)mount.c    5.27 (Berkeley) %G%";
 #endif /* not lint */
 
 #include "pathnames.h"
 #endif /* not lint */
 
 #include "pathnames.h"
@@ -34,9 +34,6 @@ static char sccsid[] = "@(#)mount.c   5.15 (Berkeley) %G%";
 #include <errno.h>
 #include <stdio.h>
 #include <strings.h>
 #include <errno.h>
 #include <stdio.h>
 #include <strings.h>
-#include <sys/dir.h>
-#include <sys/uio.h>
-#include <sys/namei.h>
 #include <sys/mount.h>
 #ifdef NFS
 #include <sys/socket.h>
 #include <sys/mount.h>
 #ifdef NFS
 #include <sys/socket.h>
@@ -56,8 +53,9 @@ static char sccsid[] = "@(#)mount.c   5.15 (Berkeley) %G%";
 #define        SETTYPE(type) \
        (!strcmp(type, FSTAB_RW) || !strcmp(type, FSTAB_RQ))
 
 #define        SETTYPE(type) \
        (!strcmp(type, FSTAB_RW) || !strcmp(type, FSTAB_RQ))
 
-int fake, verbose, mnttype;
+int fake, verbose, updateflg, mnttype;
 char *mntname, **envp;
 char *mntname, **envp;
+char **vfslist, **makevfslist();
 
 #ifdef NFS
 int xdr_dir(), xdr_fh();
 
 #ifdef NFS
 int xdr_dir(), xdr_fh();
@@ -77,7 +75,8 @@ struct nfhret {
        u_long  stat;
        nfsv2fh_t nfh;
 };
        u_long  stat;
        nfsv2fh_t nfh;
 };
-int retrycnt = 10000;
+#define        DEF_RETRY       10000
+int retrycnt;
 #define        BGRND   1
 #define        ISBGRND 2
 int opflags = 0;
 #define        BGRND   1
 #define        ISBGRND 2
 int opflags = 0;
@@ -92,9 +91,9 @@ main(argc, argv, arge)
        extern int optind;
        register struct fstab *fs;
        register int cnt;
        extern int optind;
        register struct fstab *fs;
        register int cnt;
-       int all, ch, rval, sfake, i;
+       int all, ch, rval, flags, i;
        long mntsize;
        long mntsize;
-       struct statfs *mntbuf;
+       struct statfs *mntbuf, *getmntpt();
        char *type, *options = NULL;
 
        envp = arge;
        char *type, *options = NULL;
 
        envp = arge;
@@ -102,7 +101,7 @@ main(argc, argv, arge)
        type = NULL;
        mnttype = MOUNT_UFS;
        mntname = "ufs";
        type = NULL;
        mnttype = MOUNT_UFS;
        mntname = "ufs";
-       while ((ch = getopt(argc, argv, "afrwvt:o:")) != EOF)
+       while ((ch = getopt(argc, argv, "afrwuvt:o:")) != EOF)
                switch((char)ch) {
                case 'a':
                        all = 1;
                switch((char)ch) {
                case 'a':
                        all = 1;
@@ -113,6 +112,9 @@ main(argc, argv, arge)
                case 'r':
                        type = FSTAB_RO;
                        break;
                case 'r':
                        type = FSTAB_RO;
                        break;
+               case 'u':
+                       updateflg = M_UPDATE;
+                       break;
                case 'v':
                        verbose = 1;
                        break;
                case 'v':
                        verbose = 1;
                        break;
@@ -123,6 +125,7 @@ main(argc, argv, arge)
                        options = optarg;
                        break;
                case 't':
                        options = optarg;
                        break;
                case 't':
+                       vfslist = makevfslist(optarg);
                        mnttype = getmnttype(optarg);
                        break;
                case '?':
                        mnttype = getmnttype(optarg);
                        break;
                case '?':
@@ -137,20 +140,19 @@ main(argc, argv, arge)
 
        if (all) {
                rval = 0;
 
        if (all) {
                rval = 0;
-               for (sfake = fake; fs = getfsent(); fake = sfake) {
+               while (fs = getfsent()) {
                        if (BADTYPE(fs->fs_type))
                                continue;
                        if (BADTYPE(fs->fs_type))
                                continue;
+                       if (badvfstype(fs->fs_vfstype, vfslist))
+                               continue;
                        /* `/' is special, it's always mounted */
                        if (!strcmp(fs->fs_file, "/"))
                        /* `/' is special, it's always mounted */
                        if (!strcmp(fs->fs_file, "/"))
-                               fake = 1;
-                       if ((mnttype = getmnttype(fs->fs_vfstype)) == 0) {
-                               fprintf(stderr,
-                                   "%s %s type of file system is unknown.\n",
-                                   "mount:", fs->fs_vfstype);
-                               continue;
-                       }
-                       rval |= mountfs(fs->fs_spec, fs->fs_file,
-                           type ? type : fs->fs_type, options, fs->fs_mntops);
+                               flags = M_UPDATE;
+                       else
+                               flags = updateflg;
+                       mnttype = getmnttype(fs->fs_vfstype);
+                       rval |= mountfs(fs->fs_spec, fs->fs_file, flags,
+                           type, options, fs->fs_mntops);
                }
                exit(rval);
        }
                }
                exit(rval);
        }
@@ -158,7 +160,7 @@ main(argc, argv, arge)
        if (argc == 0) {
                if (verbose || fake || type)
                        usage();
        if (argc == 0) {
                if (verbose || fake || type)
                        usage();
-               if ((mntsize = getmntinfo(&mntbuf)) == 0) {
+               if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
                        fprintf(stderr,
                                "mount: cannot get mount information\n");
                        exit(1);
                        fprintf(stderr,
                                "mount: cannot get mount information\n");
                        exit(1);
@@ -169,6 +171,22 @@ main(argc, argv, arge)
                exit(0);
        }
 
                exit(0);
        }
 
+       if (argc == 1 && updateflg) {
+               if ((mntbuf = getmntpt(*argv)) == NULL) {
+                       fprintf(stderr,
+                           "mount: unknown special file or file system %s.\n",
+                           *argv);
+                       exit(1);
+               }
+               mnttype = mntbuf->f_type;
+               if (!strcmp(mntbuf->f_mntfromname, "root_device")) {
+                       fs = getfsfile("/");
+                       strcpy(mntbuf->f_mntfromname, fs->fs_spec);
+               }
+               exit(mountfs(mntbuf->f_mntfromname, mntbuf->f_mntonname,
+                   updateflg, type, options, NULL));
+       }
+
        if (argc == 1) {
                if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) {
                        fprintf(stderr,
        if (argc == 1) {
                if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) {
                        fprintf(stderr,
@@ -181,83 +199,73 @@ main(argc, argv, arge)
                            "mount: %s has unknown file system type.\n", *argv);
                        exit(1);
                }
                            "mount: %s has unknown file system type.\n", *argv);
                        exit(1);
                }
-               if ((mnttype = getmnttype(fs->fs_vfstype)) == 0) {
-                       fprintf(stderr,
-                           "mount: %s type of file system is unknown.\n",
-                           fs->fs_vfstype);
-                       exit(1);
-               }
-               exit(mountfs(fs->fs_spec, fs->fs_file,
-                   type ? type : fs->fs_type, options, fs->fs_mntops));
+               mnttype = getmnttype(fs->fs_vfstype);
+               exit(mountfs(fs->fs_spec, fs->fs_file, updateflg,
+                   type, options, fs->fs_mntops));
        }
 
        if (argc != 2)
                usage();
 
        }
 
        if (argc != 2)
                usage();
 
-       exit(mountfs(argv[0], argv[1], type ? type : "rw", options, NULL));
+       exit(mountfs(argv[0], argv[1], updateflg, type, options, NULL));
 }
 
 }
 
-mountfs(spec, name, type, options, mntopts)
+mountfs(spec, name, flags, type, options, mntopts)
        char *spec, *name, *type, *options, *mntopts;
        char *spec, *name, *type, *options, *mntopts;
+       int flags;
 {
        extern int errno;
        register int cnt;
 {
        extern int errno;
        register int cnt;
-       int flags, argc, status, i;
+       int argc, status, i;
        struct ufs_args args;
        char *argp, *argv[50];
        char execname[MAXPATHLEN + 1], flagval[12];
 
        struct ufs_args args;
        char *argp, *argv[50];
        char execname[MAXPATHLEN + 1], flagval[12];
 
-       flags = 0;
-       if (!strcmp(type, FSTAB_RO))
-               flags |= M_RDONLY;
-       if (options)
-               getstdopts(options, &flags);
        if (mntopts)
                getstdopts(mntopts, &flags);
        if (mntopts)
                getstdopts(mntopts, &flags);
-       argc = 1;
+       if (options)
+               getstdopts(options, &flags);
+       if (type)
+               getstdopts(type, &flags);
        switch (mnttype) {
        case MOUNT_UFS:
        switch (mnttype) {
        case MOUNT_UFS:
-               if (options)
-                       getufsopts(options, &flags);
                if (mntopts)
                        getufsopts(mntopts, &flags);
                if (mntopts)
                        getufsopts(mntopts, &flags);
+               if (options)
+                       getufsopts(options, &flags);
                args.fspec = spec;
                argp = (caddr_t)&args;
                break;
 
 #ifdef NFS
        case MOUNT_NFS:
                args.fspec = spec;
                argp = (caddr_t)&args;
                break;
 
 #ifdef NFS
        case MOUNT_NFS:
-               if (options)
-                       getnfsopts(options, &nfsargs, &opflags,
-                               &retrycnt);
+               retrycnt = DEF_RETRY;
                if (mntopts)
                if (mntopts)
-                       getnfsopts(mntopts, &nfsargs, &opflags,
-                               &retrycnt);
+                       getnfsopts(mntopts, &nfsargs, &opflags, &retrycnt);
+               if (options)
+                       getnfsopts(options, &nfsargs, &opflags, &retrycnt);
                if (argp = getnfsargs(spec, name, type))
                        break;
                return (1);
 #endif /* NFS */
 
                if (argp = getnfsargs(spec, name, type))
                        break;
                return (1);
 #endif /* NFS */
 
-#ifdef MFS
        case MOUNT_MFS:
        case MOUNT_MFS:
-               mntname = "memfs";
-               if (options)
-                       argc += getmfsopts(options, &argv[argc]);
-               if (mntopts)
-                       argc += getmfsopts(mntopts, &argv[argc]);
-               /* fall through to */
-#endif /* MFS */
-
        default:
                argv[0] = mntname;
        default:
                argv[0] = mntname;
+               argc = 1;
                if (flags) {
                        argv[argc++] = "-F";
                        sprintf(flagval, "%d", flags);
                        argv[argc++] = flagval;
                }
                if (flags) {
                        argv[argc++] = "-F";
                        sprintf(flagval, "%d", flags);
                        argv[argc++] = flagval;
                }
+               if (mntopts)
+                       argc += getexecopts(mntopts, &argv[argc]);
+               if (options)
+                       argc += getexecopts(options, &argv[argc]);
                argv[argc++] = spec;
                argv[argc++] = name;
                argv[argc++] = spec;
                argv[argc++] = name;
-               sprintf(execname, "%s/%s", _PATH_EXECDIR, mntname);
+               argv[argc++] = NULL;
+               sprintf(execname, "%s/mount_%s", _PATH_EXECDIR, mntname);
                if (verbose) {
                        printf("exec: %s", execname);
                        for (i = 1; i < argc; i++)
                if (verbose) {
                        printf("exec: %s", execname);
                        for (i = 1; i < argc; i++)
@@ -279,7 +287,9 @@ mountfs(spec, name, type, options, mntopts)
                        goto out;
                }
                execve(execname, argv, envp);
                        goto out;
                }
                execve(execname, argv, envp);
-               perror(execname);
+               fprintf(stderr, "mount: cannot exec %s for %s: ",
+                       execname, name);
+               perror("");
                exit (1);
                /* NOTREACHED */
 
                exit (1);
                /* NOTREACHED */
 
@@ -293,7 +303,14 @@ mountfs(spec, name, type, options, mntopts)
                        fprintf(stderr, "Mount table full\n");
                        break;
                case EINVAL:
                        fprintf(stderr, "Mount table full\n");
                        break;
                case EINVAL:
-                       fprintf(stderr, "Bogus super block\n");
+                       if (flags & M_UPDATE)
+                               fprintf(stderr, "Specified device does %s\n",
+                                       "not match mounted device");
+                       else
+                               fprintf(stderr, "Bogus super block\n");
+                       break;
+               case EOPNOTSUPP:
+                       fprintf(stderr, "Operation not supported\n");
                        break;
                default:
                        perror((char *)NULL);
                        break;
                default:
                        perror((char *)NULL);
@@ -317,18 +334,9 @@ prmount(spec, name, flags)
        char *spec, *name;
        long flags;
 {
        char *spec, *name;
        long flags;
 {
-       register char *root;
 
        if (opflags & ISBGRND)
                return;
 
        if (opflags & ISBGRND)
                return;
-       /*
-        * trim trailing /'s and find last component of name
-        */
-       for (root = index(spec, '\0'); *--root == '/';)
-               /* void */;
-       *++root = '\0';
-       if (root = rindex(spec, '/'))
-               spec = root + 1;
        printf("%s on %s", spec, name);
        if (flags & M_RDONLY)
                printf(" (read-only)");
        printf("%s on %s", spec, name);
        if (flags & M_RDONLY)
                printf(" (read-only)");
@@ -340,6 +348,8 @@ prmount(spec, name, flags)
                printf(" (nodev)");
        if (flags & M_SYNCHRONOUS)
                printf(" (synchronous)");
                printf(" (nodev)");
        if (flags & M_SYNCHRONOUS)
                printf(" (synchronous)");
+       if (flags & M_UPDATE)
+               printf(" (update only)");
        printf("\n");
 }
 
        printf("\n");
 }
 
@@ -359,7 +369,7 @@ getmnttype(fstype)
 
 usage()
 {
 
 usage()
 {
-       fprintf(stderr, "usage: mount [-afrw]\nor mount [-frw] special | node\nor mount [-frw] special node\n");
+       fprintf(stderr, "usage: mount [-afurw]\nor mount [-furw] special | node\nor mount [-furw] special node\n");
        exit(1);
 }
 
        exit(1);
 }
 
@@ -379,24 +389,40 @@ getstdopts(options, flagp)
                } else {
                        negative = 0;
                }
                } else {
                        negative = 0;
                }
+               if (!negative && !strcasecmp(opt, FSTAB_RO)) {
+                       *flagp |= M_RDONLY;
+                       continue;
+               }
+               if (!negative && !strcasecmp(opt, FSTAB_RW)) {
+                       *flagp &= ~M_RDONLY;
+                       continue;
+               }
                if (!strcasecmp(opt, "exec")) {
                        if (negative)
                                *flagp |= M_NOEXEC;
                if (!strcasecmp(opt, "exec")) {
                        if (negative)
                                *flagp |= M_NOEXEC;
+                       else
+                               *flagp &= ~M_NOEXEC;
                        continue;
                }
                if (!strcasecmp(opt, "suid")) {
                        if (negative)
                                *flagp |= M_NOSUID;
                        continue;
                }
                if (!strcasecmp(opt, "suid")) {
                        if (negative)
                                *flagp |= M_NOSUID;
+                       else
+                               *flagp &= ~M_NOSUID;
                        continue;
                }
                if (!strcasecmp(opt, "dev")) {
                        if (negative)
                                *flagp |= M_NODEV;
                        continue;
                }
                if (!strcasecmp(opt, "dev")) {
                        if (negative)
                                *flagp |= M_NODEV;
+                       else
+                               *flagp &= ~M_NODEV;
                        continue;
                }
                if (!strcasecmp(opt, "synchronous")) {
                        if (!negative)
                                *flagp |= M_SYNCHRONOUS;
                        continue;
                }
                if (!strcasecmp(opt, "synchronous")) {
                        if (!negative)
                                *flagp |= M_SYNCHRONOUS;
+                       else
+                               *flagp &= ~M_SYNCHRONOUS;
                        continue;
                }
        }
                        continue;
                }
        }
@@ -410,8 +436,7 @@ getufsopts(options, flagp)
        return;
 }
 
        return;
 }
 
-#ifdef MFS
-getmfsopts(options, argv)
+getexecopts(options, argv)
        char *options;
        char **argv;
 {
        char *options;
        char **argv;
 {
@@ -430,7 +455,71 @@ getmfsopts(options, argv)
        }
        return (argc);
 }
        }
        return (argc);
 }
-#endif /* MFS */
+
+struct statfs *
+getmntpt(name)
+       char *name;
+{
+       long mntsize;
+       register long i;
+       struct statfs *mntbuf;
+
+       mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
+       for (i = 0; i < mntsize; i++) {
+               if (!strcmp(mntbuf[i].f_mntfromname, name) ||
+                   !strcmp(mntbuf[i].f_mntonname, name))
+                       return (&mntbuf[i]);
+       }
+       return ((struct statfs *)0);
+}
+
+static int skipvfs;
+
+badvfstype(vfstype, vfslist)
+       char *vfstype;
+       char **vfslist;
+{
+
+       if (vfslist == 0)
+               return(0);
+       while (*vfslist) {
+               if (strcmp(vfstype, *vfslist) == 0)
+                       return(skipvfs);
+               vfslist++;
+       }
+       return (!skipvfs);
+}
+
+char **
+makevfslist(fslist)
+       char *fslist;
+{
+       register char **av, *nextcp;
+       register int i;
+       char *malloc();
+
+       if (fslist == NULL)
+               return (NULL);
+       if (fslist[0] == 'n' && fslist[1] == 'o') {
+               fslist += 2;
+               skipvfs = 1;
+       }
+       for (i = 0, nextcp = fslist; *nextcp; nextcp++)
+               if (*nextcp == ',')
+                       i++;
+       av = (char **)malloc((i+2) * sizeof(char *));
+       if (av == NULL)
+               return (NULL);
+       nextcp = fslist;
+       i = 0;
+       av[i++] = nextcp;
+       while (nextcp = index(nextcp, ',')) {
+               *nextcp++ = '\0';
+               av[i++] = nextcp;
+       }
+       av[i++] = 0;
+       return (av);
+}
 
 #ifdef NFS
 /*
 
 #ifdef NFS
 /*
@@ -488,16 +577,17 @@ char *
 getnfsargs(spec)
        char *spec;
 {
 getnfsargs(spec)
        char *spec;
 {
+       extern int errno;
        register CLIENT *clp;
        struct hostent *hp;
        register CLIENT *clp;
        struct hostent *hp;
-       struct sockaddr_in saddr;
+       static struct sockaddr_in saddr;
        struct timeval pertry, try;
        enum clnt_stat clnt_stat;
        int so = RPC_ANYSOCK;
        char *hostp, *delimp;
        u_short tport;
        struct timeval pertry, try;
        enum clnt_stat clnt_stat;
        int so = RPC_ANYSOCK;
        char *hostp, *delimp;
        u_short tport;
-       struct nfhret nfhret;
-       char nam[MNAMELEN + 1];
+       static struct nfhret nfhret;
+       static char nam[MNAMELEN + 1];
 
        strncpy(nam, spec, MNAMELEN);
        nam[MNAMELEN] = '\0';
 
        strncpy(nam, spec, MNAMELEN);
        nam[MNAMELEN] = '\0';
@@ -563,8 +653,9 @@ getnfsargs(spec)
        if (nfhret.stat) {
                if (opflags & ISBGRND)
                        exit(1);
        if (nfhret.stat) {
                if (opflags & ISBGRND)
                        exit(1);
-               fprintf(stderr, "Can't access %s, errno=%d\n", spec,
-                   nfhret.stat);
+               fprintf(stderr, "Can't access %s: ", spec);
+               errno = nfhret.stat;
+               perror(NULL);
                return (0);
        }
        saddr.sin_port = htons(tport);
                return (0);
        }
        saddr.sin_port = htons(tport);