blob: e2e2046feb27be969c2f4a5259832cc8fd980032 [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));
48 if (pidfd.get() == -1) {
Elliott Hughes95646e62023-09-21 14:11:19 -070049 ASSERT_ERRNO(ENOSYS);
Josh Gao3de19152021-02-22 18:09:48 -080050 GTEST_SKIP() << "pidfd_open not available";
51 }
52
53 siginfo_t siginfo;
54 int rc = waitid(P_PIDFD, pidfd.get(), &siginfo, WEXITED);
55 if (rc == -1) {
Elliott Hughes95646e62023-09-21 14:11:19 -070056 ASSERT_ERRNO(EINVAL);
Josh Gao3de19152021-02-22 18:09:48 -080057 GTEST_SKIP() << "P_PIDFD not available";
58 }
59
60 ASSERT_EQ(child, siginfo.si_pid);
61#endif
62}
63
64TEST(pidfd, pidfd_getfd) {
65#if defined(__BIONIC__)
66 unique_fd r, w;
67 ASSERT_TRUE(android::base::Pipe(&r, &w));
68 unique_fd self(pidfd_open(getpid(), 0));
69 if (self.get() == -1) {
Elliott Hughes95646e62023-09-21 14:11:19 -070070 ASSERT_ERRNO(ENOSYS);
Josh Gao3de19152021-02-22 18:09:48 -080071 GTEST_SKIP() << "pidfd_open not available";
72 }
73
74 unique_fd dup(pidfd_getfd(self.get(), r.get(), 0));
75 if (dup.get() == -1) {
Elliott Hughes95646e62023-09-21 14:11:19 -070076 ASSERT_ERRNO(ENOSYS);
Josh Gao3de19152021-02-22 18:09:48 -080077 GTEST_SKIP() << "pidfd_getfd not available";
78 }
79
80 ASSERT_NE(r.get(), dup.get());
81 ASSERT_EQ(3, write(w.get(), "foo", 3));
82 char buf[4];
83 ASSERT_EQ(3, read(dup.get(), buf, sizeof(buf)));
84 ASSERT_EQ(0, memcmp(buf, "foo", 3));
85#endif
86}
87
88TEST_F(pidfd_DeathTest, pidfd_send_signal) {
89#if defined(__BIONIC__)
90 unique_fd self(pidfd_open(getpid(), 0));
91 if (self.get() == -1) {
Elliott Hughes95646e62023-09-21 14:11:19 -070092 ASSERT_ERRNO(ENOSYS);
Josh Gao3de19152021-02-22 18:09:48 -080093 GTEST_SKIP() << "pidfd_open not available";
94 }
95
96 if (pidfd_send_signal(self.get(), 0, nullptr, 0) == -1) {
Elliott Hughes95646e62023-09-21 14:11:19 -070097 ASSERT_ERRNO(ENOSYS);
Josh Gao3de19152021-02-22 18:09:48 -080098 GTEST_SKIP() << "pidfd_send_signal not available";
99 }
100
101 ASSERT_EXIT(({
102 // gtest will fork a child off for ASSERT_EXIT: `self` refers to the parent.
103 unique_fd child(pidfd_open(getpid(), 0));
104 pidfd_send_signal(child.get(), SIGINT, nullptr, 0);
105 }),
106 testing::KilledBySignal(SIGINT), "");
107
108#endif
109}