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/>.
27 #include "sd-daemon.h"
31 static char *user
= NULL
;
32 static int detach
= 0;
33 static char *port
= "11300";
34 static char *host_addr
;
37 nullfd(int fd
, int flags
)
42 r
= open("/dev/null", flags
);
43 if (r
!= fd
) twarn("open(\"/dev/null\")"), exit(1);
62 if (r
) return twarn("chdir");
74 su(const char *user
) {
79 pwent
= getpwnam(user
);
80 if (errno
) twarn("getpwnam(\"%s\")", user
), exit(32);
81 if (!pwent
) twarnx("getpwnam(\"%s\"): no such user", user
), exit(33);
83 r
= setgid(pwent
->pw_gid
);
84 if (r
== -1) twarn("setgid(%d \"%s\")", pwent
->pw_gid
, user
), exit(34);
86 r
= setuid(pwent
->pw_uid
);
87 if (r
== -1) twarn("setuid(%d \"%s\")", pwent
->pw_uid
, user
), exit(34);
104 sa
.sa_handler
= SIG_IGN
;
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);
120 sa
.sa_handler
= exit_cleanly
;
121 r
= sigaction(SIGTERM
, &sa
, 0);
122 if (r
== -1) twarn("sigaction(SIGTERM)"), exit(111);
126 usage(char *msg
, char *arg
)
128 if (arg
) warnx("%s: %s", msg
, arg
);
129 fprintf(stderr
, "Use: %s [OPTIONS]\n"
133 " -b DIR binlog directory (must be absolute path if used with -d)\n"
134 " -f MS fsync at most once every MS milliseconds"
135 " (use -f 0 for \"always fsync\")\n"
136 " -F never fsync (default)\n"
137 " -l ADDR listen on address (default is 0.0.0.0)\n"
138 " -p PORT listen on port (default is 11300)\n"
139 " -u USER become user and group\n"
140 " -z BYTES set the maximum job size in bytes (default is %d)\n"
141 " -s BYTES set the size of each binlog file (default is %d)\n"
142 " (will be rounded up to a multiple of 512 bytes)\n"
143 " -v show version information\n"
144 " -h show this help\n",
145 progname
, JOB_DATA_SIZE_LIMIT_DEFAULT
, BINLOG_SIZE_LIMIT_DEFAULT
);
150 parse_size_t(char *str
)
155 r
= sscanf(str
, "%zu%c", &size
, &x
);
156 if (1 != r
) usage("invalid size", str
);
161 require_arg(char *opt
, char *arg
)
163 if (!arg
) usage("option requires an argument", opt
);
168 warn_systemd_ignored_option(char *opt
, char *arg
)
170 if (sd_listen_fds(0) > 0) {
171 warnx("inherited listen fd; ignoring option: %s %s", opt
, arg
);
176 opts(int argc
, char **argv
)
180 for (i
= 1; i
< argc
; ++i
) {
181 if (argv
[i
][0] != '-') usage("unknown option", argv
[i
]);
182 if (argv
[i
][1] == 0 || argv
[i
][2] != 0) usage("unknown option",argv
[i
]);
183 switch (argv
[i
][1]) {
188 port
= require_arg("-p", argv
[++i
]);
189 warn_systemd_ignored_option("-p", argv
[i
]);
192 host_addr
= require_arg("-l", argv
[++i
]);
193 warn_systemd_ignored_option("-l", argv
[i
]);
196 job_data_size_limit
= parse_size_t(require_arg("-z",
200 binlog_size_limit
= parse_size_t(require_arg("-s", argv
[++i
]));
203 fsync_throttle_ms
= parse_size_t(require_arg("-f", argv
[++i
]));
210 user
= require_arg("-u", argv
[++i
]);
213 binlog_dir
= require_arg("-b", argv
[++i
]);
218 printf("beanstalkd %s\n", VERSION
);
221 usage("unknown option", argv
[i
]);
227 main(int argc
, char **argv
)
231 struct job binlog_jobs
= {};
236 if (detach
&& binlog_dir
) {
237 if (binlog_dir
[0] != '/') {
238 warnx("The -b option requires an absolute path when used with -d.");
239 usage("Path is not absolute", binlog_dir
);
243 r
= make_server_socket(host_addr
, port
);
244 if (r
== -1) twarnx("make_server_socket()"), exit(111);
249 /* We want to make sure that only one beanstalkd tries to use the binlog
250 * directory at a time. So acquire a lock now and never release it. */
253 if (!r
) twarnx("failed to lock binlog dir %s", binlog_dir
), exit(10);
260 binlog_jobs
.prev
= binlog_jobs
.next
= &binlog_jobs
;
261 binlog_init(&binlog_jobs
);
262 prot_replay_binlog(&binlog_jobs
);