-
Notifications
You must be signed in to change notification settings - Fork 277
only selectively kill child, not entire process group #2918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,72 +9,81 @@ Author: Daniel Kroening, kroening@kroening.com | |
\*******************************************************************/ | ||
|
||
#include "signal_catcher.h" | ||
#include "invariant.h" | ||
|
||
#if defined(_WIN32) | ||
#include <process.h> | ||
#else | ||
#include <cstdlib> | ||
#include <csignal> | ||
#endif | ||
|
||
#include <vector> | ||
|
||
// Here we have an instance of an ugly global object. | ||
// It keeps track of any child processes that we'll kill | ||
// when we are told to terminate. | ||
// when we are told to terminate. "No child" is indicated by '0'. | ||
|
||
#ifdef _WIN32 | ||
#else | ||
std::vector<pid_t> pids_of_children; | ||
pid_t child_pid = 0; | ||
|
||
void register_child(pid_t pid) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might want to include an invariant ensuring that only one child is registered. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added preconditions |
||
PRECONDITION(child_pid == 0); | ||
child_pid = pid; | ||
} | ||
|
||
void unregister_child() | ||
{ | ||
PRECONDITION(child_pid != 0); | ||
child_pid = 0; | ||
} | ||
#endif | ||
|
||
void install_signal_catcher() | ||
{ | ||
#if defined(_WIN32) | ||
#else | ||
#if defined(_WIN32) | ||
#else | ||
// declare act to deal with action on signal set | ||
// NOLINTNEXTLINE(readability/identifiers) | ||
static struct sigaction act; | ||
|
||
act.sa_handler=signal_catcher; | ||
act.sa_flags=0; | ||
act.sa_handler = signal_catcher; | ||
act.sa_flags = 0; | ||
sigfillset(&(act.sa_mask)); | ||
|
||
// install signal handler | ||
sigaction(SIGTERM, &act, nullptr); | ||
#endif | ||
#endif | ||
} | ||
|
||
void remove_signal_catcher() | ||
{ | ||
#if defined(_WIN32) | ||
#else | ||
#if defined(_WIN32) | ||
#else | ||
// declare act to deal with action on signal set | ||
// NOLINTNEXTLINE(readability/identifiers) | ||
static struct sigaction act; | ||
|
||
act.sa_handler=SIG_DFL; | ||
act.sa_flags=0; | ||
act.sa_handler = SIG_DFL; | ||
act.sa_flags = 0; | ||
sigfillset(&(act.sa_mask)); | ||
|
||
sigaction(SIGTERM, &act, nullptr); | ||
#endif | ||
#endif | ||
} | ||
|
||
void signal_catcher(int sig) | ||
{ | ||
#if defined(_WIN32) | ||
#else | ||
#if defined(_WIN32) | ||
#else | ||
|
||
#if 1 | ||
#if 0 | ||
// kill any children by killing group | ||
killpg(0, sig); | ||
#else | ||
// pass on to any children | ||
for(const auto &pid : pids_of_children) | ||
kill(pid, sig); | ||
#endif | ||
#else | ||
// pass on to our child, if any | ||
if(child_pid != 0) | ||
kill(child_pid, sig); | ||
#endif | ||
|
||
exit(sig); // should contemplate something from sysexits.h | ||
#endif | ||
#endif | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include <vector>
is probably redundant now. Actually more of the includes could be cleaned up (we don't need anything on Windows,csignal
is included by the header file already).