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