aboutsummaryrefslogtreecommitdiff
path: root/tests/pidfd_test.cpp
blob: b9fadb4a0fec789273b463ef83f68a9e21d6e456 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * 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.
 */

#include <gtest/gtest.h>

#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>

#if defined(__BIONIC__)
#include <sys/pidfd.h>
#endif

#include <android-base/silent_death_test.h>
#include <android-base/unique_fd.h>

using android::base::unique_fd;
using namespace std::chrono_literals;

using pidfd_DeathTest = SilentDeathTest;

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));
  if (pidfd.get() == -1) {
    ASSERT_EQ(ENOSYS, errno);
    GTEST_SKIP() << "pidfd_open not available";
  }

  siginfo_t siginfo;
  int rc = waitid(P_PIDFD, pidfd.get(), &siginfo, WEXITED);
  if (rc == -1) {
    ASSERT_EQ(EINVAL, errno) << strerror(errno);
    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));
  if (self.get() == -1) {
    ASSERT_EQ(ENOSYS, errno);
    GTEST_SKIP() << "pidfd_open not available";
  }

  unique_fd dup(pidfd_getfd(self.get(), r.get(), 0));
  if (dup.get() == -1) {
    ASSERT_EQ(ENOSYS, errno) << strerror(errno);
    GTEST_SKIP() << "pidfd_getfd not available";
  }

  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));
  if (self.get() == -1) {
    ASSERT_EQ(ENOSYS, errno);
    GTEST_SKIP() << "pidfd_open not available";
  }

  if (pidfd_send_signal(self.get(), 0, nullptr, 0) == -1) {
    ASSERT_EQ(ENOSYS, errno);
    GTEST_SKIP() << "pidfd_send_signal not available";
  }

  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
}