[CRIU] [PATCH] criu: use strtok in parse_ns_string

Sophie Blee-Goldman ableegoldman at google.com
Mon Aug 4 13:10:12 PDT 2014


Use strtok to parse the -n option in crtools.c. Will be needed for implementing
user namespace support.

Change-Id: Ifb7dc11c94c216a52cf9da7aab4fdf0af4f74152
---
 crtools.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)

diff --git a/crtools.c b/crtools.c
index b7ca833..49b488a 100644
--- a/crtools.c
+++ b/crtools.c
@@ -53,30 +53,34 @@ void init_opts(void)
 
 static int parse_ns_string(const char *ptr)
 {
-	const char *end = ptr + strlen(ptr);
+	int ret = 0;
+	char *ns;
 
-	do {
-		if (ptr[3] != ',' && ptr[3] != '\0')
-			goto bad_ns;
-		if (!strncmp(ptr, "uts", 3))
+	char *copy = strdup(ptr);
+	if (!copy) {
+		pr_msg("Error: malloc failed");
+		return -1;
+	}
+
+	for (ns = strtok(copy, ","); ns ; ns = strtok(NULL, ",")) {
+		if (!strcmp(ns, "uts")) {
 			opts.rst_namespaces_flags |= CLONE_NEWUTS;
-		else if (!strncmp(ptr, "ipc", 3))
+		} else if (!strcmp(ns, "ipc")) {
 			opts.rst_namespaces_flags |= CLONE_NEWIPC;
-		else if (!strncmp(ptr, "mnt", 3))
+		} else if (!strcmp(ns, "mnt")) {
 			opts.rst_namespaces_flags |= CLONE_NEWNS;
-		else if (!strncmp(ptr, "pid", 3))
+		} else if (!strcmp(ns, "pid")) {
 			opts.rst_namespaces_flags |= CLONE_NEWPID;
-		else if (!strncmp(ptr, "net", 3))
+		} else if (!strcmp(ns, "net")) {
 			opts.rst_namespaces_flags |= CLONE_NEWNET;
-		else
-			goto bad_ns;
-		ptr += 4;
-	} while (ptr < end);
-	return 0;
-
-bad_ns:
-	pr_msg("Error: unknown namespace: %s\n", ptr);
-	return -1;
+		} else {
+			pr_msg("Error: unknown namespace: %s\n", ns);
+			ret = -1;
+			break;
+		}
+	}
+	free(copy);
+	return ret;
 }
 
 char criu_dir_name[PATH_MAX];
-- 
2.0.0.526.g5318336



More information about the CRIU mailing list