Blame src/daemon/toks_daemon_init.c

5e5175
/*********************************************************/
5e5175
/*  toksvc: a framework-native token broker service      */
5e5175
/*  Copyright (C) 2020  Z. Gilboa                        */
5e5175
/*  Released under GPLv2 and GPLv3; see COPYING.TOKSVC.  */
5e5175
/*********************************************************/
5e5175
5e5175
#include <psxtypes/psxtypes.h>
5e5175
#include <ntapi/ntapi.h>
5e5175
#include <ntapi/nt_atomic.h>
5e5175
5e5175
#include <toksvc/toksvc.h>
5e5175
#include "toksvc_daemon_impl.h"
5e5175
#include "toksvc_driver_impl.h"
5e5175
5e5175
static const nt_guid toks_daemon_guid = TOKS_PORT_GUID_DAEMON;
5e5175
5e5175
static int32_t toks_daemon_init_impl(struct toks_daemon_ctx * dctx, void * htty)
5e5175
{
5e5175
	int32_t				status;
5e5175
	nt_daemon_params		dparams;
5e5175
	wchar16_t *			port_name;
5e5175
	nt_port_name_keys *		port_name_keys;
5e5175
5e5175
	/* daemon attributes */
5e5175
	dctx->daemon_attr.type    = NT_PORT_TYPE_DAEMON;
5e5175
	dctx->daemon_attr.subtype = NT_PORT_SUBTYPE_DEFAULT;
5e5175
5e5175
	/* port guid */
5e5175
	ntapi->tt_guid_copy(
5e5175
		&dctx->daemon_attr.guid,
5e5175
		&toks_daemon_guid);
5e5175
5e5175
	/* port keys */
5e5175
	if ((status = ntapi->tt_port_generate_keys(&dctx->daemon_attr.keys)))
5e5175
		return status;
5e5175
5e5175
	/* port name */
5e5175
	ntapi->tt_port_name_from_attr(
5e5175
		&dctx->daemon_name,
5e5175
		&dctx->daemon_attr);
5e5175
5e5175
	/* dparams */
5e5175
	ntapi->tt_aligned_block_memset(
5e5175
		&dparams,0,sizeof(dparams));
5e5175
5e5175
	port_name      = (wchar16_t *)&dctx->daemon_name;
5e5175
	port_name_keys = (nt_port_name_keys *)&dctx->daemon_name.port_name_keys;
5e5175
5e5175
	dparams.port_keys	= &dctx->daemon_keys;
5e5175
	dparams.port_name	= port_name;
5e5175
	dparams.port_name_keys	= port_name_keys;
5e5175
5e5175
	dparams.port_msg_size	= sizeof(nt_tty_port_msg);
5e5175
	dparams.flags		= NT_DSR_INIT_DEFAULT;
5e5175
5e5175
	dparams.daemon_once_routine	= 0;
5e5175
	dparams.daemon_loop_routine	= toks_daemon_loop;
5e5175
	dparams.daemon_loop_context	= dctx;
5e5175
5e5175
	dparams.pport_daemon		= &dctx->hport_daemon;
5e5175
	dparams.pport_internal_client	= &dctx->hport_internal_client;
5e5175
5e5175
	dparams.pevent_daemon_ready		= &dctx->hevent_daemon_ready;
5e5175
	dparams.pevent_internal_client_ready	= &dctx->hevent_internal_client_ready;
5e5175
5e5175
	dparams.stack_size_commit		= 8192;
5e5175
	dparams.stack_size_reserve		= 8192;
5e5175
5e5175
	if ((status = ntapi->dsr_init(&dparams)))
5e5175
		return status;
5e5175
5e5175
	return ntapi->tty_request_peer(
5e5175
                htty,
5e5175
		TOKS_DAEMON_TTYSIGNAL,
5e5175
		0,&(nt_guid)TTY_PTS_GUID,
5e5175
		&dctx->daemon_attr);
5e5175
}
5e5175
5e5175
static int32_t toks_daemon_once = 0;
5e5175
5e5175
int32_t __stdcall toks_daemon_init(struct toks_daemon_ctx * dctx, uint64_t drvflags)
5e5175
{
5e5175
	int32_t			status;
5e5175
	nt_timeout		timeout;
5e5175
	nt_runtime_data *	rtdata;
5e5175
5e5175
	/* rtdata */
5e5175
	if ((status = ntapi->tt_get_runtime_data(&rtdata,0)))
5e5175
		return status;
5e5175
5e5175
	/* needed? */
5e5175
	if (drvflags & TOKS_DRIVER_DAEMON_NEVER)
5e5175
		return 0;
5e5175
5e5175
	if (!(drvflags & TOKS_DRIVER_DAEMON_ALWAYS))
5e5175
		if (ntapi->tt_guid_compare(&rtdata->srv_guid,&toks_daemon_guid))
5e5175
			return 0;
5e5175
5e5175
	/* once */
5e5175
	switch (at_locked_cas_32(&toks_daemon_once,0,1)) {
5e5175
		case 0:
5e5175
			if ((status = toks_daemon_init_impl(dctx,rtdata->hsession))) {
5e5175
				at_locked_add_32(&toks_daemon_once,2);
5e5175
				return status;
5e5175
			}
5e5175
5e5175
			at_locked_inc_32(&toks_daemon_once);
5e5175
			return 0;
5e5175
5e5175
		case 1:
5e5175
			timeout.quad = -10;
5e5175
5e5175
			for (; (at_locked_cas_32(&toks_daemon_once,0,1) == 1); )
5e5175
				ntapi->zw_delay_execution(
5e5175
					NT_SYNC_ALERTABLE,
5e5175
					&timeout);
5e5175
5e5175
			return (toks_daemon_once == 2)
5e5175
				? 0 : -1;
5e5175
5e5175
		case 2:
5e5175
			return 0;
5e5175
5e5175
		case 3:
5e5175
		default:
5e5175
			return -1;
5e5175
	}
5e5175
}