[CRIU] [PATCH 4/4] zdtm: add a test case for checking file descriptor flags
Andrey Vagin
avagin at openvz.org
Mon Apr 7 03:00:15 PDT 2014
Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
test/zdtm.sh | 1 +
test/zdtm/live/static/Makefile | 1 +
test/zdtm/live/static/fd.c | 109 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 111 insertions(+)
create mode 100644 test/zdtm/live/static/fd.c
diff --git a/test/zdtm.sh b/test/zdtm.sh
index a8e6e2f..45daa96 100755
--- a/test/zdtm.sh
+++ b/test/zdtm.sh
@@ -104,6 +104,7 @@ static/chroot
static/chroot-file
static/rtc
transition/maps007
+static/fd
"
# Duplicate list with ns/ prefix
TEST_LIST=$TEST_LIST$(echo $TEST_LIST | tr ' ' '\n' | sed 's#^#ns/#')
diff --git a/test/zdtm/live/static/Makefile b/test/zdtm/live/static/Makefile
index baeedcd..9f5b1cd 100644
--- a/test/zdtm/live/static/Makefile
+++ b/test/zdtm/live/static/Makefile
@@ -133,6 +133,7 @@ TST_FILE = \
unlink_mmap02 \
file_shared \
file_append \
+ fd \
cow01 \
fdt_shared \
sockets00 \
diff --git a/test/zdtm/live/static/fd.c b/test/zdtm/live/static/fd.c
new file mode 100644
index 0000000..6d4289d
--- /dev/null
+++ b/test/zdtm/live/static/fd.c
@@ -0,0 +1,109 @@
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "zdtmtst.h"
+
+const char *test_doc = "Check that file descriptors flags are restored correctly";
+const char *test_author = "Andrew Vagin <avagin at openvz.org>";
+
+char *filename;
+TEST_OPTION(filename, string, "file name", 1);
+
+#ifndef O_PATH
+#define O_PATH 010000000
+#endif
+
+int main(int argc, char **argv)
+{
+ int fd_wo, fd_ro, fd_p, ret, fd_cl_on_ex;
+ int flags;
+ char buf[4096];
+
+ test_init(argc, argv);
+
+ fd_wo = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
+ if (fd_wo == -1) {
+ err("open");
+ return 1;
+ }
+
+ fd_cl_on_ex = dup(fd_wo);
+ if (fd_cl_on_ex < 0) {
+ err("dup");
+ return 1;
+ }
+ fcntl(fd_cl_on_ex, F_SETFD, FD_CLOEXEC);
+
+ fd_ro = open(filename, O_RDONLY, 0644);
+ if (fd_ro == -1) {
+ err("open");
+ return 1;
+ }
+
+ fd_p = open(filename, O_PATH, 0644);
+ if (fd_p == -1) {
+ err("open");
+ return 1;
+ }
+
+ test_daemon();
+ test_waitsig();
+
+ ret = write(fd_wo, "test", 4);
+ if (ret != 4) {
+ err("Can't write x");
+ return 1;
+ }
+
+ ret = read(fd_wo, buf, sizeof(buf));
+ if (ret != -1 || errno != EBADF) {
+ err("%d:%d\n", ret, errno);
+ return 1;
+ }
+
+ ret = write(fd_ro, "test", 4);
+ if (ret != -1 || errno != EBADF) {
+ err("%d:%d\n", ret, errno);
+ return 1;
+ }
+
+ ret = read(fd_ro, buf, sizeof(buf));
+ if (ret != 4) {
+ err("%d:%d\n", ret, errno);
+ return 1;
+ }
+
+ ret = write(fd_p, "test", 4);
+ if (ret != -1 || errno != EBADF) {
+ err("%d:%d\n", ret, errno);
+ return 1;
+ }
+
+ ret = read(fd_p, buf, sizeof(buf));
+ if (ret != -1 || errno != EBADF) {
+ err("%d:%d\n", ret, errno);
+ return 1;
+ }
+
+ flags = fcntl(fd_wo, F_GETFD);
+ if (flags & FD_CLOEXEC) {
+ err("%#x", flags);
+ return 1;
+ }
+
+ flags = fcntl(fd_cl_on_ex, F_GETFD);
+ if (!(flags & FD_CLOEXEC)) {
+ err("%#x", flags);
+ return 1;
+ }
+
+ pass();
+
+ return 0;
+}
--
1.8.5.3
More information about the CRIU
mailing list