[LTP] [PATCH v2] fcntl37: test posix lock across execve

Xiong Zhou xzhou@redhat.com
Tue Mar 27 15:38:36 CEST 2018


Signed-off-by: Xiong Zhou <xzhou@redhat.com>
---

v2:
	s/LDLIBS/CFLAGS/ in Makefile.
	No fork() and no another function when checking the lock.
	TST_NO_DEFAULT_MAIN to pass fd across execve.
	Update author info. It's from Daniel originally. I copied
from fcntl36.c for v1 patch and forgot to update it. Sorry for
missing this.

 testcases/kernel/syscalls/fcntl/Makefile  |  11 ++--
 testcases/kernel/syscalls/fcntl/fcntl37.c | 101 ++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+), 4 deletions(-)
 create mode 100644 testcases/kernel/syscalls/fcntl/fcntl37.c

diff --git a/testcases/kernel/syscalls/fcntl/Makefile b/testcases/kernel/syscalls/fcntl/Makefile
index ae37214..7a215c8 100644
--- a/testcases/kernel/syscalls/fcntl/Makefile
+++ b/testcases/kernel/syscalls/fcntl/Makefile
@@ -21,11 +21,14 @@ top_srcdir		?= ../../../..
 fcntl33: LDLIBS+=-lrt
 fcntl33_64: LDLIBS+=-lrt
 
-fcntl34: LDLIBS += -lpthread
-fcntl34_64: LDLIBS += -lpthread
+fcntl34: CFLAGS += -pthread
+fcntl34_64: CFLAGS += -pthread
 
-fcntl36: LDLIBS += -lpthread
-fcntl36_64: LDLIBS += -lpthread
+fcntl36: CFLAGS += -pthread
+fcntl36_64: CFLAGS += -pthread
+
+fcntl37: CFLAGS += -pthread
+fcntl37_64: CFLAGS += -pthread
 
 include $(top_srcdir)/include/mk/testcases.mk
 include $(abs_srcdir)/../utils/newer_64.mk
diff --git a/testcases/kernel/syscalls/fcntl/fcntl37.c b/testcases/kernel/syscalls/fcntl/fcntl37.c
new file mode 100644
index 0000000..d847ae3
--- /dev/null
+++ b/testcases/kernel/syscalls/fcntl/fcntl37.c
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2018 Red Hat Inc. All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: "Daniel P. Berrangé" <berrange@redhat.com>
+ *
+ * This is testing for
+ *
+ *	"Record locks are not inherited by a child created via fork(2),
+ *	 but are preserved across an execve(2)."
+ *
+ * from fcntl(2) man page.
+ *
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <sched.h>
+#include <limits.h>
+
+#define TST_NO_DEFAULT_MAIN
+
+#include "lapi/fcntl.h"
+#include "tst_safe_pthread.h"
+#include "tst_test.h"
+
+static const char fname[] = "tst_lock_execve";
+
+static void *thread_fn(void *arg)
+{
+	int fd = *(intptr_t *)arg;
+	tst_res(TINFO, "Thread running. fd %d", fd);
+	while(1) sleep(1);
+	return NULL;
+}
+
+int main(int argc, char *argv[])
+{
+	int fd, ret = 0;
+
+	if (argc == 2) {
+		fd = atoi(argv[1]);
+		struct flock flck = {
+			.l_type = F_WRLCK,
+			.l_whence = SEEK_SET,
+			.l_start = 0,
+			.l_len = 1,
+		};
+		SAFE_FCNTL(fd, F_GETLK, &flck);
+		if (flck.l_type == F_UNLCK)
+			tst_res(TFAIL, "Record lock gets lost after execve");
+		else
+			tst_res(TPASS, "Record lock survives execve");
+		SAFE_CLOSE(fd);
+		SAFE_UNLINK(fname);
+		exit(0);
+	}
+
+	fd = SAFE_OPEN(fname, O_RDWR|O_CREAT, 0755);
+	struct flock64 flck = {
+		.l_type = F_WRLCK,
+		.l_whence = SEEK_SET,
+		.l_start  = 0,
+		.l_len    = 1,
+	};
+	SAFE_FCNTL(fd, F_SETLK, &flck);
+
+	pthread_t th;
+	SAFE_PTHREAD_CREATE(&th, NULL, thread_fn, (void *)&fd);
+	sleep(1);
+
+	int flags=SAFE_FCNTL(fd, F_GETFD);
+	flags &= ~FD_CLOEXEC;
+	SAFE_FCNTL(fd, F_SETFD, flags);
+
+	char fdstr[10];
+	snprintf(fdstr, 10, "%d", fd);
+	char * const newargv[] = { argv[0], fdstr, NULL };
+	tst_res(TINFO, "execve %s with %s locked", argv[0], fname);
+	ret = execve(argv[0], newargv, NULL);
+	return ret;
+}
-- 
1.8.3.1



More information about the ltp mailing list