2013-04-17 22:34:27 +02:00
|
|
|
/*
|
2018-01-20 22:51:06 +01:00
|
|
|
* Copyright (c) 2013-2018 Joris Vink <joris@coders.se>
|
2013-04-17 22:34:27 +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-05-20 16:36:13 +02:00
|
|
|
#include <sys/types.h>
|
2016-01-18 11:30:22 +01:00
|
|
|
#include <sys/stat.h>
|
2018-10-07 20:49:16 +02:00
|
|
|
#include <sys/un.h>
|
2016-01-18 11:30:22 +01:00
|
|
|
|
2013-04-17 22:34:27 +02:00
|
|
|
#include <sys/socket.h>
|
2015-05-20 16:36:13 +02:00
|
|
|
#include <sys/resource.h>
|
2013-04-17 22:34:27 +02:00
|
|
|
|
2018-10-26 19:19:47 +02:00
|
|
|
#include <fcntl.h>
|
2016-01-22 12:08:13 +01:00
|
|
|
#include <stdio.h>
|
2013-07-27 20:56:15 +02:00
|
|
|
#include <netdb.h>
|
2013-05-03 00:04:06 +02:00
|
|
|
#include <signal.h>
|
2013-04-17 22:34:27 +02:00
|
|
|
|
|
|
|
#include "kore.h"
|
|
|
|
|
2016-01-18 11:30:22 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
|
|
|
#include "http.h"
|
|
|
|
#endif
|
|
|
|
|
2017-01-12 23:38:51 +01:00
|
|
|
#if defined(KORE_USE_PYTHON)
|
|
|
|
#include "python_api.h"
|
|
|
|
#endif
|
|
|
|
|
2018-04-13 16:05:59 +02:00
|
|
|
volatile sig_atomic_t sig_recv;
|
2013-07-27 20:56:15 +02:00
|
|
|
struct listener_head listeners;
|
|
|
|
u_int8_t nlisteners;
|
2013-06-26 11:18:32 +02:00
|
|
|
pid_t kore_pid = -1;
|
2013-06-17 23:39:17 +02:00
|
|
|
u_int16_t cpu_count = 1;
|
2014-07-31 13:27:04 +02:00
|
|
|
int foreground = 0;
|
2013-06-05 08:55:07 +02:00
|
|
|
int kore_debug = 0;
|
2018-11-15 16:01:37 +01:00
|
|
|
int kore_quiet = 0;
|
2015-05-18 21:34:39 +02:00
|
|
|
int skip_runas = 0;
|
2018-07-11 09:44:29 +02:00
|
|
|
int skip_chroot = 0;
|
|
|
|
u_int8_t worker_count = 0;
|
|
|
|
char *kore_root_path = NULL;
|
|
|
|
char *kore_runas_user = NULL;
|
2015-04-09 15:29:44 +02:00
|
|
|
u_int32_t kore_socket_backlog = 5000;
|
2013-06-04 16:53:30 +02:00
|
|
|
char *kore_pidfile = KORE_PIDFILE_DEFAULT;
|
2015-05-06 10:59:43 +02:00
|
|
|
char *kore_tls_cipher_list = KORE_DEFAULT_CIPHER_LIST;
|
2013-04-17 22:34:27 +02:00
|
|
|
|
2016-07-06 16:16:15 +02:00
|
|
|
extern char *__progname;
|
|
|
|
|
2013-06-05 08:55:07 +02:00
|
|
|
static void usage(void);
|
2014-07-04 09:25:18 +02:00
|
|
|
static void version(void);
|
2013-06-26 11:18:32 +02:00
|
|
|
static void kore_write_kore_pid(void);
|
2013-06-24 11:32:45 +02:00
|
|
|
static void kore_server_sslstart(void);
|
2018-10-18 17:18:41 +02:00
|
|
|
static void kore_server_start(int, char *[]);
|
2013-04-17 22:34:27 +02:00
|
|
|
|
2013-06-05 08:55:07 +02:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2016-07-06 16:16:15 +02:00
|
|
|
fprintf(stderr, "Usage: %s [options]\n", __progname);
|
2018-04-02 18:20:57 +02:00
|
|
|
|
2014-08-01 13:59:47 +02:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "Available options:\n");
|
2016-07-06 16:16:15 +02:00
|
|
|
#if !defined(KORE_SINGLE_BINARY)
|
|
|
|
fprintf(stderr, "\t-c\tconfiguration to use\n");
|
|
|
|
#endif
|
|
|
|
#if defined(KORE_DEBUG)
|
2016-12-05 14:45:29 +01:00
|
|
|
fprintf(stderr, "\t-d\trun with debug on\n");
|
2016-07-06 16:16:15 +02:00
|
|
|
#endif
|
|
|
|
fprintf(stderr, "\t-f\tstart in foreground\n");
|
2014-08-03 17:11:02 +02:00
|
|
|
fprintf(stderr, "\t-h\tthis help text\n");
|
2015-05-28 22:20:43 +02:00
|
|
|
fprintf(stderr, "\t-n\tdo not chroot\n");
|
2018-11-15 16:01:37 +01:00
|
|
|
fprintf(stderr, "\t-q\tonly log errors\n");
|
2016-07-06 16:16:15 +02:00
|
|
|
fprintf(stderr, "\t-r\tdo not drop privileges\n");
|
2018-04-02 18:20:57 +02:00
|
|
|
fprintf(stderr, "\t-v\tdisplay %s build information\n", __progname);
|
2014-08-01 13:59:47 +02:00
|
|
|
|
2018-04-02 18:20:57 +02:00
|
|
|
#if !defined(KORE_SINGLE_BINARY)
|
2017-02-19 06:52:29 +01:00
|
|
|
fprintf(stderr, "\nFind more information on https://kore.io\n");
|
2018-04-02 18:20:57 +02:00
|
|
|
#else
|
|
|
|
fprintf(stderr, "\nBuilt using https://kore.io\n");
|
|
|
|
#endif
|
|
|
|
|
2016-07-06 16:16:15 +02:00
|
|
|
exit(1);
|
2013-06-05 08:55:07 +02:00
|
|
|
}
|
|
|
|
|
2014-07-04 09:25:18 +02:00
|
|
|
static void
|
|
|
|
version(void)
|
|
|
|
{
|
2018-06-19 19:05:55 +02:00
|
|
|
printf("%s ", kore_version);
|
2015-11-27 16:22:50 +01:00
|
|
|
#if defined(KORE_NO_TLS)
|
|
|
|
printf("no-tls ");
|
|
|
|
#endif
|
|
|
|
#if defined(KORE_NO_HTTP)
|
|
|
|
printf("no-http ");
|
|
|
|
#endif
|
2014-07-04 09:25:18 +02:00
|
|
|
#if defined(KORE_USE_PGSQL)
|
|
|
|
printf("pgsql ");
|
|
|
|
#endif
|
|
|
|
#if defined(KORE_USE_TASKS)
|
|
|
|
printf("tasks ");
|
2015-11-27 16:22:50 +01:00
|
|
|
#endif
|
|
|
|
#if defined(KORE_DEBUG)
|
|
|
|
printf("debug ");
|
2016-07-06 16:16:15 +02:00
|
|
|
#endif
|
|
|
|
#if defined(KORE_SINGLE_BINARY)
|
|
|
|
printf("single ");
|
2014-07-04 09:25:18 +02:00
|
|
|
#endif
|
2017-01-25 22:21:30 +01:00
|
|
|
#if defined(KORE_USE_PYTHON)
|
|
|
|
printf("python ");
|
|
|
|
#endif
|
2014-07-04 09:25:18 +02:00
|
|
|
printf("\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2013-04-17 22:34:27 +02:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2017-02-09 13:29:33 +01:00
|
|
|
struct kore_runtime_call *rcall;
|
|
|
|
int ch, flags;
|
2013-04-17 22:34:27 +02:00
|
|
|
|
2014-08-01 13:59:47 +02:00
|
|
|
flags = 0;
|
|
|
|
|
2016-07-06 16:16:15 +02:00
|
|
|
#if !defined(KORE_SINGLE_BINARY)
|
2018-11-15 16:01:37 +01:00
|
|
|
while ((ch = getopt(argc, argv, "c:dfhnqrv")) != -1) {
|
2016-07-06 16:16:15 +02:00
|
|
|
#else
|
2018-11-15 16:01:37 +01:00
|
|
|
while ((ch = getopt(argc, argv, "dfhnqrv")) != -1) {
|
2016-07-06 16:16:15 +02:00
|
|
|
#endif
|
2014-08-01 13:59:47 +02:00
|
|
|
flags++;
|
2013-06-05 08:55:07 +02:00
|
|
|
switch (ch) {
|
2016-07-06 16:16:15 +02:00
|
|
|
#if !defined(KORE_SINGLE_BINARY)
|
2013-06-05 08:55:07 +02:00
|
|
|
case 'c':
|
|
|
|
config_file = optarg;
|
|
|
|
break;
|
2016-07-06 16:16:15 +02:00
|
|
|
#endif
|
2013-07-15 10:13:36 +02:00
|
|
|
#if defined(KORE_DEBUG)
|
2016-07-06 16:16:15 +02:00
|
|
|
case 'd':
|
2013-06-05 08:55:07 +02:00
|
|
|
kore_debug = 1;
|
|
|
|
break;
|
2016-07-06 16:16:15 +02:00
|
|
|
#endif
|
2014-07-31 13:27:04 +02:00
|
|
|
case 'f':
|
|
|
|
foreground = 1;
|
|
|
|
break;
|
2014-08-01 13:59:47 +02:00
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
break;
|
2013-11-18 00:42:57 +01:00
|
|
|
case 'n':
|
|
|
|
skip_chroot = 1;
|
|
|
|
break;
|
2018-11-15 16:01:37 +01:00
|
|
|
case 'q':
|
|
|
|
kore_quiet = 1;
|
|
|
|
break;
|
2015-05-18 21:34:39 +02:00
|
|
|
case 'r':
|
|
|
|
skip_runas = 1;
|
|
|
|
break;
|
2014-07-04 09:25:18 +02:00
|
|
|
case 'v':
|
|
|
|
version();
|
|
|
|
break;
|
2013-06-05 08:55:07 +02:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2014-08-01 15:55:09 +02:00
|
|
|
kore_mem_init();
|
|
|
|
|
2018-04-09 12:51:20 +02:00
|
|
|
#if !defined(KORE_SINGLE_BINARY)
|
2017-02-22 20:09:11 +01:00
|
|
|
if (argc > 0)
|
2018-07-07 13:34:58 +02:00
|
|
|
fatal("did you mean to run `kodev' instead?");
|
2018-04-09 12:51:20 +02:00
|
|
|
#endif
|
2017-02-22 20:09:11 +01:00
|
|
|
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_pid = getpid();
|
2013-07-27 20:56:15 +02:00
|
|
|
nlisteners = 0;
|
|
|
|
LIST_INIT(&listeners);
|
|
|
|
|
2013-07-15 10:13:36 +02:00
|
|
|
kore_log_init();
|
2015-11-27 16:22:50 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
2018-06-28 13:27:44 +02:00
|
|
|
http_parent_init();
|
2014-01-22 22:55:10 +01:00
|
|
|
kore_auth_init();
|
2015-11-27 16:22:50 +01:00
|
|
|
kore_validator_init();
|
2018-06-28 13:27:44 +02:00
|
|
|
kore_filemap_init();
|
2015-11-27 16:22:50 +01:00
|
|
|
#endif
|
2013-06-24 11:32:45 +02:00
|
|
|
kore_domain_init();
|
2013-12-15 01:11:56 +01:00
|
|
|
kore_module_init();
|
2013-06-24 11:32:45 +02:00
|
|
|
kore_server_sslstart();
|
2013-06-27 08:43:07 +02:00
|
|
|
|
2016-07-06 16:16:15 +02:00
|
|
|
#if !defined(KORE_SINGLE_BINARY)
|
2014-07-31 17:15:51 +02:00
|
|
|
if (config_file == NULL)
|
|
|
|
usage();
|
2018-06-28 13:27:44 +02:00
|
|
|
#endif
|
2017-01-12 23:38:51 +01:00
|
|
|
kore_module_load(NULL, NULL, KORE_MODULE_NATIVE);
|
2018-11-12 09:01:05 +01:00
|
|
|
|
|
|
|
#if defined(KORE_USE_PYTHON)
|
|
|
|
kore_python_init();
|
|
|
|
#endif
|
|
|
|
|
2018-04-09 12:51:20 +02:00
|
|
|
kore_parse_config();
|
|
|
|
|
2018-10-18 17:18:41 +02:00
|
|
|
#if defined(KORE_SINGLE_BINARY)
|
2017-02-09 13:29:33 +01:00
|
|
|
rcall = kore_runtime_getcall("kore_parent_configure");
|
|
|
|
if (rcall != NULL) {
|
2018-04-09 12:51:20 +02:00
|
|
|
kore_runtime_configure(rcall, argc, argv);
|
2017-02-09 13:29:33 +01:00
|
|
|
kore_free(rcall);
|
|
|
|
}
|
2018-10-18 17:18:41 +02:00
|
|
|
#endif
|
2014-07-31 17:15:51 +02:00
|
|
|
|
2013-06-24 09:36:40 +02:00
|
|
|
kore_platform_init();
|
2015-11-27 16:22:50 +01:00
|
|
|
|
|
|
|
#if !defined(KORE_NO_HTTP)
|
2016-01-18 11:30:22 +01:00
|
|
|
if (http_body_disk_offload > 0) {
|
|
|
|
if (mkdir(http_body_disk_path, 0700) == -1 && errno != EEXIST) {
|
|
|
|
printf("can't create http_body_disk_path '%s': %s\n",
|
|
|
|
http_body_disk_path, errno_s);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2015-11-27 16:22:50 +01:00
|
|
|
#endif
|
2013-05-04 22:18:27 +02:00
|
|
|
|
2018-05-25 20:49:02 +02:00
|
|
|
kore_signal_setup();
|
2018-10-18 17:18:41 +02:00
|
|
|
kore_server_start(argc, argv);
|
2013-05-03 00:04:06 +02:00
|
|
|
|
2018-11-15 16:01:37 +01:00
|
|
|
if (!kore_quiet)
|
|
|
|
kore_log(LOG_NOTICE, "server shutting down");
|
|
|
|
|
2013-06-26 16:37:22 +02:00
|
|
|
kore_worker_shutdown();
|
2018-10-23 21:46:34 +02:00
|
|
|
|
|
|
|
rcall = kore_runtime_getcall("kore_parent_teardown");
|
|
|
|
if (rcall != NULL) {
|
|
|
|
kore_runtime_execute(rcall);
|
|
|
|
kore_free(rcall);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlink(kore_pidfile) == -1 && errno != ENOENT)
|
|
|
|
kore_log(LOG_NOTICE, "failed to remove pidfile (%s)", errno_s);
|
2013-07-27 20:56:15 +02:00
|
|
|
|
2016-06-08 13:55:14 +02:00
|
|
|
kore_listener_cleanup();
|
2018-11-15 16:01:37 +01:00
|
|
|
|
|
|
|
if (!kore_quiet)
|
|
|
|
kore_log(LOG_NOTICE, "goodbye");
|
2016-06-08 13:55:14 +02:00
|
|
|
|
2017-02-06 11:42:53 +01:00
|
|
|
#if defined(KORE_USE_PYTHON)
|
|
|
|
kore_python_cleanup();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
kore_mem_cleanup();
|
|
|
|
|
2013-04-17 22:34:27 +02:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2015-05-25 15:42:34 +02:00
|
|
|
#if !defined(KORE_NO_TLS)
|
2013-06-24 11:32:45 +02:00
|
|
|
int
|
2016-06-09 13:52:37 +02:00
|
|
|
kore_tls_sni_cb(SSL *ssl, int *ad, void *arg)
|
2013-06-24 11:32:45 +02:00
|
|
|
{
|
|
|
|
struct kore_domain *dom;
|
|
|
|
const char *sname;
|
|
|
|
|
|
|
|
sname = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
|
2015-05-06 10:59:43 +02:00
|
|
|
kore_debug("kore_tls_sni_cb(): received host %s", sname);
|
2013-06-24 11:32:45 +02:00
|
|
|
|
|
|
|
if (sname != NULL && (dom = kore_domain_lookup(sname)) != NULL) {
|
2018-07-11 09:52:05 +02:00
|
|
|
if (dom->ssl_ctx == NULL) {
|
|
|
|
kore_log(LOG_NOTICE,
|
|
|
|
"TLS configuration for %s not complete",
|
|
|
|
dom->domain);
|
|
|
|
return (SSL_TLSEXT_ERR_NOACK);
|
|
|
|
}
|
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
kore_debug("kore_ssl_sni_cb(): Using %s CTX", sname);
|
|
|
|
SSL_set_SSL_CTX(ssl, dom->ssl_ctx);
|
2013-12-14 16:31:07 +01:00
|
|
|
|
|
|
|
if (dom->cafile != NULL) {
|
|
|
|
SSL_set_verify(ssl, SSL_VERIFY_PEER |
|
|
|
|
SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
|
|
|
|
} else {
|
|
|
|
SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL);
|
|
|
|
}
|
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
return (SSL_TLSEXT_ERR_OK);
|
2013-06-17 23:39:17 +02:00
|
|
|
}
|
|
|
|
|
2016-06-09 13:52:37 +02:00
|
|
|
return (SSL_TLSEXT_ERR_NOACK);
|
2013-06-24 11:32:45 +02:00
|
|
|
}
|
2015-05-20 16:36:13 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
kore_tls_info_callback(const SSL *ssl, int flags, int ret)
|
|
|
|
{
|
|
|
|
struct connection *c;
|
|
|
|
|
|
|
|
if (flags & SSL_CB_HANDSHAKE_START) {
|
|
|
|
if ((c = SSL_get_app_data(ssl)) == NULL)
|
|
|
|
fatal("no SSL_get_app_data");
|
2018-10-29 20:38:58 +01:00
|
|
|
|
|
|
|
#if defined(TLS1_3_VERSION)
|
|
|
|
if (SSL_version(ssl) != TLS1_3_VERSION)
|
|
|
|
#endif
|
|
|
|
c->tls_reneg++;
|
2015-05-20 16:36:13 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-01 10:22:32 +02:00
|
|
|
#endif
|
2013-06-17 23:39:17 +02:00
|
|
|
|
2013-07-27 20:56:15 +02:00
|
|
|
int
|
2015-11-27 16:22:50 +01:00
|
|
|
kore_server_bind(const char *ip, const char *port, const char *ccb)
|
2013-07-27 20:56:15 +02:00
|
|
|
{
|
2017-02-22 22:23:30 +01:00
|
|
|
int r;
|
2013-07-27 20:56:15 +02:00
|
|
|
struct listener *l;
|
2015-06-14 16:54:44 +02:00
|
|
|
struct addrinfo hints, *results;
|
2013-07-27 20:56:15 +02:00
|
|
|
|
|
|
|
kore_debug("kore_server_bind(%s, %s)", ip, port);
|
|
|
|
|
2015-06-14 16:54:44 +02:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
hints.ai_flags = 0;
|
|
|
|
|
|
|
|
r = getaddrinfo(ip, port, &hints, &results);
|
2013-07-27 20:56:15 +02:00
|
|
|
if (r != 0)
|
|
|
|
fatal("getaddrinfo(%s): %s", ip, gai_strerror(r));
|
|
|
|
|
2018-10-07 20:49:16 +02:00
|
|
|
if ((l = kore_listener_alloc(results->ai_family, ccb)) == NULL) {
|
2013-07-27 20:56:15 +02:00
|
|
|
freeaddrinfo(results);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2018-10-07 20:49:16 +02:00
|
|
|
if (bind(l->fd, results->ai_addr, results->ai_addrlen) == -1) {
|
|
|
|
kore_listener_free(l);
|
2013-07-27 20:56:15 +02:00
|
|
|
freeaddrinfo(results);
|
2018-10-07 20:49:16 +02:00
|
|
|
kore_log(LOG_ERR, "bind(): %s", errno_s);
|
2013-07-27 20:56:15 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2018-10-07 20:49:16 +02:00
|
|
|
freeaddrinfo(results);
|
|
|
|
|
|
|
|
if (listen(l->fd, kore_socket_backlog) == -1) {
|
|
|
|
kore_listener_free(l);
|
|
|
|
kore_log(LOG_ERR, "listen(): %s", errno_s);
|
2013-07-27 20:56:15 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:01:37 +01:00
|
|
|
if (foreground && !kore_quiet) {
|
2018-10-07 20:49:16 +02:00
|
|
|
#if !defined(KORE_NO_TLS)
|
|
|
|
kore_log(LOG_NOTICE, "running on https://%s:%s", ip, port);
|
|
|
|
#else
|
|
|
|
kore_log(LOG_NOTICE, "running on http://%s:%s", ip, port);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kore_server_bind_unix(const char *path, const char *ccb)
|
|
|
|
{
|
|
|
|
struct listener *l;
|
2018-10-15 20:18:54 +02:00
|
|
|
int len;
|
2018-10-07 20:49:16 +02:00
|
|
|
struct sockaddr_un sun;
|
2018-10-15 20:18:54 +02:00
|
|
|
socklen_t socklen;
|
2018-10-07 20:49:16 +02:00
|
|
|
|
|
|
|
memset(&sun, 0, sizeof(sun));
|
|
|
|
sun.sun_family = AF_UNIX;
|
|
|
|
|
2018-10-15 20:18:54 +02:00
|
|
|
len = snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", path);
|
|
|
|
if (len == -1 || (size_t)len >= sizeof(sun.sun_path)) {
|
2018-10-07 20:49:16 +02:00
|
|
|
kore_log(LOG_ERR, "unix socket path '%s' too long", path);
|
2013-07-27 20:56:15 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2018-10-15 20:18:54 +02:00
|
|
|
#if defined(__linux__)
|
|
|
|
if (sun.sun_path[0] == '@')
|
|
|
|
sun.sun_path[0] = '\0';
|
|
|
|
#endif
|
|
|
|
|
|
|
|
socklen = sizeof(sun.sun_family) + len;
|
|
|
|
|
2018-10-07 20:49:16 +02:00
|
|
|
if ((l = kore_listener_alloc(AF_UNIX, ccb)) == NULL)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
2018-10-15 20:18:54 +02:00
|
|
|
if (bind(l->fd, (struct sockaddr *)&sun, socklen) == -1) {
|
2018-10-07 20:49:16 +02:00
|
|
|
kore_log(LOG_ERR, "bind: %s", errno_s);
|
|
|
|
kore_listener_free(l);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
2013-07-27 20:56:15 +02:00
|
|
|
|
2015-04-09 15:29:44 +02:00
|
|
|
if (listen(l->fd, kore_socket_backlog) == -1) {
|
2017-02-01 17:12:11 +01:00
|
|
|
kore_log(LOG_ERR, "listen(): %s", errno_s);
|
2018-10-07 20:49:16 +02:00
|
|
|
kore_listener_free(l);
|
2013-07-27 20:56:15 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:01:37 +01:00
|
|
|
if (foreground && !kore_quiet)
|
2018-10-07 20:49:16 +02:00
|
|
|
kore_log(LOG_NOTICE, "running on %s", path);
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct listener *
|
|
|
|
kore_listener_alloc(int family, const char *ccb)
|
|
|
|
{
|
|
|
|
struct listener *l;
|
|
|
|
|
|
|
|
switch (family) {
|
|
|
|
case AF_INET:
|
|
|
|
case AF_INET6:
|
|
|
|
case AF_UNIX:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("unknown address family %d", family);
|
|
|
|
}
|
|
|
|
|
|
|
|
l = kore_calloc(1, sizeof(struct listener));
|
|
|
|
|
2018-10-07 21:25:00 +02:00
|
|
|
nlisteners++;
|
|
|
|
LIST_INSERT_HEAD(&listeners, l, list);
|
|
|
|
|
2018-10-07 20:49:16 +02:00
|
|
|
l->fd = -1;
|
|
|
|
l->family = family;
|
2018-10-09 19:34:40 +02:00
|
|
|
|
|
|
|
l->evt.type = KORE_TYPE_LISTENER;
|
|
|
|
l->evt.handle = kore_listener_accept;
|
2018-10-07 20:49:16 +02:00
|
|
|
|
|
|
|
if ((l->fd = socket(family, SOCK_STREAM, 0)) == -1) {
|
|
|
|
kore_listener_free(l);
|
|
|
|
kore_log(LOG_ERR, "socket(): %s", errno_s);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2018-10-26 19:19:47 +02:00
|
|
|
if (fcntl(l->fd, F_SETFD, FD_CLOEXEC) == -1) {
|
|
|
|
kore_listener_free(l);
|
|
|
|
kore_log(LOG_ERR, "fcntl(): %s", errno_s);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2018-10-07 21:21:37 +02:00
|
|
|
if (!kore_connection_nonblock(l->fd, family != AF_UNIX)) {
|
2018-10-07 20:49:16 +02:00
|
|
|
kore_listener_free(l);
|
|
|
|
kore_log(LOG_ERR, "kore_connection_nonblock(): %s", errno_s);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!kore_sockopt(l->fd, SOL_SOCKET, SO_REUSEADDR)) {
|
|
|
|
kore_listener_free(l);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2015-11-27 16:22:50 +01:00
|
|
|
if (ccb != NULL) {
|
2017-01-12 23:38:51 +01:00
|
|
|
if ((l->connect = kore_runtime_getcall(ccb)) == NULL) {
|
2017-02-01 17:12:11 +01:00
|
|
|
kore_log(LOG_ERR, "no such callback: '%s'", ccb);
|
2018-10-07 20:49:16 +02:00
|
|
|
kore_listener_free(l);
|
|
|
|
return (NULL);
|
2015-11-27 16:22:50 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
l->connect = NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-07 20:49:16 +02:00
|
|
|
return (l);
|
|
|
|
}
|
2014-08-01 20:00:09 +02:00
|
|
|
|
2018-10-07 20:49:16 +02:00
|
|
|
void
|
|
|
|
kore_listener_free(struct listener *l)
|
|
|
|
{
|
|
|
|
LIST_REMOVE(l, list);
|
|
|
|
|
|
|
|
if (l->fd != -1)
|
|
|
|
close(l->fd);
|
|
|
|
|
|
|
|
kore_free(l);
|
2013-07-27 20:56:15 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 19:34:40 +02:00
|
|
|
void
|
|
|
|
kore_listener_accept(void *arg, int error)
|
|
|
|
{
|
|
|
|
struct connection *c;
|
|
|
|
struct listener *l = arg;
|
|
|
|
u_int32_t accepted;
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
fatal("error on listening socket");
|
|
|
|
|
|
|
|
if (!(l->evt.flags & KORE_EVENT_READ))
|
|
|
|
return;
|
|
|
|
|
|
|
|
accepted = 0;
|
|
|
|
|
|
|
|
while (worker_active_connections < worker_max_connections) {
|
|
|
|
if (worker_accept_threshold != 0 &&
|
2018-10-22 09:01:05 +02:00
|
|
|
accepted >= worker_accept_threshold) {
|
|
|
|
kore_worker_make_busy();
|
2018-10-09 19:34:40 +02:00
|
|
|
break;
|
2018-10-22 09:01:05 +02:00
|
|
|
}
|
2018-10-09 19:34:40 +02:00
|
|
|
|
|
|
|
if (!kore_connection_accept(l, &c))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (c == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
accepted++;
|
|
|
|
kore_platform_event_all(c->fd, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 22:23:30 +01:00
|
|
|
int
|
|
|
|
kore_sockopt(int fd, int what, int opt)
|
|
|
|
{
|
|
|
|
int on;
|
|
|
|
|
|
|
|
on = 1;
|
|
|
|
if (setsockopt(fd, what, opt, (const char *)&on, sizeof(on)) == -1) {
|
|
|
|
kore_log(LOG_ERR, "setsockopt(): %s", errno_s);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2018-05-25 20:49:02 +02:00
|
|
|
void
|
|
|
|
kore_signal_setup(void)
|
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
sig_recv = 0;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = kore_signal;
|
|
|
|
|
|
|
|
if (sigfillset(&sa.sa_mask) == -1)
|
|
|
|
fatal("sigfillset: %s", errno_s);
|
|
|
|
|
|
|
|
if (sigaction(SIGHUP, &sa, NULL) == -1)
|
|
|
|
fatal("sigaction: %s", errno_s);
|
|
|
|
if (sigaction(SIGQUIT, &sa, NULL) == -1)
|
|
|
|
fatal("sigaction: %s", errno_s);
|
|
|
|
if (sigaction(SIGTERM, &sa, NULL) == -1)
|
|
|
|
fatal("sigaction: %s", errno_s);
|
2018-07-11 09:44:29 +02:00
|
|
|
if (sigaction(SIGUSR1, &sa, NULL) == -1)
|
|
|
|
fatal("sigaction: %s", errno_s);
|
2018-10-26 19:19:47 +02:00
|
|
|
if (sigaction(SIGCHLD, &sa, NULL) == -1)
|
|
|
|
fatal("sigaction: %s", errno_s);
|
2018-05-25 20:49:02 +02:00
|
|
|
|
|
|
|
if (foreground) {
|
|
|
|
if (sigaction(SIGINT, &sa, NULL) == -1)
|
|
|
|
fatal("sigaction: %s", errno_s);
|
|
|
|
} else {
|
|
|
|
(void)signal(SIGINT, SIG_IGN);
|
|
|
|
}
|
2018-06-28 13:27:44 +02:00
|
|
|
|
|
|
|
(void)signal(SIGPIPE, SIG_IGN);
|
2018-05-25 20:49:02 +02:00
|
|
|
}
|
|
|
|
|
2016-06-08 13:55:14 +02:00
|
|
|
void
|
|
|
|
kore_listener_cleanup(void)
|
|
|
|
{
|
|
|
|
struct listener *l;
|
|
|
|
|
|
|
|
while (!LIST_EMPTY(&listeners)) {
|
|
|
|
l = LIST_FIRST(&listeners);
|
2018-10-07 20:49:16 +02:00
|
|
|
kore_listener_free(l);
|
2016-06-08 13:55:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 11:18:32 +02:00
|
|
|
void
|
|
|
|
kore_signal(int sig)
|
|
|
|
{
|
|
|
|
sig_recv = sig;
|
|
|
|
}
|
|
|
|
|
2018-10-23 19:49:42 +02:00
|
|
|
void
|
|
|
|
kore_shutdown(void)
|
|
|
|
{
|
|
|
|
if (worker != NULL) {
|
|
|
|
kore_msg_send(KORE_MSG_PARENT, KORE_MSG_SHUTDOWN, NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fatal("kore_shutdown: called from parent");
|
|
|
|
}
|
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
static void
|
|
|
|
kore_server_sslstart(void)
|
|
|
|
{
|
2015-05-25 15:42:34 +02:00
|
|
|
#if !defined(KORE_NO_TLS)
|
2013-06-24 11:32:45 +02:00
|
|
|
kore_debug("kore_server_sslstart()");
|
|
|
|
|
|
|
|
SSL_library_init();
|
|
|
|
SSL_load_error_strings();
|
2014-08-01 10:22:32 +02:00
|
|
|
#endif
|
2013-06-17 23:39:17 +02:00
|
|
|
}
|
|
|
|
|
2013-06-26 11:18:32 +02:00
|
|
|
static void
|
2018-10-18 17:18:41 +02:00
|
|
|
kore_server_start(int argc, char *argv[])
|
2013-06-26 11:18:32 +02:00
|
|
|
{
|
2017-01-12 23:38:51 +01:00
|
|
|
u_int32_t tmp;
|
|
|
|
int quit;
|
2017-02-09 13:36:08 +01:00
|
|
|
struct kore_runtime_call *rcall;
|
2018-12-22 09:25:00 +01:00
|
|
|
u_int64_t now, netwait, timerwait;
|
2013-09-02 08:52:16 +02:00
|
|
|
|
2018-10-18 17:18:41 +02:00
|
|
|
if (foreground == 0) {
|
|
|
|
if (daemon(1, 0) == -1)
|
|
|
|
fatal("cannot daemon(): %s", errno_s);
|
|
|
|
#if defined(KORE_SINGLE_BINARY)
|
|
|
|
rcall = kore_runtime_getcall("kore_parent_daemonized");
|
|
|
|
if (rcall != NULL) {
|
|
|
|
kore_runtime_execute(rcall);
|
|
|
|
kore_free(rcall);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2013-06-26 11:18:32 +02:00
|
|
|
|
|
|
|
kore_pid = getpid();
|
2017-02-22 20:38:07 +01:00
|
|
|
kore_write_kore_pid();
|
2013-06-26 11:18:32 +02:00
|
|
|
|
2018-11-15 16:01:37 +01:00
|
|
|
if (!kore_quiet) {
|
|
|
|
kore_log(LOG_NOTICE, "%s is starting up", __progname);
|
2014-07-04 09:14:05 +02:00
|
|
|
#if defined(KORE_USE_PGSQL)
|
2018-11-15 16:01:37 +01:00
|
|
|
kore_log(LOG_NOTICE, "pgsql built-in enabled");
|
2014-07-04 09:14:05 +02:00
|
|
|
#endif
|
|
|
|
#if defined(KORE_USE_TASKS)
|
2018-11-15 16:01:37 +01:00
|
|
|
kore_log(LOG_NOTICE, "tasks built-in enabled");
|
2014-07-04 09:14:05 +02:00
|
|
|
#endif
|
2016-07-11 08:12:23 +02:00
|
|
|
#if defined(KORE_USE_JSONRPC)
|
2018-11-15 16:01:37 +01:00
|
|
|
kore_log(LOG_NOTICE, "jsonrpc built-in enabled");
|
2016-07-11 08:12:23 +02:00
|
|
|
#endif
|
2017-02-09 13:38:09 +01:00
|
|
|
#if defined(KORE_USE_PYTHON)
|
2018-11-15 16:01:37 +01:00
|
|
|
kore_log(LOG_NOTICE, "python built-in enabled");
|
2017-02-09 13:38:09 +01:00
|
|
|
#endif
|
2018-11-15 16:01:37 +01:00
|
|
|
}
|
|
|
|
|
2017-02-09 13:36:08 +01:00
|
|
|
#if !defined(KORE_SINGLE_BINARY)
|
|
|
|
rcall = kore_runtime_getcall("kore_parent_configure");
|
|
|
|
if (rcall != NULL) {
|
2018-10-18 17:18:41 +02:00
|
|
|
kore_runtime_configure(rcall, argc, argv);
|
2017-02-09 13:36:08 +01:00
|
|
|
kore_free(rcall);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_platform_proctitle("kore [parent]");
|
2015-06-22 21:13:32 +02:00
|
|
|
kore_msg_init();
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_worker_init();
|
|
|
|
|
2015-06-22 21:13:32 +02:00
|
|
|
/* Set worker_max_connections for kore_connection_init(). */
|
|
|
|
tmp = worker_max_connections;
|
|
|
|
worker_max_connections = worker_count;
|
|
|
|
|
|
|
|
net_init();
|
|
|
|
kore_connection_init();
|
|
|
|
kore_platform_event_init();
|
|
|
|
kore_msg_parent_init();
|
|
|
|
|
2014-07-31 13:27:04 +02:00
|
|
|
quit = 0;
|
2015-06-22 21:13:32 +02:00
|
|
|
worker_max_connections = tmp;
|
2018-07-11 09:44:29 +02:00
|
|
|
|
2018-12-22 09:25:00 +01:00
|
|
|
kore_timer_init();
|
2018-12-22 09:41:55 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
2018-12-22 09:25:00 +01:00
|
|
|
kore_timer_add(kore_accesslog_run, 10, NULL, 0);
|
2018-12-22 09:41:55 +01:00
|
|
|
#endif
|
2018-12-22 09:25:00 +01:00
|
|
|
|
2014-07-31 13:27:04 +02:00
|
|
|
while (quit != 1) {
|
2013-06-26 16:37:22 +02:00
|
|
|
if (sig_recv != 0) {
|
2014-07-31 13:27:04 +02:00
|
|
|
switch (sig_recv) {
|
|
|
|
case SIGHUP:
|
2013-06-26 16:37:22 +02:00
|
|
|
kore_worker_dispatch_signal(sig_recv);
|
2014-07-31 13:27:04 +02:00
|
|
|
kore_module_reload(0);
|
|
|
|
break;
|
|
|
|
case SIGINT:
|
|
|
|
case SIGQUIT:
|
2016-01-31 16:48:17 +01:00
|
|
|
case SIGTERM:
|
2014-07-31 13:27:04 +02:00
|
|
|
quit = 1;
|
|
|
|
kore_worker_dispatch_signal(sig_recv);
|
|
|
|
continue;
|
2018-07-11 09:44:29 +02:00
|
|
|
case SIGUSR1:
|
|
|
|
kore_worker_dispatch_signal(sig_recv);
|
|
|
|
break;
|
2018-10-26 19:19:47 +02:00
|
|
|
case SIGCHLD:
|
|
|
|
kore_worker_wait(0);
|
|
|
|
break;
|
2014-07-31 13:27:04 +02:00
|
|
|
default:
|
|
|
|
break;
|
2013-06-26 16:37:22 +02:00
|
|
|
}
|
2014-07-31 13:27:04 +02:00
|
|
|
|
2013-06-26 16:37:22 +02:00
|
|
|
sig_recv = 0;
|
|
|
|
}
|
|
|
|
|
2018-12-22 09:25:00 +01:00
|
|
|
netwait = 100;
|
|
|
|
now = kore_time_ms();
|
|
|
|
|
|
|
|
timerwait = kore_timer_run(now);
|
|
|
|
if (timerwait < netwait)
|
|
|
|
netwait = timerwait;
|
|
|
|
|
|
|
|
kore_platform_event_wait(netwait);
|
2015-06-22 21:13:32 +02:00
|
|
|
kore_connection_prune(KORE_CONNECTION_PRUNE_DISCONNECT);
|
2013-06-26 16:37:22 +02:00
|
|
|
}
|
2015-12-29 20:39:39 +01:00
|
|
|
|
2018-12-22 09:25:00 +01:00
|
|
|
now = kore_time_ms();
|
2018-12-22 09:41:55 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
2018-12-22 09:25:00 +01:00
|
|
|
kore_accesslog_gather(NULL, now, 1);
|
2018-12-22 09:41:55 +01:00
|
|
|
#endif
|
2018-12-22 09:25:00 +01:00
|
|
|
|
2016-01-04 22:40:14 +01:00
|
|
|
kore_platform_event_cleanup();
|
|
|
|
kore_connection_cleanup();
|
2016-02-01 20:02:02 +01:00
|
|
|
kore_domain_cleanup();
|
2016-01-04 22:40:14 +01:00
|
|
|
net_cleanup();
|
2013-06-26 11:18:32 +02:00
|
|
|
}
|
|
|
|
|
2013-06-17 23:39:17 +02:00
|
|
|
static void
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_write_kore_pid(void)
|
2013-06-04 16:53:30 +02:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if ((fp = fopen(kore_pidfile, "w+")) == NULL) {
|
2014-07-31 17:15:51 +02:00
|
|
|
printf("warning: couldn't write pid to %s (%s)\n",
|
|
|
|
kore_pidfile, errno_s);
|
2013-06-04 16:53:30 +02:00
|
|
|
} else {
|
2013-06-26 11:18:32 +02:00
|
|
|
fprintf(fp, "%d\n", kore_pid);
|
2013-06-04 16:53:30 +02:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|