[PATCH 3/3] tests: check -z and -Z options

Paul Chaignon paul.chaignon at gmail.com
Mon Apr 1 21:16:06 UTC 2019


This patch adds two test cases for the -z and -Z options.

Signed-off-by: Paul Chaignon <paul.chaignon at gmail.com>
---
 stage_output.c              | 74 +++++++++++++++++++++++++++++++++++++
 tests/.gitignore            |  2 +
 tests/gen_tests.in          |  2 +
 tests/open-Z.c              | 59 +++++++++++++++++++++++++++++
 tests/open-z.c              | 58 +++++++++++++++++++++++++++++
 tests/pure_executables.list |  2 +
 6 files changed, 197 insertions(+)
 create mode 100644 stage_output.c
 create mode 100644 tests/open-Z.c
 create mode 100644 tests/open-z.c

diff --git a/stage_output.c b/stage_output.c
new file mode 100644
index 00000000..81b29b31
--- /dev/null
+++ b/stage_output.c
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2017 Burkhard Kohl <burkhard.kohl at intel.com>
+ * Copyright (c) 2019 Paul Chaignon <paul.chaignon at gmail.com>
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+/*
+ *  Output staging is based on the "open_memstream()" function, see:
+ *  http://man7.org/linux/man-pages/man3/open_memstream.3.html
+ *  Requires glibc version 2.7 mininal
+ *
+ *  open_memstream returns a FILE stream that allows writing to a
+ *  dynamically growing buffer, that can be either copied to
+ *  tcp->outf (syscall successful) or dropped (syscall failed)
+ */
+
+#include "defs.h"
+#if HAVE_OPEN_MEMSTREAM
+
+FILE *
+strace_openmemstream(struct tcb *tcp)
+{
+	FILE *fp = NULL;
+
+	tcp->memfptr = NULL;
+	fp = open_memstream(&tcp->memfptr, &tcp->memfloc);
+	if (!fp)
+		perror_msg_and_die("open_memstream");
+	/* Call to fflush required to update tcp->memfptr, see open_memstream
+	 * man page. */
+	fflush(fp);
+	tcp->memf = fp;
+	return fp;
+}
+
+void
+drop_staged_out(struct tcb *tcp)
+{
+	if (!tcp->memf)
+		return;
+
+	if (fclose(tcp->memf))
+		perror_msg("fclose(tcp->memf)");
+	if (tcp->memfptr) {
+		if (debug_flag)
+			error_msg("syscall output dropped: %s", tcp->memfptr);
+		free(tcp->memfptr);
+		tcp->memfptr = NULL;
+	}
+
+	/* reopen tcp->memf for subsequent use */
+	strace_openmemstream(tcp);
+}
+
+void
+publish_staged_out(struct tcb *tcp)
+{
+	if (!tcp->memf)
+		return;
+
+	if (fclose(tcp->memf))
+		perror_msg("flose(tcp->memf)");
+	if (tcp->memfptr) {
+		fprintf(tcp->outf, "%s", tcp->memfptr);
+		free(tcp->memfptr);
+		tcp->memfptr = NULL;
+	}
+
+	/* reopen tcp->memf for subsequent use */
+	strace_openmemstream(tcp);
+}
+#endif
diff --git a/tests/.gitignore b/tests/.gitignore
index 5a601641..f837656a 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -362,6 +362,8 @@ oldselect-efault
 oldselect-efault-P
 oldstat
 open
+open-z
+open-Z
 openat
 orphaned_process_group
 osf_utimes
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index 314608eb..d4ba061e 100644
--- a/tests/gen_tests.in
+++ b/tests/gen_tests.in
@@ -309,6 +309,8 @@ oldselect-efault	-a13 -e trace=select
 oldselect-efault-P	-a13 -e trace=select -P /dev/full 9>>/dev/full
 oldstat	-a32 -v -P stat.sample -P /dev/full
 open	-a30 -P $NAME.sample
+open-Z	-a30 -e trace=open -P open.sample -Z
+open-z	-a30 -e trace=open -P open.sample -z
 openat	-a36 -P $NAME.sample
 orphaned_process_group	. "${srcdir=.}/PTRACE_SEIZE.sh"; run_strace_match_diff -f -e trace=none -e signal='!chld'
 osf_utimes	-a21
diff --git a/tests/open-Z.c b/tests/open-Z.c
new file mode 100644
index 00000000..b692502f
--- /dev/null
+++ b/tests/open-Z.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2019 Paul Chaignon <paul.chaignon at gmail.com>
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#include "tests.h"
+#include <asm/unistd.h>
+
+#ifdef __NR_open
+
+# include <asm/fcntl.h>
+# include <stdio.h>
+# include <unistd.h>
+
+int
+main(void)
+{
+	static const char sample[] = "open.sample";
+	long fd = syscall(__NR_open, sample, O_RDONLY | O_CREAT, 0400);
+
+	if (fd == -1)
+		printf("open(\"%s\", O_RDONLY|O_CREAT, 0400) = %s\n",
+		       sample, sprintrc(fd));
+
+	if (fd != -1) {
+		close(fd);
+		if (unlink(sample))
+			perror_msg_and_fail("unlink");
+
+		fd = syscall(__NR_open, sample, O_RDONLY);
+		if (fd == -1)
+			printf("open(\"%s\", O_RDONLY) = %s\n", sample,
+			       sprintrc(fd));
+
+		fd = syscall(__NR_open, sample,
+			     O_WRONLY | O_NONBLOCK | 0x80000000);
+		if (fd == -1)
+			printf("open(\"%s\", O_WRONLY|O_NONBLOCK|0x80000000) = %s\n",
+			       sample, sprintrc(fd));
+	}
+
+# ifdef O_TMPFILE
+	fd = syscall(__NR_open, sample, O_WRONLY | O_TMPFILE, 0600);
+	if (fd == -1)
+		printf("open(\"%s\", O_WRONLY|O_TMPFILE, 0600) = %s\n",
+		       sample, sprintrc(fd));
+# endif /* O_TMPFILE */
+
+	puts("+++ exited with 0 +++");
+	return 0;
+}
+
+#else
+
+SKIP_MAIN_UNDEFINED("__NR_open")
+
+#endif
diff --git a/tests/open-z.c b/tests/open-z.c
new file mode 100644
index 00000000..ab988105
--- /dev/null
+++ b/tests/open-z.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2019 Paul Chaignon <paul.chaignon at gmail.com>
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#include "tests.h"
+#include <asm/unistd.h>
+
+#ifdef __NR_open
+
+# include <asm/fcntl.h>
+# include <stdio.h>
+# include <unistd.h>
+
+int
+main(void)
+{
+	static const char sample[] = "open.sample";
+	long fd = syscall(__NR_open, sample, O_RDONLY | O_CREAT, 0400);
+
+	if (fd != -1) {
+		printf("open(\"%s\", O_RDONLY|O_CREAT, 0400) = %s\n",
+		       sample, sprintrc(fd));
+
+		close(fd);
+		if (unlink(sample))
+			perror_msg_and_fail("unlink");
+
+		fd = syscall(__NR_open, sample, O_RDONLY);
+		if (fd != -1)
+			printf("open(\"%s\", O_RDONLY) = %s\n", sample,
+			       sprintrc(fd));
+
+		fd = syscall(__NR_open, sample,
+			     O_WRONLY | O_NONBLOCK | 0x80000000);
+		if (fd != -1)
+			printf("open(\"%s\", O_WRONLY|O_NONBLOCK|0x80000000) = %s\n",
+			       sample, sprintrc(fd));
+	}
+
+# ifdef O_TMPFILE
+	fd = syscall(__NR_open, sample, O_WRONLY | O_TMPFILE, 0600);
+	if (fd != -1)
+		printf("open(\"%s\", O_WRONLY|O_TMPFILE, 0600) = %s\n",
+		       sample, sprintrc(fd));
+# endif /* O_TMPFILE */
+
+	puts("+++ exited with 0 +++");
+	return 0;
+}
+
+#else
+
+SKIP_MAIN_UNDEFINED("__NR_open")
+
+#endif
diff --git a/tests/pure_executables.list b/tests/pure_executables.list
index b695a0ec..210ccb57 100755
--- a/tests/pure_executables.list
+++ b/tests/pure_executables.list
@@ -310,6 +310,8 @@ oldselect
 oldselect-efault
 oldstat
 open
+open-z
+open-Z
 openat
 osf_utimes
 pause
-- 
2.17.1



More information about the Strace-devel mailing list