[3/6] criu: Add lib-bsd

Submitted by Dmitry Safonov on Nov. 23, 2019, 11:46 p.m.

Details

Message ID 20191123234627.483372-4-dima@arista.com
State New
Series "criu: dlopen() optional libraries"
Headers show

Commit Message

Dmitry Safonov Nov. 23, 2019, 11:46 p.m.
Provides strlcpy(), strlcat() and optionally setproctitle_init(),
setproctitle().

Get them from loaded in runtime libbsd.so or use home-made criu
versions.

Signed-off-by: Dmitry Safonov <dima@arista.com>
---
 criu/Makefile.crtools        |  2 +-
 criu/cgroup-props.c          |  1 -
 criu/cgroup.c                |  1 -
 criu/cr-dump.c               |  2 +-
 criu/cr-libs.c               |  1 +
 criu/cr-service.c            |  2 +-
 criu/crtools.c               |  2 +-
 criu/include/cr-libs.h       |  3 +-
 criu/include/lib-bsd.h       | 11 +++++++
 criu/include/setproctitle.h  | 19 -----------
 criu/include/string.h        | 20 ------------
 criu/{string.c => lib-bsd.c} | 63 ++++++++++++++++++++++++++++--------
 criu/log.c                   |  2 +-
 criu/net.c                   |  2 +-
 criu/proc_parse.c            |  2 +-
 criu/tun.c                   |  2 +-
 16 files changed, 72 insertions(+), 63 deletions(-)
 create mode 100644 criu/include/lib-bsd.h
 delete mode 100644 criu/include/setproctitle.h
 delete mode 100644 criu/include/string.h
 rename criu/{string.c => lib-bsd.c} (52%)

Patch hide | download patch | download mbox

diff --git a/criu/Makefile.crtools b/criu/Makefile.crtools
index 1833ba77526d..55996c09775b 100644
--- a/criu/Makefile.crtools
+++ b/criu/Makefile.crtools
@@ -38,6 +38,7 @@  obj-y			+= irmap.o
 obj-y			+= kcmp-ids.o
 obj-y			+= kerndat.o
 obj-y			+= libnetlink.o
+obj-y			+= lib-bsd.o
 obj-y			+= log.o
 obj-y			+= lsm.o
 obj-y			+= mem.o
@@ -73,7 +74,6 @@  obj-y			+= sk-tcp.o
 obj-y			+= sk-unix.o
 obj-y			+= sockets.o
 obj-y			+= stats.o
-obj-y			+= string.o
 obj-y			+= sysctl.o
 obj-y			+= sysfs_parse.o
 obj-y			+= timerfd.o
diff --git a/criu/cgroup-props.c b/criu/cgroup-props.c
index ecd95935207b..7fe7196687c2 100644
--- a/criu/cgroup-props.c
+++ b/criu/cgroup-props.c
@@ -14,7 +14,6 @@ 
 #include "cgroup-props.h"
 #include "cr_options.h"
 #include "xmalloc.h"
-#include "string.h"
 #include "util.h"
 #include "common/list.h"
 #include "log.h"
diff --git a/criu/cgroup.c b/criu/cgroup.c
index a66fc960e6f5..4b7281eb7d2a 100644
--- a/criu/cgroup.c
+++ b/criu/cgroup.c
@@ -20,7 +20,6 @@ 
 #include "util-pie.h"
 #include "namespaces.h"
 #include "seize.h"
-#include "string.h"
 #include "protobuf.h"
 #include "images/core.pb-c.h"
 #include "images/cgroup.pb-c.h"
diff --git a/criu/cr-dump.c b/criu/cr-dump.c
index 6bdd28400d6b..b12d52f664da 100644
--- a/criu/cr-dump.c
+++ b/criu/cr-dump.c
@@ -38,7 +38,7 @@ 
 #include "crtools.h"
 #include "cr_options.h"
 #include "servicefd.h"
-#include "string.h"
+#include "lib-bsd.h"
 #include "ptrace-compat.h"
 #include "util.h"
 #include "namespaces.h"
diff --git a/criu/cr-libs.c b/criu/cr-libs.c
index b7da1f847afa..356d807dbb18 100644
--- a/criu/cr-libs.c
+++ b/criu/cr-libs.c
@@ -26,6 +26,7 @@  struct so_desc {
 	unsigned	loaded_major;
 };
 static struct so_desc crlibs[SHARED_LIB_LAST] = {
+{ "libbsd.so", "Can't set title and using self-made strlcpy()", 0 },
 };
 
 static void *try_load_name_version(const char *so_name, const unsigned major)
diff --git a/criu/cr-service.c b/criu/cr-service.c
index 549b3368b3c8..66ec93855d55 100644
--- a/criu/cr-service.c
+++ b/criu/cr-service.c
@@ -41,7 +41,7 @@ 
 #include "common/scm.h"
 #include "uffd.h"
 
-#include "setproctitle.h"
+#include "lib-bsd.h"
 
 #include "cr-errno.h"
 #include "namespaces.h"
diff --git a/criu/crtools.c b/criu/crtools.c
index 25cce4265cce..aad3da0887f5 100644
--- a/criu/crtools.c
+++ b/criu/crtools.c
@@ -45,7 +45,7 @@ 
 #include "proc_parse.h"
 #include "kerndat.h"
 
-#include "setproctitle.h"
+#include "lib-bsd.h"
 #include "sysctl.h"
 #include "img-remote.h"
 
diff --git a/criu/include/cr-libs.h b/criu/include/cr-libs.h
index ef6ceca1f64d..de42e4359cd3 100644
--- a/criu/include/cr-libs.h
+++ b/criu/include/cr-libs.h
@@ -6,7 +6,8 @@ 
 #endif
 
 enum shared_libs {
-	SHARED_LIB_LAST = 0,
+	LIB_BSD = 0,
+	SHARED_LIB_LAST,
 };
 
 /* Called on init to dlopen() all .so libraries */
diff --git a/criu/include/lib-bsd.h b/criu/include/lib-bsd.h
new file mode 100644
index 000000000000..19e593ac37e0
--- /dev/null
+++ b/criu/include/lib-bsd.h
@@ -0,0 +1,11 @@ 
+#ifndef __CR_LIB_BSD_H__
+#define __CR_LIB_BSD_H__
+
+#include <stddef.h> /* size_t */
+
+extern size_t strlcpy(char *dest, const char *src, size_t size);
+extern size_t strlcat(char *dest, const char *src, size_t count);
+extern void setproctitle_init(int argc, char *argv[], char *envp[]);
+extern void setproctitle(const char *fmt, ...);
+
+#endif /* __CR_LIB_BSD_H__ */
diff --git a/criu/include/setproctitle.h b/criu/include/setproctitle.h
deleted file mode 100644
index bc634331bde4..000000000000
--- a/criu/include/setproctitle.h
+++ /dev/null
@@ -1,19 +0,0 @@ 
-#ifndef __CR_SETPROCTITLE_H__
-#define __CR_SETPROCTITLE_H__
-
-#ifdef CONFIG_HAS_LIBBSD
-#include <bsd/unistd.h>
-#else
-
-/*
- * setproctitle_init is in the libbsd since v0.6.0. This macro allows to
- * compile criu with libbsd<0.6.0.
- */
-#ifndef CONFIG_HAS_SETPROCTITLE_INIT
-#define setproctitle_init(argc, argv, envp)
-#endif
-
-#define setproctitle(fmt, ...)
-#endif
-
-#endif /* __CR_SETPROCTITLE_H__ */
diff --git a/criu/include/string.h b/criu/include/string.h
deleted file mode 100644
index bc5f9d21967a..000000000000
--- a/criu/include/string.h
+++ /dev/null
@@ -1,20 +0,0 @@ 
-#ifndef __CR_STRING_H__
-#define __CR_STRING_H__
-
-#include <sys/types.h>
-
-#ifdef CONFIG_HAS_LIBBSD
-# include <bsd/string.h>
-#endif
-
-#include "common/config.h"
-
-#ifndef CONFIG_HAS_STRLCPY
-extern size_t strlcpy(char *dest, const char *src, size_t size);
-#endif
-
-#ifndef CONFIG_HAS_STRLCAT
-extern size_t strlcat(char *dest, const char *src, size_t count);
-#endif
-
-#endif /* __CR_STRING_H__ */
diff --git a/criu/string.c b/criu/lib-bsd.c
similarity index 52%
rename from criu/string.c
rename to criu/lib-bsd.c
index 543c642912c6..0e3fcaf7d2bd 100644
--- a/criu/string.c
+++ b/criu/lib-bsd.c
@@ -1,14 +1,14 @@ 
-/*
- * Adopted from linux kernel
- */
-#include <sys/types.h>
+#include <stddef.h>
 #include <string.h>
 
-#include "string.h"
+#include "cr-libs.h"
+
+/*
+ * Adopted from Linux kernel
+ */
 
-#ifndef CONFIG_HAS_STRLCPY
 /**
- * strlcpy - Copy a %NUL terminated string into a sized buffer
+ * criu_strlcpy - Copy a %NULL terminated string into a sized buffer
  * @dest: Where to copy the string to
  * @src: Where to copy the string from
  * @size: size of destination buffer
@@ -18,7 +18,7 @@ 
  * of course, the buffer size is zero). It does not pad
  * out the result like strncpy() does.
  */
-size_t strlcpy(char *dest, const char *src, size_t size)
+static size_t criu_strlcpy(char *dest, const char *src, size_t size)
 {
 	size_t ret = strlen(src);
 
@@ -29,16 +29,14 @@  size_t strlcpy(char *dest, const char *src, size_t size)
 	}
 	return ret;
 }
-#endif
 
-#ifndef CONFIG_HAS_STRLCAT
 /**
- * strlcat - Append a length-limited, %NUL-terminated string to another
+ * criu_strlcat - Append a length-limited, %NULL-terminated string to another
  * @dest: The string to be appended to
  * @src: The string to append to it
  * @count: The size of the destination buffer.
  */
-size_t strlcat(char *dest, const char *src, size_t count)
+static size_t criu_strlcat(char *dest, const char *src, size_t count)
 {
 	size_t dsize = strlen(dest);
 	size_t len = strlen(src);
@@ -57,4 +55,43 @@  size_t strlcat(char *dest, const char *src, size_t count)
 	dest[len] = 0;
 	return res;
 }
-#endif
+
+size_t strlcpy(char *dest, const char *src, size_t size)
+{
+	__typeof__(strlcpy) *f;
+
+	f = shared_libs_lookup_once(LIB_BSD, __func__);
+	if (f)
+		return f(dest, src, size);
+	else
+		return criu_strlcpy(dest, src, size);
+}
+
+size_t strlcat(char *dest, const char *src, size_t count)
+{
+	__typeof__(strlcat) *f;
+
+	f = shared_libs_lookup_once(LIB_BSD, __func__);
+	if (f)
+		return f(dest, src, count);
+	else
+		return criu_strlcat(dest, src, count);
+}
+
+void setproctitle_init(int argc, char *argv[], char *envp[])
+{
+	__typeof__(setproctitle_init) *f;
+
+	f = shared_libs_lookup_once(LIB_BSD, __func__);
+	if (f)
+		f(argc, argv, envp);
+}
+
+void setproctitle(const char *fmt, ...)
+{
+	__typeof__(setproctitle) *f;
+
+	f = shared_libs_lookup_once(LIB_BSD, __func__);
+	if (f)
+		f(fmt);
+}
diff --git a/criu/log.c b/criu/log.c
index 0ee113b91ac2..d259ac2ecb5d 100644
--- a/criu/log.c
+++ b/criu/log.c
@@ -20,7 +20,7 @@ 
 #include "servicefd.h"
 #include "rst-malloc.h"
 #include "common/lock.h"
-#include "string.h"
+#include "lib-bsd.h"
 #include "version.h"
 
 #include "../soccr/soccr.h"
diff --git a/criu/net.c b/criu/net.c
index 2285ae4032c0..758bac9d9119 100644
--- a/criu/net.c
+++ b/criu/net.c
@@ -35,7 +35,7 @@ 
 #include "action-scripts.h"
 #include "sockets.h"
 #include "pstree.h"
-#include "string.h"
+#include "lib-bsd.h"
 #include "sysctl.h"
 #include "kerndat.h"
 #include "util.h"
diff --git a/criu/proc_parse.c b/criu/proc_parse.c
index fa7644992b5e..88b0bd8a45ee 100644
--- a/criu/proc_parse.c
+++ b/criu/proc_parse.c
@@ -33,7 +33,7 @@ 
 #include "cr_options.h"
 #include "sysfs_parse.h"
 #include "seccomp.h"
-#include "string.h"
+#include "lib-bsd.h"
 #include "namespaces.h"
 #include "files-reg.h"
 #include "cgroup.h"
diff --git a/criu/tun.c b/criu/tun.c
index b13148b0b912..0433178b4756 100644
--- a/criu/tun.c
+++ b/criu/tun.c
@@ -12,7 +12,7 @@ 
 #include "cr_options.h"
 #include "imgset.h"
 #include "protobuf.h"
-#include "string.h"
+#include "lib-bsd.h"
 #include "files.h"
 #include "files-reg.h"
 #include "tun.h"