blob: 7c691ef1699284cdbc540c9944446ef1939bc31a [file] [log] [blame]
Josh Gao3de19152021-02-22 18:09:48 -08001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
19#include <signal.h>
20#include <sys/types.h>
21#include <unistd.h>
22
23#if defined(__BIONIC__)
24#include <sys/pidfd.h>
25#endif
26
27#include <android-base/unique_fd.h>
28
29#include <gtest/gtest.h>
30
31#include "BionicDeathTest.h"
32
33using android::base::unique_fd;
34using namespace std::chrono_literals;
35
36using pidfd_DeathTest = BionicDeathTest;
37
38TEST(pidfd, pidfd_open) {
39#if defined(__BIONIC__)
40 pid_t child = fork();
41 ASSERT_NE(-1, child);
42 if (child == 0) {
43 _exit(42);
44 }
45
46 unique_fd pidfd(pidfd_open(child, 0));
47 if (pidfd.get() == -1) {
48 ASSERT_EQ(ENOSYS, errno);
49 GTEST_SKIP() << "pidfd_open not available";
50 }
51
52 siginfo_t siginfo;
53 int rc = waitid(P_PIDFD, pidfd.get(), &siginfo, WEXITED);
54 if (rc == -1) {
55 ASSERT_EQ(EINVAL, errno) << strerror(errno);
56 GTEST_SKIP() << "P_PIDFD not available";
57 }
58
59 ASSERT_EQ(child, siginfo.si_pid);
60#endif
61}
62
63TEST(pidfd, pidfd_getfd) {
64#if defined(__BIONIC__)
65 unique_fd r, w;
66 ASSERT_TRUE(android::base::Pipe(&r, &w));
67 unique_fd self(pidfd_open(getpid(), 0));
68 if (self.get() == -1) {
69 ASSERT_EQ(ENOSYS, errno);
70 GTEST_SKIP() << "pidfd_open not available";
71 }
72
73 unique_fd dup(pidfd_getfd(self.get(), r.get(), 0));
74 if (dup.get() == -1) {
75 ASSERT_EQ(ENOSYS, errno) << strerror(errno);
76 GTEST_SKIP() << "pidfd_getfd not available";
77 }
78
79 ASSERT_NE(r.get(), dup.get());
80 ASSERT_EQ(3, write(w.get(), "foo", 3));
81 char buf[4];
82 ASSERT_EQ(3, read(dup.get(), buf, sizeof(buf)));
83 ASSERT_EQ(0, memcmp(buf, "foo", 3));
84#endif
85}
86
87TEST_F(pidfd_DeathTest, pidfd_send_signal) {
88#if defined(__BIONIC__)
89 unique_fd self(pidfd_open(getpid(), 0));
90 if (self.get() == -1) {
91 ASSERT_EQ(ENOSYS, errno);
92 GTEST_SKIP() << "pidfd_open not available";
93 }
94
95 if (pidfd_send_signal(self.get(), 0, nullptr, 0) == -1) {
96 ASSERT_EQ(ENOSYS, errno);
97 GTEST_SKIP() << "pidfd_send_signal not available";
98 }
99
100 ASSERT_EXIT(({
101 // gtest will fork a child off for ASSERT_EXIT: `self` refers to the parent.
102 unique_fd child(pidfd_open(getpid(), 0));
103 pidfd_send_signal(child.get(), SIGINT, nullptr, 0);
104 }),
105 testing::KilledBySignal(SIGINT), "");
106
107#endif
108}