-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
266 lines (215 loc) · 7.13 KB
/
Copy pathmain.c
File metadata and controls
266 lines (215 loc) · 7.13 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include "controller.h"
#include "logger.h"
#define LISTEN_PORT 27910
#define IP_ADDRESS_SZ 15 // 111.222.333.444
#define SOCKET_FD 0
#define PIPE_FD 1
#define WAIT_INDEFINITELY -1
#define umin(x,y) (((uint64_t)(x) < (uint64_t)(y)) ? (x) : (y))
int sockfd = -1;
int connfd = -1;
int shutdown_pipe[] = {-1, -1};
FILE *ostream = NULL;
extern pthread_cond_t sessions_cv;
extern pthread_t scheduler_pthread;
extern bool shutdown_flag;
extern pthread_mutex_t scheduler_lock_m;
void dispatch(const char *msg, uint32_t length, char *peer_ip_address);
int innit_scheduler();
// Signal handler to close the port cleanly if we get killed
void handle_sig(int sig)
{
(void)sig;
if (shutdown_pipe[1] != -1) {
uint8_t foxes = 0xff;
int saved_errno = errno; // write() can change errno. Prolly fine, but Sprocket insisted.
write(shutdown_pipe[1], &foxes, 1);
errno = saved_errno;
}
}
static bool no_big_deal(int err)
{
return (err == EINTR ||
err == EAGAIN ||
err == EWOULDBLOCK ||
err == ENETDOWN ||
err == EPROTO ||
err == ENOPROTOOPT ||
err == EHOSTDOWN ||
err == ENONET ||
err == EHOSTUNREACH ||
err == EOPNOTSUPP ||
err == ENETUNREACH);
}
static bool maybe_a_problem(int err)
{
return (err == EMFILE ||
err == ENFILE ||
err == ENOBUFS ||
err == ENOMEM);
}
int main ()
{
struct sockaddr_in serv_addr, cli_addr;
socklen_t clilen;
struct timeval sock_timeout_val = {.tv_sec = 1, .tv_usec = 0};
int bytes_read, msg_len = 0;
int ret = 0;
bool healthy_sample = false;
char peer_ip_addr_str[IP_ADDRESS_SZ + 1];
char rawbuf[MAX_BUFF_SZ];
char msg[MAX_BUFF_SZ];
char *end_of_msg;
uint32_t space_left = 0,
bytes_to_grab = 0;
// close the port cleanly when I ctrl+C this sumbitch
struct sigaction sa = {.sa_handler = handle_sig};
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
ostream = stdout;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
out(ostream, "Failed to create socket: %s\n", strerror(errno));
return 1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(LISTEN_PORT);
if(bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
out(stderr, "Failed to bind socket: %s\n", strerror(errno));
return 1;
}
if(listen(sockfd, 3) != 0) {
out(stderr, "Failed to listen on socket: %s\n", strerror(errno));
return 1;
}
if(pipe(shutdown_pipe) != 0) {
out(stderr, "Failed to create shutdown pipe: %s\n", strerror(errno));
return 1;
}
if( (ret = innit_scheduler()) != 0) {
out(stderr, "Failed to kick off the scheduler. Something in innit_scheduler() failed. ret = %d\n", ret);
return ret;
}
struct pollfd polls[] =
{
[SOCKET_FD] = {.fd = sockfd, .events = POLLIN, .revents = 0},
[PIPE_FD] = {.fd = shutdown_pipe[0], .events = POLLIN, .revents = 0}
};
out(ostream, "listening on port %d\n", LISTEN_PORT);
while(1) {
// Wait for the socket or the pipe to squawk
ret = poll(polls, 2, WAIT_INDEFINITELY);
if(ret < 0) {
if (errno == EINTR) continue;
out(stderr, "wth? Pole broke!: %s\n", strerror(errno));
break;
}
if(polls[PIPE_FD].revents & POLLIN) {
// Somebody wrote to the pipe. As of this writing, that means
// I hit ctrl+c or otherwise sent a sigint or sigterm to the process.
// Or the scheduler paniced.
// Drain the pipe and bail.
uint8_t buf[32];
read(polls[PIPE_FD].fd, buf, sizeof(buf));
break;
}
if (polls[SOCKET_FD].revents & (POLLERR | POLLHUP | POLLNVAL)) {
out(stderr, "Listening socket poll error: revents=0x%x\n", polls[SOCKET_FD].revents);
break;
}
if (!(polls[SOCKET_FD].revents & POLLIN)) continue;
clilen = sizeof(cli_addr);
msg_len = 0;
healthy_sample = false;
memset(msg, 0, sizeof(msg));
connfd = accept(sockfd, (struct sockaddr*)&cli_addr, &clilen);
if (connfd < 0) {
int errno_temp = errno;
if(no_big_deal(errno_temp)) continue;
if(maybe_a_problem(errno_temp)) {
out(stderr, "Warning: accept() returned %d: %s\n", errno_temp, strerror(errno_temp));
// TODO: backoff?
continue;
}
out(stderr, "Something bad happened trying to accept() the socket. %d: %s\n", errno_temp, strerror(errno_temp));
break;
}
setsockopt(connfd, SOL_SOCKET, SO_RCVTIMEO, &sock_timeout_val, sizeof(sock_timeout_val));
// get the remote peer (useful for debug)
//peer_addr = (uint32_t)cli_addr.sin_addr.s_addr;
memset(peer_ip_addr_str, 0, IP_ADDRESS_SZ + 1);
inet_ntop(AF_INET, &cli_addr.sin_addr, peer_ip_addr_str, sizeof(peer_ip_addr_str));
//snprintf(peer_ip_addr_str, IP_ADDRESS_SZ, "%d.%d.%d.%d", peer_addr & 0xff, (peer_addr >> 8) & 0xff, (peer_addr >> 16) & 0xff, (peer_addr >> 24) & 0xff);
do {
bytes_read = read(connfd, rawbuf, MAX_BUFF_SZ);
// parse
if(bytes_read > 0) {
// how much sapce is left in the buffer?
space_left = sizeof(msg) - msg_len - 1;
// grab the whole buffer if we have room, else, just grab however much we have room for
bytes_to_grab = (uint) umin(space_left, bytes_read);
memcpy(&msg[msg_len], rawbuf, bytes_to_grab);
msg_len += bytes_to_grab;
end_of_msg = strchr(msg, '\n');
if (end_of_msg) {
// Message is healthy
healthy_sample = true;
break;
}
if (space_left == 0) {
msg[sizeof(msg) - 1] = 0;
out(stderr, "message too long from peer: %s\n", peer_ip_addr_str);
out(stderr, "message: \"%s\"\n", msg);
break;
}
} else if (bytes_read == 0) {
// connection closed by remote peer. I think.
break;
} else {
// socket error
if (errno == EINTR) continue;
if (errno == EAGAIN || errno == EWOULDBLOCK) {
out(stderr, "timed-out waiting for newline from peer: %s\n", peer_ip_addr_str);
out(stderr, " incomplete message: \"%s\"\n", msg);
} else {
out(stderr, "read error from peer: %s\n", peer_ip_addr_str);
perror(" errno:");
}
break;
}
} while(1);
close(connfd);
connfd = -1;
if(healthy_sample) dispatch(msg, msg_len, peer_ip_addr_str);
}
if(there_is_a_panic()) {
out(stderr, "The scheduler paniced!\n");
}
// Tell the scheduler to wrap things up and exit
// Set the shutdown flag
pthread_mutex_lock(&scheduler_lock_m);
shutdown_flag = true;
pthread_mutex_unlock(&scheduler_lock_m);
// kick the scheduler
pthread_cond_broadcast(&sessions_cv);
// wait for it to die
pthread_join(scheduler_pthread, NULL);
close(sockfd);
close(shutdown_pipe[1]);
close(shutdown_pipe[0]);
}