| Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame] | 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 |  | 
| Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 17 | #include <gtest/gtest.h> | 
|  | 18 |  | 
| Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame] | 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 |  | 
| Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 29 | #include <android-base/silent_death_test.h> | 
| Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame] | 30 | #include <android-base/unique_fd.h> | 
|  | 31 |  | 
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 32 | #include "utils.h" | 
|  | 33 |  | 
| Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame] | 34 | using android::base::unique_fd; | 
|  | 35 | using namespace std::chrono_literals; | 
|  | 36 |  | 
| Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 37 | using pidfd_DeathTest = SilentDeathTest; | 
| Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | TEST(pidfd, pidfd_open) { | 
|  | 40 | #if defined(__BIONIC__) | 
|  | 41 | pid_t child = fork(); | 
|  | 42 | ASSERT_NE(-1, child); | 
|  | 43 | if (child == 0) { | 
|  | 44 | _exit(42); | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | unique_fd pidfd(pidfd_open(child, 0)); | 
| Elliott Hughes | 4ae4be9 | 2023-09-22 17:15:25 -0700 | [diff] [blame] | 48 | if (pidfd.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_open() in this kernel"; | 
|  | 49 | ASSERT_NE(-1, pidfd.get()) << strerror(errno); | 
| Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame] | 50 |  | 
|  | 51 | siginfo_t siginfo; | 
|  | 52 | int rc = waitid(P_PIDFD, pidfd.get(), &siginfo, WEXITED); | 
|  | 53 | if (rc == -1) { | 
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 54 | ASSERT_ERRNO(EINVAL); | 
| Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame] | 55 | GTEST_SKIP() << "P_PIDFD not available"; | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | ASSERT_EQ(child, siginfo.si_pid); | 
|  | 59 | #endif | 
|  | 60 | } | 
|  | 61 |  | 
|  | 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)); | 
| Elliott Hughes | 4ae4be9 | 2023-09-22 17:15:25 -0700 | [diff] [blame] | 67 | if (self.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_open() in this kernel"; | 
|  | 68 | ASSERT_NE(-1, self.get()) << strerror(errno); | 
| Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame] | 69 |  | 
|  | 70 | unique_fd dup(pidfd_getfd(self.get(), r.get(), 0)); | 
| Elliott Hughes | 4ae4be9 | 2023-09-22 17:15:25 -0700 | [diff] [blame] | 71 | if (dup.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_getfd() in this kernel"; | 
|  | 72 | ASSERT_NE(-1, dup.get()) << strerror(errno); | 
| Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame] | 73 |  | 
|  | 74 | ASSERT_NE(r.get(), dup.get()); | 
|  | 75 | ASSERT_EQ(3, write(w.get(), "foo", 3)); | 
|  | 76 | char buf[4]; | 
|  | 77 | ASSERT_EQ(3, read(dup.get(), buf, sizeof(buf))); | 
|  | 78 | ASSERT_EQ(0, memcmp(buf, "foo", 3)); | 
|  | 79 | #endif | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | TEST_F(pidfd_DeathTest, pidfd_send_signal) { | 
|  | 83 | #if defined(__BIONIC__) | 
|  | 84 | unique_fd self(pidfd_open(getpid(), 0)); | 
| Elliott Hughes | 4ae4be9 | 2023-09-22 17:15:25 -0700 | [diff] [blame] | 85 | if (self.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_open() in this kernel"; | 
|  | 86 | ASSERT_NE(-1, self.get()) << strerror(errno); | 
| Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame] | 87 |  | 
| Elliott Hughes | 4ae4be9 | 2023-09-22 17:15:25 -0700 | [diff] [blame] | 88 | int rc = pidfd_send_signal(self.get(), 0, nullptr, 0); | 
|  | 89 | if (rc == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_send_signal() in this kernel"; | 
|  | 90 | ASSERT_EQ(0, rc) << strerror(errno); | 
| Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame] | 91 |  | 
|  | 92 | ASSERT_EXIT(({ | 
|  | 93 | // gtest will fork a child off for ASSERT_EXIT: `self` refers to the parent. | 
|  | 94 | unique_fd child(pidfd_open(getpid(), 0)); | 
|  | 95 | pidfd_send_signal(child.get(), SIGINT, nullptr, 0); | 
|  | 96 | }), | 
|  | 97 | testing::KilledBySignal(SIGINT), ""); | 
|  | 98 |  | 
|  | 99 | #endif | 
|  | 100 | } |