E-MailRelay
Classes | Public Types | Static Public Member Functions | List of all members
G::Cleanup Class Reference

A static interface for registering cleanup functions that are called when the process terminates abnormally. More...

#include <gcleanup.h>

Classes

struct  Arg
 Opaque leaky string pointer wrapper created by G::Cleanup::arg(). More...
 
struct  Block
 A RAII class to temporarily block signal delivery. More...
 

Public Types

using Fn = bool(*)(const Arg &) GDEF_FSIG_NOEXCEPT
 

Static Public Member Functions

static void init ()
 An optional early-initialisation function. May be called more than once. More...
 
static void add (Fn, Arg arg)
 Adds the given handler to the list of handlers that are to be called when the process terminates abnormally. More...
 
static void atexit (bool active=true)
 Ensures that the cleanup functions are also called via atexit(), in addition to abnormal-termination signals. More...
 
static void block () noexcept
 Temporarily blocks signals until release()d. More...
 
static void release () noexcept
 Releases block()ed signals. More...
 
static Arg arg (const char *)
 Duplicates a c-string for add(). More...
 
static Arg arg (const std::string &)
 Duplicates a string for add(). More...
 
static Arg arg (const Path &)
 Duplicates a path for add(). More...
 
static Arg arg (std::nullptr_t)
 Duplicates an empty string for add(). More...
 

Detailed Description

A static interface for registering cleanup functions that are called when the process terminates abnormally.

On unix this relates to signals like SIGTERM, SIGINT etc.

Definition at line 40 of file gcleanup.h.

Member Typedef Documentation

◆ Fn

using G::Cleanup::Fn = bool (*)(const Arg &) GDEF_FSIG_NOEXCEPT

Definition at line 63 of file gcleanup.h.

Member Function Documentation

◆ add()

void G::Cleanup::add ( Fn  fn,
Arg  arg 
)
static

Adds the given handler to the list of handlers that are to be called when the process terminates abnormally.

In principle the handler function should be fully reentrant and signal-safe.

The 'arg' value should come from arg(). The Arg object contains a copy of the data passed to it. It uses memory allocated on the heap which is never freed because it has to remain valid even as the process is terminating.

Once the handler returns true it is removed from the list of handlers; if it returns false then it may be retried.

Definition at line 94 of file gcleanup_unix.cpp.

◆ arg() [1/4]

G::Cleanup::Arg G::Cleanup::arg ( const char *  p)
static

Duplicates a c-string for add().

The duped pointer will be passed to the handler.

Definition at line 117 of file gcleanup_unix.cpp.

◆ arg() [2/4]

G::Cleanup::Arg G::Cleanup::arg ( const Path p)
static

Duplicates a path for add().

The path's string pointer will be passed to the handler.

Definition at line 130 of file gcleanup_unix.cpp.

◆ arg() [3/4]

G::Cleanup::Arg G::Cleanup::arg ( const std::string &  s)
static

Duplicates a string for add().

The duplicate's data() pointer will be passed to the handler.

Definition at line 124 of file gcleanup_unix.cpp.

◆ arg() [4/4]

G::Cleanup::Arg G::Cleanup::arg ( std::nullptr_t  )
static

Duplicates an empty string for add().

Definition at line 137 of file gcleanup_unix.cpp.

◆ atexit()

void G::Cleanup::atexit ( bool  active = true)
static

Ensures that the cleanup functions are also called via atexit(), in addition to abnormal-termination signals.

This can be useful when ancient third-party library code (eg. Xlib) might call exit(), but be careful to disable these exit handlers before normal termination by calling atexit(false).

Definition at line 100 of file gcleanup_unix.cpp.

◆ block()

void G::Cleanup::block ( )
staticnoexcept

Temporarily blocks signals until release()d.

This should be used before creating threads so that only the main thread does signal handling.

Definition at line 106 of file gcleanup_unix.cpp.

◆ init()

void G::Cleanup::init ( )
static

An optional early-initialisation function. May be called more than once.

Definition at line 89 of file gcleanup_unix.cpp.

◆ release()

void G::Cleanup::release ( )
staticnoexcept

Releases block()ed signals.

Definition at line 111 of file gcleanup_unix.cpp.


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