1 /*
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 <gtest/gtest.h>
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 
25 #if defined(__BIONIC__)
26 #include <sys/pidfd.h>
27 #endif
28 
29 #include <android-base/silent_death_test.h>
30 #include <android-base/unique_fd.h>
31 
32 using android::base::unique_fd;
33 using namespace std::chrono_literals;
34 
35 using pidfd_DeathTest = SilentDeathTest;
36 
TEST(pidfd,pidfd_open)37 TEST(pidfd, pidfd_open) {
38 #if defined(__BIONIC__)
39   pid_t child = fork();
40   ASSERT_NE(-1, child);
41   if (child == 0) {
42     _exit(42);
43   }
44 
45   unique_fd pidfd(pidfd_open(child, 0));
46   if (pidfd.get() == -1) {
47     ASSERT_EQ(ENOSYS, errno);
48     GTEST_SKIP() << "pidfd_open not available";
49   }
50 
51   siginfo_t siginfo;
52   int rc = waitid(P_PIDFD, pidfd.get(), &siginfo, WEXITED);
53   if (rc == -1) {
54     ASSERT_EQ(EINVAL, errno) << strerror(errno);
55     GTEST_SKIP() << "P_PIDFD not available";
56   }
57 
58   ASSERT_EQ(child, siginfo.si_pid);
59 #endif
60 }
61 
TEST(pidfd,pidfd_getfd)62 TEST(pidfd, pidfd_getfd) {
63 #if defined(__BIONIC__)
64   unique_fd r, w;
65   ASSERT_TRUE(android::base::Pipe(&r, &w));
66   unique_fd self(pidfd_open(getpid(), 0));
67   if (self.get() == -1) {
68     ASSERT_EQ(ENOSYS, errno);
69     GTEST_SKIP() << "pidfd_open not available";
70   }
71 
72   unique_fd dup(pidfd_getfd(self.get(), r.get(), 0));
73   if (dup.get() == -1) {
74     ASSERT_EQ(ENOSYS, errno) << strerror(errno);
75     GTEST_SKIP() << "pidfd_getfd not available";
76   }
77 
78   ASSERT_NE(r.get(), dup.get());
79   ASSERT_EQ(3, write(w.get(), "foo", 3));
80   char buf[4];
81   ASSERT_EQ(3, read(dup.get(), buf, sizeof(buf)));
82   ASSERT_EQ(0, memcmp(buf, "foo", 3));
83 #endif
84 }
85 
TEST_F(pidfd_DeathTest,pidfd_send_signal)86 TEST_F(pidfd_DeathTest, pidfd_send_signal) {
87 #if defined(__BIONIC__)
88   unique_fd self(pidfd_open(getpid(), 0));
89   if (self.get() == -1) {
90     ASSERT_EQ(ENOSYS, errno);
91     GTEST_SKIP() << "pidfd_open not available";
92   }
93 
94   if (pidfd_send_signal(self.get(), 0, nullptr, 0) == -1) {
95     ASSERT_EQ(ENOSYS, errno);
96     GTEST_SKIP() << "pidfd_send_signal not available";
97   }
98 
99   ASSERT_EXIT(({
100                 // gtest will fork a child off for ASSERT_EXIT: `self` refers to the parent.
101                 unique_fd child(pidfd_open(getpid(), 0));
102                 pidfd_send_signal(child.get(), SIGINT, nullptr, 0);
103               }),
104               testing::KilledBySignal(SIGINT), "");
105 
106 #endif
107 }
108