aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/signals/signals.cpp
blob: 45e199e95f4486c5a72f0b9d2bf9350e522ed136 (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "signals.h"
#include "utils.h"

#include <yql/essentials/utils/backtrace/backtrace.h>

#include <util/stream/output.h>
#include <util/generic/yexception.h>
#include <util/datetime/base.h>
#include <util/network/socket.h>
#include <util/system/getpid.h>

#ifdef _linux_
#   include <sys/prctl.h>
#endif

#include <string.h>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>


namespace NYql {

volatile sig_atomic_t NeedTerminate = 0;
volatile sig_atomic_t NeedQuit = 0;
volatile sig_atomic_t NeedReconfigure = 0;
volatile sig_atomic_t NeedReopenLog = 0;
volatile sig_atomic_t NeedReapZombies = 0;
volatile sig_atomic_t NeedInterrupt = 0;

volatile sig_atomic_t CatchInterrupt = 0;

TPipe SignalPipeW;
TPipe SignalPipeR;

namespace {

void SignalHandler(int signo)
{
    switch (signo) {
    case SIGTERM:
        NeedTerminate = 1;
        break;

    case SIGQUIT:
        NeedQuit = 1;
        break;

#ifdef _unix_
    case SIGHUP:
        NeedReconfigure = 1;
        break;

    case SIGUSR1:
        NeedReopenLog = 1;
        break;

    case SIGCHLD:
        NeedReapZombies = 1;
        break;
#endif

    case SIGINT:
        if (CatchInterrupt) {
            NeedInterrupt = 1;
        } else {
            fprintf(stderr, "%s (pid=%d) captured SIGINT\n",
                    GetProcTitle(), getpid());
            signal(signo, SIG_DFL);
            raise(signo);
        }
        break;

    default:
        break;
    }
}

void SignalHandlerWithSelfPipe(int signo)
{
    SignalHandler(signo);

    int savedErrno = errno;
    if (write(SignalPipeW.GetHandle(), "x", 1) == -1 && errno != EAGAIN) {
        static TStringBuf msg("cannot write to signal pipe");
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif
        write(STDERR_FILENO, msg.data(), msg.size());
        abort();
    }
    errno = savedErrno;
}


#ifndef _unix_
const char* strsignal(int signo)
{
    switch (signo) {
    case SIGTERM: return "SIGTERM";
    case SIGINT: return "SIGINT";
    case SIGQUIT: return "SIGQUIT";
    default:
        return "UNKNOWN";
    }
}
#endif


#ifdef _unix_
int SetSignalHandler(int signo, void (*handler)(int))
{
    struct sigaction sa;
    sa.sa_flags = SA_RESTART;
    sa.sa_handler = handler;
    sigemptyset(&sa.sa_mask);

    return sigaction(signo, &sa, nullptr);
}

#else
int SetSignalHandler(int signo, void (*handler)(int))
{
    return (signal(signo, handler) == SIG_ERR) ? -1 : 0;
}

#endif

struct TSignalHandlerDesc
{
    int signo;
    void (*handler)(int);
};

void SetSignalHandlers(const TSignalHandlerDesc* handlerDescs)
{
    sigset_t interestedSignals;
    SigEmptySet(&interestedSignals);

    for (int i = 0; handlerDescs[i].signo != -1; i++) {
        int signo = handlerDescs[i].signo;
        SigAddSet(&interestedSignals, signo);

        if (SetSignalHandler(signo, handlerDescs[i].handler) == -1) {
            ythrow TSystemError() << "Cannot set handler for signal "
                                  << strsignal(signo);
        }
    }

    if (SigProcMask(SIG_BLOCK, &interestedSignals, NULL) == -1) {
        ythrow TSystemError() << "Cannot set sigprocmask";
    }

    NYql::NBacktrace::RegisterKikimrFatalActions();
}

} // namespace


void InitSignals()
{
    TSignalHandlerDesc handlerDescs[] = {
        { SIGTERM, SignalHandler },
        { SIGINT, SignalHandler },
        { SIGQUIT, SignalHandler },
#ifdef _unix_
        { SIGPIPE, SIG_IGN },
        { SIGHUP, SignalHandler },
        { SIGUSR1, SignalHandler },
        { SIGCHLD, SignalHandler },
#endif
        { -1, nullptr }
    };

    SetSignalHandlers(handlerDescs);
}

void InitSignalsWithSelfPipe()
{
    TSignalHandlerDesc handlerDescs[] = {
        { SIGTERM, SignalHandlerWithSelfPipe },
        { SIGINT, SignalHandlerWithSelfPipe },
        { SIGQUIT, SignalHandlerWithSelfPipe },
#ifdef _unix_
        { SIGPIPE, SIG_IGN },
        { SIGHUP, SignalHandlerWithSelfPipe },
        { SIGUSR1, SignalHandlerWithSelfPipe },
        { SIGCHLD, SignalHandlerWithSelfPipe },
#endif
        { -1, nullptr }
    };

    TPipe::Pipe(SignalPipeR, SignalPipeW);
    SetNonBlock(SignalPipeR.GetHandle());
    SetNonBlock(SignalPipeW.GetHandle());

    SetSignalHandlers(handlerDescs);
}

void CatchInterruptSignal(bool doCatch) {
    CatchInterrupt = doCatch;
}

void SigSuspend(const sigset_t* mask)
{
#ifdef _unix_
    sigsuspend(mask);
#else
    Y_UNUSED(mask);
    Sleep(TDuration::Seconds(1));
#endif
}

void AllowAnySignals()
{
    sigset_t blockMask;
    SigEmptySet(&blockMask);

    if (SigProcMask(SIG_SETMASK, &blockMask, NULL) == -1) {
        ythrow TSystemError() << "Cannot set sigprocmask";
    }
}

bool HasPendingQuitOrTerm() {
#ifdef _unix_
    sigset_t signals;
    SigEmptySet(&signals);
    if (sigpending(&signals)) {
        ythrow TSystemError() << "Error in sigpending";
    }

    return (SigIsMember(&signals, SIGQUIT) == 1) || (SigIsMember(&signals, SIGTERM) == 1);
#else
    return false;
#endif
}
} // namespace NYql