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/>.
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>
39 static char *user
= NULL
;
40 static int detach
= 0;
41 static int port
= 11300;
42 static struct in_addr host_addr
= { INADDR_ANY
};
45 nullfd(int fd
, int flags
)
50 r
= open("/dev/null", flags
);
51 if (r
!= fd
) twarn("open(\"/dev/null\")"), exit(1);
78 su(const char *user
) {
83 pwent
= getpwnam(user
);
84 if (errno
) twarn("getpwnam(\"%s\")", user
), exit(32);
85 if (!pwent
) twarnx("getpwnam(\"%s\"): no such user", user
), exit(33);
87 r
= setgid(pwent
->pw_gid
);
88 if (r
== -1) twarn("setgid(%d \"%s\")", pwent
->pw_gid
, user
), exit(34);
90 r
= setuid(pwent
->pw_uid
);
91 if (r
== -1) twarn("setuid(%d \"%s\")", pwent
->pw_uid
, user
), exit(34);
108 sa
.sa_handler
= SIG_IGN
;
110 r
= sigemptyset(&sa
.sa_mask
);
111 if (r
== -1) twarn("sigemptyset()"), exit(111);
113 r
= sigaction(SIGPIPE
, &sa
, 0);
114 if (r
== -1) twarn("sigaction(SIGPIPE)"), exit(111);
116 sa
.sa_handler
= enter_drain_mode
;
117 r
= sigaction(SIGUSR1
, &sa
, 0);
118 if (r
== -1) twarn("sigaction(SIGUSR1)"), exit(111);
120 sa
.sa_handler
= exit_cleanly
;
121 r
= sigaction(SIGINT
, &sa
, 0);
122 if (r
== -1) twarn("sigaction(SIGINT)"), exit(111);
125 /* This is a workaround for a mystifying workaround in libevent's epoll
126 * implementation. The epoll_init() function creates an epoll fd with space to
127 * handle RLIMIT_NOFILE - 1 fds, accompanied by the following puzzling comment:
128 * "Solaris is somewhat retarded - it's important to drop backwards
129 * compatibility when making changes. So, don't dare to put rl.rlim_cur here."
130 * This is presumably to work around a bug in Solaris, but it has the
131 * unfortunate side-effect of causing epoll_ctl() (and, therefore, event_add())
132 * to fail for a valid fd if we have hit the limit of open fds. That makes it
133 * hard to provide reasonable behavior in that situation. So, let's reduce the
134 * real value of RLIMIT_NOFILE by one, after epoll_init() has run. */
141 r
= getrlimit(RLIMIT_NOFILE
, &rl
);
142 if (r
!= 0) twarn("getrlimit(RLIMIT_NOFILE)"), exit(2);
146 r
= setrlimit(RLIMIT_NOFILE
, &rl
);
147 if (r
!= 0) twarn("setrlimit(RLIMIT_NOFILE)"), exit(2);
151 usage(char *msg
, char *arg
)
153 if (arg
) warnx("%s: %s", msg
, arg
);
154 fprintf(stderr
, "Use: %s [OPTIONS]\n"
158 " -b DIR binlog directory\n"
159 " -l ADDR listen on address (default is 0.0.0.0)\n"
160 " -p PORT listen on port (default is 11300)\n"
161 " -u USER become user and group\n"
162 " -z SIZE set the maximum job size in bytes (default is %d)\n"
163 " -v show version information\n"
164 " -h show this help\n",
165 progname
, JOB_DATA_SIZE_LIMIT_DEFAULT
);
170 parse_size_t(char *str
)
175 r
= sscanf(str
, "%zu%c", &size
, &x
);
176 if (1 != r
) usage("invalid size", str
);
181 require_arg(char *opt
, char *arg
)
183 if (!arg
) usage("option requires an argument", opt
);
188 parse_port(char *portstr
)
194 port
= strtol(portstr
, &end
, 10);
195 if (end
== portstr
) usage("invalid port", portstr
);
196 if (end
[0] != 0) usage("invalid port", portstr
);
197 if (errno
) usage("invalid port", portstr
);
202 static struct in_addr
203 parse_host(char *hoststr
)
208 r
= inet_aton(hoststr
, &addr
);
209 if (!r
) usage("invalid address", hoststr
);
215 opts(int argc
, char **argv
)
219 for (i
= 1; i
< argc
; ++i
) {
220 if (argv
[i
][0] != '-') usage("unknown option", argv
[i
]);
221 if (argv
[i
][1] == 0 || argv
[i
][2] != 0) usage("unknown option",argv
[i
]);
222 switch (argv
[i
][1]) {
227 port
= parse_port(require_arg("-p", argv
[++i
]));
230 host_addr
= parse_host(require_arg("-l", argv
[++i
]));
233 job_data_size_limit
= parse_size_t(require_arg("-z",
237 user
= require_arg("-u", argv
[++i
]);
240 binlog_dir
= require_arg("-b", argv
[++i
]);
245 printf("beanstalkd %s\n", VERSION
);
248 usage("unknown option", argv
[i
]);
254 main(int argc
, char **argv
)
257 struct event_base
*ev_base
;
265 /* We want to make sure that only one beanstalkd tries to use the binlog
266 * directory at a time. So acquire a lock now and never release it. */
269 if (!r
) twarnx("failed to lock binlog dir %s", binlog_dir
), exit(10);
272 r
= make_server_socket(host_addr
, port
);
273 if (r
== -1) twarnx("make_server_socket()"), exit(111);
276 ev_base
= event_init();
280 unbrake((evh
) h_accept
);
281 prot_replay_binlog();
286 event_reinit(ev_base
);
291 twarnx("got here for some reason");