gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/base_poll_test.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 "test/syscalls/linux/base_poll_test.h" 16 17 #include <sys/syscall.h> 18 #include <sys/types.h> 19 #include <syscall.h> 20 #include <unistd.h> 21 22 #include <memory> 23 24 #include "gtest/gtest.h" 25 #include "absl/memory/memory.h" 26 #include "absl/time/clock.h" 27 #include "absl/time/time.h" 28 #include "test/util/test_util.h" 29 30 namespace gvisor { 31 namespace testing { 32 33 static volatile int timer_fired = 0; 34 static void SigAlarmHandler(int, siginfo_t*, void*) { timer_fired = 1; } 35 36 BasePollTest::BasePollTest() { 37 // Register our SIGALRM handler, but save the original so we can restore in 38 // the destructor. 39 struct sigaction sa = {}; 40 sa.sa_sigaction = SigAlarmHandler; 41 sigfillset(&sa.sa_mask); 42 TEST_PCHECK(sigaction(SIGALRM, &sa, &original_alarm_sa_) == 0); 43 } 44 45 BasePollTest::~BasePollTest() { 46 ClearTimer(); 47 TEST_PCHECK(sigaction(SIGALRM, &original_alarm_sa_, nullptr) == 0); 48 } 49 50 void BasePollTest::SetTimer(absl::Duration duration) { 51 pid_t tgid = getpid(); 52 pid_t tid = gettid(); 53 ClearTimer(); 54 55 // Create a new timer thread. 56 timer_ = std::make_unique<TimerThread>(absl::Now() + duration, tgid, tid); 57 } 58 59 bool BasePollTest::TimerFired() const { return timer_fired; } 60 61 void BasePollTest::ClearTimer() { 62 timer_.reset(); 63 timer_fired = 0; 64 } 65 66 } // namespace testing 67 } // namespace gvisor