Skip to content

Commit

Permalink
Make fatal signals cause an exit more promptly in special cases.
Browse files Browse the repository at this point in the history
The fatal-signal library notices and records fatal signals (e.g. SIGTERM)
and terminates the process on the next trip through poll_block().  But
some special utilities do not always invoke poll_block() promptly, e.g.
"ovs-ofctl monitor" does not call poll_block() as long as OpenFlow messages
are available.  But these special cases seem like they are all likely to
call into functions that themselves block (those with "_block" in their
names).  So make a new rule that such functions should always call
fatal_signal_run(), either directly or through poll_block().  This commit
implements and documents that rule.

Bug #2625.
  • Loading branch information
blp committed Apr 13, 2010
1 parent 9888b1b commit b302749
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/fatal-signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ fatal_signal_handler(int sig_nr)
stored_sig_nr = sig_nr;
}

/* Check whether a fatal signal has occurred and, if so, call the fatal signal
* hooks and exit.
*
* This function is called automatically by poll_block(), but specialized
* programs that may not always call poll_block() on a regular basis should
* also call it periodically. (Therefore, any function with "block" in its
* name should call fatal_signal_run() each time it is called, either directly
* or through poll_block(), because such functions can only used by specialized
* programs that can afford to block outside their main loop around
* poll_block().)
*/
void
fatal_signal_run(void)
{
Expand Down
4 changes: 4 additions & 0 deletions lib/jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "byteq.h"
#include "dynamic-string.h"
#include "fatal-signal.h"
#include "json.h"
#include "list.h"
#include "ofpbuf.h"
Expand Down Expand Up @@ -293,6 +294,8 @@ jsonrpc_send_block(struct jsonrpc *rpc, struct jsonrpc_msg *msg)
{
int error;

fatal_signal_run();

error = jsonrpc_send(rpc, msg);
if (error) {
return error;
Expand All @@ -314,6 +317,7 @@ jsonrpc_recv_block(struct jsonrpc *rpc, struct jsonrpc_msg **msgp)
for (;;) {
int error = jsonrpc_recv(rpc, msgp);
if (error != EAGAIN) {
fatal_signal_run();
return error;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/ovsdb-idl.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "bitmap.h"
#include "dynamic-string.h"
#include "fatal-signal.h"
#include "json.h"
#include "jsonrpc.h"
#include "ovsdb-data.h"
Expand Down Expand Up @@ -1281,6 +1282,7 @@ ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn *txn)
{
enum ovsdb_idl_txn_status status;

fatal_signal_run();
while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
ovsdb_idl_run(txn->idl);
ovsdb_idl_wait(txn->idl);
Expand Down
4 changes: 4 additions & 0 deletions lib/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <string.h>
#include "coverage.h"
#include "dynamic-string.h"
#include "fatal-signal.h"
#include "flow.h"
#include "ofp-print.h"
#include "ofpbuf.h"
Expand Down Expand Up @@ -237,6 +238,8 @@ stream_open_block(int error, struct stream **streamp)
{
struct stream *stream = *streamp;

fatal_signal_run();

while (error == EAGAIN) {
stream_run(stream);
stream_run_wait(stream);
Expand Down Expand Up @@ -570,6 +573,7 @@ pstream_accept_block(struct pstream *pstream, struct stream **new_stream)
{
int error;

fatal_signal_run();
while ((error = pstream_accept(pstream, new_stream)) == EAGAIN) {
pstream_wait(pstream);
poll_block();
Expand Down
9 changes: 9 additions & 0 deletions lib/vconn.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <string.h>
#include "coverage.h"
#include "dynamic-string.h"
#include "fatal-signal.h"
#include "flow.h"
#include "ofp-print.h"
#include "ofpbuf.h"
Expand Down Expand Up @@ -273,6 +274,8 @@ vconn_open_block(const char *name, int min_version, struct vconn **vconnp)
struct vconn *vconn;
int error;

fatal_signal_run();

error = vconn_open(name, min_version, &vconn);
while (error == EAGAIN) {
vconn_run(vconn);
Expand Down Expand Up @@ -607,6 +610,9 @@ int
vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
{
int retval;

fatal_signal_run();

while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
vconn_run(vconn);
vconn_run_wait(vconn);
Expand All @@ -621,6 +627,9 @@ int
vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp)
{
int retval;

fatal_signal_run();

while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
vconn_run(vconn);
vconn_run_wait(vconn);
Expand Down

0 comments on commit b302749

Please sign in to comment.