kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

python.c (152060B)



      1 /*
      2  * Copyright (c) 2016 Stanislav Yudin <stan@endlessinsomnia.com>
      3  * Copyright (c) 2017-2022 Joris Vink <joris@coders.se>
      4  *
      5  * Permission to use, copy, modify, and distribute this software for any
      6  * purpose with or without fee is hereby granted, provided that the above
      7  * copyright notice and this permission notice appear in all copies.
      8  *
      9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     16  */
     17 
     18 #include <sys/param.h>
     19 #include <sys/types.h>
     20 #include <sys/stat.h>
     21 #include <sys/socket.h>
     22 #include <sys/wait.h>
     23 #include <sys/un.h>
     24 
     25 #include <ctype.h>
     26 #include <libgen.h>
     27 #include <inttypes.h>
     28 #include <signal.h>
     29 #include <fcntl.h>
     30 #include <unistd.h>
     31 #include <stdarg.h>
     32 #include <stddef.h>
     33 
     34 #include "kore.h"
     35 #include "http.h"
     36 
     37 #if defined(KORE_USE_PGSQL)
     38 #include "pgsql.h"
     39 #endif
     40 
     41 #if defined(KORE_USE_CURL)
     42 #include "curl.h"
     43 #endif
     44 
     45 #if defined(KORE_USE_ACME)
     46 #include "acme.h"
     47 #endif
     48 
     49 #include "python_api.h"
     50 #include "python_methods.h"
     51 
     52 #if defined(KORE_USE_CURL)
     53 #include "python_curlopt.h"
     54 #endif
     55 
     56 #include <frameobject.h>
     57 
     58 #if PY_VERSION_HEX < 0x030A0000
     59 typedef enum {
     60 	PYGEN_RETURN = 0,
     61 	PYGEN_ERROR = -1,
     62 	PYGEN_NEXT = 1,
     63 } PySendResult;
     64 #endif
     65 
     66 struct reqcall {
     67 	PyObject		*f;
     68 	TAILQ_ENTRY(reqcall)	list;
     69 };
     70 
     71 TAILQ_HEAD(reqcall_list, reqcall);
     72 
     73 PyMODINIT_FUNC		python_module_init(void);
     74 
     75 static PyObject		*python_import(const char *);
     76 static PyObject		*pyconnection_alloc(struct connection *);
     77 static PyObject		*python_callable(PyObject *, const char *);
     78 static void		python_split_arguments(char *, char **, size_t);
     79 static void		python_kore_recvobj(struct kore_msg *, const void *);
     80 
     81 static PyObject		*python_cmsg_to_list(struct msghdr *);
     82 static const char	*python_string_from_dict(PyObject *, const char *);
     83 static int		python_bool_from_dict(PyObject *, const char *, int *);
     84 static int		python_long_from_dict(PyObject *, const char *, long *);
     85 
     86 static int		pyhttp_response_sent(struct netbuf *);
     87 static PyObject		*pyhttp_file_alloc(struct http_file *);
     88 static PyObject		*pyhttp_request_alloc(const struct http_request *);
     89 
     90 static struct python_coro	*python_coro_create(PyObject *,
     91 				    struct http_request *);
     92 static struct kore_domain	*python_route_domain_resolve(struct pyroute *);
     93 
     94 static int		python_route_install(struct pyroute *);
     95 static int		python_route_params(PyObject *, struct kore_route *,
     96 			    const char *, int, int);
     97 static int		python_route_methods(PyObject *, PyObject *,
     98 			    struct kore_route *);
     99 static int		python_route_auth(PyObject *, struct kore_route *);
    100 static int		python_route_hooks(PyObject *, struct kore_route *);
    101 static int		python_route_hook_set(PyObject *, const char *,
    102 			    struct kore_runtime_call **);
    103 
    104 static int		python_coro_run(struct python_coro *);
    105 static void		python_coro_wakeup(struct python_coro *);
    106 static void		python_coro_suspend(struct python_coro *);
    107 static void		python_coro_trace(const char *, struct python_coro *);
    108 
    109 static void		pysocket_evt_handle(void *, int);
    110 static void		pysocket_op_timeout(void *, u_int64_t);
    111 static PyObject		*pysocket_op_create(struct pysocket *,
    112 			    int, const void *, size_t);
    113 
    114 static struct pysocket	*pysocket_alloc(void);
    115 static PyObject		*pysocket_async_recv(struct pysocket_op *);
    116 static PyObject		*pysocket_async_send(struct pysocket_op *);
    117 static PyObject		*pysocket_async_accept(struct pysocket_op *);
    118 static PyObject		*pysocket_async_connect(struct pysocket_op *);
    119 
    120 static void		pylock_do_release(struct pylock *);
    121 
    122 static void		pytimer_run(void *, u_int64_t);
    123 static void		pyproc_timeout(void *, u_int64_t);
    124 static void		pysuspend_wakeup(void *, u_int64_t);
    125 
    126 static void		pygather_reap_coro(struct pygather_op *,
    127 			    struct python_coro *);
    128 
    129 static int		pyhttp_preprocess(struct http_request *);
    130 static int		pyhttp_iterobj_chunk_sent(struct netbuf *);
    131 static int		pyhttp_iterobj_next(struct pyhttp_iterobj *);
    132 static void		pyhttp_iterobj_disconnect(struct connection *);
    133 
    134 static int		pyconnection_x509_cb(void *, int, int, const char *,
    135 			    const void *, size_t, int);
    136 
    137 #if defined(KORE_USE_PGSQL)
    138 static int		pykore_pgsql_result(struct pykore_pgsql *);
    139 static void		pykore_pgsql_callback(struct kore_pgsql *, void *);
    140 static int		pykore_pgsql_params(struct pykore_pgsql *, PyObject *);
    141 static int		pykore_pgsql_params(struct pykore_pgsql *, PyObject *);
    142 #endif
    143 
    144 #if defined(KORE_USE_CURL)
    145 static void		python_curl_http_callback(struct kore_curl *, void *);
    146 static void		python_curl_handle_callback(struct kore_curl *, void *);
    147 static PyObject		*pyhttp_client_request(struct pyhttp_client *, int,
    148 			    PyObject *);
    149 static PyObject		*python_curlopt_set(struct pycurl_data *,
    150 			    long, PyObject *);
    151 static int		python_curlopt_from_dict(struct pycurl_data *,
    152 			    PyObject *);
    153 #endif
    154 
    155 static void	python_append_path(const char *);
    156 static void	python_push_integer(PyObject *, const char *, long);
    157 static void	python_push_type(const char *, PyObject *, PyTypeObject *);
    158 
    159 static int	python_validator_check(PyObject *);
    160 static int	python_runtime_http_request(void *, struct http_request *);
    161 static void	python_runtime_http_request_free(void *, struct http_request *);
    162 static void	python_runtime_http_body_chunk(void *, struct http_request *,
    163 		    const void *, size_t);
    164 static int	python_runtime_validator(void *, struct http_request *,
    165 		    const void *);
    166 static void	python_runtime_wsmessage(void *, struct connection *,
    167 		    u_int8_t, const void *, size_t);
    168 static void	python_runtime_execute(void *);
    169 static int	python_runtime_onload(void *, int);
    170 static void	python_runtime_signal(void *, int);
    171 static void	python_runtime_configure(void *, int, char **);
    172 static void	python_runtime_connect(void *, struct connection *);
    173 
    174 static void	python_module_load(struct kore_module *);
    175 static void	python_module_free(struct kore_module *);
    176 static void	python_module_reload(struct kore_module *);
    177 static void	*python_module_getsym(struct kore_module *, const char *);
    178 
    179 static void	*python_malloc(void *, size_t);
    180 static void	*python_calloc(void *, size_t, size_t);
    181 static void	*python_realloc(void *, void *, size_t);
    182 static void	python_free(void *, void *);
    183 
    184 struct kore_module_functions kore_python_module = {
    185 	.free = python_module_free,
    186 	.load = python_module_load,
    187 	.getsym = python_module_getsym,
    188 	.reload = python_module_reload
    189 };
    190 
    191 struct kore_runtime kore_python_runtime = {
    192 	KORE_RUNTIME_PYTHON,
    193 	.http_request = python_runtime_http_request,
    194 	.http_body_chunk = python_runtime_http_body_chunk,
    195 	.http_request_free = python_runtime_http_request_free,
    196 	.validator = python_runtime_validator,
    197 	.wsconnect = python_runtime_connect,
    198 	.wsmessage = python_runtime_wsmessage,
    199 	.wsdisconnect = python_runtime_connect,
    200 	.onload = python_runtime_onload,
    201 	.signal = python_runtime_signal,
    202 	.connect = python_runtime_connect,
    203 	.execute = python_runtime_execute,
    204 	.configure = python_runtime_configure,
    205 };
    206 
    207 static struct {
    208 	const char		*symbol;
    209 	int			value;
    210 } python_integers[] = {
    211 	{ "LOG_ERR", LOG_ERR },
    212 	{ "LOG_INFO", LOG_INFO },
    213 	{ "LOG_NOTICE", LOG_NOTICE },
    214 	{ "RESULT_OK", KORE_RESULT_OK },
    215 	{ "RESULT_RETRY", KORE_RESULT_RETRY },
    216 	{ "RESULT_ERROR", KORE_RESULT_ERROR },
    217 	{ "MODULE_LOAD", KORE_MODULE_LOAD },
    218 	{ "MODULE_UNLOAD", KORE_MODULE_UNLOAD },
    219 	{ "TIMER_ONESHOT", KORE_TIMER_ONESHOT },
    220 	{ "CONN_PROTO_HTTP", CONN_PROTO_HTTP },
    221 	{ "CONN_PROTO_UNKNOWN", CONN_PROTO_UNKNOWN },
    222 	{ "CONN_PROTO_WEBSOCKET", CONN_PROTO_WEBSOCKET },
    223 	{ "CONN_STATE_ESTABLISHED", CONN_STATE_ESTABLISHED },
    224 	{ "HTTP_METHOD_GET", HTTP_METHOD_GET },
    225 	{ "HTTP_METHOD_PUT", HTTP_METHOD_PUT },
    226 	{ "HTTP_METHOD_HEAD", HTTP_METHOD_HEAD },
    227 	{ "HTTP_METHOD_POST", HTTP_METHOD_POST },
    228 	{ "HTTP_METHOD_DELETE", HTTP_METHOD_DELETE },
    229 	{ "HTTP_METHOD_OPTIONS", HTTP_METHOD_OPTIONS },
    230 	{ "HTTP_METHOD_PATCH", HTTP_METHOD_PATCH },
    231 	{ "WEBSOCKET_OP_TEXT", WEBSOCKET_OP_TEXT },
    232 	{ "WEBSOCKET_OP_BINARY", WEBSOCKET_OP_BINARY },
    233 	{ "WEBSOCKET_BROADCAST_LOCAL", WEBSOCKET_BROADCAST_LOCAL },
    234 	{ "WEBSOCKET_BROADCAST_GLOBAL", WEBSOCKET_BROADCAST_GLOBAL },
    235 	{ NULL, -1 }
    236 };
    237 
    238 static PyMemAllocatorEx allocator = {
    239 	.ctx = NULL,
    240 	.malloc = python_malloc,
    241 	.calloc = python_calloc,
    242 	.realloc = python_realloc,
    243 	.free = python_free
    244 };
    245 
    246 #if defined(__linux__)
    247 #include "seccomp.h"
    248 
    249 static struct sock_filter filter_python[] = {
    250 	/* Required for kore.proc */
    251 #if defined(SYS_dup2)
    252 	KORE_SYSCALL_ALLOW(dup2),
    253 #endif
    254 #if defined(SYS_dup3)
    255 	KORE_SYSCALL_ALLOW(dup3),
    256 #endif
    257 #if defined(SYS_pipe)
    258 	KORE_SYSCALL_ALLOW(pipe),
    259 #endif
    260 #if defined(SYS_pipe2)
    261 	KORE_SYSCALL_ALLOW(pipe2),
    262 #endif
    263 	KORE_SYSCALL_ALLOW(wait4),
    264 	KORE_SYSCALL_ALLOW(execve),
    265 
    266 	/* Socket related. */
    267 	KORE_SYSCALL_ALLOW(bind),
    268 	KORE_SYSCALL_ALLOW(listen),
    269 	KORE_SYSCALL_ALLOW(sendto),
    270 	KORE_SYSCALL_ALLOW(recvfrom),
    271 	KORE_SYSCALL_ALLOW(getsockname),
    272 	KORE_SYSCALL_ALLOW(getpeername),
    273 	KORE_SYSCALL_ALLOW_ARG(socket, 0, AF_INET),
    274 	KORE_SYSCALL_ALLOW_ARG(socket, 0, AF_INET6),
    275 	KORE_SYSCALL_ALLOW_ARG(socket, 0, AF_UNIX),
    276 };
    277 
    278 #define PYSECCOMP_ACTION_ALLOW		1
    279 #define PYSECCOMP_ACTION_DENY		2
    280 
    281 #define PYSECCOMP_SYSCALL_FILTER	1
    282 #define PYSECCOMP_SYSCALL_ARG		2
    283 #define PYSECCOMP_SYSCALL_MASK		3
    284 #define PYSECCOMP_SYSCALL_FLAG		4
    285 
    286 static int	pyseccomp_filter_install(struct pyseccomp *,
    287 		    const char *, int, int, int, int);
    288 static PyObject	*pyseccomp_common_action(struct pyseccomp *, PyObject *,
    289 		    PyObject *, int, int);
    290 
    291 static struct pyseccomp			*py_seccomp = NULL;
    292 #endif
    293 
    294 static TAILQ_HEAD(, pyproc)		procs;
    295 static TAILQ_HEAD(, pyroute)		routes;
    296 static struct reqcall_list		prereq;
    297 
    298 static struct kore_pool			coro_pool;
    299 static struct kore_pool			iterobj_pool;
    300 static struct kore_pool			queue_wait_pool;
    301 static struct kore_pool			gather_coro_pool;
    302 static struct kore_pool			queue_object_pool;
    303 static struct kore_pool			gather_result_pool;
    304 
    305 static u_int64_t			coro_id;
    306 static int				coro_count;
    307 static int				coro_tracing;
    308 static struct coro_list			coro_runnable;
    309 static struct coro_list			coro_suspended;
    310 
    311 extern const char *__progname;
    312 
    313 static PyObject		*pickle = NULL;
    314 static PyObject		*kore_app = NULL;
    315 static PyObject		*pickle_dumps = NULL;
    316 static PyObject		*pickle_loads = NULL;
    317 static PyObject		*python_tracer = NULL;
    318 
    319 /* XXX */
    320 static struct python_coro		*coro_running = NULL;
    321 
    322 #if !defined(KORE_SINGLE_BINARY)
    323 const char	*kore_pymodule = NULL;
    324 #endif
    325 
    326 void
    327 kore_python_init(void)
    328 {
    329 	struct kore_runtime_call	*rcall;
    330 
    331 	coro_id = 0;
    332 	coro_count = 0;
    333 	coro_tracing = 0;
    334 
    335 	TAILQ_INIT(&prereq);
    336 
    337 	TAILQ_INIT(&procs);
    338 	TAILQ_INIT(&routes);
    339 	TAILQ_INIT(&coro_runnable);
    340 	TAILQ_INIT(&coro_suspended);
    341 
    342 	kore_pool_init(&coro_pool, "coropool", sizeof(struct python_coro), 100);
    343 
    344 	kore_pool_init(&iterobj_pool, "iterobj_pool",
    345 	    sizeof(struct pyhttp_iterobj), 100);
    346 	kore_pool_init(&queue_wait_pool, "queue_wait_pool",
    347 	    sizeof(struct pyqueue_waiting), 100);
    348 	kore_pool_init(&gather_coro_pool, "gather_coro_pool",
    349 	    sizeof(struct pygather_coro), 100);
    350 	kore_pool_init(&queue_object_pool, "queue_object_pool",
    351 	    sizeof(struct pyqueue_object), 100);
    352 	kore_pool_init(&gather_result_pool, "gather_result_pool",
    353 	    sizeof(struct pygather_result), 100);
    354 
    355 	PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &allocator);
    356 	PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &allocator);
    357 	PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &allocator);
    358 
    359 #if defined(KORE_DEBUG)
    360 	PyMem_SetupDebugHooks();
    361 #endif
    362 
    363 	kore_msg_register(KORE_PYTHON_SEND_OBJ, python_kore_recvobj);
    364 
    365 	if (PyImport_AppendInittab("kore", &python_module_init) == -1)
    366 		fatal("kore_python_init: failed to add new module");
    367 
    368 	rcall = kore_runtime_getcall("kore_python_preinit");
    369 	if (rcall != NULL) {
    370 		kore_runtime_execute(rcall);
    371 		kore_free(rcall);
    372 	}
    373 
    374 	Py_InitializeEx(0);
    375 
    376 	if ((pickle = PyImport_ImportModule("pickle")) == NULL)
    377 		fatal("failed to import pickle module");
    378 
    379 	if ((pickle_dumps = PyObject_GetAttrString(pickle, "dumps")) == NULL)
    380 		fatal("pickle module has no dumps method");
    381 
    382 	if ((pickle_loads = PyObject_GetAttrString(pickle, "loads")) == NULL)
    383 		fatal("pickle module has no loads method");
    384 
    385 #if defined(__linux__)
    386 	kore_seccomp_filter("python", filter_python,
    387 	    KORE_FILTER_LEN(filter_python));
    388 #endif
    389 
    390 #if !defined(KORE_SINGLE_BINARY)
    391 	if (kore_pymodule) {
    392 		if (!kore_configure_setting("deployment", "dev"))
    393 			fatal("failed to set initial deployment");
    394 	}
    395 #endif
    396 }
    397 
    398 void
    399 kore_python_cleanup(void)
    400 {
    401 	if (Py_IsInitialized()) {
    402 		PyErr_Clear();
    403 		Py_Finalize();
    404 	}
    405 }
    406 
    407 void
    408 kore_python_path(const char *path)
    409 {
    410 	python_append_path(path);
    411 }
    412 
    413 void
    414 kore_python_coro_run(void)
    415 {
    416 	struct pygather_op	*op;
    417 	struct python_coro	*coro;
    418 
    419 	while ((coro = TAILQ_FIRST(&coro_runnable)) != NULL) {
    420 		if (coro->state != CORO_STATE_RUNNABLE)
    421 			fatal("non-runnable coro on coro_runnable");
    422 
    423 		if (python_coro_run(coro) == KORE_RESULT_OK) {
    424 			if (coro->gatherop != NULL) {
    425 				op = coro->gatherop;
    426 				if (op->coro->request != NULL)
    427 					http_request_wakeup(op->coro->request);
    428 				else
    429 					python_coro_wakeup(op->coro);
    430 				pygather_reap_coro(op, coro);
    431 			} else {
    432 				kore_python_coro_delete(coro);
    433 			}
    434 		}
    435 	}
    436 
    437 	/*
    438 	 * Let Kore do HTTP processing so awoken coroutines run asap without
    439 	 * having to wait for a tick from the event loop.
    440 	 *
    441 	 * Maybe it is more beneficial that we track if something related
    442 	 * to HTTP requests was awoken and only run if true?
    443 	 */
    444 	http_process();
    445 
    446 #if defined(KORE_USE_CURL)
    447 	/*
    448 	 * If a coroutine fired off a curl instance, immediately
    449 	 * let it make progress.
    450 	 */
    451 	kore_curl_do_timeout();
    452 #endif
    453 }
    454 
    455 void
    456 kore_python_coro_delete(void *obj)
    457 {
    458 	struct python_coro	*coro;
    459 
    460 	coro = obj;
    461 	coro_count--;
    462 
    463 	python_coro_trace(coro->killed ? "killed" : "deleted", coro);
    464 
    465 	coro_running = coro;
    466 
    467 	if (coro->lockop != NULL) {
    468 		coro->lockop->active = 0;
    469 		TAILQ_REMOVE(&coro->lockop->lock->ops, coro->lockop, list);
    470 		Py_DECREF((PyObject *)coro->lockop);
    471 		coro->lockop = NULL;
    472 	}
    473 
    474 	Py_DECREF(coro->obj);
    475 	coro_running = NULL;
    476 
    477 	if (coro->state == CORO_STATE_RUNNABLE)
    478 		TAILQ_REMOVE(&coro_runnable, coro, list);
    479 	else
    480 		TAILQ_REMOVE(&coro_suspended, coro, list);
    481 
    482 	kore_free(coro->name);
    483 	Py_XDECREF(coro->result);
    484 
    485 	kore_pool_put(&coro_pool, coro);
    486 }
    487 
    488 int
    489 kore_python_coro_pending(void)
    490 {
    491 	return (!TAILQ_EMPTY(&coro_runnable));
    492 }
    493 
    494 void
    495 kore_python_routes_resolve(void)
    496 {
    497 	struct pyroute		*route;
    498 
    499 	while ((route = TAILQ_FIRST(&routes)) != NULL) {
    500 		TAILQ_REMOVE(&routes, route, list);
    501 		if (!python_route_install(route))
    502 			fatalx("failed to install route for %s", route->path);
    503 		Py_DECREF((PyObject *)route);
    504 	}
    505 }
    506 
    507 void
    508 kore_python_log_error(const char *function)
    509 {
    510 	const char	*sval;
    511 	PyObject	*ret, *repr, *type, *value, *traceback;
    512 
    513 	if (!PyErr_Occurred() || PyErr_ExceptionMatches(PyExc_StopIteration))
    514 		return;
    515 
    516 	PyErr_Fetch(&type, &value, &traceback);
    517 
    518 	if (type == NULL || value == NULL) {
    519 		kore_log(LOG_ERR, "unknown python exception in '%s'", function);
    520 		return;
    521 	}
    522 
    523 	if (value == NULL || !PyObject_IsInstance(value, type))
    524 		PyErr_NormalizeException(&type, &value, &traceback);
    525 
    526 	/*
    527 	 * If we're in an active coroutine and it was tied to a gather
    528 	 * operation we have to make sure we can use the Exception that
    529 	 * was thrown as the result value so we can propagate it via the
    530 	 * return list of kore.gather().
    531 	 */
    532 	if (coro_running != NULL && coro_running->gatherop != NULL) {
    533 		PyErr_SetObject(PyExc_StopIteration, value);
    534 	} else if (python_tracer != NULL) {
    535 		/*
    536 		 * Call the user-supplied tracer callback.
    537 		 */
    538 		ret = PyObject_CallFunctionObjArgs(python_tracer,
    539 		    type, value, traceback, NULL);
    540 		Py_XDECREF(ret);
    541 	} else {
    542 		if ((repr = PyObject_Repr(value)) == NULL)
    543 			sval = "unknown";
    544 		else
    545 			sval = PyUnicode_AsUTF8(repr);
    546 
    547 		kore_log(LOG_ERR,
    548 		    "uncaught exception %s in '%s'", sval, function);
    549 
    550 		Py_XDECREF(repr);
    551 	}
    552 
    553 	Py_DECREF(type);
    554 	Py_DECREF(value);
    555 	Py_XDECREF(traceback);
    556 }
    557 
    558 void
    559 kore_python_proc_reap(void)
    560 {
    561 	struct pyproc		*proc;
    562 	struct python_coro	*coro;
    563 	pid_t			child;
    564 	int			status;
    565 
    566 	for (;;) {
    567 		if ((child = waitpid(-1, &status, WNOHANG)) == -1) {
    568 			if (errno == ECHILD)
    569 				return;
    570 			if (errno == EINTR)
    571 				continue;
    572 			kore_log(LOG_NOTICE, "waitpid: %s", errno_s);
    573 			return;
    574 		}
    575 
    576 		if (child == 0)
    577 			return;
    578 
    579 		proc = NULL;
    580 
    581 		TAILQ_FOREACH(proc, &procs, list) {
    582 			if (proc->pid == child)
    583 				break;
    584 		}
    585 
    586 		if (proc == NULL)
    587 			continue;
    588 
    589 		proc->pid = -1;
    590 		proc->reaped = 1;
    591 		proc->status = status;
    592 
    593 		if (proc->timer != NULL) {
    594 			kore_timer_remove(proc->timer);
    595 			proc->timer = NULL;
    596 		}
    597 
    598 		/*
    599 		 * If someone is waiting on proc.reap() then wakeup that
    600 		 * coroutine, otherwise wakeup the coroutine that created
    601 		 * the process.
    602 		 */
    603 		if (proc->op != NULL)
    604 			coro = proc->op->coro;
    605 		else
    606 			coro = proc->coro;
    607 
    608 		if (coro->request != NULL)
    609 			http_request_wakeup(coro->request);
    610 		else
    611 			python_coro_wakeup(coro);
    612 	}
    613 }
    614 
    615 #if defined(__linux__)
    616 void
    617 kore_python_seccomp_hook(const char *method)
    618 {
    619 	struct kore_runtime	*rt;
    620 	PyObject		*func, *result;
    621 
    622 	if ((func = kore_module_getsym(method, &rt)) == NULL)
    623 		return;
    624 
    625 	if (rt->type != KORE_RUNTIME_PYTHON)
    626 		return;
    627 
    628 	py_seccomp = PyObject_New(struct pyseccomp, &pyseccomp_type);
    629 	if (py_seccomp == NULL)
    630 		fatal("failed to create seccomp object");
    631 
    632 	py_seccomp->elm = 0;
    633 	py_seccomp->filters = NULL;
    634 
    635 	result = PyObject_CallFunctionObjArgs(func,
    636 	    (PyObject *)py_seccomp, NULL);
    637 	kore_python_log_error(method);
    638 
    639 	kore_seccomp_filter("koreapp", py_seccomp->filters, py_seccomp->elm);
    640 
    641 	Py_XDECREF(result);
    642 }
    643 
    644 void
    645 kore_python_seccomp_cleanup(void)
    646 {
    647 	Py_XDECREF(py_seccomp);
    648 	py_seccomp = NULL;
    649 }
    650 
    651 static void
    652 pyseccomp_dealloc(struct pyseccomp *seccomp)
    653 {
    654 	kore_free(seccomp->filters);
    655 
    656 	seccomp->elm = 0;
    657 	seccomp->filters = NULL;
    658 }
    659 
    660 static PyObject *
    661 pyseccomp_bpf_stmt(struct pyseccomp *seccomp, PyObject *args)
    662 {
    663 	u_int32_t		k;
    664 	u_int16_t		code;
    665 	size_t			len, off;
    666 	struct sock_filter	filter[1];
    667 
    668 	if (!PyArg_ParseTuple(args, "HI", &code, &k))
    669 		return (NULL);
    670 
    671 	filter[0].k = k;
    672 	filter[0].jt = 0;
    673 	filter[0].jf = 0;
    674 	filter[0].code = code;
    675 
    676 	len = sizeof(struct sock_filter);
    677 	off = seccomp->elm * sizeof(struct sock_filter);
    678 	seccomp->filters = kore_realloc(seccomp->filters, off + len);
    679 
    680 	memcpy(seccomp->filters + off, filter, len);
    681 	seccomp->elm += 1;
    682 
    683 	Py_RETURN_NONE;
    684 }
    685 
    686 static PyObject *
    687 pyseccomp_allow(struct pyseccomp *seccomp, PyObject *args)
    688 {
    689 	const char		*syscall;
    690 
    691 	if (!PyArg_ParseTuple(args, "s", &syscall))
    692 		return (NULL);
    693 
    694 	if (!pyseccomp_filter_install(seccomp, syscall,
    695 	    PYSECCOMP_SYSCALL_FILTER, 0, 0, SECCOMP_RET_ALLOW))
    696 		return (NULL);
    697 
    698 	Py_RETURN_NONE;
    699 }
    700 
    701 static PyObject *
    702 pyseccomp_allow_arg(struct pyseccomp *seccomp, PyObject *args)
    703 {
    704 	return (pyseccomp_common_action(seccomp, args, NULL,
    705 	    PYSECCOMP_SYSCALL_ARG, PYSECCOMP_ACTION_ALLOW));
    706 }
    707 
    708 static PyObject *
    709 pyseccomp_allow_flag(struct pyseccomp *seccomp, PyObject *args)
    710 {
    711 	return (pyseccomp_common_action(seccomp, args, NULL,
    712 	    PYSECCOMP_SYSCALL_FLAG, PYSECCOMP_ACTION_ALLOW));
    713 }
    714 
    715 static PyObject *
    716 pyseccomp_allow_mask(struct pyseccomp *seccomp, PyObject *args)
    717 {
    718 	return (pyseccomp_common_action(seccomp, args, NULL,
    719 	    PYSECCOMP_SYSCALL_MASK, PYSECCOMP_ACTION_ALLOW));
    720 }
    721 
    722 static PyObject *
    723 pyseccomp_deny(struct pyseccomp *seccomp, PyObject *args, PyObject *kwargs)
    724 {
    725 	long			err;
    726 	const char		*syscall;
    727 
    728 	if (!PyArg_ParseTuple(args, "s", &syscall))
    729 		return (NULL);
    730 
    731 	err = EACCES;
    732 
    733 	if (kwargs != NULL)
    734 		python_long_from_dict(kwargs, "errno", &err);
    735 
    736 	if (!pyseccomp_filter_install(seccomp, syscall,
    737 	    PYSECCOMP_SYSCALL_FILTER, 0, 0, SECCOMP_RET_ERRNO | (int)err))
    738 		return (NULL);
    739 
    740 	Py_RETURN_NONE;
    741 }
    742 
    743 static PyObject *
    744 pyseccomp_deny_arg(struct pyseccomp *seccomp, PyObject *args, PyObject *kwargs)
    745 {
    746 	return (pyseccomp_common_action(seccomp, args, kwargs,
    747 	    PYSECCOMP_SYSCALL_ARG, PYSECCOMP_ACTION_DENY));
    748 }
    749 
    750 static PyObject *
    751 pyseccomp_deny_flag(struct pyseccomp *seccomp, PyObject *args, PyObject *kwargs)
    752 {
    753 	return (pyseccomp_common_action(seccomp, args, kwargs,
    754 	    PYSECCOMP_SYSCALL_FLAG, PYSECCOMP_ACTION_DENY));
    755 }
    756 
    757 static PyObject *
    758 pyseccomp_deny_mask(struct pyseccomp *seccomp, PyObject *args, PyObject *kwargs)
    759 {
    760 	return (pyseccomp_common_action(seccomp, args, kwargs,
    761 	    PYSECCOMP_SYSCALL_MASK, PYSECCOMP_ACTION_DENY));
    762 }
    763 
    764 static PyObject *
    765 pyseccomp_common_action(struct pyseccomp *sc, PyObject *args,
    766     PyObject *kwargs, int which, int action)
    767 {
    768 	long			err;
    769 	const char		*syscall;
    770 	int			arg, val;
    771 
    772 	if (!PyArg_ParseTuple(args, "sii", &syscall, &arg, &val))
    773 		return (NULL);
    774 
    775 	switch (action) {
    776 	case PYSECCOMP_ACTION_ALLOW:
    777 		action = SECCOMP_RET_ALLOW;
    778 		break;
    779 	case PYSECCOMP_ACTION_DENY:
    780 		err = EACCES;
    781 		if (kwargs != NULL)
    782 			python_long_from_dict(kwargs, "errno", &err);
    783 		action = SECCOMP_RET_ERRNO | (int)err;
    784 		break;
    785 	default:
    786 		fatal("%s: bad action %d", __func__, action);
    787 	}
    788 
    789 	if (!pyseccomp_filter_install(sc, syscall, which, arg, val, action))
    790 		return (NULL);
    791 
    792 	Py_RETURN_NONE;
    793 }
    794 
    795 static int
    796 pyseccomp_filter_install(struct pyseccomp *seccomp, const char *syscall,
    797     int which, int arg, int val, int action)
    798 {
    799 	struct sock_filter	*filter;
    800 	size_t			elm, len, off;
    801 
    802 	switch (which) {
    803 	case PYSECCOMP_SYSCALL_FILTER:
    804 		filter = kore_seccomp_syscall_filter(syscall, action);
    805 		break;
    806 	case PYSECCOMP_SYSCALL_ARG:
    807 		filter = kore_seccomp_syscall_arg(syscall, action, arg, val);
    808 		break;
    809 	case PYSECCOMP_SYSCALL_MASK:
    810 		filter = kore_seccomp_syscall_mask(syscall, action, arg, val);
    811 		break;
    812 	case PYSECCOMP_SYSCALL_FLAG:
    813 		filter = kore_seccomp_syscall_flag(syscall, action, arg, val);
    814 		break;
    815 	default:
    816 		fatal("%s: invalid syscall instruction %d", __func__, which);
    817 	}
    818 
    819 	if (filter == NULL) {
    820 		PyErr_Format(PyExc_RuntimeError,
    821 		    "system call '%s' does not exist", syscall);
    822 		return (KORE_RESULT_ERROR);
    823 	}
    824 
    825 	elm = 0;
    826 
    827 	/*
    828 	 * Find the number of elements in the BPF program, by looking for
    829 	 * the KORE_BPF_GUARD element.
    830 	 */
    831 	for (;;) {
    832 		if (filter[elm].code == USHRT_MAX &&
    833 		    filter[elm].jt == UCHAR_MAX &&
    834 		    filter[elm].jf == UCHAR_MAX &&
    835 		    filter[elm].k == UINT_MAX)
    836 			break;
    837 
    838 		elm++;
    839 	}
    840 
    841 	len = elm * sizeof(struct sock_filter);
    842 	off = seccomp->elm * sizeof(struct sock_filter);
    843 	seccomp->filters = kore_realloc(seccomp->filters, off + len);
    844 
    845 	memcpy(seccomp->filters + off, filter, len);
    846 	seccomp->elm += elm;
    847 
    848 	kore_free(filter);
    849 
    850 	return (KORE_RESULT_OK);
    851 }
    852 #endif
    853 
    854 static int
    855 python_long_from_dict(PyObject *dict, const char *key, long *result)
    856 {
    857 	PyObject	*obj;
    858 
    859 	if ((obj = PyDict_GetItemString(dict, key)) == NULL)
    860 		return (KORE_RESULT_ERROR);
    861 
    862 	if (!PyLong_CheckExact(obj))
    863 		return (KORE_RESULT_ERROR);
    864 
    865 	PyErr_Clear();
    866 	*result = PyLong_AsLong(obj);
    867 	if (*result == -1 && PyErr_Occurred()) {
    868 		PyErr_Clear();
    869 		return (KORE_RESULT_ERROR);
    870 	}
    871 
    872 	return (KORE_RESULT_OK);
    873 }
    874 
    875 static int
    876 python_bool_from_dict(PyObject *dict, const char *key, int *result)
    877 {
    878 	PyObject	*obj;
    879 
    880 	if ((obj = PyDict_GetItemString(dict, key)) == NULL)
    881 		return (KORE_RESULT_ERROR);
    882 
    883 	if (!PyBool_Check(obj))
    884 		return (KORE_RESULT_ERROR);
    885 
    886 	*result = (obj == Py_True);
    887 
    888 	return (KORE_RESULT_OK);
    889 }
    890 
    891 static const char *
    892 python_string_from_dict(PyObject *dict, const char *key)
    893 {
    894 	PyObject	*obj;
    895 
    896 	if ((obj = PyDict_GetItemString(dict, key)) == NULL)
    897 		return (NULL);
    898 
    899 	if (!PyUnicode_Check(obj))
    900 		return (NULL);
    901 
    902 	return (PyUnicode_AsUTF8AndSize(obj, NULL));
    903 }
    904 
    905 static PyObject *
    906 python_cmsg_to_list(struct msghdr *msg)
    907 {
    908 	struct cmsghdr		*c;
    909 	size_t			len;
    910 	Py_ssize_t		idx;
    911 	PyObject		*list, *tuple;
    912 
    913 	if ((list = PyList_New(0)) == NULL)
    914 		return (NULL);
    915 
    916 	idx = 0;
    917 
    918 	for (c = CMSG_FIRSTHDR(msg); c != NULL; c = CMSG_NXTHDR(msg, c)) {
    919 		len = c->cmsg_len - sizeof(*c);
    920 
    921 		tuple = Py_BuildValue("(Iiiy#)", len,
    922 		    c->cmsg_level, c->cmsg_type, CMSG_DATA(c), len);
    923 
    924 		if (tuple == NULL) {
    925 			Py_DECREF(list);
    926 			return (NULL);
    927 		}
    928 
    929 		/* Steals a reference to tuple. */
    930 		if (PyList_Insert(list, idx++, tuple) == -1) {
    931 			Py_DECREF(tuple);
    932 			Py_DECREF(list);
    933 			return (NULL);
    934 		}
    935 	}
    936 
    937 	return (list);
    938 }
    939 
    940 static void *
    941 python_malloc(void *ctx, size_t len)
    942 {
    943 	return (kore_malloc(len));
    944 }
    945 
    946 static void *
    947 python_calloc(void *ctx, size_t memb, size_t len)
    948 {
    949 	return (kore_calloc(memb, len));
    950 }
    951 
    952 static void *
    953 python_realloc(void *ctx, void *ptr, size_t len)
    954 {
    955 	return (kore_realloc(ptr, len));
    956 }
    957 
    958 static void
    959 python_free(void *ctx, void *ptr)
    960 {
    961 	kore_free(ptr);
    962 }
    963 
    964 static void
    965 python_module_free(struct kore_module *module)
    966 {
    967 	kore_free(module->path);
    968 	Py_DECREF(module->handle);
    969 	kore_free(module);
    970 }
    971 
    972 static void
    973 python_split_arguments(char *args, char **argv, size_t elm)
    974 {
    975 	size_t		idx;
    976 	char		*p, *line, *end;
    977 
    978 	if (elm <= 1)
    979 		fatal("not enough elements (%zu)", elm);
    980 
    981 	idx = 0;
    982 	line = args;
    983 
    984 	for (p = line; *p != '\0'; p++) {
    985 		if (idx >= elm - 1)
    986 			break;
    987 
    988 		if (*p == ' ') {
    989 			*p = '\0';
    990 			if (*line != '\0')
    991 				argv[idx++] = line;
    992 			line = p + 1;
    993 			continue;
    994 		}
    995 
    996 		if (*p != '"')
    997 			continue;
    998 
    999 		line = p + 1;
   1000 		if ((end = strchr(line, '"')) == NULL)
   1001 			break;
   1002 
   1003 		*end = '\0';
   1004 		argv[idx++] = line;
   1005 		line = end + 1;
   1006 
   1007 		while (isspace(*(unsigned char *)line))
   1008 			line++;
   1009 
   1010 		p = line;
   1011 	}
   1012 
   1013 	if (idx < elm - 1 && *line != '\0')
   1014 		argv[idx++] = line;
   1015 
   1016 	argv[idx] = NULL;
   1017 }
   1018 
   1019 static void
   1020 python_module_reload(struct kore_module *module)
   1021 {
   1022 	PyObject	*handle;
   1023 
   1024 	PyErr_Clear();
   1025 	if ((handle = PyImport_ReloadModule(module->handle)) == NULL) {
   1026 		kore_python_log_error("python_module_reload");
   1027 		return;
   1028 	}
   1029 
   1030 	Py_DECREF(module->handle);
   1031 	module->handle = handle;
   1032 }
   1033 
   1034 static void
   1035 python_module_load(struct kore_module *module)
   1036 {
   1037 	module->handle = python_import(module->path);
   1038 	if (module->handle == NULL)
   1039 		fatal("%s: failed to import module", module->path);
   1040 }
   1041 
   1042 static void *
   1043 python_module_getsym(struct kore_module *module, const char *symbol)
   1044 {
   1045 	return (python_callable(module->handle, symbol));
   1046 }
   1047 
   1048 static struct python_coro *
   1049 python_coro_create(PyObject *obj, struct http_request *req)
   1050 {
   1051 	struct python_coro	*coro;
   1052 
   1053 	if (!PyCoro_CheckExact(obj))
   1054 		fatal("%s: object is not a coroutine", __func__);
   1055 
   1056 	coro = kore_pool_get(&coro_pool);
   1057 	coro_count++;
   1058 
   1059 	coro->name = NULL;
   1060 	coro->result = NULL;
   1061 	coro->sockop = NULL;
   1062 	coro->lockop = NULL;
   1063 	coro->gatherop = NULL;
   1064 	coro->exception = NULL;
   1065 	coro->exception_msg = NULL;
   1066 
   1067 	coro->obj = obj;
   1068 	coro->killed = 0;
   1069 	coro->request = req;
   1070 	coro->id = coro_id++;
   1071 	coro->state = CORO_STATE_RUNNABLE;
   1072 
   1073 	TAILQ_INSERT_TAIL(&coro_runnable, coro, list);
   1074 
   1075 	if (coro->request != NULL)
   1076 		http_request_sleep(coro->request);
   1077 
   1078 	python_coro_trace("created", coro);
   1079 
   1080 	return (coro);
   1081 }
   1082 
   1083 static int
   1084 python_coro_run(struct python_coro *coro)
   1085 {
   1086 	PySendResult	res;
   1087 	PyObject	*item;
   1088 	PyObject	*type, *traceback;
   1089 
   1090 	if (coro->state != CORO_STATE_RUNNABLE)
   1091 		fatal("non-runnable coro attempted to run");
   1092 
   1093 	coro_running = coro;
   1094 
   1095 	for (;;) {
   1096 		python_coro_trace("running", coro);
   1097 
   1098 		PyErr_Clear();
   1099 #if PY_VERSION_HEX < 0x030A0000
   1100 		res = PYGEN_RETURN;
   1101 		item = _PyGen_Send((PyGenObject *)coro->obj, NULL);
   1102 #else
   1103 		/*
   1104 		 * Python 3.10.x its PyIter_Send() will return a PYGEN_ERROR
   1105 		 * if the coro returned (instead of yielding) and the result
   1106 		 * ends up being Py_None. This means the returned item is
   1107 		 * NULL but no StopIteration exception has occurred.
   1108 		 */
   1109 		res = PyIter_Send(coro->obj, NULL, &item);
   1110 #endif
   1111 		if (item == NULL || res == PYGEN_ERROR) {
   1112 			Py_XDECREF(item);
   1113 			if (coro->gatherop == NULL && PyErr_Occurred() &&
   1114 			    PyErr_ExceptionMatches(PyExc_StopIteration)) {
   1115 				PyErr_Fetch(&type, &coro->result, &traceback);
   1116 				Py_DECREF(type);
   1117 				Py_XDECREF(traceback);
   1118 			} else if (PyErr_Occurred()) {
   1119 				kore_python_log_error("coroutine");
   1120 				if (coro->request != NULL) {
   1121 					http_response(coro->request,
   1122 					    HTTP_STATUS_INTERNAL_ERROR,
   1123 					    NULL, 0);
   1124 				}
   1125 			}
   1126 
   1127 			coro_running = NULL;
   1128 			return (KORE_RESULT_OK);
   1129 		}
   1130 
   1131 #if PY_VERSION_HEX >= 0x030A0000
   1132 		if (res == PYGEN_RETURN) {
   1133 			coro->result = item;
   1134 			coro_running = NULL;
   1135 			return (KORE_RESULT_OK);
   1136 		}
   1137 #endif
   1138 
   1139 		if (item == Py_None) {
   1140 			Py_DECREF(item);
   1141 			break;
   1142 		}
   1143 
   1144 		Py_DECREF(item);
   1145 	}
   1146 
   1147 	python_coro_suspend(coro);
   1148 	coro_running = NULL;
   1149 
   1150 	if (coro->request != NULL)
   1151 		http_request_sleep(coro->request);
   1152 
   1153 	return (KORE_RESULT_RETRY);
   1154 }
   1155 
   1156 static void
   1157 python_coro_wakeup(struct python_coro *coro)
   1158 {
   1159 	if (coro->state != CORO_STATE_SUSPENDED)
   1160 		return;
   1161 
   1162 	coro->state = CORO_STATE_RUNNABLE;
   1163 	TAILQ_REMOVE(&coro_suspended, coro, list);
   1164 	TAILQ_INSERT_TAIL(&coro_runnable, coro, list);
   1165 
   1166 	python_coro_trace("wokeup", coro);
   1167 }
   1168 
   1169 static void
   1170 python_coro_suspend(struct python_coro *coro)
   1171 {
   1172 	if (coro->state != CORO_STATE_RUNNABLE)
   1173 		return;
   1174 
   1175 	coro->state = CORO_STATE_SUSPENDED;
   1176 	TAILQ_REMOVE(&coro_runnable, coro, list);
   1177 	TAILQ_INSERT_TAIL(&coro_suspended, coro, list);
   1178 
   1179 	python_coro_trace("suspended", coro);
   1180 }
   1181 
   1182 static void
   1183 python_coro_trace(const char *label, struct python_coro *coro)
   1184 {
   1185 	int			line;
   1186 	PyGenObject		*gen;
   1187 	PyCodeObject		*code;
   1188 	const char		*func, *fname, *file;
   1189 
   1190 	if (coro_tracing == 0)
   1191 		return;
   1192 
   1193 	gen = (PyGenObject *)coro->obj;
   1194 
   1195 	if (gen->gi_frame != NULL && gen->gi_frame->f_code != NULL) {
   1196 		code = gen->gi_frame->f_code;
   1197 		func = PyUnicode_AsUTF8AndSize(code->co_name, NULL);
   1198 		file = PyUnicode_AsUTF8AndSize(code->co_filename, NULL);
   1199 
   1200 		if ((fname = strrchr(file, '/')) == NULL)
   1201 			fname = file;
   1202 		else
   1203 			fname++;
   1204 	} else {
   1205 		func = "unknown";
   1206 		fname = "unknown";
   1207 	}
   1208 
   1209 	if (gen->gi_frame != NULL)
   1210 		line = PyFrame_GetLineNumber(gen->gi_frame);
   1211 	else
   1212 		line = -1;
   1213 
   1214 	if (coro->name) {
   1215 		kore_log(LOG_NOTICE, "coro '%s' %s <%s> @ [%s:%d]",
   1216 		    coro->name, label, func, fname, line);
   1217 	} else {
   1218 		kore_log(LOG_NOTICE, "coro %" PRIu64 " %s <%s> @ [%s:%d]",
   1219 		    coro->id, label, func, fname, line);
   1220 	}
   1221 }
   1222 
   1223 static void
   1224 pyconnection_dealloc(struct pyconnection *pyc)
   1225 {
   1226 	PyObject_Del((PyObject *)pyc);
   1227 }
   1228 
   1229 static void
   1230 pyhttp_dealloc(struct pyhttp_request *pyreq)
   1231 {
   1232 	Py_XDECREF(pyreq->dict);
   1233 	Py_XDECREF(pyreq->data);
   1234 	PyObject_Del((PyObject *)pyreq);
   1235 }
   1236 
   1237 static void
   1238 pyhttp_file_dealloc(struct pyhttp_file *pyfile)
   1239 {
   1240 	PyObject_Del((PyObject *)pyfile);
   1241 }
   1242 
   1243 static int
   1244 python_runtime_http_request(void *addr, struct http_request *req)
   1245 {
   1246 	int			ret, idx, cnt;
   1247 	PyObject		*pyret, *args, *callable;
   1248 	PyObject		*cargs[HTTP_CAPTURE_GROUPS + 1];
   1249 
   1250 	if (req->py_coro != NULL) {
   1251 		python_coro_wakeup(req->py_coro);
   1252 		if (python_coro_run(req->py_coro) == KORE_RESULT_OK) {
   1253 			kore_python_coro_delete(req->py_coro);
   1254 			req->py_coro = NULL;
   1255 
   1256 			if (req->fsm_state != PYHTTP_STATE_PREPROCESS)
   1257 				return (KORE_RESULT_OK);
   1258 		}
   1259 		return (KORE_RESULT_RETRY);
   1260 	}
   1261 
   1262 	switch (req->fsm_state) {
   1263 	case PYHTTP_STATE_INIT:
   1264 		req->py_rqnext = TAILQ_FIRST(&prereq);
   1265 		req->fsm_state = PYHTTP_STATE_PREPROCESS;
   1266 		if (req->py_req == NULL) {
   1267 			if ((req->py_req = pyhttp_request_alloc(req)) == NULL)
   1268 				fatal("%s: pyreq alloc failed", __func__);
   1269 		}
   1270 		/* fallthrough */
   1271 	case PYHTTP_STATE_PREPROCESS:
   1272 		ret = pyhttp_preprocess(req);
   1273 		switch (ret) {
   1274 		case KORE_RESULT_OK:
   1275 			req->fsm_state = PYHTTP_STATE_RUN;
   1276 			break;
   1277 		case KORE_RESULT_RETRY:
   1278 			return (KORE_RESULT_RETRY);
   1279 		case KORE_RESULT_ERROR:
   1280 			return (KORE_RESULT_OK);
   1281 		default:
   1282 			fatal("invalid state pyhttp state %d", req->fsm_state);
   1283 		}
   1284 		/* fallthrough */
   1285 	case PYHTTP_STATE_RUN:
   1286 		break;
   1287 	}
   1288 
   1289 	cnt = 0;
   1290 	callable = (PyObject *)addr;
   1291 
   1292 	/* starts at 1 to skip the full path. */
   1293 	if (req->rt->type == HANDLER_TYPE_DYNAMIC) {
   1294 		for (idx = 1; idx < HTTP_CAPTURE_GROUPS - 1; idx++) {
   1295 			if (req->cgroups[idx].rm_so == -1 ||
   1296 			    req->cgroups[idx].rm_eo == -1)
   1297 				break;
   1298 
   1299 			cargs[cnt] = PyUnicode_FromStringAndSize(req->path +
   1300 			    req->cgroups[idx].rm_so,
   1301 			    req->cgroups[idx].rm_eo - req->cgroups[idx].rm_so);
   1302 
   1303 			if (cargs[cnt] == NULL) {
   1304 				while (cnt >= 0)
   1305 					Py_XDECREF(cargs[cnt--]);
   1306 				kore_python_log_error("http request");
   1307 				http_response(req,
   1308 				    HTTP_STATUS_INTERNAL_ERROR, NULL, 0);
   1309 				return (KORE_RESULT_OK);
   1310 			}
   1311 
   1312 			cnt++;
   1313 		}
   1314 	}
   1315 
   1316 	cargs[cnt] = NULL;
   1317 
   1318 	if ((args = PyTuple_New(cnt + 1)) == NULL)
   1319 		fatal("%s: PyTuple_New failed", __func__);
   1320 
   1321 	Py_INCREF(req->py_req);
   1322 	if (PyTuple_SetItem(args, 0, req->py_req) != 0)
   1323 		fatal("python_runtime_http_request: PyTuple_SetItem failed");
   1324 
   1325 	for (idx = 0; cargs[idx] != NULL; idx++) {
   1326 		if (PyTuple_SetItem(args, 1 + idx, cargs[idx]) != 0)
   1327 			fatal("%s: PyTuple_SetItem failed (%d)", __func__, idx);
   1328 	}
   1329 
   1330 	PyErr_Clear();
   1331 	pyret = PyObject_Call(callable, args, NULL);
   1332 	Py_DECREF(args);
   1333 
   1334 	if (pyret == NULL) {
   1335 		kore_python_log_error("python_runtime_http_request");
   1336 		http_response(req, HTTP_STATUS_INTERNAL_ERROR, NULL, 0);
   1337 		return (KORE_RESULT_OK);
   1338 	}
   1339 
   1340 	if (PyCoro_CheckExact(pyret)) {
   1341 		req->py_coro = python_coro_create(pyret, req);
   1342 		if (python_coro_run(req->py_coro) == KORE_RESULT_OK) {
   1343 			http_request_wakeup(req);
   1344 			kore_python_coro_delete(req->py_coro);
   1345 			req->py_coro = NULL;
   1346 			return (KORE_RESULT_OK);
   1347 		}
   1348 		return (KORE_RESULT_RETRY);
   1349 	}
   1350 
   1351 	if (pyret != Py_None)
   1352 		fatal("python_runtime_http_request: unexpected return type");
   1353 
   1354 	Py_DECREF(pyret);
   1355 
   1356 	return (KORE_RESULT_OK);
   1357 }
   1358 
   1359 static void
   1360 python_runtime_http_request_free(void *addr, struct http_request *req)
   1361 {
   1362 	PyObject	*ret;
   1363 
   1364 	if (req->py_req == NULL) {
   1365 		if ((req->py_req = pyhttp_request_alloc(req)) == NULL)
   1366 			fatal("%s: pyreq alloc failed", __func__);
   1367 	}
   1368 
   1369 	PyErr_Clear();
   1370 	ret = PyObject_CallFunctionObjArgs(addr, req->py_req, NULL);
   1371 
   1372 	if (ret == NULL)
   1373 		kore_python_log_error("python_runtime_http_request_free");
   1374 
   1375 	Py_XDECREF(ret);
   1376 }
   1377 
   1378 static void
   1379 python_runtime_http_body_chunk(void *addr, struct http_request *req,
   1380     const void *data, size_t len)
   1381 {
   1382 	PyObject	*args, *ret;
   1383 
   1384 	if (req->py_req == NULL) {
   1385 		if ((req->py_req = pyhttp_request_alloc(req)) == NULL)
   1386 			fatal("%s: pyreq alloc failed", __func__);
   1387 	}
   1388 
   1389 	if ((args = Py_BuildValue("(Oy#)", req->py_req, data, len)) == NULL) {
   1390 		kore_python_log_error("python_runtime_http_body_chunk");
   1391 		return;
   1392 	}
   1393 
   1394 	PyErr_Clear();
   1395 	ret = PyObject_Call(addr, args, NULL);
   1396 
   1397 	if (ret == NULL)
   1398 		kore_python_log_error("python_runtime_http_body_chunk");
   1399 
   1400 	Py_XDECREF(ret);
   1401 	Py_DECREF(args);
   1402 }
   1403 
   1404 static int
   1405 python_runtime_validator(void *addr, struct http_request *req, const void *data)
   1406 {
   1407 	int			ret;
   1408 	struct python_coro	*coro;
   1409 	PyObject		*pyret, *args, *callable, *arg;
   1410 
   1411 	if (req->py_req == NULL) {
   1412 		if ((req->py_req = pyhttp_request_alloc(req)) == NULL)
   1413 			fatal("%s: pyreq alloc failed", __func__);
   1414 	}
   1415 
   1416 	if (req->py_validator != NULL) {
   1417 		coro = req->py_validator;
   1418 		python_coro_wakeup(coro);
   1419 		if (python_coro_run(coro) == KORE_RESULT_OK) {
   1420 			ret = python_validator_check(coro->result);
   1421 			kore_python_coro_delete(coro);
   1422 			req->py_validator = NULL;
   1423 			return (ret);
   1424 		}
   1425 
   1426 		return (KORE_RESULT_RETRY);
   1427 	}
   1428 
   1429 	callable = (PyObject *)addr;
   1430 
   1431 	if (req->flags & HTTP_VALIDATOR_IS_REQUEST) {
   1432 		if ((args = PyTuple_New(1)) == NULL)
   1433 			fatal("%s: PyTuple_New failed", __func__);
   1434 
   1435 		Py_INCREF(req->py_req);
   1436 		if (PyTuple_SetItem(args, 0, req->py_req) != 0)
   1437 			fatal("%s: PyTuple_SetItem failed", __func__);
   1438 	} else {
   1439 		if ((arg = PyUnicode_FromString(data)) == NULL)
   1440 			fatal("python_runtime_validator: PyUnicode failed");
   1441 
   1442 		if ((args = PyTuple_New(2)) == NULL)
   1443 			fatal("%s: PyTuple_New failed", __func__);
   1444 
   1445 		Py_INCREF(req->py_req);
   1446 		if (PyTuple_SetItem(args, 0, req->py_req) != 0 ||
   1447 		    PyTuple_SetItem(args, 1, arg) != 0)
   1448 			fatal("%s: PyTuple_SetItem failed", __func__);
   1449 	}
   1450 
   1451 	PyErr_Clear();
   1452 	pyret = PyObject_Call(callable, args, NULL);
   1453 	Py_DECREF(args);
   1454 
   1455 	if (pyret == NULL) {
   1456 		kore_python_log_error("python_runtime_validator");
   1457 		fatal("failed to execute python call");
   1458 	}
   1459 
   1460 	if (PyCoro_CheckExact(pyret)) {
   1461 		coro = python_coro_create(pyret, req);
   1462 		req->py_validator = coro;
   1463 		if (python_coro_run(coro) == KORE_RESULT_OK) {
   1464 			http_request_wakeup(req);
   1465 			ret = python_validator_check(coro->result);
   1466 			kore_python_coro_delete(coro);
   1467 			req->py_validator = NULL;
   1468 			return (ret);
   1469 		}
   1470 		return (KORE_RESULT_RETRY);
   1471 	}
   1472 
   1473 	ret = python_validator_check(pyret);
   1474 	Py_DECREF(pyret);
   1475 
   1476 	return (ret);
   1477 }
   1478 
   1479 static int
   1480 python_validator_check(PyObject *obj)
   1481 {
   1482 	int	ret;
   1483 
   1484 	if (obj == NULL)
   1485 		return (KORE_RESULT_ERROR);
   1486 
   1487 	if (!PyBool_Check(obj)) {
   1488 		kore_log(LOG_WARNING,
   1489 		    "validator did not return True/False");
   1490 		ret = KORE_RESULT_ERROR;
   1491 	}
   1492 
   1493 	if (obj == Py_True)
   1494 		ret = KORE_RESULT_OK;
   1495 	else
   1496 		ret = KORE_RESULT_ERROR;
   1497 
   1498 	return (ret);
   1499 }
   1500 
   1501 static void
   1502 python_runtime_wsmessage(void *addr, struct connection *c, u_int8_t op,
   1503     const void *data, size_t len)
   1504 {
   1505 	PyObject	*callable, *args, *pyret, *pyc, *pyop, *pydata;
   1506 
   1507 	callable = (PyObject *)addr;
   1508 
   1509 	if ((pyc = pyconnection_alloc(c)) == NULL)
   1510 		fatal("python_runtime_wsmessage: pyc alloc failed");
   1511 
   1512 	if ((pyop = PyLong_FromLong((long)op)) == NULL)
   1513 		fatal("python_runtime_wsmessage: PyLong_FromLong failed");
   1514 
   1515 	switch (op) {
   1516 	case WEBSOCKET_OP_TEXT:
   1517 		if ((pydata = PyUnicode_FromStringAndSize(data, len)) == NULL)
   1518 			fatal("wsmessage: PyUnicode_AsUTF8AndSize failed");
   1519 		break;
   1520 	case WEBSOCKET_OP_BINARY:
   1521 		if ((pydata = PyBytes_FromStringAndSize(data, len)) == NULL)
   1522 			fatal("wsmessage: PyBytes_FromString failed");
   1523 		break;
   1524 	default:
   1525 		fatal("python_runtime_wsmessage: invalid op");
   1526 	}
   1527 
   1528 	if ((args = PyTuple_New(3)) == NULL)
   1529 		fatal("python_runtime_wsmessage: PyTuple_New failed");
   1530 
   1531 	if (PyTuple_SetItem(args, 0, pyc) != 0 ||
   1532 	    PyTuple_SetItem(args, 1, pyop) != 0 ||
   1533 	    PyTuple_SetItem(args, 2, pydata) != 0)
   1534 		fatal("python_runtime_wsmessage: PyTuple_SetItem failed");
   1535 
   1536 	PyErr_Clear();
   1537 	pyret = PyObject_Call(callable, args, NULL);
   1538 	Py_DECREF(args);
   1539 
   1540 	if (pyret == NULL) {
   1541 		kore_python_log_error("python_runtime_wsconnect");
   1542 		fatal("failed to execute python call");
   1543 	}
   1544 
   1545 	Py_DECREF(pyret);
   1546 }
   1547 
   1548 static void
   1549 python_runtime_execute(void *addr)
   1550 {
   1551 	PyObject	*callable, *args, *pyret;
   1552 
   1553 	callable = (PyObject *)addr;
   1554 
   1555 	if ((args = PyTuple_New(0)) == NULL)
   1556 		fatal("python_runtime_execute: PyTuple_New failed");
   1557 
   1558 	PyErr_Clear();
   1559 	pyret = PyObject_Call(callable, args, NULL);
   1560 	Py_DECREF(args);
   1561 
   1562 	if (pyret == NULL) {
   1563 		kore_python_log_error("python_runtime_execute");
   1564 		fatal("failed to execute python call");
   1565 	}
   1566 
   1567 	Py_DECREF(pyret);
   1568 }
   1569 
   1570 static void
   1571 python_runtime_configure(void *addr, int argc, char **argv)
   1572 {
   1573 	int		i;
   1574 	PyObject	*callable, *args, *pyret, *pyarg, *list;
   1575 
   1576 	callable = (PyObject *)addr;
   1577 
   1578 	if ((args = PyTuple_New(1)) == NULL)
   1579 		fatal("python_runtime_configure: PyTuple_New failed");
   1580 
   1581 	if ((list = PyList_New(argc + 1)) == NULL)
   1582 		fatal("python_runtime_configure: PyList_New failed");
   1583 
   1584 	if ((pyarg = PyUnicode_FromString(__progname)) == NULL)
   1585 		fatal("python_runtime_configure: PyUnicode_FromString");
   1586 
   1587 	if (PyList_SetItem(list, 0, pyarg) == -1)
   1588 		fatal("python_runtime_configure: PyList_SetItem");
   1589 
   1590 	for (i = 0; i < argc; i++) {
   1591 		if ((pyarg = PyUnicode_FromString(argv[i])) == NULL)
   1592 			fatal("python_runtime_configure: PyUnicode_FromString");
   1593 
   1594 		if (PyList_SetItem(list, i + 1, pyarg) == -1)
   1595 			fatal("python_runtime_configure: PyList_SetItem");
   1596 	}
   1597 
   1598 	if (PyTuple_SetItem(args, 0, list) != 0)
   1599 		fatal("python_runtime_configure: PyTuple_SetItem");
   1600 
   1601 	PyErr_Clear();
   1602 	pyret = PyObject_Call(callable, args, NULL);
   1603 	Py_DECREF(args);
   1604 
   1605 	if (pyret == NULL) {
   1606 		kore_python_log_error("python_runtime_configure");
   1607 		fatal("failed to configure your application");
   1608 	}
   1609 
   1610 	Py_DECREF(pyret);
   1611 }
   1612 
   1613 static int
   1614 python_runtime_onload(void *addr, int action)
   1615 {
   1616 	int		ret;
   1617 	PyObject	*pyret, *args, *pyact, *callable;
   1618 
   1619 	callable = (PyObject *)addr;
   1620 
   1621 	if ((pyact = PyLong_FromLong(action)) == NULL)
   1622 		fatal("python_runtime_onload: PyLong_FromLong failed");
   1623 
   1624 	if ((args = PyTuple_New(1)) == NULL)
   1625 		fatal("python_runtime_onload: PyTuple_New failed");
   1626 
   1627 	if (PyTuple_SetItem(args, 0, pyact) != 0)
   1628 		fatal("python_runtime_onload: PyTuple_SetItem failed");
   1629 
   1630 	PyErr_Clear();
   1631 	pyret = PyObject_Call(callable, args, NULL);
   1632 	Py_DECREF(args);
   1633 
   1634 	if (pyret == NULL) {
   1635 		kore_python_log_error("python_runtime_onload");
   1636 		return (KORE_RESULT_ERROR);
   1637 	}
   1638 
   1639 	if (!PyLong_Check(pyret))
   1640 		fatal("python_runtime_onload: unexpected return type");
   1641 
   1642 	ret = (int)PyLong_AsLong(pyret);
   1643 	Py_DECREF(pyret);
   1644 
   1645 	return (ret);
   1646 }
   1647 
   1648 static void
   1649 python_runtime_connect(void *addr, struct connection *c)
   1650 {
   1651 	PyObject	*pyc, *pyret, *args, *callable;
   1652 
   1653 	callable = (PyObject *)addr;
   1654 
   1655 	if ((pyc = pyconnection_alloc(c)) == NULL)
   1656 		fatal("python_runtime_connect: pyc alloc failed");
   1657 
   1658 	if ((args = PyTuple_New(1)) == NULL)
   1659 		fatal("python_runtime_connect: PyTuple_New failed");
   1660 
   1661 	if (PyTuple_SetItem(args, 0, pyc) != 0)
   1662 		fatal("python_runtime_connect: PyTuple_SetItem failed");
   1663 
   1664 	PyErr_Clear();
   1665 	pyret = PyObject_Call(callable, args, NULL);
   1666 	Py_DECREF(args);
   1667 
   1668 	if (pyret == NULL) {
   1669 		kore_python_log_error("python_runtime_connect");
   1670 		kore_connection_disconnect(c);
   1671 	}
   1672 
   1673 	Py_DECREF(pyret);
   1674 }
   1675 
   1676 static void
   1677 python_runtime_signal(void *addr, int sig)
   1678 {
   1679 	PyObject	*obj, *ret;
   1680 
   1681 	if ((obj = Py_BuildValue("i", sig)) == NULL) {
   1682 		kore_python_log_error("python_runtime_signal");
   1683 		return;
   1684 	}
   1685 
   1686 	ret = PyObject_CallFunctionObjArgs(addr, obj, NULL);
   1687 
   1688 	Py_DECREF(obj);
   1689 	Py_XDECREF(ret);
   1690 }
   1691 
   1692 PyMODINIT_FUNC
   1693 python_module_init(void)
   1694 {
   1695 	int			i;
   1696 	struct pyconfig		*config;
   1697 	PyObject		*pykore;
   1698 
   1699 	if ((pykore = PyModule_Create(&pykore_module)) == NULL)
   1700 		fatal("python_module_init: failed to setup pykore module");
   1701 
   1702 	python_push_type("pyproc", pykore, &pyproc_type);
   1703 	python_push_type("pylock", pykore, &pylock_type);
   1704 	python_push_type("pytimer", pykore, &pytimer_type);
   1705 	python_push_type("pyqueue", pykore, &pyqueue_type);
   1706 	python_push_type("pyroute", pykore, &pyroute_type);
   1707 	python_push_type("pysocket", pykore, &pysocket_type);
   1708 	python_push_type("pydomain", pykore, &pydomain_type);
   1709 	python_push_type("pyconnection", pykore, &pyconnection_type);
   1710 
   1711 #if defined(__linux__)
   1712 	python_push_type("pyseccomp", pykore, &pyseccomp_type);
   1713 #endif
   1714 
   1715 #if defined(KORE_USE_CURL)
   1716 	python_push_type("pycurlhandle", pykore, &pycurl_handle_type);
   1717 	python_push_type("pyhttpclient", pykore, &pyhttp_client_type);
   1718 
   1719 	for (i = 0; py_curlopt[i].name != NULL; i++) {
   1720 		python_push_integer(pykore, py_curlopt[i].name,
   1721 		    py_curlopt[i].value);
   1722 	}
   1723 #endif
   1724 
   1725 	python_push_type("pyhttp_file", pykore, &pyhttp_file_type);
   1726 	python_push_type("pyhttp_request", pykore, &pyhttp_request_type);
   1727 
   1728 	for (i = 0; python_integers[i].symbol != NULL; i++) {
   1729 		python_push_integer(pykore, python_integers[i].symbol,
   1730 		    python_integers[i].value);
   1731 	}
   1732 
   1733 	if ((config = PyObject_New(struct pyconfig, &pyconfig_type)) == NULL)
   1734 		fatal("failed to create config object");
   1735 
   1736 	if (PyObject_SetAttrString(pykore, "config", (PyObject *)config) == -1)
   1737 		fatal("failed to add config object");
   1738 
   1739 	return (pykore);
   1740 }
   1741 
   1742 static int
   1743 pyconfig_setattr(PyObject *self, PyObject *attr, PyObject *val)
   1744 {
   1745 	char		*v;
   1746 	int		ret;
   1747 	PyObject	*repr;
   1748 	const char	*name, *value;
   1749 
   1750 	ret = -1;
   1751 	repr = NULL;
   1752 
   1753 	if (!PyUnicode_Check(attr))
   1754 		fatal("setattr: attribute name not a unicode string");
   1755 
   1756 	if (PyLong_CheckExact(val)) {
   1757 		if ((repr = PyObject_Repr(val)) == NULL)
   1758 			return (-1);
   1759 		value = PyUnicode_AsUTF8(repr);
   1760 	} else if (PyUnicode_CheckExact(val)) {
   1761 		value = PyUnicode_AsUTF8(val);
   1762 	} else if (PyBool_Check(val)) {
   1763 		if (val == Py_False)
   1764 			value = "False";
   1765 		else
   1766 			value = "True";
   1767 	} else {
   1768 		fatal("invalid object, config expects integer, bool or string");
   1769 	}
   1770 
   1771 	name = PyUnicode_AsUTF8(attr);
   1772 	v = kore_strdup(value);
   1773 
   1774 	if (!kore_configure_setting(name, v)) {
   1775 		ret = -1;
   1776 		PyErr_SetString(PyExc_RuntimeError,
   1777 		    "configured cannot be changed at runtime");
   1778 	} else {
   1779 		ret = 0;
   1780 	}
   1781 
   1782 	kore_free(v);
   1783 
   1784 	Py_XDECREF(repr);
   1785 
   1786 	return (ret);
   1787 }
   1788 
   1789 static void
   1790 python_append_path(const char *path)
   1791 {
   1792 	PyObject	*mpath, *spath;
   1793 
   1794 	if ((mpath = PyUnicode_FromString(path)) == NULL)
   1795 		fatal("python_append_path: PyUnicode_FromString failed");
   1796 
   1797 	if ((spath = PySys_GetObject("path")) == NULL)
   1798 		fatal("python_append_path: PySys_GetObject failed");
   1799 
   1800 	PyList_Append(spath, mpath);
   1801 	Py_DECREF(mpath);
   1802 }
   1803 
   1804 static void
   1805 python_push_type(const char *name, PyObject *module, PyTypeObject *type)
   1806 {
   1807 	if (PyType_Ready(type) == -1)
   1808 		fatal("python_push_type: failed to ready %s", name);
   1809 
   1810 	Py_INCREF(type);
   1811 
   1812 	if (PyModule_AddObject(module, name, (PyObject *)type) == -1)
   1813 		fatal("python_push_type: failed to push %s", name);
   1814 }
   1815 
   1816 static void
   1817 python_push_integer(PyObject *module, const char *name, long value)
   1818 {
   1819 	if (PyModule_AddIntConstant(module, name, value) == -1)
   1820 		fatal("python_push_integer: failed to add %s", name);
   1821 }
   1822 
   1823 #if defined(KORE_USE_PGSQL)
   1824 static PyObject *
   1825 python_kore_pgsql_register(PyObject *self, PyObject *args)
   1826 {
   1827 	const char	*db, *conninfo;
   1828 
   1829 	if (!PyArg_ParseTuple(args, "ss", &db, &conninfo))
   1830 		return (NULL);
   1831 
   1832 	(void)kore_pgsql_register(db, conninfo);
   1833 
   1834 	Py_RETURN_TRUE;
   1835 }
   1836 #endif
   1837 
   1838 static PyObject *
   1839 python_kore_app(PyObject *self, PyObject *args)
   1840 {
   1841 	PyObject	*obj;
   1842 
   1843 	if (!PyArg_ParseTuple(args, "O", &obj)) {
   1844 		PyErr_Clear();
   1845 
   1846 		if (kore_app == NULL)
   1847 			Py_RETURN_NONE;
   1848 
   1849 		Py_INCREF(kore_app);
   1850 		return (kore_app);
   1851 	}
   1852 
   1853 	Py_XDECREF(kore_app);
   1854 
   1855 	kore_app = obj;
   1856 	Py_INCREF(kore_app);
   1857 
   1858 	Py_RETURN_TRUE;
   1859 }
   1860 
   1861 static PyObject *
   1862 python_kore_log(PyObject *self, PyObject *args)
   1863 {
   1864 	int		prio;
   1865 	const char	*message;
   1866 
   1867 	if (!PyArg_ParseTuple(args, "is", &prio, &message))
   1868 		return (NULL);
   1869 
   1870 	kore_log(prio, "%s", message);
   1871 
   1872 	Py_RETURN_TRUE;
   1873 }
   1874 
   1875 static PyObject *
   1876 python_kore_time(PyObject *self, PyObject *args)
   1877 {
   1878 	u_int64_t	now;
   1879 
   1880 	now = kore_time_ms();
   1881 
   1882 	return (PyLong_FromUnsignedLongLong(now));
   1883 }
   1884 
   1885 static PyObject *
   1886 python_kore_server(PyObject *self, PyObject *args, PyObject *kwargs)
   1887 {
   1888 	struct kore_server	*srv;
   1889 	const char		*name, *ip, *port, *path;
   1890 
   1891 	if (kwargs == NULL) {
   1892 		PyErr_SetString(PyExc_RuntimeError, "missing keyword args");
   1893 		return (NULL);
   1894 	}
   1895 
   1896 	ip = python_string_from_dict(kwargs, "ip");
   1897 	path = python_string_from_dict(kwargs, "path");
   1898 
   1899 	if (ip == NULL && path == NULL) {
   1900 		PyErr_SetString(PyExc_RuntimeError,
   1901 		    "missing ip or path keywords");
   1902 		return (NULL);
   1903 	}
   1904 
   1905 	if (ip != NULL && path != NULL) {
   1906 		PyErr_SetString(PyExc_RuntimeError, "ip/path are exclusive");
   1907 		return (NULL);
   1908 	}
   1909 
   1910 	name = python_string_from_dict(kwargs, "name");
   1911 	if (name == NULL)
   1912 		name = "default";
   1913 
   1914 	if ((srv = kore_server_lookup(name)) != NULL) {
   1915 		PyErr_Format(PyExc_RuntimeError,
   1916 		    "server '%s' already exist", name);
   1917 		return (NULL);
   1918 	}
   1919 
   1920 	srv = kore_server_create(name);
   1921 	python_bool_from_dict(kwargs, "tls", &srv->tls);
   1922 
   1923 	if (srv->tls && !kore_tls_supported()) {
   1924 		kore_server_free(srv);
   1925 		PyErr_SetString(PyExc_RuntimeError,
   1926 		    "TLS not supported in this Kore build");
   1927 		return (NULL);
   1928 	}
   1929 
   1930 	if (ip != NULL) {
   1931 		if ((port = python_string_from_dict(kwargs, "port")) == NULL) {
   1932 			kore_server_free(srv);
   1933 			PyErr_SetString(PyExc_RuntimeError,
   1934 			    "missing or invalid 'port' keyword");
   1935 			return (NULL);
   1936 		}
   1937 
   1938 		if (!kore_server_bind(srv, ip, port, NULL)) {
   1939 			PyErr_Format(PyExc_RuntimeError,
   1940 			    "failed to bind to '%s:%s'", ip, port);
   1941 			return (NULL);
   1942 		}
   1943 	} else {
   1944 		if (!kore_server_bind_unix(srv, path, NULL)) {
   1945 			PyErr_Format(PyExc_RuntimeError,
   1946 			    "failed to bind to '%s'", path);
   1947 			return (NULL);
   1948 		}
   1949 	}
   1950 
   1951 	kore_server_finalize(srv);
   1952 
   1953 	Py_RETURN_NONE;
   1954 }
   1955 
   1956 static PyObject *
   1957 python_kore_privsep(PyObject *self, PyObject *args, PyObject *kwargs)
   1958 {
   1959 	struct kore_privsep	*ps;
   1960 	const char		*val;
   1961 	PyObject		*skip, *obj;
   1962 	Py_ssize_t		list_len, idx;
   1963 
   1964 	if (!PyArg_ParseTuple(args, "s", &val))
   1965 		return (NULL);
   1966 
   1967 	if (!strcmp(val, "worker")) {
   1968 		ps = &worker_privsep;
   1969 	} else if (!strcmp(val, "keymgr")) {
   1970 		ps = &keymgr_privsep;
   1971 #if defined(KORE_USE_ACME)
   1972 	} else if (!strcmp(val, "acme")) {
   1973 		ps = &acme_privsep;
   1974 #endif
   1975 	} else {
   1976 		PyErr_Format(PyExc_RuntimeError,
   1977 		    "unknown privsep process '%s'", val);
   1978 		return (NULL);
   1979 	}
   1980 
   1981 	if ((val = python_string_from_dict(kwargs, "root")) != NULL) {
   1982 		kore_free(ps->root);
   1983 		ps->root = kore_strdup(val);
   1984 	}
   1985 
   1986 	if ((val = python_string_from_dict(kwargs, "runas")) != NULL) {
   1987 		kore_free(ps->runas);
   1988 		ps->runas = kore_strdup(val);
   1989 	}
   1990 
   1991 	if ((skip = PyDict_GetItemString(kwargs, "skip")) != NULL) {
   1992 		if (!PyList_CheckExact(skip)) {
   1993 			PyErr_Format(PyExc_RuntimeError,
   1994 			    "privsep skip keyword needs to be a list");
   1995 			return (NULL);
   1996 		}
   1997 
   1998 		list_len = PyList_Size(skip);
   1999 
   2000 		for (idx = 0; idx < list_len; idx++) {
   2001 			if ((obj = PyList_GetItem(skip, idx)) == NULL)
   2002 				return (NULL);
   2003 
   2004 			if (!PyUnicode_Check(obj))
   2005 				return (NULL);
   2006 
   2007 			if ((val = PyUnicode_AsUTF8AndSize(obj, NULL)) == NULL)
   2008 				return (NULL);
   2009 
   2010 			if (!strcmp(val, "chroot")) {
   2011 				ps->skip_chroot = 1;
   2012 			} else {
   2013 				PyErr_Format(PyExc_RuntimeError,
   2014 				    "unknown skip keyword '%s'", val);
   2015 				return (NULL);
   2016 			}
   2017 		}
   2018 	}
   2019 
   2020 	Py_RETURN_NONE;
   2021 }
   2022 
   2023 static PyObject *
   2024 python_kore_prerequest(PyObject *self, PyObject *args)
   2025 {
   2026 	PyObject		*f;
   2027 	struct reqcall		*rq;
   2028 
   2029 	if (!PyArg_ParseTuple(args, "O", &f))
   2030 		return (NULL);
   2031 
   2032 	rq = kore_calloc(1, sizeof(*rq));
   2033 	rq->f = f;
   2034 
   2035 	Py_INCREF(f);
   2036 	TAILQ_INSERT_TAIL(&prereq, rq, list);
   2037 
   2038 	return (f);
   2039 }
   2040 
   2041 static PyObject *
   2042 python_kore_task_create(PyObject *self, PyObject *args)
   2043 {
   2044 	PyObject		*obj;
   2045 	struct python_coro	*coro;
   2046 
   2047 	if (!PyArg_ParseTuple(args, "O", &obj))
   2048 		return (NULL);
   2049 
   2050 	if (!PyCoro_CheckExact(obj))
   2051 		fatal("%s: object is not a coroutine", __func__);
   2052 
   2053 	coro = python_coro_create(obj, NULL);
   2054 	Py_INCREF(obj);
   2055 
   2056 	return (PyLong_FromUnsignedLongLong(coro->id));
   2057 }
   2058 
   2059 static PyObject *
   2060 python_kore_task_id(PyObject *self, PyObject *args)
   2061 {
   2062 	if (coro_running == NULL) {
   2063 		PyErr_SetString(PyExc_RuntimeError,
   2064 		    "no coroutine active");
   2065 		return (NULL);
   2066 	}
   2067 
   2068 	return (PyLong_FromUnsignedLongLong(coro_running->id));
   2069 }
   2070 
   2071 static PyObject *
   2072 python_kore_task_kill(PyObject *self, PyObject *args)
   2073 {
   2074 	u_int64_t		id;
   2075 	struct python_coro	*coro, *active;
   2076 
   2077 	if (!PyArg_ParseTuple(args, "K", &id))
   2078 		return (NULL);
   2079 
   2080 	if (coro_running != NULL && coro_running->id == id) {
   2081 		PyErr_SetString(PyExc_RuntimeError,
   2082 		    "refusing to kill active coroutine");
   2083 		return (NULL);
   2084 	}
   2085 
   2086 	/* Remember active coro, as delete sets coro_running to NULL. */
   2087 	active = coro_running;
   2088 
   2089 	TAILQ_FOREACH(coro, &coro_runnable, list) {
   2090 		if (coro->id == id) {
   2091 			coro->killed++;
   2092 			kore_python_coro_delete(coro);
   2093 			coro_running = active;
   2094 			Py_RETURN_TRUE;
   2095 		}
   2096 	}
   2097 
   2098 	TAILQ_FOREACH(coro, &coro_suspended, list) {
   2099 		if (coro->id == id) {
   2100 			coro->killed++;
   2101 			kore_python_coro_delete(coro);
   2102 			coro_running = active;
   2103 			Py_RETURN_TRUE;
   2104 		}
   2105 	}
   2106 
   2107 	Py_RETURN_FALSE;
   2108 }
   2109 
   2110 static PyObject *
   2111 python_kore_socket_wrap(PyObject *self, PyObject *args)
   2112 {
   2113 	struct pysocket		*sock;
   2114 	PyObject		*pysock, *pyfd, *pyfam, *pyproto;
   2115 
   2116 	sock = NULL;
   2117 	pyfd = NULL;
   2118 	pyfam = NULL;
   2119 	pyproto = NULL;
   2120 
   2121 	if (!PyArg_ParseTuple(args, "O", &pysock))
   2122 		return (NULL);
   2123 
   2124 	if ((pyfd = PyObject_CallMethod(pysock, "fileno", NULL)) == NULL)
   2125 		return (NULL);
   2126 
   2127 	if ((pyfam = PyObject_GetAttrString(pysock, "family")) == NULL)
   2128 		goto out;
   2129 
   2130 	if ((pyproto = PyObject_GetAttrString(pysock, "proto")) == NULL)
   2131 		goto out;
   2132 
   2133 	if ((sock = pysocket_alloc()) == NULL)
   2134 		goto out;
   2135 
   2136 	sock->socket = pysock;
   2137 	Py_INCREF(sock->socket);
   2138 
   2139 	sock->fd = (int)PyLong_AsLong(pyfd);
   2140 	sock->family = (int)PyLong_AsLong(pyfam);
   2141 	sock->protocol = (int)PyLong_AsLong(pyproto);
   2142 
   2143 	memset(&sock->addr, 0, sizeof(sock->addr));
   2144 
   2145 	switch (sock->family) {
   2146 	case AF_INET:
   2147 	case AF_UNIX:
   2148 		break;
   2149 	default:
   2150 		PyErr_SetString(PyExc_RuntimeError, "unsupported family");
   2151 		Py_DECREF((PyObject *)sock);
   2152 		sock = NULL;
   2153 		goto out;
   2154 	}
   2155 
   2156 out:
   2157 	Py_XDECREF(pyfd);
   2158 	Py_XDECREF(pyfam);
   2159 	Py_XDECREF(pyproto);
   2160 
   2161 	return ((PyObject *)sock);
   2162 }
   2163 
   2164 static PyObject *
   2165 python_kore_queue(PyObject *self, PyObject *args)
   2166 {
   2167 	struct pyqueue		*queue;
   2168 
   2169 	if ((queue = PyObject_New(struct pyqueue, &pyqueue_type)) == NULL)
   2170 		return (NULL);
   2171 
   2172 	TAILQ_INIT(&queue->objects);
   2173 	TAILQ_INIT(&queue->waiting);
   2174 
   2175 	return ((PyObject *)queue);
   2176 }
   2177 
   2178 static PyObject *
   2179 python_kore_worker(PyObject *self, PyObject *args)
   2180 {
   2181 	if (worker == NULL) {
   2182 		Py_RETURN_NONE;
   2183 	}
   2184 
   2185 	return (PyLong_FromLong(worker->id));
   2186 }
   2187 
   2188 static PyObject *
   2189 python_kore_tracer(PyObject *self, PyObject *args)
   2190 {
   2191 	PyObject		*obj;
   2192 
   2193 	if (python_tracer != NULL) {
   2194 		PyErr_SetString(PyExc_RuntimeError, "tracer already set");
   2195 		return (NULL);
   2196 	}
   2197 
   2198 	if (!PyArg_ParseTuple(args, "O", &obj))
   2199 		return (NULL);
   2200 
   2201 	if (!PyCallable_Check(obj)) {
   2202 		PyErr_SetString(PyExc_RuntimeError, "object not callable");
   2203 		Py_DECREF(obj);
   2204 		return (NULL);
   2205 	}
   2206 
   2207 	Py_INCREF(obj);
   2208 	python_tracer = obj;
   2209 
   2210 	Py_RETURN_TRUE;
   2211 }
   2212 
   2213 static PyObject *
   2214 python_kore_domain(PyObject *self, PyObject *args, PyObject *kwargs)
   2215 {
   2216 #if defined(KORE_USE_ACME)
   2217 	int			acme;
   2218 	char			*acert, *akey;
   2219 #endif
   2220 	struct kore_server	*srv;
   2221 	long			depth;
   2222 	const char		*name;
   2223 	struct pydomain		*domain;
   2224 	const char		*cert, *key, *ca, *attach, *crl;
   2225 
   2226 	ca = NULL;
   2227 	depth = -1;
   2228 	key = NULL;
   2229 	crl = NULL;
   2230 	cert = NULL;
   2231 	attach = NULL;
   2232 
   2233 #if defined(KORE_USE_ACME)
   2234 	acme = 0;
   2235 #endif
   2236 
   2237 	if (!PyArg_ParseTuple(args, "s", &name))
   2238 		return (NULL);
   2239 
   2240 	if (kwargs != NULL)
   2241 		attach = python_string_from_dict(kwargs, "attach");
   2242 
   2243 	if (attach == NULL)
   2244 		attach = "default";
   2245 
   2246 	if ((srv = kore_server_lookup(attach)) == NULL) {
   2247 		PyErr_Format(PyExc_RuntimeError,
   2248 		    "server '%s' does not exist", attach);
   2249 		return (NULL);
   2250 	}
   2251 
   2252 	if (srv->tls) {
   2253 		if (kwargs == NULL) {
   2254 			PyErr_Format(PyExc_RuntimeError,
   2255 			    "no keywords for TLS enabled domain %s", name);
   2256 			return (NULL);
   2257 		}
   2258 		key = python_string_from_dict(kwargs, "key");
   2259 		cert = python_string_from_dict(kwargs, "cert");
   2260 
   2261 #if defined(KORE_USE_ACME)
   2262 		python_bool_from_dict(kwargs, "acme", &acme);
   2263 
   2264 		if (acme) {
   2265 			kore_acme_get_paths(name, &akey, &acert);
   2266 			acme_domains++;
   2267 			key = akey;
   2268 			cert = acert;
   2269 		}
   2270 #endif
   2271 
   2272 		if (key == NULL || cert == NULL) {
   2273 			PyErr_Format(PyExc_RuntimeError,
   2274 			    "missing key or cert keywords for TLS listener");
   2275 			return (NULL);
   2276 		}
   2277 
   2278 		ca = python_string_from_dict(kwargs, "client_verify");
   2279 		if (ca != NULL) {
   2280 			python_long_from_dict(kwargs, "verify_depth", &depth);
   2281 			if (depth < 0) {
   2282 				PyErr_Format(PyExc_RuntimeError,
   2283 				    "invalid depth '%d'", depth);
   2284 				return (NULL);
   2285 			}
   2286 			crl = python_string_from_dict(kwargs, "crl");
   2287 		}
   2288 	} else if (key != NULL || cert != NULL || ca != NULL) {
   2289 		kore_log(LOG_INFO, "ignoring tls settings for '%s'", name);
   2290 	}
   2291 
   2292 	if (kore_domain_lookup(srv, name) != NULL) {
   2293 		PyErr_SetString(PyExc_RuntimeError, "domain exists");
   2294 		return (NULL);
   2295 	}
   2296 
   2297 	if ((domain = PyObject_New(struct pydomain, &pydomain_type)) == NULL)
   2298 		return (NULL);
   2299 
   2300 	domain->next = NULL;
   2301 	domain->kwargs = NULL;
   2302 
   2303 	if ((domain->config = kore_domain_new(name)) == NULL)
   2304 		fatal("failed to create new domain configuration");
   2305 
   2306 	if (!kore_domain_attach(domain->config, srv))
   2307 		fatal("failed to attach domain configuration");
   2308 
   2309 	if (srv->tls) {
   2310 		domain->config->certkey = kore_strdup(key);
   2311 		domain->config->certfile = kore_strdup(cert);
   2312 
   2313 #if defined(KORE_USE_ACME)
   2314 		domain->config->acme = acme;
   2315 
   2316 		if (domain->config->acme) {
   2317 			kore_free(akey);
   2318 			kore_free(acert);
   2319 		}
   2320 #endif
   2321 		if (ca != NULL) {
   2322 			domain->config->cafile = kore_strdup(ca);
   2323 			domain->config->x509_verify_depth = depth;
   2324 			if (crl != NULL)
   2325 				domain->config->crlfile = kore_strdup(crl);
   2326 		}
   2327 	}
   2328 
   2329 	return ((PyObject *)domain);
   2330 }
   2331 
   2332 static PyObject *
   2333 python_kore_route(PyObject *self, PyObject *args, PyObject *kwargs)
   2334 {
   2335 	const char		*path;
   2336 	PyObject		*inner;
   2337 	struct pyroute		*route;
   2338 
   2339 	if ((route = PyObject_New(struct pyroute, &pyroute_type)) == NULL)
   2340 		return (NULL);
   2341 
   2342 	if (!PyArg_ParseTuple(args, "s", &path))
   2343 		return (NULL);
   2344 
   2345 	route->domain = NULL;
   2346 	route->kwargs = kwargs;
   2347 	route->path = kore_strdup(path);
   2348 
   2349 	Py_XINCREF(route->kwargs);
   2350 
   2351 	inner = PyObject_GetAttrString((PyObject *)route, "inner");
   2352 	if (inner == NULL) {
   2353 		Py_DECREF((PyObject *)route);
   2354 		PyErr_SetString(PyExc_RuntimeError, "failed to find inner");
   2355 		return (NULL);
   2356 	}
   2357 
   2358 	return (inner);
   2359 }
   2360 
   2361 static PyObject *
   2362 python_kore_gather(PyObject *self, PyObject *args, PyObject *kwargs)
   2363 {
   2364 	struct pygather_op	*op;
   2365 	PyObject		*obj;
   2366 	struct pygather_coro	*coro;
   2367 	Py_ssize_t		sz, idx;
   2368 	int			concurrency;
   2369 
   2370 	if (coro_running == NULL) {
   2371 		PyErr_SetString(PyExc_RuntimeError,
   2372 		    "kore.gather only available in coroutines");
   2373 		return (NULL);
   2374 	}
   2375 
   2376 	sz = PyTuple_Size(args);
   2377 
   2378 	if (sz > INT_MAX) {
   2379 		PyErr_SetString(PyExc_TypeError, "too many arguments");
   2380 		return (NULL);
   2381 	}
   2382 
   2383 	if (kwargs != NULL &&
   2384 	    (obj = PyDict_GetItemString(kwargs, "concurrency")) != NULL) {
   2385 		if (!PyLong_Check(obj)) {
   2386 			PyErr_SetString(PyExc_TypeError,
   2387 			    "concurrency level must be an integer");
   2388 			return (NULL);
   2389 		}
   2390 
   2391 		PyErr_Clear();
   2392 		concurrency = (int)PyLong_AsLong(obj);
   2393 		if (concurrency == -1 && PyErr_Occurred())
   2394 			return (NULL);
   2395 
   2396 		if (concurrency == 0)
   2397 			concurrency = sz;
   2398 	} else {
   2399 		concurrency = sz;
   2400 	}
   2401 
   2402 	op = PyObject_New(struct pygather_op, &pygather_op_type);
   2403 	if (op == NULL)
   2404 		return (NULL);
   2405 
   2406 	op->running = 0;
   2407 	op->count = (int)sz;
   2408 	op->coro = coro_running;
   2409 	op->concurrency = concurrency;
   2410 
   2411 	TAILQ_INIT(&op->results);
   2412 	TAILQ_INIT(&op->coroutines);
   2413 
   2414 	for (idx = 0; idx < sz; idx++) {
   2415 		if ((obj = PyTuple_GetItem(args, idx)) == NULL) {
   2416 			Py_DECREF((PyObject *)op);
   2417 			return (NULL);
   2418 		}
   2419 
   2420 		if (!PyCoro_CheckExact(obj)) {
   2421 			Py_DECREF((PyObject *)op);
   2422 			PyErr_SetString(PyExc_TypeError, "not a coroutine");
   2423 			return (NULL);
   2424 		}
   2425 
   2426 		Py_INCREF(obj);
   2427 
   2428 		coro = kore_pool_get(&gather_coro_pool);
   2429 		coro->coro = python_coro_create(obj, NULL);
   2430 		coro->coro->gatherop = op;
   2431 		TAILQ_INSERT_TAIL(&op->coroutines, coro, list);
   2432 
   2433 		if (idx > concurrency - 1)
   2434 			python_coro_suspend(coro->coro);
   2435 		else
   2436 			op->running++;
   2437 	}
   2438 
   2439 	return ((PyObject *)op);
   2440 }
   2441 
   2442 static PyObject *
   2443 python_kore_lock(PyObject *self, PyObject *args)
   2444 {
   2445 	struct pylock		*lock;
   2446 
   2447 	if ((lock = PyObject_New(struct pylock, &pylock_type)) == NULL)
   2448 		return (NULL);
   2449 
   2450 	lock->owner = NULL;
   2451 	TAILQ_INIT(&lock->ops);
   2452 
   2453 	return ((PyObject *)lock);
   2454 }
   2455 
   2456 static PyObject *
   2457 python_kore_fatal(PyObject *self, PyObject *args)
   2458 {
   2459 	const char	*reason;
   2460 
   2461 	if (!PyArg_ParseTuple(args, "s", &reason))
   2462 		reason = "python_kore_fatal: PyArg_ParseTuple failed";
   2463 
   2464 	fatal("%s", reason);
   2465 
   2466 	/* not reached */
   2467 	Py_RETURN_TRUE;
   2468 }
   2469 
   2470 static PyObject *
   2471 python_kore_fatalx(PyObject *self, PyObject *args)
   2472 {
   2473 	const char	*reason;
   2474 
   2475 	if (!PyArg_ParseTuple(args, "s", &reason))
   2476 		reason = "python_kore_fatalx: PyArg_ParseTuple failed";
   2477 
   2478 	fatalx("%s", reason);
   2479 
   2480 	/* not reached */
   2481 	Py_RETURN_TRUE;
   2482 }
   2483 
   2484 static PyObject *
   2485 python_kore_setname(PyObject *self, PyObject *args)
   2486 {
   2487 	const char	*name;
   2488 	extern char	*kore_progname;
   2489 
   2490 	if (!PyArg_ParseTuple(args, "s", &name))
   2491 		return (NULL);
   2492 
   2493 	kore_free(kore_progname);
   2494 	kore_progname = kore_strdup(name);
   2495 
   2496 	Py_RETURN_NONE;
   2497 }
   2498 
   2499 static PyObject *
   2500 python_kore_sigtrap(PyObject *self, PyObject *args)
   2501 {
   2502 	int		sig;
   2503 
   2504 	if (!PyArg_ParseTuple(args, "i", &sig))
   2505 		return (NULL);
   2506 
   2507 	kore_signal_trap(sig);
   2508 
   2509 	Py_RETURN_NONE;
   2510 }
   2511 
   2512 static PyObject *
   2513 python_kore_sendobj(PyObject *self, PyObject *args, PyObject *kwargs)
   2514 {
   2515 	long		val;
   2516 	u_int16_t	dst;
   2517 	char		*ptr;
   2518 	Py_ssize_t	length;
   2519 	PyObject	*object, *bytes;
   2520 
   2521 	if (!PyArg_ParseTuple(args, "O", &object))
   2522 		return (NULL);
   2523 
   2524 	bytes = PyObject_CallFunctionObjArgs(pickle_dumps, object, NULL);
   2525 	if (bytes == NULL)
   2526 		return (NULL);
   2527 
   2528 	if (PyBytes_AsStringAndSize(bytes, &ptr, &length) == -1) {
   2529 		Py_DECREF(bytes);
   2530 		return (NULL);
   2531 	}
   2532 
   2533 	dst = KORE_MSG_WORKER_ALL;
   2534 
   2535 	if (kwargs != NULL) {
   2536 		if (python_long_from_dict(kwargs, "worker", &val)) {
   2537 			if (val <= 0 || val > worker_count ||
   2538 			    val >= KORE_WORKER_MAX) {
   2539 				PyErr_Format(PyExc_RuntimeError,
   2540 				    "worker %ld invalid", val);
   2541 				Py_DECREF(bytes);
   2542 				return (NULL);
   2543 			}
   2544 
   2545 			dst = val;
   2546 		}
   2547 	}
   2548 
   2549 	kore_msg_send(dst, KORE_PYTHON_SEND_OBJ, ptr, length);
   2550 	Py_DECREF(bytes);
   2551 
   2552 	Py_RETURN_NONE;
   2553 }
   2554 
   2555 static void
   2556 python_kore_recvobj(struct kore_msg *msg, const void *data)
   2557 {
   2558 	struct kore_runtime	*rt;
   2559 	PyObject		*onmsg, *ret, *bytes, *obj;
   2560 
   2561 	if ((onmsg = kore_module_getsym("koreapp.onmsg", &rt)) == NULL)
   2562 		return;
   2563 
   2564 	if (rt->type != KORE_RUNTIME_PYTHON)
   2565 		return;
   2566 
   2567 	if ((bytes = PyBytes_FromStringAndSize(data, msg->length)) == NULL) {
   2568 		Py_DECREF(onmsg);
   2569 		kore_python_log_error("koreapp.onmsg");
   2570 		return;
   2571 	}
   2572 
   2573 	obj = PyObject_CallFunctionObjArgs(pickle_loads, bytes, NULL);
   2574 	Py_DECREF(bytes);
   2575 
   2576 	if (obj == NULL) {
   2577 		Py_DECREF(onmsg);
   2578 		kore_python_log_error("koreapp.onmsg");
   2579 		return;
   2580 	}
   2581 
   2582 	ret = PyObject_CallFunctionObjArgs(onmsg, obj, NULL);
   2583 	kore_python_log_error("koreapp.onmsg");
   2584 
   2585 	Py_DECREF(obj);
   2586 	Py_DECREF(onmsg);
   2587 	Py_XDECREF(ret);
   2588 }
   2589 
   2590 static PyObject *
   2591 python_kore_suspend(PyObject *self, PyObject *args)
   2592 {
   2593 	struct pysuspend_op	*op;
   2594 	int			delay;
   2595 
   2596 	if (!PyArg_ParseTuple(args, "i", &delay))
   2597 		return (NULL);
   2598 
   2599 	op = PyObject_New(struct pysuspend_op, &pysuspend_op_type);
   2600 	if (op == NULL)
   2601 		return (NULL);
   2602 
   2603 	op->timer = NULL;
   2604 	op->delay = delay;
   2605 	op->coro = coro_running;
   2606 	op->state = PYSUSPEND_OP_INIT;
   2607 
   2608 	return ((PyObject *)op);
   2609 }
   2610 
   2611 static PyObject *
   2612 python_kore_shutdown(PyObject *self, PyObject *args)
   2613 {
   2614 	kore_shutdown();
   2615 
   2616 	Py_RETURN_TRUE;
   2617 }
   2618 
   2619 static PyObject *
   2620 python_kore_coroname(PyObject *self, PyObject *args)
   2621 {
   2622 	const char		*name;
   2623 
   2624 	if (coro_running == NULL) {
   2625 		PyErr_SetString(PyExc_RuntimeError,
   2626 		    "kore.coroname() only available in coroutines");
   2627 		return (NULL);
   2628 	}
   2629 
   2630 	if (!PyArg_ParseTuple(args, "s", &name))
   2631 		return (NULL);
   2632 
   2633 	kore_free(coro_running->name);
   2634 	coro_running->name = kore_strdup(name);
   2635 
   2636 	Py_RETURN_NONE;
   2637 }
   2638 
   2639 static PyObject *
   2640 python_kore_corotrace(PyObject *self, PyObject *args)
   2641 {
   2642 	if (!PyArg_ParseTuple(args, "b", &coro_tracing))
   2643 		return (NULL);
   2644 
   2645 	Py_RETURN_NONE;
   2646 }
   2647 
   2648 static PyObject *
   2649 python_kore_timer(PyObject *self, PyObject *args, PyObject *kwargs)
   2650 {
   2651 	u_int64_t		ms;
   2652 	PyObject		*obj;
   2653 	int			flags;
   2654 	struct pytimer		*timer;
   2655 
   2656 	if (worker == NULL) {
   2657 		PyErr_SetString(PyExc_RuntimeError,
   2658 		    "kore.timer not supported on parent process");
   2659 		return (NULL);
   2660 	}
   2661 
   2662 	if (!PyArg_ParseTuple(args, "OKi", &obj, &ms, &flags))
   2663 		return (NULL);
   2664 
   2665 	if (flags & ~(KORE_TIMER_FLAGS)) {
   2666 		PyErr_SetString(PyExc_RuntimeError, "invalid flags");
   2667 		return (NULL);
   2668 	}
   2669 
   2670 	if ((timer = PyObject_New(struct pytimer, &pytimer_type)) == NULL)
   2671 		return (NULL);
   2672 
   2673 	timer->udata = NULL;
   2674 	timer->flags = flags;
   2675 	timer->callable = obj;
   2676 	timer->run = kore_timer_add(pytimer_run, ms, timer, flags);
   2677 
   2678 	Py_INCREF((PyObject *)timer);
   2679 	Py_INCREF(timer->callable);
   2680 
   2681 	if (kwargs != NULL) {
   2682 		if ((obj = PyDict_GetItemString(kwargs, "data")) != NULL) {
   2683 			Py_INCREF(obj);
   2684 			timer->udata = obj;
   2685 		}
   2686 	}
   2687 
   2688 	return ((PyObject *)timer);
   2689 }
   2690 
   2691 static PyObject *
   2692 python_kore_proc(PyObject *self, PyObject *args)
   2693 {
   2694 	const char		*cmd;
   2695 	struct pyproc		*proc;
   2696 	char			*copy, *argv[32], *env[1];
   2697 	int			timeo, in_pipe[2], out_pipe[2];
   2698 
   2699 	timeo = -1;
   2700 
   2701 	if (coro_running == NULL) {
   2702 		PyErr_SetString(PyExc_RuntimeError,
   2703 		    "kore.proc only available in coroutines");
   2704 		return (NULL);
   2705 	}
   2706 
   2707 	if (!PyArg_ParseTuple(args, "s|i", &cmd, &timeo))
   2708 		return (NULL);
   2709 
   2710 	if (pipe(in_pipe) == -1) {
   2711 		PyErr_SetString(PyExc_RuntimeError, errno_s);
   2712 		return (NULL);
   2713 	}
   2714 
   2715 	if (pipe(out_pipe) == -1) {
   2716 		close(in_pipe[0]);
   2717 		close(in_pipe[1]);
   2718 		PyErr_SetString(PyExc_RuntimeError, errno_s);
   2719 		return (NULL);
   2720 	}
   2721 
   2722 	if ((proc = PyObject_New(struct pyproc, &pyproc_type)) == NULL) {
   2723 		close(in_pipe[0]);
   2724 		close(in_pipe[1]);
   2725 		close(out_pipe[0]);
   2726 		close(out_pipe[1]);
   2727 		return (NULL);
   2728 	}
   2729 
   2730 	proc->pid = -1;
   2731 	proc->op = NULL;
   2732 	proc->apid = -1;
   2733 	proc->reaped = 0;
   2734 	proc->status = 0;
   2735 	proc->timer = NULL;
   2736 	proc->coro = coro_running;
   2737 	proc->in = pysocket_alloc();
   2738 	proc->out = pysocket_alloc();
   2739 
   2740 	if (proc->in == NULL || proc->out == NULL) {
   2741 		Py_DECREF((PyObject *)proc);
   2742 		return (NULL);
   2743 	}
   2744 
   2745 	TAILQ_INSERT_TAIL(&procs, proc, list);
   2746 
   2747 	proc->pid = fork();
   2748 	if (proc->pid == -1) {
   2749 		if (errno == ENOSYS) {
   2750 			Py_DECREF((PyObject *)proc);
   2751 			PyErr_SetString(PyExc_RuntimeError, errno_s);
   2752 			return (NULL);
   2753 		}
   2754 		fatal("python_kore_proc: fork(): %s", errno_s);
   2755 	}
   2756 
   2757 	if (proc->pid == 0) {
   2758 		close(in_pipe[1]);
   2759 		close(out_pipe[0]);
   2760 
   2761 		if (dup2(out_pipe[1], STDOUT_FILENO) == -1 ||
   2762 		    dup2(out_pipe[1], STDERR_FILENO) == -1 ||
   2763 		    dup2(in_pipe[0], STDIN_FILENO) == -1)
   2764 			fatal("dup2: %s", errno_s);
   2765 
   2766 		env[0] = NULL;
   2767 		copy = kore_strdup(cmd);
   2768 		python_split_arguments(copy, argv, 32);
   2769 
   2770 		(void)execve(argv[0], argv, env);
   2771 		kore_log(LOG_ERR, "kore.proc failed to execute %s (%s)",
   2772 		    argv[0], errno_s);
   2773 		exit(1);
   2774 	}
   2775 
   2776 	close(in_pipe[0]);
   2777 	close(out_pipe[1]);
   2778 
   2779 	if (!kore_connection_nonblock(in_pipe[1], 0) ||
   2780 	    !kore_connection_nonblock(out_pipe[0], 0))
   2781 		fatal("failed to mark kore.proc pipes are non-blocking");
   2782 
   2783 	proc->apid = proc->pid;
   2784 	proc->in->fd = in_pipe[1];
   2785 	proc->out->fd = out_pipe[0];
   2786 
   2787 	if (timeo != -1) {
   2788 		proc->timer = kore_timer_add(pyproc_timeout,
   2789 		    timeo, proc, KORE_TIMER_ONESHOT);
   2790 	}
   2791 
   2792 	return ((PyObject *)proc);
   2793 }
   2794 
   2795 static PyObject *
   2796 python_import(const char *path)
   2797 {
   2798 	struct stat	st;
   2799 	PyObject	*module;
   2800 	char		*dir, *file, *copy, *p;
   2801 
   2802 	if (stat(path, &st) == -1)
   2803 		fatal("python_import: stat(%s): %s", path, errno_s);
   2804 
   2805 	if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
   2806 		fatal("python_import: '%s' is not a file or directory", path);
   2807 
   2808 	copy = kore_strdup(path);
   2809 	if ((p = dirname(copy)) == NULL)
   2810 		fatal("dirname: %s: %s", path, errno_s);
   2811 
   2812 	dir = kore_strdup(p);
   2813 	kore_free(copy);
   2814 
   2815 	copy = kore_strdup(path);
   2816 	if ((p = basename(copy)) == NULL)
   2817 		fatal("basename: %s: %s", path, errno_s);
   2818 
   2819 	file = kore_strdup(p);
   2820 	kore_free(copy);
   2821 
   2822 	if ((p = strrchr(file, '.')) != NULL)
   2823 		*p = '\0';
   2824 
   2825 	python_append_path(dir);
   2826 
   2827 	if (S_ISDIR(st.st_mode))
   2828 		python_append_path(path);
   2829 
   2830 	module = PyImport_ImportModule(file);
   2831 	if (module == NULL)
   2832 		PyErr_Print();
   2833 
   2834 	kore_free(dir);
   2835 	kore_free(file);
   2836 
   2837 	return (module);
   2838 }
   2839 
   2840 static PyObject *
   2841 python_callable(PyObject *module, const char *symbol)
   2842 {
   2843 	char		*base, *method;
   2844 	PyObject	*res, *obj, *meth;
   2845 
   2846 	res = NULL;
   2847 	obj = NULL;
   2848 	base = kore_strdup(symbol);
   2849 
   2850 	if ((method = strchr(base, '.')) != NULL)
   2851 		*(method)++ = '\0';
   2852 
   2853 	if ((obj = PyObject_GetAttrString(module, base)) == NULL)
   2854 		goto out;
   2855 
   2856 	if (method != NULL) {
   2857 		if ((meth = PyObject_GetAttrString(obj, method)) == NULL)
   2858 			goto out;
   2859 
   2860 		Py_DECREF(obj);
   2861 		obj = meth;
   2862 	}
   2863 
   2864 	if (!PyCallable_Check(obj))
   2865 		goto out;
   2866 
   2867 	res = obj;
   2868 	obj = NULL;
   2869 
   2870 out:
   2871 	if (obj != NULL)
   2872 		Py_DECREF(obj);
   2873 
   2874 	PyErr_Clear();
   2875 	kore_free(base);
   2876 
   2877 	return (res);
   2878 }
   2879 
   2880 static PyObject *
   2881 pyconnection_alloc(struct connection *c)
   2882 {
   2883 	struct pyconnection		*pyc;
   2884 
   2885 	pyc = PyObject_New(struct pyconnection, &pyconnection_type);
   2886 	if (pyc == NULL)
   2887 		return (NULL);
   2888 
   2889 	pyc->c = c;
   2890 
   2891 	return ((PyObject *)pyc);
   2892 }
   2893 
   2894 static PyObject *
   2895 pyconnection_disconnect(struct pyconnection *pyc, PyObject *args)
   2896 {
   2897 	kore_connection_disconnect(pyc->c);
   2898 
   2899 	Py_RETURN_TRUE;
   2900 }
   2901 
   2902 static PyObject *
   2903 pyconnection_get_fd(struct pyconnection *pyc, void *closure)
   2904 {
   2905 	PyObject	*fd;
   2906 
   2907 	if ((fd = PyLong_FromLong(pyc->c->fd)) == NULL)
   2908 		return (PyErr_NoMemory());
   2909 
   2910 	return (fd);
   2911 }
   2912 
   2913 static PyObject *
   2914 pyconnection_get_addr(struct pyconnection *pyc, void *closure)
   2915 {
   2916 	void		*ptr;
   2917 	PyObject	*result;
   2918 	char		addr[INET6_ADDRSTRLEN];
   2919 
   2920 	switch (pyc->c->family) {
   2921 	case AF_INET:
   2922 		ptr = &pyc->c->addr.ipv4.sin_addr;
   2923 		break;
   2924 	case AF_INET6:
   2925 		ptr = &pyc->c->addr.ipv6.sin6_addr;
   2926 		break;
   2927 	default:
   2928 		PyErr_SetString(PyExc_RuntimeError, "invalid family");
   2929 		return (NULL);
   2930 	}
   2931 
   2932 	if (inet_ntop(pyc->c->family, ptr, addr, sizeof(addr)) == NULL) {
   2933 		PyErr_SetString(PyExc_RuntimeError, "inet_ntop failed");
   2934 		return (NULL);
   2935 	}
   2936 
   2937 	if ((result = PyUnicode_FromString(addr)) == NULL)
   2938 		return (PyErr_NoMemory());
   2939 
   2940 	return (result);
   2941 }
   2942 
   2943 static PyObject *
   2944 pyconnection_get_peer_x509(struct pyconnection *pyc, void *closure)
   2945 {
   2946 	size_t		len;
   2947 	u_int8_t	*der;
   2948 	PyObject	*bytes;
   2949 
   2950 	if (pyc->c->tls_cert == NULL) {
   2951 		Py_RETURN_NONE;
   2952 	}
   2953 
   2954 	if (!kore_tls_x509_data(pyc->c, &der, &len)) {
   2955 		PyErr_SetString(PyExc_RuntimeError,
   2956 		    "failed to obtain certificate data");
   2957 		return (NULL);
   2958 	}
   2959 
   2960 	bytes = PyBytes_FromStringAndSize((char *)der, len);
   2961 	kore_free(der);
   2962 
   2963 	return (bytes);
   2964 }
   2965 
   2966 static PyObject *
   2967 pyconnection_get_peer_x509dict(struct pyconnection *pyc, void *closure)
   2968 {
   2969 	KORE_X509_NAMES	*name;
   2970 	PyObject	*dict, *issuer, *subject, *ret;
   2971 
   2972 	ret = NULL;
   2973 	issuer = NULL;
   2974 	subject = NULL;
   2975 
   2976 	if (pyc->c->tls_cert == NULL) {
   2977 		Py_RETURN_NONE;
   2978 	}
   2979 
   2980 	if ((dict = PyDict_New()) == NULL)
   2981 		goto out;
   2982 
   2983 	if ((issuer = PyDict_New()) == NULL)
   2984 		goto out;
   2985 
   2986 	if (PyDict_SetItemString(dict, "issuer", issuer) == -1)
   2987 		goto out;
   2988 
   2989 	if ((subject = PyDict_New()) == NULL)
   2990 		goto out;
   2991 
   2992 	if (PyDict_SetItemString(dict, "subject", subject) == -1)
   2993 		goto out;
   2994 
   2995 	PyErr_Clear();
   2996 
   2997 	if ((name = kore_tls_x509_subject_name(pyc->c)) == NULL) {
   2998 		PyErr_Format(PyExc_RuntimeError,
   2999 		    "failed to obtain x509 subjectName");
   3000 		goto out;
   3001 	}
   3002 
   3003 	if (!kore_tls_x509name_foreach(name, 0, subject,
   3004 	    pyconnection_x509_cb)) {
   3005 		if (PyErr_Occurred() == NULL) {
   3006 			PyErr_Format(PyExc_RuntimeError,
   3007 			    "failed to add subject name to dictionary");
   3008 		}
   3009 		goto out;
   3010 	}
   3011 
   3012 	if ((name = kore_tls_x509_issuer_name(pyc->c)) == NULL) {
   3013 		PyErr_Format(PyExc_RuntimeError,
   3014 		    "failed to obtain x509 issuerName");
   3015 		goto out;
   3016 	}
   3017 
   3018 	if (!kore_tls_x509name_foreach(name, 0, issuer, pyconnection_x509_cb)) {
   3019 		if (PyErr_Occurred() == NULL) {
   3020 			PyErr_Format(PyExc_RuntimeError,
   3021 			    "failed to add issuer name to dictionary");
   3022 		}
   3023 		goto out;
   3024 	}
   3025 
   3026 	ret = dict;
   3027 	dict = NULL;
   3028 
   3029 out:
   3030 	Py_XDECREF(dict);
   3031 	Py_XDECREF(issuer);
   3032 	Py_XDECREF(subject);
   3033 
   3034 	return (ret);
   3035 }
   3036 
   3037 static int
   3038 pyconnection_x509_cb(void *udata, int islast, int nid, const char *field,
   3039     const void *data, size_t len, int flags)
   3040 {
   3041 	PyObject	*dict, *obj;
   3042 
   3043 	dict = udata;
   3044 
   3045 	if ((obj = PyUnicode_FromStringAndSize(data, len)) == NULL)
   3046 		return (KORE_RESULT_ERROR);
   3047 
   3048 	if (PyDict_SetItemString(dict, field, obj) == -1) {
   3049 		Py_DECREF(obj);
   3050 		return (KORE_RESULT_ERROR);
   3051 	}
   3052 
   3053 	Py_DECREF(obj);
   3054 	return (KORE_RESULT_OK);
   3055 }
   3056 
   3057 static void
   3058 pytimer_run(void *arg, u_int64_t now)
   3059 {
   3060 	PyObject	*ret;
   3061 	struct pytimer	*timer = arg;
   3062 
   3063 	PyErr_Clear();
   3064 	ret = PyObject_CallFunctionObjArgs(timer->callable, timer->udata, NULL);
   3065 	Py_XDECREF(ret);
   3066 	Py_XDECREF(timer->udata);
   3067 
   3068 	timer->udata = NULL;
   3069 	kore_python_log_error("pytimer_run");
   3070 
   3071 	if (timer->flags & KORE_TIMER_ONESHOT) {
   3072 		timer->run = NULL;
   3073 		Py_DECREF((PyObject *)timer);
   3074 	}
   3075 }
   3076 
   3077 static void
   3078 pytimer_dealloc(struct pytimer *timer)
   3079 {
   3080 	if (timer->run != NULL) {
   3081 		kore_timer_remove(timer->run);
   3082 		timer->run = NULL;
   3083 	}
   3084 
   3085 	if (timer->callable != NULL) {
   3086 		Py_DECREF(timer->callable);
   3087 		timer->callable = NULL;
   3088 	}
   3089 
   3090 	PyObject_Del((PyObject *)timer);
   3091 }
   3092 
   3093 static PyObject *
   3094 pytimer_close(struct pytimer *timer, PyObject *args)
   3095 {
   3096 	if (timer->run != NULL) {
   3097 		kore_timer_remove(timer->run);
   3098 		timer->run = NULL;
   3099 	}
   3100 
   3101 	if (timer->callable != NULL) {
   3102 		Py_DECREF(timer->callable);
   3103 		timer->callable = NULL;
   3104 	}
   3105 
   3106 	if (timer->udata != NULL) {
   3107 		Py_DECREF(timer->udata);
   3108 		timer->udata = NULL;
   3109 	}
   3110 
   3111 	Py_INCREF((PyObject *)timer);
   3112 	Py_RETURN_TRUE;
   3113 }
   3114 
   3115 static void
   3116 pysuspend_op_dealloc(struct pysuspend_op *op)
   3117 {
   3118 	if (op->timer != NULL) {
   3119 		kore_timer_remove(op->timer);
   3120 		op->timer = NULL;
   3121 	}
   3122 
   3123 	PyObject_Del((PyObject *)op);
   3124 }
   3125 
   3126 static PyObject *
   3127 pysuspend_op_await(PyObject *sop)
   3128 {
   3129 	Py_INCREF(sop);
   3130 	return (sop);
   3131 }
   3132 
   3133 static PyObject *
   3134 pysuspend_op_iternext(struct pysuspend_op *op)
   3135 {
   3136 	switch (op->state) {
   3137 	case PYSUSPEND_OP_INIT:
   3138 		op->timer = kore_timer_add(pysuspend_wakeup, op->delay,
   3139 		    op, KORE_TIMER_ONESHOT);
   3140 		op->state = PYSUSPEND_OP_WAIT;
   3141 		break;
   3142 	case PYSUSPEND_OP_WAIT:
   3143 		break;
   3144 	case PYSUSPEND_OP_CONTINUE:
   3145 		PyErr_SetNone(PyExc_StopIteration);
   3146 		return (NULL);
   3147 	default:
   3148 		fatal("unknown state %d for pysuspend_op", op->state);
   3149 	}
   3150 
   3151 	Py_RETURN_NONE;
   3152 }
   3153 
   3154 static void
   3155 pysuspend_wakeup(void *arg, u_int64_t now)
   3156 {
   3157 	struct pysuspend_op	*op = arg;
   3158 
   3159 	op->timer = NULL;
   3160 	op->state = PYSUSPEND_OP_CONTINUE;
   3161 
   3162 	if (op->coro->request != NULL)
   3163 		http_request_wakeup(op->coro->request);
   3164 	else
   3165 		python_coro_wakeup(op->coro);
   3166 }
   3167 
   3168 static struct pysocket *
   3169 pysocket_alloc(void)
   3170 {
   3171 	struct pysocket		*sock;
   3172 
   3173 	if ((sock = PyObject_New(struct pysocket, &pysocket_type)) == NULL)
   3174 		return (NULL);
   3175 
   3176 	sock->fd = -1;
   3177 	sock->family = -1;
   3178 	sock->protocol = -1;
   3179 	sock->scheduled = 0;
   3180 
   3181 	sock->socket = NULL;
   3182 	sock->recvop = NULL;
   3183 	sock->sendop = NULL;
   3184 
   3185 	sock->event.s = sock;
   3186 	sock->event.evt.flags = 0;
   3187 	sock->event.evt.type = KORE_TYPE_PYSOCKET;
   3188 	sock->event.evt.handle = pysocket_evt_handle;
   3189 
   3190 	return (sock);
   3191 }
   3192 
   3193 static void
   3194 pysocket_dealloc(struct pysocket *sock)
   3195 {
   3196 	if (sock->scheduled && sock->fd != -1) {
   3197 		kore_platform_disable_read(sock->fd);
   3198 #if !defined(__linux__)
   3199 		kore_platform_disable_write(sock->fd);
   3200 #endif
   3201 	}
   3202 
   3203 	if (sock->socket != NULL) {
   3204 		Py_DECREF(sock->socket);
   3205 	} else if (sock->fd != -1) {
   3206 		(void)close(sock->fd);
   3207 	}
   3208 
   3209 	PyObject_Del((PyObject *)sock);
   3210 }
   3211 
   3212 static PyObject *
   3213 pysocket_send(struct pysocket *sock, PyObject *args)
   3214 {
   3215 	Py_buffer	buf;
   3216 	PyObject	*ret;
   3217 
   3218 	if (!PyArg_ParseTuple(args, "y*", &buf))
   3219 		return (NULL);
   3220 
   3221 	ret = pysocket_op_create(sock, PYSOCKET_TYPE_SEND, buf.buf, buf.len);
   3222 	PyBuffer_Release(&buf);
   3223 
   3224 	return (ret);
   3225 }
   3226 
   3227 static PyObject *
   3228 pysocket_sendto(struct pysocket *sock, PyObject *args)
   3229 {
   3230 	Py_buffer		buf;
   3231 	struct pysocket_op	*op;
   3232 	PyObject		*ret;
   3233 	int			port;
   3234 	const char		*ip, *sockaddr;
   3235 
   3236 	switch (sock->family) {
   3237 	case AF_INET:
   3238 		if (!PyArg_ParseTuple(args, "siy*", &ip, &port, &buf))
   3239 			return (NULL);
   3240 		if (port <= 0 || port >= USHRT_MAX) {
   3241 			PyErr_SetString(PyExc_RuntimeError, "invalid port");
   3242 			return (NULL);
   3243 		}
   3244 		break;
   3245 	case AF_UNIX:
   3246 		if (!PyArg_ParseTuple(args, "sy*", &sockaddr, &buf))
   3247 			return (NULL);
   3248 		break;
   3249 	default:
   3250 		PyErr_SetString(PyExc_RuntimeError, "unsupported family");
   3251 		return (NULL);
   3252 	}
   3253 
   3254 	ret = pysocket_op_create(sock, PYSOCKET_TYPE_SENDTO, buf.buf, buf.len);
   3255 	PyBuffer_Release(&buf);
   3256 
   3257 	op = (struct pysocket_op *)ret;
   3258 
   3259 	switch (sock->family) {
   3260 	case AF_INET:
   3261 		op->sendaddr.ipv4.sin_family = AF_INET;
   3262 		op->sendaddr.ipv4.sin_port = htons(port);
   3263 		op->sendaddr.ipv4.sin_addr.s_addr = inet_addr(ip);
   3264 		break;
   3265 	case AF_UNIX:
   3266 		op->sendaddr.sun.sun_family = AF_UNIX;
   3267 		if (kore_strlcpy(op->sendaddr.sun.sun_path, sockaddr,
   3268 		    sizeof(op->sendaddr.sun.sun_path)) >=
   3269 		    sizeof(op->sendaddr.sun.sun_path)) {
   3270 			Py_DECREF(ret);
   3271 			PyErr_SetString(PyExc_RuntimeError,
   3272 			    "unix socket path too long");
   3273 			return (NULL);
   3274 		}
   3275 		break;
   3276 	default:
   3277 		Py_DECREF(ret);
   3278 		PyErr_SetString(PyExc_RuntimeError, "unsupported family");
   3279 		return (NULL);
   3280 	}
   3281 
   3282 	return (ret);
   3283 }
   3284 
   3285 static PyObject *
   3286 pysocket_recv(struct pysocket *sock, PyObject *args)
   3287 {
   3288 	Py_ssize_t		len;
   3289 	struct pysocket_op	*op;
   3290 	PyObject		*obj;
   3291 	int			timeo;
   3292 
   3293 	timeo = -1;
   3294 
   3295 	if (!PyArg_ParseTuple(args, "n|i", &len, &timeo))
   3296 		return (NULL);
   3297 
   3298 	obj = pysocket_op_create(sock, PYSOCKET_TYPE_RECV, NULL, len);
   3299 	if (obj == NULL)
   3300 		return (NULL);
   3301 
   3302 	op = (struct pysocket_op *)obj;
   3303 
   3304 	if (timeo != -1) {
   3305 		op->timer = kore_timer_add(pysocket_op_timeout,
   3306 		    timeo, op, KORE_TIMER_ONESHOT);
   3307 	}
   3308 
   3309 	return (obj);
   3310 }
   3311 
   3312 static PyObject *
   3313 pysocket_recvmsg(struct pysocket *sock, PyObject *args)
   3314 {
   3315 	Py_ssize_t	len;
   3316 
   3317 	if (!PyArg_ParseTuple(args, "n", &len))
   3318 		return (NULL);
   3319 
   3320 	return (pysocket_op_create(sock, PYSOCKET_TYPE_RECVMSG, NULL, len));
   3321 }
   3322 
   3323 static PyObject *
   3324 pysocket_recvfrom(struct pysocket *sock, PyObject *args)
   3325 {
   3326 	Py_ssize_t	len;
   3327 
   3328 	if (!PyArg_ParseTuple(args, "n", &len))
   3329 		return (NULL);
   3330 
   3331 	return (pysocket_op_create(sock, PYSOCKET_TYPE_RECVFROM, NULL, len));
   3332 }
   3333 
   3334 static PyObject *
   3335 pysocket_accept(struct pysocket *sock, PyObject *args)
   3336 {
   3337 	return (pysocket_op_create(sock, PYSOCKET_TYPE_ACCEPT, NULL, 0));
   3338 }
   3339 
   3340 static PyObject *
   3341 pysocket_connect(struct pysocket *sock, PyObject *args)
   3342 {
   3343 	const char		*host;
   3344 	int			port, len;
   3345 
   3346 	port = 0;
   3347 
   3348 	if (!PyArg_ParseTuple(args, "s|i", &host, &port))
   3349 		return (NULL);
   3350 
   3351 	if (port < 0 || port > USHRT_MAX) {
   3352 		PyErr_SetString(PyExc_RuntimeError, "invalid port number");
   3353 		return (NULL);
   3354 	}
   3355 
   3356 	switch (sock->family) {
   3357 	case AF_INET:
   3358 		sock->addr.ipv4.sin_family = AF_INET;
   3359 		sock->addr.ipv4.sin_port = htons(port);
   3360 		if (inet_pton(sock->family, host,
   3361 		    &sock->addr.ipv4.sin_addr) == -1) {
   3362 			PyErr_SetString(PyExc_RuntimeError, "invalid host");
   3363 			return (NULL);
   3364 		}
   3365 		sock->addr_len = sizeof(sock->addr.ipv4);
   3366 		break;
   3367 	case AF_UNIX:
   3368 		sock->addr.sun.sun_family = AF_UNIX;
   3369 		len = snprintf(sock->addr.sun.sun_path,
   3370 		    sizeof(sock->addr.sun.sun_path), "%s", host);
   3371 		if (len == -1 ||
   3372 		    (size_t)len >= sizeof(sock->addr.sun.sun_path)) {
   3373 			PyErr_SetString(PyExc_RuntimeError, "path too long");
   3374 			return (NULL);
   3375 		}
   3376 #if defined(__linux__)
   3377 		/* Assume abstract socket if prefixed with '@'. */
   3378 		if (sock->addr.sun.sun_path[0] == '@')
   3379 			sock->addr.sun.sun_path[0] = '\0';
   3380 #endif
   3381 		sock->addr_len = sizeof(sock->addr.sun.sun_family) + len;
   3382 		break;
   3383 	default:
   3384 		fatal("unsupported socket family %d", sock->family);
   3385 	}
   3386 
   3387 	return (pysocket_op_create(sock, PYSOCKET_TYPE_CONNECT, NULL, 0));
   3388 }
   3389 
   3390 static PyObject *
   3391 pysocket_close(struct pysocket *sock, PyObject *args)
   3392 {
   3393 	if (sock->scheduled) {
   3394 		sock->scheduled = 0;
   3395 		kore_platform_disable_read(sock->fd);
   3396 #if !defined(__linux__)
   3397 		kore_platform_disable_write(sock->fd);
   3398 #endif
   3399 	}
   3400 
   3401 	if (sock->socket != NULL) {
   3402 		Py_DECREF(sock->socket);
   3403 		sock->socket = NULL;
   3404 	} else if (sock->fd != -1) {
   3405 		(void)close(sock->fd);
   3406 	}
   3407 
   3408 	sock->fd = -1;
   3409 	sock->event.evt.handle(&sock->event, 1);
   3410 
   3411 	Py_RETURN_TRUE;
   3412 }
   3413 
   3414 static void
   3415 pysocket_op_dealloc(struct pysocket_op *op)
   3416 {
   3417 	if (op->type == PYSOCKET_TYPE_RECV ||
   3418 	    op->type == PYSOCKET_TYPE_RECVMSG ||
   3419 	    op->type == PYSOCKET_TYPE_RECVFROM ||
   3420 	    op->type == PYSOCKET_TYPE_SEND ||
   3421 	    op->type == PYSOCKET_TYPE_SENDTO)
   3422 		kore_buf_cleanup(&op->buffer);
   3423 
   3424 	switch (op->type) {
   3425 	case PYSOCKET_TYPE_RECV:
   3426 	case PYSOCKET_TYPE_ACCEPT:
   3427 	case PYSOCKET_TYPE_RECVMSG:
   3428 	case PYSOCKET_TYPE_RECVFROM:
   3429 		if (op->socket->recvop != op)
   3430 			fatal("recvop mismatch");
   3431 		op->socket->recvop = NULL;
   3432 		break;
   3433 	case PYSOCKET_TYPE_SEND:
   3434 	case PYSOCKET_TYPE_SENDTO:
   3435 	case PYSOCKET_TYPE_CONNECT:
   3436 		if (op->socket->sendop != op)
   3437 			fatal("sendop mismatch");
   3438 		op->socket->sendop = NULL;
   3439 		break;
   3440 	}
   3441 
   3442 	if (op->timer != NULL) {
   3443 		kore_timer_remove(op->timer);
   3444 		op->timer = NULL;
   3445 	}
   3446 
   3447 	op->coro->sockop = NULL;
   3448 	Py_DECREF(op->socket);
   3449 
   3450 	PyObject_Del((PyObject *)op);
   3451 }
   3452 
   3453 static PyObject *
   3454 pysocket_op_create(struct pysocket *sock, int type, const void *ptr, size_t len)
   3455 {
   3456 	struct pysocket_op	*op;
   3457 
   3458 	if (coro_running->sockop != NULL)
   3459 		fatal("pysocket_op_create: coro has active socketop");
   3460 
   3461 	switch (type) {
   3462 	case PYSOCKET_TYPE_RECV:
   3463 	case PYSOCKET_TYPE_ACCEPT:
   3464 	case PYSOCKET_TYPE_RECVMSG:
   3465 	case PYSOCKET_TYPE_RECVFROM:
   3466 		if (sock->recvop != NULL) {
   3467 			PyErr_SetString(PyExc_RuntimeError,
   3468 			    "only one recv operation can be done per socket");
   3469 			return (NULL);
   3470 		}
   3471 		break;
   3472 	case PYSOCKET_TYPE_SEND:
   3473 	case PYSOCKET_TYPE_SENDTO:
   3474 	case PYSOCKET_TYPE_CONNECT:
   3475 		if (sock->sendop != NULL) {
   3476 			PyErr_SetString(PyExc_RuntimeError,
   3477 			    "only one send operation can be done per socket");
   3478 			return (NULL);
   3479 		}
   3480 		break;
   3481 	default:
   3482 		fatal("unknown pysocket_op type %u", type);
   3483 	}
   3484 
   3485 	op = PyObject_New(struct pysocket_op, &pysocket_op_type);
   3486 	if (op == NULL)
   3487 		return (NULL);
   3488 
   3489 	op->eof = 0;
   3490 	op->self = op;
   3491 	op->type = type;
   3492 	op->timer = NULL;
   3493 	op->socket = sock;
   3494 	op->coro = coro_running;
   3495 
   3496 	coro_running->sockop = op;
   3497 	Py_INCREF(op->socket);
   3498 
   3499 	switch (type) {
   3500 	case PYSOCKET_TYPE_RECV:
   3501 	case PYSOCKET_TYPE_RECVMSG:
   3502 	case PYSOCKET_TYPE_RECVFROM:
   3503 		sock->recvop = op;
   3504 		kore_buf_init(&op->buffer, len);
   3505 		break;
   3506 	case PYSOCKET_TYPE_SEND:
   3507 	case PYSOCKET_TYPE_SENDTO:
   3508 		sock->sendop = op;
   3509 		kore_buf_init(&op->buffer, len);
   3510 		kore_buf_append(&op->buffer, ptr, len);
   3511 		kore_buf_reset(&op->buffer);
   3512 		break;
   3513 	case PYSOCKET_TYPE_ACCEPT:
   3514 		sock->recvop = op;
   3515 		break;
   3516 	case PYSOCKET_TYPE_CONNECT:
   3517 		sock->sendop = op;
   3518 		break;
   3519 	default:
   3520 		fatal("unknown pysocket_op type %u", type);
   3521 	}
   3522 
   3523 	if (sock->scheduled == 0) {
   3524 		sock->scheduled = 1;
   3525 		kore_platform_event_all(sock->fd, &sock->event);
   3526 	}
   3527 
   3528 	return ((PyObject *)op);
   3529 }
   3530 
   3531 static PyObject *
   3532 pysocket_op_await(PyObject *obj)
   3533 {
   3534 	Py_INCREF(obj);
   3535 	return (obj);
   3536 }
   3537 
   3538 static PyObject *
   3539 pysocket_op_iternext(struct pysocket_op *op)
   3540 {
   3541 	PyObject		*ret;
   3542 
   3543 	if (op->socket->fd == -1) {
   3544 		PyErr_SetNone(PyExc_StopIteration);
   3545 		return (NULL);
   3546 	}
   3547 
   3548 	if (op->eof) {
   3549 		if (op->coro->exception != NULL) {
   3550 			PyErr_SetString(op->coro->exception,
   3551 			    op->coro->exception_msg);
   3552 			op->coro->exception = NULL;
   3553 			return (NULL);
   3554 		}
   3555 
   3556 		if (op->type != PYSOCKET_TYPE_RECV) {
   3557 			PyErr_SetString(PyExc_RuntimeError, "socket EOF");
   3558 			return (NULL);
   3559 		}
   3560 
   3561 		/* Drain the recv socket. */
   3562 		op->socket->event.evt.flags |= KORE_EVENT_READ;
   3563 		return (pysocket_async_recv(op));
   3564 	}
   3565 
   3566 	switch (op->type) {
   3567 	case PYSOCKET_TYPE_CONNECT:
   3568 		ret = pysocket_async_connect(op);
   3569 		break;
   3570 	case PYSOCKET_TYPE_ACCEPT:
   3571 		ret = pysocket_async_accept(op);
   3572 		break;
   3573 	case PYSOCKET_TYPE_RECV:
   3574 	case PYSOCKET_TYPE_RECVMSG:
   3575 	case PYSOCKET_TYPE_RECVFROM:
   3576 		ret = pysocket_async_recv(op);
   3577 		break;
   3578 	case PYSOCKET_TYPE_SEND:
   3579 	case PYSOCKET_TYPE_SENDTO:
   3580 		ret = pysocket_async_send(op);
   3581 		break;
   3582 	default:
   3583 		PyErr_SetString(PyExc_RuntimeError, "invalid op type");
   3584 		return (NULL);
   3585 	}
   3586 
   3587 	return (ret);
   3588 }
   3589 
   3590 static void
   3591 pysocket_op_timeout(void *arg, u_int64_t now)
   3592 {
   3593 	struct pysocket_op	*op = arg;
   3594 
   3595 	op->eof = 1;
   3596 	op->timer = NULL;
   3597 
   3598 	op->coro->exception = PyExc_TimeoutError;
   3599 	op->coro->exception_msg = "timeout before operation completed";
   3600 
   3601 	if (op->coro->request != NULL)
   3602 		http_request_wakeup(op->coro->request);
   3603 	else
   3604 		python_coro_wakeup(op->coro);
   3605 }
   3606 
   3607 static PyObject *
   3608 pysocket_async_connect(struct pysocket_op *op)
   3609 {
   3610 	if (connect(op->socket->fd, (struct sockaddr *)&op->socket->addr,
   3611 	    op->socket->addr_len) == -1) {
   3612 		if (errno != EALREADY && errno != EINPROGRESS &&
   3613 		    errno != EISCONN && errno != EAGAIN) {
   3614 			PyErr_SetString(PyExc_RuntimeError, errno_s);
   3615 			return (NULL);
   3616 		}
   3617 
   3618 		if (errno != EISCONN) {
   3619 			Py_RETURN_NONE;
   3620 		}
   3621 	}
   3622 
   3623 	PyErr_SetNone(PyExc_StopIteration);
   3624 	return (NULL);
   3625 }
   3626 
   3627 static PyObject *
   3628 pysocket_async_accept(struct pysocket_op *op)
   3629 {
   3630 	int			fd;
   3631 	struct pysocket		*sock;
   3632 
   3633 	if (!(op->socket->event.evt.flags & KORE_EVENT_READ)) {
   3634 		Py_RETURN_NONE;
   3635 	}
   3636 
   3637 	if ((sock = pysocket_alloc()) == NULL)
   3638 		return (NULL);
   3639 
   3640 	sock->addr_len = sizeof(sock->addr);
   3641 
   3642 	if ((fd = accept(op->socket->fd,
   3643 	    (struct sockaddr *)&sock->addr, &sock->addr_len)) == -1) {
   3644 		Py_DECREF((PyObject *)sock);
   3645 		if (errno == EAGAIN || errno == EWOULDBLOCK) {
   3646 			op->socket->event.evt.flags &= ~KORE_EVENT_READ;
   3647 			Py_RETURN_NONE;
   3648 		}
   3649 		PyErr_SetString(PyExc_RuntimeError, errno_s);
   3650 		return (NULL);
   3651 	}
   3652 
   3653 	if (!kore_connection_nonblock(fd, 0)) {
   3654 		Py_DECREF((PyObject *)sock);
   3655 		PyErr_SetString(PyExc_RuntimeError, errno_s);
   3656 		return (NULL);
   3657 	}
   3658 
   3659 	sock->fd = fd;
   3660 	sock->socket = NULL;
   3661 	sock->family = op->socket->family;
   3662 	sock->protocol = op->socket->protocol;
   3663 
   3664 	PyErr_SetObject(PyExc_StopIteration, (PyObject *)sock);
   3665 	Py_DECREF((PyObject *)sock);
   3666 
   3667 	return (NULL);
   3668 }
   3669 
   3670 static PyObject *
   3671 pysocket_async_recv(struct pysocket_op *op)
   3672 {
   3673 	ssize_t			ret;
   3674 	size_t			len;
   3675 	u_int16_t		port;
   3676 	struct iovec		iov;
   3677 	struct msghdr		msg;
   3678 	socklen_t		socklen;
   3679 	struct sockaddr 	*sendaddr;
   3680 	const char		*ptr, *ip;
   3681 	u_int8_t		ancdata[1024];
   3682 	PyObject		*bytes, *result, *tuple, *list;
   3683 
   3684 	if (!(op->socket->event.evt.flags & KORE_EVENT_READ)) {
   3685 		Py_RETURN_NONE;
   3686 	}
   3687 
   3688 	socklen = 0;
   3689 
   3690 	for (;;) {
   3691 		switch (op->type) {
   3692 		case PYSOCKET_TYPE_RECV:
   3693 			ret = read(op->socket->fd, op->buffer.data,
   3694 			    op->buffer.length);
   3695 			break;
   3696 		case PYSOCKET_TYPE_RECVMSG:
   3697 			memset(&msg, 0, sizeof(msg));
   3698 
   3699 			iov.iov_base = op->buffer.data;
   3700 			iov.iov_len = op->buffer.length;
   3701 
   3702 			msg.msg_iov = &iov;
   3703 			msg.msg_iovlen = 1;
   3704 			msg.msg_name = &op->sendaddr;
   3705 			msg.msg_namelen = sizeof(op->sendaddr);
   3706 			msg.msg_control = ancdata;
   3707 			msg.msg_controllen = sizeof(ancdata);
   3708 
   3709 			memset(&op->sendaddr, 0, sizeof(op->sendaddr));
   3710 			ret = recvmsg(op->socket->fd, &msg, 0);
   3711 			break;
   3712 		case PYSOCKET_TYPE_RECVFROM:
   3713 			sendaddr = (struct sockaddr *)&op->sendaddr;
   3714 			switch (op->socket->family) {
   3715 			case AF_INET:
   3716 				socklen = sizeof(op->sendaddr.ipv4);
   3717 				break;
   3718 			case AF_UNIX:
   3719 				socklen = sizeof(op->sendaddr.sun);
   3720 				break;
   3721 			default:
   3722 				fatal("%s: non AF_INET/AF_UNIX", __func__);
   3723 			}
   3724 
   3725 			memset(sendaddr, 0, socklen);
   3726 			ret = recvfrom(op->socket->fd, op->buffer.data,
   3727 			    op->buffer.length, 0, sendaddr, &socklen);
   3728 			break;
   3729 		default:
   3730 			fatal("%s: unknown type %d", __func__, op->type);
   3731 		}
   3732 
   3733 		if (ret == -1) {
   3734 			if (errno == EINTR)
   3735 				continue;
   3736 			if (errno == EAGAIN || errno == EWOULDBLOCK) {
   3737 				op->socket->event.evt.flags &= ~KORE_EVENT_READ;
   3738 				Py_RETURN_NONE;
   3739 			}
   3740 			PyErr_SetString(PyExc_RuntimeError, errno_s);
   3741 			return (NULL);
   3742 		}
   3743 
   3744 		break;
   3745 	}
   3746 
   3747 	op->coro->exception = NULL;
   3748 	op->coro->exception_msg = NULL;
   3749 
   3750 	if (op->timer != NULL) {
   3751 		kore_timer_remove(op->timer);
   3752 		op->timer = NULL;
   3753 	}
   3754 
   3755 	if (op->type == PYSOCKET_TYPE_RECV && ret == 0) {
   3756 		PyErr_SetNone(PyExc_StopIteration);
   3757 		return (NULL);
   3758 	}
   3759 
   3760 	ptr = (const char *)op->buffer.data;
   3761 	if ((bytes = PyBytes_FromStringAndSize(ptr, ret)) == NULL)
   3762 		return (NULL);
   3763 
   3764 	list = NULL;
   3765 
   3766 	switch (op->type) {
   3767 	case PYSOCKET_TYPE_RECV:
   3768 		PyErr_SetObject(PyExc_StopIteration, bytes);
   3769 		Py_DECREF(bytes);
   3770 		return (NULL);
   3771 	case PYSOCKET_TYPE_RECVMSG:
   3772 		socklen = msg.msg_namelen;
   3773 		if ((list = python_cmsg_to_list(&msg)) == NULL) {
   3774 			Py_DECREF(bytes);
   3775 			return (NULL);
   3776 		}
   3777 		break;
   3778 	case PYSOCKET_TYPE_RECVFROM:
   3779 		break;
   3780 	default:
   3781 		fatal("%s: unknown type %d", __func__, op->type);
   3782 	}
   3783 
   3784 	switch(op->socket->family) {
   3785 	case AF_INET:
   3786 		port = ntohs(op->sendaddr.ipv4.sin_port);
   3787 		ip = inet_ntoa(op->sendaddr.ipv4.sin_addr);
   3788 
   3789 		if (op->type == PYSOCKET_TYPE_RECVFROM)
   3790 			tuple = Py_BuildValue("(sHN)", ip, port, bytes);
   3791 		else
   3792 			tuple = Py_BuildValue("(sHNN)", ip, port, bytes, list);
   3793 		break;
   3794 	case AF_UNIX:
   3795 		len = strlen(op->sendaddr.sun.sun_path);
   3796 #if defined(__linux__)
   3797 		if (len == 0 && socklen > 0) {
   3798 			len = socklen - sizeof(sa_family_t);
   3799 			op->sendaddr.sun.sun_path[0] = '@';
   3800 			op->sendaddr.sun.sun_path[len] = '\0';
   3801 		}
   3802 #endif
   3803 		if (len == 0) {
   3804 			if (op->type == PYSOCKET_TYPE_RECVFROM) {
   3805 				tuple = Py_BuildValue("(ON)", Py_None, bytes);
   3806 			} else {
   3807 				tuple = Py_BuildValue("(ONN)",
   3808 				    Py_None, bytes, list);
   3809 			}
   3810 		} else {
   3811 			if (op->type == PYSOCKET_TYPE_RECVFROM) {
   3812 				tuple = Py_BuildValue("(sN)",
   3813 				    op->sendaddr.sun.sun_path, bytes);
   3814 			} else {
   3815 				tuple = Py_BuildValue("(sNN)",
   3816 				    op->sendaddr.sun.sun_path, bytes, list);
   3817 			}
   3818 		}
   3819 		break;
   3820 	default:
   3821 		fatal("%s: non AF_INET/AF_UNIX", __func__);
   3822 	}
   3823 
   3824 	if (tuple == NULL) {
   3825 		Py_XDECREF(list);
   3826 		Py_DECREF(bytes);
   3827 		return (NULL);
   3828 	}
   3829 
   3830 	result = PyObject_CallFunctionObjArgs(PyExc_StopIteration, tuple, NULL);
   3831 	if (result == NULL) {
   3832 		Py_DECREF(tuple);
   3833 		return (NULL);
   3834 	}
   3835 
   3836 	Py_DECREF(tuple);
   3837 	PyErr_SetObject(PyExc_StopIteration, result);
   3838 	Py_DECREF(result);
   3839 
   3840 	return (NULL);
   3841 }
   3842 
   3843 static PyObject *
   3844 pysocket_async_send(struct pysocket_op *op)
   3845 {
   3846 	ssize_t			ret;
   3847 	socklen_t		socklen;
   3848 	const struct sockaddr	*sendaddr;
   3849 
   3850 	if (!(op->socket->event.evt.flags & KORE_EVENT_WRITE)) {
   3851 		Py_RETURN_NONE;
   3852 	}
   3853 
   3854 	for (;;) {
   3855 		if (op->type == PYSOCKET_TYPE_SEND) {
   3856 			ret = write(op->socket->fd,
   3857 			    op->buffer.data + op->buffer.offset,
   3858 			    op->buffer.length - op->buffer.offset);
   3859 		} else {
   3860 			sendaddr = (const struct sockaddr *)&op->sendaddr;
   3861 
   3862 			switch (op->socket->family) {
   3863 			case AF_INET:
   3864 				socklen = sizeof(op->sendaddr.ipv4);
   3865 				break;
   3866 			case AF_UNIX:
   3867 				socklen = sizeof(op->sendaddr.sun);
   3868 #if defined(__linux__)
   3869 				if (op->sendaddr.sun.sun_path[0] == '@') {
   3870 					socklen = sizeof(sa_family_t) +
   3871 					    strlen(op->sendaddr.sun.sun_path);
   3872 					op->sendaddr.sun.sun_path[0] = '\0';
   3873 				}
   3874 #endif
   3875 				break;
   3876 			default:
   3877 				fatal("non AF_INET/AF_UNIX in %s", __func__);
   3878 			}
   3879 
   3880 			ret = sendto(op->socket->fd,
   3881 			    op->buffer.data + op->buffer.offset,
   3882 			    op->buffer.length - op->buffer.offset,
   3883 			    0, sendaddr, socklen);
   3884 		}
   3885 
   3886 		if (ret == -1) {
   3887 			if (errno == EINTR)
   3888 				continue;
   3889 			if (errno == EAGAIN || errno == EWOULDBLOCK) {
   3890 				op->socket->event.evt.flags &=
   3891 				    ~KORE_EVENT_WRITE;
   3892 				Py_RETURN_NONE;
   3893 			}
   3894 			PyErr_SetString(PyExc_RuntimeError, errno_s);
   3895 			return (NULL);
   3896 		}
   3897 		break;
   3898 	}
   3899 
   3900 	op->buffer.offset += (size_t)ret;
   3901 
   3902 	if (op->buffer.offset == op->buffer.length) {
   3903 		PyErr_SetNone(PyExc_StopIteration);
   3904 		return (NULL);
   3905 	}
   3906 
   3907 	Py_RETURN_NONE;
   3908 }
   3909 
   3910 static void
   3911 pysocket_evt_handle(void *arg, int eof)
   3912 {
   3913 	struct pysocket_event		*event = arg;
   3914 	struct pysocket			*socket = event->s;
   3915 
   3916 	if ((eof || (event->evt.flags & KORE_EVENT_READ)) &&
   3917 	    socket->recvop != NULL) {
   3918 		if (socket->recvop->coro->request != NULL)
   3919 			http_request_wakeup(socket->recvop->coro->request);
   3920 		else
   3921 			python_coro_wakeup(socket->recvop->coro);
   3922 		socket->recvop->eof = eof;
   3923 	}
   3924 
   3925 	if ((eof || (event->evt.flags & KORE_EVENT_WRITE)) &&
   3926 	    socket->sendop != NULL) {
   3927 		if (socket->sendop->coro->request != NULL)
   3928 			http_request_wakeup(socket->sendop->coro->request);
   3929 		else
   3930 			python_coro_wakeup(socket->sendop->coro);
   3931 		socket->sendop->eof = eof;
   3932 	}
   3933 }
   3934 
   3935 static void
   3936 pyqueue_dealloc(struct pyqueue *queue)
   3937 {
   3938 	struct pyqueue_object	*object;
   3939 	struct pyqueue_waiting	*waiting;
   3940 
   3941 	while ((object = TAILQ_FIRST(&queue->objects)) != NULL) {
   3942 		TAILQ_REMOVE(&queue->objects, object, list);
   3943 		Py_DECREF(object->obj);
   3944 		kore_pool_put(&queue_object_pool, object);
   3945 	}
   3946 
   3947 	while ((waiting = TAILQ_FIRST(&queue->waiting)) != NULL) {
   3948 		TAILQ_REMOVE(&queue->waiting, waiting, list);
   3949 		if (waiting->op != NULL)
   3950 			waiting->op->waiting = NULL;
   3951 		kore_pool_put(&queue_wait_pool, waiting);
   3952 	}
   3953 
   3954 	PyObject_Del((PyObject *)queue);
   3955 }
   3956 
   3957 static PyObject *
   3958 pyqueue_pop(struct pyqueue *queue, PyObject *args)
   3959 {
   3960 	struct pyqueue_op	*op;
   3961 
   3962 	if ((op = PyObject_New(struct pyqueue_op, &pyqueue_op_type)) == NULL)
   3963 		return (NULL);
   3964 
   3965 	op->queue = queue;
   3966 	op->waiting = kore_pool_get(&queue_wait_pool);
   3967 	op->waiting->op = op;
   3968 
   3969 	op->waiting->coro = coro_running;
   3970 	TAILQ_INSERT_TAIL(&queue->waiting, op->waiting, list);
   3971 
   3972 	Py_INCREF((PyObject *)queue);
   3973 
   3974 	return ((PyObject *)op);
   3975 }
   3976 
   3977 static PyObject *
   3978 pyqueue_popnow(struct pyqueue *queue, PyObject *args)
   3979 {
   3980 	PyObject		*obj;
   3981 	struct pyqueue_object	*object;
   3982 
   3983 	if ((object = TAILQ_FIRST(&queue->objects)) == NULL) {
   3984 		Py_RETURN_NONE;
   3985 	}
   3986 
   3987 	TAILQ_REMOVE(&queue->objects, object, list);
   3988 
   3989 	obj = object->obj;
   3990 	kore_pool_put(&queue_object_pool, object);
   3991 
   3992 	return (obj);
   3993 }
   3994 
   3995 static PyObject *
   3996 pyqueue_push(struct pyqueue *queue, PyObject *args)
   3997 {
   3998 	PyObject		*obj;
   3999 	struct pyqueue_object	*object;
   4000 	struct pyqueue_waiting	*waiting;
   4001 
   4002 	if (!PyArg_ParseTuple(args, "O", &obj))
   4003 		return (NULL);
   4004 
   4005 	Py_INCREF(obj);
   4006 
   4007 	object = kore_pool_get(&queue_object_pool);
   4008 	object->obj = obj;
   4009 
   4010 	TAILQ_INSERT_TAIL(&queue->objects, object, list);
   4011 
   4012 	/* Wakeup first in line if any. */
   4013 	if ((waiting = TAILQ_FIRST(&queue->waiting)) != NULL) {
   4014 		TAILQ_REMOVE(&queue->waiting, waiting, list);
   4015 
   4016 		/* wakeup HTTP request if one is tied. */
   4017 		if (waiting->coro->request != NULL)
   4018 			http_request_wakeup(waiting->coro->request);
   4019 		else
   4020 			python_coro_wakeup(waiting->coro);
   4021 
   4022 		waiting->op->waiting = NULL;
   4023 		kore_pool_put(&queue_wait_pool, waiting);
   4024 	}
   4025 
   4026 	Py_RETURN_TRUE;
   4027 }
   4028 
   4029 static void
   4030 pyqueue_op_dealloc(struct pyqueue_op *op)
   4031 {
   4032 	if (op->waiting != NULL) {
   4033 		TAILQ_REMOVE(&op->queue->waiting, op->waiting, list);
   4034 		kore_pool_put(&queue_wait_pool, op->waiting);
   4035 		op->waiting = NULL;
   4036 	}
   4037 
   4038 	Py_DECREF((PyObject *)op->queue);
   4039 	PyObject_Del((PyObject *)op);
   4040 }
   4041 
   4042 static PyObject *
   4043 pyqueue_op_await(PyObject *obj)
   4044 {
   4045 	Py_INCREF(obj);
   4046 	return (obj);
   4047 }
   4048 
   4049 static PyObject *
   4050 pyqueue_op_iternext(struct pyqueue_op *op)
   4051 {
   4052 	PyObject		*obj;
   4053 	struct pyqueue_object	*object;
   4054 	struct pyqueue_waiting	*waiting;
   4055 
   4056 	if ((object = TAILQ_FIRST(&op->queue->objects)) == NULL) {
   4057 		Py_RETURN_NONE;
   4058 	}
   4059 
   4060 	TAILQ_REMOVE(&op->queue->objects, object, list);
   4061 
   4062 	obj = object->obj;
   4063 	kore_pool_put(&queue_object_pool, object);
   4064 
   4065 	TAILQ_FOREACH(waiting, &op->queue->waiting, list) {
   4066 		if (waiting->coro->id == coro_running->id) {
   4067 			TAILQ_REMOVE(&op->queue->waiting, waiting, list);
   4068 			waiting->op->waiting = NULL;
   4069 			kore_pool_put(&queue_wait_pool, waiting);
   4070 			break;
   4071 		}
   4072 	}
   4073 
   4074 	PyErr_SetObject(PyExc_StopIteration, obj);
   4075 	Py_DECREF(obj);
   4076 
   4077 	return (NULL);
   4078 }
   4079 
   4080 static void
   4081 pylock_dealloc(struct pylock *lock)
   4082 {
   4083 	struct pylock_op	*op;
   4084 
   4085 	while ((op = TAILQ_FIRST(&lock->ops)) != NULL) {
   4086 		TAILQ_REMOVE(&lock->ops, op, list);
   4087 		op->active = 0;
   4088 		op->coro->lockop = NULL;
   4089 		Py_DECREF((PyObject *)op);
   4090 	}
   4091 
   4092 	PyObject_Del((PyObject *)lock);
   4093 }
   4094 
   4095 static PyObject *
   4096 pylock_trylock(struct pylock *lock, PyObject *args)
   4097 {
   4098 	if (lock->owner != NULL)
   4099 		Py_RETURN_FALSE;
   4100 
   4101 	lock->owner = coro_running;
   4102 
   4103 	Py_RETURN_TRUE;
   4104 }
   4105 
   4106 static PyObject *
   4107 pylock_release(struct pylock *lock, PyObject *args)
   4108 {
   4109 	if (lock->owner == NULL) {
   4110 		PyErr_SetString(PyExc_RuntimeError, "no lock owner set");
   4111 		return (NULL);
   4112 	}
   4113 
   4114 	if (lock->owner->id != coro_running->id) {
   4115 		PyErr_SetString(PyExc_RuntimeError, "lock not owned by caller");
   4116 		return (NULL);
   4117 	}
   4118 
   4119 	pylock_do_release(lock);
   4120 
   4121 	Py_RETURN_NONE;
   4122 }
   4123 
   4124 static PyObject *
   4125 pylock_aenter(struct pylock *lock, PyObject *args)
   4126 {
   4127 	struct pylock_op	*op;
   4128 
   4129 	if (coro_running->lockop != NULL) {
   4130 		fatal("%s: lockop not NULL for %" PRIu64,
   4131 		    __func__, coro_running->id);
   4132 	}
   4133 
   4134 	if (lock->owner != NULL && lock->owner->id == coro_running->id) {
   4135 		PyErr_SetString(PyExc_RuntimeError, "recursive lock detected");
   4136 		return (NULL);
   4137 	}
   4138 
   4139 	if ((op = PyObject_New(struct pylock_op, &pylock_op_type)) == NULL)
   4140 		return (NULL);
   4141 
   4142 	op->active = 1;
   4143 	op->lock = lock;
   4144 	op->locking = 1;
   4145 	op->coro = coro_running;
   4146 
   4147 	coro_running->lockop = op;
   4148 
   4149 	Py_INCREF((PyObject *)op);
   4150 	Py_INCREF((PyObject *)lock);
   4151 
   4152 	TAILQ_INSERT_TAIL(&lock->ops, op, list);
   4153 
   4154 	return ((PyObject *)op);
   4155 }
   4156 
   4157 static PyObject *
   4158 pylock_aexit(struct pylock *lock, PyObject *args)
   4159 {
   4160 	struct pylock_op	*op;
   4161 
   4162 	if (coro_running->lockop != NULL) {
   4163 		fatal("%s: lockop not NULL for %" PRIu64,
   4164 		    __func__, coro_running->id);
   4165 	}
   4166 
   4167 	if (lock->owner == NULL || lock->owner->id != coro_running->id) {
   4168 		PyErr_SetString(PyExc_RuntimeError, "invalid lock owner");
   4169 		return (NULL);
   4170 	}
   4171 
   4172 	if ((op = PyObject_New(struct pylock_op, &pylock_op_type)) == NULL)
   4173 		return (NULL);
   4174 
   4175 	op->active = 1;
   4176 	op->lock = lock;
   4177 	op->locking = 0;
   4178 	op->coro = coro_running;
   4179 
   4180 	coro_running->lockop = op;
   4181 
   4182 	Py_INCREF((PyObject *)op);
   4183 	Py_INCREF((PyObject *)lock);
   4184 
   4185 	TAILQ_INSERT_TAIL(&lock->ops, op, list);
   4186 
   4187 	return ((PyObject *)op);
   4188 }
   4189 
   4190 static void
   4191 pylock_do_release(struct pylock *lock)
   4192 {
   4193 	struct pylock_op	*op;
   4194 
   4195 	lock->owner = NULL;
   4196 
   4197 	TAILQ_FOREACH(op, &lock->ops, list) {
   4198 		if (op->locking == 0)
   4199 			continue;
   4200 
   4201 		op->active = 0;
   4202 		op->coro->lockop = NULL;
   4203 		TAILQ_REMOVE(&lock->ops, op, list);
   4204 
   4205 		if (op->coro->request != NULL)
   4206 			http_request_wakeup(op->coro->request);
   4207 		else
   4208 			python_coro_wakeup(op->coro);
   4209 
   4210 		Py_DECREF((PyObject *)op);
   4211 		break;
   4212 	}
   4213 }
   4214 
   4215 static void
   4216 pylock_op_dealloc(struct pylock_op *op)
   4217 {
   4218 	if (op->active) {
   4219 		TAILQ_REMOVE(&op->lock->ops, op, list);
   4220 		op->active = 0;
   4221 	}
   4222 
   4223 	op->coro->lockop = NULL;
   4224 
   4225 	Py_DECREF((PyObject *)op->lock);
   4226 	PyObject_Del((PyObject *)op);
   4227 }
   4228 
   4229 static PyObject *
   4230 pylock_op_await(PyObject *obj)
   4231 {
   4232 	Py_INCREF(obj);
   4233 	return (obj);
   4234 }
   4235 
   4236 static PyObject *
   4237 pylock_op_iternext(struct pylock_op *op)
   4238 {
   4239 	if (op->locking == 0) {
   4240 		if (op->lock->owner == NULL) {
   4241 			PyErr_SetString(PyExc_RuntimeError,
   4242 			    "no lock owner set");
   4243 			return (NULL);
   4244 		}
   4245 
   4246 		if (op->lock->owner->id != coro_running->id) {
   4247 			PyErr_SetString(PyExc_RuntimeError,
   4248 			    "lock not owned by caller");
   4249 			return (NULL);
   4250 		}
   4251 
   4252 		pylock_do_release(op->lock);
   4253 	} else {
   4254 		if (op->lock->owner != NULL) {
   4255 			/*
   4256 			 * We could be beat by another coroutine that grabbed
   4257 			 * the lock even if we were the one woken up for it.
   4258 			 */
   4259 			if (op->active == 0) {
   4260 				op->active = 1;
   4261 				op->coro->lockop = op;
   4262 				TAILQ_INSERT_HEAD(&op->lock->ops, op, list);
   4263 				Py_INCREF((PyObject *)op);
   4264 			}
   4265 			Py_RETURN_NONE;
   4266 		}
   4267 
   4268 		op->lock->owner = coro_running;
   4269 	}
   4270 
   4271 	if (op->active) {
   4272 		op->active = 0;
   4273 		op->coro->lockop = NULL;
   4274 		TAILQ_REMOVE(&op->lock->ops, op, list);
   4275 		Py_DECREF((PyObject *)op);
   4276 	}
   4277 
   4278 	PyErr_SetNone(PyExc_StopIteration);
   4279 
   4280 	return (NULL);
   4281 }
   4282 
   4283 static void
   4284 pyproc_timeout(void *arg, u_int64_t now)
   4285 {
   4286 	struct pyproc	*proc = arg;
   4287 
   4288 	proc->timer = NULL;
   4289 
   4290 	if (proc->coro->sockop != NULL)
   4291 		proc->coro->sockop->eof = 1;
   4292 
   4293 	proc->coro->exception = PyExc_TimeoutError;
   4294 	proc->coro->exception_msg = "timeout before process exited";
   4295 
   4296 	if (proc->coro->request != NULL)
   4297 		http_request_wakeup(proc->coro->request);
   4298 	else
   4299 		python_coro_wakeup(proc->coro);
   4300 }
   4301 
   4302 static void
   4303 pyproc_dealloc(struct pyproc *proc)
   4304 {
   4305 	int	status;
   4306 
   4307 	TAILQ_REMOVE(&procs, proc, list);
   4308 
   4309 	if (proc->timer != NULL) {
   4310 		kore_timer_remove(proc->timer);
   4311 		proc->timer = NULL;
   4312 	}
   4313 
   4314 	if (proc->pid != -1) {
   4315 		if (kill(proc->pid, SIGKILL) == -1) {
   4316 			kore_log(LOG_NOTICE,
   4317 			    "kore.proc failed to send SIGKILL %d (%s)",
   4318 			    proc->pid, errno_s);
   4319 		}
   4320 
   4321 		for (;;) {
   4322 			if (waitpid(proc->pid, &status, 0) == -1) {
   4323 				if (errno == EINTR)
   4324 					continue;
   4325 				kore_log(LOG_NOTICE,
   4326 				    "kore.proc failed to wait for %d (%s)",
   4327 				    proc->pid, errno_s);
   4328 			}
   4329 			break;
   4330 		}
   4331 	}
   4332 
   4333 	if (proc->in != NULL) {
   4334 		Py_DECREF((PyObject *)proc->in);
   4335 		proc->in = NULL;
   4336 	}
   4337 
   4338 	if (proc->out != NULL) {
   4339 		Py_DECREF((PyObject *)proc->out);
   4340 		proc->out = NULL;
   4341 	}
   4342 
   4343 	PyObject_Del((PyObject *)proc);
   4344 }
   4345 
   4346 static PyObject *
   4347 pyproc_kill(struct pyproc *proc, PyObject *args)
   4348 {
   4349 	if (proc->pid != -1 && kill(proc->pid, SIGKILL) == -1)
   4350 		kore_log(LOG_NOTICE, "kill(%d): %s", proc->pid, errno_s);
   4351 
   4352 	Py_RETURN_TRUE;
   4353 }
   4354 
   4355 static PyObject *
   4356 pyproc_reap(struct pyproc *proc, PyObject *args)
   4357 {
   4358 	struct pyproc_op	*op;
   4359 
   4360 	if (proc->op != NULL) {
   4361 		PyErr_Format(PyExc_RuntimeError,
   4362 		    "process %d already being reaped", proc->apid);
   4363 		return (NULL);
   4364 	}
   4365 
   4366 	if (proc->timer != NULL) {
   4367 		kore_timer_remove(proc->timer);
   4368 		proc->timer = NULL;
   4369 	}
   4370 
   4371 	if ((op = PyObject_New(struct pyproc_op, &pyproc_op_type)) == NULL)
   4372 		return (NULL);
   4373 
   4374 	op->proc = proc;
   4375 	op->coro = coro_running;
   4376 
   4377 	proc->op = op;
   4378 
   4379 	Py_INCREF((PyObject *)proc);
   4380 
   4381 	return ((PyObject *)op);
   4382 }
   4383 
   4384 static PyObject *
   4385 pyproc_recv(struct pyproc *proc, PyObject *args)
   4386 {
   4387 	Py_ssize_t		len;
   4388 	struct pysocket_op	*op;
   4389 	PyObject		*obj;
   4390 	int			timeo;
   4391 
   4392 	timeo = -1;
   4393 
   4394 	if (proc->out == NULL) {
   4395 		PyErr_SetString(PyExc_RuntimeError, "stdout closed");
   4396 		return (NULL);
   4397 	}
   4398 
   4399 	if (!PyArg_ParseTuple(args, "n|i", &len, &timeo))
   4400 		return (NULL);
   4401 
   4402 	obj = pysocket_op_create(proc->out, PYSOCKET_TYPE_RECV, NULL, len);
   4403 	if (obj == NULL)
   4404 		return (NULL);
   4405 
   4406 	op = (struct pysocket_op *)obj;
   4407 
   4408 	if (timeo != -1) {
   4409 		op->timer = kore_timer_add(pysocket_op_timeout,
   4410 		    timeo, op, KORE_TIMER_ONESHOT);
   4411 	}
   4412 
   4413 	return (obj);
   4414 }
   4415 
   4416 static PyObject *
   4417 pyproc_send(struct pyproc *proc, PyObject *args)
   4418 {
   4419 	Py_buffer	buf;
   4420 	PyObject	*ret;
   4421 
   4422 	if (proc->in == NULL) {
   4423 		PyErr_SetString(PyExc_RuntimeError, "stdin closed");
   4424 		return (NULL);
   4425 	}
   4426 
   4427 	if (!PyArg_ParseTuple(args, "y*", &buf))
   4428 		return (NULL);
   4429 
   4430 	ret = pysocket_op_create(proc->in,
   4431 	    PYSOCKET_TYPE_SEND, buf.buf, buf.len);
   4432 
   4433 	PyBuffer_Release(&buf);
   4434 
   4435 	return (ret);
   4436 }
   4437 
   4438 static PyObject *
   4439 pyproc_close_stdin(struct pyproc *proc, PyObject *args)
   4440 {
   4441 	if (proc->in != NULL) {
   4442 		Py_DECREF((PyObject *)proc->in);
   4443 		proc->in = NULL;
   4444 	}
   4445 
   4446 	Py_RETURN_TRUE;
   4447 }
   4448 
   4449 static PyObject *
   4450 pyproc_get_pid(struct pyproc *proc, void *closure)
   4451 {
   4452 	return (PyLong_FromLong(proc->apid));
   4453 }
   4454 
   4455 static void
   4456 pyproc_op_dealloc(struct pyproc_op *op)
   4457 {
   4458 	Py_DECREF((PyObject *)op->proc);
   4459 	PyObject_Del((PyObject *)op);
   4460 }
   4461 
   4462 static PyObject *
   4463 pyproc_op_await(PyObject *sop)
   4464 {
   4465 	Py_INCREF(sop);
   4466 	return (sop);
   4467 }
   4468 
   4469 static PyObject *
   4470 pyproc_op_iternext(struct pyproc_op *op)
   4471 {
   4472 	int		ret;
   4473 	PyObject	*res;
   4474 
   4475 	if (op->proc->coro->exception != NULL) {
   4476 		PyErr_SetString(op->proc->coro->exception,
   4477 		    op->proc->coro->exception_msg);
   4478 		op->proc->coro->exception = NULL;
   4479 		return (NULL);
   4480 	}
   4481 
   4482 	if (op->proc->reaped == 0)
   4483 		Py_RETURN_NONE;
   4484 
   4485 	if (WIFSTOPPED(op->proc->status)) {
   4486 		op->proc->reaped = 0;
   4487 		Py_RETURN_NONE;
   4488 	}
   4489 
   4490 	if (WIFEXITED(op->proc->status)) {
   4491 		ret = WEXITSTATUS(op->proc->status);
   4492 	} else {
   4493 		ret = op->proc->status;
   4494 	}
   4495 
   4496 	if ((res = PyLong_FromLong(ret)) == NULL)
   4497 		return (NULL);
   4498 
   4499 	PyErr_SetObject(PyExc_StopIteration, res);
   4500 	Py_DECREF(res);
   4501 
   4502 	return (NULL);
   4503 }
   4504 
   4505 static void
   4506 pygather_reap_coro(struct pygather_op *op, struct python_coro *reap)
   4507 {
   4508 	struct pygather_coro	*coro;
   4509 	struct pygather_result	*result;
   4510 #if PY_VERSION_HEX >= 0x030A0000
   4511 	PyObject		*type, *traceback;
   4512 #endif
   4513 
   4514 	TAILQ_FOREACH(coro, &op->coroutines, list) {
   4515 		if (coro->coro->id == reap->id)
   4516 			break;
   4517 	}
   4518 
   4519 	if (coro == NULL)
   4520 		fatal("coroutine %" PRIu64 " not found in gather", reap->id);
   4521 
   4522 	op->running--;
   4523 	if (op->running < 0)
   4524 		fatal("gatherop: running miscount (%d)", op->running);
   4525 
   4526 	result = kore_pool_get(&gather_result_pool);
   4527 	result->obj = NULL;
   4528 
   4529 #if PY_VERSION_HEX < 0x030A0000
   4530 	if (_PyGen_FetchStopIterationValue(&result->obj) == -1) {
   4531 		result->obj = Py_None;
   4532 		Py_INCREF(Py_None);
   4533 	}
   4534 #else
   4535 	if (PyErr_Occurred()) {
   4536 		Py_XDECREF(coro->coro->result);
   4537 		PyErr_Fetch(&type, &coro->coro->result, &traceback);
   4538 		Py_DECREF(type);
   4539 		Py_XDECREF(traceback);
   4540 	} else {
   4541 		if (coro->coro->result == NULL) {
   4542 			coro->coro->result = Py_None;
   4543 			Py_INCREF(Py_None);
   4544 		}
   4545 	}
   4546 
   4547 	result->obj = coro->coro->result;
   4548 	Py_INCREF(result->obj);
   4549 #endif
   4550 
   4551 	TAILQ_INSERT_TAIL(&op->results, result, list);
   4552 
   4553 	TAILQ_REMOVE(&op->coroutines, coro, list);
   4554 	kore_pool_put(&gather_coro_pool, coro);
   4555 
   4556 	kore_python_coro_delete(reap);
   4557 }
   4558 
   4559 static void
   4560 pygather_op_dealloc(struct pygather_op *op)
   4561 {
   4562 	struct python_coro		*old;
   4563 	struct pygather_coro		*coro, *next;
   4564 	struct pygather_result		*res, *rnext;
   4565 
   4566 	/*
   4567 	 * Since we are calling kore_python_coro_delete() on all the
   4568 	 * remaining coroutines in this gather op we must remember the
   4569 	 * original coroutine that is running as the removal will end
   4570 	 * up setting coro_running to NULL.
   4571 	 */
   4572 	old = coro_running;
   4573 
   4574 	for (coro = TAILQ_FIRST(&op->coroutines); coro != NULL; coro = next) {
   4575 		next = TAILQ_NEXT(coro, list);
   4576 		TAILQ_REMOVE(&op->coroutines, coro, list);
   4577 
   4578 		/* Make sure we don't end up in pygather_reap_coro(). */
   4579 		coro->coro->gatherop = NULL;
   4580 
   4581 		kore_python_coro_delete(coro->coro);
   4582 		kore_pool_put(&gather_coro_pool, coro);
   4583 	}
   4584 
   4585 	coro_running = old;
   4586 
   4587 	for (res = TAILQ_FIRST(&op->results); res != NULL; res = rnext) {
   4588 		rnext = TAILQ_NEXT(res, list);
   4589 		TAILQ_REMOVE(&op->results, res, list);
   4590 
   4591 		Py_DECREF(res->obj);
   4592 		kore_pool_put(&gather_result_pool, res);
   4593 	}
   4594 
   4595 	PyObject_Del((PyObject *)op);
   4596 }
   4597 
   4598 static PyObject *
   4599 pygather_op_await(PyObject *obj)
   4600 {
   4601 	Py_INCREF(obj);
   4602 	return (obj);
   4603 }
   4604 
   4605 static PyObject *
   4606 pygather_op_iternext(struct pygather_op *op)
   4607 {
   4608 	int				idx;
   4609 	struct pygather_coro		*coro;
   4610 	struct pygather_result		*res, *next;
   4611 	PyObject			*list, *obj;
   4612 
   4613 	if (!TAILQ_EMPTY(&op->coroutines)) {
   4614 		if (op->running > 0)
   4615 			Py_RETURN_NONE;
   4616 
   4617 		TAILQ_FOREACH(coro, &op->coroutines, list) {
   4618 			if (op->running >= op->concurrency)
   4619 				break;
   4620 			python_coro_wakeup(coro->coro);
   4621 			op->running++;
   4622 		}
   4623 
   4624 		Py_RETURN_NONE;
   4625 	}
   4626 
   4627 	if ((list = PyList_New(op->count)) == NULL)
   4628 		return (NULL);
   4629 
   4630 	idx = 0;
   4631 
   4632 	for (res = TAILQ_FIRST(&op->results); res != NULL; res = next) {
   4633 		next = TAILQ_NEXT(res, list);
   4634 		TAILQ_REMOVE(&op->results, res, list);
   4635 
   4636 		obj = res->obj;
   4637 		res->obj = NULL;
   4638 		kore_pool_put(&gather_result_pool, res);
   4639 
   4640 		if (PyList_SetItem(list, idx++, obj) != 0) {
   4641 			Py_DECREF(list);
   4642 			return (NULL);
   4643 		}
   4644 	}
   4645 
   4646 	PyErr_SetObject(PyExc_StopIteration, list);
   4647 	Py_DECREF(list);
   4648 
   4649 	return (NULL);
   4650 }
   4651 
   4652 static PyObject *
   4653 pyhttp_request_alloc(const struct http_request *req)
   4654 {
   4655 	union { const void *cp; void *p; }	ptr;
   4656 	struct pyhttp_request			*pyreq;
   4657 
   4658 	pyreq = PyObject_New(struct pyhttp_request, &pyhttp_request_type);
   4659 	if (pyreq == NULL)
   4660 		return (NULL);
   4661 
   4662 	/*
   4663 	 * Hack around all http apis taking a non-const pointer and us having
   4664 	 * a const pointer for the req data structure. This is because we
   4665 	 * could potentially be called from a validator where the argument
   4666 	 * is a http_request pointer.
   4667 	 */
   4668 	ptr.cp = req;
   4669 	pyreq->req = ptr.p;
   4670 	pyreq->data = NULL;
   4671 	pyreq->dict = NULL;
   4672 
   4673 	return ((PyObject *)pyreq);
   4674 }
   4675 
   4676 static PyObject *
   4677 pyhttp_file_alloc(struct http_file *file)
   4678 {
   4679 	struct pyhttp_file		*pyfile;
   4680 
   4681 	pyfile = PyObject_New(struct pyhttp_file, &pyhttp_file_type);
   4682 	if (pyfile == NULL)
   4683 		return (NULL);
   4684 
   4685 	pyfile->file = file;
   4686 
   4687 	return ((PyObject *)pyfile);
   4688 }
   4689 
   4690 static int
   4691 pyhttp_preprocess(struct http_request *req)
   4692 {
   4693 	struct reqcall		*rq;
   4694 	PyObject		*ret;
   4695 
   4696 	rq = req->py_rqnext;
   4697 
   4698 	while (rq) {
   4699 		req->py_rqnext = TAILQ_NEXT(rq, list);
   4700 
   4701 		PyErr_Clear();
   4702 		ret = PyObject_CallFunctionObjArgs(rq->f, req->py_req, NULL);
   4703 
   4704 		if (ret == NULL) {
   4705 			kore_python_log_error("preprocess");
   4706 			http_response(req, HTTP_STATUS_INTERNAL_ERROR, NULL, 0);
   4707 			return (KORE_RESULT_ERROR);
   4708 		}
   4709 
   4710 		if (ret == Py_False) {
   4711 			Py_DECREF(ret);
   4712 			return (KORE_RESULT_ERROR);
   4713 		}
   4714 
   4715 		if (PyCoro_CheckExact(ret)) {
   4716 			req->py_coro = python_coro_create(ret, req);
   4717 			if (python_coro_run(req->py_coro) == KORE_RESULT_OK) {
   4718 				http_request_wakeup(req);
   4719 				kore_python_coro_delete(req->py_coro);
   4720 				req->py_coro = NULL;
   4721 				rq = req->py_rqnext;
   4722 				continue;
   4723 			}
   4724 			return (KORE_RESULT_RETRY);
   4725 		}
   4726 
   4727 		Py_DECREF(ret);
   4728 		rq = req->py_rqnext;
   4729 	}
   4730 
   4731 	return (KORE_RESULT_OK);
   4732 }
   4733 
   4734 static PyObject *
   4735 pyhttp_response(struct pyhttp_request *pyreq, PyObject *args)
   4736 {
   4737 	struct connection	*c;
   4738 	char			*ptr;
   4739 	Py_ssize_t		length;
   4740 	int			status;
   4741 	struct pyhttp_iterobj	*iterobj;
   4742 	PyObject		*obj, *iterator;
   4743 
   4744 	length = -1;
   4745 
   4746 	if (!PyArg_ParseTuple(args, "iO", &status, &obj))
   4747 		return (NULL);
   4748 
   4749 	if (PyBytes_CheckExact(obj)) {
   4750 		if (PyBytes_AsStringAndSize(obj, &ptr, &length) == -1)
   4751 			return (NULL);
   4752 
   4753 		if (length < 0) {
   4754 			PyErr_SetString(PyExc_TypeError, "invalid length");
   4755 			return (NULL);
   4756 		}
   4757 
   4758 		Py_INCREF(obj);
   4759 
   4760 		http_response_stream(pyreq->req, status, ptr, length,
   4761 		    pyhttp_response_sent, obj);
   4762 	} else if (obj == Py_None) {
   4763 		http_response(pyreq->req, status, NULL, 0);
   4764 	} else {
   4765 		c = pyreq->req->owner;
   4766 		if (c->state == CONN_STATE_DISCONNECTING) {
   4767 			Py_RETURN_FALSE;
   4768 		}
   4769 
   4770 		if ((iterator = PyObject_GetIter(obj)) == NULL)
   4771 			return (NULL);
   4772 
   4773 		iterobj = kore_pool_get(&iterobj_pool);
   4774 		iterobj->iterator = iterator;
   4775 		iterobj->connection = c;
   4776 		iterobj->remove = 0;
   4777 
   4778 		kore_buf_init(&iterobj->buf, 4096);
   4779 
   4780 		c->hdlr_extra = iterobj;
   4781 		c->flags |= CONN_IS_BUSY;
   4782 		c->disconnect = pyhttp_iterobj_disconnect;
   4783 
   4784 		pyreq->req->flags |= HTTP_REQUEST_NO_CONTENT_LENGTH;
   4785 		http_response_header(pyreq->req, "transfer-encoding",
   4786 		    "chunked");
   4787 
   4788 		http_response(pyreq->req, status, NULL, 0);
   4789 		pyhttp_iterobj_next(iterobj);
   4790 	}
   4791 
   4792 	Py_RETURN_TRUE;
   4793 }
   4794 
   4795 static int
   4796 pyhttp_response_sent(struct netbuf *nb)
   4797 {
   4798 	PyObject	*data;
   4799 
   4800 	data = nb->extra;
   4801 	Py_DECREF(data);
   4802 
   4803 	return (KORE_RESULT_OK);
   4804 }
   4805 
   4806 static int
   4807 pyhttp_iterobj_next(struct pyhttp_iterobj *iterobj)
   4808 {
   4809 	struct netbuf		*nb;
   4810 	PyObject		*obj;
   4811 	const char		*ptr;
   4812 	Py_ssize_t		length;
   4813 
   4814 	PyErr_Clear();
   4815 
   4816 	if ((obj = PyIter_Next(iterobj->iterator)) == NULL) {
   4817 		if (PyErr_Occurred()) {
   4818 			kore_python_log_error("pyhttp_iterobj_next");
   4819 			return (KORE_RESULT_ERROR);
   4820 		}
   4821 
   4822 		return (KORE_RESULT_OK);
   4823 	}
   4824 
   4825 	if ((ptr = PyUnicode_AsUTF8AndSize(obj, &length)) == NULL) {
   4826 		kore_python_log_error("pyhttp_iterobj_next");
   4827 		return (KORE_RESULT_ERROR);
   4828 	}
   4829 
   4830 	kore_buf_reset(&iterobj->buf);
   4831 	kore_buf_appendf(&iterobj->buf, "%lx\r\n", length);
   4832 	kore_buf_append(&iterobj->buf, ptr, length);
   4833 	kore_buf_appendf(&iterobj->buf, "\r\n");
   4834 
   4835 	Py_DECREF(obj);
   4836 
   4837 	net_send_stream(iterobj->connection, iterobj->buf.data,
   4838 	    iterobj->buf.offset, pyhttp_iterobj_chunk_sent, &nb);
   4839 
   4840 	nb->extra = iterobj;
   4841 
   4842 	return (KORE_RESULT_RETRY);
   4843 }
   4844 
   4845 static int
   4846 pyhttp_iterobj_chunk_sent(struct netbuf *nb)
   4847 {
   4848 	int			ret;
   4849 	struct pyhttp_iterobj	*iterobj;
   4850 
   4851 	iterobj = nb->extra;
   4852 
   4853 	if (iterobj->remove) {
   4854 		ret = KORE_RESULT_ERROR;
   4855 	} else {
   4856 		ret = pyhttp_iterobj_next(iterobj);
   4857 	}
   4858 
   4859 	if (ret != KORE_RESULT_RETRY) {
   4860 		iterobj->connection->hdlr_extra = NULL;
   4861 		iterobj->connection->disconnect = NULL;
   4862 		iterobj->connection->flags &= ~CONN_IS_BUSY;
   4863 
   4864 		if (iterobj->remove == 0)
   4865 			http_start_recv(iterobj->connection);
   4866 
   4867 		kore_buf_reset(&iterobj->buf);
   4868 		kore_buf_appendf(&iterobj->buf, "0\r\n\r\n");
   4869 		net_send_queue(iterobj->connection,
   4870 		    iterobj->buf.data, iterobj->buf.offset);
   4871 
   4872 		Py_DECREF(iterobj->iterator);
   4873 
   4874 		kore_buf_cleanup(&iterobj->buf);
   4875 		kore_pool_put(&iterobj_pool, iterobj);
   4876 	} else {
   4877 		ret = KORE_RESULT_OK;
   4878 	}
   4879 
   4880 	return (ret);
   4881 }
   4882 
   4883 static void
   4884 pyhttp_iterobj_disconnect(struct connection *c)
   4885 {
   4886 	struct pyhttp_iterobj	*iterobj;
   4887 
   4888 	iterobj = c->hdlr_extra;
   4889 	iterobj->remove = 1;
   4890 	c->hdlr_extra = NULL;
   4891 }
   4892 
   4893 static PyObject *
   4894 pyhttp_response_header(struct pyhttp_request *pyreq, PyObject *args)
   4895 {
   4896 	const char		*header, *value;
   4897 
   4898 	if (!PyArg_ParseTuple(args, "ss", &header, &value))
   4899 		return (NULL);
   4900 
   4901 	http_response_header(pyreq->req, header, value);
   4902 
   4903 	Py_RETURN_TRUE;
   4904 }
   4905 
   4906 static PyObject *
   4907 pyhttp_request_header(struct pyhttp_request *pyreq, PyObject *args)
   4908 {
   4909 	const char		*value;
   4910 	const char		*header;
   4911 	PyObject		*result;
   4912 
   4913 	if (!PyArg_ParseTuple(args, "s", &header))
   4914 		return (NULL);
   4915 
   4916 	if (!http_request_header(pyreq->req, header, &value)) {
   4917 		Py_RETURN_NONE;
   4918 	}
   4919 
   4920 	if ((result = PyUnicode_FromString(value)) == NULL)
   4921 		return (PyErr_NoMemory());
   4922 
   4923 	return (result);
   4924 }
   4925 
   4926 static PyObject *
   4927 pyhttp_body_read(struct pyhttp_request *pyreq, PyObject *args)
   4928 {
   4929 	ssize_t			ret;
   4930 	size_t			len;
   4931 	Py_ssize_t		pylen;
   4932 	PyObject		*result;
   4933 	u_int8_t		buf[1024];
   4934 
   4935 	if (!PyArg_ParseTuple(args, "n", &pylen) || pylen < 0)
   4936 		return (NULL);
   4937 
   4938 	len = (size_t)pylen;
   4939 	if (len > sizeof(buf)) {
   4940 		PyErr_SetString(PyExc_RuntimeError, "len > sizeof(buf)");
   4941 		return (NULL);
   4942 	}
   4943 
   4944 	ret = http_body_read(pyreq->req, buf, len);
   4945 	if (ret == -1) {
   4946 		PyErr_SetString(PyExc_RuntimeError, "http_body_read() failed");
   4947 		return (NULL);
   4948 	}
   4949 
   4950 	result = Py_BuildValue("ny#", ret, buf, ret);
   4951 	if (result == NULL)
   4952 		return (PyErr_NoMemory());
   4953 
   4954 	return (result);
   4955 }
   4956 
   4957 static PyObject *
   4958 pyhttp_populate_get(struct pyhttp_request *pyreq, PyObject *args)
   4959 {
   4960 	http_populate_get(pyreq->req);
   4961 	Py_RETURN_TRUE;
   4962 }
   4963 
   4964 static PyObject *
   4965 pyhttp_populate_post(struct pyhttp_request *pyreq, PyObject *args)
   4966 {
   4967 	http_populate_post(pyreq->req);
   4968 	Py_RETURN_TRUE;
   4969 }
   4970 
   4971 static PyObject *
   4972 pyhttp_populate_multi(struct pyhttp_request *pyreq, PyObject *args)
   4973 {
   4974 	http_populate_multipart_form(pyreq->req);
   4975 	Py_RETURN_TRUE;
   4976 }
   4977 
   4978 static PyObject *
   4979 pyhttp_populate_cookies(struct pyhttp_request *pyreq, PyObject *args)
   4980 {
   4981 	http_populate_cookies(pyreq->req);
   4982 	Py_RETURN_TRUE;
   4983 }
   4984 
   4985 static PyObject *
   4986 pyhttp_argument(struct pyhttp_request *pyreq, PyObject *args)
   4987 {
   4988 	const char	*name;
   4989 	PyObject	*value;
   4990 	char		*string;
   4991 
   4992 	if (!PyArg_ParseTuple(args, "s", &name))
   4993 		return (NULL);
   4994 
   4995 	if (!http_argument_get_string(pyreq->req, name, &string)) {
   4996 		Py_RETURN_NONE;
   4997 	}
   4998 
   4999 	if ((value = PyUnicode_FromString(string)) == NULL)
   5000 		return (PyErr_NoMemory());
   5001 
   5002 	return (value);
   5003 }
   5004 
   5005 static PyObject *
   5006 pyhttp_cookie(struct pyhttp_request *pyreq, PyObject *args)
   5007 {
   5008 	const char	*name;
   5009 	PyObject	*value;
   5010 	char		*string;
   5011 
   5012 	if (!PyArg_ParseTuple(args, "s", &name))
   5013 		return (NULL);
   5014 
   5015 	if (!http_request_cookie(pyreq->req, name, &string)) {
   5016 		Py_RETURN_NONE;
   5017 	}
   5018 
   5019 	if ((value = PyUnicode_FromString(string)) == NULL)
   5020 		return (NULL);
   5021 
   5022 	return (value);
   5023 }
   5024 
   5025 static PyObject *
   5026 pyhttp_file_lookup(struct pyhttp_request *pyreq, PyObject *args)
   5027 {
   5028 	const char		*name;
   5029 	struct http_file	*file;
   5030 	PyObject		*pyfile;
   5031 
   5032 	if (!PyArg_ParseTuple(args, "s", &name))
   5033 		return (NULL);
   5034 
   5035 	if ((file = http_file_lookup(pyreq->req, name)) == NULL) {
   5036 		Py_RETURN_NONE;
   5037 	}
   5038 
   5039 	if ((pyfile = pyhttp_file_alloc(file)) == NULL)
   5040 		return (PyErr_NoMemory());
   5041 
   5042 	return (pyfile);
   5043 }
   5044 
   5045 static PyObject *
   5046 pyhttp_file_read(struct pyhttp_file *pyfile, PyObject *args)
   5047 {
   5048 	ssize_t			ret;
   5049 	size_t			len;
   5050 	Py_ssize_t		pylen;
   5051 	PyObject		*result;
   5052 	u_int8_t		buf[1024];
   5053 
   5054 	if (!PyArg_ParseTuple(args, "n", &pylen) || pylen < 0)
   5055 		return (NULL);
   5056 
   5057 	len = (size_t)pylen;
   5058 	if (len > sizeof(buf)) {
   5059 		PyErr_SetString(PyExc_RuntimeError, "len > sizeof(buf)");
   5060 		return (NULL);
   5061 	}
   5062 
   5063 	ret = http_file_read(pyfile->file, buf, len);
   5064 	if (ret == -1) {
   5065 		PyErr_SetString(PyExc_RuntimeError, "http_file_read() failed");
   5066 		return (NULL);
   5067 	}
   5068 
   5069 	result = Py_BuildValue("ny#", ret, buf, ret);
   5070 	if (result == NULL)
   5071 		return (PyErr_NoMemory());
   5072 
   5073 	return (result);
   5074 }
   5075 
   5076 static PyObject *
   5077 pyhttp_websocket_handshake(struct pyhttp_request *pyreq, PyObject *args)
   5078 {
   5079 	struct connection	*c;
   5080 	PyObject		*onconnect, *onmsg, *ondisconnect;
   5081 
   5082 	if (!PyArg_ParseTuple(args, "OOO", &onconnect, &onmsg, &ondisconnect))
   5083 		return (NULL);
   5084 
   5085 	kore_websocket_handshake(pyreq->req, NULL, NULL, NULL);
   5086 
   5087 	c = pyreq->req->owner;
   5088 
   5089 	Py_INCREF(onconnect);
   5090 	Py_INCREF(onmsg);
   5091 	Py_INCREF(ondisconnect);
   5092 
   5093 	c->ws_connect = kore_calloc(1, sizeof(struct kore_runtime_call));
   5094 	c->ws_connect->addr = onconnect;
   5095 	c->ws_connect->runtime = &kore_python_runtime;
   5096 
   5097 	c->ws_message = kore_calloc(1, sizeof(struct kore_runtime_call));
   5098 	c->ws_message->addr = onmsg;
   5099 	c->ws_message->runtime = &kore_python_runtime;
   5100 
   5101 	c->ws_disconnect = kore_calloc(1, sizeof(struct kore_runtime_call));
   5102 	c->ws_disconnect->addr = ondisconnect;
   5103 	c->ws_disconnect->runtime = &kore_python_runtime;
   5104 
   5105 	python_runtime_connect(onconnect, c);
   5106 
   5107 	Py_RETURN_TRUE;
   5108 }
   5109 
   5110 static PyObject *
   5111 pyconnection_websocket_send(struct pyconnection *pyc, PyObject *args)
   5112 {
   5113 	int		op;
   5114 	ssize_t		len;
   5115 	const char	*data;
   5116 
   5117 	if (pyc->c->proto != CONN_PROTO_WEBSOCKET) {
   5118 		PyErr_SetString(PyExc_TypeError, "not a websocket connection");
   5119 		return (NULL);
   5120 	}
   5121 
   5122 	len = -1;
   5123 
   5124 	if (!PyArg_ParseTuple(args, "iy#", &op, &data, &len))
   5125 		return (NULL);
   5126 
   5127 	if (len < 0) {
   5128 		PyErr_SetString(PyExc_TypeError, "invalid length");
   5129 		return (NULL);
   5130 	}
   5131 
   5132 	switch (op) {
   5133 	case WEBSOCKET_OP_TEXT:
   5134 	case WEBSOCKET_OP_BINARY:
   5135 		break;
   5136 	default:
   5137 		PyErr_SetString(PyExc_TypeError, "invalid op parameter");
   5138 		return (NULL);
   5139 	}
   5140 
   5141 	kore_websocket_send(pyc->c, op, data, len);
   5142 
   5143 	Py_RETURN_TRUE;
   5144 }
   5145 
   5146 static PyObject *
   5147 python_websocket_broadcast(PyObject *self, PyObject *args)
   5148 {
   5149 	struct connection	*c;
   5150 	ssize_t			len;
   5151 	struct pyconnection	*pyc;
   5152 	const char		*data;
   5153 	PyObject		*pysrc;
   5154 	int			op, broadcast;
   5155 
   5156 	len = -1;
   5157 
   5158 	if (!PyArg_ParseTuple(args, "Oiy#i", &pysrc, &op, &data, &len,
   5159 	    &broadcast))
   5160 		return (NULL);
   5161 
   5162 	if (len < 0) {
   5163 		PyErr_SetString(PyExc_TypeError, "invalid length");
   5164 		return (NULL);
   5165 	}
   5166 
   5167 	switch (op) {
   5168 	case WEBSOCKET_OP_TEXT:
   5169 	case WEBSOCKET_OP_BINARY:
   5170 		break;
   5171 	default:
   5172 		PyErr_SetString(PyExc_TypeError, "invalid op parameter");
   5173 		return (NULL);
   5174 	}
   5175 
   5176 	if (pysrc == Py_None) {
   5177 		c = NULL;
   5178 	} else {
   5179 		if (!PyObject_TypeCheck(pysrc, &pyconnection_type))
   5180 			return (NULL);
   5181 		pyc = (struct pyconnection *)pysrc;
   5182 		c = pyc->c;
   5183 	}
   5184 
   5185 	kore_websocket_broadcast(c, op, data, len, broadcast);
   5186 
   5187 	Py_RETURN_TRUE;
   5188 }
   5189 
   5190 static PyObject *
   5191 pyhttp_get_host(struct pyhttp_request *pyreq, void *closure)
   5192 {
   5193 	PyObject	*host;
   5194 
   5195 	if ((host = PyUnicode_FromString(pyreq->req->host)) == NULL)
   5196 		return (PyErr_NoMemory());
   5197 
   5198 	return (host);
   5199 }
   5200 
   5201 static PyObject *
   5202 pyhttp_get_path(struct pyhttp_request *pyreq, void *closure)
   5203 {
   5204 	PyObject	*path;
   5205 
   5206 	if ((path = PyUnicode_FromString(pyreq->req->path)) == NULL)
   5207 		return (PyErr_NoMemory());
   5208 
   5209 	return (path);
   5210 }
   5211 
   5212 static PyObject *
   5213 pyhttp_get_body(struct pyhttp_request *pyreq, void *closure)
   5214 {
   5215 	ssize_t			ret;
   5216 	struct kore_buf		buf;
   5217 	PyObject		*body;
   5218 	u_int8_t		data[BUFSIZ];
   5219 
   5220 	kore_buf_init(&buf, 1024);
   5221 	if (!http_body_rewind(pyreq->req)) {
   5222 		PyErr_SetString(PyExc_RuntimeError,
   5223 		    "http_body_rewind() failed");
   5224 		return (NULL);
   5225 	}
   5226 
   5227 	for (;;) {
   5228 		ret = http_body_read(pyreq->req, data, sizeof(data));
   5229 		if (ret == -1) {
   5230 			kore_buf_cleanup(&buf);
   5231 			PyErr_SetString(PyExc_RuntimeError,
   5232 			    "http_body_read() failed");
   5233 			return (NULL);
   5234 		}
   5235 
   5236 		if (ret == 0)
   5237 			break;
   5238 
   5239 		kore_buf_append(&buf, data, (size_t)ret);
   5240 	}
   5241 
   5242 	body = PyBytes_FromStringAndSize((char *)buf.data, buf.offset);
   5243 	kore_buf_free(&buf);
   5244 
   5245 	if (body == NULL)
   5246 		return (PyErr_NoMemory());
   5247 
   5248 	return (body);
   5249 }
   5250 
   5251 static PyObject *
   5252 pyhttp_get_agent(struct pyhttp_request *pyreq, void *closure)
   5253 {
   5254 	return (PyUnicode_FromString(pyreq->req->path));
   5255 }
   5256 
   5257 static PyObject *
   5258 pyhttp_get_method(struct pyhttp_request *pyreq, void *closure)
   5259 {
   5260 	return (PyLong_FromUnsignedLong(pyreq->req->method));
   5261 }
   5262 
   5263 static PyObject *
   5264 pyhttp_get_protocol(struct pyhttp_request *pyreq, void *closure)
   5265 {
   5266 	struct connection	*c;
   5267 	const char		*proto;
   5268 
   5269 	c = pyreq->req->owner;
   5270 
   5271 	if (c->owner->server->tls)
   5272 		proto = "https";
   5273 	else
   5274 		proto = "http";
   5275 
   5276 	return (PyUnicode_FromString(proto));
   5277 }
   5278 
   5279 static PyObject *
   5280 pyhttp_get_body_path(struct pyhttp_request *pyreq, void *closure)
   5281 {
   5282 	if (pyreq->req->http_body_path == NULL) {
   5283 		Py_RETURN_NONE;
   5284 	}
   5285 
   5286 	return (PyUnicode_FromString(pyreq->req->http_body_path));
   5287 }
   5288 
   5289 static PyObject *
   5290 pyhttp_get_body_digest(struct pyhttp_request *pyreq, void *closure)
   5291 {
   5292 	PyObject	*digest;
   5293 
   5294 	digest = PyBytes_FromStringAndSize((char *)pyreq->req->http_body_digest,
   5295 	    sizeof(pyreq->req->http_body_digest));
   5296 
   5297 	return (digest);
   5298 }
   5299 
   5300 static PyObject *
   5301 pyhttp_get_connection(struct pyhttp_request *pyreq, void *closure)
   5302 {
   5303 	PyObject	*pyc;
   5304 
   5305 	if (pyreq->req->owner == NULL) {
   5306 		Py_RETURN_NONE;
   5307 	}
   5308 
   5309 	if ((pyc = pyconnection_alloc(pyreq->req->owner)) == NULL)
   5310 		return (PyErr_NoMemory());
   5311 
   5312 	return (pyc);
   5313 }
   5314 
   5315 static PyObject *
   5316 pyhttp_file_get_name(struct pyhttp_file *pyfile, void *closure)
   5317 {
   5318 	PyObject	*name;
   5319 
   5320 	if ((name = PyUnicode_FromString(pyfile->file->name)) == NULL)
   5321 		return (PyErr_NoMemory());
   5322 
   5323 	return (name);
   5324 }
   5325 
   5326 static PyObject *
   5327 pyhttp_file_get_filename(struct pyhttp_file *pyfile, void *closure)
   5328 {
   5329 	PyObject	*name;
   5330 
   5331 	if ((name = PyUnicode_FromString(pyfile->file->filename)) == NULL)
   5332 		return (PyErr_NoMemory());
   5333 
   5334 	return (name);
   5335 }
   5336 
   5337 void
   5338 pyroute_dealloc(struct pyroute *route)
   5339 {
   5340 	kore_free(route->path);
   5341 
   5342 	Py_XDECREF(route->func);
   5343 	Py_XDECREF(route->kwargs);
   5344 
   5345 	PyObject_Del((PyObject *)route);
   5346 }
   5347 
   5348 static PyObject *
   5349 pyroute_inner(struct pyroute *route, PyObject *args)
   5350 {
   5351 	PyObject	*obj;
   5352 
   5353 	if (!PyArg_ParseTuple(args, "O", &obj))
   5354 		return (NULL);
   5355 
   5356 	if (!PyCallable_Check(obj))
   5357 		return (NULL);
   5358 
   5359 	route->func = obj;
   5360 	Py_INCREF(route->func);
   5361 
   5362 	TAILQ_INSERT_TAIL(&routes, route, list);
   5363 
   5364 	return (route->func);
   5365 }
   5366 
   5367 void
   5368 pydomain_dealloc(struct pydomain *domain)
   5369 {
   5370 	PyObject_Del((PyObject *)domain);
   5371 }
   5372 
   5373 static int
   5374 pydomain_set_accesslog(struct pydomain *domain, PyObject *arg, void *closure)
   5375 {
   5376 	const char		*path;
   5377 
   5378 	if (!PyUnicode_CheckExact(arg))
   5379 		return (-1);
   5380 
   5381 	if (domain->config->accesslog != -1) {
   5382 		PyErr_Format(PyExc_RuntimeError,
   5383 		    "domain %s accesslog already set", domain->config->domain);
   5384 		return (-1);
   5385 	}
   5386 
   5387 	path = PyUnicode_AsUTF8(arg);
   5388 
   5389 	domain->config->accesslog = open(path,
   5390 	    O_CREAT | O_APPEND | O_WRONLY,
   5391 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
   5392 
   5393 	if (domain->config->accesslog == -1) {
   5394 		PyErr_Format(PyExc_RuntimeError,
   5395 		    "failed to open accesslog for %s (%s:%s)",
   5396 		    domain->config->domain, path, errno_s);
   5397 		return (-1);
   5398 	}
   5399 
   5400 	return (0);
   5401 }
   5402 
   5403 static PyObject *
   5404 pydomain_filemaps(struct pydomain *domain, PyObject *args)
   5405 {
   5406 	Py_ssize_t		idx;
   5407 	struct kore_route	*rt;
   5408 	const char		*url, *path;
   5409 	PyObject		*dict, *key, *value, *auth;
   5410 
   5411 	if (!PyArg_ParseTuple(args, "O", &dict))
   5412 		return (NULL);
   5413 
   5414 	if (!PyDict_CheckExact(dict)) {
   5415 		PyErr_SetString(PyExc_RuntimeError, "filemaps not a dict");
   5416 		return (NULL);
   5417 	}
   5418 
   5419 	idx = 0;
   5420 	while (PyDict_Next(dict, &idx, &key, &value)) {
   5421 		if (!PyUnicode_CheckExact(key)) {
   5422 			PyErr_SetString(PyExc_RuntimeError,
   5423 			    "filemap key not a string");
   5424 			return (NULL);
   5425 		}
   5426 
   5427 		url = PyUnicode_AsUTF8(key);
   5428 
   5429 		if (!PyUnicode_CheckExact(value) &&
   5430 		    !PyTuple_CheckExact(value)) {
   5431 			PyErr_SetString(PyExc_RuntimeError,
   5432 			    "filemap value can be either be a string or tuple");
   5433 			return (NULL);
   5434 		}
   5435 
   5436 		if (PyTuple_CheckExact(value)) {
   5437 			auth = PyTuple_GetItem(value, 1);
   5438 			if (!PyDict_CheckExact(auth)) {
   5439 				PyErr_SetString(PyExc_RuntimeError,
   5440 				    "filemap value tuple auth is not a dict");
   5441 				return (NULL);
   5442 			}
   5443 
   5444 			value = PyTuple_GetItem(value, 0);
   5445 			if (!PyUnicode_CheckExact(value)) {
   5446 				PyErr_SetString(PyExc_RuntimeError,
   5447 				    "filemap value tuple path is invalid");
   5448 				return (NULL);
   5449 			}
   5450 		} else {
   5451 			auth = NULL;
   5452 		}
   5453 
   5454 		path = PyUnicode_AsUTF8(value);
   5455 
   5456 		rt = kore_filemap_create(domain->config, path, url, NULL);
   5457 		if (rt == NULL) {
   5458 			PyErr_Format(PyExc_RuntimeError,
   5459 			    "failed to create filemap %s->%s for %s",
   5460 			    url, path, domain->config->domain);
   5461 			return (NULL);
   5462 		}
   5463 
   5464 		if (auth != NULL) {
   5465 			if (!python_route_auth(auth, rt)) {
   5466 				kore_python_log_error("python_route_auth");
   5467 				kore_route_free(rt);
   5468 				return (KORE_RESULT_ERROR);
   5469 			}
   5470 		}
   5471 	}
   5472 
   5473 	Py_RETURN_NONE;
   5474 }
   5475 
   5476 static PyObject *
   5477 pydomain_route(struct pydomain *domain, PyObject *args, PyObject *kwargs)
   5478 {
   5479 	PyObject		*obj;
   5480 	const char		*path;
   5481 	struct pyroute		*route;
   5482 
   5483 	if (!PyArg_ParseTuple(args, "sO", &path, &obj))
   5484 		return (NULL);
   5485 
   5486 	if (!PyCallable_Check(obj))
   5487 		return (NULL);
   5488 
   5489 	if ((route = PyObject_New(struct pyroute, &pyroute_type)) == NULL)
   5490 		return (NULL);
   5491 
   5492 	route->kwargs = kwargs;
   5493 	route->domain = domain->config;
   5494 	route->path = kore_strdup(path);
   5495 
   5496 	Py_XINCREF(route->kwargs);
   5497 
   5498 	route->func = obj;
   5499 	Py_INCREF(route->func);
   5500 
   5501 	TAILQ_INSERT_TAIL(&routes, route, list);
   5502 
   5503 	Py_RETURN_NONE;
   5504 }
   5505 
   5506 static int
   5507 python_route_install(struct pyroute *route)
   5508 {
   5509 	const char			*val;
   5510 	struct kore_domain		*domain;
   5511 	struct kore_route		*rt, *entry;
   5512 	PyObject			*kwargs, *repr, *obj;
   5513 
   5514 	if ((repr = PyObject_Repr(route->func)) == NULL) {
   5515 		kore_python_log_error("python_route_install");
   5516 		return (KORE_RESULT_ERROR);
   5517 	}
   5518 
   5519 	domain = python_route_domain_resolve(route);
   5520 
   5521 	rt = kore_calloc(1, sizeof(*rt));
   5522 	rt->dom = domain;
   5523 	rt->methods = HTTP_METHOD_ALL;
   5524 	rt->path = kore_strdup(route->path);
   5525 
   5526 	TAILQ_INIT(&rt->params);
   5527 
   5528 	val = PyUnicode_AsUTF8(repr);
   5529 	rt->func = kore_strdup(val);
   5530 
   5531 	kwargs = route->kwargs;
   5532 
   5533 	rt->rcall = kore_calloc(1, sizeof(struct kore_runtime_call));
   5534 	rt->rcall->addr = route->func;
   5535 	rt->rcall->runtime = &kore_python_runtime;
   5536 	Py_INCREF(rt->rcall->addr);
   5537 
   5538 	if (kwargs != NULL) {
   5539 		if ((obj = PyDict_GetItemString(kwargs, "methods")) != NULL) {
   5540 			if (!python_route_methods(obj, kwargs, rt)) {
   5541 				kore_python_log_error("python_route_install");
   5542 				kore_route_free(rt);
   5543 				return (KORE_RESULT_ERROR);
   5544 			}
   5545 		}
   5546 
   5547 		if ((obj = PyDict_GetItemString(kwargs, "auth")) != NULL) {
   5548 			if (!python_route_auth(obj, rt)) {
   5549 				kore_python_log_error("python_route_install");
   5550 				kore_route_free(rt);
   5551 				return (KORE_RESULT_ERROR);
   5552 			}
   5553 		}
   5554 
   5555 		if ((obj = PyDict_GetItemString(kwargs, "hooks")) != NULL) {
   5556 			if (!python_route_hooks(obj, rt)) {
   5557 				kore_python_log_error("python_route_install");
   5558 				kore_route_free(rt);
   5559 				return (KORE_RESULT_ERROR);
   5560 			}
   5561 		}
   5562 	}
   5563 
   5564 	if (rt->path[0] == '/') {
   5565 		rt->type = HANDLER_TYPE_STATIC;
   5566 	} else {
   5567 		rt->type = HANDLER_TYPE_DYNAMIC;
   5568 		if (regcomp(&rt->rctx, rt->path, REG_EXTENDED))
   5569 			fatal("failed to compile regex for '%s'", rt->path);
   5570 	}
   5571 
   5572 	TAILQ_FOREACH(entry, &domain->routes, list) {
   5573 		if (!strcmp(entry->path, rt->path) &&
   5574 		    (entry->methods & rt->methods))
   5575 			fatal("duplicate route for '%s'", route->path);
   5576 	}
   5577 
   5578 	TAILQ_INSERT_TAIL(&domain->routes, rt, list);
   5579 
   5580 	return (KORE_RESULT_OK);
   5581 }
   5582 
   5583 static struct kore_domain *
   5584 python_route_domain_resolve(struct pyroute *route)
   5585 {
   5586 	struct kore_server	*srv;
   5587 	const char		*name;
   5588 	struct kore_domain	*domain;
   5589 
   5590 	if (route->domain != NULL)
   5591 		return (route->domain);
   5592 
   5593 	if (route->kwargs != NULL)
   5594 		name = python_string_from_dict(route->kwargs, "domain");
   5595 	else
   5596 		name = NULL;
   5597 
   5598 	if (name != NULL) {
   5599 		domain = NULL;
   5600 		LIST_FOREACH(srv, &kore_servers, list) {
   5601 			TAILQ_FOREACH(domain, &srv->domains, list) {
   5602 				if (!strcmp(domain->domain, name))
   5603 					break;
   5604 			}
   5605 		}
   5606 
   5607 		if (domain == NULL)
   5608 			fatal("domain '%s' does not exist", name);
   5609 	} else {
   5610 		if ((domain = kore_domain_byid(1)) != NULL)
   5611 			fatal("ambiguous domain on route, please specify one");
   5612 		if ((domain = kore_domain_byid(0)) == NULL)
   5613 			fatal("no domains configured, please configure one");
   5614 	}
   5615 
   5616 	return (domain);
   5617 }
   5618 
   5619 static int
   5620 python_route_methods(PyObject *obj, PyObject *kwargs, struct kore_route *rt)
   5621 {
   5622 	const char		*val;
   5623 	PyObject		*item;
   5624 	int			method;
   5625 	Py_ssize_t		list_len, idx;
   5626 
   5627 	if (!PyList_CheckExact(obj)) {
   5628 		PyErr_SetString(PyExc_RuntimeError, "methods not a list");
   5629 		return (KORE_RESULT_ERROR);
   5630 	}
   5631 
   5632 	rt->methods = 0;
   5633 	list_len = PyList_Size(obj);
   5634 
   5635 	for (idx = 0; idx < list_len; idx++) {
   5636 		if ((item = PyList_GetItem(obj, idx)) == NULL)
   5637 			return (KORE_RESULT_ERROR);
   5638 
   5639 		if ((val = PyUnicode_AsUTF8(item)) == NULL)
   5640 			return (KORE_RESULT_ERROR);
   5641 
   5642 		if ((method = http_method_value(val)) == 0) {
   5643 			PyErr_Format(PyExc_RuntimeError,
   5644 			    "unknown HTTP method: %s", val);
   5645 			return (KORE_RESULT_ERROR);
   5646 		}
   5647 
   5648 		rt->methods |= method;
   5649 		if (method == HTTP_METHOD_GET)
   5650 			rt->methods |= HTTP_METHOD_HEAD;
   5651 
   5652 		if (!python_route_params(kwargs, rt, val, method, 0))
   5653 			return (KORE_RESULT_ERROR);
   5654 
   5655 		if (!python_route_params(kwargs, rt, "qs", method, 1))
   5656 			return (KORE_RESULT_ERROR);
   5657 	}
   5658 
   5659 	return (KORE_RESULT_OK);
   5660 }
   5661 
   5662 static int
   5663 python_route_params(PyObject *kwargs, struct kore_route *rt,
   5664     const char *method, int type, int qs)
   5665 {
   5666 	Py_ssize_t			idx;
   5667 	const char			*val;
   5668 	int				vtype;
   5669 	struct kore_validator		*vldr;
   5670 	struct kore_route_params	*param;
   5671 	PyObject			*obj, *key, *item;
   5672 
   5673 	if ((obj = PyDict_GetItemString(kwargs, method)) == NULL)
   5674 		return (KORE_RESULT_OK);
   5675 
   5676 	if (!PyDict_CheckExact(obj))
   5677 		return (KORE_RESULT_ERROR);
   5678 
   5679 	idx = 0;
   5680 	while (PyDict_Next(obj, &idx, &key, &item)) {
   5681 		if (!PyUnicode_CheckExact(key))
   5682 			return (KORE_RESULT_ERROR);
   5683 
   5684 		val = PyUnicode_AsUTF8(key);
   5685 
   5686 		if (PyUnicode_CheckExact(item)) {
   5687 			vtype = KORE_VALIDATOR_TYPE_REGEX;
   5688 		} else if (PyCallable_Check(item)) {
   5689 			vtype = KORE_VALIDATOR_TYPE_FUNCTION;
   5690 		} else {
   5691 			PyErr_Format(PyExc_RuntimeError,
   5692 			    "validator '%s' must be regex or function", val);
   5693 			return (KORE_RESULT_ERROR);
   5694 		}
   5695 
   5696 		vldr = kore_calloc(1, sizeof(*vldr));
   5697 		vldr->type = vtype;
   5698 
   5699 		if (vtype == KORE_VALIDATOR_TYPE_REGEX) {
   5700 			val = PyUnicode_AsUTF8(item);
   5701 			if (regcomp(&(vldr->rctx),
   5702 			    val, REG_EXTENDED | REG_NOSUB)) {
   5703 				PyErr_Format(PyExc_RuntimeError,
   5704 				    "Invalid regex (%s)", val);
   5705 				kore_free(vldr);
   5706 				return (KORE_RESULT_ERROR);
   5707 			}
   5708 		} else {
   5709 			vldr->rcall = kore_calloc(1, sizeof(*vldr->rcall));
   5710 			vldr->rcall->addr = item;
   5711 			vldr->rcall->runtime = &kore_python_runtime;
   5712 			Py_INCREF(item);
   5713 		}
   5714 
   5715 		val = PyUnicode_AsUTF8(key);
   5716 		vldr->name = kore_strdup(val);
   5717 
   5718 		param = kore_calloc(1, sizeof(*param));
   5719 		param->flags = 0;
   5720 		param->method = type;
   5721 		param->validator = vldr;
   5722 		param->name = kore_strdup(val);
   5723 
   5724 		if (type == HTTP_METHOD_GET || qs == 1)
   5725 			param->flags = KORE_PARAMS_QUERY_STRING;
   5726 
   5727 		TAILQ_INSERT_TAIL(&rt->params, param, list);
   5728 	}
   5729 
   5730 	return (KORE_RESULT_OK);
   5731 }
   5732 
   5733 static int
   5734 python_route_auth(PyObject *dict, struct kore_route *rt)
   5735 {
   5736 	int			type;
   5737 	struct kore_auth	*auth;
   5738 	struct kore_validator	*vldr;
   5739 	PyObject		*obj, *repr;
   5740 	const char		*value, *redir;
   5741 
   5742 	if (!PyDict_CheckExact(dict))
   5743 		return (KORE_RESULT_ERROR);
   5744 
   5745 	if ((value = python_string_from_dict(dict, "type")) == NULL) {
   5746 		PyErr_SetString(PyExc_RuntimeError,
   5747 		    "missing or invalid 'type' keyword");
   5748 		return (KORE_RESULT_ERROR);
   5749 	}
   5750 
   5751 	if (!strcmp(value, "cookie")) {
   5752 		type = KORE_AUTH_TYPE_COOKIE;
   5753 	} else if (!strcmp(value, "header")) {
   5754 		type = KORE_AUTH_TYPE_HEADER;
   5755 	} else {
   5756 		PyErr_Format(PyExc_RuntimeError,
   5757 		    "invalid 'type' (%s) in auth dictionary for '%s'",
   5758 		    value, rt->path);
   5759 		return (KORE_RESULT_ERROR);
   5760 	}
   5761 
   5762 	if ((value = python_string_from_dict(dict, "value")) == NULL) {
   5763 		PyErr_SetString(PyExc_RuntimeError,
   5764 		    "missing or invalid 'value' keyword");
   5765 		return (KORE_RESULT_ERROR);
   5766 	}
   5767 
   5768 	redir = python_string_from_dict(dict, "redirect");
   5769 
   5770 	if ((obj = PyDict_GetItemString(dict, "verify")) == NULL ||
   5771 	    !PyCallable_Check(obj)) {
   5772 		PyErr_Format(PyExc_RuntimeError,
   5773 		    "missing 'verify' in auth dictionary for '%s'", rt->path);
   5774 		return (KORE_RESULT_ERROR);
   5775 	}
   5776 
   5777 	auth = kore_calloc(1, sizeof(*auth));
   5778 	auth->type = type;
   5779 	auth->value = kore_strdup(value);
   5780 
   5781 	if (redir != NULL)
   5782 		auth->redirect = kore_strdup(redir);
   5783 
   5784 	vldr = kore_calloc(1, sizeof(*vldr));
   5785 	vldr->type = KORE_VALIDATOR_TYPE_FUNCTION;
   5786 
   5787 	vldr->rcall = kore_calloc(1, sizeof(*vldr->rcall));
   5788 	vldr->rcall->addr = obj;
   5789 	vldr->rcall->runtime = &kore_python_runtime;
   5790 	Py_INCREF(obj);
   5791 
   5792 	if ((repr = PyObject_Repr(obj)) == NULL) {
   5793 		kore_free(vldr->rcall);
   5794 		kore_free(vldr);
   5795 		kore_free(auth);
   5796 		return (KORE_RESULT_ERROR);
   5797 	}
   5798 
   5799 	value = PyUnicode_AsUTF8(repr);
   5800 	vldr->name = kore_strdup(value);
   5801 	Py_DECREF(repr);
   5802 
   5803 	auth->validator = vldr;
   5804 	rt->auth = auth;
   5805 
   5806 	return (KORE_RESULT_OK);
   5807 }
   5808 
   5809 static int
   5810 python_route_hooks(PyObject *dict, struct kore_route *rt)
   5811 {
   5812 	if (!PyDict_CheckExact(dict))
   5813 		return (KORE_RESULT_ERROR);
   5814 
   5815 	if (!python_route_hook_set(dict, "on_free", &rt->on_free))
   5816 		return (KORE_RESULT_ERROR);
   5817 
   5818 	if (!python_route_hook_set(dict, "on_headers", &rt->on_headers))
   5819 		return (KORE_RESULT_ERROR);
   5820 
   5821 	if (!python_route_hook_set(dict, "on_body_chunk", &rt->on_body_chunk))
   5822 		return (KORE_RESULT_ERROR);
   5823 
   5824 	return (KORE_RESULT_OK);
   5825 }
   5826 
   5827 static int
   5828 python_route_hook_set(PyObject *dict, const char *name,
   5829     struct kore_runtime_call **out)
   5830 {
   5831 	PyObject			*obj;
   5832 	struct kore_runtime_call	*rcall;
   5833 
   5834 	if ((obj = PyDict_GetItemString(dict, name)) == NULL)
   5835 		return (KORE_RESULT_OK);
   5836 
   5837 	if (!PyCallable_Check(obj)) {
   5838 		PyErr_Format(PyExc_RuntimeError,
   5839 		    "%s for a route not callable", name);
   5840 		Py_DECREF(obj);
   5841 		return (KORE_RESULT_ERROR);
   5842 	}
   5843 
   5844 	rcall = kore_calloc(1, sizeof(struct kore_runtime_call));
   5845 	rcall->addr = obj;
   5846 	rcall->runtime = &kore_python_runtime;
   5847 
   5848 	Py_INCREF(rcall->addr);
   5849 	*out = rcall;
   5850 
   5851 	return (KORE_RESULT_OK);
   5852 }
   5853 
   5854 #if defined(KORE_USE_PGSQL)
   5855 static PyObject *
   5856 python_kore_pgsql_query(PyObject *self, PyObject *args, PyObject *kwargs)
   5857 {
   5858 	struct pykore_pgsql	*op;
   5859 	PyObject		*obj;
   5860 	const char		*db, *query;
   5861 
   5862 	if (!PyArg_ParseTuple(args, "ss", &db, &query))
   5863 		return (NULL);
   5864 
   5865 	op = PyObject_New(struct pykore_pgsql, &pykore_pgsql_type);
   5866 	if (op == NULL)
   5867 		return (NULL);
   5868 
   5869 	op->binary = 0;
   5870 	op->param.count = 0;
   5871 	op->param.objs = NULL;
   5872 	op->param.values = NULL;
   5873 	op->param.lengths = NULL;
   5874 	op->param.formats = NULL;
   5875 
   5876 	op->result = NULL;
   5877 	op->coro = coro_running;
   5878 	op->db = kore_strdup(db);
   5879 	op->query = kore_strdup(query);
   5880 	op->state = PYKORE_PGSQL_PREINIT;
   5881 
   5882 	memset(&op->sql, 0, sizeof(op->sql));
   5883 
   5884 	if (kwargs != NULL) {
   5885 		if ((obj = PyDict_GetItemString(kwargs, "params")) != NULL) {
   5886 			if (!pykore_pgsql_params(op, obj)) {
   5887 				Py_DECREF((PyObject *)op);
   5888 				return (NULL);
   5889 			}
   5890 		}
   5891 
   5892 		if ((obj = PyDict_GetItemString(kwargs, "binary")) != NULL) {
   5893 			if (obj == Py_True) {
   5894 				op->binary = 1;
   5895 			} else if (obj == Py_False) {
   5896 				op->binary = 0;
   5897 			} else {
   5898 				Py_DECREF((PyObject *)op);
   5899 				PyErr_SetString(PyExc_RuntimeError,
   5900 				    "pgsql: binary not True or False");
   5901 				return (NULL);
   5902 			}
   5903 		}
   5904 	}
   5905 
   5906 	return ((PyObject *)op);
   5907 }
   5908 
   5909 static int
   5910 pykore_pgsql_params(struct pykore_pgsql *op, PyObject *list)
   5911 {
   5912 	union { const char *cp; char *p; }	ptr;
   5913 	PyObject				*item;
   5914 	int					format;
   5915 	Py_ssize_t				i, len, vlen;
   5916 
   5917 	if (!PyList_CheckExact(list)) {
   5918 		if (list == Py_None)
   5919 			return (KORE_RESULT_OK);
   5920 
   5921 		PyErr_SetString(PyExc_RuntimeError,
   5922 		    "pgsql: params keyword must be a list");
   5923 		return (KORE_RESULT_ERROR);
   5924 	}
   5925 
   5926 	len = PyList_Size(list);
   5927 	if (len == 0)
   5928 		return (KORE_RESULT_OK);
   5929 
   5930 	if (len > INT_MAX) {
   5931 		PyErr_SetString(PyExc_RuntimeError,
   5932 		    "pgsql: list length too large");
   5933 		return (KORE_RESULT_ERROR);
   5934 	}
   5935 
   5936 	op->param.count = len;
   5937 	op->param.lengths = kore_calloc(len, sizeof(int));
   5938 	op->param.formats = kore_calloc(len, sizeof(int));
   5939 	op->param.values = kore_calloc(len, sizeof(char *));
   5940 	op->param.objs = kore_calloc(len, sizeof(PyObject *));
   5941 
   5942 	for (i = 0; i < len; i++) {
   5943 		if ((item = PyList_GetItem(list, i)) == NULL)
   5944 			return (KORE_RESULT_ERROR);
   5945 
   5946 		if (PyUnicode_CheckExact(item)) {
   5947 			format = 0;
   5948 			ptr.cp = PyUnicode_AsUTF8AndSize(item, &vlen);
   5949 		} else if (PyBytes_CheckExact(item)) {
   5950 			format = 1;
   5951 			if (PyBytes_AsStringAndSize(item, &ptr.p, &vlen) == -1)
   5952 				ptr.p = NULL;
   5953 		} else {
   5954 			PyErr_Format(PyExc_RuntimeError,
   5955 			    "pgsql: item %zu is not a string or bytes", i);
   5956 			return (KORE_RESULT_ERROR);
   5957 		}
   5958 
   5959 		if (ptr.cp == NULL)
   5960 			return (KORE_RESULT_ERROR);
   5961 
   5962 		op->param.lengths[i] = vlen;
   5963 		op->param.values[i] = ptr.cp;
   5964 		op->param.formats[i] = format;
   5965 
   5966 		/* Hold on to it since we are directly referencing its data. */
   5967 		op->param.objs[i] = item;
   5968 		Py_INCREF(item);
   5969 	}
   5970 
   5971 	return (KORE_RESULT_OK);
   5972 }
   5973 
   5974 static void
   5975 pykore_pgsql_dealloc(struct pykore_pgsql *pysql)
   5976 {
   5977 	Py_ssize_t	i;
   5978 
   5979 	kore_free(pysql->db);
   5980 	kore_free(pysql->query);
   5981 	kore_pgsql_cleanup(&pysql->sql);
   5982 
   5983 	if (pysql->result != NULL)
   5984 		Py_DECREF(pysql->result);
   5985 
   5986 	for (i = 0; i < pysql->param.count; i++)
   5987 		Py_XDECREF(pysql->param.objs[i]);
   5988 
   5989 	kore_free(pysql->param.objs);
   5990 	kore_free(pysql->param.values);
   5991 	kore_free(pysql->param.lengths);
   5992 	kore_free(pysql->param.formats);
   5993 
   5994 	PyObject_Del((PyObject *)pysql);
   5995 }
   5996 
   5997 static PyObject *
   5998 pykore_pgsql_iternext(struct pykore_pgsql *pysql)
   5999 {
   6000 	switch (pysql->state) {
   6001 	case PYKORE_PGSQL_PREINIT:
   6002 		kore_pgsql_init(&pysql->sql);
   6003 		kore_pgsql_bind_callback(&pysql->sql,
   6004 		    pykore_pgsql_callback, pysql);
   6005 		pysql->state = PYKORE_PGSQL_INITIALIZE;
   6006 		/* fallthrough */
   6007 	case PYKORE_PGSQL_INITIALIZE:
   6008 		if (!kore_pgsql_setup(&pysql->sql, pysql->db,
   6009 		    KORE_PGSQL_ASYNC)) {
   6010 			if (pysql->sql.state == KORE_PGSQL_STATE_INIT)
   6011 				break;
   6012 			PyErr_Format(PyExc_RuntimeError, "pgsql error: %s",
   6013 			    pysql->sql.error);
   6014 			return (NULL);
   6015 		}
   6016 		/* fallthrough */
   6017 	case PYKORE_PGSQL_QUERY:
   6018 		if (pysql->param.count > 0) {
   6019 			if (!kore_pgsql_query_param_fields(&pysql->sql,
   6020 			    pysql->query, pysql->binary,
   6021 			    pysql->param.count, pysql->param.values,
   6022 			    pysql->param.lengths, pysql->param.formats)) {
   6023 				PyErr_Format(PyExc_RuntimeError,
   6024 				    "pgsql error: %s", pysql->sql.error);
   6025 				return (NULL);
   6026 			}
   6027 		} else {
   6028 			if (!kore_pgsql_query(&pysql->sql, pysql->query)) {
   6029 				PyErr_Format(PyExc_RuntimeError,
   6030 				    "pgsql error: %s", pysql->sql.error);
   6031 				return (NULL);
   6032 			}
   6033 		}
   6034 		pysql->state = PYKORE_PGSQL_WAIT;
   6035 		break;
   6036 wait_again:
   6037 	case PYKORE_PGSQL_WAIT:
   6038 		switch (pysql->sql.state) {
   6039 		case KORE_PGSQL_STATE_WAIT:
   6040 			break;
   6041 		case KORE_PGSQL_STATE_COMPLETE:
   6042 			PyErr_SetNone(PyExc_StopIteration);
   6043 			if (pysql->result != NULL) {
   6044 				PyErr_SetObject(PyExc_StopIteration,
   6045 				    pysql->result);
   6046 				Py_DECREF(pysql->result);
   6047 				pysql->result = NULL;
   6048 			} else {
   6049 				PyErr_SetObject(PyExc_StopIteration, Py_None);
   6050 			}
   6051 			return (NULL);
   6052 		case KORE_PGSQL_STATE_ERROR:
   6053 			PyErr_Format(PyExc_RuntimeError,
   6054 			    "failed to perform query: %s", pysql->sql.error);
   6055 			return (NULL);
   6056 		case KORE_PGSQL_STATE_RESULT:
   6057 			if (!pykore_pgsql_result(pysql))
   6058 				return (NULL);
   6059 			goto wait_again;
   6060 		default:
   6061 			kore_pgsql_continue(&pysql->sql);
   6062 			goto wait_again;
   6063 		}
   6064 		break;
   6065 	default:
   6066 		PyErr_SetString(PyExc_RuntimeError, "bad pykore_pgsql state");
   6067 		return (NULL);
   6068 	}
   6069 
   6070 	/* tell caller to wait. */
   6071 	Py_RETURN_NONE;
   6072 }
   6073 
   6074 static void
   6075 pykore_pgsql_callback(struct kore_pgsql *pgsql, void *arg)
   6076 {
   6077 	struct pykore_pgsql	*op = arg;
   6078 
   6079 	if (op->coro->request != NULL)
   6080 		http_request_wakeup(op->coro->request);
   6081 	else
   6082 		python_coro_wakeup(op->coro);
   6083 }
   6084 
   6085 static PyObject *
   6086 pykore_pgsql_await(PyObject *obj)
   6087 {
   6088 	Py_INCREF(obj);
   6089 	return (obj);
   6090 }
   6091 
   6092 static int
   6093 pykore_pgsql_result(struct pykore_pgsql *pysql)
   6094 {
   6095 	const char	*val;
   6096 	char		key[64];
   6097 	PyObject	*list, *pyrow, *pyval;
   6098 	int		rows, row, field, fields, len;
   6099 
   6100 	if ((list = PyList_New(0)) == NULL) {
   6101 		PyErr_SetNone(PyExc_MemoryError);
   6102 		return (KORE_RESULT_ERROR);
   6103 	}
   6104 
   6105 	rows = kore_pgsql_ntuples(&pysql->sql);
   6106 	fields = kore_pgsql_nfields(&pysql->sql);
   6107 
   6108 	for (row = 0; row < rows; row++) {
   6109 		if ((pyrow = PyDict_New()) == NULL) {
   6110 			Py_DECREF(list);
   6111 			PyErr_SetNone(PyExc_MemoryError);
   6112 			return (KORE_RESULT_ERROR);
   6113 		}
   6114 
   6115 		for (field = 0; field < fields; field++) {
   6116 			val = kore_pgsql_getvalue(&pysql->sql, row, field);
   6117 			len = kore_pgsql_getlength(&pysql->sql, row, field);
   6118 
   6119 			if (kore_pgsql_column_binary(&pysql->sql, field)) {
   6120 				pyval = PyBytes_FromStringAndSize(val, len);
   6121 			} else {
   6122 				pyval = PyUnicode_FromString(val);
   6123 			}
   6124 
   6125 			if (pyval == NULL) {
   6126 				Py_DECREF(pyrow);
   6127 				Py_DECREF(list);
   6128 				PyErr_SetNone(PyExc_MemoryError);
   6129 				return (KORE_RESULT_ERROR);
   6130 			}
   6131 
   6132 			(void)snprintf(key, sizeof(key), "%s",
   6133 			    kore_pgsql_fieldname(&pysql->sql, field));
   6134 
   6135 			if (PyDict_SetItemString(pyrow, key, pyval) == -1) {
   6136 				Py_DECREF(pyval);
   6137 				Py_DECREF(pyrow);
   6138 				Py_DECREF(list);
   6139 				PyErr_SetString(PyExc_RuntimeError,
   6140 				    "failed to add new value to row");
   6141 				return (KORE_RESULT_ERROR);
   6142 			}
   6143 
   6144 			Py_DECREF(pyval);
   6145 		}
   6146 
   6147 		if (PyList_Insert(list, row, pyrow) == -1) {
   6148 			Py_DECREF(pyrow);
   6149 			Py_DECREF(list);
   6150 			PyErr_SetString(PyExc_RuntimeError,
   6151 			    "failed to add new row to list");
   6152 			return (KORE_RESULT_ERROR);
   6153 		}
   6154 
   6155 		Py_DECREF(pyrow);
   6156 	}
   6157 
   6158 	pysql->result = list;
   6159 	kore_pgsql_continue(&pysql->sql);
   6160 
   6161 	return (KORE_RESULT_OK);
   6162 }
   6163 #endif
   6164 
   6165 #if defined(KORE_USE_CURL)
   6166 static PyObject *
   6167 python_curlopt_set(struct pycurl_data *data, long opt, PyObject *value)
   6168 {
   6169 	int		i;
   6170 
   6171 	for (i = 0; py_curlopt[i].name != NULL; i++) {
   6172 		if (py_curlopt[i].value == opt)
   6173 			break;
   6174 	}
   6175 
   6176 	if (py_curlopt[i].name == NULL) {
   6177 		PyErr_Format(PyExc_RuntimeError, "invalid option '%ld'", opt);
   6178 		return (NULL);
   6179 	}
   6180 
   6181 	if (py_curlopt[i].cb == NULL) {
   6182 		PyErr_Format(PyExc_RuntimeError, "option '%s' not implemented",
   6183 		    py_curlopt[i].name);
   6184 		return (NULL);
   6185 	}
   6186 
   6187 	return (py_curlopt[i].cb(data, i, value));
   6188 }
   6189 
   6190 static int
   6191 python_curlopt_from_dict(struct pycurl_data *data, PyObject *dict)
   6192 {
   6193 	long		opt;
   6194 	Py_ssize_t	idx;
   6195 	PyObject	*key, *value, *obj;
   6196 
   6197 	idx = 0;
   6198 
   6199 	if (!PyDict_CheckExact(dict)) {
   6200 		PyErr_SetString(PyExc_RuntimeError,
   6201 		    "curlopt must be a dictionary");
   6202 		return (KORE_RESULT_ERROR);
   6203 	}
   6204 
   6205 	while (PyDict_Next(dict, &idx, &key, &value)) {
   6206 		if (!PyLong_CheckExact(key)) {
   6207 			PyErr_Format(PyExc_RuntimeError,
   6208 			    "invalid key in curlopt keyword");
   6209 			return (KORE_RESULT_ERROR);
   6210 		}
   6211 
   6212 		opt = PyLong_AsLong(key);
   6213 
   6214 		if ((obj = python_curlopt_set(data, opt, value)) == NULL)
   6215 			return (KORE_RESULT_ERROR);
   6216 
   6217 		Py_DECREF(obj);
   6218 	}
   6219 
   6220 	return (KORE_RESULT_OK);
   6221 }
   6222 
   6223 static PyObject *
   6224 python_kore_curl_handle(PyObject *self, PyObject *args)
   6225 {
   6226 	const char		*url;
   6227 	struct pycurl_handle	*handle;
   6228 
   6229 	if (!PyArg_ParseTuple(args, "s", &url))
   6230 		return (NULL);
   6231 
   6232 	handle = PyObject_New(struct pycurl_handle, &pycurl_handle_type);
   6233 	if (handle == NULL)
   6234 		return (NULL);
   6235 
   6236 	handle->url = kore_strdup(url);
   6237 	memset(&handle->data.curl, 0, sizeof(handle->data.curl));
   6238 
   6239 	handle->body = NULL;
   6240 	LIST_INIT(&handle->data.slists);
   6241 
   6242 	if (!kore_curl_init(&handle->data.curl, handle->url, KORE_CURL_ASYNC)) {
   6243 		Py_DECREF((PyObject *)handle);
   6244 		PyErr_SetString(PyExc_RuntimeError, "failed to setup call");
   6245 		return (NULL);
   6246 	}
   6247 
   6248 	return ((PyObject *)handle);
   6249 }
   6250 
   6251 static void
   6252 pycurl_handle_dealloc(struct pycurl_handle *handle)
   6253 {
   6254 	struct pycurl_slist	*psl;
   6255 
   6256 	while ((psl = LIST_FIRST(&handle->data.slists))) {
   6257 		LIST_REMOVE(psl, list);
   6258 		curl_slist_free_all(psl->slist);
   6259 		kore_free(psl);
   6260 	}
   6261 
   6262 	if (handle->body != NULL)
   6263 		kore_buf_free(handle->body);
   6264 
   6265 	kore_free(handle->url);
   6266 	kore_curl_cleanup(&handle->data.curl);
   6267 
   6268 	PyObject_Del((PyObject *)handle);
   6269 }
   6270 
   6271 static PyObject *
   6272 pycurl_handle_setbody(struct pycurl_handle *handle, PyObject *args)
   6273 {
   6274 	PyObject		*obj;
   6275 	char			*ptr;
   6276 	Py_ssize_t		length;
   6277 
   6278 	if (!PyArg_ParseTuple(args, "O", &obj))
   6279 		return (NULL);
   6280 
   6281 	if (handle->body != NULL) {
   6282 		PyErr_SetString(PyExc_RuntimeError,
   6283 		    "curl handle already has body attached");
   6284 		return (NULL);
   6285 	}
   6286 
   6287 	if (!PyBytes_CheckExact(obj)) {
   6288 		PyErr_SetString(PyExc_RuntimeError,
   6289 		    "curl.setbody expects bytes");
   6290 		return (NULL);
   6291 	}
   6292 
   6293 	if (PyBytes_AsStringAndSize(obj, &ptr, &length) == -1)
   6294 		return (NULL);
   6295 
   6296 	if (length < 0) {
   6297 		PyErr_SetString(PyExc_TypeError, "invalid length");
   6298 		return (NULL);
   6299 	}
   6300 
   6301 	handle->body = kore_buf_alloc(length);
   6302 	kore_buf_append(handle->body, ptr, length);
   6303 	kore_buf_reset(handle->body);
   6304 
   6305 	curl_easy_setopt(handle->data.curl.handle,
   6306 	    CURLOPT_READFUNCTION, kore_curl_frombuf);
   6307 	curl_easy_setopt(handle->data.curl.handle,
   6308 	    CURLOPT_READDATA, handle->body);
   6309 
   6310 	curl_easy_setopt(handle->data.curl.handle, CURLOPT_UPLOAD, 1);
   6311 
   6312 	Py_RETURN_TRUE;
   6313 }
   6314 
   6315 static PyObject *
   6316 pycurl_handle_setopt(struct pycurl_handle *handle, PyObject *args)
   6317 {
   6318 	int		opt;
   6319 	PyObject	*value;
   6320 
   6321 	if (!PyArg_ParseTuple(args, "iO", &opt, &value))
   6322 		return (NULL);
   6323 
   6324 	return (python_curlopt_set(&handle->data, opt, value));
   6325 }
   6326 
   6327 static PyObject *
   6328 pycurl_handle_setopt_string(struct pycurl_data *data, int idx, PyObject *obj)
   6329 {
   6330 	const char		*str;
   6331 
   6332 	if (!PyUnicode_Check(obj)) {
   6333 		PyErr_Format(PyExc_RuntimeError,
   6334 		    "option '%s' requires a string as argument",
   6335 		    py_curlopt[idx].name);
   6336 		return (NULL);
   6337 	}
   6338 
   6339 	if ((str = PyUnicode_AsUTF8(obj)) == NULL)
   6340 		return (NULL);
   6341 
   6342 	curl_easy_setopt(data->curl.handle,
   6343 	    CURLOPTTYPE_OBJECTPOINT + py_curlopt[idx].value, str);
   6344 
   6345 	Py_RETURN_TRUE;
   6346 }
   6347 
   6348 static PyObject *
   6349 pycurl_handle_setopt_long(struct pycurl_data *data, int idx, PyObject *obj)
   6350 {
   6351 	long		val;
   6352 
   6353 	if (!PyLong_CheckExact(obj)) {
   6354 		PyErr_Format(PyExc_RuntimeError,
   6355 		    "option '%s' requires a long as argument",
   6356 		    py_curlopt[idx].name);
   6357 		return (NULL);
   6358 	}
   6359 
   6360 	PyErr_Clear();
   6361 	val = PyLong_AsLong(obj);
   6362 	if (val == -1 && PyErr_Occurred())
   6363 		return (NULL);
   6364 
   6365 	curl_easy_setopt(data->curl.handle,
   6366 	    CURLOPTTYPE_LONG + py_curlopt[idx].value, val);
   6367 
   6368 	Py_RETURN_TRUE;
   6369 }
   6370 
   6371 static PyObject *
   6372 pycurl_handle_setopt_slist(struct pycurl_data *data, int idx, PyObject *obj)
   6373 {
   6374 	struct pycurl_slist	*psl;
   6375 	PyObject		*item;
   6376 	const char		*sval;
   6377 	struct curl_slist	*slist;
   6378 	Py_ssize_t		list_len, i;
   6379 
   6380 	if (!PyList_CheckExact(obj)) {
   6381 		PyErr_Format(PyExc_RuntimeError,
   6382 		    "option '%s' requires a list as argument",
   6383 		    py_curlopt[idx].name);
   6384 		return (NULL);
   6385 	}
   6386 
   6387 	slist = NULL;
   6388 	list_len = PyList_Size(obj);
   6389 
   6390 	for (i = 0; i < list_len; i++) {
   6391 		if ((item = PyList_GetItem(obj, i)) == NULL)
   6392 			return (NULL);
   6393 
   6394 		if (!PyUnicode_Check(item))
   6395 			return (NULL);
   6396 
   6397 		if ((sval = PyUnicode_AsUTF8AndSize(item, NULL)) == NULL)
   6398 			return (NULL);
   6399 
   6400 		if ((slist = curl_slist_append(slist, sval)) == NULL)
   6401 			fatal("%s: curl_slist_append failed", __func__);
   6402 	}
   6403 
   6404 	psl = kore_calloc(1, sizeof(*psl));
   6405 	psl->slist = slist;
   6406 	LIST_INSERT_HEAD(&data->slists, psl, list);
   6407 
   6408 	curl_easy_setopt(data->curl.handle,
   6409 	    CURLOPTTYPE_OBJECTPOINT + py_curlopt[idx].value, slist);
   6410 
   6411 	Py_RETURN_TRUE;
   6412 }
   6413 
   6414 static PyObject *
   6415 pycurl_handle_run(struct pycurl_handle *handle, PyObject *args)
   6416 {
   6417 	struct pycurl_handle_op		*op;
   6418 
   6419 	op = PyObject_New(struct pycurl_handle_op, &pycurl_handle_op_type);
   6420 	if (op == NULL)
   6421 		return (NULL);
   6422 
   6423 	Py_INCREF(handle);
   6424 
   6425 	op->handle = handle;
   6426 	op->coro = coro_running;
   6427 	op->state = CURL_CLIENT_OP_RUN;
   6428 
   6429 	kore_curl_bind_callback(&handle->data.curl,
   6430 	    python_curl_handle_callback, op);
   6431 
   6432 	return ((PyObject *)op);
   6433 }
   6434 
   6435 static void
   6436 pycurl_handle_op_dealloc(struct pycurl_handle_op *op)
   6437 {
   6438 	Py_DECREF(op->handle);
   6439 	PyObject_Del((PyObject *)op);
   6440 }
   6441 
   6442 static PyObject *
   6443 pycurl_handle_op_await(PyObject *op)
   6444 {
   6445 	Py_INCREF(op);
   6446 	return (op);
   6447 }
   6448 
   6449 static PyObject *
   6450 pycurl_handle_op_iternext(struct pycurl_handle_op *op)
   6451 {
   6452 	size_t			len;
   6453 	PyObject		*result;
   6454 	const u_int8_t		*response;
   6455 
   6456 	if (op->state == CURL_CLIENT_OP_RUN) {
   6457 		kore_curl_run(&op->handle->data.curl);
   6458 		op->state = CURL_CLIENT_OP_RESULT;
   6459 		Py_RETURN_NONE;
   6460 	}
   6461 
   6462 	if (op->handle->body != NULL) {
   6463 		kore_buf_free(op->handle->body);
   6464 		op->handle->body = NULL;
   6465 	}
   6466 
   6467 	if (!kore_curl_success(&op->handle->data.curl)) {
   6468 		/* Do not log the url here, may contain some sensitive data. */
   6469 		PyErr_Format(PyExc_RuntimeError, "request failed: %s",
   6470 		    kore_curl_strerror(&op->handle->data.curl));
   6471 		return (NULL);
   6472 	}
   6473 
   6474 	kore_curl_response_as_bytes(&op->handle->data.curl, &response, &len);
   6475 
   6476 	if ((result = PyBytes_FromStringAndSize((const char *)response,
   6477 	    len)) == NULL)
   6478 		return (NULL);
   6479 
   6480 	PyErr_SetObject(PyExc_StopIteration, result);
   6481 	Py_DECREF(result);
   6482 
   6483 	return (NULL);
   6484 }
   6485 
   6486 static PyObject *
   6487 python_kore_httpclient(PyObject *self, PyObject *args, PyObject *kwargs)
   6488 {
   6489 	struct pyhttp_client	*client;
   6490 	const char		*url, *v;
   6491 
   6492 	if (!PyArg_ParseTuple(args, "s", &url))
   6493 		return (NULL);
   6494 
   6495 	client = PyObject_New(struct pyhttp_client, &pyhttp_client_type);
   6496 	if (client == NULL)
   6497 		return (NULL);
   6498 
   6499 	client->unix = NULL;
   6500 	client->tlskey = NULL;
   6501 	client->curlopt = NULL;
   6502 	client->tlscert = NULL;
   6503 	client->cabundle = NULL;
   6504 
   6505 	client->tlsverify = 1;
   6506 	client->url = kore_strdup(url);
   6507 
   6508 	if (kwargs != NULL) {
   6509 		if ((v = python_string_from_dict(kwargs, "tlscert")) != NULL)
   6510 			client->tlscert = kore_strdup(v);
   6511 
   6512 		if ((v = python_string_from_dict(kwargs, "tlskey")) != NULL)
   6513 			client->tlskey = kore_strdup(v);
   6514 
   6515 		if ((v = python_string_from_dict(kwargs, "cabundle")) != NULL)
   6516 			client->cabundle = kore_strdup(v);
   6517 
   6518 		if ((v = python_string_from_dict(kwargs, "unix")) != NULL)
   6519 			client->unix = kore_strdup(v);
   6520 
   6521 		client->curlopt = PyDict_GetItemString(kwargs, "curlopt");
   6522 		Py_XINCREF(client->curlopt);
   6523 
   6524 		python_bool_from_dict(kwargs, "tlsverify", &client->tlsverify);
   6525 	}
   6526 
   6527 	if ((client->tlscert != NULL && client->tlskey == NULL) ||
   6528 	    (client->tlskey != NULL && client->tlscert == NULL)) {
   6529 		Py_DECREF((PyObject *)client);
   6530 		PyErr_SetString(PyExc_RuntimeError,
   6531 		    "invalid TLS client configuration");
   6532 		return (NULL);
   6533 	}
   6534 
   6535 	return ((PyObject *)client);
   6536 }
   6537 
   6538 static void
   6539 pyhttp_client_dealloc(struct pyhttp_client *client)
   6540 {
   6541 	kore_free(client->url);
   6542 	kore_free(client->unix);
   6543 	kore_free(client->tlskey);
   6544 	kore_free(client->tlscert);
   6545 	kore_free(client->cabundle);
   6546 
   6547 	Py_XDECREF(client->curlopt);
   6548 
   6549 	PyObject_Del((PyObject *)client);
   6550 }
   6551 
   6552 static PyObject *
   6553 pyhttp_client_get(struct pyhttp_client *client, PyObject *args,
   6554     PyObject *kwargs)
   6555 {
   6556 	return (pyhttp_client_request(client, HTTP_METHOD_GET, kwargs));
   6557 }
   6558 
   6559 static PyObject *
   6560 pyhttp_client_put(struct pyhttp_client *client, PyObject *args,
   6561     PyObject *kwargs)
   6562 {
   6563 	return (pyhttp_client_request(client, HTTP_METHOD_PUT, kwargs));
   6564 }
   6565 
   6566 static PyObject *
   6567 pyhttp_client_post(struct pyhttp_client *client, PyObject *args,
   6568     PyObject *kwargs)
   6569 {
   6570 	return (pyhttp_client_request(client, HTTP_METHOD_POST, kwargs));
   6571 }
   6572 
   6573 static PyObject *
   6574 pyhttp_client_head(struct pyhttp_client *client, PyObject *args,
   6575     PyObject *kwargs)
   6576 {
   6577 	return (pyhttp_client_request(client, HTTP_METHOD_HEAD, kwargs));
   6578 }
   6579 
   6580 static PyObject *
   6581 pyhttp_client_patch(struct pyhttp_client *client, PyObject *args,
   6582     PyObject *kwargs)
   6583 {
   6584 	return (pyhttp_client_request(client, HTTP_METHOD_PATCH, kwargs));
   6585 }
   6586 
   6587 static PyObject *
   6588 pyhttp_client_delete(struct pyhttp_client *client, PyObject *args,
   6589     PyObject *kwargs)
   6590 {
   6591 	return (pyhttp_client_request(client, HTTP_METHOD_DELETE, kwargs));
   6592 }
   6593 
   6594 static PyObject *
   6595 pyhttp_client_options(struct pyhttp_client *client, PyObject *args,
   6596     PyObject *kwargs)
   6597 {
   6598 	return (pyhttp_client_request(client, HTTP_METHOD_OPTIONS, kwargs));
   6599 }
   6600 
   6601 static PyObject *
   6602 pyhttp_client_request(struct pyhttp_client *client, int m, PyObject *kwargs)
   6603 {
   6604 	struct pyhttp_client_op		*op;
   6605 	char				*ptr;
   6606 	const char			*k, *v;
   6607 	Py_ssize_t			length, idx;
   6608 	PyObject			*data, *headers, *key, *obj;
   6609 
   6610 	ptr = NULL;
   6611 	length = 0;
   6612 	headers = NULL;
   6613 
   6614 	if (kwargs != NULL &&
   6615 	    ((headers = PyDict_GetItemString(kwargs, "headers")) != NULL)) {
   6616 		if (!PyDict_CheckExact(headers)) {
   6617 			PyErr_SetString(PyExc_RuntimeError,
   6618 			    "headers keyword must be a dict");
   6619 			return (NULL);
   6620 		}
   6621 	}
   6622 
   6623 	switch (m) {
   6624 	case HTTP_METHOD_GET:
   6625 	case HTTP_METHOD_HEAD:
   6626 	case HTTP_METHOD_OPTIONS:
   6627 		break;
   6628 	case HTTP_METHOD_PUT:
   6629 	case HTTP_METHOD_POST:
   6630 	case HTTP_METHOD_PATCH:
   6631 	case HTTP_METHOD_DELETE:
   6632 		length = -1;
   6633 
   6634 		if (kwargs == NULL) {
   6635 			if (m == HTTP_METHOD_DELETE) {
   6636 				length = 0;
   6637 				break;
   6638 			}
   6639 
   6640 			PyErr_Format(PyExc_RuntimeError,
   6641 			    "no keyword arguments given, but body expected ",
   6642 			    http_method_text(m));
   6643 			return (NULL);
   6644 		}
   6645 
   6646 		if ((data = PyDict_GetItemString(kwargs, "body")) == NULL)
   6647 			return (NULL);
   6648 
   6649 		if (PyBytes_AsStringAndSize(data, &ptr, &length) == -1)
   6650 			return (NULL);
   6651 
   6652 		if (length < 0) {
   6653 			PyErr_SetString(PyExc_TypeError, "invalid length");
   6654 			return (NULL);
   6655 		}
   6656 		break;
   6657 	default:
   6658 		fatal("%s: unknown method %d", __func__, m);
   6659 	}
   6660 
   6661 	op = PyObject_New(struct pyhttp_client_op, &pyhttp_client_op_type);
   6662 	if (op == NULL)
   6663 		return (NULL);
   6664 
   6665 	if (!kore_curl_init(&op->data.curl, client->url, KORE_CURL_ASYNC)) {
   6666 		Py_DECREF((PyObject *)op);
   6667 		PyErr_SetString(PyExc_RuntimeError, "failed to setup call");
   6668 		return (NULL);
   6669 	}
   6670 
   6671 	op->headers = 0;
   6672 	op->coro = coro_running;
   6673 	op->state = CURL_CLIENT_OP_RUN;
   6674 	LIST_INIT(&op->data.slists);
   6675 
   6676 	Py_INCREF(client);
   6677 	op->client = client;
   6678 
   6679 	kore_curl_http_setup(&op->data.curl, m, ptr, length);
   6680 	kore_curl_bind_callback(&op->data.curl, python_curl_http_callback, op);
   6681 
   6682 	/* Go in with our own bare hands. */
   6683 	if (client->unix != NULL) {
   6684 #if defined(__linux__)
   6685 		if (client->unix[0] == '@') {
   6686 			curl_easy_setopt(op->data.curl.handle,
   6687 			    CURLOPT_ABSTRACT_UNIX_SOCKET, client->unix + 1);
   6688 		} else {
   6689 			curl_easy_setopt(op->data.curl.handle,
   6690 			    CURLOPT_UNIX_SOCKET_PATH, client->unix);
   6691 		}
   6692 #else
   6693 		curl_easy_setopt(op->data.curl.handle, CURLOPT_UNIX_SOCKET_PATH,
   6694 		    client->unix);
   6695 #endif
   6696 	}
   6697 
   6698 	if (client->tlskey != NULL && client->tlscert != NULL) {
   6699 		 curl_easy_setopt(op->data.curl.handle, CURLOPT_SSLCERT,
   6700 		    client->tlscert);
   6701 		 curl_easy_setopt(op->data.curl.handle, CURLOPT_SSLKEY,
   6702 		    client->tlskey);
   6703 	}
   6704 
   6705 	if (client->tlsverify == 0) {
   6706 		curl_easy_setopt(op->data.curl.handle,
   6707 		    CURLOPT_SSL_VERIFYHOST, 0);
   6708 		curl_easy_setopt(op->data.curl.handle,
   6709 		    CURLOPT_SSL_VERIFYPEER, 0);
   6710 	}
   6711 
   6712 	if (client->curlopt != NULL) {
   6713 		if (!python_curlopt_from_dict(&op->data, client->curlopt)) {
   6714 			Py_DECREF((PyObject *)op);
   6715 			return (NULL);
   6716 		}
   6717 	}
   6718 
   6719 	if (client->cabundle != NULL) {
   6720 		curl_easy_setopt(op->data.curl.handle, CURLOPT_CAINFO,
   6721 		    client->cabundle);
   6722 	}
   6723 
   6724 	if (headers != NULL) {
   6725 		idx = 0;
   6726 		while (PyDict_Next(headers, &idx, &key, &obj)) {
   6727 			if ((k = PyUnicode_AsUTF8(key)) == NULL) {
   6728 				Py_DECREF((PyObject *)op);
   6729 				return (NULL);
   6730 			}
   6731 
   6732 			if ((v = PyUnicode_AsUTF8(obj)) == NULL) {
   6733 				Py_DECREF((PyObject *)op);
   6734 				return (NULL);
   6735 			}
   6736 
   6737 			kore_curl_http_set_header(&op->data.curl, k, v);
   6738 		}
   6739 	}
   6740 
   6741 	if (kwargs != NULL) {
   6742 		if ((obj = PyDict_GetItemString(kwargs, "curlopt")) != NULL) {
   6743 			if (!python_curlopt_from_dict(&op->data, obj)) {
   6744 				Py_DECREF((PyObject *)op);
   6745 				return (NULL);
   6746 			}
   6747 		}
   6748 
   6749 		python_bool_from_dict(kwargs, "return_headers", &op->headers);
   6750 	}
   6751 
   6752 	return ((PyObject *)op);
   6753 }
   6754 
   6755 static void
   6756 pyhttp_client_op_dealloc(struct pyhttp_client_op *op)
   6757 {
   6758 	struct pycurl_slist	*psl;
   6759 
   6760 	while ((psl = LIST_FIRST(&op->data.slists))) {
   6761 		LIST_REMOVE(psl, list);
   6762 		curl_slist_free_all(psl->slist);
   6763 		kore_free(psl);
   6764 	}
   6765 
   6766 	Py_DECREF(op->client);
   6767 	kore_curl_cleanup(&op->data.curl);
   6768 	PyObject_Del((PyObject *)op);
   6769 }
   6770 
   6771 static PyObject *
   6772 pyhttp_client_op_await(PyObject *op)
   6773 {
   6774 	Py_INCREF(op);
   6775 	return (op);
   6776 }
   6777 
   6778 static PyObject *
   6779 pyhttp_client_op_iternext(struct pyhttp_client_op *op)
   6780 {
   6781 	size_t			len;
   6782 	struct http_header	*hdr;
   6783 	const u_int8_t		*response;
   6784 	PyObject		*result, *tuple, *dict, *value;
   6785 
   6786 	if (op->state == CURL_CLIENT_OP_RUN) {
   6787 		kore_curl_run(&op->data.curl);
   6788 		op->state = CURL_CLIENT_OP_RESULT;
   6789 		Py_RETURN_NONE;
   6790 	}
   6791 
   6792 	if (!kore_curl_success(&op->data.curl)) {
   6793 		PyErr_Format(PyExc_RuntimeError, "request to '%s' failed: %s",
   6794 		    op->data.curl.url, kore_curl_strerror(&op->data.curl));
   6795 		return (NULL);
   6796 	}
   6797 
   6798 	kore_curl_response_as_bytes(&op->data.curl, &response, &len);
   6799 
   6800 	if (op->headers) {
   6801 		kore_curl_http_parse_headers(&op->data.curl);
   6802 
   6803 		if ((dict = PyDict_New()) == NULL)
   6804 			return (NULL);
   6805 
   6806 		TAILQ_FOREACH(hdr, &op->data.curl.http.resp_hdrs, list) {
   6807 			value = PyUnicode_FromString(hdr->value);
   6808 			if (value == NULL) {
   6809 				Py_DECREF(dict);
   6810 				return (NULL);
   6811 			}
   6812 
   6813 			if (PyDict_SetItemString(dict,
   6814 			    hdr->header, value) == -1) {
   6815 				Py_DECREF(dict);
   6816 				Py_DECREF(value);
   6817 				return (NULL);
   6818 			}
   6819 
   6820 			Py_DECREF(value);
   6821 		}
   6822 
   6823 		if ((tuple = Py_BuildValue("(iOy#)", op->data.curl.http.status,
   6824 		    dict, (const char *)response, len)) == NULL)
   6825 			return (NULL);
   6826 
   6827 		Py_DECREF(dict);
   6828 	} else {
   6829 		if ((tuple = Py_BuildValue("(iy#)", op->data.curl.http.status,
   6830 		    (const char *)response, len)) == NULL)
   6831 			return (NULL);
   6832 	}
   6833 
   6834 	result = PyObject_CallFunctionObjArgs(PyExc_StopIteration, tuple, NULL);
   6835 	if (result == NULL) {
   6836 		Py_DECREF(tuple);
   6837 		return (NULL);
   6838 	}
   6839 
   6840 	Py_DECREF(tuple);
   6841 	PyErr_SetObject(PyExc_StopIteration, result);
   6842 	Py_DECREF(result);
   6843 
   6844 	return (NULL);
   6845 }
   6846 
   6847 static void
   6848 python_curl_http_callback(struct kore_curl *curl, void *arg)
   6849 {
   6850 	struct pyhttp_client_op		*op = arg;
   6851 
   6852 	if (op->coro->request != NULL)
   6853 		http_request_wakeup(op->coro->request);
   6854 	else
   6855 		python_coro_wakeup(op->coro);
   6856 }
   6857 
   6858 static void
   6859 python_curl_handle_callback(struct kore_curl *curl, void *arg)
   6860 {
   6861 	struct pycurl_handle_op		*op = arg;
   6862 
   6863 	if (op->coro->request != NULL)
   6864 		http_request_wakeup(op->coro->request);
   6865 	else
   6866 		python_coro_wakeup(op->coro);
   6867 }
   6868 #endif