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