Restore previous signal handler.
Probably doesn't matter, but odd that sigaction() wasn't just passing
null if it actually didn't care, and it's slightly less code to restore
rather than synthesize a new "probably right" struct sigaction.
Noticed by inspection.
Test: treehugger
Change-Id: I826f860e5e671692b2282e0597eed3f089a32b1e
diff --git a/ScopedAlarm.h b/ScopedAlarm.h
index bb50b9e..f1c4e73 100644
--- a/ScopedAlarm.h
+++ b/ScopedAlarm.h
@@ -29,10 +29,9 @@
public:
ScopedAlarm(std::chrono::microseconds us, std::function<void()> func) {
func_ = func;
- struct sigaction oldact {};
struct sigaction act {};
act.sa_handler = [](int) { ScopedAlarm::func_(); };
- sigaction(SIGALRM, &act, &oldact);
+ sigaction(SIGALRM, &act, &old_act_);
std::chrono::seconds s = std::chrono::duration_cast<std::chrono::seconds>(us);
itimerval t = itimerval{};
@@ -43,13 +42,12 @@
~ScopedAlarm() {
itimerval t = itimerval{};
setitimer(ITIMER_REAL, &t, NULL);
- struct sigaction act {};
- act.sa_handler = SIG_DFL;
- sigaction(SIGALRM, &act, NULL);
+ sigaction(SIGALRM, &old_act_, NULL);
}
private:
static std::function<void()> func_;
+ struct sigaction old_act_;
};
} // namespace android