[PATCH v2 6/6] tests/pidfd_getfd.c: test decoding of targetfd

Dmitry V. Levin ldv at altlinux.org
Sat May 23 22:11:20 UTC 2020


On Fri, May 15, 2020 at 09:18:40PM +0200, Ákos Uzonyi wrote:
> * tests/pidfd_getfd.c (k_pidfd_getfd): rename argument: pid -> pidfd
> (main): test decoding of targetfd
> 
> Signed-off-by: Uzonyi Ákos <uzonyi.akos at gmail.com>
> ---
>  tests/pidfd_getfd.c | 20 +++++++++++++++-----
>  1 file changed, 15 insertions(+), 5 deletions(-)
> 
> diff --git a/tests/pidfd_getfd.c b/tests/pidfd_getfd.c
> index 791e58e3..1218e5e8 100644
> --- a/tests/pidfd_getfd.c
> +++ b/tests/pidfd_getfd.c
> @@ -13,7 +13,9 @@
>  
>  #ifdef __NR_pidfd_getfd
>  
> +# include <signal.h>
>  # include <stdio.h>
> +# include <stdlib.h>
>  # include <unistd.h>
>  
>  # ifndef PIDFD_PATH
> @@ -29,12 +31,12 @@
>  static const char *errstr;
>  
>  static long
> -k_pidfd_getfd(const unsigned int pid, const unsigned int fd,
> +k_pidfd_getfd(const unsigned int pidfd, const unsigned int fd,
>  	      const unsigned int flags)
>  {
>  	const kernel_ulong_t fill = (kernel_ulong_t) 0xdefaced00000000ULL;
>  	const kernel_ulong_t bad = (kernel_ulong_t) 0xbadc0dedbadc0dedULL;
> -	const kernel_ulong_t arg1 = fill | pid;
> +	const kernel_ulong_t arg1 = fill | pidfd;
>  	const kernel_ulong_t arg2 = fill | fd;
>  	const kernel_ulong_t arg3 = fill | flags;
>  	const long rc = syscall(__NR_pidfd_getfd,
> @@ -54,7 +56,14 @@ main(void)
>  	rc = k_pidfd_getfd(0, 0, 0xbadc0ded);
>  	printf("pidfd_getfd(0" FD0_PATH ", 0, 0xbadc0ded) = %s\n", errstr);
>  
> -	int pid = getpid();
> +	int pid = fork();
> +	if (pid == 0) {
> +		dup2(0, 42);
> +		close(0);
> +		sleep(1);
> +		exit(0);
> +	}

Why 42?
Could you just "int copy = dup(0)" in the parent, close(0) in the child,
and close(copy) in the parent?

Why sleep(1)?  It's not the most reliable method of synchronization.
In tests we usually create a pipe, the child closes its write end and
reads from its read end.  When the parent is no more, the child reads EOF.


-- 
ldv


More information about the Strace-devel mailing list