2021-02-23 03:09:48 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-04-10 02:13:09 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2021-02-23 03:09:48 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
#include <sys/pidfd.h>
|
|
|
|
#endif
|
|
|
|
|
2021-04-10 02:13:09 +02:00
|
|
|
#include <android-base/silent_death_test.h>
|
2021-02-23 03:09:48 +01:00
|
|
|
#include <android-base/unique_fd.h>
|
|
|
|
|
2023-09-21 23:11:19 +02:00
|
|
|
#include "utils.h"
|
|
|
|
|
2021-02-23 03:09:48 +01:00
|
|
|
using android::base::unique_fd;
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2021-04-10 02:13:09 +02:00
|
|
|
using pidfd_DeathTest = SilentDeathTest;
|
2021-02-23 03:09:48 +01:00
|
|
|
|
|
|
|
TEST(pidfd, pidfd_open) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
pid_t child = fork();
|
|
|
|
ASSERT_NE(-1, child);
|
|
|
|
if (child == 0) {
|
|
|
|
_exit(42);
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_fd pidfd(pidfd_open(child, 0));
|
2023-09-23 02:15:25 +02:00
|
|
|
if (pidfd.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_open() in this kernel";
|
|
|
|
ASSERT_NE(-1, pidfd.get()) << strerror(errno);
|
2021-02-23 03:09:48 +01:00
|
|
|
|
|
|
|
siginfo_t siginfo;
|
|
|
|
int rc = waitid(P_PIDFD, pidfd.get(), &siginfo, WEXITED);
|
|
|
|
if (rc == -1) {
|
2023-09-21 23:11:19 +02:00
|
|
|
ASSERT_ERRNO(EINVAL);
|
2021-02-23 03:09:48 +01:00
|
|
|
GTEST_SKIP() << "P_PIDFD not available";
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_EQ(child, siginfo.si_pid);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pidfd, pidfd_getfd) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
unique_fd r, w;
|
|
|
|
ASSERT_TRUE(android::base::Pipe(&r, &w));
|
|
|
|
unique_fd self(pidfd_open(getpid(), 0));
|
2023-09-23 02:15:25 +02:00
|
|
|
if (self.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_open() in this kernel";
|
|
|
|
ASSERT_NE(-1, self.get()) << strerror(errno);
|
2021-02-23 03:09:48 +01:00
|
|
|
|
|
|
|
unique_fd dup(pidfd_getfd(self.get(), r.get(), 0));
|
2023-09-23 02:15:25 +02:00
|
|
|
if (dup.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_getfd() in this kernel";
|
|
|
|
ASSERT_NE(-1, dup.get()) << strerror(errno);
|
2021-02-23 03:09:48 +01:00
|
|
|
|
|
|
|
ASSERT_NE(r.get(), dup.get());
|
|
|
|
ASSERT_EQ(3, write(w.get(), "foo", 3));
|
|
|
|
char buf[4];
|
|
|
|
ASSERT_EQ(3, read(dup.get(), buf, sizeof(buf)));
|
|
|
|
ASSERT_EQ(0, memcmp(buf, "foo", 3));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(pidfd_DeathTest, pidfd_send_signal) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
unique_fd self(pidfd_open(getpid(), 0));
|
2023-09-23 02:15:25 +02:00
|
|
|
if (self.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_open() in this kernel";
|
|
|
|
ASSERT_NE(-1, self.get()) << strerror(errno);
|
2021-02-23 03:09:48 +01:00
|
|
|
|
2023-09-23 02:15:25 +02:00
|
|
|
int rc = pidfd_send_signal(self.get(), 0, nullptr, 0);
|
|
|
|
if (rc == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_send_signal() in this kernel";
|
|
|
|
ASSERT_EQ(0, rc) << strerror(errno);
|
2021-02-23 03:09:48 +01:00
|
|
|
|
|
|
|
ASSERT_EXIT(({
|
|
|
|
// gtest will fork a child off for ASSERT_EXIT: `self` refers to the parent.
|
|
|
|
unique_fd child(pidfd_open(getpid(), 0));
|
|
|
|
pidfd_send_signal(child.get(), SIGINT, nullptr, 0);
|
|
|
|
}),
|
|
|
|
testing::KilledBySignal(SIGINT), "");
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|