gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/tkill.cc (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  #include <sys/syscall.h>
    16  #include <sys/types.h>
    17  #include <unistd.h>
    18  
    19  #include <cerrno>
    20  #include <csignal>
    21  
    22  #include "gtest/gtest.h"
    23  #include "test/util/logging.h"
    24  #include "test/util/test_util.h"
    25  #include "test/util/thread_util.h"
    26  
    27  namespace gvisor {
    28  namespace testing {
    29  
    30  namespace {
    31  
    32  static int tkill(pid_t tid, int sig) {
    33    int ret;
    34    do {
    35      // NOTE(b/25434735): tkill(2) could return EAGAIN for RT signals.
    36      ret = syscall(SYS_tkill, tid, sig);
    37    } while (ret == -1 && errno == EAGAIN);
    38    return ret;
    39  }
    40  
    41  TEST(TkillTest, InvalidTID) {
    42    EXPECT_THAT(tkill(-1, 0), SyscallFailsWithErrno(EINVAL));
    43    EXPECT_THAT(tkill(0, 0), SyscallFailsWithErrno(EINVAL));
    44  }
    45  
    46  TEST(TkillTest, ValidTID) {
    47    EXPECT_THAT(tkill(gettid(), 0), SyscallSucceeds());
    48  }
    49  
    50  void SigHandler(int sig, siginfo_t* info, void* context) {
    51    TEST_CHECK(sig == SIGRTMAX);
    52    TEST_CHECK(info->si_pid == getpid());
    53    TEST_CHECK(info->si_uid == getuid());
    54    TEST_CHECK(info->si_code == SI_TKILL);
    55  }
    56  
    57  // Test with a real signal. Regression test for b/24790092.
    58  TEST(TkillTest, ValidTIDAndRealSignal) {
    59    struct sigaction sa;
    60    sa.sa_sigaction = SigHandler;
    61    sigfillset(&sa.sa_mask);
    62    sa.sa_flags = SA_SIGINFO;
    63    ASSERT_THAT(sigaction(SIGRTMAX, &sa, nullptr), SyscallSucceeds());
    64    // InitGoogle blocks all RT signals, so we need undo it.
    65    sigset_t unblock;
    66    sigemptyset(&unblock);
    67    sigaddset(&unblock, SIGRTMAX);
    68    ASSERT_THAT(sigprocmask(SIG_UNBLOCK, &unblock, nullptr), SyscallSucceeds());
    69    EXPECT_THAT(tkill(gettid(), SIGRTMAX), SyscallSucceeds());
    70  }
    71  
    72  }  // namespace
    73  
    74  }  // namespace testing
    75  }  // namespace gvisor