1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
/*
* Check decoding of pidfd_getfd syscall.
*
* Copyright (c) 2019 Dmitry V. Levin <ldv@strace.io>
* Copyright (c) 2020-2021 The strace developers.
* All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "tests.h"
#include "scno.h"
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#ifndef PIDFD_PATH
# define PIDFD_PATH ""
#endif
#ifndef FD0_PATH
# define FD0_PATH ""
#endif
#ifndef PRINT_PIDFD
# define PRINT_PIDFD 0
#endif
#ifndef SKIP_IF_PROC_IS_UNAVAILABLE
# define SKIP_IF_PROC_IS_UNAVAILABLE
#endif
static const char *errstr;
static long
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 | pidfd;
const kernel_ulong_t arg2 = fill | fd;
const kernel_ulong_t arg3 = fill | flags;
const long rc = syscall(__NR_pidfd_getfd,
arg1, arg2, arg3, bad, bad, bad);
errstr = sprintrc(rc);
return rc;
}
int
main(void)
{
SKIP_IF_PROC_IS_UNAVAILABLE;
long rc;
rc = k_pidfd_getfd(-1U, -1U, 0);
printf("pidfd_getfd(-1, -1, 0) = %s\n", errstr);
rc = k_pidfd_getfd(0, 0, 0xbadc0ded);
printf("pidfd_getfd(0" FD0_PATH ", 0, 0xbadc0ded) = %s\n", errstr);
int child_wait_fds[2];
if (pipe(child_wait_fds))
perror_msg_and_fail("pipe");
int dupfd = dup(0);
int pid = fork();
if (pid == 0) {
close(0);
close(child_wait_fds[1]);
if (read(child_wait_fds[0], &child_wait_fds[1], sizeof(int)))
_exit(2);
_exit(0);
}
close(dupfd);
int pidfd = syscall(__NR_pidfd_open, pid, 0);
#if PRINT_PIDFD
char pidfd_str[sizeof("<pid:>") + 3 * sizeof(int)];
snprintf(pidfd_str, sizeof(pidfd_str), "<pid:%d>", pid);
#else
const char *pidfd_str = PIDFD_PATH;
#endif
rc = k_pidfd_getfd(pidfd, dupfd, 0);
printf("pidfd_getfd(%d%s, %d%s, 0) = %s%s\n",
pidfd, pidfd >= 0 ? pidfd_str : "",
dupfd, pidfd >= 0 ? FD0_PATH : "",
errstr, rc >= 0 ? FD0_PATH : "");
puts("+++ exited with 0 +++");
close(child_wait_fds[1]);
int status;
assert(wait(&status) == pid);
assert(status == 0);
return 0;
}
|