aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/signal-safe.hpp
blob: ac7514fc153031a31d956a213929f10933ac8b57 (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
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
#pragma once

#include "defines.hpp"
#include <cstring>

template <uint16_t N>
class CMaxLengthCString {
  public:
    CMaxLengthCString() {
        m_str[0] = '\0';
    }
    void operator+=(char const* rhs) {
        write(rhs, strlen(rhs));
    }
    void write(char const* data, size_t len) {
        if (m_boundsExceeded || m_strPos + len >= N) {
            m_boundsExceeded = true;
            return;
        }
        memcpy(m_str + m_strPos, data, len);
        m_strPos += len;
        m_str[m_strPos] = '\0';
    }
    void write(char c) {
        if (m_boundsExceeded || m_strPos + 1 >= N) {
            m_boundsExceeded = true;
            return;
        }
        m_str[m_strPos] = c;
        m_strPos++;
    }
    void writeNum(size_t num) {
        size_t d = 1;
        while (num / 10 >= d)
            d *= 10;
        while (num > 0) {
            char c = '0' + (num / d);
            write(c);
            num %= d;
            d /= 10;
        }
    }
    char const* getStr() {
        return m_str;
    };
    bool boundsExceeded() {
        return m_boundsExceeded;
    };

  private:
    char   m_str[N];
    size_t m_strPos         = 0;
    bool   m_boundsExceeded = false;
};

template <uint16_t BUFSIZE>
class CBufFileWriter {
  public:
    CBufFileWriter(int fd_) : m_fd(fd_) {}
    ~CBufFileWriter() {
        flush();
    }
    void write(char const* data, size_t len) {
        while (len > 0) {
            size_t to_add = std::min(len, (size_t)BUFSIZE - m_writeBufPos);
            memcpy(m_writeBuf + m_writeBufPos, data, to_add);
            data += to_add;
            len -= to_add;
            m_writeBufPos += to_add;
            if (m_writeBufPos == BUFSIZE)
                flush();
        }
    }
    void write(char c) {
        if (m_writeBufPos == BUFSIZE)
            flush();
        m_writeBuf[m_writeBufPos] = c;
        m_writeBufPos++;
    }
    void operator+=(char const* str) {
        write(str, strlen(str));
    }
    void operator+=(std::string_view str) {
        write(str.data(), str.size());
    }
    void operator+=(char c) {
        write(c);
    }
    void writeNum(size_t num) {
        size_t d = 1;
        while (num / 10 >= d)
            d *= 10;
        while (num > 0) {
            char c = '0' + (num / d);
            write(c);
            num %= d;
            d /= 10;
        }
    }
    void writeCmdOutput(const char* cmd) {
        int pipefd[2];
        if (pipe(pipefd) < 0) {
            *this += "<pipe(pipefd) failed with";
            writeNum(errno);
            *this += ">\n";
            return;
        }
        // terminate child instead of waiting
        {
            struct sigaction act;
            act.sa_handler = SIG_DFL;
            sigemptyset(&act.sa_mask);
            act.sa_flags = SA_NOCLDWAIT;
#ifdef SA_RESTORER
            act.sa_restorer = NULL;
#endif
            sigaction(SIGCHLD, &act, nullptr);
        }
        const pid_t pid = fork();
        if (pid < 0) {
            *this += "<fork() failed with ";
            writeNum(errno);
            *this += ">\n";
            return;
        }
        if (pid == 0) {
            close(pipefd[0]);
            dup2(pipefd[1], STDOUT_FILENO);
            char const* const argv[] = {"/bin/sh", "-c", cmd, nullptr};
            execv("/bin/sh", (char* const*)argv);

            CBufFileWriter<64> failmsg(pipefd[1]);
            failmsg += "<execv(";
            failmsg += cmd;
            failmsg += ") resulted in errno ";
            failmsg.write(errno);
            failmsg += ">\n";
            close(pipefd[1]);
            abort();
        } else {
            close(pipefd[1]);
            int64_t len = 0;
            char    readbuf[256];
            while ((len = read(pipefd[0], readbuf, 256)) > 0) {
                write(readbuf, len);
            }
            if (len < 0) {
                *this += "<interrupted, read() resulted in errno ";
                writeNum(errno);
                *this += ">\n";
            }
            close(pipefd[0]);
        }
    }
    void flush() {
        size_t i = 0;
        while (i < m_writeBufPos) {
            auto written = ::write(m_fd, m_writeBuf + i, m_writeBufPos - i);
            if (written <= 0) {
                return;
            }
            i += written;
        }
        m_writeBufPos = 0;
    }

  private:
    char   m_writeBuf[BUFSIZE] = {0};
    size_t m_writeBufPos       = 0;
    int    m_fd                = 0;
};

char const* sigGetenv(char const* name);

char const* sigStrsignal(int sig);