[11/12] util: Compile out getpid-always when not needed

Submitted by Pavel Emelianov on April 24, 2017, 8:09 p.m.

Details

Message ID 58FE5B72.3060001@virtuozzo.com
State New
Series "Speed up kdat checks"
Headers show

Commit Message

Pavel Emelianov April 24, 2017, 8:09 p.m.
Some glibc-s still provide working getpid() vs clone() calls. For
such systems calling getpid syscall all the times can be speeded up.

Plus a proggie to find out whether the getpid() works OK or not.

Signed-off-by: Pavel Emelyanov <xemul@virtuozzo.com>
---
 criu/util.c         |  3 +++
 scripts/check_pid.c | 25 +++++++++++++++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 scripts/check_pid.c

Patch hide | download patch | download mbox

diff --git a/criu/util.c b/criu/util.c
index dad9386..a3929c8 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -35,6 +35,7 @@ 
 #include <sched.h>
 #include <ctype.h>
 
+#include "config.h"
 #include "bitops.h"
 #include "page.h"
 #include "common/compiler.h"
@@ -1416,6 +1417,7 @@  out_munmap:
 	return ret;
 }
 
+#ifndef CONFIG_GLIBC_GETPID_OK
 /*
  * In glibc 2.24, getpid() returns a parent PID, if a child was
  * created with the CLONE_VM flag.
@@ -1424,3 +1426,4 @@  int getpid()
 {
        return syscall(__NR_getpid);
 }
+#endif
diff --git a/scripts/check_pid.c b/scripts/check_pid.c
new file mode 100644
index 0000000..121173a
--- /dev/null
+++ b/scripts/check_pid.c
@@ -0,0 +1,25 @@ 
+#define _GNU_SOURCE
+#include <unistd.h>
+#include <sched.h>
+#include <signal.h>
+#include <sys/wait.h>
+
+static int chk_pid(void *arg)
+{
+	return (getpid() != *(int *)arg) ? 0 : 1;
+}
+
+int main(int argc, char **argv)
+{
+	int pid, kid, status;
+	static char stk[2 << 12];
+
+	pid = getpid();
+	kid = clone(chk_pid, stk + sizeof(stk), CLONE_VM | SIGCHLD, &pid);
+	if (kid < 0)
+		return 1;
+	waitpid(kid, &status, 0);
+	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
+		return 1;
+	return 0;
+}