On Fri, 19 Sep 2014 16:08:33 +0200
Otto Moerbeek <[email protected]> wrote:

> On Fri, Sep 19, 2014 at 10:21:13PM +1000, Adam wrote:
> 
> > Patch for argument parsing below.
> > 
> > Diff taken in relation to Revision 3 of the aforementioned patch.
> > 
> > I do not understand why this should be a separate diff for this, as
> > it is essentially part of the functionality itself.
> > 
> > Let me know if you want it all together.
> 
> Well, this is not what I meant.
> 
> I meant: if you want to restructure, do not mix that with the
> additiont of functionality. 
> 
>       -Otto

In this case, Revision 4 (the whole lot together) below:




Index: sys/tmpfs/tmpfs_vfsops.c
===================================================================
RCS file: /cvs/src/sys/tmpfs/tmpfs_vfsops.c,v
retrieving revision 1.4
diff -u -p -u -r1.4 tmpfs_vfsops.c
--- sys/tmpfs/tmpfs_vfsops.c    12 Jul 2014 18:50:25 -0000      1.4
+++ sys/tmpfs/tmpfs_vfsops.c    19 Sep 2014 21:35:56 -0000
@@ -118,7 +118,21 @@ tmpfs_mount(struct mount *mp, const char
        }
 #endif
 
+       /*
+        * If updating, check whether changing from read-only to
+        * read/write; if there is no device name, that's all we do.
+        */
        if (mp->mnt_flag & MNT_UPDATE) {
+
+               /* Update if changing the read-only flag. */
+               if(mp->mnt_flag & MNT_RDONLY)
+                       return 0;
+
+               if(mp->mnt_flag & MNT_WANTRDWR) {
+                       mp->mnt_flag &= ~(MNT_WANTRDWR | MNT_RDONLY);
+                       return 0;
+               }
+
                /* TODO */
                return EOPNOTSUPP;
        }
Index: sbin/mount_tmpfs/mount_tmpfs.8
===================================================================
RCS file: /cvs/src/sbin/mount_tmpfs/mount_tmpfs.8,v
retrieving revision 1.3
diff -u -p -u -r1.3 mount_tmpfs.8
--- sbin/mount_tmpfs/mount_tmpfs.8      5 Feb 2014 15:32:26 -0000       1.3
+++ sbin/mount_tmpfs/mount_tmpfs.8      19 Sep 2014 21:35:56 -0000
@@ -43,6 +43,7 @@
 .Op Fl o Ar options
 .Op Fl s Ar size
 .Op Fl u Ar user
+.Op Fl P Ar template
 .Ar tmpfs
 .Ar mount_point
 .Sh DESCRIPTION
@@ -86,10 +87,28 @@ If zero is given (the default), the avai
 main memory and swap space) will be used.
 Note that four megabytes are always reserved for the system and cannot
 be assigned to the file system.
+.It Fl P Ar template
+If
+.Ar template
+is a directory, populate the created tmpfs file system with the
+contents of the directory.
+If
+.Ar template
+is a block device, populate the created tmpfs file system with the
+contents of the FFS file system contained on the device.
 .It Fl u Ar user
 Specifies the user name or UID of the root inode of the file system.
 Defaults to the mount point's UID.
 .El
+.Pp
+When the
+.Fl P Ar template
+option is used, permissions are always copied from the template.
+The
+.Fl u , Fl g , 
+and
+.Fl m
+options only affect the root inode of the file system.
 .Pp
 Every option that accepts a numerical value as its argument can take a
 trailing
Index: sbin/mount_tmpfs/mount_tmpfs.c
===================================================================
RCS file: /cvs/src/sbin/mount_tmpfs/mount_tmpfs.c,v
retrieving revision 1.4
diff -u -p -u -r1.4 mount_tmpfs.c
--- sbin/mount_tmpfs/mount_tmpfs.c      21 Jan 2014 21:58:27 -0000      1.4
+++ sbin/mount_tmpfs/mount_tmpfs.c      19 Sep 2014 21:35:56 -0000
@@ -39,6 +39,7 @@ __RCSID("$NetBSD: mount_tmpfs.c,v 1.24 2
 #include <sys/param.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
+#include <sys/wait.h>
 
 #include <ctype.h>
 #include <err.h>
@@ -51,6 +52,9 @@ __RCSID("$NetBSD: mount_tmpfs.c,v 1.24 2
 #include <string.h>
 #include <unistd.h>
 #include <util.h>
+#include <paths.h>
+
+#include "pathnames.h"
 
 #include "mount_tmpfs.h"
 
@@ -74,10 +78,17 @@ static void pathadj(const char *, char *
 
 /* --------------------------------------------------------------------- */
 
+static int do_exec(const char *, const char *, char *const[]);
+static int isdir(const char *);
+static void tcopy(char *, char *, struct tmpfs_args *);
+static int gettmpmnt(char *, size_t);
+
+/* --------------------------------------------------------------------- */
+
 void
 mount_tmpfs_parseargs(int argc, char *argv[],
        struct tmpfs_args *args, int *mntflags,
-       char *canon_dev, char *canon_dir)
+       char *template, char *canon_dir)
 {
        int gidset, modeset, uidset; /* Ought to be 'bool'. */
        int ch;
@@ -99,7 +110,7 @@ mount_tmpfs_parseargs(int argc, char *ar
        modeset = 0; mode = 0;
 
        optind = optreset = 1;
-       while ((ch = getopt(argc, argv, "g:m:n:o:s:u:")) != -1 ) {
+       while ((ch = getopt(argc, argv, "g:m:n:o:s:u:P:")) != -1 ) {
                switch (ch) {
                case 'g':
                        gid = a_gid(optarg);
@@ -134,6 +145,12 @@ mount_tmpfs_parseargs(int argc, char *ar
                        uid = a_uid(optarg);
                        uidset = 1;
                        break;
+               
+               case 'P':
+                       if(strlcpy(template, optarg, MAXPATHLEN)
+                               >= MAXPATHLEN)
+                               errx(1, "template path %s too long", optarg);
+                       break;
 
                case '?':
                default:
@@ -146,7 +163,6 @@ mount_tmpfs_parseargs(int argc, char *ar
        if (argc != 2)
                usage();
 
-       strlcpy(canon_dev, argv[0], MAXPATHLEN);
        pathadj(argv[1], canon_dir);
 
        if (stat(canon_dir, &sb) == -1)
@@ -165,7 +181,7 @@ usage(void)
        extern char *__progname;
        (void)fprintf(stderr,
            "usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s size]\n"
-           "           [-u user] tmpfs mount_point\n", __progname);
+           "           [-u user] [-P template] tmpfs mount_point\n", 
__progname);
        exit(1);
 }
 
@@ -175,14 +191,34 @@ int
 mount_tmpfs(int argc, char *argv[])
 {
        struct tmpfs_args args;
-       char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
-       int mntflags;
+       char canon_dir[MAXPATHLEN] = {'\0'};
+       char template[MAXPATHLEN] = {'\0'};
+       int mntflags, rdonly;
 
        mount_tmpfs_parseargs(argc, argv, &args, &mntflags,
-           canon_dev, canon_dir);
+           template, canon_dir);
+
+       rdonly = mntflags & MNT_RDONLY;
+
+       if(template[0] != '\0')
+               mntflags &= ~MNT_RDONLY;
 
        if (mount(MOUNT_TMPFS, canon_dir, mntflags, &args) == -1)
                err(EXIT_FAILURE, "tmpfs on %s", canon_dir);
+               
+       if(template[0] != '\0') {
+               tcopy(template, canon_dir, &args);
+
+               if (rdonly) {
+                       mntflags |= MNT_RDONLY | MNT_UPDATE;
+                       if (mount(MOUNT_TMPFS, canon_dir, mntflags, &args) < 0) 
{
+                               warn("%s: mount (update, rdonly)", canon_dir);
+                               if (unmount(canon_dir, 0) != 0)
+                                       warn("unmount %s", canon_dir);
+                               exit(1);
+                       }
+               }
+       }
 
        return EXIT_SUCCESS;
 }
@@ -249,4 +285,139 @@ pathadj(const char *input, char *adjuste
                warnx("\"%s\" is a non-resolved or relative path.", input);
                warnx("using \"%s\" instead.", adjusted);
        }
+}
+
+/* Code copied from sbin/newfs/newfs.c to copy a template. */
+
+static int
+do_exec(const char *dir, const char *cmd, char *const argv[])
+{
+       pid_t pid;
+       int ret, status;
+       sig_t intsave, quitsave;
+
+       switch (pid = fork()) {
+       case -1:
+               err(1, "fork");
+       case 0:
+               if (dir != NULL && chdir(dir) != 0)
+                       err(1, "chdir");
+               if (execv(cmd, argv) != 0)
+                       err(1, "%s", cmd);
+               break;
+       default:
+               intsave = signal(SIGINT, SIG_IGN);
+               quitsave = signal(SIGQUIT, SIG_IGN);
+               for (;;) {
+                       ret = waitpid(pid, &status, 0);
+                       if (ret == -1)
+                               err(11, "waitpid");
+                       if (WIFEXITED(status)) {
+                               status = WEXITSTATUS(status);
+                               if (status != 0)
+                                       warnx("%s: exited", cmd);
+                               break;
+                       } else if (WIFSIGNALED(status)) {
+                               warnx("%s: %s", cmd,
+                                   strsignal(WTERMSIG(status)));
+                               status = 1;
+                               break;
+                       }
+               }
+               signal(SIGINT, intsave);
+               signal(SIGQUIT, quitsave);
+               return (status);
+       }
+       /* NOTREACHED */
+       return (-1);
+}
+
+static int
+isdir(const char *path)
+{
+       struct stat st;
+
+       if (stat(path, &st) != 0)
+               err(1, "cannot stat %s", path);
+       if (!S_ISDIR(st.st_mode) && !S_ISBLK(st.st_mode))
+               errx(1, "%s: not a dir or a block device", path);
+       return (S_ISDIR(st.st_mode));
+}
+
+static void
+tcopy(char *src, char *dst, struct tmpfs_args *args)
+{
+       int ret, dir, created = 0;
+       struct ufs_args mount_args;
+       char mountpoint[MNAMELEN];
+       char *const argv[] = { "pax", "-rw", "-pe", ".", dst, NULL } ;
+
+       dir = isdir(src);
+       if (dir) {
+               size_t sret;
+               sret = strlcpy(mountpoint, src, sizeof(mountpoint));
+               if(sret >= sizeof(mountpoint))
+                       errx(1, "template path %s too long", src);
+       }
+       else {
+               created = gettmpmnt(mountpoint, sizeof(mountpoint));
+               memset(&mount_args, 0, sizeof(mount_args));
+               mount_args.fspec = src;
+               ret = mount(MOUNT_FFS, mountpoint, MNT_RDONLY, &mount_args);
+               if (ret != 0) {
+                       if (created && rmdir(mountpoint) != 0)
+                               warn("rmdir %s", mountpoint);
+                       if (unmount(dst, 0) != 0)
+                               warn("unmount %s", dst);
+                       err(1, "mount %s %s", src, mountpoint);
+               }
+       }
+       ret = do_exec(mountpoint, "/bin/pax", argv);
+       if (!dir && unmount(mountpoint, 0) != 0)
+               warn("unmount %s", mountpoint);
+       if (created && rmdir(mountpoint) != 0)
+               warn("rmdir %s", mountpoint);
+       if (ret != 0) {
+               if (unmount(dst, 0) != 0)
+                       warn("unmount %s", dst);
+               errx(1, "copy %s to %s failed", mountpoint, dst);
+       }
+}
+
+static int
+gettmpmnt(char *mountpoint, size_t len)
+{
+       const char *tmp = NULL;
+       const char *mnt = _PATH_MNT;
+       struct statfs fs;
+       size_t n;
+
+       if (!issetugid())
+               tmp = getenv("TMPDIR");
+
+       if (tmp == NULL || *tmp == '\0')
+               tmp = _PATH_TMP;
+
+       if (statfs(tmp, &fs) != 0)
+               err(1, "statfs %s", tmp);
+       if (fs.f_flags & MNT_RDONLY) {
+               if (statfs(mnt, &fs) != 0)
+                       err(1, "statfs %s", mnt);
+               if (strcmp(fs.f_mntonname, "/") != 0)
+                       errx(1, "tmp mountpoint %s busy", mnt);
+               if (strlcpy(mountpoint, mnt, len) >= len)
+                       errx(1, "tmp mountpoint %s too long", mnt);
+               return (0);
+       }
+       n = strlcpy(mountpoint, tmp, len);
+       if (n >= len)
+               errx(1, "tmp mount point too long");
+       if (mountpoint[n - 1] != '/')
+               strlcat(mountpoint, "/", len);
+       n = strlcat(mountpoint, "mntXXXXXXXXXX", len);
+       if (n >= len)
+               errx(1, "tmp mount point too long");
+       if (mkdtemp(mountpoint) == NULL)
+               err(1, "mkdtemp %s", mountpoint);
+       return (1);
 }
Index: sbin/mount_tmpfs/pathnames.h
===================================================================
RCS file: sbin/mount_tmpfs/pathnames.h
diff -N sbin/mount_tmpfs/pathnames.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ sbin/mount_tmpfs/pathnames.h        19 Sep 2014 21:35:56 -0000
@@ -0,0 +1,35 @@
+/*     $OpenBSD: pathnames.h,v 1.2 2004/07/02 15:48:36 otto Exp $      */
+/*     $NetBSD: pathnames.h,v 1.1 1996/09/11 20:27:15 christos Exp $   */
+
+/*
+ * Copyright (c) 1996 Christos Zoulas.  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 Christos Zoulas.
+ * 4. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
+ */
+
+#define _PATH_SBIN     "/sbin"
+#define _PATH_USRSBIN  "/usr/sbin"
+#define _PATH_MNT      "/mnt"

Reply via email to