Revision 2:
1. Fixed up an issue caused by bad diffing technique.
2. Only functionality changes included here; parsing changes to follow next
as per Otto's instruction.
3. Documentation changes will follow as well.
4. I have received some more feedback from Doug Hogan regarding newfs bugs,
and some more changes are coming up.
5. Modified pathnames.h a little. What is the consensus on defining things
like _PATH_MNT here? I am finding it difficult to justify having an
entire header file just for one #define, but I am unsure what OpenBSD
considers acceptable. Input here appreciated.
Index: 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
--- mount_tmpfs.c 21 Jan 2014 21:58:27 -0000 1.4
+++ mount_tmpfs.c 19 Sep 2014 05:23:16 -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,6 +78,13 @@ 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,
@@ -176,13 +187,31 @@ mount_tmpfs(int argc, char *argv[])
{
struct tmpfs_args args;
char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
+ char template[MAXPATHLEN] = {'\0'};
int mntflags;
mount_tmpfs_parseargs(argc, argv, &args, &mntflags,
canon_dev, canon_dir);
+ int rdonly = mntflags & MNT_RDONLY;
+ if(template) 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 +278,133 @@ pathadj(const char *input, char *adjuste
warnx("\"%s\" is a non-resolved or relative path.", input);
warnx("using \"%s\" instead.", adjusted);
}
+}
+
+/* Code grafted 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)
+ strlcpy(mountpoint, src, sizeof(mountpoint));
+ 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;
+ const char *mnt = _PATH_MNT;
+ struct statfs fs;
+ size_t n;
+
+ 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: pathnames.h
===================================================================
RCS file: pathnames.h
diff -N pathnames.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ pathnames.h 18 Sep 2014 08:22:42 -0000
@@ -0,0 +1,33 @@
/* $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) 1989, 1993, 1994
+ * 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. 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.
+ */
+
+#define _PATH_MNT "/mnt"