gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/pause.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 <errno.h>
    16  #include <signal.h>
    17  #include <sys/syscall.h>
    18  #include <sys/types.h>
    19  #include <unistd.h>
    20  
    21  #include <atomic>
    22  
    23  #include "gtest/gtest.h"
    24  #include "absl/synchronization/mutex.h"
    25  #include "absl/time/clock.h"
    26  #include "absl/time/time.h"
    27  #include "test/util/signal_util.h"
    28  #include "test/util/test_util.h"
    29  #include "test/util/thread_util.h"
    30  
    31  namespace gvisor {
    32  namespace testing {
    33  
    34  namespace {
    35  
    36  void NoopSignalHandler(int sig, siginfo_t* info, void* context) {}
    37  
    38  }  // namespace
    39  
    40  TEST(PauseTest, OnlyReturnsWhenSignalHandled) {
    41    struct sigaction sa;
    42    sigfillset(&sa.sa_mask);
    43  
    44    // Ensure that SIGUSR1 is ignored.
    45    sa.sa_handler = SIG_IGN;
    46    ASSERT_THAT(sigaction(SIGUSR1, &sa, nullptr), SyscallSucceeds());
    47  
    48    // Register a handler for SIGUSR2.
    49    sa.sa_sigaction = NoopSignalHandler;
    50    sa.sa_flags = SA_SIGINFO;
    51    ASSERT_THAT(sigaction(SIGUSR2, &sa, nullptr), SyscallSucceeds());
    52  
    53    // The child sets their own tid.
    54    absl::Mutex mu;
    55    pid_t child_tid = 0;
    56    bool child_tid_available = false;
    57    std::atomic<int> sent_signal{0};
    58    std::atomic<int> waking_signal{0};
    59    ScopedThread t([&] {
    60      mu.Lock();
    61      child_tid = gettid();
    62      child_tid_available = true;
    63      mu.Unlock();
    64      EXPECT_THAT(pause(), SyscallFailsWithErrno(EINTR));
    65      waking_signal.store(sent_signal.load());
    66    });
    67    mu.Lock();
    68    mu.Await(absl::Condition(&child_tid_available));
    69    mu.Unlock();
    70  
    71    // Wait a bit to let the child enter pause().
    72    absl::SleepFor(absl::Seconds(3));
    73  
    74    // The child should not be woken by SIGUSR1.
    75    sent_signal.store(SIGUSR1);
    76    ASSERT_THAT(tgkill(getpid(), child_tid, SIGUSR1), SyscallSucceeds());
    77    absl::SleepFor(absl::Seconds(3));
    78  
    79    // The child should be woken by SIGUSR2.
    80    sent_signal.store(SIGUSR2);
    81    ASSERT_THAT(tgkill(getpid(), child_tid, SIGUSR2), SyscallSucceeds());
    82    absl::SleepFor(absl::Seconds(3));
    83  
    84    EXPECT_EQ(SIGUSR2, waking_signal.load());
    85  }
    86  
    87  }  // namespace testing
    88  }  // namespace gvisor