diff options
author | Sage Weil <sage.weil@dreamhost.com> | 2012-02-11 18:45:06 +0100 |
---|---|---|
committer | Sage Weil <sage.weil@dreamhost.com> | 2012-02-15 06:03:53 +0100 |
commit | ecd280253af71127e701149984e75ab9f2da61ee (patch) | |
tree | baf965305e00b14da57083fb322cb366e50f7a23 /src/test/signals.cc | |
parent | doc: Balance backticks. (diff) | |
download | ceph-ecd280253af71127e701149984e75ab9f2da61ee.tar.xz ceph-ecd280253af71127e701149984e75ab9f2da61ee.zip |
signals: implement safe async signal handler framework
Based on http://evbergen.home.xs4all.nl/unix-signals.html.
Instead of his design, though, we write single bytes, and create a pipe per
signal we have handlers registered for.
Signed-off-by: Sage Weil <sage@newdream.net>
Diffstat (limited to 'src/test/signals.cc')
-rw-r--r-- | src/test/signals.cc | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/test/signals.cc b/src/test/signals.cc index d68424c1735..43754988559 100644 --- a/src/test/signals.cc +++ b/src/test/signals.cc @@ -45,3 +45,68 @@ TEST(SignalApi, SimpleInstallAndTest) TEST(SignalEffects, ErrnoTest1) { } + +bool usr1 = false; +bool usr2 = false; + +void reset() +{ + usr1 = false; + usr2 = false; +} + +void testhandler(int signal) +{ + switch (signal) { + case SIGUSR1: + usr1 = true; + break; + case SIGUSR2: + usr2 = true; + break; + default: + assert(0 == "unexpected signal"); + } +} + +TEST(SignalHandler, Single) +{ + reset(); + init_async_signal_handler(); + register_async_signal_handler(SIGUSR1, testhandler); + ASSERT_TRUE(usr1 == false); + + int ret = kill(getpid(), SIGUSR1); + ASSERT_EQ(ret, 0); + + sleep(1); + ASSERT_TRUE(usr1 == true); + + unregister_async_signal_handler(SIGUSR1, testhandler); + shutdown_async_signal_handler(); +} + +TEST(SignalHandler, Multiple) +{ + int ret; + + reset(); + init_async_signal_handler(); + register_async_signal_handler(SIGUSR1, testhandler); + register_async_signal_handler(SIGUSR2, testhandler); + ASSERT_TRUE(usr1 == false); + ASSERT_TRUE(usr2 == false); + + ret = kill(getpid(), SIGUSR1); + ASSERT_EQ(ret, 0); + ret = kill(getpid(), SIGUSR2); + ASSERT_EQ(ret, 0); + + sleep(1); + ASSERT_TRUE(usr1 == true); + ASSERT_TRUE(usr2 == true); + + unregister_async_signal_handler(SIGUSR1, testhandler); + unregister_async_signal_handler(SIGUSR2, testhandler); + shutdown_async_signal_handler(); +} |