Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SIGPIPE and SIGTERM handlers
/*
* Copyright (c) 2015 David Wicks, sansumbrella.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <signal.h>
///
/// @file Global Unix signal handlers. Reasonable default actions for Cinder and generic applications.
///
/// These signal handlers allow applications to shutdown cleanly on SIGTERM and keep running on SIGPIPE.
///
/// To allow your application to handle signals when running within lldb (Xcode's debugger),
/// you must send commands to the debugger. You can do this by adding a symbolic breakpoint on `main`
/// and executing the following debugger commands on that breakpoint:
///
/// process handle SIGTERM -n true -p true -s false
/// process handle SIGPIPE -n true -p true -s false
///
/// These tell lldb not to stop (-s) when those signals are received while debugging the process, and
/// to pass (-p) them along to your application. Feel free to have the program automatically continue.
///
/// When first running your program through the debugger, you should see the following in the output window:
/// NAME PASS STOP NOTIFY
/// ========== ===== ===== ======
/// SIGTERM true false true
/// NAME PASS STOP NOTIFY
/// ========== ===== ===== ======
/// SIGPIPE true false true
///
/// The above confirms that lldb got your commands.
///
#if defined(USE_CUSTOM_UNIX_SIGNAL_HANDLERS)
// User must provide handleBrokenPipeSignal and handleTerminateSignal functions.
#elif defined(CINDER_CINDER)
#include "cinder/app/App.h"
#include "cinder/Log.h"
/// Graceful ignoring of SIGPIPE.
void handleBrokenPipeSignal(int signum)
{
CI_LOG_W("Ignoring SIGPIPE signal.");
}
/// Graceful handler for terminate signal
void handleTerminateSignal(int signum)
{
CI_LOG_I("Received terminate signal (SIGTERM)");
ci::app::App::get()->dispatchAsync([] { ci::app::App::get()->quit(); });
}
#else // Not Cinder and not user-defined
#include <iostream>
/// Graceful ignoring of SIGPIPE.
/// Not strictly threadsafe, but only likely to garble the console output.
void handleBrokenPipeSignal(int signum)
{
std::cout << "Ignoring SIGPIPE signal." << std::endl;
}
/// Graceful handler for terminate signal
/// Not strictly threadsafe (cout), but we're terminating anyway.
void handleTerminateSignal(int signum)
{
std::cout << "Received terminate signal (SIGTERM). Exiting." << std::endl;
std::exit(signum);
}
#endif
/// Installs more graphics-application-friendly defaults for unix signals.
/// Default handlers log the signals.
/// Ignore broken pipes to prevent crashing on network errors.
/// Exit more gracefully when terminate signal is received.
inline void installUnixSignalHandlers() {
signal(SIGPIPE, &handleBrokenPipeSignal);
signal(SIGTERM, &handleTerminateSignal);
}
@sansumbrella

This comment has been minimized.

Copy link
Owner Author

commented Dec 7, 2015

This is unnecessary when using the asio library for networking, as it installs signal handlers for you (and converts them into error codes or exceptions depending on the version of functions you call).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.