forked from beanstalkd/beanstalkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
205 lines (179 loc) · 5.41 KB
/
main.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
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <fcntl.h>
#include "sd-daemon.h"
#include "dat.h"
static char *user = NULL;
static char *port = "11300";
static char *host_addr;
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);
}
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);
}
static void
usage(char *msg, char *arg)
{
if (arg) warnx("%s: %s", msg, arg);
fprintf(stderr, "Use: %s [OPTIONS]\n"
"\n"
"Options:\n"
" -b DIR wal directory\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 wal file (default is %d)\n"
" (will be rounded up to a multiple of 512 bytes)\n"
" -c compact the binlog (default)\n"
" -n do not compact the binlog\n"
" -v show version information\n"
" -V increase verbosity\n"
" -h show this help\n",
progname, JOB_DATA_SIZE_LIMIT_DEFAULT, Filesizedef);
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
warn_systemd_ignored_option(char *opt, char *arg)
{
if (sd_listen_fds(0) > 0) {
warnx("inherited listen fd; ignoring option: %s %s", opt, arg);
}
}
static void
opts(int argc, char **argv, Wal *w)
{
int i;
int64 ms;
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 'p':
port = require_arg("-p", argv[++i]);
warn_systemd_ignored_option("-p", argv[i]);
break;
case 'l':
host_addr = require_arg("-l", argv[++i]);
warn_systemd_ignored_option("-l", argv[i]);
break;
case 'z':
job_data_size_limit = parse_size_t(require_arg("-z",
argv[++i]));
break;
case 's':
w->filesz = parse_size_t(require_arg("-s", argv[++i]));
break;
case 'c':
w->nocomp = 0;
break;
case 'n':
w->nocomp = 1;
break;
case 'f':
ms = (int64)parse_size_t(require_arg("-f", argv[++i]));
w->syncrate = ms * 1000000;
w->wantsync = 1;
break;
case 'F':
w->wantsync = 0;
break;
case 'u':
user = require_arg("-u", argv[++i]);
break;
case 'b':
w->dir = require_arg("-b", argv[++i]);
w->use = 1;
break;
case 'h':
usage(NULL, NULL);
case 'v':
printf("beanstalkd %s\n", version);
exit(0);
case 'V':
verbose++;
break;
default:
usage("unknown option", argv[i]);
}
}
}
int
main(int argc, char **argv)
{
int r;
Srv s = {};
s.wal.filesz = Filesizedef;
struct job list = {};
progname = argv[0];
opts(argc, argv, &s.wal);
if (verbose) {
printf("pid %d\n", getpid());
}
r = make_server_socket(host_addr, port);
if (r == -1) twarnx("make_server_socket()"), exit(111);
s.sock.fd = r;
prot_init();
if (user) su(user);
set_sig_handlers();
if (s.wal.use) {
// We want to make sure that only one beanstalkd tries
// to use the wal directory at a time. So acquire a lock
// now and never release it.
if (!waldirlock(&s.wal)) {
twarnx("failed to lock wal dir %s", s.wal.dir);
exit(10);
}
list.prev = list.next = &list;
walinit(&s.wal, &list);
prot_replay(&s, &list);
}
srv(&s);
return 0;
}