[CRIU] [PATCH 1/2] zdtm: move get_smaps_bits to separate file for reuse

Pavel Tikhomirov ptikhomirov at parallels.com
Fri Sep 12 03:39:25 PDT 2014


Signed-off-by: Pavel Tikhomirov <ptikhomirov at parallels.com>
---
 test/zdtm/lib/zdtmtst.h                |   1 +
 test/zdtm/live/static/Makefile         |   4 +-
 test/zdtm/live/static/get_smaps_bits.c | 127 +++++++++++++++++++++++++++++++
 test/zdtm/live/static/maps02.c         | 134 ---------------------------------
 4 files changed, 131 insertions(+), 135 deletions(-)
 create mode 100644 test/zdtm/live/static/get_smaps_bits.c

diff --git a/test/zdtm/lib/zdtmtst.h b/test/zdtm/lib/zdtmtst.h
index 18eb082..cea7d35 100644
--- a/test/zdtm/lib/zdtmtst.h
+++ b/test/zdtm/lib/zdtmtst.h
@@ -123,4 +123,5 @@ extern int tcp_init_server(int family, int *port);
 extern int tcp_accept_server(int sock);
 extern int tcp_init_client(int family, char *servIP, unsigned short servPort);
 
+extern int get_smaps_bits(unsigned long where, unsigned long *flags, unsigned long *madv);
 #endif /* _VIMITESU_H_ */
diff --git a/test/zdtm/live/static/Makefile b/test/zdtm/live/static/Makefile
index 07d1114..4cea9e4 100644
--- a/test/zdtm/live/static/Makefile
+++ b/test/zdtm/live/static/Makefile
@@ -70,6 +70,7 @@ TST_NOFILE	=				\
 		maps03				\
 		maps04				\
 		maps05				\
+		mlock_setuid			\
 		xids00				\
 		groups				\
 		pdeath_sig			\
@@ -283,12 +284,13 @@ socket_listen6: override CFLAGS += -D ZDTM_IPV6
 sigpending:		override LDLIBS += -lrt
 vdso01:			override LDLIBS += -lrt
 mntns_link_remap:	override CFLAGS += -DZDTM_LINK_REMAP
+maps02:		get_smaps_bits.o
 
 $(LIB):	force
 	$(Q) $(MAKE) -C $(LIBDIR)
 
 clean:
-	$(RM) -f $(OBJ) $(TST) *~ criu-rtc.so criu-rtc.pb-c.c criu-rtc.pb-c.h
+	$(RM) -f $(OBJ) $(TST) *~ criu-rtc.so criu-rtc.pb-c.c criu-rtc.pb-c.h get_smaps_bits.o
 
 cleandep:	clean
 	$(RM) -f $(DEP)
diff --git a/test/zdtm/live/static/get_smaps_bits.c b/test/zdtm/live/static/get_smaps_bits.c
new file mode 100644
index 0000000..a478d02
--- /dev/null
+++ b/test/zdtm/live/static/get_smaps_bits.c
@@ -0,0 +1,127 @@
+#include <string.h>
+#include <sys/mman.h>
+#include "zdtmtst.h"
+
+#ifndef MAP_HUGETLB
+# define MAP_HUGETLB 0x40000
+#endif
+
+#ifndef MADV_HUGEPAGE
+# define MADV_HUGEPAGE 14
+#endif
+
+#ifndef MADV_NOHUGEPAGE
+# define MADV_NOHUGEPAGE 15
+#endif
+
+#ifndef MADV_DONTDUMP
+# define MADV_DONTDUMP 16
+#endif
+
+static void parse_vmflags(char *buf, unsigned long *flags, unsigned long *madv)
+{
+	char *tok;
+
+	if (!buf[0])
+		return;
+
+	tok = strtok(buf, " \n");
+	if (!tok)
+		return;
+
+#define _vmflag_match(_t, _s) (_t[0] == _s[0] && _t[1] == _s[1])
+
+	do {
+		/* mmap() block */
+		if (_vmflag_match(tok, "gd"))
+			*flags |= MAP_GROWSDOWN;
+		else if (_vmflag_match(tok, "lo"))
+			*flags |= MAP_LOCKED;
+		else if (_vmflag_match(tok, "nr"))
+			*flags |= MAP_NORESERVE;
+		else if (_vmflag_match(tok, "ht"))
+			*flags |= MAP_HUGETLB;
+
+		/* madvise() block */
+		if (_vmflag_match(tok, "sr"))
+			*madv |= (1ul << MADV_SEQUENTIAL);
+		else if (_vmflag_match(tok, "rr"))
+			*madv |= (1ul << MADV_RANDOM);
+		else if (_vmflag_match(tok, "dc"))
+			*madv |= (1ul << MADV_DONTFORK);
+		else if (_vmflag_match(tok, "dd"))
+			*madv |= (1ul << MADV_DONTDUMP);
+		else if (_vmflag_match(tok, "mg"))
+			*madv |= (1ul << MADV_MERGEABLE);
+		else if (_vmflag_match(tok, "hg"))
+			*madv |= (1ul << MADV_HUGEPAGE);
+		else if (_vmflag_match(tok, "nh"))
+			*madv |= (1ul << MADV_NOHUGEPAGE);
+
+		/*
+		 * Anything else is just ignored.
+		 */
+	} while ((tok = strtok(NULL, " \n")));
+
+#undef _vmflag_match
+}
+
+#define is_hex_digit(c)				\
+	(((c) >= '0' && (c) <= '9')	||	\
+	 ((c) >= 'a' && (c) <= 'f')	||	\
+	 ((c) >= 'A' && (c) <= 'F'))
+
+static int is_vma_range_fmt(char *line, unsigned long *start, unsigned long *end)
+{
+	char *p = line;
+	while (*line && is_hex_digit(*line))
+		line++;
+
+	if (*line++ != '-')
+		return 0;
+
+	while (*line && is_hex_digit(*line))
+		line++;
+
+	if (*line++ != ' ')
+		return 0;
+
+	sscanf(p, "%lx-%lx", start, end);
+	return 1;
+}
+
+int get_smaps_bits(unsigned long where, unsigned long *flags, unsigned long *madv)
+{
+	unsigned long start = 0, end = 0;
+	FILE *smaps = NULL;
+	char buf[1024];
+	int found = 0;
+
+	if (!where)
+		return 0;
+
+	smaps = fopen("/proc/self/smaps", "r");
+	if (!smaps) {
+		err("Can't open smaps: %m");
+		return -1;
+	}
+
+	while (fgets(buf, sizeof(buf), smaps)) {
+		is_vma_range_fmt(buf, &start, &end);
+
+		if (!strncmp(buf, "VmFlags: ", 9) && start == where) {
+			found = 1;
+			parse_vmflags(buf, flags, madv);
+			break;
+		}
+	}
+
+	fclose(smaps);
+
+	if (!found) {
+		err("VmFlags not found for %lx\n", where);
+		return -1;
+	}
+
+	return 0;
+}
diff --git a/test/zdtm/live/static/maps02.c b/test/zdtm/live/static/maps02.c
index b0fc0f4..336044f 100644
--- a/test/zdtm/live/static/maps02.c
+++ b/test/zdtm/live/static/maps02.c
@@ -1,32 +1,6 @@
-#include <errno.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <signal.h>
-#include <string.h>
 #include <sys/mman.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-#include <linux/limits.h>
 #include "zdtmtst.h"
 
-#ifndef MAP_HUGETLB
-# define MAP_HUGETLB 0x40000
-#endif
-
-#ifndef MADV_HUGEPAGE
-# define MADV_HUGEPAGE 14
-#endif
-
-#ifndef MADV_NOHUGEPAGE
-# define MADV_NOHUGEPAGE 15
-#endif
-
-#ifndef MADV_DONTDUMP
-# define MADV_DONTDUMP 16
-#endif
-
 const char *test_doc	= "Test shared memory with advises";
 const char *test_author	= "Cyrill Gorcunov <gorcunov at openvz.org>";
 
@@ -38,114 +12,6 @@ struct mmap_data {
 	unsigned long	new_madv;
 };
 
-static void parse_vmflags(char *buf, unsigned long *flags, unsigned long *madv)
-{
-	char *tok;
-
-	if (!buf[0])
-		return;
-
-	tok = strtok(buf, " \n");
-	if (!tok)
-		return;
-
-#define _vmflag_match(_t, _s) (_t[0] == _s[0] && _t[1] == _s[1])
-
-	do {
-		/* mmap() block */
-		if (_vmflag_match(tok, "gd"))
-			*flags |= MAP_GROWSDOWN;
-		else if (_vmflag_match(tok, "lo"))
-			*flags |= MAP_LOCKED;
-		else if (_vmflag_match(tok, "nr"))
-			*flags |= MAP_NORESERVE;
-		else if (_vmflag_match(tok, "ht"))
-			*flags |= MAP_HUGETLB;
-
-		/* madvise() block */
-		if (_vmflag_match(tok, "sr"))
-			*madv |= (1ul << MADV_SEQUENTIAL);
-		else if (_vmflag_match(tok, "rr"))
-			*madv |= (1ul << MADV_RANDOM);
-		else if (_vmflag_match(tok, "dc"))
-			*madv |= (1ul << MADV_DONTFORK);
-		else if (_vmflag_match(tok, "dd"))
-			*madv |= (1ul << MADV_DONTDUMP);
-		else if (_vmflag_match(tok, "mg"))
-			*madv |= (1ul << MADV_MERGEABLE);
-		else if (_vmflag_match(tok, "hg"))
-			*madv |= (1ul << MADV_HUGEPAGE);
-		else if (_vmflag_match(tok, "nh"))
-			*madv |= (1ul << MADV_NOHUGEPAGE);
-
-		/*
-		 * Anything else is just ignored.
-		 */
-	} while ((tok = strtok(NULL, " \n")));
-
-#undef _vmflag_match
-}
-
-#define is_hex_digit(c)				\
-	(((c) >= '0' && (c) <= '9')	||	\
-	 ((c) >= 'a' && (c) <= 'f')	||	\
-	 ((c) >= 'A' && (c) <= 'F'))
-
-static int is_vma_range_fmt(char *line, unsigned long *start, unsigned long *end)
-{
-	char *p = line;
-	while (*line && is_hex_digit(*line))
-		line++;
-
-	if (*line++ != '-')
-		return 0;
-
-	while (*line && is_hex_digit(*line))
-		line++;
-
-	if (*line++ != ' ')
-		return 0;
-
-	sscanf(p, "%lx-%lx", start, end);
-	return 1;
-}
-
-static int get_smaps_bits(unsigned long where, unsigned long *flags, unsigned long *madv)
-{
-	unsigned long start = 0, end = 0;
-	FILE *smaps = NULL;
-	char buf[1024];
-	int found = 0;
-
-	if (!where)
-		return 0;
-
-	smaps = fopen("/proc/self/smaps", "r");
-	if (!smaps) {
-		err("Can't open smaps: %m");
-		return -1;
-	}
-
-	while (fgets(buf, sizeof(buf), smaps)) {
-		is_vma_range_fmt(buf, &start, &end);
-
-		if (!strncmp(buf, "VmFlags: ", 9) && start == where) {
-			found = 1;
-			parse_vmflags(buf, flags, madv);
-			break;
-		}
-	}
-
-	fclose(smaps);
-
-	if (!found) {
-		err("VmFlags not found for %lx\n", where);
-		return -1;
-	}
-
-	return 0;
-}
-
 #define MEM_SIZE (8192)
 
 static int alloc_anon_mmap(struct mmap_data *m, int flags, int adv)
-- 
1.9.3



More information about the CRIU mailing list