aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/signal-safe.hpp
blob: ef6430973101ee4e45c28e9158514044ee8c2b9e (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 MaxLengthCString {
  public:
    MaxLengthCString() : m_strPos(0), m_boundsExceeded(false) {
        m_str[0] = '\0';
    }
    inline 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 write_num(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* get_str() {
        return m_str;
    };
    bool boundsExceeded() {
        return m_boundsExceeded;
    };

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

template <uint16_t BUFSIZE>
class BufFileWriter {
  public:
    inline BufFileWriter(int fd_) : m_writeBufPos(0), m_fd(fd_) {}
    ~BufFileWriter() {
        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();
        }
    }
    inline void write(char c) {
        if (m_writeBufPos == BUFSIZE)
            flush();
        m_writeBuf[m_writeBufPos] = c;
        m_writeBufPos++;
    }
    inline void operator+=(char const* str) {
        write(str, strlen(str));
    }
    inline void operator+=(std::string_view str) {
        write(str.data(), str.size());
    }
    inline 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, NULL);
        }
        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, NULL};
            execv("/bin/sh", (char* const*)argv);

            BufFileWriter<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]);
            long len;
            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];
    size_t m_writeBufPos;
    int    m_fd;
};

char const* sig_getenv(char const* name);

char const* sig_strsignal(int sig);