[CRIU] Re: [PATCH] proc: Make sure eol remains on task name

Cyrill Gorcunov gorcunov at openvz.org
Fri May 4 09:06:27 EDT 2012


On Fri, May 04, 2012 at 04:30:08PM +0400, Cyrill Gorcunov wrote:
> On Fri, May 04, 2012 at 04:26:08PM +0400, Pavel Emelyanov wrote:
> > > Btw, what about this one?
> > 
> > The strlcpy exists in glibc since some version. What are we going to do about it?
> 
> Hmm, are you sure? If so we need own name for this helper I think.
> 

Maybe something like below?

	Cyrill
-------------- next part --------------
>From 47a7c64b2fd412db19ac1d000ae0c1773f1d4842 Mon Sep 17 00:00:00 2001
From: Cyrill Gorcunov <gorcunov at openvz.org>
Date: Fri, 4 May 2012 17:05:40 +0400
Subject: [PATCH] proc_parse: Use strlcpy to make sure the copied string has
 EOS

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 Makefile          |    9 +++++++++
 config/feats.mak  |    9 +++++++++
 config/utils.mak  |    7 +++++++
 include/strlcpy.h |   10 ++++++++++
 include/util.h    |    1 +
 proc_parse.c      |    4 ++--
 strlcpy.c         |   19 +++++++++++++++++++
 7 files changed, 57 insertions(+), 2 deletions(-)
 create mode 100644 config/feats.mak
 create mode 100644 config/utils.mak
 create mode 100644 include/strlcpy.h
 create mode 100644 strlcpy.c

diff --git a/Makefile b/Makefile
index 6f32df6..3a9b575 100644
--- a/Makefile
+++ b/Makefile
@@ -61,6 +61,15 @@ DEPS		:= $(patsubst %.o,%.d,$(OBJS))
 include Makefile.syscall
 include Makefile.pie
 
+include config/utils.mak
+include config/feats.mak
+
+ifeq ($(call try-cc,$(SOURCE_STRLCPY),),y)
+	DEFINES	+= -DHAVE_STRLCPY
+else
+	OBJS	+= strlcpy.o
+endif
+
 all: $(PROGRAM)
 
 %.o: %.c
diff --git a/config/feats.mak b/config/feats.mak
new file mode 100644
index 0000000..17de057
--- /dev/null
+++ b/config/feats.mak
@@ -0,0 +1,9 @@
+define SOURCE_STRLCPY
+#include <string.h>
+int main(void)
+{
+	char buf[4];
+	strlcpy(buf, "test", sizeof(buf));
+	return 0;
+}
+endef
diff --git a/config/utils.mak b/config/utils.mak
new file mode 100644
index 0000000..da7c434
--- /dev/null
+++ b/config/utils.mak
@@ -0,0 +1,7 @@
+# try-cc
+# Usage: option = $(call try-cc, source-to-build, cc-options)
+try-cc = $(shell sh -c						  \
+	'TMP="$(OUTPUT)$(TMPOUT).$$$$";				  \
+	 echo "$(1)" |						  \
+	 $(CC) -x c - $(2) -o "$$TMP" > /dev/null 2>&1 && echo y; \
+	 rm -f "$$TMP"')
diff --git a/include/strlcpy.h b/include/strlcpy.h
new file mode 100644
index 0000000..402962b
--- /dev/null
+++ b/include/strlcpy.h
@@ -0,0 +1,10 @@
+#ifndef STRLCPY_H__
+#define STRLCPY_H__
+
+#include <sys/types.h>
+
+#ifndef HAVE_STRLCPY
+size_t strlcpy(char *dest, const char *src, size_t size);
+#endif
+
+#endif /* STRLCPY_H__ */
diff --git a/include/util.h b/include/util.h
index 18933f3..efd646f 100644
--- a/include/util.h
+++ b/include/util.h
@@ -17,6 +17,7 @@
 #include "compiler.h"
 #include "types.h"
 #include "log.h"
+#include "strlcpy.h"
 
 #define PREF_SHIFT_OP(pref, op, size)	((size) op (pref ##BYTES_SHIFT))
 #define KBYTES_SHIFT	10
diff --git a/proc_parse.c b/proc_parse.c
index aceb4f5..61b6cf4 100644
--- a/proc_parse.c
+++ b/proc_parse.c
@@ -228,7 +228,7 @@ int parse_pid_stat_small(pid_t pid, struct proc_pid_stat_small *s)
 	*tok = '\0';
 	*p = '\0';
 
-	strncpy(s->comm, tok + 1, sizeof(s->comm));
+	strlcpy(s->comm, tok + 1, sizeof(s->comm));
 
 	n = sscanf(p + 1, " %c %d %d %d", &s->state, &s->ppid, &s->pgid, &s->sid);
 	if (n < 4)
@@ -276,7 +276,7 @@ int parse_pid_stat(pid_t pid, struct proc_pid_stat *s)
 	*tok = '\0';
 	*p = '\0';
 
-	strncpy(s->comm, tok + 1, sizeof(s->comm));
+	strlcpy(s->comm, tok + 1, sizeof(s->comm));
 
 	n = sscanf(p + 1,
 	       " %c %d %d %d %d %d %u %lu %lu %lu %lu "
diff --git a/strlcpy.c b/strlcpy.c
new file mode 100644
index 0000000..68b5951
--- /dev/null
+++ b/strlcpy.c
@@ -0,0 +1,19 @@
+#include <sys/types.h>
+#include <string.h>
+
+#include "strlcpy.h"
+
+#ifndef HAVE_STRLCPY
+size_t strlcpy(char *dest, const char *src, size_t size)
+{
+	size_t ret, len;
+	if (size) {
+		ret = strlen(src);
+		len = (ret >= size) ? size - 1 : ret;
+		memcpy(dest, src, len);
+	} else
+		len = 0;
+	dest[len] = '\0';
+	return len;
+}
+#endif
-- 
1.7.7.6



More information about the CRIU mailing list