2013-06-17 23:39:17 +02:00
|
|
|
/*
|
2016-01-04 12:58:51 +01:00
|
|
|
* Copyright (c) 2013-2016 Joris Vink <joris@coders.se>
|
2013-06-17 23:39:17 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/event.h>
|
2014-06-30 08:42:18 +02:00
|
|
|
#include <sys/sysctl.h>
|
2014-10-26 22:41:55 +01:00
|
|
|
|
|
|
|
#if defined(__FreeBSD_version)
|
|
|
|
#include <sys/cpuset.h>
|
2013-07-28 19:21:49 +02:00
|
|
|
#endif
|
|
|
|
|
2016-01-22 12:08:13 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-06-17 23:39:17 +02:00
|
|
|
#include "kore.h"
|
|
|
|
|
2014-04-02 00:06:24 +02:00
|
|
|
#if defined(KORE_USE_PGSQL)
|
2014-07-03 22:14:46 +02:00
|
|
|
#include "pgsql.h"
|
2014-04-02 00:06:24 +02:00
|
|
|
#endif
|
|
|
|
|
2014-06-30 14:43:34 +02:00
|
|
|
#if defined(KORE_USE_TASKS)
|
2014-07-03 22:14:46 +02:00
|
|
|
#include "tasks.h"
|
2014-06-30 14:43:34 +02:00
|
|
|
#endif
|
|
|
|
|
2013-06-17 23:39:17 +02:00
|
|
|
static int kfd = -1;
|
|
|
|
static struct kevent *events;
|
2013-06-27 12:37:14 +02:00
|
|
|
static u_int32_t event_count = 0;
|
2013-06-17 23:39:17 +02:00
|
|
|
|
|
|
|
void
|
2013-06-24 09:36:40 +02:00
|
|
|
kore_platform_init(void)
|
2013-06-17 23:39:17 +02:00
|
|
|
{
|
2014-10-26 22:41:55 +01:00
|
|
|
#if defined(__MACH__) || defined(__FreeBSD_version)
|
2013-07-28 19:21:49 +02:00
|
|
|
long n;
|
|
|
|
size_t len = sizeof(n);
|
2014-10-26 22:41:55 +01:00
|
|
|
int mib[] = { CTL_HW, HW_NCPU };
|
2013-07-28 19:21:49 +02:00
|
|
|
|
2014-10-26 22:41:55 +01:00
|
|
|
if (sysctl(mib, 2, &n, &len, NULL, 0) == -1) {
|
|
|
|
kore_debug("kore_platform_init(): sysctl %s", errno_s);
|
|
|
|
cpu_count = 1;
|
|
|
|
} else {
|
2013-07-28 19:21:49 +02:00
|
|
|
cpu_count = (u_int16_t)n;
|
2014-10-26 22:41:55 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
cpu_count = 0;
|
|
|
|
#endif /* __MACH__ || __FreeBSD_version */
|
2013-06-17 23:39:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_platform_worker_setcpu(struct kore_worker *kw)
|
2013-06-17 23:39:17 +02:00
|
|
|
{
|
2014-10-26 22:41:55 +01:00
|
|
|
#if defined(__FreeBSD_version)
|
|
|
|
cpuset_t cpuset;
|
|
|
|
|
|
|
|
CPU_ZERO(&cpuset);
|
|
|
|
CPU_SET(kw->cpu, &cpuset);
|
|
|
|
if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID,
|
|
|
|
-1, sizeof(cpuset), &cpuset) == -1) {
|
|
|
|
fatal("failed: %s", errno_s);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __FreeBSD_version */
|
2013-06-17 23:39:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_platform_event_init(void)
|
2013-06-17 23:39:17 +02:00
|
|
|
{
|
2013-07-27 20:56:15 +02:00
|
|
|
struct listener *l;
|
|
|
|
|
2013-06-17 23:39:17 +02:00
|
|
|
if ((kfd = kqueue()) == -1)
|
|
|
|
fatal("kqueue(): %s", errno_s);
|
|
|
|
|
2014-10-16 21:45:43 +02:00
|
|
|
event_count = (worker_max_connections * 2) + nlisteners;
|
2013-06-27 12:37:14 +02:00
|
|
|
events = kore_calloc(event_count, sizeof(struct kevent));
|
2013-06-26 16:37:22 +02:00
|
|
|
|
2015-06-22 21:13:32 +02:00
|
|
|
/* Hack to check if we're running under the parent or not. */
|
|
|
|
if (worker != NULL) {
|
|
|
|
LIST_FOREACH(l, &listeners, list) {
|
|
|
|
kore_platform_event_schedule(l->fd,
|
|
|
|
EVFILT_READ, EV_ADD | EV_DISABLE, l);
|
|
|
|
}
|
2013-07-27 20:56:15 +02:00
|
|
|
}
|
2013-06-17 23:39:17 +02:00
|
|
|
}
|
|
|
|
|
2016-01-22 12:25:19 +01:00
|
|
|
void
|
|
|
|
kore_platform_event_cleanup(void)
|
|
|
|
{
|
|
|
|
if (kfd >= 0) {
|
|
|
|
close(kfd);
|
|
|
|
kfd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events != NULL) {
|
|
|
|
kore_mem_free(events);
|
|
|
|
events = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
int
|
2014-07-28 23:35:12 +02:00
|
|
|
kore_platform_event_wait(u_int64_t timer)
|
2013-06-17 23:39:17 +02:00
|
|
|
{
|
2014-10-22 21:16:49 +02:00
|
|
|
u_int32_t r;
|
2013-07-27 20:56:15 +02:00
|
|
|
struct listener *l;
|
2013-06-17 23:39:17 +02:00
|
|
|
struct connection *c;
|
2013-07-27 20:56:15 +02:00
|
|
|
u_int8_t type;
|
2013-06-26 16:37:22 +02:00
|
|
|
struct timespec timeo;
|
2013-07-27 20:56:15 +02:00
|
|
|
int n, i;
|
2013-06-17 23:39:17 +02:00
|
|
|
|
2015-04-06 18:54:35 +02:00
|
|
|
timeo.tv_sec = timer / 1000;
|
|
|
|
timeo.tv_nsec = (timer % 1000) * 1000000;
|
2015-04-24 11:45:00 +02:00
|
|
|
n = kevent(kfd, NULL, 0, events, event_count, &timeo);
|
2013-06-17 23:39:17 +02:00
|
|
|
if (n == -1) {
|
|
|
|
if (errno == EINTR)
|
2014-10-22 21:16:49 +02:00
|
|
|
return (0);
|
2013-06-17 23:39:17 +02:00
|
|
|
fatal("kevent(): %s", errno_s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n > 0)
|
|
|
|
kore_debug("main(): %d sockets available", n);
|
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
r = 0;
|
2013-06-17 23:39:17 +02:00
|
|
|
for (i = 0; i < n; i++) {
|
2013-07-27 20:56:15 +02:00
|
|
|
if (events[i].udata == NULL)
|
|
|
|
fatal("events[%d].udata == NULL", i);
|
|
|
|
|
|
|
|
type = *(u_int8_t *)events[i].udata;
|
2013-06-17 23:39:17 +02:00
|
|
|
|
|
|
|
if (events[i].flags & EV_EOF ||
|
|
|
|
events[i].flags & EV_ERROR) {
|
2014-06-30 14:43:34 +02:00
|
|
|
switch (type) {
|
|
|
|
case KORE_TYPE_LISTENER:
|
2013-06-17 23:39:17 +02:00
|
|
|
fatal("error on server socket");
|
2014-06-30 14:43:34 +02:00
|
|
|
/* NOTREACHED */
|
2014-04-02 00:06:24 +02:00
|
|
|
#if defined(KORE_USE_PGSQL)
|
2014-06-30 14:43:34 +02:00
|
|
|
case KORE_TYPE_PGSQL_CONN:
|
2014-04-02 00:06:24 +02:00
|
|
|
kore_pgsql_handle(events[i].udata, 1);
|
2014-06-30 14:43:34 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(KORE_USE_TASKS)
|
|
|
|
case KORE_TYPE_TASK:
|
|
|
|
kore_task_handle(events[i].udata, 1);
|
|
|
|
break;
|
2014-04-02 00:06:24 +02:00
|
|
|
#endif
|
2014-06-30 14:43:34 +02:00
|
|
|
default:
|
|
|
|
c = (struct connection *)events[i].udata;
|
|
|
|
kore_connection_disconnect(c);
|
|
|
|
break;
|
|
|
|
}
|
2014-04-02 00:06:24 +02:00
|
|
|
|
2013-06-17 23:39:17 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-04-02 00:06:24 +02:00
|
|
|
switch (type) {
|
|
|
|
case KORE_TYPE_LISTENER:
|
2013-07-27 20:56:15 +02:00
|
|
|
l = (struct listener *)events[i].udata;
|
|
|
|
|
2015-04-09 15:29:44 +02:00
|
|
|
while (worker_active_connections <
|
2014-07-30 15:20:09 +02:00
|
|
|
worker_max_connections) {
|
2015-05-18 12:20:28 +02:00
|
|
|
if (worker_accept_threshold != 0 &&
|
|
|
|
r >= worker_accept_threshold)
|
2015-04-23 10:24:00 +02:00
|
|
|
break;
|
|
|
|
|
2015-04-20 15:17:42 +02:00
|
|
|
if (!kore_connection_accept(l, &c)) {
|
|
|
|
r = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-27 00:22:48 +02:00
|
|
|
if (c == NULL)
|
2013-07-13 22:24:00 +02:00
|
|
|
break;
|
2013-06-27 00:22:48 +02:00
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
r++;
|
2014-09-17 08:25:45 +02:00
|
|
|
kore_platform_event_all(c->fd, c);
|
2013-06-27 00:22:48 +02:00
|
|
|
}
|
2014-04-02 00:06:24 +02:00
|
|
|
break;
|
|
|
|
case KORE_TYPE_CONNECTION:
|
2013-06-17 23:39:17 +02:00
|
|
|
c = (struct connection *)events[i].udata;
|
2013-07-13 19:56:38 +02:00
|
|
|
if (events[i].filter == EVFILT_READ &&
|
|
|
|
!(c->flags & CONN_READ_BLOCK))
|
2013-06-17 23:39:17 +02:00
|
|
|
c->flags |= CONN_READ_POSSIBLE;
|
2013-07-13 19:56:38 +02:00
|
|
|
if (events[i].filter == EVFILT_WRITE &&
|
|
|
|
!(c->flags & CONN_WRITE_BLOCK))
|
2013-06-17 23:39:17 +02:00
|
|
|
c->flags |= CONN_WRITE_POSSIBLE;
|
|
|
|
|
2015-12-01 20:55:00 +01:00
|
|
|
if (c->handle != NULL && !c->handle(c))
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_connection_disconnect(c);
|
2014-04-02 00:06:24 +02:00
|
|
|
break;
|
|
|
|
#if defined(KORE_USE_PGSQL)
|
|
|
|
case KORE_TYPE_PGSQL_CONN:
|
|
|
|
kore_pgsql_handle(events[i].udata, 0);
|
|
|
|
break;
|
2014-06-30 14:43:34 +02:00
|
|
|
#endif
|
|
|
|
#if defined(KORE_USE_TASKS)
|
|
|
|
case KORE_TYPE_TASK:
|
|
|
|
kore_task_handle(events[i].udata, 0);
|
|
|
|
break;
|
2014-04-02 00:06:24 +02:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
fatal("wrong type in event %d", type);
|
2013-06-17 23:39:17 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-22 21:16:49 +02:00
|
|
|
|
|
|
|
return (r);
|
2013-06-17 23:39:17 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 08:25:45 +02:00
|
|
|
void
|
|
|
|
kore_platform_event_all(int fd, void *c)
|
|
|
|
{
|
2015-06-23 18:16:19 +02:00
|
|
|
kore_platform_event_schedule(fd, EVFILT_READ, EV_ADD | EV_CLEAR, c);
|
|
|
|
kore_platform_event_schedule(fd, EVFILT_WRITE, EV_ADD | EV_CLEAR, c);
|
2014-09-17 08:25:45 +02:00
|
|
|
}
|
|
|
|
|
2013-06-17 23:39:17 +02:00
|
|
|
void
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_platform_event_schedule(int fd, int type, int flags, void *data)
|
2013-06-17 23:39:17 +02:00
|
|
|
{
|
2015-04-24 11:45:00 +02:00
|
|
|
struct kevent event[1];
|
|
|
|
|
|
|
|
EV_SET(&event[0], fd, type, flags, 0, 0, data);
|
|
|
|
if (kevent(kfd, event, 1, NULL, 0, NULL) == -1)
|
|
|
|
fatal("kevent: %s", errno_s);
|
2013-06-17 23:39:17 +02:00
|
|
|
}
|
|
|
|
|
2013-06-26 16:37:22 +02:00
|
|
|
void
|
|
|
|
kore_platform_enable_accept(void)
|
|
|
|
{
|
2013-07-27 20:56:15 +02:00
|
|
|
struct listener *l;
|
|
|
|
|
|
|
|
LIST_FOREACH(l, &listeners, list)
|
|
|
|
kore_platform_event_schedule(l->fd, EVFILT_READ, EV_ENABLE, l);
|
2013-06-26 16:37:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_platform_disable_accept(void)
|
|
|
|
{
|
2013-07-27 20:56:15 +02:00
|
|
|
struct listener *l;
|
|
|
|
|
|
|
|
LIST_FOREACH(l, &listeners, list)
|
|
|
|
kore_platform_event_schedule(l->fd, EVFILT_READ, EV_DISABLE, l);
|
2013-06-26 16:37:22 +02:00
|
|
|
}
|
|
|
|
|
2014-04-02 00:06:24 +02:00
|
|
|
void
|
|
|
|
kore_platform_schedule_read(int fd, void *data)
|
|
|
|
{
|
|
|
|
kore_platform_event_schedule(fd, EVFILT_READ, EV_ADD, data);
|
|
|
|
}
|
|
|
|
|
2015-12-09 21:29:44 +01:00
|
|
|
void
|
|
|
|
kore_platform_schedule_write(int fd, void *data)
|
|
|
|
{
|
|
|
|
kore_platform_event_schedule(fd, EVFILT_WRITE, EV_ADD, data);
|
|
|
|
}
|
|
|
|
|
2014-04-02 00:06:24 +02:00
|
|
|
void
|
|
|
|
kore_platform_disable_read(int fd)
|
|
|
|
{
|
|
|
|
kore_platform_event_schedule(fd, EVFILT_READ, EV_DELETE, NULL);
|
|
|
|
}
|
|
|
|
|
2013-06-17 23:39:17 +02:00
|
|
|
void
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_platform_proctitle(char *title)
|
2013-06-17 23:39:17 +02:00
|
|
|
{
|
2013-07-17 20:17:00 +02:00
|
|
|
#ifndef __MACH__
|
2013-06-17 23:39:17 +02:00
|
|
|
setproctitle("%s", title);
|
2013-07-17 20:17:00 +02:00
|
|
|
#endif
|
2013-06-17 23:39:17 +02:00
|
|
|
}
|