Use z rather than the obsolete Z modifier.
[beanstalkd.git] / beanstalkd.c
blob8a1219b14bbdad16e0aa60cfe968f56d741bdd17
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>
32 #include "net.h"
33 #include "util.h"
34 #include "prot.h"
36 static char *user = NULL;
37 static int detach = 0;
38 static int port = 11300;
39 static struct in_addr host_addr = { INADDR_ANY };
41 static void
42 nullfd(int fd, int flags)
44 int r;
46 close(fd);
47 r = open("/dev/null", flags);
48 if (r != fd) twarn("open(\"/dev/null\")"), exit(1);
51 static void
52 dfork()
54 pid_t p;
56 p = fork();
57 if (p == -1) exit(1);
58 if (p) exit(0);
61 static void
62 daemonize()
64 chdir("/");
65 nullfd(0, O_RDONLY);
66 nullfd(1, O_WRONLY);
67 nullfd(2, O_WRONLY);
68 umask(0);
69 dfork();
70 setsid();
71 dfork();
74 static void
75 su(const char *user) {
76 int r;
77 struct passwd *pwent;
79 errno = 0;
80 pwent = getpwnam(user);
81 if (errno) twarn("getpwnam(\"%s\")", user), exit(32);
82 if (!pwent) twarnx("getpwnam(\"%s\"): no such user", user), exit(33);
84 r = setgid(pwent->pw_gid);
85 if (r == -1) twarn("setgid(%d \"%s\")", pwent->pw_gid, user), exit(34);
87 r = setuid(pwent->pw_uid);
88 if (r == -1) twarn("setuid(%d \"%s\")", pwent->pw_uid, user), exit(34);
91 void
92 exit_cleanly(int sig)
94 exit(0);
98 static void
99 set_sig_handlers()
101 int r;
102 struct sigaction sa;
104 sa.sa_handler = SIG_IGN;
105 sa.sa_flags = 0;
106 r = sigemptyset(&sa.sa_mask);
107 if (r == -1) twarn("sigemptyset()"), exit(111);
109 r = sigaction(SIGPIPE, &sa, 0);
110 if (r == -1) twarn("sigaction(SIGPIPE)"), exit(111);
112 sa.sa_handler = enter_drain_mode;
113 r = sigaction(SIGUSR1, &sa, 0);
114 if (r == -1) twarn("sigaction(SIGUSR1)"), exit(111);
116 sa.sa_handler = exit_cleanly;
117 r = sigaction(SIGINT, &sa, 0);
118 if (r == -1) twarn("sigaction(SIGINT)"), exit(111);
121 /* This is a workaround for a mystifying workaround in libevent's epoll
122 * implementation. The epoll_init() function creates an epoll fd with space to
123 * handle RLIMIT_NOFILE - 1 fds, accompanied by the following puzzling comment:
124 * "Solaris is somewhat retarded - it's important to drop backwards
125 * compatibility when making changes. So, don't dare to put rl.rlim_cur here."
126 * This is presumably to work around a bug in Solaris, but it has the
127 * unfortunate side-effect of causing epoll_ctl() (and, therefore, event_add())
128 * to fail for a valid fd if we have hit the limit of open fds. That makes it
129 * hard to provide reasonable behavior in that situation. So, let's reduce the
130 * real value of RLIMIT_NOFILE by one, after epoll_init() has run. */
131 static void
132 nudge_fd_limit()
134 int r;
135 struct rlimit rl;
137 r = getrlimit(RLIMIT_NOFILE, &rl);
138 if (r != 0) twarn("getrlimit(RLIMIT_NOFILE)"), exit(2);
140 rl.rlim_cur--;
142 r = setrlimit(RLIMIT_NOFILE, &rl);
143 if (r != 0) twarn("setrlimit(RLIMIT_NOFILE)"), exit(2);
146 static void
147 usage(char *msg, char *arg)
149 if (arg) warnx("%s: %s", msg, arg);
150 fprintf(stderr, "Use: %s [-d] [-l ADDR] [-p PORT] [-u USER] [-h]\n"
151 "\n"
152 "Options:\n"
153 " -d detach\n"
154 " -l ADDR listen on address (default is 0.0.0.0)\n"
155 " -p PORT listen on port (default is 11300)\n"
156 " -u USER become user and group\n"
157 " -h show this help\n",
158 progname);
159 exit(arg ? 5 : 0);
162 static int
163 parse_port(char *portstr)
165 int port;
166 char *end;
168 errno = 0;
169 port = strtol(portstr, &end, 10);
170 if (end == portstr) usage("invalid port", portstr);
171 if (end[0] != 0) usage("invalid port", portstr);
172 if (errno) usage("invalid port", portstr);
174 return port;
177 static struct in_addr
178 parse_host(char *hoststr)
180 int r;
181 struct in_addr addr;
183 r = inet_aton(hoststr, &addr);
184 if (!r) usage("invalid address", hoststr);
186 return addr;
189 static void
190 opts(int argc, char **argv)
192 int i;
194 for (i = 1; i < argc; ++i) {
195 if (argv[i][0] != '-') usage("unknown option", argv[i]);
196 if (argv[i][1] == 0 || argv[i][2] != 0) usage("unknown option",argv[i]);
197 switch (argv[i][1]) {
198 case 'd':
199 detach = 1;
200 break;
201 case 'p':
202 port = parse_port(argv[++i]);
203 break;
204 case 'l':
205 host_addr = parse_host(argv[++i]);
206 break;
207 case 'u':
208 user = argv[++i];
209 break;
210 case 'h':
211 usage(NULL, NULL);
212 default:
213 usage("unknown option", argv[i]);
219 main(int argc, char **argv)
221 int r;
223 progname = argv[0];
224 opts(argc, argv);
226 prot_init();
228 r = make_server_socket(host_addr, port);
229 if (r == -1) twarnx("make_server_socket()"), exit(111);
231 if (user) su(user);
232 if (detach) daemonize();
233 event_init();
234 set_sig_handlers();
235 nudge_fd_limit();
237 unbrake((evh) h_accept);
239 event_dispatch();
240 twarnx("got here for some reason");
241 return 0;