Update comment.
[beanstalkd.git] / beanstalkd.c
blob625fef0b68cae095a501c3ab23c1ab657fc77532
1 /* beanstalk - fast, general-purpose work queue */
3 /* Copyright (C) 2007 Keith Rarick and Philotic Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <signal.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <sys/stat.h>
24 #include <sys/resource.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <pwd.h>
31 #include <event.h>
33 #include "net.h"
34 #include "util.h"
35 #include "prot.h"
36 #include "config.h"
37 #include "binlog.h"
39 static char *user = NULL;
40 static int detach = 0;
41 static int port = 11300;
42 static struct in_addr host_addr;
44 static void
45 nullfd(int fd, int flags)
47 int r;
49 close(fd);
50 r = open("/dev/null", flags);
51 if (r != fd) twarn("open(\"/dev/null\")"), exit(1);
54 static void
55 dfork()
57 pid_t p;
59 p = fork();
60 if (p == -1) exit(1);
61 if (p) exit(0);
64 static void
65 daemonize()
67 int r;
69 r = chdir("/");
70 if (r) return twarn("chdir");
72 nullfd(0, O_RDONLY);
73 nullfd(1, O_WRONLY);
74 nullfd(2, O_WRONLY);
75 umask(0);
76 dfork();
77 setsid();
78 dfork();
81 static void
82 su(const char *user) {
83 int r;
84 struct passwd *pwent;
86 errno = 0;
87 pwent = getpwnam(user);
88 if (errno) twarn("getpwnam(\"%s\")", user), exit(32);
89 if (!pwent) twarnx("getpwnam(\"%s\"): no such user", user), exit(33);
91 r = setgid(pwent->pw_gid);
92 if (r == -1) twarn("setgid(%d \"%s\")", pwent->pw_gid, user), exit(34);
94 r = setuid(pwent->pw_uid);
95 if (r == -1) twarn("setuid(%d \"%s\")", pwent->pw_uid, user), exit(34);
98 void
99 exit_cleanly(int sig)
101 binlog_shutdown();
102 exit(0);
106 static void
107 set_sig_handlers()
109 int r;
110 struct sigaction sa;
112 sa.sa_handler = SIG_IGN;
113 sa.sa_flags = 0;
114 r = sigemptyset(&sa.sa_mask);
115 if (r == -1) twarn("sigemptyset()"), exit(111);
117 r = sigaction(SIGPIPE, &sa, 0);
118 if (r == -1) twarn("sigaction(SIGPIPE)"), exit(111);
120 sa.sa_handler = enter_drain_mode;
121 r = sigaction(SIGUSR1, &sa, 0);
122 if (r == -1) twarn("sigaction(SIGUSR1)"), exit(111);
124 sa.sa_handler = exit_cleanly;
125 r = sigaction(SIGINT, &sa, 0);
126 if (r == -1) twarn("sigaction(SIGINT)"), exit(111);
129 /* This is a workaround for a mystifying workaround in libevent's epoll
130 * implementation. The epoll_init() function creates an epoll fd with space to
131 * handle RLIMIT_NOFILE - 1 fds, accompanied by the following puzzling comment:
132 * "Solaris is somewhat retarded - it's important to drop backwards
133 * compatibility when making changes. So, don't dare to put rl.rlim_cur here."
134 * This is presumably to work around a bug in Solaris, but it has the
135 * unfortunate side-effect of causing epoll_ctl() (and, therefore, event_add())
136 * to fail for a valid fd if we have hit the limit of open fds. That makes it
137 * hard to provide reasonable behavior in that situation. So, let's reduce the
138 * real value of RLIMIT_NOFILE by one, after epoll_init() has run. */
139 static void
140 nudge_fd_limit()
142 int r;
143 struct rlimit rl;
145 r = getrlimit(RLIMIT_NOFILE, &rl);
146 if (r != 0) twarn("getrlimit(RLIMIT_NOFILE)"), exit(2);
148 rl.rlim_cur--;
150 r = setrlimit(RLIMIT_NOFILE, &rl);
151 if (r != 0) twarn("setrlimit(RLIMIT_NOFILE)"), exit(2);
154 static void
155 usage(char *msg, char *arg)
157 if (arg) warnx("%s: %s", msg, arg);
158 fprintf(stderr, "Use: %s [OPTIONS]\n"
159 "\n"
160 "Options:\n"
161 " -d detach\n"
162 " -b DIR binlog directory\n"
163 " -l ADDR listen on address (default is 0.0.0.0)\n"
164 " -p PORT listen on port (default is 11300)\n"
165 " -u USER become user and group\n"
166 " -z SIZE set the maximum job size in bytes (default is %d)\n"
167 " -v show version information\n"
168 " -h show this help\n",
169 progname, JOB_DATA_SIZE_LIMIT_DEFAULT);
170 exit(arg ? 5 : 0);
173 static size_t
174 parse_size_t(char *str)
176 char r, x;
177 size_t size;
179 r = sscanf(str, "%zu%c", &size, &x);
180 if (1 != r) usage("invalid size", str);
181 return size;
184 static char *
185 require_arg(char *opt, char *arg)
187 if (!arg) usage("option requires an argument", opt);
188 return arg;
191 static int
192 parse_port(char *portstr)
194 int port;
195 char *end;
197 errno = 0;
198 port = strtol(portstr, &end, 10);
199 if (end == portstr) usage("invalid port", portstr);
200 if (end[0] != 0) usage("invalid port", portstr);
201 if (errno) usage("invalid port", portstr);
203 return port;
206 static struct in_addr
207 parse_host(char *hoststr)
209 int r;
210 struct in_addr addr;
212 r = inet_aton(hoststr, &addr);
213 if (!r) usage("invalid address", hoststr);
215 return addr;
218 static void
219 opts(int argc, char **argv)
221 int i;
223 for (i = 1; i < argc; ++i) {
224 if (argv[i][0] != '-') usage("unknown option", argv[i]);
225 if (argv[i][1] == 0 || argv[i][2] != 0) usage("unknown option",argv[i]);
226 switch (argv[i][1]) {
227 case 'd':
228 detach = 1;
229 break;
230 case 'p':
231 port = parse_port(require_arg("-p", argv[++i]));
232 break;
233 case 'l':
234 host_addr = parse_host(require_arg("-l", argv[++i]));
235 break;
236 case 'z':
237 job_data_size_limit = parse_size_t(require_arg("-z",
238 argv[++i]));
239 break;
240 case 'u':
241 user = require_arg("-u", argv[++i]);
242 break;
243 case 'b':
244 binlog_dir = require_arg("-b", argv[++i]);
245 break;
246 case 'h':
247 usage(NULL, NULL);
248 case 'v':
249 printf("beanstalkd %s\n", VERSION);
250 exit(0);
251 default:
252 usage("unknown option", argv[i]);
258 main(int argc, char **argv)
260 int r;
261 struct event_base *ev_base;
262 struct job binlog_jobs = {};
264 host_addr.s_addr = INADDR_ANY;
266 progname = argv[0];
267 opts(argc, argv);
269 job_init();
270 prot_init();
272 /* We want to make sure that only one beanstalkd tries to use the binlog
273 * directory at a time. So acquire a lock now and never release it. */
274 if (binlog_dir) {
275 r = binlog_lock();
276 if (!r) twarnx("failed to lock binlog dir %s", binlog_dir), exit(10);
279 r = make_server_socket(host_addr, port);
280 if (r == -1) twarnx("make_server_socket()"), exit(111);
282 if (user) su(user);
283 ev_base = event_init();
284 set_sig_handlers();
285 nudge_fd_limit();
287 unbrake((evh) h_accept);
289 binlog_jobs.prev = binlog_jobs.next = &binlog_jobs;
290 binlog_init(&binlog_jobs);
291 prot_replay_binlog(&binlog_jobs);
293 if (detach) {
294 daemonize();
295 event_reinit(ev_base);
298 event_dispatch();
299 binlog_shutdown();
300 twarnx("got here for some reason");
301 return 0;