• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tstack / lnav / 11872214087-1756

16 Nov 2024 06:12PM UTC coverage: 70.243% (+0.5%) from 69.712%
11872214087-1756

push

github

tstack
[build] disable regex101

46266 of 65866 relevant lines covered (70.24%)

467515.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/timer.cc
1
/**
2
 * Copyright (c) 2015, Suresh Sundriyal
3
 *
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * * Redistributions of source code must retain the above copyright notice, this
10
 * list of conditions and the following disclaimer.
11
 * * Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation
13
 * and/or other materials provided with the distribution.
14
 * * Neither the name of the copyright holder nor the names of its contributors
15
 * may be used to endorse or promote products derived from this software
16
 * without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29

30
#include "timer.hh"
31

32
#include "base/lnav_log.hh"
33
#include "config.h"
34

35
static const struct itimerval DISABLE_TV = {{0, 0}, {0, 0}};
36

37
timer::error::error(int err) : e_err(err) {}
×
38

39
timer::interrupt_timer::interrupt_timer(struct timeval t,
×
40
                                        sighandler_t_ sighandler = SIG_IGN)
×
41
    : new_handler(sighandler), new_val((struct itimerval){{0, 0}, t}),
×
42
      old_val(DISABLE_TV), armed(false)
×
43
{
44
    memset(&this->old_handler, 0, sizeof(this->old_handler));
×
45
}
46

47
int
48
timer::interrupt_timer::arm_timer()
×
49
{
50
    struct sigaction sa;
51

52
    // Disable the interval timer before setting the handler and arming the
53
    // interval timer or else we will have a race-condition where the timer
54
    // might fire and the appropriate handler might not be set.
55
    if (setitimer(ITIMER_REAL, &DISABLE_TV, &this->old_val) != 0) {
×
56
        log_error("Unable to disable the timer: %s", strerror(errno));
×
57
        return -1;
×
58
    }
59
    memset(&sa, 0, sizeof(sa));
×
60
    sa.sa_handler = this->new_handler;
×
61
    if (sigaction(SIGALRM, &sa, &this->old_handler) == -1) {
×
62
        log_error("Unable to set the signal handler: %s", strerror(errno));
×
63
        if (setitimer(ITIMER_REAL, &this->old_val, NULL) != 0) {
×
64
            log_error("Unable to reset the interrupt timer: %s",
×
65
                      strerror(errno));
66
            throw timer::error(errno);
×
67
        }
68
        return -1;
×
69
    }
70

71
    if (setitimer(ITIMER_REAL, &this->new_val, NULL) != 0) {
×
72
        if (sigaction(SIGALRM, &this->old_handler, NULL) == -1) {
×
73
            log_error("Unable to reset the signal handler: %s",
×
74
                      strerror(errno));
75
            throw timer::error(errno);
×
76
        }
77
        log_error("Unable to set the timer: %s", strerror(errno));
×
78
        return -1;
×
79
    }
80
    this->armed = true;
×
81
    return 0;
×
82
}
83

84
bool
85
timer::interrupt_timer::is_armed()
×
86
{
87
    return this->armed;
×
88
}
89

90
void
91
timer::interrupt_timer::disarm_timer()
×
92
{
93
    if (this->armed) {
×
94
        // Disable the interval timer before resetting the handler and rearming
95
        // the previous interval timer or else we will have a race-condition
96
        // where the timer might fire and the appropriate handler might not be
97
        // set.
98
        if (setitimer(ITIMER_REAL, &DISABLE_TV, NULL) != 0) {
×
99
            log_error("Failed to disable the timer: %s", strerror(errno));
×
100
            throw timer::error(errno);
×
101
        }
102
        if (sigaction(SIGALRM, &this->old_handler, NULL) == -1) {
×
103
            log_error("Failed to reinstall previous SIGALRM handler: %s",
×
104
                      strerror(errno));
105
            throw timer::error(errno);
×
106
        }
107
        if (setitimer(ITIMER_REAL, &this->old_val, NULL) != 0) {
×
108
            log_error("Failed to reset the timer to previous value: %s",
×
109
                      strerror(errno));
110
            throw timer::error(errno);
×
111
        }
112
        this->armed = false;
×
113
        this->old_val = DISABLE_TV;
×
114
        memset(&this->old_handler, 0, sizeof(this->old_handler));
×
115
    }
116
}
117

118
timer::interrupt_timer::~interrupt_timer()
×
119
{
120
    this->disarm_timer();
×
121
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc