Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Add sanity timeouts #142
Conversation
zyga
reviewed
Sep 14, 2016
| + if (sigaction(SIGALRM, &act, NULL) < 0) { | ||
| + die("cannot uninstall signal handler for SIGALRM"); | ||
| + } | ||
| + if (sanity_timeout_expired) { |
zyga
added some commits
Sep 14, 2016
jdstrand
reviewed
Sep 14, 2016
| + // system call we may be sleeping on to get interrupted. | ||
| + if (sigaction(SIGALRM, &act, NULL) < 0) { | ||
| + die("cannot install signal handler for SIGALRM"); | ||
| + } |
jdstrand
Sep 14, 2016
Contributor
This is more standard (eg, http://www.gnu.org/software/libc/manual/html_node/Sigaction-Function-Example.html):
struct sigaction act;
act.sa_handler = sc_SIGALRM_handler;
if (sigemptyset(&act.sa_mask) < 0) {
die(...);
}
act.sa_flags = 0;
if (sigaction(SIGINT, &act, NULL) < 0) {
die(...);
}
If you use this, you can drop the comment since it is clear the flag won't be set.
jdstrand
reviewed
Sep 14, 2016
| + struct sigaction act = { }; | ||
| + if (sigaction(SIGALRM, &act, NULL) < 0) { | ||
| + die("cannot uninstall signal handler for SIGALRM"); | ||
| + } |
jdstrand
Sep 14, 2016
Contributor
Similarly, I think you would want to use this instead:
struct sigaction act;
if (sigemptyset(&act.sa_mask) < 0) {
die(...);
}
if (sigaction(SIGINT, &act, NULL) < 0) {
die(...);
}
However, rather than setting this to the empty set, perhaps it would be better to save off the old action in sc_enable_sanity_timeout() and then reinstating it here. The GNU libc URL above shows how to do this; I suspect that may be overkill.
zyga
Sep 14, 2016
Collaborator
I assume you meant SIGARLM? I also tweaked this to restore SIG_DFL. I don't think we need to restore anything else as we're not using any other signals at this time (YAGNI)
zyga
added some commits
Sep 14, 2016
jdstrand
reviewed
Sep 14, 2016
| + sanity_timeout_expired = 0; | ||
| + struct sigaction act = {.sa_handler = sc_SIGALRM_handler }; | ||
| + if (sigemptyset(&act.sa_mask) < 0) | ||
| + die("cannot initialize POSIX signal set"); |
jdstrand
Sep 14, 2016
Contributor
Nitpick: you aren't using '{}' for a one line body here but you do a few lines below. Does this pass indent?
jdstrand
reviewed
Sep 14, 2016
| + alarm(0); | ||
| + struct sigaction act = {.sa_handler = SIG_DFL }; | ||
| + if (sigemptyset(&act.sa_mask) < 0) | ||
| + die("cannot initialize POSIX signal set"); |
|
+1 provided my style comments are addressed. |
zyga commentedSep 14, 2016
This patch adds library functions to ensure that ensures a system call
such as flock() doesn't block for more than a given "sanity timeout"
value. This is meant to guard against bugs in the code that might
manifest as hanging process, keeping a flock-based lock alive, that will
never wake up.
The timer is implemented using SIGARLM and alarm(). The intent is to
simply wake up the system call and detect a flag being set by the signal
handler. This relies on using a signal handler that is not restarting
system calls.
Signed-off-by: Zygmunt Krynicki zygmunt.krynicki@canonical.com