* [LTP] [PATCH V3 1/2] Add Syscall numbers for pidfd_open @ 2020-01-24 9:48 Viresh Kumar 2020-01-24 9:48 ` [LTP] [PATCH V3 2/2] syscalls/pidfd_open Viresh Kumar 0 siblings, 1 reply; 3+ messages in thread From: Viresh Kumar @ 2020-01-24 9:48 UTC (permalink / raw) To: ltp Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org> --- V1->V3: - No changes include/lapi/syscalls/aarch64.in | 1 + include/lapi/syscalls/arm.in | 1 + include/lapi/syscalls/hppa.in | 1 + include/lapi/syscalls/i386.in | 1 + include/lapi/syscalls/ia64.in | 1 + include/lapi/syscalls/powerpc.in | 1 + include/lapi/syscalls/powerpc64.in | 1 + include/lapi/syscalls/s390.in | 1 + include/lapi/syscalls/s390x.in | 1 + include/lapi/syscalls/sh.in | 1 + include/lapi/syscalls/sparc.in | 1 + include/lapi/syscalls/sparc64.in | 1 + include/lapi/syscalls/x86_64.in | 1 + 13 files changed, 13 insertions(+) diff --git a/include/lapi/syscalls/aarch64.in b/include/lapi/syscalls/aarch64.in index 0e00641bcf4d..19f3aa4475bd 100644 --- a/include/lapi/syscalls/aarch64.in +++ b/include/lapi/syscalls/aarch64.in @@ -270,4 +270,5 @@ pkey_mprotect 288 pkey_alloc 289 pkey_free 290 pidfd_send_signal 424 +pidfd_open 434 _sysctl 1078 diff --git a/include/lapi/syscalls/arm.in b/include/lapi/syscalls/arm.in index a6a594c2e85d..979a0183d4f5 100644 --- a/include/lapi/syscalls/arm.in +++ b/include/lapi/syscalls/arm.in @@ -355,3 +355,4 @@ pkey_alloc (__NR_SYSCALL_BASE+395) pkey_free (__NR_SYSCALL_BASE+396) statx (__NR_SYSCALL_BASE+397) pidfd_send_signal (__NR_SYSCALL_BASE+424) +pidfd_open (__NR_SYSCALL_BASE+434) diff --git a/include/lapi/syscalls/hppa.in b/include/lapi/syscalls/hppa.in index d92f33f40a50..b0807ca6b69f 100644 --- a/include/lapi/syscalls/hppa.in +++ b/include/lapi/syscalls/hppa.in @@ -27,3 +27,4 @@ copy_file_range 346 preadv2 347 pwritev2 348 pidfd_send_signal 424 +pidfd_open 434 diff --git a/include/lapi/syscalls/i386.in b/include/lapi/syscalls/i386.in index bad6e409fbdc..f19e24bd63c9 100644 --- a/include/lapi/syscalls/i386.in +++ b/include/lapi/syscalls/i386.in @@ -354,3 +354,4 @@ pkey_alloc 381 pkey_free 382 statx 383 pidfd_send_signal 424 +pidfd_open 434 diff --git a/include/lapi/syscalls/ia64.in b/include/lapi/syscalls/ia64.in index 0e6ba54a8f76..92cc881d933b 100644 --- a/include/lapi/syscalls/ia64.in +++ b/include/lapi/syscalls/ia64.in @@ -310,3 +310,4 @@ pkey_mprotect 1354 pkey_alloc 1355 pkey_free 1356 pidfd_send_signal 1448 +pidfd_open 1458 diff --git a/include/lapi/syscalls/powerpc.in b/include/lapi/syscalls/powerpc.in index 91467f326ed2..f83f7d233e61 100644 --- a/include/lapi/syscalls/powerpc.in +++ b/include/lapi/syscalls/powerpc.in @@ -357,6 +357,7 @@ preadv2 380 pwritev2 381 statx 383 pidfd_send_signal 424 +pidfd_open 434 pkey_mprotect 386 pkey_alloc 384 pkey_free 385 diff --git a/include/lapi/syscalls/powerpc64.in b/include/lapi/syscalls/powerpc64.in index 91467f326ed2..f83f7d233e61 100644 --- a/include/lapi/syscalls/powerpc64.in +++ b/include/lapi/syscalls/powerpc64.in @@ -357,6 +357,7 @@ preadv2 380 pwritev2 381 statx 383 pidfd_send_signal 424 +pidfd_open 434 pkey_mprotect 386 pkey_alloc 384 pkey_free 385 diff --git a/include/lapi/syscalls/s390.in b/include/lapi/syscalls/s390.in index dcf0a71482ef..f99aceb71a81 100644 --- a/include/lapi/syscalls/s390.in +++ b/include/lapi/syscalls/s390.in @@ -344,3 +344,4 @@ pkey_mprotect 384 pkey_alloc 385 pkey_free 386 pidfd_send_signal 424 +pidfd_open 434 diff --git a/include/lapi/syscalls/s390x.in b/include/lapi/syscalls/s390x.in index 2dce00f900a2..9666095b6bea 100644 --- a/include/lapi/syscalls/s390x.in +++ b/include/lapi/syscalls/s390x.in @@ -342,3 +342,4 @@ pkey_mprotect 384 pkey_alloc 385 pkey_free 386 pidfd_send_signal 424 +pidfd_open 434 diff --git a/include/lapi/syscalls/sh.in b/include/lapi/syscalls/sh.in index 996ebc956a9a..73549b46e6dd 100644 --- a/include/lapi/syscalls/sh.in +++ b/include/lapi/syscalls/sh.in @@ -371,3 +371,4 @@ copy_file_range 391 preadv2 392 pwritev2 393 pidfd_send_signal 424 +pidfd_open 434 diff --git a/include/lapi/syscalls/sparc.in b/include/lapi/syscalls/sparc.in index 65902e00aa23..6c610ba67308 100644 --- a/include/lapi/syscalls/sparc.in +++ b/include/lapi/syscalls/sparc.in @@ -349,3 +349,4 @@ pkey_mprotect 362 pkey_alloc 363 pkey_free 364 pidfd_send_signal 424 +pidfd_open 434 diff --git a/include/lapi/syscalls/sparc64.in b/include/lapi/syscalls/sparc64.in index d303622b46df..d20e87926913 100644 --- a/include/lapi/syscalls/sparc64.in +++ b/include/lapi/syscalls/sparc64.in @@ -324,3 +324,4 @@ pkey_mprotect 362 pkey_alloc 363 pkey_free 364 pidfd_send_signal 424 +pidfd_open 434 diff --git a/include/lapi/syscalls/x86_64.in b/include/lapi/syscalls/x86_64.in index 3e9e19ae6af0..cba0080dd27a 100644 --- a/include/lapi/syscalls/x86_64.in +++ b/include/lapi/syscalls/x86_64.in @@ -321,3 +321,4 @@ pkey_alloc 330 pkey_free 331 statx 332 pidfd_send_signal 424 +pidfd_open 434 -- 2.21.0.rc0.269.g1a574e7a288b ^ permalink raw reply related [flat|nested] 3+ messages in thread
* [LTP] [PATCH V3 2/2] syscalls/pidfd_open 2020-01-24 9:48 [LTP] [PATCH V3 1/2] Add Syscall numbers for pidfd_open Viresh Kumar @ 2020-01-24 9:48 ` Viresh Kumar 2020-01-24 16:49 ` Cyril Hrubis 0 siblings, 1 reply; 3+ messages in thread From: Viresh Kumar @ 2020-01-24 9:48 UTC (permalink / raw) To: ltp Add tests to check working of pidfd_open() syscall. Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org> --- V2->V3: - Add pidfd_open03 to syscalls and .gitignore files. - Use TEST, tst_get_unused_pid, etc. - Do the failure testing with help of array and .tcnt. - Improved print messages. - Removed useless comments. - Few more minor changes. V1->V2: - New test to test failures - Use SAFE_FORK, checkpoints, etc - Print error numbers as well. - Improved print messages. configure.ac | 1 + include/lapi/pidfd_open.h | 21 +++++++ runtest/syscalls | 4 ++ .../kernel/syscalls/pidfd_open/.gitignore | 3 + testcases/kernel/syscalls/pidfd_open/Makefile | 6 ++ .../kernel/syscalls/pidfd_open/pidfd_open01.c | 34 ++++++++++ .../kernel/syscalls/pidfd_open/pidfd_open02.c | 63 +++++++++++++++++++ .../kernel/syscalls/pidfd_open/pidfd_open03.c | 63 +++++++++++++++++++ 8 files changed, 195 insertions(+) create mode 100644 include/lapi/pidfd_open.h create mode 100644 testcases/kernel/syscalls/pidfd_open/.gitignore create mode 100644 testcases/kernel/syscalls/pidfd_open/Makefile create mode 100644 testcases/kernel/syscalls/pidfd_open/pidfd_open01.c create mode 100644 testcases/kernel/syscalls/pidfd_open/pidfd_open02.c create mode 100644 testcases/kernel/syscalls/pidfd_open/pidfd_open03.c diff --git a/configure.ac b/configure.ac index 50d14967d3c6..1bf0911d88ad 100644 --- a/configure.ac +++ b/configure.ac @@ -79,6 +79,7 @@ AC_CHECK_FUNCS([ \ mknodat \ name_to_handle_at \ openat \ + pidfd_open \ pidfd_send_signal \ pkey_mprotect \ preadv \ diff --git a/include/lapi/pidfd_open.h b/include/lapi/pidfd_open.h new file mode 100644 index 000000000000..ced163be83bf --- /dev/null +++ b/include/lapi/pidfd_open.h @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2020 Linaro Limited. All rights reserved. + * Author: Viresh Kumar <viresh.kumar@linaro.org> + */ + +#ifndef PIDFD_OPEN_H +#define PIDFD_OPEN_H + +#include "config.h" +#include <sys/types.h> +#include "lapi/syscalls.h" + +#ifndef HAVE_PIDFD_OPEN +int pidfd_open(pid_t pid, unsigned int flags) +{ + return tst_syscall(__NR_pidfd_open, pid, flags); +} +#endif + +#endif /* PIDFD_OPEN_H */ diff --git a/runtest/syscalls b/runtest/syscalls index f58fefe17c34..f083758bc637 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -848,6 +848,10 @@ pause03 pause03 personality01 personality01 personality02 personality02 +pidfd_open01 pidfd_open01 +pidfd_open02 pidfd_open02 +pidfd_open03 pidfd_open03 + pidfd_send_signal01 pidfd_send_signal01 pidfd_send_signal02 pidfd_send_signal02 pidfd_send_signal03 pidfd_send_signal03 diff --git a/testcases/kernel/syscalls/pidfd_open/.gitignore b/testcases/kernel/syscalls/pidfd_open/.gitignore new file mode 100644 index 000000000000..e0b8900c1c33 --- /dev/null +++ b/testcases/kernel/syscalls/pidfd_open/.gitignore @@ -0,0 +1,3 @@ +pidfd_open01 +pidfd_open02 +pidfd_open03 diff --git a/testcases/kernel/syscalls/pidfd_open/Makefile b/testcases/kernel/syscalls/pidfd_open/Makefile new file mode 100644 index 000000000000..5ea7d67db123 --- /dev/null +++ b/testcases/kernel/syscalls/pidfd_open/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk +include $(top_srcdir)/include/mk/generic_leaf_target.mk diff --git a/testcases/kernel/syscalls/pidfd_open/pidfd_open01.c b/testcases/kernel/syscalls/pidfd_open/pidfd_open01.c new file mode 100644 index 000000000000..e40ea4f2eff5 --- /dev/null +++ b/testcases/kernel/syscalls/pidfd_open/pidfd_open01.c @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org> + * + * Description: + * Basic pidfd_open() test, fetches the PID of the current process and tries to + * get its file descriptor. + */ +#include <sys/types.h> +#include <sys/syscall.h> + +#include "tst_test.h" +#include "lapi/pidfd_open.h" +#include "lapi/syscalls.h" + +static void run(void) +{ + int fd; + + TEST(pidfd_open(getpid(), 0)); + + fd = TST_RET; + if (fd == -1) + tst_brk(TFAIL | TTERRNO, "pidfd_open(getpid(), 0) failed"); + + SAFE_CLOSE(fd); + + tst_res(TPASS, "pidfd_open(getpid(), 0) passed"); +} + +static struct tst_test test = { + .min_kver = "5.3", + .test_all = run, +}; diff --git a/testcases/kernel/syscalls/pidfd_open/pidfd_open02.c b/testcases/kernel/syscalls/pidfd_open/pidfd_open02.c new file mode 100644 index 000000000000..5e5431335731 --- /dev/null +++ b/testcases/kernel/syscalls/pidfd_open/pidfd_open02.c @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org> + * + * Description: + * Basic pidfd_open() test to test invalid arguments. + */ +#include <sys/types.h> +#include <sys/syscall.h> + +#include "tst_test.h" +#include "lapi/pidfd_open.h" +#include "lapi/syscalls.h" + +#define INVALID_PID -1 +#define MY_PID 0 +#define EXPIRED_PID 1 + +static struct tcase { + char *name; + int pid; + int flags; + int exp_errno; +} tcases[] = { + {"test-expired-pid", EXPIRED_PID, 0, ESRCH}, + {"test-invalid-pid", INVALID_PID, 0, EINVAL}, + {"test-invalid-flags", MY_PID, 1, EINVAL}, +}; + +static void run(unsigned int n) +{ + struct tcase *tc = &tcases[n]; + pid_t pid; + + if (tc->pid == EXPIRED_PID) + pid = tst_get_unused_pid(); + else if (tc->pid == MY_PID) + pid = getpid(); + else + pid = tc->pid; + + TEST(pidfd_open(pid, tc->flags)); + + if (TST_RET != -1) { + SAFE_CLOSE(TST_RET); + tst_brk(TFAIL, "%s: pidfd_open succeeded unexpectedly (index: %d)", + tc->name, n); + } + + if (tc->exp_errno != TST_ERR) { + tst_brk(TFAIL | TTERRNO, "%s: pidfd_open() should fail with %s", + tc->name, tst_strerrno(tc->exp_errno)); + } + + tst_res(TPASS | TTERRNO, "%s: pidfd_open() failed as expected", + tc->name); +} + +static struct tst_test test = { + .min_kver = "5.3", + .tcnt = ARRAY_SIZE(tcases), + .test = run, +}; diff --git a/testcases/kernel/syscalls/pidfd_open/pidfd_open03.c b/testcases/kernel/syscalls/pidfd_open/pidfd_open03.c new file mode 100644 index 000000000000..efa14dc52d7a --- /dev/null +++ b/testcases/kernel/syscalls/pidfd_open/pidfd_open03.c @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org> + * + * Description: + * This program opens the PID file descriptor of the child process created with + * fork(). It then uses poll to monitor the file descriptor for process exit, as + * indicated by an EPOLLIN event. + */ +#include <poll.h> +#include <sys/types.h> +#include <sys/syscall.h> +#include <stdio.h> +#include <stdlib.h> + +#include "tst_test.h" +#include "lapi/pidfd_open.h" +#include "lapi/syscalls.h" + +static void run(void) +{ + struct pollfd pollfd; + int fd, ready; + pid_t pid; + + pid = SAFE_FORK(); + + if (!pid) { + TST_CHECKPOINT_WAIT(0); + exit(EXIT_SUCCESS); + } else { + TEST(pidfd_open(pid, 0)); + + fd = TST_RET; + if (fd == -1) + tst_brk(TFAIL | TTERRNO, "pidfd_open() failed"); + + TST_CHECKPOINT_WAKE(0); + + pollfd.fd = fd; + pollfd.events = POLLIN; + + ready = poll(&pollfd, 1, -1); + + SAFE_CLOSE(fd); + SAFE_WAITPID(pid, NULL, 0); + + if (ready == -1) + tst_brk(TBROK | TERRNO, "poll() failed"); + + if (ready != 1) + tst_brk(TBROK, "poll() should return 1"); + + tst_res(TPASS, "pidfd_open() passed"); + } +} + +static struct tst_test test = { + .min_kver = "5.3", + .test_all = run, + .forks_child = 1, + .needs_checkpoints = 1, +}; -- 2.21.0.rc0.269.g1a574e7a288b ^ permalink raw reply related [flat|nested] 3+ messages in thread
* [LTP] [PATCH V3 2/2] syscalls/pidfd_open 2020-01-24 9:48 ` [LTP] [PATCH V3 2/2] syscalls/pidfd_open Viresh Kumar @ 2020-01-24 16:49 ` Cyril Hrubis 0 siblings, 0 replies; 3+ messages in thread From: Cyril Hrubis @ 2020-01-24 16:49 UTC (permalink / raw) To: ltp Hi! > --- /dev/null > +++ b/testcases/kernel/syscalls/pidfd_open/pidfd_open01.c > @@ -0,0 +1,34 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org> > + * > + * Description: > + * Basic pidfd_open() test, fetches the PID of the current process and tries to > + * get its file descriptor. > + */ > +#include <sys/types.h> > +#include <sys/syscall.h> > + > +#include "tst_test.h" > +#include "lapi/pidfd_open.h" > +#include "lapi/syscalls.h" All the sys/ headers and the lapi/syscalls.h are included from the lapi/pidfd_open.h there is no need to include them again here. > +static void run(void) > +{ > + int fd; > + > + TEST(pidfd_open(getpid(), 0)); > + > + fd = TST_RET; > + if (fd == -1) > + tst_brk(TFAIL | TTERRNO, "pidfd_open(getpid(), 0) failed"); > + > + SAFE_CLOSE(fd); Why can't we use the TST_RET directly here? I.e. if (TST_RET == -1) etc. > + tst_res(TPASS, "pidfd_open(getpid(), 0) passed"); > +} > + > +static struct tst_test test = { > + .min_kver = "5.3", > + .test_all = run, > +}; > diff --git a/testcases/kernel/syscalls/pidfd_open/pidfd_open02.c b/testcases/kernel/syscalls/pidfd_open/pidfd_open02.c > new file mode 100644 > index 000000000000..5e5431335731 > --- /dev/null > +++ b/testcases/kernel/syscalls/pidfd_open/pidfd_open02.c > @@ -0,0 +1,63 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org> > + * > + * Description: > + * Basic pidfd_open() test to test invalid arguments. > + */ > +#include <sys/types.h> > +#include <sys/syscall.h> > + > +#include "tst_test.h" > +#include "lapi/pidfd_open.h" > +#include "lapi/syscalls.h" Here as well. > +#define INVALID_PID -1 > +#define MY_PID 0 > +#define EXPIRED_PID 1 > + > +static struct tcase { > + char *name; > + int pid; This is usually handled by setting a pointer to global variable that is initialized in the test setup, e.g.: static int expired_pid; ... {"expired pid", &expired_pid, 0, ESRCH} ... static void run(void) { ... TEST(pidfd_open(*tc->pid, tc->flags)); ... } static void setup(void) { expired_pid = tst_get_unused_pid(); } > + int flags; > + int exp_errno; > +} tcases[] = { > + {"test-expired-pid", EXPIRED_PID, 0, ESRCH}, > + {"test-invalid-pid", INVALID_PID, 0, EINVAL}, > + {"test-invalid-flags", MY_PID, 1, EINVAL}, > +}; > + > +static void run(unsigned int n) > +{ > + struct tcase *tc = &tcases[n]; > + pid_t pid; > + > + if (tc->pid == EXPIRED_PID) > + pid = tst_get_unused_pid(); > + else if (tc->pid == MY_PID) > + pid = getpid(); > + else > + pid = tc->pid; > + > + TEST(pidfd_open(pid, tc->flags)); > + > + if (TST_RET != -1) { > + SAFE_CLOSE(TST_RET); > + tst_brk(TFAIL, "%s: pidfd_open succeeded unexpectedly (index: %d)", > + tc->name, n); > + } > + > + if (tc->exp_errno != TST_ERR) { > + tst_brk(TFAIL | TTERRNO, "%s: pidfd_open() should fail with %s", > + tc->name, tst_strerrno(tc->exp_errno)); > + } > + > + tst_res(TPASS | TTERRNO, "%s: pidfd_open() failed as expected", > + tc->name); > +} > + > +static struct tst_test test = { > + .min_kver = "5.3", > + .tcnt = ARRAY_SIZE(tcases), > + .test = run, > +}; > diff --git a/testcases/kernel/syscalls/pidfd_open/pidfd_open03.c b/testcases/kernel/syscalls/pidfd_open/pidfd_open03.c > new file mode 100644 > index 000000000000..efa14dc52d7a > --- /dev/null > +++ b/testcases/kernel/syscalls/pidfd_open/pidfd_open03.c > @@ -0,0 +1,63 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org> > + * > + * Description: > + * This program opens the PID file descriptor of the child process created with > + * fork(). It then uses poll to monitor the file descriptor for process exit, as > + * indicated by an EPOLLIN event. > + */ > +#include <poll.h> > +#include <sys/types.h> > +#include <sys/syscall.h> > +#include <stdio.h> > +#include <stdlib.h> > + > +#include "tst_test.h" > +#include "lapi/pidfd_open.h" > +#include "lapi/syscalls.h" > + > +static void run(void) > +{ > + struct pollfd pollfd; > + int fd, ready; > + pid_t pid; > + > + pid = SAFE_FORK(); > + > + if (!pid) { > + TST_CHECKPOINT_WAIT(0); > + exit(EXIT_SUCCESS); > + } else { > + TEST(pidfd_open(pid, 0)); > + > + fd = TST_RET; > + if (fd == -1) > + tst_brk(TFAIL | TTERRNO, "pidfd_open() failed"); > + > + TST_CHECKPOINT_WAKE(0); > + > + pollfd.fd = fd; > + pollfd.events = POLLIN; > + > + ready = poll(&pollfd, 1, -1); > + > + SAFE_CLOSE(fd); > + SAFE_WAITPID(pid, NULL, 0); > + > + if (ready == -1) > + tst_brk(TBROK | TERRNO, "poll() failed"); > + > + if (ready != 1) > + tst_brk(TBROK, "poll() should return 1"); Shouldn't this rather be TFAIL? Something like: if (ready != 1) tst_res(TFAIL, "poll() returned %i", ready); else tst_res(TPASS, ...); > + tst_res(TPASS, "pidfd_open() passed"); Just a minor thing, there is no need for the else branch since the child calls exit(). > + } > +} > + > +static struct tst_test test = { > + .min_kver = "5.3", > + .test_all = run, > + .forks_child = 1, > + .needs_checkpoints = 1, > +}; > -- > 2.21.0.rc0.269.g1a574e7a288b > -- Cyril Hrubis chrubis@suse.cz ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-01-24 16:49 UTC | newest] Thread overview: 3+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2020-01-24 9:48 [LTP] [PATCH V3 1/2] Add Syscall numbers for pidfd_open Viresh Kumar 2020-01-24 9:48 ` [LTP] [PATCH V3 2/2] syscalls/pidfd_open Viresh Kumar 2020-01-24 16:49 ` Cyril Hrubis
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox