kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

python.c (155162B)



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