~emersion/blackbox

ref: 4f8e51ebbd48739f5ff782a8e6e624ae8fcb5014 blackbox/main.c -rw-r--r-- 4.9 KiB
4f8e51ebSimon Ser Accept command to run as arg 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#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 <sys/wait.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 [command...]\n");
			return 1;
		}
	}

	int in_fd;
	if (optind < argc) {
		char **cmd = &argv[optind];

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

		pid_t pid = fork();
		if (pid < 0) {
			perror("fork failed");
			return 1;
		} else if (pid == 0) {
			close(child_pipe[0]);

			// Redirect stdout to the pipe
			if (dup2(child_pipe[1], STDOUT_FILENO) < 0) {
				perror("dup2 failed");
				_exit(1);
			}
			close(child_pipe[1]);

			// Then redirect stderr to stdout
			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) {
				perror("dup2 failed");
				_exit(1);
			}

			// Fork again to avoid SIGCHLD
			pid = fork();
			if (pid < 0) {
				perror("fork failed");
				_exit(1);
			} else if (pid == 0) {
				execvp(cmd[0], cmd);
				perror("execvp failed");
				_exit(1);
			}

			_exit(0);
		}

		close(child_pipe[1]);
		in_fd = child_pipe[0];

		int stat;
		if (waitpid(pid, &stat, 0) < 0) {
			perror("waitpid failed");
			return 1;
		}
		if (stat != 0) {
			fprintf(stderr, "failed to spawn child process\n");
			return 1;
		}
	} else {
		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;
}