~emersion/blackbox

ref: 5b298bf4c1d7179ce8d1d5f1540bef62991f9d79 blackbox/main.c -rw-r--r-- 3.8 KiB
5b298bf4Simon Ser Handle SIGINT/SIGTERM 5 months ago
                                                                                
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
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

struct ring_buffer {
	// TODO: make size configurable
	char data[1024 * 1024];
	size_t offset, total;
};

static int set_nonblock(int fd) {
	int flags = fcntl(fd, F_GETFL);
	if (flags < 0) {
		perror("fcntl(F_GETFL) failed");
		return flags;
	}
	int ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
	if (ret < 0) {
		perror("fcntl(F_SETFL) failed");
		return ret;
	}
	return 0;
}

static int ring_buffer_read(struct ring_buffer *ring, int fd) {
	while (1) {
		int nread = read(fd, &ring->data[ring->offset], sizeof(ring->data) - ring->offset);
		if (nread < 0) {
			if (errno == EAGAIN) {
				break;
			}
			return nread;
		}

		ring->offset += nread;
		ring->offset %= sizeof(ring->data);

		ring->total += nread;
	}

	return 0;
}

static int ring_buffer_write(struct ring_buffer *ring, int fd) {
	if (ring->total <= sizeof(ring->data)) {
		if (write(fd, ring->data, ring->total) < 0) {
			perror("write failed");
			return -1;
		}
	} else {
		if (write(fd, &ring->data[ring->offset], sizeof(ring->data) - ring->offset) < 0 || write(fd, ring->data, ring->offset) < 0) {
			perror("write failed");
			return -1;
		}
	}
	return 0;
}

static int sig_fd = -1;

static void handle_signal(int sig) {
	if (write(sig_fd, &sig, sizeof(sig) != sizeof(sig))) {
		abort();
	}
}

int main(int argc, char *argv[]) {
	int opt;
	while ((opt = getopt(argc, argv, "")) != -1) {
		// TODO: add more useful options
		switch (opt) {
		default:
			fprintf(stderr, "usage: blackbox\n");
			return 1;
		}
	}

	int in_fd = STDIN_FILENO;
	if (set_nonblock(in_fd) != 0) {
		return 1;
	}

	const char *runtime_dir = getenv("XDG_RUNTIME_DIR");
	if (runtime_dir == NULL) {
		runtime_dir = "/tmp";
	}

	int socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (socket_fd < 0) {
		perror("socket failed");
		return 1;
	}

	struct sockaddr_un sockaddr = {0};
	sockaddr.sun_family = AF_UNIX;
	snprintf(sockaddr.sun_path, sizeof(sockaddr.sun_path), "%s/blackbox-%d", runtime_dir, getpid());
	if (bind(socket_fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)) != 0) {
		perror("bind failed");
		return 1;
	}
	if (listen(socket_fd, 128) != 0) {
		perror("listen failed");
		return 1;
	}

	if (set_nonblock(socket_fd) != 0) {
		return 1;
	}

	int sig_pipe[2];
	if (pipe(sig_pipe) != 0) {
		perror("pipe failed");
		return 1;
	}
	sig_fd = sig_pipe[1];

	if (signal(SIGTERM, handle_signal) == SIG_ERR || signal(SIGINT, handle_signal) == SIG_ERR) {
		perror("signal failed");
		return 1;
	}

	fprintf(stderr, "Flight recorder started\n");
	fprintf(stderr, "To grab data, run: nc -dU %s\n", sockaddr.sun_path);

	static struct ring_buffer ring_buffer = {0};

	struct pollfd pollfds[3] = {0};
	size_t pollfds_len = 0;

	struct pollfd *socket_pollfd = &pollfds[pollfds_len++];
	socket_pollfd->fd = socket_fd;
	socket_pollfd->events = POLLIN;

	struct pollfd *in_pollfd = &pollfds[pollfds_len++];
	in_pollfd->fd = in_fd;
	in_pollfd->events = POLLIN;

	struct pollfd *sig_pollfd = &pollfds[pollfds_len++];
	sig_pollfd->fd = sig_pipe[0];
	sig_pollfd->events = POLLIN;

	while (1) {
		if (poll(pollfds, pollfds_len, -1) < 0) {
			if (errno == EINTR) {
				continue;
			}
			perror("poll failed");
			return 1;
		}

		if (in_pollfd->revents & POLLHUP) {
			break;
		}

		if (in_pollfd->revents & POLLIN) {
			if (ring_buffer_read(&ring_buffer, in_fd) < 0) {
				return 1;
			}
		}

		if (socket_pollfd->revents & POLLIN) {
			// TODO: read commands from client FD
			int fd = accept(socket_fd, NULL, NULL);
			if (fd < 0) {
				perror("accept failed");
				return 1;
			}

			if (ring_buffer_write(&ring_buffer, fd) < 0) {
				close(fd);
				return 1;
			}

			close(fd);
		}

		if (sig_pollfd->revents & POLLIN) {
			break;
		}
	}

	unlink(sockaddr.sun_path);
	return 0;
}