Evo C++ Library v0.5.1
Classes | Public Types | Static Public Member Functions | List of all members
Signal Class Reference

#include <evo/process.h>

Detailed Description

Process signal handling.

Example

Example using Signal::Main helper for a common case:

#include <evo/process.h>
static void on_shutdown(Signal::SigNumType, Signal::Type) {
// shutdown code goes here
}
int main() {
// Setup shutdown handler and call Signal::shutdown_sync() from destructor
Signal::Main signal_main(on_shutdown);
// main code goes here
return 0;
}

Here's the same code without the helper:

#include <evo/process.h>
static void on_shutdown(Signal::SigNumType, Signal::Type) {
// shutdown code goes here
}
int main() {
// main code goes here
// Required for Windows
return 0;
}

Classes

struct  Main
 Helper for common shutdown signal handling in program main(). More...
 
struct  MainServer
 Helper for common server shutdown signal handling in program main(). More...
 

Public Types

enum  Action { aDEFAULT, aIGNORE }
 Signal handling action. More...
 
typedef void(* Handler) (SigNumType, Type)
 Signal handler function type More...
 
typedef void(* OnShutdown) (SigNumType, Type)
 General shutdown handler type. More...
 
typedef int SigNumType
 Signal number value type. More...
 
enum  Type {
  tUNKNOWN = 0, tINTERRUPT, tTERMINATE, tPIPE,
  tCHILD, tHUP, tUSER1, tUSER2,
  tWIN_BREAK, tWIN_CLOSE, tWIN_LOGOFF, tWIN_SHUTDOWN
}
 Signal type. More...
 

Static Public Member Functions

static bool send_signal (ProcessId pid, Type signal)
 Send signal to current process. More...
 
static bool set_handler (Type type, Action action)
 Set signal handling option. More...
 
static bool set_handler (Type type, Handler handler)
 Set signal handler. More...
 
static bool set_on_shutdown (OnShutdown on_shutdown)
 Set shutdown handler. More...
 
static void shutdown_sync ()
 Sync shutdown with signal handler. More...
 

Member Typedef Documentation

◆ Handler

typedef void(* Handler) (SigNumType, Type)

Signal handler function type

◆ OnShutdown

typedef void(* OnShutdown) (SigNumType, Type)

General shutdown handler type.

◆ SigNumType

typedef int SigNumType

Signal number value type.

Member Enumeration Documentation

◆ Action

enum Action

Signal handling action.

Enumerator
aDEFAULT 

Use default handler, i.e. remove user handler.

aIGNORE 

Ignore signal.

◆ Type

enum Type

Signal type.

Enumerator
tUNKNOWN 

Unknown signal type (used internally)

tINTERRUPT 

Interrupted by Control-C (SIGINT) [Portable: Linux/Unix/Windows].

tTERMINATE 

General terminate (SIGTERM) [Portable: Linux/Unix/Windows].

tPIPE 

Write on broken pipe (SIGPIPE) [Linux/Unix].

tCHILD 

Child process terminated (SIGCHLD), ignored by default [Linux/Unix].

tHUP 

Terminal hangup, or daemon reload (SIGHUP) [Linux/Unix].

tUSER1 

User defined signal 1 (SIGUSR1) [Linux/Unix].

tUSER2 

User defined signal 2 (SIGUSR2) [Linux/Unix].

tWIN_BREAK 

Control-Break (CTRL_BREAK_EVENT) [Windows].

tWIN_CLOSE 

Process closed by user (CTRL_CLOSE_EVENT) [Windows].

tWIN_LOGOFF 

User logging off, though which user is undefined (CTRL_LOGOFF_EVENT) [Windows, services only].

tWIN_SHUTDOWN 

System shutting down (CTRL_SHUTDOWN_EVENT) [Windows, services only].

Member Function Documentation

◆ send_signal()

static bool send_signal ( ProcessId  pid,
Type  signal 
)
inlinestatic

Send signal to current process.

Parameters
pidProcess ID or process group ID to send to
signalSignal type to send

◆ set_handler() [1/2]

static bool set_handler ( Type  type,
Action  action 
)
inlinestatic

Set signal handling option.

Parameters
typeSignal type
actionAction for signal
Returns
Whether successful, false if action couldn't be set

◆ set_handler() [2/2]

static bool set_handler ( Type  type,
Handler  handler 
)
inlinestatic

Set signal handler.

Parameters
typeSignal type
handlerHandler for signal type, NULL to make sure main handler is installed without setting a user defined handler
Returns
Whether successful, false if action couldn't be set

◆ set_on_shutdown()

static bool set_on_shutdown ( OnShutdown  on_shutdown)
inlinestatic

Set shutdown handler.

  • This sets a general shutdown handler that is called on tINTERRUPT or tTERMINATE signals
  • When this is set, shutdown_sync() should be called by main() right before it returns – this is required for graceful shutdown to work correctly in Windows
  • Other handlers that were set via set_handler() are called before on_shutdown

◆ shutdown_sync()

static void shutdown_sync ( )
inlinestatic

Sync shutdown with signal handler.

  • This should be called right before main() exits, to notify that the process has shut down
  • Windows signal handlers work differently and run in another thread, and this notifies that thread that shutdown is complete so it may finish and kill the process
  • This is a no-op if a shutdown handler wasn't set, and is always a no-op in Linux/Unix

The documentation for this class was generated from the following file: