-
Notifications
You must be signed in to change notification settings - Fork 1
/
beanstalkd.c
305 lines (257 loc) · 8.02 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
/* beanstalk - fast, general-purpose work queue */
/* Copyright (C) 2007 Keith Rarick and Philotic Inc.
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <event.h>
#include <limits.h>
#include "net.h"
#include "util.h"
#include "prot.h"
#include "binlog.h"
#include "group.h"
static char *user = NULL;
static int detach = 0;
static char *port = "11300";
static char *host_addr;
static void
nullfd(int fd, int flags)
{
int r;
close(fd);
r = open("/dev/null", flags);
if (r != fd) twarn("open(\"/dev/null\")"), exit(1);
}
static void
dfork()
{
pid_t p;
p = fork();
if (p == -1) exit(1);
if (p) exit(0);
}
static void
daemonize()
{
int r;
r = chdir("/");
if (r) return twarn("chdir");
nullfd(0, O_RDONLY);
nullfd(1, O_WRONLY);
nullfd(2, O_WRONLY);
umask(0);
dfork();
setsid();
dfork();
}
static void
su(const char *user) {
int r;
struct passwd *pwent;
errno = 0;
pwent = getpwnam(user);
if (errno) twarn("getpwnam(\"%s\")", user), exit(32);
if (!pwent) twarnx("getpwnam(\"%s\"): no such user", user), exit(33);
r = setgid(pwent->pw_gid);
if (r == -1) twarn("setgid(%d \"%s\")", pwent->pw_gid, user), exit(34);
r = setuid(pwent->pw_uid);
if (r == -1) twarn("setuid(%d \"%s\")", pwent->pw_uid, user), exit(34);
}
void
exit_cleanly(int sig)
{
binlog_shutdown();
exit(0);
}
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) twarn("sigemptyset()"), exit(111);
r = sigaction(SIGPIPE, &sa, 0);
if (r == -1) twarn("sigaction(SIGPIPE)"), exit(111);
sa.sa_handler = enter_drain_mode;
r = sigaction(SIGUSR1, &sa, 0);
if (r == -1) twarn("sigaction(SIGUSR1)"), exit(111);
sa.sa_handler = exit_cleanly;
r = sigaction(SIGINT, &sa, 0);
if (r == -1) twarn("sigaction(SIGINT)"), exit(111);
sa.sa_handler = exit_cleanly;
r = sigaction(SIGTERM, &sa, 0);
if (r == -1) twarn("sigaction(SIGTERM)"), exit(111);
}
/* This is a workaround for a mystifying workaround in libevent's epoll
* implementation. The epoll_init() function creates an epoll fd with space to
* handle RLIMIT_NOFILE - 1 fds, accompanied by the following puzzling comment:
* "Solaris is somewhat retarded - it's important to drop backwards
* compatibility when making changes. So, don't dare to put rl.rlim_cur here."
* This is presumably to work around a bug in Solaris, but it has the
* unfortunate side-effect of causing epoll_ctl() (and, therefore, event_add())
* to fail for a valid fd if we have hit the limit of open fds. That makes it
* hard to provide reasonable behavior in that situation. So, let's reduce the
* real value of RLIMIT_NOFILE by one, after epoll_init() has run. */
static void
nudge_fd_limit()
{
int r;
struct rlimit rl;
r = getrlimit(RLIMIT_NOFILE, &rl);
if (r != 0) twarn("getrlimit(RLIMIT_NOFILE)"), exit(2);
rl.rlim_cur--;
r = setrlimit(RLIMIT_NOFILE, &rl);
if (r != 0) twarn("setrlimit(RLIMIT_NOFILE)"), exit(2);
}
static void
usage(char *msg, char *arg)
{
if (arg) warnx("%s: %s", msg, arg);
fprintf(stderr, "Use: %s [OPTIONS]\n"
"\n"
"Options:\n"
" -d detach\n"
" -b DIR binlog directory (must be absolute path if used with -d)\n"
" -f MS fsync at most once every MS milliseconds"
" (use -f 0 for \"always fsync\")\n"
" -F never fsync (default)\n"
" -l ADDR listen on address (default is 0.0.0.0)\n"
" -p PORT listen on port (default is 11300)\n"
" -u USER become user and group\n"
" -z BYTES set the maximum job size in bytes (default is %d)\n"
" -s BYTES set the size of each binlog file (default is %d)\n"
#ifndef HAVE_POSIX_FALLOCATE
" (will be rounded up to a multiple of 512 bytes)\n"
#endif
" -v show version information\n"
" -h show this help\n",
progname, JOB_DATA_SIZE_LIMIT_DEFAULT, BINLOG_SIZE_LIMIT_DEFAULT);
exit(arg ? 5 : 0);
}
static size_t
parse_size_t(char *str)
{
char r, x;
size_t size;
r = sscanf(str, "%zu%c", &size, &x);
if (1 != r) usage("invalid size", str);
return size;
}
static char *
require_arg(char *opt, char *arg)
{
if (!arg) usage("option requires an argument", opt);
return arg;
}
static void
opts(int argc, char **argv)
{
int i;
for (i = 1; i < argc; ++i) {
if (argv[i][0] != '-') usage("unknown option", argv[i]);
if (argv[i][1] == 0 || argv[i][2] != 0) usage("unknown option",argv[i]);
switch (argv[i][1]) {
case 'd':
detach = 1;
break;
case 'p':
port = require_arg("-p", argv[++i]);
break;
case 'l':
host_addr = require_arg("-l", argv[++i]);
break;
case 'z':
job_data_size_limit = parse_size_t(require_arg("-z",
argv[++i]));
break;
case 's':
binlog_size_limit = parse_size_t(require_arg("-s", argv[++i]));
break;
case 'f':
fsync_throttle_ms = parse_size_t(require_arg("-f", argv[++i]));
enable_fsync = 1;
break;
case 'F':
enable_fsync = 0;
break;
case 'u':
user = require_arg("-u", argv[++i]);
break;
case 'b':
binlog_dir = require_arg("-b", argv[++i]);
break;
case 'h':
usage(NULL, NULL);
case 'v':
printf("beanstalkd %s\n", VERSION);
exit(0);
default:
usage("unknown option", argv[i]);
}
}
}
int
main(int argc, char **argv)
{
int r;
struct event_base *ev_base;
struct job binlog_jobs = {};
progname = argv[0];
opts(argc, argv);
if (detach && binlog_dir) {
if (binlog_dir[0] != '/') {
warnx("The -b option requires an absolute path when used with -d.");
usage("Path is not absolute", binlog_dir);
}
}
job_init();
prot_init();
/* We want to make sure that only one beanstalkd tries to use the binlog
* directory at a time. So acquire a lock now and never release it. */
if (binlog_dir) {
r = binlog_lock();
if (!r) twarnx("failed to lock binlog dir %s", binlog_dir), exit(10);
}
r = make_server_socket(host_addr, port);
if (r == -1) twarnx("make_server_socket()"), exit(111);
if (user) su(user);
ev_base = event_init();
set_sig_handlers();
nudge_fd_limit();
unbrake((evh) h_accept);
binlog_jobs.prev = binlog_jobs.next = &binlog_jobs;
binlog_init(&binlog_jobs);
prot_replay_binlog(&binlog_jobs);
if (detach) {
daemonize();
event_reinit(ev_base);
}
event_dispatch();
twarnx("event_dispatch error");
binlog_shutdown();
return 0;
}