blob: c01b93fac81c1c9656db899daec6d9db900decbb [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
Elliott Hughes141b9172021-04-09 17:13:09 -070017#include <gtest/gtest.h>
18
Josh Gao3de19152021-02-22 18:09:48 -080019#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 Hughes141b9172021-04-09 17:13:09 -070029#include <android-base/silent_death_test.h>
Josh Gao3de19152021-02-22 18:09:48 -080030#include <android-base/unique_fd.h>
31
Elliott Hughes95646e62023-09-21 14:11:19 -070032#include "utils.h"
33
Josh Gao3de19152021-02-22 18:09:48 -080034using android::base::unique_fd;
35using namespace std::chrono_literals;
36
Elliott Hughes141b9172021-04-09 17:13:09 -070037using pidfd_DeathTest = SilentDeathTest;
Josh Gao3de19152021-02-22 18:09:48 -080038
39TEST(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 Hughes4ae4be92023-09-22 17:15:25 -070048 if (pidfd.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_open() in this kernel";
49 ASSERT_NE(-1, pidfd.get()) << strerror(errno);
Josh Gao3de19152021-02-22 18:09:48 -080050
51 siginfo_t siginfo;
52 int rc = waitid(P_PIDFD, pidfd.get(), &siginfo, WEXITED);
53 if (rc == -1) {
Elliott Hughes95646e62023-09-21 14:11:19 -070054 ASSERT_ERRNO(EINVAL);
Josh Gao3de19152021-02-22 18:09:48 -080055 GTEST_SKIP() << "P_PIDFD not available";
56 }
57
58 ASSERT_EQ(child, siginfo.si_pid);
59#endif
60}
61
62TEST(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 Hughes4ae4be92023-09-22 17:15:25 -070067 if (self.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_open() in this kernel";
68 ASSERT_NE(-1, self.get()) << strerror(errno);
Josh Gao3de19152021-02-22 18:09:48 -080069
70 unique_fd dup(pidfd_getfd(self.get(), r.get(), 0));
Elliott Hughes4ae4be92023-09-22 17:15:25 -070071 if (dup.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_getfd() in this kernel";
72 ASSERT_NE(-1, dup.get()) << strerror(errno);
Josh Gao3de19152021-02-22 18:09:48 -080073
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
82TEST_F(pidfd_DeathTest, pidfd_send_signal) {
83#if defined(__BIONIC__)
84 unique_fd self(pidfd_open(getpid(), 0));
Elliott Hughes4ae4be92023-09-22 17:15:25 -070085 if (self.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_open() in this kernel";
86 ASSERT_NE(-1, self.get()) << strerror(errno);
Josh Gao3de19152021-02-22 18:09:48 -080087
Elliott Hughes4ae4be92023-09-22 17:15:25 -070088 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 Gao3de19152021-02-22 18:09:48 -080091
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}