80
82
#define GEARMAND_LOG_REOPEN_TIME 60
81
83
#define GEARMAND_LISTEN_BACKLOG 32
85
#include "util/error.h"
86
#include "util/pidfile.h"
88
using namespace gearman_util;
90
struct gearmand_log_info_st
88
} gearmand_log_info_st;
96
gearmand_log_info_st() :
90
104
static gearmand_st *_gearmand;
92
106
static bool _set_fdlimit(rlim_t fds);
93
static bool _pid_write(const char *pid_file);
94
static void _pid_delete(const char *pid_file);
95
107
static bool _switch_user(const char *user);
96
109
static bool _set_signals(void);
97
111
static void _shutdown_handler(int signal_arg);
98
112
static void _log(const char *line, gearman_verbose_t verbose, void *context);
192
200
#ifdef HAVE_LIBMEMCACHED
193
201
if (gearman_server_queue_libmemcached_conf(&conf) != GEARMAN_SUCCESS)
195
fprintf(stderr, "gearmand: gearman_queue_libmemcached_conf: %s\n",
196
gearman_conf_error(&conf));
203
error::message("gearman_queue_libmemcached_conf", gearman_conf_error(&conf));
200
207
#ifdef HAVE_LIBTOKYOCABINET
201
208
if (gearman_server_queue_libtokyocabinet_conf(&conf) != GEARMAN_SUCCESS)
203
fprintf(stderr, "gearmand: gearman_queue_libtokyocabinet_conf: %s\n",
204
gearman_conf_error(&conf));
210
error::message("gearman_queue_libtokyocabinet_conf", gearman_conf_error(&conf));
218
223
#ifdef HAVE_LIBPQ
219
224
if (gearman_server_queue_libpq_conf(&conf) != GEARMAN_SUCCESS)
221
fprintf(stderr, "gearmand: gearman_queue_libpq_conf: %s\n",
222
gearman_conf_error(&conf));
226
error::message("gearman_queue_libpq_conf", gearman_conf_error(&conf));
227
231
if (gearmand_protocol_http_conf(&conf) != GEARMAN_SUCCESS)
229
fprintf(stderr, "gearmand: gearman_protocol_http_conf: %s\n",
230
gearman_conf_error(&conf));
233
error::message("gearman_protocol_http_conf", gearman_conf_error(&conf));
244
247
/* Check for option values that were given. */
245
248
while (gearman_conf_module_value(&module, &name, &value))
247
if (!strcmp(name, "backlog"))
250
if (not strcmp(name, "backlog"))
248
252
backlog= atoi(value);
249
else if (!strcmp(name, "daemon"))
254
else if (not strcmp(name, "daemon"))
254
fprintf(stderr, "gearmand: fork:%d\n", errno);
259
error::perror("fork");
264
269
if (setsid() == -1)
266
fprintf(stderr, "gearmand: setsid:%d\n", errno);
271
error::perror("setsid");
270
275
close_stdio= true;
272
else if (!strcmp(name, "file-descriptors"))
273
fds= (rlim_t)atoi(value);
274
else if (!strcmp(name, "help"))
277
else if (not strcmp(name, "file-descriptors"))
279
fds= static_cast<rlim_t>(atoi(value));
281
else if (not strcmp(name, "help"))
276
283
printf("\ngearmand %s - %s\n\n", gearman_version(), gearman_bugreport());
277
284
printf("usage: %s [OPTIONS]\n", argv[0]);
278
285
gearman_conf_usage(&conf);
281
else if (!strcmp(name, "job-retries"))
282
job_retries= (uint8_t)atoi(value);
283
else if (!strcmp(name, "log-file"))
288
else if (not strcmp(name, "job-retries"))
290
job_retries= static_cast<uint8_t>(atoi(value));
292
else if (not strcmp(name, "log-file"))
284
294
log_info.file= value;
285
else if (!strcmp(name, "listen"))
296
else if (not strcmp(name, "listen"))
287
else if (!strcmp(name, "port"))
288
port= (in_port_t)atoi(value);
289
else if (!strcmp(name, "pid-file"))
300
else if (not strcmp(name, "port"))
302
port= static_cast<in_port_t>(atoi(value));
304
else if (not strcmp(name, "pid-file"))
291
else if (!strcmp(name, "protocol"))
308
else if (not strcmp(name, "protocol"))
293
else if (!strcmp(name, "queue-type"))
312
else if (not strcmp(name, "queue-type"))
294
314
queue_type= value;
295
else if (!strcmp(name, "threads"))
296
threads= (uint32_t)atoi(value);
297
else if (!strcmp(name, "user"))
316
else if (not strcmp(name, "threads"))
318
threads= static_cast<uint32_t>(atoi(value));
320
else if (not strcmp(name, "user"))
299
else if (!strcmp(name, "verbose"))
324
else if (not strcmp(name, "verbose"))
301
else if (!strcmp(name, "round-robin"))
328
else if (not strcmp(name, "round-robin"))
303
else if (!strcmp(name, "version"))
332
else if (not strcmp(name, "version"))
304
334
printf("\ngearmand %s - %s\n", gearman_version(), gearman_bugreport());
305
else if (!strcmp(name, "worker-wakeup"))
306
worker_wakeup= (uint8_t)atoi(value);
336
else if (not strcmp(name, "worker-wakeup"))
338
worker_wakeup= static_cast<uint8_t>(atoi(value));
309
fprintf(stderr, "gearmand: Unknown option:%s\n", name);
342
error::message("Unknown option", name);
320
353
if (dup2(fd, STDIN_FILENO) == -1)
322
fprintf(stderr, "gearmand: dup2:%d\n", errno);
355
error::perror("dup2");
326
359
if (dup2(fd, STDOUT_FILENO) == -1)
328
fprintf(stderr, "gearmand: dup2:%d\n", errno);
361
error::perror("dup2");
332
365
if (dup2(fd, STDERR_FILENO) == -1)
334
fprintf(stderr, "gearmand: dup2:%d\n", errno);
367
error::perror("dup2");
356
388
gearmand_set_threads(_gearmand, threads);
357
389
gearmand_set_job_retries(_gearmand, job_retries);
358
390
gearmand_set_worker_wakeup(_gearmand, worker_wakeup);
359
gearmand_set_log_fn(_gearmand, _log, &log_info, verbose);
391
gearmand_set_log_fn(_gearmand, _log, &log_info, static_cast<gearman_verbose_t>(verbose));
360
392
gearmand_set_round_robin(_gearmand, round_robin);
362
394
if (queue_type != NULL)
364
396
#ifdef HAVE_LIBDRIZZLE
365
if (!strcmp(queue_type, "libdrizzle"))
397
if (not strcmp(queue_type, "libdrizzle"))
367
399
ret= gearmand_queue_libdrizzle_init(_gearmand, &conf);
368
400
if (ret != GEARMAN_SUCCESS)
435
467
if (queue_type != NULL)
437
469
#ifdef HAVE_LIBDRIZZLE
438
if (!strcmp(queue_type, "libdrizzle"))
470
if (not strcmp(queue_type, "libdrizzle"))
439
471
gearmand_queue_libdrizzle_deinit(_gearmand);
441
473
#ifdef HAVE_LIBMEMCACHED
442
if (!strcmp(queue_type, "libmemcached"))
474
if (not strcmp(queue_type, "libmemcached"))
443
475
gearmand_queue_libmemcached_deinit(_gearmand);
445
477
#ifdef HAVE_LIBSQLITE3
446
if (!strcmp(queue_type, "libsqlite3"))
478
if (not strcmp(queue_type, "libsqlite3"))
447
479
gearmand_queue_libsqlite3_deinit(_gearmand);
449
481
#ifdef HAVE_LIBPQ
450
if (!strcmp(queue_type, "libpq"))
482
if (not strcmp(queue_type, "libpq"))
451
483
gearmand_queue_libpq_deinit(_gearmand);
453
485
#ifdef HAVE_LIBTOKYOCABINET
454
if (!strcmp(queue_type, "libtokyocabinet"))
486
if (not strcmp(queue_type, "libtokyocabinet"))
455
487
gearmand_queue_libtokyocabinet_deinit(_gearmand);
496
524
if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
498
fprintf(stderr, "gearmand: Failed to set limit for the number of file "
499
"descriptors (%d). Try running as root or giving a "
500
"smaller value to -f.\n",
508
static bool _pid_write(const char *pid_file)
512
f= fopen(pid_file, "w");
515
fprintf(stderr, "gearmand: Could not open pid file for writing: %s (%d)\n",
520
fprintf(f, "%" PRId64 "\n", (int64_t)getpid());
524
fprintf(stderr, "gearmand: Could not close the pid file: %s (%d)\n",
532
static void _pid_delete(const char *pid_file)
534
if (unlink(pid_file) == -1)
536
fprintf(stderr, "gearmand: Could not remove the pid file: %s (%d)\n",
526
error::perror("Failed to set limit for the number of file "
527
"descriptors. Try running as root or giving a "
528
"smaller value to -f.");
541
535
static bool _switch_user(const char *user)
547
541
if (user == NULL || user[0] == 0)
550
"gearmand: Must specify '-u root' if you want to run as root\n");
543
error::message("Must specify '-u root' if you want to run as root");
554
547
pw= getpwnam(user);
557
fprintf(stderr, "gearmand: Could not find user '%s'\n", user);
550
error::message("Could not find user", user);
561
554
if (setgid(pw->pw_gid) == -1 || setuid(pw->pw_uid) == -1)
563
fprintf(stderr, "gearmand: Could not switch to user '%s'\n", user);
556
error::message("Could not switch to user", user);
567
560
else if (user != NULL)
569
fprintf(stderr, "gearmand: Must be root to switch users\n");
562
error::message("Must be root to switch users.");
576
570
static bool _set_signals(void)
578
572
struct sigaction sa;
583
577
if (sigemptyset(&sa.sa_mask) == -1 ||
584
578
sigaction(SIGPIPE, &sa, 0) == -1)
586
fprintf(stderr, "gearmand: Could not set SIGPIPE handler (%d)\n", errno);
580
error::perror("Could not set SIGPIPE handler.");
590
584
sa.sa_handler= _shutdown_handler;
591
585
if (sigaction(SIGTERM, &sa, 0) == -1)
593
fprintf(stderr, "gearmand: Could not set SIGTERM handler (%d)\n", errno);
587
error::perror("Could not set SIGTERM handler.");
597
591
if (sigaction(SIGINT, &sa, 0) == -1)
599
fprintf(stderr, "gearmand: Could not set SIGINT handler (%d)\n", errno);
593
error::perror("Could not set SIGINT handler.");
603
597
if (sigaction(SIGUSR1, &sa, 0) == -1)
605
fprintf(stderr, "gearmand: Could not set SIGUSR1 handler (%d)\n", errno);
599
error::perror("Could not set SIGUSR1 handler.");
612
607
static void _shutdown_handler(int signal_arg)