Skip to contentSkip to conversation

user-cr: UTS support and test

PATCH In review Dan Smith 2 participants 5 messages Mar 16, 2009 3 messages not in the archive

lore
Share & tools
lore

Dan Smith

author
#0 lore raw

This patch set against Oren's user-cr tree makes the necessary changes to mktree.c to support restarting a checkpoint that contains UTS namespace information. It also includes a test program to be used for verification.

A message that is not in the archive (sent off-list or not yet received): 1237227184-2757-1-git-send-email-danms-r/[email protected]

author
#2 lore raw

PATCH 1/1 Add UTS support to mktree

Read the namespace records from the restore stream and do the unshare() necessary to create a new namespace. For UTS, set hostname and domainname to match what was stored in the checkpoint.

---
 mktree.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 66 insertions(+), 0 deletions(-)
mktree.c +66 -0
index 55149dd..e0898db 100644
@@ -14,6 +14,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <signal.h>
+#include <sched.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <asm/unistd.h>
@@ -101,6 +102,7 @@ static int cr_read_obj_type(struct cr_ctx *ctx, void *buf, int n, int type);
 
 static int cr_read_head(struct cr_ctx *ctx);
 static int cr_read_head_arch(struct cr_ctx *ctx);
+static int cr_read_namespaces(struct cr_ctx *ctx);
 static int cr_read_tree(struct cr_ctx *ctx);
 
 struct pid_swap {
@@ -191,6 +193,12 @@ int main(int argc, char *argv[])
 		exit(1);
 	}
 
+	ret = cr_read_namespaces(&ctx);
+	if (ret < 0) {
+		perror("read c/r namespaces");
+		exit(1);
+	}
+
 	ret = cr_fork_feeder(&ctx);
 	if (ret < 0)
 		exit(1);
@@ -557,6 +565,64 @@ static int cr_read_head_arch(struct cr_ctx *ctx)
 	return 0;
 }
 
+static int cr_read_task_namespaces(struct cr_ctx *ctx)
+{
+
+	struct cr_hdr_nsproxy hhn;
+	struct cr_hdr_utsns hhu;
+	int parent;
+
+	parent = cr_read_obj_type(ctx, &hhn, sizeof(hhn), CR_HDR_NSP);
+	if (parent < 0)
+		return parent;
+	else if (parent != 0) {
+		errno = -EINVAL;
+		return -1;
+	}
+
+	if (hhn.types == 0)
+		return 0;
+
+	if (unshare(CLONE_NEWUTS) != 0) {
+		perror("unshare(CLONE_NEWUTS)");
+		return -1;
+	}
+
+	if (hhn.types & CR_NSP_UTS) {
+		parent = cr_read_obj_type(ctx, &hhu, sizeof(hhu),
+					  CR_HDR_UTSNS);
+		if (parent < 0)
+			return parent;
+		else if (parent != 0) {
+			errno = -EINVAL;
+			return -1;
+		}
+
+		cr_dbg("UTS namespace nn=%s dn=%s\n",
+		       hhu.nodename,
+		       hhu.domainname);
+
+		sethostname(hhu.nodename, strlen(hhu.nodename));
+		setdomainname(hhu.domainname, strlen(hhu.domainname));
+	}
+
+	return 0;
+}
+
+static int cr_read_namespaces(struct cr_ctx *ctx)
+{
+	int n;
+	int ret = 0;
+
+	for (n = 0; n < ctx->pids_nr; n++) {
+		ret = cr_read_task_namespaces(ctx);
+		if (ret < 0)
+			break;
+	}
+
+	return ret;
+}
+
 static int cr_read_tree(struct cr_ctx *ctx)
 {
 	struct cr_hdr *h = (struct cr_hdr *) ctx->buf;
Signature · 1 line
1.5.6.3

A message that is not in the archive (sent off-list or not yet received): 1237227184-2757-2-git-send-email-danms-r/[email protected]

#4 lore raw

Re: [PATCH] Add UTS support to mktree

Quoted 64 lines · Dan Smith

Read the namespace records from the restore stream and do the unshare() necessary to create a new namespace. For UTS, set hostname and domainname to match what was stored in the checkpoint.

---
 mktree.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 66 insertions(+), 0 deletions(-)
mktree.c +26 -0
index 55149dd..e0898db 100644
@@ -14,6 +14,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <signal.h>
+#include <sched.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <asm/unistd.h>
@@ -101,6 +102,7 @@ static int cr_read_obj_type(struct cr_ctx *ctx, void *buf, int n, int type);
 
 static int cr_read_head(struct cr_ctx *ctx);
 static int cr_read_head_arch(struct cr_ctx *ctx);
+static int cr_read_namespaces(struct cr_ctx *ctx);
 static int cr_read_tree(struct cr_ctx *ctx);
 
 struct pid_swap {
@@ -191,6 +193,12 @@ int main(int argc, char *argv[])
 		exit(1);
 	}
 
+	ret = cr_read_namespaces(&ctx);
+	if (ret < 0) {
+		perror("read c/r namespaces");
+		exit(1);
+	}
+
 	ret = cr_fork_feeder(&ctx);
 	if (ret < 0)
 		exit(1);
@@ -557,6 +565,64 @@ static int cr_read_head_arch(struct cr_ctx *ctx)
 	return 0;
 }
 
+static int cr_read_task_namespaces(struct cr_ctx *ctx)
+{
+
+	struct cr_hdr_nsproxy hhn;
+	struct cr_hdr_utsns hhu;
+	int parent;
+
+	parent = cr_read_obj_type(ctx, &hhn, sizeof(hhn), CR_HDR_NSP);
+	if (parent < 0)
+		return parent;
+	else if (parent != 0) {
+		errno = -EINVAL;
+		return -1;
+	}
+
+	if (hhn.types == 0)
+		return 0;
+

mktree.c static int cr_read_head_arch(struct cr_ctx *ctx) · line 595

Quoted 10 lines
mktree.c +10 -0
+	if (unshare(CLONE_NEWUTS) != 0) {
+		perror("unshare(CLONE_NEWUTS)");
+		return -1;
+	}
+
+	if (hhn.types & CR_NSP_UTS) {
+		parent = cr_read_obj_type(ctx, &hhu, sizeof(hhu),
+					  CR_HDR_UTSNS);
+		if (parent < 0)
+			return parent;

Man this reinforces the point someone (Oren?) was making about not being able to error out gracefully from a restart. If we fail right here, we're suddenly in our own uts namespace. Which coudl be fine for awhile, but could really mess with an admin's mind.

Sure we could move the unshare() to after we've read all of the namespace data, but then the same thing could happen if the first step in fork_feeder fails.

mktree.c static int cr_read_head_arch(struct cr_ctx *ctx) · line 606

Quoted 11 lines
mktree.c +11 -0
+		else if (parent != 0) {
+			errno = -EINVAL;
+			return -1;
+		}
+
+		cr_dbg("UTS namespace nn=%s dn=%s\n",
+		       hhu.nodename,
+		       hhu.domainname);
+
+		sethostname(hhu.nodename, strlen(hhu.nodename));
+		setdomainname(hhu.domainname, strlen(hhu.domainname));

*these *shoudln't* fail (if unshare succeeded), but it's still worth checking.

Quoted 29 lines
mktree.c +19 -0
+	}
+
+	return 0;
+}
+
+static int cr_read_namespaces(struct cr_ctx *ctx)
+{
+	int n;
+	int ret = 0;
+
+	for (n = 0; n < ctx->pids_nr; n++) {
+		ret = cr_read_task_namespaces(ctx);
+		if (ret < 0)
+			break;
+	}
+
+	return ret;
+}
+
 static int cr_read_tree(struct cr_ctx *ctx)
 {
 	struct cr_hdr *h = (struct cr_hdr *) ctx->buf;
Signature · 6 lines
1.5.6.3

_______________________________________________
Containers mailing list
Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linux-foundation.org/mailman/listinfo/containers

A message that is not in the archive (sent off-list or not yet received): 20090316185805.GA7329-r/[email protected]

author
#6 lore raw

Re: [PATCH] Add UTS support to mktree

SH> *these *shoudln't* fail (if unshare succeeded), but it's still SH> worth checking.

Oops, yep :)

Signature · 3 lines
Dan Smith
IBM Linux Technology Center
email: danms-r/[email protected]
author
#7 lore raw

PATCH 1/1 Add a simple UTS test program

In reply to a message not in the archive

This program creates a top-level UTS namespace and then forks a child. The parent sets the hostname to a different value every second and the child prints the value. If the child's value changes with the parent, even after restart, then the pair are running in the same UTS namespace. The test self-checkpoints every iteration.

---
 utstest.c |  114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 114 insertions(+), 0 deletions(-)
 create mode 100644 utstest.c
utstest.c new file +114 -0
new file mode 100644
index 0000000..1afca49
@@ -0,0 +1,114 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sched.h>
+#include <string.h>
+#include <stdlib.h>
+#include <asm/unistd.h>
+
+#define OUTFILE "/tmp/cr-test.out"
+#define CKPTDIR "/tmp"
+
+int ckpt = 0;
+
+int do_checkpoint(void)
+{
+	char fn[256];
+	int fd;
+	int ret;
+
+	snprintf(fn, sizeof(fn)-1, CKPTDIR "/ckpt-%i", ckpt++);
+
+	fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+	if (fd < 0) {
+		perror(fn);
+		return -1;
+	}
+
+	ret = syscall(__NR_checkpoint, getpid(), fd, 0);
+	if (ret < 0)
+		printf("Checkpoint to %s returned %i (%m)\n", fn, ret);
+
+	close(fd);
+
+	return ret;
+}
+
+void child(int fd, int subunshare)
+{
+	char hostname[256] = "foo";
+	int ret;
+
+	if (subunshare) {
+		ret = unshare(CLONE_NEWUTS);
+		if (ret) {
+			printf("unshare: %m");
+			exit(1);
+		}
+	}
+
+	while (1) {
+		ret = gethostname(hostname, sizeof(hostname));
+		if (ret) {
+			perror("gethostname");
+			_exit(1);
+		}
+
+		printf("Hostname in child is:  %s\n", hostname);
+		sleep(1);
+	}
+}
+
+int main(int argc, char **argv)
+{
+	int ret;
+	int fd;
+	int i = 0;
+	int subunshare = 0;
+
+#ifndef NOUTS
+	ret = unshare(CLONE_NEWUTS);
+	if (ret) {
+		perror("unshare");
+		return 1;
+	}
+#endif
+
+	/* Pass '-u' to test nested namespace */
+	if ((argc == 2) && (strcmp(argv[1], "-u") == 0))
+		subunshare = 1;
+
+	fd = open(OUTFILE, O_RDWR | O_CREAT | O_TRUNC, 0666);
+	if (fd < 0) {
+		perror(OUTFILE);
+		return 1;
+	}
+
+	dup2(fd, 1);
+	dup2(fd, 2);
+	close(0);
+
+	setlinebuf(stdout);
+	setlinebuf(stderr);
+
+	if (fork() == 0)
+		child(fd, subunshare);
+
+	while (1) {
+#ifndef NOUTS
+		char hostname[256];
+
+		snprintf(hostname, sizeof(hostname)-1, "test%i", i++);
+		ret = sethostname(hostname, strlen(hostname));
+		if (ret) {
+			perror("sethostname");
+			return 1;
+		}
+
+		printf("Hostname in parent is: %s\n", hostname);
+#endif
+		do_checkpoint();
+		sleep(1);
+	}
+}
Signature · 1 line
1.5.6.3