[PATCH 3/5] Dynamic Pseudo Root

Steve Dickson SteveD at redhat.com
Mon Feb 18 07:43:31 EST 2008


Author: Steve Dickson <steved at redhat.com>

    Supporting routines need for the dynamic 
    pseudo root support 
    
    Signed-off-by: Steve Dickson <steved at redhat.com>

diff -up /dev/null nfs-utils/support/nfs/execute.c
--- /dev/null	2008-02-13 08:13:58.514018448 -0500
+++ nfs-utils/support/nfs/execute.c	2008-02-16 11:53:48.000000000 -0500
@@ -0,0 +1,92 @@
+#include <sys/wait.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+
+#include "xlog.h"
+
+/*
+ * Log system errors or error messages.
+ */
+void 
+syserror(char *message)
+{
+	if (errno)
+		xlog(L_ERROR, "%s: errno %d (%s)", message, errno, strerror(errno));
+	else
+		xlog(L_ERROR, "Error: %s", message);
+}
+/*
+ * Log the the given argv[] if debugging is on.
+ */
+static void
+show_argv(char *msg, char *const argv[])
+{
+	char buf[BUFSIZ], *ptr;
+	int i, cc=0;
+
+	if (!xlog_enabled(D_CALL))
+		return;
+
+	ptr = buf;
+	if (msg) {
+		sprintf(ptr+cc, "%s ", msg);
+		cc = strlen(buf);
+	}
+	for (i=0; argv[i] != NULL && cc < BUFSIZ; i++) {
+		if ((cc + strlen(argv[i])) >= BUFSIZ)
+			break;
+		sprintf(ptr+cc, "%s ", argv[i]);
+		cc = strlen(buf);
+	}
+	xlog(D_CALL, "%s", buf);
+}
+/*
+ *  Execute the give command using the 
+ *  given argv as the arguments.
+ */
+int
+execute_cmd(const char *cmd,  char *const myargv[])
+{
+	int pfd[2], pid, status, cc;
+	char buf[BUFSIZ], *ch;
+
+	show_argv("executing:", myargv);
+
+	if (pipe(pfd) < 0) {
+		syserror("pipe() failed");
+		return errno;
+	}
+	switch((pid = fork())) {
+	case -1: 
+		syserror("fork() failed");
+		break;
+	case 0:  /* child */
+		close(pfd[0]);
+		if (dup2(pfd[1], fileno(stdout)) < 0)
+			perror("dup2(stdout)");
+		if (dup2(pfd[1], fileno(stderr)) < 0)
+			perror("dup2(stderr)");
+		close(pfd[1]);
+		execvp(cmd, myargv);
+		perror("execv");
+		_exit(255);
+		break;
+	default: /* parent */
+		status = 0;
+		close(pfd[1]);
+		waitpid(pid, &status, 0);
+		if (WIFEXITED(status) /* && (kiderr = WEXITSTATUS(status))*/ ) {
+			cc = read(pfd[0], buf, BUFSIZ);
+			if (cc > 0) { 
+				if ((ch = strrchr(buf, '\n')) != NULL)
+					*ch = '\0';
+				errno = 0;
+				syserror(buf);
+			}
+		}
+		break;
+	}
+	return errno;
+}
diff -up nfs-utils/support/nfs/Makefile.am.new nfs-utils/support/nfs/Makefile.am
--- nfs-utils/support/nfs/Makefile.am.new	2008-02-16 11:55:51.000000000 -0500
+++ nfs-utils/support/nfs/Makefile.am	2008-02-16 11:53:48.000000000 -0500
@@ -4,7 +4,7 @@ noinst_LIBRARIES = libnfs.a
 libnfs_a_SOURCES = exports.c rmtab.c xio.c rpcmisc.c rpcdispatch.c \
 		   xlog.c xcommon.c wildmat.c nfssvc.c nfsclient.c \
 		   nfsexport.c getfh.c nfsctl.c \
-		   svc_socket.c cacheio.c closeall.c nfs_mntent.c
+		   svc_socket.c cacheio.c closeall.c nfs_mntent.c execute.c
 
 MAINTAINERCLEANFILES = Makefile.in
 
diff -up nfs-utils/support/include/nfslib.h.new nfs-utils/support/include/nfslib.h
--- nfs-utils/support/include/nfslib.h.new	2008-02-16 11:55:51.000000000 -0500
+++ nfs-utils/support/include/nfslib.h	2008-02-16 11:53:48.000000000 -0500
@@ -155,4 +155,7 @@ void closeall(int min);
 int			svctcp_socket (u_long __number, int __reuse);
 int			svcudp_socket (u_long __number, int __reuse);
 
+extern int execute_cmd(const char *cmd,  char *const myargv[]);
+extern void syserror(char *message);
+
 #endif /* NFSLIB_H */
diff -up /dev/null nfs-utils/support/include/execute.h
--- /dev/null	2008-02-13 08:13:58.514018448 -0500
+++ nfs-utils/support/include/execute.h	2008-02-16 11:53:48.000000000 -0500
@@ -0,0 +1,38 @@
+/*
+ * support/include/execute.h
+ *
+ * Routines that will execute random shell commands.
+ *
+ */
+
+#ifndef EXECUTE_H
+#define EXECUTE_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifndef _RMDIR_CMD
+#define _RMDIR_CMD "/bin/rmdir"
+#endif
+
+#ifndef _MKDIR_CMD
+#define _MKDIR_CMD "/bin/mkdir"
+#endif
+
+inline int
+exec_rmdir(char *dir)
+{
+	char *const myargv[] = {_RMDIR_CMD, dir, NULL};
+
+	return execute_cmd(_RMDIR_CMD, myargv);
+}
+inline int
+exec_mkpath(char *dir)
+{
+	char *const myargv[] = {_MKDIR_CMD, "-p", dir, NULL};
+
+	return execute_cmd(_MKDIR_CMD, myargv);
+}
+
+#endif /* EXECUTE_H */
diff -up nfs-utils/support/include/Makefile.am.new nfs-utils/support/include/Makefile.am
--- nfs-utils/support/include/Makefile.am.new	2008-02-16 11:55:51.000000000 -0500
+++ nfs-utils/support/include/Makefile.am	2008-02-16 11:53:48.000000000 -0500
@@ -3,9 +3,11 @@
 SUBDIRS = nfs rpcsvc sys
 
 noinst_HEADERS = \
+	execute.h \
 	exportfs.h \
 	ha-callout.h \
 	misc.h \
+	mounts.h \
 	nfs_mntent.h \
 	nfs_paths.h \
 	nfslib.h \
diff -up /dev/null nfs-utils/support/include/mounts.h
--- /dev/null	2008-02-13 08:13:58.514018448 -0500
+++ nfs-utils/support/include/mounts.h	2008-02-16 11:56:21.000000000 -0500
@@ -0,0 +1,60 @@
+/*
+ * support/include/mounts.h
+ *
+ * Routines used to mount and umount different
+ * type of filesystems.
+ *
+ */
+
+#ifndef MOUNTS_H
+#define MOUNTS_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <sys/mount.h>
+
+struct mountargs {
+	const char *spec;
+	const char *node;
+	const char *type;
+	int flags;
+	void *data;
+};
+extern int v4root_mount(struct mountargs *args);
+extern int v4root_umount(struct mountargs *args);
+
+#ifndef MNT_DETACH
+#define MNT_DETACH  0x00000002  /* Just detach from the tree */
+#endif
+
+inline int
+bind_mount(char *dev, char *dir)
+{
+	struct mountargs args = {dev, dir, "bind", MS_BIND, NULL};
+
+	xlog(D_CALL, "mount --bind %s %s", dev, dir);
+
+	return v4root_mount(&args);
+}
+inline int
+tmpfs_mount(char *dir)
+{
+	struct mountargs args = {"tmpfs", dir, "tmpfs", 0, NULL};
+
+	xlog(D_CALL, "mount -t tmpfs tmpfs %s", dir);
+
+	return v4root_mount(&args);
+}
+inline int
+lazy_umount(char *dir)
+{
+	struct mountargs args = {NULL, dir, NULL, MNT_DETACH, NULL};
+
+	xlog(D_CALL, "umount -l %s", dir);
+
+	return v4root_umount(&args);
+}
+
+#endif /* MOUNTS_H */


More information about the NFSv4 mailing list