@@ -52,6 +52,10 @@ using testing::internal::AlwaysTrue;
52
52
# include < signal.h>
53
53
# include < stdio.h>
54
54
55
+ # if GTEST_OS_LINUX
56
+ # include < sys/time.h>
57
+ # endif // GTEST_OS_LINUX
58
+
55
59
# include " gtest/gtest-spi.h"
56
60
57
61
// Indicates that this translation unit is part of Google Test's
@@ -372,6 +376,57 @@ TEST_F(TestForDeathTest, FastDeathTestInChangedDir) {
372
376
ASSERT_DEATH (_exit (1 ), " " );
373
377
}
374
378
379
+ # if GTEST_OS_LINUX
380
+ void SigprofAction (int , siginfo_t *, void *) { /* no op */ }
381
+
382
+ // Sets SIGPROF action and ITIMER_PROF timer (interval: 1ms).
383
+ void SetSigprofActionAndTimer () {
384
+ struct itimerval timer;
385
+ timer.it_interval .tv_sec = 0 ;
386
+ timer.it_interval .tv_usec = 1 ;
387
+ timer.it_value = timer.it_interval ;
388
+ ASSERT_EQ (0 , setitimer (ITIMER_PROF, &timer, NULL ));
389
+ struct sigaction signal_action;
390
+ memset (&signal_action, 0 , sizeof (signal_action));
391
+ sigemptyset (&signal_action.sa_mask );
392
+ signal_action.sa_sigaction = SigprofAction;
393
+ signal_action.sa_flags = SA_RESTART | SA_SIGINFO;
394
+ ASSERT_EQ (0 , sigaction (SIGPROF, &signal_action, NULL ));
395
+ }
396
+
397
+ // Disables ITIMER_PROF timer and ignores SIGPROF signal.
398
+ void DisableSigprofActionAndTimer (struct sigaction * old_signal_action) {
399
+ struct itimerval timer;
400
+ timer.it_interval .tv_usec = 0 ;
401
+ timer.it_value .tv_usec = 0 ;
402
+ ASSERT_EQ (0 , setitimer (ITIMER_PROF, &timer, NULL ));
403
+ struct sigaction signal_action;
404
+ memset (&signal_action, 0 , sizeof (signal_action));
405
+ sigemptyset (&signal_action.sa_mask );
406
+ signal_action.sa_handler = SIG_IGN;
407
+ ASSERT_EQ (0 , sigaction (SIGPROF, &signal_action, old_signal_action));
408
+ }
409
+
410
+ // Tests that death tests work when SIGPROF handler and timer are set.
411
+ TEST_F (TestForDeathTest, FastSigprofActionSet) {
412
+ testing::GTEST_FLAG (death_test_style) = " fast" ;
413
+ SetSigprofActionAndTimer ();
414
+ EXPECT_DEATH (_exit (1 ), " " );
415
+ struct sigaction old_signal_action;
416
+ DisableSigprofActionAndTimer (&old_signal_action);
417
+ EXPECT_TRUE (old_signal_action.sa_sigaction == SigprofAction);
418
+ }
419
+
420
+ TEST_F (TestForDeathTest, ThreadSafeSigprofActionSet) {
421
+ testing::GTEST_FLAG (death_test_style) = " threadsafe" ;
422
+ SetSigprofActionAndTimer ();
423
+ EXPECT_DEATH (_exit (1 ), " " );
424
+ struct sigaction old_signal_action;
425
+ DisableSigprofActionAndTimer (&old_signal_action);
426
+ EXPECT_TRUE (old_signal_action.sa_sigaction == SigprofAction);
427
+ }
428
+ # endif // GTEST_OS_LINUX
429
+
375
430
// Repeats a representative sample of death tests in the "threadsafe" style:
376
431
377
432
TEST_F (TestForDeathTest, StaticMemberFunctionThreadsafeStyle) {
0 commit comments