public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01
@ 2019-06-04 11:47 Christian Amann
  2019-06-04 11:47 ` [LTP] [PATCH v2 3/4] syscalls/pidfd_send_signal02 Christian Amann
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Christian Amann @ 2019-06-04 11:47 UTC (permalink / raw)
  To: ltp

Add testcase to check if pidfd_send_signal() can provide
the same functionality as rt_sigqueueinfo().

Signed-off-by: Christian Amann <camann@suse.com>
---
 configure.ac                                       |   2 +
 runtest/syscalls                                   |   2 +
 .../kernel/syscalls/pidfd_send_signal/.gitignore   |   1 +
 .../kernel/syscalls/pidfd_send_signal/Makefile     |  14 +++
 .../syscalls/pidfd_send_signal/pidfd_send_signal.h |  27 ++++++
 .../pidfd_send_signal/pidfd_send_signal01.c        | 108 +++++++++++++++++++++
 6 files changed, 154 insertions(+)
 create mode 100644 testcases/kernel/syscalls/pidfd_send_signal/.gitignore
 create mode 100644 testcases/kernel/syscalls/pidfd_send_signal/Makefile
 create mode 100644 testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal.h
 create mode 100644 testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal01.c

diff --git a/configure.ac b/configure.ac
index 5a3dc5b62..b3e108306 100644
--- a/configure.ac
+++ b/configure.ac
@@ -71,6 +71,7 @@ AC_CHECK_FUNCS([ \
     mkdirat \
     mknodat \
     openat \
+    pidfd_send_signal \
     preadv \
     preadv2 \
     profil \
@@ -237,6 +238,7 @@ else
 fi
 AC_DEFINE_UNQUOTED(NUMA_ERROR_MSG, ["$numa_error_msg"], [Error message when no NUMA support])
 
+
 LTP_CHECK_SYSCALL_PERF_EVENT_OPEN
 LTP_CHECK_SYSCALL_QUOTACTL
 LTP_CHECK_SYSCALL_SIGNALFD
diff --git a/runtest/syscalls b/runtest/syscalls
index 762b15b1f..bd71a12f8 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -836,6 +836,8 @@ pause03 pause03
 personality01 personality01
 personality02 personality02
 
+pidfd_send_signal01 pidfd_send_signal01
+
 pipe01 pipe01
 pipe02 pipe02
 pipe03 pipe03
diff --git a/testcases/kernel/syscalls/pidfd_send_signal/.gitignore b/testcases/kernel/syscalls/pidfd_send_signal/.gitignore
new file mode 100644
index 000000000..7ccbf2d80
--- /dev/null
+++ b/testcases/kernel/syscalls/pidfd_send_signal/.gitignore
@@ -0,0 +1 @@
+/pidfd_send_signal01
diff --git a/testcases/kernel/syscalls/pidfd_send_signal/Makefile b/testcases/kernel/syscalls/pidfd_send_signal/Makefile
new file mode 100644
index 000000000..23e4ec478
--- /dev/null
+++ b/testcases/kernel/syscalls/pidfd_send_signal/Makefile
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+#
+#   Copyright (c) 2019 SUSE LLC
+#
+#   Author: Christian Amann <camann@suse.com>
+#
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
+
+pidfd_send_signal01: CFLAGS += -pthread
diff --git a/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal.h b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal.h
new file mode 100644
index 000000000..aa883c932
--- /dev/null
+++ b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal.h
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 SUSE LLC
+ * Author: Christian Amann <camann@suse.com>
+ */
+
+#ifndef PIDFD_SEND_SIGNAL_H
+#define PIDFD_SEND_SIGNAL_H
+
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+
+static void check_syscall_support(void)
+{
+	/* allow the tests to fail early */
+	tst_syscall(__NR_pidfd_send_signal);
+}
+
+#ifndef HAVE_PIDFD_SEND_SIGNAL
+static int pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
+				 unsigned int flags)
+{
+	return tst_syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
+}
+#endif /* HAVE_PIDFD_SEND_SIGNAL */
+
+#endif /* PIDFD_SEND_SIGNAL_H */
diff --git a/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal01.c b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal01.c
new file mode 100644
index 000000000..f44fce013
--- /dev/null
+++ b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal01.c
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 SUSE LLC
+ * Author: Christian Amann <camann@suse.com>
+ */
+/*
+ * Tests if the pidfd_send_signal syscall behaves
+ * like rt_sigqueueinfo when a pointer to a siginfo_t
+ * struct is passed.
+ */
+
+#define _GNU_SOURCE
+
+#include <signal.h>
+#include <stdlib.h>
+#include "tst_safe_pthread.h"
+#include "pidfd_send_signal.h"
+
+#define SIGNAL  SIGUSR1
+#define DATA	777
+
+static struct sigaction *sig_action;
+static int sig_rec;
+static siginfo_t *uinfo;
+static int pidfd;
+
+static void received_signal(int sig, siginfo_t *info, void *ucontext)
+{
+	if (info && ucontext) {
+		if (sig == SIGNAL && uinfo->si_value.sival_int == DATA) {
+			tst_res(TPASS, "Received correct signal and data!");
+			sig_rec = 1;
+		} else {
+			tst_res(TFAIL, "Received wrong signal and/or data!");
+		}
+	} else {
+		tst_res(TFAIL, "Signal handling went wrong!");
+	}
+}
+
+static void *handle_thread(void *arg)
+{
+	SAFE_SIGACTION(SIGNAL, sig_action, NULL);
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
+	return arg;
+}
+
+static void verify_pidfd_send_signal(void)
+{
+	pthread_t thr;
+
+	SAFE_PTHREAD_CREATE(&thr, NULL, handle_thread, NULL);
+
+	TST_CHECKPOINT_WAIT(0);
+
+	TEST(pidfd_send_signal(pidfd, SIGNAL, uinfo, 0));
+	if (TST_RET != 0) {
+		tst_res(TFAIL | TTERRNO, "pidfd_send_signal() failed");
+		return;
+	}
+
+	TST_CHECKPOINT_WAKE(0);
+	SAFE_PTHREAD_JOIN(thr, NULL);
+
+	if (sig_rec) {
+		tst_res(TPASS,
+			"pidfd_send_signal() behaved like rt_sigqueueinfo()");
+	}
+}
+
+static void setup(void)
+{
+	check_syscall_support();
+
+	pidfd = SAFE_OPEN("/proc/self", O_DIRECTORY | O_CLOEXEC);
+
+	sig_action = SAFE_MALLOC(sizeof(struct sigaction));
+
+	memset(sig_action, 0, sizeof(*sig_action));
+	sig_action->sa_sigaction = received_signal;
+	sig_action->sa_flags = SA_SIGINFO;
+
+	uinfo = SAFE_MALLOC(sizeof(siginfo_t));
+
+	memset(uinfo, 0, sizeof(*uinfo));
+	uinfo->si_signo = SIGNAL;
+	uinfo->si_code = SI_QUEUE;
+	uinfo->si_pid = getpid();
+	uinfo->si_uid = getuid();
+	uinfo->si_value.sival_int = DATA;
+
+	sig_rec = 0;
+}
+
+static void cleanup(void)
+{
+	free(uinfo);
+	free(sig_action);
+	if (pidfd > 0)
+		SAFE_CLOSE(pidfd);
+}
+
+static struct tst_test test = {
+	.test_all = verify_pidfd_send_signal,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_checkpoints = 1,
+};
-- 
2.16.4


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [LTP] [PATCH v2 3/4] syscalls/pidfd_send_signal02
  2019-06-04 11:47 [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01 Christian Amann
@ 2019-06-04 11:47 ` Christian Amann
  2019-06-12 13:54   ` Petr Vorel
  2019-06-04 11:47 ` [LTP] [PATCH v2 4/4] syscalls/pidfd_send_signal03 Christian Amann
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: Christian Amann @ 2019-06-04 11:47 UTC (permalink / raw)
  To: ltp

Add test to check basic error handling of the syscall.

Signed-off-by: Christian Amann <camann@suse.com>
---
 runtest/syscalls                                   |   1 +
 .../kernel/syscalls/pidfd_send_signal/.gitignore   |   1 +
 .../pidfd_send_signal/pidfd_send_signal02.c        | 106 +++++++++++++++++++++
 3 files changed, 108 insertions(+)
 create mode 100644 testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal02.c

diff --git a/runtest/syscalls b/runtest/syscalls
index bd71a12f8..90a1cf8e6 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -837,6 +837,7 @@ personality01 personality01
 personality02 personality02
 
 pidfd_send_signal01 pidfd_send_signal01
+pidfd_send_signal02 pidfd_send_signal02
 
 pipe01 pipe01
 pipe02 pipe02
diff --git a/testcases/kernel/syscalls/pidfd_send_signal/.gitignore b/testcases/kernel/syscalls/pidfd_send_signal/.gitignore
index 7ccbf2d80..6ea6401a8 100644
--- a/testcases/kernel/syscalls/pidfd_send_signal/.gitignore
+++ b/testcases/kernel/syscalls/pidfd_send_signal/.gitignore
@@ -1 +1,2 @@
 /pidfd_send_signal01
+/pidfd_send_signal02
diff --git a/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal02.c b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal02.c
new file mode 100644
index 000000000..83e0790bf
--- /dev/null
+++ b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal02.c
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 SUSE LLC
+ * Author: Christian Amann <camann@suse.com>
+ */
+/*
+ * Tests basic error handling of the pidfd_send_signal
+ * system call.
+ *
+ * 1) Pass invalid flag value to syscall (value chosen
+ *    to be unlikely to collide with future extensions)
+ *    -> EINVAL
+ * 2) Pass a file descriptor that is corresponding to a
+ *    regular file instead of a pid directory
+ *    -> EBADF
+ * 3) Pass a signal that is different from the one used
+ *    to initialize the siginfo_t struct
+ *    -> EINVAL
+ * 4) Try to send signal to other process (init) with
+ *    missing privileges
+ *    -> EPERM
+ */
+
+#define _GNU_SOURCE
+
+#include <signal.h>
+#include "pwd.h"
+#include "tst_safe_pthread.h"
+#include "pidfd_send_signal.h"
+
+#define CORRECT_SIGNAL		SIGUSR1
+#define DIFFERENT_SIGNAL	SIGUSR2
+
+static siginfo_t info;
+static int pidfd;
+static int init_pidfd;
+static int dummyfd;
+
+static struct tcase {
+	int		*fd;
+	siginfo_t	*siginf;
+	int		signal;
+	int		flags;
+	int		exp_err;
+} tcases[] = {
+	{&pidfd, &info, CORRECT_SIGNAL, 99999, EINVAL},
+	{&dummyfd, &info, CORRECT_SIGNAL, 0, EBADF},
+	{&pidfd, &info, DIFFERENT_SIGNAL, 0, EINVAL},
+	{&init_pidfd, &info, CORRECT_SIGNAL, 0, EPERM},
+};
+
+static void verify_pidfd_send_signal(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+
+	TEST(pidfd_send_signal(*tc->fd, tc->signal, tc->siginf, tc->flags));
+	if (tc->exp_err != TST_ERR) {
+		tst_res(TFAIL | TTERRNO,
+			"pidfd_send_signal() did not fail with %s but",
+			tst_strerrno(tc->exp_err));
+		return;
+	}
+
+	tst_res(TPASS | TTERRNO,
+		"pidfd_send_signal() failed as expected");
+}
+
+static void setup(void)
+{
+	struct passwd *pw;
+
+	check_syscall_support();
+
+	pidfd = SAFE_OPEN("/proc/self", O_DIRECTORY | O_CLOEXEC);
+	init_pidfd = SAFE_OPEN("/proc/1", O_DIRECTORY | O_CLOEXEC);
+	dummyfd = SAFE_OPEN("dummy_file", O_RDWR | O_CREAT, 0664);
+
+	if (getuid() == 0) {
+		pw = SAFE_GETPWNAM("nobody");
+		SAFE_SETUID(pw->pw_uid);
+	}
+
+	memset(&info, 0, sizeof(info));
+	info.si_signo = CORRECT_SIGNAL;
+	info.si_code = SI_QUEUE;
+	info.si_pid = getpid();
+	info.si_uid = getuid();
+}
+
+static void cleanup(void)
+{
+	if (dummyfd > 0)
+		SAFE_CLOSE(dummyfd);
+	if (init_pidfd > 0)
+		SAFE_CLOSE(init_pidfd);
+	if (pidfd > 0)
+		SAFE_CLOSE(pidfd);
+}
+
+static struct tst_test test = {
+	.test = verify_pidfd_send_signal,
+	.tcnt = ARRAY_SIZE(tcases),
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+};
-- 
2.16.4


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [LTP] [PATCH v2 4/4] syscalls/pidfd_send_signal03
  2019-06-04 11:47 [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01 Christian Amann
  2019-06-04 11:47 ` [LTP] [PATCH v2 3/4] syscalls/pidfd_send_signal02 Christian Amann
@ 2019-06-04 11:47 ` Christian Amann
  2019-06-12 14:13   ` Petr Vorel
  2019-06-06 13:10 ` [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01 Cyril Hrubis
  2019-06-12 13:41 ` Petr Vorel
  3 siblings, 1 reply; 8+ messages in thread
From: Christian Amann @ 2019-06-04 11:47 UTC (permalink / raw)
  To: ltp

Add testcase to check if the syscall will send a signal
to a process with the same PID as the targeted process.

Signed-off-by: Christian Amann <camann@suse.com>
---
 runtest/syscalls                                   |   1 +
 .../kernel/syscalls/pidfd_send_signal/.gitignore   |   1 +
 .../pidfd_send_signal/pidfd_send_signal03.c        | 142 +++++++++++++++++++++
 3 files changed, 144 insertions(+)
 create mode 100644 testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal03.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 90a1cf8e6..7274c7741 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -838,6 +838,7 @@ personality02 personality02
 
 pidfd_send_signal01 pidfd_send_signal01
 pidfd_send_signal02 pidfd_send_signal02
+pidfd_send_signal03 pidfd_send_signal03
 
 pipe01 pipe01
 pipe02 pipe02
diff --git a/testcases/kernel/syscalls/pidfd_send_signal/.gitignore b/testcases/kernel/syscalls/pidfd_send_signal/.gitignore
index 6ea6401a8..cba7d50a4 100644
--- a/testcases/kernel/syscalls/pidfd_send_signal/.gitignore
+++ b/testcases/kernel/syscalls/pidfd_send_signal/.gitignore
@@ -1,2 +1,3 @@
 /pidfd_send_signal01
 /pidfd_send_signal02
+/pidfd_send_signal03
diff --git a/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal03.c b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal03.c
new file mode 100644
index 000000000..a04d59ef2
--- /dev/null
+++ b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal03.c
@@ -0,0 +1,142 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 SUSE LLC
+ * Author: Christian Amann <camann@suse.com>
+ */
+/*
+ * This test checks if the pidfd_send_signal syscall wrongfully sends
+ * a signal to a new process which inherited the PID of the actual
+ * target process.
+ * In order to do so it is necessary to start a process with a pre-
+ * determined PID. This is accomplished by writing to the
+ * /proc/sys/kernel/ns_last_pid file.
+ * By utilizing this, this test forks two children with the same PID.
+ * It is then checked, if the syscall will send a signal to the second
+ * child using the pidfd of the first one.
+ */
+
+#define _GNU_SOURCE
+
+#include <signal.h>
+#include <stdio.h>
+#include <unistd.h>
+#include "pidfd_send_signal.h"
+#include "tst_safe_pthread.h"
+//#include "tst_safe_file_ops.h"
+
+#define PIDTRIES	3
+
+static char *last_pid_file;
+static int pidfd, new_pidfd;
+static int old_inode, new_inode;
+
+static int get_inode_number(int fd)
+{
+	struct stat file_stat;
+
+	SAFE_FSTAT(fd, &file_stat);
+	return file_stat.st_ino;
+}
+
+static void verify_pidfd_send_signal(void)
+{
+	pid_t pid, new_pid;
+	char pid_filename[32];
+	char pid_str[16];
+	int i, fail;
+
+	fail = 1;
+	for (i = 0; i < PIDTRIES; i++) {
+		pid = SAFE_FORK();
+		if (pid == 0) {
+			TST_CHECKPOINT_WAIT(0);
+			return;
+		}
+
+		sprintf(pid_filename, "/proc/%d", pid);
+		pidfd = SAFE_OPEN(pid_filename, O_DIRECTORY | O_CLOEXEC);
+		old_inode = get_inode_number(pidfd);
+
+		TST_CHECKPOINT_WAKE(0);
+		tst_reap_children();
+
+		/* Manipulate PID for next process */
+		sprintf(pid_str, "%d", pid - 1);
+		SAFE_FILE_PRINTF(last_pid_file, pid_str);
+
+		new_pid = SAFE_FORK();
+		if (new_pid == 0) {
+			TST_CHECKPOINT_WAIT(0);
+			return;
+		}
+
+		if (new_pid == pid) {
+			new_pidfd = SAFE_OPEN(pid_filename,
+					O_DIRECTORY | O_CLOEXEC);
+			new_inode = get_inode_number(new_pidfd);
+			SAFE_CLOSE(new_pidfd);
+			fail = 0;
+			break;
+		}
+
+		if (i < PIDTRIES) {
+			tst_res(TINFO,
+				"Failed to set correct PID, trying again...");
+		}
+		SAFE_CLOSE(pidfd);
+		TST_CHECKPOINT_WAKE(0);
+		tst_reap_children();
+	}
+	if (fail) {
+		tst_brk(TBROK,
+			"Could not set new child to same PID as the old one!");
+	}
+	if (old_inode == new_inode) {
+		tst_res(TWARN,
+			"File descriptor of new process points to the inode "
+			"of the old process!");
+	}
+
+	TEST(pidfd_send_signal(pidfd, SIGUSR1, NULL, 0));
+	if (TST_RET == -1 && TST_ERR == ESRCH) {
+		tst_res(TPASS,
+			"Did not send signal to wrong process with same PID!");
+	} else {
+		tst_res(TFAIL | TTERRNO,
+			"pidf_send_signal() ended unexpectedly - return value: %ld, error",
+			TST_RET);
+	}
+	TST_CHECKPOINT_WAKE(0);
+	tst_reap_children();
+
+	SAFE_CLOSE(pidfd);
+}
+
+static void setup(void)
+{
+	check_syscall_support();
+
+	last_pid_file = "/proc/sys/kernel/ns_last_pid";
+	if (access(last_pid_file, F_OK) == -1) {
+		tst_brk(TCONF, "%s does not exist, cannot set PIDs",
+			last_pid_file);
+	}
+}
+
+static void cleanup(void)
+{
+	tst_reap_children();
+	if (new_pidfd > 0)
+		SAFE_CLOSE(new_pidfd);
+	if (pidfd > 0)
+		SAFE_CLOSE(pidfd);
+}
+
+static struct tst_test test = {
+	.test_all = verify_pidfd_send_signal,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_root = 1,
+	.needs_checkpoints = 1,
+	.forks_child = 1,
+};
-- 
2.16.4


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01
  2019-06-04 11:47 [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01 Christian Amann
  2019-06-04 11:47 ` [LTP] [PATCH v2 3/4] syscalls/pidfd_send_signal02 Christian Amann
  2019-06-04 11:47 ` [LTP] [PATCH v2 4/4] syscalls/pidfd_send_signal03 Christian Amann
@ 2019-06-06 13:10 ` Cyril Hrubis
  2019-06-11 11:12   ` Christian Amann
  2019-06-12 13:41 ` Petr Vorel
  3 siblings, 1 reply; 8+ messages in thread
From: Cyril Hrubis @ 2019-06-06 13:10 UTC (permalink / raw)
  To: ltp

Hi!
When I attempty to compile this code I get:

In file included from pidfd_send_signal.h:11:0,
                 from pidfd_send_signal01.c:17:
pidfd_send_signal.h: In function 'check_syscall_support':
pidfd_send_signal.h:16:14: error: '__NR_pidfd_send_signal' undeclared (first use in this function); did you mean '__NR_perf_event_open'?
  tst_syscall(__NR_pidfd_send_signal);
              ^
../../../../include/lapi/syscalls.h:39:6: note: in definition of macro 'tst_syscall'
  if (NR == __LTP__NR_INVALID_SYSCALL) { \
      ^~
pidfd_send_signal.h:16:14: note: each undeclared identifier is reported only once for each function it appears in
  tst_syscall(__NR_pidfd_send_signal);
              ^
../../../../include/lapi/syscalls.h:39:6: note: in definition of macro 'tst_syscall'
  if (NR == __LTP__NR_INVALID_SYSCALL) { \
      ^~
pidfd_send_signal.h: In function 'pidfd_send_signal':
pidfd_send_signal.h:23:21: error: '__NR_pidfd_send_signal' undeclared (first use in this function); did you mean 'pidfd_send_signal'?
  return tst_syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
                     ^
../../../../include/lapi/syscalls.h:39:6: note: in definition of macro 'tst_syscall'
  if (NR == __LTP__NR_INVALID_SYSCALL) { \
      ^~
make: *** [<builtin>: pidfd_send_signal01] Error 1


Have you forgotten to git add the *.in files with syscall numbers?

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01
  2019-06-06 13:10 ` [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01 Cyril Hrubis
@ 2019-06-11 11:12   ` Christian Amann
  0 siblings, 0 replies; 8+ messages in thread
From: Christian Amann @ 2019-06-11 11:12 UTC (permalink / raw)
  To: ltp

Hi,

No, the *.in file changes are in a separate commit [1]. I didn't include
it in version 2 of this patch because it didn't change.

Sorry for that, I will include those commits in the future.

Kind regards,

Christian

[1] https://patchwork.ozlabs.org/patch/1099966/


On 06/06/2019 15:10, Cyril Hrubis wrote:
> Hi!
> When I attempty to compile this code I get:
>
> In file included from pidfd_send_signal.h:11:0,
>                  from pidfd_send_signal01.c:17:
> pidfd_send_signal.h: In function 'check_syscall_support':
> pidfd_send_signal.h:16:14: error: '__NR_pidfd_send_signal' undeclared (first use in this function); did you mean '__NR_perf_event_open'?
>   tst_syscall(__NR_pidfd_send_signal);
>               ^
> ../../../../include/lapi/syscalls.h:39:6: note: in definition of macro 'tst_syscall'
>   if (NR == __LTP__NR_INVALID_SYSCALL) { \
>       ^~
> pidfd_send_signal.h:16:14: note: each undeclared identifier is reported only once for each function it appears in
>   tst_syscall(__NR_pidfd_send_signal);
>               ^
> ../../../../include/lapi/syscalls.h:39:6: note: in definition of macro 'tst_syscall'
>   if (NR == __LTP__NR_INVALID_SYSCALL) { \
>       ^~
> pidfd_send_signal.h: In function 'pidfd_send_signal':
> pidfd_send_signal.h:23:21: error: '__NR_pidfd_send_signal' undeclared (first use in this function); did you mean 'pidfd_send_signal'?
>   return tst_syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
>                      ^
> ../../../../include/lapi/syscalls.h:39:6: note: in definition of macro 'tst_syscall'
>   if (NR == __LTP__NR_INVALID_SYSCALL) { \
>       ^~
> make: *** [<builtin>: pidfd_send_signal01] Error 1
>
>
> Have you forgotten to git add the *.in files with syscall numbers?
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01
  2019-06-04 11:47 [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01 Christian Amann
                   ` (2 preceding siblings ...)
  2019-06-06 13:10 ` [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01 Cyril Hrubis
@ 2019-06-12 13:41 ` Petr Vorel
  3 siblings, 0 replies; 8+ messages in thread
From: Petr Vorel @ 2019-06-12 13:41 UTC (permalink / raw)
  To: ltp

> Add testcase to check if pidfd_send_signal() can provide
> the same functionality as rt_sigqueueinfo().

> Signed-off-by: Christian Amann <camann@suse.com>
Reviewed-by: Petr Vorel <pvorel@suse.cz>

Generally LGTM, nice work, just tiny details below.

BTW inspiration for other tests from kernel selftest [1]
...

> diff --git a/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal01.c b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal01.c
> new file mode 100644
> index 000000000..f44fce013
> --- /dev/null
> +++ b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal01.c
> @@ -0,0 +1,108 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2019 SUSE LLC
> + * Author: Christian Amann <camann@suse.com>
> + */
> +/*
> + * Tests if the pidfd_send_signal syscall behaves
> + * like rt_sigqueueinfo when a pointer to a siginfo_t
> + * struct is passed.
> + */
> +
> +#define _GNU_SOURCE
Why _GNU_SOURCE? I removed it from all 3 tests and the header and it compiles
fine....

...
> +
> +static void check_syscall_support(void)
> +{
> +	/* allow the tests to fail early */
> +	tst_syscall(__NR_pidfd_send_signal);
> +}
> +
> +#ifndef HAVE_PIDFD_SEND_SIGNAL
> +static int pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
> +				 unsigned int flags)
> +{
> +	return tst_syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
> +}
> +#endif /* HAVE_PIDFD_SEND_SIGNAL */
I wonder if we want to info whether our wrapper or libc wrapper it's used (it
probably takes time when glibc takes it).

> +
> +#endif /* PIDFD_SEND_SIGNAL_H */
> diff --git a/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal01.c b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal01.c
> new file mode 100644
> index 000000000..f44fce013
> --- /dev/null
> +++ b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal01.c
> @@ -0,0 +1,108 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2019 SUSE LLC
> + * Author: Christian Amann <camann@suse.com>
> + */
> +/*
> + * Tests if the pidfd_send_signal syscall behaves
> + * like rt_sigqueueinfo when a pointer to a siginfo_t
> + * struct is passed.
> + */
> +
> +#define _GNU_SOURCE
> +
> +#include <signal.h>
> +#include <stdlib.h>
> +#include "tst_safe_pthread.h"
> +#include "pidfd_send_signal.h"
> +
> +#define SIGNAL  SIGUSR1
> +#define DATA	777
> +
> +static struct sigaction *sig_action;
> +static int sig_rec;
> +static siginfo_t *uinfo;
> +static int pidfd;
> +
> +static void received_signal(int sig, siginfo_t *info, void *ucontext)
> +{
> +	if (info && ucontext) {
> +		if (sig == SIGNAL && uinfo->si_value.sival_int == DATA) {
> +			tst_res(TPASS, "Received correct signal and data!");
> +			sig_rec = 1;
> +		} else {
> +			tst_res(TFAIL, "Received wrong signal and/or data!");
> +		}
> +	} else {
> +		tst_res(TFAIL, "Signal handling went wrong!");
> +	}
> +}
> +
> +static void *handle_thread(void *arg)
> +{
> +	SAFE_SIGACTION(SIGNAL, sig_action, NULL);
> +	TST_CHECKPOINT_WAKE_AND_WAIT(0);
> +	return arg;
> +}
> +
> +static void verify_pidfd_send_signal(void)
> +{
> +	pthread_t thr;
> +
> +	SAFE_PTHREAD_CREATE(&thr, NULL, handle_thread, NULL);
> +
> +	TST_CHECKPOINT_WAIT(0);
> +
> +	TEST(pidfd_send_signal(pidfd, SIGNAL, uinfo, 0));
> +	if (TST_RET != 0) {
> +		tst_res(TFAIL | TTERRNO, "pidfd_send_signal() failed");
> +		return;
> +	}
> +
> +	TST_CHECKPOINT_WAKE(0);
> +	SAFE_PTHREAD_JOIN(thr, NULL);
> +
> +	if (sig_rec) {
> +		tst_res(TPASS,
> +			"pidfd_send_signal() behaved like rt_sigqueueinfo()");
> +	}
> +}
> +
> +static void setup(void)
> +{
> +	check_syscall_support();
> +
> +	pidfd = SAFE_OPEN("/proc/self", O_DIRECTORY | O_CLOEXEC);
> +
> +	sig_action = SAFE_MALLOC(sizeof(struct sigaction));
> +
> +	memset(sig_action, 0, sizeof(*sig_action));
> +	sig_action->sa_sigaction = received_signal;
> +	sig_action->sa_flags = SA_SIGINFO;
> +
> +	uinfo = SAFE_MALLOC(sizeof(siginfo_t));
> +
> +	memset(uinfo, 0, sizeof(*uinfo));
> +	uinfo->si_signo = SIGNAL;
> +	uinfo->si_code = SI_QUEUE;
> +	uinfo->si_pid = getpid();
> +	uinfo->si_uid = getuid();
> +	uinfo->si_value.sival_int = DATA;
> +
> +	sig_rec = 0;
I guess this is not needed (as sig_rec is static.


Kind regards,
Petr

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/testing/selftests/pidfd/pidfd_test.c

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [LTP] [PATCH v2 3/4] syscalls/pidfd_send_signal02
  2019-06-04 11:47 ` [LTP] [PATCH v2 3/4] syscalls/pidfd_send_signal02 Christian Amann
@ 2019-06-12 13:54   ` Petr Vorel
  0 siblings, 0 replies; 8+ messages in thread
From: Petr Vorel @ 2019-06-12 13:54 UTC (permalink / raw)
  To: ltp

Hi Christian,

> Add test to check basic error handling of the syscall.

> Signed-off-by: Christian Amann <camann@suse.com>
Reviewed-by: Petr Vorel <pvorel@suse.cz>

...
> +#define _GNU_SOURCE
Again, if not needed, remove it.

> +
> +#include <signal.h>
> +#include "pwd.h"
#include <pwd.h">

The rest LGTM.

Kind regards,
Petr

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [LTP] [PATCH v2 4/4] syscalls/pidfd_send_signal03
  2019-06-04 11:47 ` [LTP] [PATCH v2 4/4] syscalls/pidfd_send_signal03 Christian Amann
@ 2019-06-12 14:13   ` Petr Vorel
  0 siblings, 0 replies; 8+ messages in thread
From: Petr Vorel @ 2019-06-12 14:13 UTC (permalink / raw)
  To: ltp

> Add testcase to check if the syscall will send a signal
> to a process with the same PID as the targeted process.
Reviewed-by: Petr Vorel <pvorel@suse.cz>

LGTM, thanks for your patch!

> +++ b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal03.c
...
> +#define _GNU_SOURCE
I guess this is not needed.
> +
> +#include <signal.h>
> +#include <stdio.h>
> +#include <unistd.h>
> +#include "pidfd_send_signal.h"
> +#include "tst_safe_pthread.h"
> +//#include "tst_safe_file_ops.h"
Reviewer should remove this comment.

> +
> +#define PIDTRIES	3
> +
> +static char *last_pid_file;
> +static int pidfd, new_pidfd;
> +static int old_inode, new_inode;
> +
> +static int get_inode_number(int fd)
> +{
> +	struct stat file_stat;
> +
> +	SAFE_FSTAT(fd, &file_stat);
> +	return file_stat.st_ino;
> +}
> +
> +static void verify_pidfd_send_signal(void)
> +{
> +	pid_t pid, new_pid;
> +	char pid_filename[32];
> +	char pid_str[16];
> +	int i, fail;
> +
> +	fail = 1;
> +	for (i = 0; i < PIDTRIES; i++) {
> +		pid = SAFE_FORK();
> +		if (pid == 0) {
> +			TST_CHECKPOINT_WAIT(0);
> +			return;
> +		}
> +
> +		sprintf(pid_filename, "/proc/%d", pid);
> +		pidfd = SAFE_OPEN(pid_filename, O_DIRECTORY | O_CLOEXEC);
> +		old_inode = get_inode_number(pidfd);
> +
> +		TST_CHECKPOINT_WAKE(0);
> +		tst_reap_children();
> +
> +		/* Manipulate PID for next process */
> +		sprintf(pid_str, "%d", pid - 1);
> +		SAFE_FILE_PRINTF(last_pid_file, pid_str);
> +
> +		new_pid = SAFE_FORK();
> +		if (new_pid == 0) {
> +			TST_CHECKPOINT_WAIT(0);
> +			return;
> +		}
> +
> +		if (new_pid == pid) {
> +			new_pidfd = SAFE_OPEN(pid_filename,
> +					O_DIRECTORY | O_CLOEXEC);
> +			new_inode = get_inode_number(new_pidfd);
> +			SAFE_CLOSE(new_pidfd);
> +			fail = 0;
> +			break;
> +		}
> +
> +		if (i < PIDTRIES) {
> +			tst_res(TINFO,
> +				"Failed to set correct PID, trying again...");
> +		}
> +		SAFE_CLOSE(pidfd);
> +		TST_CHECKPOINT_WAKE(0);
> +		tst_reap_children();
> +	}
> +	if (fail) {
> +		tst_brk(TBROK,
> +			"Could not set new child to same PID as the old one!");
> +	}
> +	if (old_inode == new_inode) {
> +		tst_res(TWARN,
> +			"File descriptor of new process points to the inode "
> +			"of the old process!");
I'd join this string on a single line. Also not sure if this is not TFAIL,
but probably not.

> +	}
> +
> +	TEST(pidfd_send_signal(pidfd, SIGUSR1, NULL, 0));
> +	if (TST_RET == -1 && TST_ERR == ESRCH) {
> +		tst_res(TPASS,
> +			"Did not send signal to wrong process with same PID!");
> +	} else {
> +		tst_res(TFAIL | TTERRNO,
> +			"pidf_send_signal() ended unexpectedly - return value: %ld, error",
> +			TST_RET);
> +	}
> +	TST_CHECKPOINT_WAKE(0);
> +	tst_reap_children();
> +
> +	SAFE_CLOSE(pidfd);
A bit repeating yourself, but not important.

Kind regards,
Petr

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-06-12 14:13 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-06-04 11:47 [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01 Christian Amann
2019-06-04 11:47 ` [LTP] [PATCH v2 3/4] syscalls/pidfd_send_signal02 Christian Amann
2019-06-12 13:54   ` Petr Vorel
2019-06-04 11:47 ` [LTP] [PATCH v2 4/4] syscalls/pidfd_send_signal03 Christian Amann
2019-06-12 14:13   ` Petr Vorel
2019-06-06 13:10 ` [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01 Cyril Hrubis
2019-06-11 11:12   ` Christian Amann
2019-06-12 13:41 ` Petr Vorel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox