summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/util/sigbushandler.cpp
blob: 9de3f16be67fb86fdaf94320986b237c7ea8698a (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "sigbushandler.h"
#include "statefile.h"
#include "statebuf.h"
#include <unistd.h>
#include <cstring>

namespace search {

SigBusHandler *SigBusHandler::_instance = nullptr;

namespace {

std::atomic<int> sigBusNesting;

class TryLockGuard
{
    bool _gotLock;
public:
    TryLockGuard() noexcept
        : _gotLock(false)
    {
        int expzero = 0;
        _gotLock = sigBusNesting.compare_exchange_strong(expzero, 1);
    }

    ~TryLockGuard() noexcept {
        if (_gotLock) {
            sigBusNesting = 0;
        }
    }

    bool gotLock() const noexcept { return _gotLock; }
};


/*
 * Write string to standard error using only async signal safe methods.
 */
void
mystderr(const char *msg) noexcept
{
    const char *p = msg;
    while (*p != '\0') {
        ++p;
    }
    write(STDERR_FILENO, msg, static_cast<size_t>(p - msg));
}

}

void
SigBusHandler::trap()
{
    struct sigaction sa;
    _instance = this;
    memset(&sa, 0, sizeof(sa));
    sa.sa_sigaction = SigBusHandler::forward;
    sa.sa_flags = SA_SIGINFO;
    sigemptyset(&sa.sa_mask);
    sigaddset(&sa.sa_mask, SIGBUS);
    sigaction(SIGBUS, &sa, nullptr);
    _trapped = true;
}


void
SigBusHandler::untrap()
{
    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = SIG_DFL;
    sa.sa_flags = 0;
    sigemptyset(&sa.sa_mask);
    sigaction(SIGBUS, &sa, nullptr);
    _trapped = false;
    _instance = nullptr;
}


void
SigBusHandler::forward(int sig, siginfo_t *si, void *ucv)
{
    _instance->handle(sig, si, ucv);
}


void
SigBusHandler::handle(int sig, siginfo_t *si, void *ucv)
{
    (void) sig;
    (void) ucv;

    StateBuf sb(_buf, sizeof(_buf));
    bool raced = false;
    do {
        // Protect against multiple threads.
        TryLockGuard guard;
        if (!guard.gotLock()) {
            raced = true;
            break;
        }
        sb.appendKey("state") << "down";
        sb.appendTimestamp();
        sb.appendKey("operation") << "sigbus";
        sb.appendKey("errno") << static_cast<long>(si->si_errno);
        sb.appendKey("code") << static_cast<long>(si->si_code);
        if (si->si_code != 0) {
            sb.appendAddr(si->si_addr);
        }
        sb << '\n';
        // TODO: Report backing store file, for quick diagnostics.
        if (_stateFile != nullptr) {
            _stateFile->addState(sb.base(), sb.size(), true);
        }
        _fired = true;
    } while (0);
    if (raced) {
        mystderr("SIGBUS handler call race, ignoring signal\n");
        sleep(5);
        return;
    }
    untrap(); // Further bus errors will trigger core dump

    if (_unwind != nullptr) {
        // Unit test is using siglongjmp based unwinding
        sigjmp_buf *unwind = _unwind;
        _unwind = nullptr;
        siglongjmp(*unwind, 1);
    } else {
        // Normal case, sleep 3 seconds (i.e. allow main thread to detect
        // issue and notify cluster controller) before returning and
        // likely core dumping.
        sleep(3);
    }
}


SigBusHandler::SigBusHandler(StateFile *stateFile)
    : _stateFile(stateFile),
      _unwind(nullptr),
      _trapped(false),
      _fired(false)
{
    trap();
}

SigBusHandler::~SigBusHandler()
{
    untrap();
}

}