2013-06-26 11:18:32 +02:00
|
|
|
/*
|
2016-01-04 12:58:51 +01:00
|
|
|
* Copyright (c) 2013-2016 Joris Vink <joris@coders.se>
|
2013-06-26 11:18:32 +02:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2015-06-14 16:44:37 +02:00
|
|
|
#include <sys/param.h>
|
2013-06-26 16:37:22 +02:00
|
|
|
#include <sys/shm.h>
|
2013-06-26 11:18:32 +02:00
|
|
|
#include <sys/wait.h>
|
2014-07-31 09:14:03 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2015-06-22 21:13:32 +02:00
|
|
|
#include <sys/socket.h>
|
2013-06-26 11:18:32 +02:00
|
|
|
|
2015-04-20 15:17:42 +02:00
|
|
|
#include <fcntl.h>
|
2013-06-26 11:18:32 +02:00
|
|
|
#include <grp.h>
|
2013-07-06 20:55:22 +02:00
|
|
|
#include <pwd.h>
|
2013-06-26 11:18:32 +02:00
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include "kore.h"
|
2015-11-27 16:22:50 +01:00
|
|
|
|
|
|
|
#if !defined(KORE_NO_HTTP)
|
2013-06-26 11:18:32 +02:00
|
|
|
#include "http.h"
|
2015-11-27 16:22:50 +01:00
|
|
|
#endif
|
2013-06-26 11:18:32 +02:00
|
|
|
|
2014-03-30 23:54:35 +02:00
|
|
|
#if defined(KORE_USE_PGSQL)
|
2014-07-03 22:14:46 +02:00
|
|
|
#include "pgsql.h"
|
2014-03-30 23:54:35 +02:00
|
|
|
#endif
|
|
|
|
|
2014-06-28 16:17:18 +02:00
|
|
|
#if defined(KORE_USE_TASKS)
|
2014-07-03 22:14:46 +02:00
|
|
|
#include "tasks.h"
|
2014-06-28 16:17:18 +02:00
|
|
|
#endif
|
|
|
|
|
2017-01-12 23:38:51 +01:00
|
|
|
#if defined(KORE_USE_PYTHON)
|
|
|
|
#include "python_api.h"
|
|
|
|
#endif
|
|
|
|
|
2013-06-27 00:22:48 +02:00
|
|
|
#if defined(WORKER_DEBUG)
|
|
|
|
#define worker_debug(fmt, ...) printf(fmt, ##__VA_ARGS__)
|
2013-06-26 16:37:22 +02:00
|
|
|
#else
|
2013-06-27 00:22:48 +02:00
|
|
|
#define worker_debug(fmt, ...)
|
2013-06-26 16:37:22 +02:00
|
|
|
#endif
|
|
|
|
|
2016-01-31 16:47:00 +01:00
|
|
|
#if !defined(WAIT_ANY)
|
|
|
|
#define WAIT_ANY (-1)
|
|
|
|
#endif
|
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
#define WORKER_LOCK_TIMEOUT 500
|
|
|
|
|
2013-07-15 11:24:49 +02:00
|
|
|
#define WORKER(id) \
|
|
|
|
(struct kore_worker *)((u_int8_t *)kore_workers + \
|
|
|
|
(sizeof(struct kore_worker) * id))
|
2013-06-27 00:22:48 +02:00
|
|
|
|
|
|
|
struct wlock {
|
2014-07-30 15:20:09 +02:00
|
|
|
volatile int lock;
|
|
|
|
pid_t current;
|
2013-06-27 00:22:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static int worker_trylock(void);
|
2014-07-30 15:20:09 +02:00
|
|
|
static void worker_unlock(void);
|
2013-06-27 00:22:48 +02:00
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
static inline int kore_worker_acceptlock_obtain(void);
|
|
|
|
static inline void kore_worker_acceptlock_release(void);
|
2013-06-26 16:37:22 +02:00
|
|
|
|
2013-06-27 00:22:48 +02:00
|
|
|
static struct kore_worker *kore_workers;
|
2013-06-26 16:37:22 +02:00
|
|
|
static int shm_accept_key;
|
2013-06-27 08:43:07 +02:00
|
|
|
static struct wlock *accept_lock;
|
2013-06-26 11:18:32 +02:00
|
|
|
|
2013-06-26 16:37:22 +02:00
|
|
|
extern volatile sig_atomic_t sig_recv;
|
|
|
|
struct kore_worker *worker = NULL;
|
2015-04-27 10:36:33 +02:00
|
|
|
u_int8_t worker_set_affinity = 1;
|
2015-05-18 12:20:28 +02:00
|
|
|
u_int32_t worker_accept_threshold = 0;
|
2014-07-31 09:14:03 +02:00
|
|
|
u_int32_t worker_rlimit_nofiles = 1024;
|
2013-06-26 16:37:22 +02:00
|
|
|
u_int32_t worker_max_connections = 250;
|
2013-06-27 00:22:48 +02:00
|
|
|
u_int32_t worker_active_connections = 0;
|
2013-06-26 11:18:32 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
kore_worker_init(void)
|
|
|
|
{
|
2013-06-27 00:22:48 +02:00
|
|
|
size_t len;
|
2013-06-26 11:18:32 +02:00
|
|
|
u_int16_t i, cpu;
|
|
|
|
|
|
|
|
if (worker_count == 0)
|
2014-07-31 13:59:42 +02:00
|
|
|
worker_count = 1;
|
2013-06-26 11:18:32 +02:00
|
|
|
|
2016-06-08 13:55:14 +02:00
|
|
|
#if !defined(KORE_NO_TLS)
|
|
|
|
/* account for the key manager. */
|
|
|
|
worker_count += 1;
|
|
|
|
#endif
|
|
|
|
|
2013-06-27 00:22:48 +02:00
|
|
|
len = sizeof(*accept_lock) +
|
|
|
|
(sizeof(struct kore_worker) * worker_count);
|
2013-10-14 11:26:11 +02:00
|
|
|
|
2015-04-28 10:20:36 +02:00
|
|
|
shm_accept_key = shmget(IPC_PRIVATE, len, IPC_CREAT | IPC_EXCL | 0700);
|
|
|
|
if (shm_accept_key == -1)
|
|
|
|
fatal("kore_worker_init(): shmget() %s", errno_s);
|
2015-03-25 19:42:24 +01:00
|
|
|
if ((accept_lock = shmat(shm_accept_key, NULL, 0)) == (void *)-1)
|
2013-06-26 16:37:22 +02:00
|
|
|
fatal("kore_worker_init(): shmat() %s", errno_s);
|
|
|
|
|
2013-06-27 00:22:48 +02:00
|
|
|
accept_lock->lock = 0;
|
|
|
|
accept_lock->current = 0;
|
|
|
|
|
2013-07-15 11:24:49 +02:00
|
|
|
kore_workers = (struct kore_worker *)((u_int8_t *)accept_lock +
|
|
|
|
sizeof(*accept_lock));
|
2013-06-27 00:22:48 +02:00
|
|
|
memset(kore_workers, 0, sizeof(struct kore_worker) * worker_count);
|
2013-06-26 16:37:22 +02:00
|
|
|
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_debug("kore_worker_init(): system has %d cpu's", cpu_count);
|
|
|
|
kore_debug("kore_worker_init(): starting %d workers", worker_count);
|
2015-01-19 15:26:53 +01:00
|
|
|
|
|
|
|
if (worker_count > cpu_count) {
|
2016-02-12 08:57:58 +01:00
|
|
|
kore_debug("kore_worker_init(): more workers than cpu's");
|
2015-01-19 15:26:53 +01:00
|
|
|
}
|
2013-06-26 11:18:32 +02:00
|
|
|
|
|
|
|
cpu = 0;
|
|
|
|
for (i = 0; i < worker_count; i++) {
|
2013-06-27 00:22:48 +02:00
|
|
|
kore_worker_spawn(i, cpu++);
|
2013-06-26 11:18:32 +02:00
|
|
|
if (cpu == cpu_count)
|
|
|
|
cpu = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-27 00:22:48 +02:00
|
|
|
kore_worker_spawn(u_int16_t id, u_int16_t cpu)
|
2013-06-26 11:18:32 +02:00
|
|
|
{
|
|
|
|
struct kore_worker *kw;
|
|
|
|
|
2013-06-27 00:22:48 +02:00
|
|
|
kw = WORKER(id);
|
|
|
|
kw->id = id;
|
2013-06-26 11:18:32 +02:00
|
|
|
kw->cpu = cpu;
|
2013-07-07 14:48:32 +02:00
|
|
|
kw->has_lock = 0;
|
|
|
|
kw->active_hdlr = NULL;
|
2013-06-27 08:43:07 +02:00
|
|
|
|
2015-06-22 21:13:32 +02:00
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, kw->pipe) == -1)
|
|
|
|
fatal("socketpair(): %s", errno_s);
|
|
|
|
|
2015-06-22 22:11:03 +02:00
|
|
|
if (!kore_connection_nonblock(kw->pipe[0], 0) ||
|
|
|
|
!kore_connection_nonblock(kw->pipe[1], 0))
|
2015-06-22 21:13:32 +02:00
|
|
|
fatal("could not set pipe fds to nonblocking: %s", errno_s);
|
|
|
|
|
2013-07-07 14:48:32 +02:00
|
|
|
kw->pid = fork();
|
2013-06-26 11:18:32 +02:00
|
|
|
if (kw->pid == -1)
|
|
|
|
fatal("could not spawn worker child: %s", errno_s);
|
|
|
|
|
|
|
|
if (kw->pid == 0) {
|
|
|
|
kw->pid = getpid();
|
|
|
|
kore_worker_entry(kw);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-22 21:13:32 +02:00
|
|
|
struct kore_worker *
|
|
|
|
kore_worker_data(u_int8_t id)
|
|
|
|
{
|
|
|
|
if (id >= worker_count)
|
|
|
|
fatal("id %u too large for worker count", id);
|
|
|
|
|
|
|
|
return (WORKER(id));
|
|
|
|
}
|
|
|
|
|
2013-06-26 16:37:22 +02:00
|
|
|
void
|
|
|
|
kore_worker_shutdown(void)
|
|
|
|
{
|
2013-06-27 00:22:48 +02:00
|
|
|
struct kore_worker *kw;
|
|
|
|
u_int16_t id, done;
|
|
|
|
|
2013-06-26 16:37:22 +02:00
|
|
|
kore_log(LOG_NOTICE, "waiting for workers to drain and shutdown");
|
2013-06-27 00:22:48 +02:00
|
|
|
for (;;) {
|
|
|
|
done = 0;
|
|
|
|
for (id = 0; id < worker_count; id++) {
|
|
|
|
kw = WORKER(id);
|
|
|
|
if (kw->pid != 0)
|
|
|
|
kore_worker_wait(1);
|
|
|
|
else
|
|
|
|
done++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (done == worker_count)
|
|
|
|
break;
|
|
|
|
}
|
2013-06-26 16:37:22 +02:00
|
|
|
|
|
|
|
if (shmctl(shm_accept_key, IPC_RMID, NULL) == -1) {
|
|
|
|
kore_log(LOG_NOTICE,
|
|
|
|
"failed to deleted shm segment: %s", errno_s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_worker_dispatch_signal(int sig)
|
|
|
|
{
|
2013-06-27 00:22:48 +02:00
|
|
|
u_int16_t id;
|
2013-06-26 16:37:22 +02:00
|
|
|
struct kore_worker *kw;
|
|
|
|
|
2013-06-27 00:22:48 +02:00
|
|
|
for (id = 0; id < worker_count; id++) {
|
|
|
|
kw = WORKER(id);
|
2015-01-19 15:26:53 +01:00
|
|
|
if (kill(kw->pid, sig) == -1) {
|
2013-06-26 16:37:22 +02:00
|
|
|
kore_debug("kill(%d, %d): %s", kw->pid, sig, errno_s);
|
2015-01-19 15:26:53 +01:00
|
|
|
}
|
2013-06-26 16:37:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 11:18:32 +02:00
|
|
|
void
|
2016-06-08 13:55:14 +02:00
|
|
|
kore_worker_privdrop(void)
|
2013-06-26 11:18:32 +02:00
|
|
|
{
|
2016-04-28 03:18:21 +02:00
|
|
|
rlim_t fd;
|
2014-07-31 09:14:03 +02:00
|
|
|
struct rlimit rl;
|
2015-05-18 21:34:39 +02:00
|
|
|
struct passwd *pw = NULL;
|
2013-06-26 11:18:32 +02:00
|
|
|
|
2015-05-18 21:34:39 +02:00
|
|
|
/* Must happen before chroot. */
|
|
|
|
if (skip_runas == 0) {
|
|
|
|
pw = getpwnam(runas_user);
|
|
|
|
if (pw == NULL) {
|
|
|
|
fatal("cannot getpwnam(\"%s\") runas user: %s",
|
|
|
|
runas_user, errno_s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-18 00:42:57 +01:00
|
|
|
if (skip_chroot == 0) {
|
2015-05-20 11:36:02 +02:00
|
|
|
if (chroot(chroot_path) == -1) {
|
|
|
|
fatal("cannot chroot(\"%s\"): %s",
|
|
|
|
chroot_path, errno_s);
|
|
|
|
}
|
|
|
|
|
2013-11-18 00:42:57 +01:00
|
|
|
if (chdir("/") == -1)
|
2015-05-18 21:34:39 +02:00
|
|
|
fatal("cannot chdir(\"/\"): %s", errno_s);
|
2013-11-18 00:42:57 +01:00
|
|
|
}
|
|
|
|
|
2015-04-20 15:17:42 +02:00
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rl) == -1) {
|
|
|
|
kore_log(LOG_WARNING, "getrlimit(RLIMIT_NOFILE): %s", errno_s);
|
|
|
|
} else {
|
|
|
|
for (fd = 0; fd < rl.rlim_cur; fd++) {
|
|
|
|
if (fcntl(fd, F_GETFD, NULL) != -1) {
|
|
|
|
worker_rlimit_nofiles++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 10:06:18 +02:00
|
|
|
rl.rlim_cur = worker_rlimit_nofiles;
|
|
|
|
rl.rlim_max = worker_rlimit_nofiles;
|
|
|
|
if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
|
|
|
|
kore_log(LOG_ERR, "setrlimit(RLIMIT_NOFILE, %d): %s",
|
|
|
|
worker_rlimit_nofiles, errno_s);
|
|
|
|
}
|
|
|
|
|
2015-05-18 21:34:39 +02:00
|
|
|
if (skip_runas == 0) {
|
2013-11-18 00:42:57 +01:00
|
|
|
if (setgroups(1, &pw->pw_gid) ||
|
2015-06-14 16:44:37 +02:00
|
|
|
#if defined(__MACH__) || defined(NetBSD)
|
2015-06-14 16:50:10 +02:00
|
|
|
setgid(pw->pw_gid) || setegid(pw->pw_gid) ||
|
2013-11-18 00:42:57 +01:00
|
|
|
setuid(pw->pw_uid) || seteuid(pw->pw_uid))
|
2013-07-17 20:17:00 +02:00
|
|
|
#else
|
2013-11-18 00:42:57 +01:00
|
|
|
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
|
|
|
|
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
|
2013-07-17 20:17:00 +02:00
|
|
|
#endif
|
2015-05-18 21:34:39 +02:00
|
|
|
fatal("cannot drop privileges");
|
2013-11-18 00:42:57 +01:00
|
|
|
}
|
2016-06-08 13:55:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_worker_entry(struct kore_worker *kw)
|
|
|
|
{
|
2017-01-12 23:38:51 +01:00
|
|
|
char buf[16];
|
|
|
|
int quit, had_lock, r;
|
|
|
|
u_int64_t now, next_lock, netwait;
|
2016-07-06 21:59:17 +02:00
|
|
|
#if defined(KORE_SINGLE_BINARY)
|
2017-01-12 23:38:51 +01:00
|
|
|
struct kore_runtime_call *rcall;
|
2016-07-06 21:59:17 +02:00
|
|
|
#endif
|
2016-06-08 13:55:14 +02:00
|
|
|
|
|
|
|
worker = kw;
|
2013-06-26 11:18:32 +02:00
|
|
|
|
2014-04-23 16:29:58 +02:00
|
|
|
(void)snprintf(buf, sizeof(buf), "kore [wrk %d]", kw->id);
|
2016-06-08 13:55:14 +02:00
|
|
|
#if !defined(KORE_NO_TLS)
|
|
|
|
if (kw->id == KORE_WORKER_KEYMGR)
|
|
|
|
(void)snprintf(buf, sizeof(buf), "kore [keymgr]");
|
|
|
|
#endif
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_platform_proctitle(buf);
|
2015-04-27 10:36:33 +02:00
|
|
|
|
|
|
|
if (worker_set_affinity == 1)
|
|
|
|
kore_platform_worker_setcpu(kw);
|
2013-06-26 11:18:32 +02:00
|
|
|
|
|
|
|
kore_pid = kw->pid;
|
|
|
|
|
|
|
|
sig_recv = 0;
|
|
|
|
signal(SIGHUP, kore_signal);
|
|
|
|
signal(SIGQUIT, kore_signal);
|
2016-01-31 16:48:17 +01:00
|
|
|
signal(SIGTERM, kore_signal);
|
2013-06-26 11:18:32 +02:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
2014-07-31 13:27:04 +02:00
|
|
|
if (foreground)
|
|
|
|
signal(SIGINT, kore_signal);
|
|
|
|
else
|
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
|
2016-06-08 13:55:14 +02:00
|
|
|
#if !defined(KORE_NO_TLS)
|
|
|
|
if (kw->id == KORE_WORKER_KEYMGR) {
|
|
|
|
kore_keymgr_run();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
kore_worker_privdrop();
|
|
|
|
|
2013-07-15 10:13:36 +02:00
|
|
|
net_init();
|
2015-11-27 16:22:50 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
2013-06-26 11:18:32 +02:00
|
|
|
http_init();
|
2015-11-27 16:22:50 +01:00
|
|
|
kore_accesslog_worker_init();
|
|
|
|
#endif
|
2015-04-06 18:54:35 +02:00
|
|
|
kore_timer_init();
|
2013-07-15 10:13:36 +02:00
|
|
|
kore_connection_init();
|
2014-10-18 02:32:05 +02:00
|
|
|
kore_domain_load_crl();
|
2016-06-08 13:55:14 +02:00
|
|
|
kore_domain_keymgr_init();
|
2013-06-26 11:18:32 +02:00
|
|
|
|
|
|
|
quit = 0;
|
2014-07-30 15:20:09 +02:00
|
|
|
had_lock = 0;
|
2014-10-22 21:16:49 +02:00
|
|
|
next_lock = 0;
|
2016-12-04 16:49:42 +01:00
|
|
|
worker_active_connections = 0;
|
|
|
|
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_platform_event_init();
|
2015-06-22 21:13:32 +02:00
|
|
|
kore_msg_worker_init();
|
2013-06-26 11:18:32 +02:00
|
|
|
|
2014-03-30 23:54:35 +02:00
|
|
|
#if defined(KORE_USE_PGSQL)
|
|
|
|
kore_pgsql_init();
|
|
|
|
#endif
|
|
|
|
|
2014-06-28 16:17:18 +02:00
|
|
|
#if defined(KORE_USE_TASKS)
|
|
|
|
kore_task_init();
|
|
|
|
#endif
|
|
|
|
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_log(LOG_NOTICE, "worker %d started (cpu#%d)", kw->id, kw->cpu);
|
2016-07-06 21:59:17 +02:00
|
|
|
|
|
|
|
#if defined(KORE_SINGLE_BINARY)
|
2017-01-12 23:38:51 +01:00
|
|
|
rcall = kore_runtime_getcall("kore_onload");
|
|
|
|
if (rcall != NULL) {
|
2017-01-26 13:26:55 +01:00
|
|
|
kore_runtime_execute(rcall);
|
2017-01-12 23:38:51 +01:00
|
|
|
kore_free(rcall);
|
|
|
|
}
|
2016-07-06 21:59:17 +02:00
|
|
|
#endif
|
2017-01-26 13:26:55 +01:00
|
|
|
kore_module_onload();
|
2013-06-27 08:43:07 +02:00
|
|
|
|
2013-06-26 11:18:32 +02:00
|
|
|
for (;;) {
|
|
|
|
if (sig_recv != 0) {
|
2016-01-31 16:48:17 +01:00
|
|
|
switch (sig_recv) {
|
|
|
|
case SIGHUP:
|
2014-06-29 21:15:23 +02:00
|
|
|
kore_module_reload(1);
|
2016-01-31 16:48:17 +01:00
|
|
|
break;
|
|
|
|
case SIGQUIT:
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
2013-06-26 11:18:32 +02:00
|
|
|
quit = 1;
|
2016-01-31 16:48:17 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-10-18 02:32:05 +02:00
|
|
|
|
2013-06-26 11:18:32 +02:00
|
|
|
sig_recv = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
now = kore_time_ms();
|
2015-04-06 18:54:35 +02:00
|
|
|
netwait = kore_timer_run(now);
|
2016-12-04 12:16:27 +01:00
|
|
|
if (netwait > 100)
|
|
|
|
netwait = 100;
|
2014-10-22 21:16:49 +02:00
|
|
|
|
|
|
|
if (now > next_lock) {
|
|
|
|
if (kore_worker_acceptlock_obtain()) {
|
|
|
|
if (had_lock == 0) {
|
|
|
|
kore_platform_enable_accept();
|
|
|
|
had_lock = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-30 15:20:09 +02:00
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
if (!worker->has_lock) {
|
2014-07-30 15:20:09 +02:00
|
|
|
if (had_lock == 1) {
|
|
|
|
had_lock = 0;
|
|
|
|
kore_platform_disable_accept();
|
|
|
|
}
|
2014-07-28 23:27:58 +02:00
|
|
|
}
|
2013-06-27 08:43:07 +02:00
|
|
|
|
2015-04-06 18:54:35 +02:00
|
|
|
r = kore_platform_event_wait(netwait);
|
2014-10-22 21:16:49 +02:00
|
|
|
if (worker->has_lock && r > 0) {
|
2013-06-26 16:37:22 +02:00
|
|
|
kore_worker_acceptlock_release();
|
2014-10-22 21:16:49 +02:00
|
|
|
next_lock = now + WORKER_LOCK_TIMEOUT;
|
|
|
|
}
|
2013-06-27 08:43:07 +02:00
|
|
|
|
2015-11-27 16:22:50 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
2013-06-26 11:18:32 +02:00
|
|
|
http_process();
|
2015-11-27 16:22:50 +01:00
|
|
|
#endif
|
2013-06-26 11:18:32 +02:00
|
|
|
|
2016-12-26 20:03:01 +01:00
|
|
|
kore_connection_check_timeout();
|
2015-06-22 21:13:32 +02:00
|
|
|
kore_connection_prune(KORE_CONNECTION_PRUNE_DISCONNECT);
|
2013-06-26 11:18:32 +02:00
|
|
|
|
2015-11-27 16:22:50 +01:00
|
|
|
if (quit)
|
|
|
|
break;
|
2013-06-26 11:18:32 +02:00
|
|
|
}
|
|
|
|
|
2016-01-04 22:40:14 +01:00
|
|
|
kore_platform_event_cleanup();
|
|
|
|
kore_connection_cleanup();
|
2016-02-01 12:30:20 +01:00
|
|
|
kore_domain_cleanup();
|
2016-06-08 13:55:14 +02:00
|
|
|
kore_module_cleanup();
|
2015-12-29 20:39:39 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
2016-01-04 22:40:14 +01:00
|
|
|
http_cleanup();
|
2015-12-29 20:39:39 +01:00
|
|
|
#endif
|
2016-01-04 22:40:14 +01:00
|
|
|
net_cleanup();
|
2016-06-08 13:55:14 +02:00
|
|
|
|
2017-01-12 23:38:51 +01:00
|
|
|
#if defined(KORE_USE_PYTHON)
|
|
|
|
kore_python_cleanup();
|
|
|
|
#endif
|
|
|
|
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_debug("worker %d shutting down", kw->id);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2013-06-26 16:37:22 +02:00
|
|
|
void
|
|
|
|
kore_worker_wait(int final)
|
|
|
|
{
|
2013-06-27 00:22:48 +02:00
|
|
|
u_int16_t id;
|
2013-06-26 16:37:22 +02:00
|
|
|
pid_t pid;
|
2013-06-27 00:22:48 +02:00
|
|
|
struct kore_worker *kw;
|
2013-06-26 16:37:22 +02:00
|
|
|
int status;
|
|
|
|
|
|
|
|
if (final)
|
|
|
|
pid = waitpid(WAIT_ANY, &status, 0);
|
|
|
|
else
|
|
|
|
pid = waitpid(WAIT_ANY, &status, WNOHANG);
|
|
|
|
|
|
|
|
if (pid == -1) {
|
|
|
|
kore_debug("waitpid(): %s", errno_s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == 0)
|
|
|
|
return;
|
|
|
|
|
2013-06-27 00:22:48 +02:00
|
|
|
for (id = 0; id < worker_count; id++) {
|
|
|
|
kw = WORKER(id);
|
2013-06-26 16:37:22 +02:00
|
|
|
if (kw->pid != pid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
kore_log(LOG_NOTICE, "worker %d (%d)-> status %d",
|
|
|
|
kw->id, pid, status);
|
|
|
|
|
2013-06-27 00:22:48 +02:00
|
|
|
if (final) {
|
|
|
|
kw->pid = 0;
|
|
|
|
break;
|
|
|
|
}
|
2013-06-26 16:37:22 +02:00
|
|
|
|
|
|
|
if (WEXITSTATUS(status) || WTERMSIG(status) ||
|
|
|
|
WCOREDUMP(status)) {
|
|
|
|
kore_log(LOG_NOTICE,
|
2013-07-07 14:56:50 +02:00
|
|
|
"worker %d (pid: %d) (hdlr: %s) gone",
|
|
|
|
kw->id, kw->pid,
|
|
|
|
(kw->active_hdlr != NULL) ? kw->active_hdlr->func :
|
|
|
|
"none");
|
2013-07-05 20:19:50 +02:00
|
|
|
|
2016-06-08 13:55:14 +02:00
|
|
|
#if !defined(KORE_NO_TLS)
|
|
|
|
if (id == KORE_WORKER_KEYMGR) {
|
|
|
|
kore_log(LOG_CRIT, "keymgr gone, stopping");
|
|
|
|
kw->pid = 0;
|
|
|
|
if (raise(SIGTERM) != 0) {
|
|
|
|
kore_log(LOG_WARNING,
|
|
|
|
"failed to raise SIGTERM signal");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-30 15:20:09 +02:00
|
|
|
if (kw->pid == accept_lock->current)
|
|
|
|
worker_unlock();
|
2013-07-07 14:48:32 +02:00
|
|
|
|
|
|
|
if (kw->active_hdlr != NULL) {
|
|
|
|
kw->active_hdlr->errors++;
|
|
|
|
kore_log(LOG_NOTICE,
|
|
|
|
"hdlr %s has caused %d error(s)",
|
|
|
|
kw->active_hdlr->func,
|
|
|
|
kw->active_hdlr->errors);
|
2013-07-05 20:19:50 +02:00
|
|
|
}
|
|
|
|
|
2013-07-07 14:48:32 +02:00
|
|
|
kore_log(LOG_NOTICE, "restarting worker %d", kw->id);
|
2015-06-22 21:13:32 +02:00
|
|
|
kore_msg_parent_remove(kw);
|
2013-06-27 00:22:48 +02:00
|
|
|
kore_worker_spawn(kw->id, kw->cpu);
|
2015-06-22 21:13:32 +02:00
|
|
|
kore_msg_parent_add(kw);
|
2013-06-27 08:43:07 +02:00
|
|
|
} else {
|
|
|
|
kore_log(LOG_NOTICE,
|
2013-07-07 14:48:32 +02:00
|
|
|
"worker %d (pid: %d) signaled us (%d)",
|
|
|
|
kw->id, kw->pid, status);
|
2013-06-26 16:37:22 +02:00
|
|
|
}
|
2013-06-27 00:22:48 +02:00
|
|
|
|
|
|
|
break;
|
2013-06-26 16:37:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
static inline void
|
2013-10-24 09:05:46 +02:00
|
|
|
kore_worker_acceptlock_release(void)
|
|
|
|
{
|
2014-07-30 15:20:09 +02:00
|
|
|
if (worker_count == 1)
|
2013-10-24 09:05:46 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (worker->has_lock != 1)
|
|
|
|
return;
|
|
|
|
|
2014-07-30 15:20:09 +02:00
|
|
|
worker_unlock();
|
|
|
|
worker->has_lock = 0;
|
2013-10-24 09:05:46 +02:00
|
|
|
}
|
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
static inline int
|
2013-06-26 16:37:22 +02:00
|
|
|
kore_worker_acceptlock_obtain(void)
|
|
|
|
{
|
2014-07-28 23:27:58 +02:00
|
|
|
int r;
|
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
if (worker->has_lock == 1)
|
|
|
|
return (1);
|
|
|
|
|
2014-07-30 15:20:09 +02:00
|
|
|
if (worker_count == 1) {
|
2013-06-27 08:43:07 +02:00
|
|
|
worker->has_lock = 1;
|
2014-07-28 23:27:58 +02:00
|
|
|
return (1);
|
2013-06-26 16:37:22 +02:00
|
|
|
}
|
|
|
|
|
2014-07-30 15:20:09 +02:00
|
|
|
if (worker_active_connections >= worker_max_connections)
|
|
|
|
return (0);
|
2014-07-28 23:27:58 +02:00
|
|
|
|
2014-07-30 15:20:09 +02:00
|
|
|
r = 0;
|
2013-06-27 00:22:48 +02:00
|
|
|
if (worker_trylock()) {
|
2014-07-30 15:20:09 +02:00
|
|
|
r = 1;
|
|
|
|
worker->has_lock = 1;
|
2013-06-26 16:37:22 +02:00
|
|
|
}
|
2014-07-28 23:27:58 +02:00
|
|
|
|
|
|
|
return (r);
|
2013-06-26 16:37:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-06-27 00:22:48 +02:00
|
|
|
worker_trylock(void)
|
2013-06-26 16:37:22 +02:00
|
|
|
{
|
2014-07-30 15:20:09 +02:00
|
|
|
if (!__sync_bool_compare_and_swap(&(accept_lock->lock), 0, 1))
|
2013-06-27 00:22:48 +02:00
|
|
|
return (0);
|
2013-06-26 16:37:22 +02:00
|
|
|
|
2013-06-27 08:43:07 +02:00
|
|
|
worker_debug("wrk#%d grabbed lock (%d/%d)\n", worker->id,
|
|
|
|
worker_active_connections, worker_max_connections);
|
2014-07-30 15:20:09 +02:00
|
|
|
accept_lock->current = worker->pid;
|
2013-06-26 16:37:22 +02:00
|
|
|
|
2013-06-27 00:22:48 +02:00
|
|
|
return (1);
|
2013-06-26 16:37:22 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 00:22:48 +02:00
|
|
|
static void
|
2014-07-30 15:20:09 +02:00
|
|
|
worker_unlock(void)
|
2013-06-27 00:22:48 +02:00
|
|
|
{
|
2014-07-30 15:20:09 +02:00
|
|
|
accept_lock->current = 0;
|
|
|
|
if (!__sync_bool_compare_and_swap(&(accept_lock->lock), 1, 0))
|
|
|
|
kore_log(LOG_NOTICE, "worker_unlock(): wasnt locked");
|
2013-06-27 00:22:48 +02:00
|
|
|
}
|