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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
/*
* Check decoding of pidfd_open syscall.
*
* Copyright (c) 2019 Dmitry V. Levin <ldv@strace.io>
* Copyright (c) 2019-2024 The strace developers.
* All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "tests.h"
#include "scno.h"
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include "kernel_fcntl.h"
#include "pidns.h"
static const char *errstr;
static long
k_pidfd_open(const unsigned int pid, 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 arg2 = fill | flags;
const long rc = syscall(__NR_pidfd_open,
arg1, arg2, bad, bad, bad, bad);
errstr = sprintrc(rc);
return rc;
}
int
main(void)
{
PIDNS_TEST_INIT;
#if defined PATH_TRACING || defined PRINT_PATHS
skip_if_unavailable("/proc/self/fd/");
#endif
#ifdef PATH_TRACING
static const char path_full[] = "/dev/full";
(void) close(0);
if (open(path_full, O_WRONLY))
perror_msg_and_skip(path_full);
#endif
k_pidfd_open(0, 0);
#ifndef PATH_TRACING
pidns_print_leader();
printf("pidfd_open(0, 0) = %s\n", errstr);
#endif
k_pidfd_open(-1U, 0);
#ifndef PATH_TRACING
pidns_print_leader();
printf("pidfd_open(-1, 0) = %s\n", errstr);
#endif
k_pidfd_open(0, O_NONBLOCK);
#ifndef PATH_TRACING
pidns_print_leader();
printf("pidfd_open(0, PIDFD_NONBLOCK) = %s\n", errstr);
#endif
k_pidfd_open(-1U, O_NONBLOCK);
#ifndef PATH_TRACING
pidns_print_leader();
printf("pidfd_open(-1, PIDFD_NONBLOCK) = %s\n", errstr);
#endif
k_pidfd_open(0, O_EXCL);
#ifndef PATH_TRACING
pidns_print_leader();
printf("pidfd_open(0, PIDFD_THREAD) = %s\n", errstr);
#endif
k_pidfd_open(-1U, O_EXCL);
#ifndef PATH_TRACING
pidns_print_leader();
printf("pidfd_open(-1, PIDFD_THREAD) = %s\n", errstr);
#endif
k_pidfd_open(0, -1U);
#ifndef PATH_TRACING
pidns_print_leader();
printf("pidfd_open(0, PIDFD_NONBLOCK|PIDFD_THREAD|%#x) = %s\n",
-1U & (~(O_NONBLOCK | O_EXCL)), errstr);
#endif
const unsigned int flags = 0xfacefeed & (~(O_NONBLOCK | O_EXCL));
const int pid = getpid();
k_pidfd_open(pid, flags);
#ifndef PATH_TRACING
const char *pid_str = pidns_pid2str(PT_TGID);
pidns_print_leader();
printf("pidfd_open(%d%s, %#x /* PIDFD_??? */) = %s\n",
pid, pid_str, flags, errstr);
#endif
#ifdef PRINT_PATHS
long rc = k_pidfd_open(pid, 0);
if (rc < 0)
perror_msg_and_skip("pidfd_open");
#else
k_pidfd_open(pid, 0);
#endif
#ifndef PATH_TRACING
pidns_print_leader();
printf("pidfd_open(%d%s, 0) = "
# if defined PRINT_PIDFD
"%ld<pid:%d>\n", pid, pid_str, rc, pid
# elif defined PRINT_PATHS
"%ld<%s>\n", pid, pid_str, rc, get_fd_path(rc)
# else
"%s\n", pid, pid_str, errstr
# endif
);
#endif
pidns_print_leader();
puts("+++ exited with 0 +++");
return 0;
}
|