summaryrefslogtreecommitdiff
path: root/cpp/include/IceUtil/CtrlCHandler.h
blob: 98d963371ab4d1b9d4ac1b7963956fc45822ff56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// **********************************************************************
//
// Copyright (c) 2003 - 2004
// ZeroC, Inc.
// North Palm Beach, FL, USA
//
// All Rights Reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************


#ifndef ICE_UTIL_CTRL_C_HANDLER_H
#define ICE_UTIL_CTRL_C_HANDLER_H

#include <IceUtil/Config.h>
#include <IceUtil/Exception.h>

namespace IceUtil
{

// The CtrlCHandler provides a portable way to handle CTRL+C and
// CTRL+C like signals 
// On Unix/POSIX, the CtrlCHandler handles SIGHUP, SIGINT and SIGTERM. 
// On Windows, it is essentially a wrapper for SetConsoleCtrlHandler().
// 
// In a process, only one CtrlCHandler can exist at a given time: 
// the CtrlCHandler constructor raises CtrlCHandlerException if
// you attempt to create a second CtrlCHandler.
// On Unix/POSIX, it is essential to create the CtrlCHandler before
// creating any thread, as the CtrlCHandler constructor masks
// SIGHUP, SIGINT and SIGTERM; by default, threads created later will 
// inherit this signal mask.
//
// When a CTRL+C or CTRL+C like signal is sent to the process, the 
// user-registered callback is called in a separate thread; it is 
// given the signal number. The callback must not raise exceptions.
// On Unix/POSIX, the callback is NOT a signal handler and can call 
// functions that are not async-signal safe.
//
// The CtrCHandler destructor "unregisters" the callback. However
// on Unix/POSIX it does not restore the old signal mask in any
// thread, so SIGHUP, SIGINT and SIGTERM are then ignored.
//
// TODO: Maybe the behavior on Windows should be the same? Now we
// just restore the default behavior (TerminateProcess).

typedef void (*CtrlCHandlerCallback)(int);

class ICE_UTIL_API CtrlCHandler
{
public:

    CtrlCHandler(CtrlCHandlerCallback = 0);
    ~CtrlCHandler();

    void setCallback(CtrlCHandlerCallback);
    CtrlCHandlerCallback getCallback() const;
};


class ICE_UTIL_API CtrlCHandlerException : public Exception
{ 
public:
 
    CtrlCHandlerException(const char*, int);
    virtual const std::string& ice_name() const;
    virtual Exception* ice_clone() const;
    virtual void ice_throw() const;
};


}
#endif