forked from beanstalkd/beanstalkd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
beanstalkd.c
343 lines (269 loc) · 8.86 KB
/
beanstalkd.c
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* beanstalk - fast, general-purpose work queue */
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "conn.h"
#include "net.h"
#include "beanstalkd.h"
#include "util.h"
/* job data cannot be greater than this */
#define JOB_DATA_SIZE_LIMIT ((1 << 16) - 1)
static void
drop_root()
{
/* pass */
}
static void
daemonize()
{
/* pass */
}
static void
set_sig_handlers()
{
int r;
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
r = sigemptyset(&sa.sa_mask);
if (r == -1) perror("sigemptyset()"), exit(111);
r = sigaction(SIGPIPE, &sa, 0);
if (r == -1) perror("sigaction(SIGPIPE)"), exit(111);
}
static void
check_err(conn c, const char *s)
{
if (errno == EAGAIN) return;
if (errno == EINTR) return;
if (errno == EWOULDBLOCK) return;
perror(s);
conn_close(c);
return;
}
/* Scan the given string for the sequence "\r\n" and return the line length.
* Always returns at least 2 if a match is found. Returns 0 if no match. */
static int
scan_line_end(const char *s, int size)
{
char *match;
match = memchr(s, '\r', size - 1);
if (!match) return 0;
/* this is safe because we only scan size - 1 chars above */
if (match[1] == '\n') return match - s + 2;
return 0;
}
/* parse the command line */
static int
which_cmd(conn c)
{
#define TEST_CMD(s,c,o) if (strncmp((s), (c), CONSTSTRLEN(c)) == 0) return (o);
TEST_CMD(c->cmd, CMD_PUT, OP_PUT);
TEST_CMD(c->cmd, CMD_PEEK, OP_PEEK);
TEST_CMD(c->cmd, CMD_RESERVE, OP_RESERVE);
TEST_CMD(c->cmd, CMD_DELETE, OP_DELETE);
TEST_CMD(c->cmd, CMD_JOBSTATS, OP_JOBSTATS);
TEST_CMD(c->cmd, CMD_STATS, OP_STATS);
return OP_UNKNOWN;
}
static void
reply_word(conn c, const char *word, int len)
{
int r;
r = conn_update_evq(c, EV_WRITE | EV_PERSIST, NULL);
if (r == -1) return warn("conn_update_evq() failed"), conn_close(c);
c->reply = word;
c->reply_len = len;
c->state = STATE_SENDWORD;
}
static void
enqueue_job(conn c)
{
job j = c->job;
/* check if the trailer is present and correct */
if (memcmp(j->data + j->data_size - 2, "\r\n", 2)) return conn_close(c);
/* we have a complete job, so let's stick it in the pqueue */
/* TODO stick it in */
warn("TODO stick it in");
c->job = NULL;
reply_word(c, MSG_INSERTED, MSG_INSERTED_LEN);
}
static void
check_for_complete_job(conn c)
{
job j = c->job;
/* do we have a complete job? */
if (j->data_read == j->data_size) return enqueue_job(c);
/* otherwise we have incomplete data, so just keep waiting */
c->state = STATE_WANTDATA;
}
/* Copy up to data_size trailing bytes into the job, then the rest into the cmd
* buffer. If c->job exists, this assumes that c->job->data is empty. */
static void
fill_extra_data(conn c)
{
int extra_bytes, job_data_bytes = 0, cmd_bytes;
/* how many extra bytes did we read? */
extra_bytes = c->cmd_read - c->cmd_len;
/* how many bytes should we put into the job data? */
if (c->job) {
job_data_bytes = min(extra_bytes, c->job->data_size);
memcpy(c->job->data, c->cmd + c->cmd_len, job_data_bytes);
c->job->data_read = job_data_bytes;
}
/* how many bytes are left to go into the next cmd? */
cmd_bytes = extra_bytes - job_data_bytes;
memmove(c->cmd, c->cmd + c->cmd_len + job_data_bytes, cmd_bytes);
c->cmd_read = cmd_bytes;
}
static void
do_cmd(conn c)
{
char type;
char *size_buf, *end_buf;
unsigned int pri, data_size;
/* NUL-terminate this string so we can use strtol and friends */
c->cmd[c->cmd_len - 2] = '\0';
/* check for possible maliciousness */
if (strlen(c->cmd) != c->cmd_len - 2) return conn_close(c);
type = which_cmd(c);
switch (type) {
case OP_PUT:
errno = 0;
pri = strtoul(c->cmd + 4, &size_buf, 10);
if (errno) return conn_close(c);
errno = 0;
data_size = strtoul(size_buf, &end_buf, 10);
if (errno) return conn_close(c);
if (data_size > JOB_DATA_SIZE_LIMIT) return conn_close(c);
/* don't allow trailing garbage */
if (end_buf[0] != '\0') return conn_close(c);
c->job = make_job(pri, data_size + 2);
fprintf(stderr, "put pri=%d bytes=%d\n", pri, data_size);
fill_extra_data(c);
check_for_complete_job(c);
break;
case OP_PEEK:
reply_word(c, MSG_NOTFOUND, MSG_NOTFOUND_LEN);
break;
case OP_RESERVE:
/* don't allow trailing garbage */
if (c->cmd_len != CMD_RESERVE_LEN + 2) return conn_close(c);
fprintf(stderr, "got reserve cmd: %s\n", c->cmd);
/* TODO reserve the job */
warn("TODO reserve the job");
fill_extra_data(c);
c->state = STATE_WRITE;
break;
case OP_DELETE:
reply_word(c, MSG_NOTFOUND, MSG_NOTFOUND_LEN);
break;
case OP_STATS:
/* don't allow trailing garbage */
if (c->cmd_len != CMD_STATS_LEN + 2) return conn_close(c);
warn("got stats command");
conn_close(c);
break;
case OP_JOBSTATS:
warn("got job stats command");
conn_close(c);
break;
default:
/* unknown command -- protocol error */
fprintf(stderr, "got unknown cmd: %s\n", c->cmd);
return conn_close(c);
}
}
static void
h_conn(const int fd, const short which, conn c)
{
int r;
job j;
if (fd != c->fd) {
warn("Argh! event fd doesn't match conn fd.");
close(fd);
return conn_close(c);
}
fprintf(stderr, "%d: got event %d\n", fd, which);
switch (c->state) {
case STATE_WANTCOMMAND:
r = read(fd, c->cmd + c->cmd_read, LINE_BUF_SIZE - c->cmd_read);
if (r == -1) return check_err(c, "read()");
if (r == 0) return conn_close(c); /* the client hung up */
c->cmd_read += r; /* we got some bytes */
c->cmd_len = scan_line_end(c->cmd, c->cmd_read); /* find the EOL */
/* yay, complete command line */
if (c->cmd_len) return do_cmd(c);
/* command line too long? */
if (c->cmd_read >= LINE_BUF_SIZE) return conn_close(c);
/* otherwise we have an incomplete line, so just keep waiting */
break;
case STATE_WANTDATA:
j = c->job;
r = read(fd, j->data + j->data_read, j->data_size - j->data_read);
if (r == -1) return check_err(c, "read()");
if (r == 0) return conn_close(c); /* the client hung up */
fprintf(stderr, "got %d data bytes\n", r);
j->data_read += r; /* we got some bytes */
/* (j->data_read > j->data_size) can't happen */
check_for_complete_job(c);
break;
case STATE_SENDWORD:
r = write(fd, c->reply+c->reply_sent, c->reply_len - c->reply_sent);
if (r == -1) return check_err(c, "write()");
if (r == 0) return conn_close(c); /* the client hung up */
c->reply_sent += r; /* we got some bytes */
/* (c->reply_sent > c->reply_len) can't happen */
if (c->reply_sent == c->reply_len) {
r = conn_update_evq(c, EV_READ | EV_PERSIST, NULL);
if (r == -1) return warn("update flags failed"), conn_close(c);
c->reply_sent = 0; /* now that we're done, reset this */
c->state = STATE_WANTCOMMAND;
}
/* otherwise we sent an incomplete reply, so just keep waiting */
break;
}
}
static void
h_accept(const int fd, const short which, struct event *ev)
{
conn c;
int cfd, flags, r;
socklen_t addrlen;
struct sockaddr addr;
addrlen = sizeof addr;
cfd = accept(fd, &addr, &addrlen);
if (cfd == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK) perror("accept()");
return;
}
flags = fcntl(cfd, F_GETFL, 0);
if (flags < 0) return perror("getting flags"), close(cfd), v();
r = fcntl(cfd, F_SETFL, flags | O_NONBLOCK);
if (r < 0) return perror("setting O_NONBLOCK"), close(cfd), v();
fprintf(stderr, "got a new connection %d\n", cfd);
c = make_conn(cfd, STATE_WANTCOMMAND);
if (!c) return warn("make_conn() failed"), close(cfd), v();
r = conn_update_evq(c, EV_READ | EV_PERSIST, (evh) h_conn);
if (r == -1) return warn("conn_update_evq() failed"), close(cfd), v();
}
int
main(int argc, char **argv)
{
/*q q;*/
int listen_socket;
struct event listen_evq;
listen_socket = make_server_socket(HOST, PORT);
if (listen_socket == -1) warn("make_server_socket()"), exit(111);
drop_root();
daemonize();
event_init();
set_sig_handlers();
event_set(&listen_evq, listen_socket, EV_READ | EV_PERSIST, (evh) h_accept, &listen_evq);
event_add(&listen_evq, NULL);
/*q = q_new();*/
event_dispatch();
return 0;
}