2013-06-17 23:39:17 +02:00
|
|
|
/*
|
2019-02-22 16:57:28 +01:00
|
|
|
* Copyright (c) 2013-2019 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>
|
2018-06-28 13:27:44 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.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;
|
2018-02-14 13:48:49 +01:00
|
|
|
static struct kevent *events = NULL;
|
2013-06-27 12:37:14 +02:00
|
|
|
static u_int32_t event_count = 0;
|
2013-06-17 23:39:17 +02:00
|
|
|
|
2018-07-31 06:51:34 +02:00
|
|
|
#if defined(KORE_USE_PLATFORM_PLEDGE)
|
|
|
|
static char pledges[256] = { "stdio rpath inet error" };
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
}
|
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;
|
|
|
|
|
2018-02-14 13:48:49 +01:00
|
|
|
if (kfd != -1)
|
|
|
|
close(kfd);
|
|
|
|
if (events != NULL)
|
|
|
|
kore_free(events);
|
|
|
|
|
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) {
|
2019-09-27 12:31:17 +02:00
|
|
|
if (l->fd == -1)
|
|
|
|
continue;
|
2015-06-22 21:13:32 +02:00
|
|
|
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)
|
|
|
|
{
|
2016-02-01 20:02:02 +01:00
|
|
|
if (kfd != -1) {
|
2016-01-22 12:25:19 +01:00
|
|
|
close(kfd);
|
|
|
|
kfd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events != NULL) {
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(events);
|
2016-01-22 12:25:19 +01:00
|
|
|
events = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-22 09:25:00 +01:00
|
|
|
void
|
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;
|
2018-10-09 19:34:40 +02:00
|
|
|
struct kore_event *evt;
|
2013-07-27 20:56:15 +02:00
|
|
|
int n, i;
|
2019-03-21 10:17:08 +01:00
|
|
|
struct timespec timeo, *ts;
|
2013-06-17 23:39:17 +02:00
|
|
|
|
2019-03-21 10:17:08 +01:00
|
|
|
if (timer == KORE_WAIT_INFINITE) {
|
|
|
|
ts = NULL;
|
|
|
|
} else {
|
|
|
|
timeo.tv_sec = timer / 1000;
|
|
|
|
timeo.tv_nsec = (timer % 1000) * 1000000;
|
|
|
|
ts = &timeo;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = kevent(kfd, NULL, 0, events, event_count, ts);
|
2013-06-17 23:39:17 +02:00
|
|
|
if (n == -1) {
|
|
|
|
if (errno == EINTR)
|
2018-12-22 09:25:00 +01:00
|
|
|
return;
|
2013-06-17 23:39:17 +02:00
|
|
|
fatal("kevent(): %s", errno_s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n > 0)
|
|
|
|
kore_debug("main(): %d sockets available", n);
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
2018-12-22 09:25:00 +01:00
|
|
|
evt = (struct kore_event *)events[i].udata;
|
|
|
|
|
|
|
|
if (evt == NULL)
|
|
|
|
fatal("evt == NULL");
|
2013-07-27 20:56:15 +02:00
|
|
|
|
2018-10-09 19:34:40 +02:00
|
|
|
r = 0;
|
2013-06-17 23:39:17 +02:00
|
|
|
|
2018-10-09 19:34:40 +02:00
|
|
|
if (events[i].filter == EVFILT_READ)
|
|
|
|
evt->flags |= KORE_EVENT_READ;
|
2014-04-02 00:06:24 +02:00
|
|
|
|
2018-10-09 19:34:40 +02:00
|
|
|
if (events[i].filter == EVFILT_WRITE)
|
|
|
|
evt->flags |= KORE_EVENT_WRITE;
|
2013-06-17 23:39:17 +02:00
|
|
|
|
2018-10-09 19:34:40 +02:00
|
|
|
if (events[i].flags & EV_EOF || events[i].flags & EV_ERROR)
|
|
|
|
r = 1;
|
|
|
|
|
2018-12-22 09:25:00 +01:00
|
|
|
evt->handle(evt, 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)
|
|
|
|
{
|
2018-07-30 15:58:47 +02:00
|
|
|
kore_platform_event_schedule(fd, EVFILT_READ, EV_ADD | EV_CLEAR, data);
|
2014-04-02 00:06:24 +02:00
|
|
|
}
|
|
|
|
|
2015-12-09 21:29:44 +01:00
|
|
|
void
|
|
|
|
kore_platform_schedule_write(int fd, void *data)
|
|
|
|
{
|
2018-07-30 15:58:47 +02:00
|
|
|
kore_platform_event_schedule(fd, EVFILT_WRITE, EV_ADD | EV_CLEAR, data);
|
2015-12-09 21:29:44 +01:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-10-15 20:18:54 +02:00
|
|
|
void
|
|
|
|
kore_platform_disable_write(int fd)
|
|
|
|
{
|
|
|
|
kore_platform_event_schedule(fd, EVFILT_WRITE, 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
|
|
|
{
|
2019-03-29 16:24:14 +01:00
|
|
|
#ifdef __MACH__
|
|
|
|
kore_proctitle(title);
|
|
|
|
#else
|
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
|
|
|
}
|
2018-06-28 13:27:44 +02:00
|
|
|
|
|
|
|
#if defined(KORE_USE_PLATFORM_SENDFILE)
|
|
|
|
int
|
|
|
|
kore_platform_sendfile(struct connection *c, struct netbuf *nb)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
off_t len, smin;
|
|
|
|
|
|
|
|
smin = nb->fd_len - nb->fd_off;
|
|
|
|
len = MIN(SENDFILE_PAYLOAD_MAX, smin);
|
|
|
|
|
|
|
|
#if defined(__MACH__)
|
|
|
|
ret = sendfile(nb->file_ref->fd, c->fd, nb->fd_off, &len, NULL, 0);
|
|
|
|
#else
|
|
|
|
ret = sendfile(nb->file_ref->fd, c->fd, nb->fd_off, len, NULL, &len, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ret == -1) {
|
|
|
|
if (errno == EAGAIN) {
|
|
|
|
nb->fd_off += len;
|
2018-10-09 19:34:40 +02:00
|
|
|
c->evt.flags &= ~KORE_EVENT_WRITE;
|
2018-06-28 13:27:44 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno == EINTR) {
|
|
|
|
nb->fd_off += len;
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
nb->fd_off += len;
|
|
|
|
|
|
|
|
if (len == 0 || nb->fd_off == nb->fd_len) {
|
2018-12-22 09:25:00 +01:00
|
|
|
net_remove_netbuf(c, nb);
|
2018-06-28 13:27:44 +02:00
|
|
|
c->snb = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-31 06:51:34 +02:00
|
|
|
|
2019-09-25 14:25:49 +02:00
|
|
|
void
|
|
|
|
kore_platform_sandbox(void)
|
|
|
|
{
|
|
|
|
#if defined(KORE_USE_PLATFORM_PLEDGE)
|
|
|
|
kore_platform_pledge();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-07-31 06:51:34 +02:00
|
|
|
#if defined(KORE_USE_PLATFORM_PLEDGE)
|
|
|
|
void
|
|
|
|
kore_platform_pledge(void)
|
|
|
|
{
|
|
|
|
if (pledge(pledges, NULL) == -1)
|
|
|
|
fatal("failed to pledge process");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_platform_add_pledge(const char *pledge)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = strlcat(pledges, " ", sizeof(pledges));
|
|
|
|
if (len >= sizeof(pledges))
|
|
|
|
fatal("truncation on pledges");
|
|
|
|
|
|
|
|
len = strlcat(pledges, pledge, sizeof(pledges));
|
|
|
|
if (len >= sizeof(pledges))
|
|
|
|
fatal("truncation on pledges (%s)", pledge);
|
|
|
|
}
|
|
|
|
#endif
|