Blame src/daemon/ptyc_daemon_init.c

778de5
/*********************************************************/
778de5
/*  ptycon: a pty-console bridge                         */
f25e99
/*  Copyright (C) 2016--2017  SysDeer Technologies, LLC  */
778de5
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.  */
778de5
/*********************************************************/
778de5
778de5
#include <psxtypes/psxtypes.h>
778de5
#include <ntapi/ntapi.h>
778de5
#include <ntapi/nt_atomic.h>
778de5
778de5
#include <ptycon/ptycon.h>
778de5
#include "ptycon_daemon_impl.h"
778de5
#include "ptycon_driver_impl.h"
778de5
778de5
static const nt_guid ptyc_daemon_guid = PTYC_PORT_GUID_DAEMON;
778de5
cc2a48
static int32_t ptyc_daemon_init_impl(struct ptyc_daemon_ctx * dctx, void * htty)
778de5
{
778de5
	int32_t				status;
778de5
	nt_daemon_params		dparams;
778de5
	wchar16_t *			port_name;
778de5
	nt_port_name_keys *		port_name_keys;
778de5
778de5
	/* daemon attributes */
778de5
	dctx->daemon_attr.type    = NT_PORT_TYPE_DAEMON;
778de5
	dctx->daemon_attr.subtype = NT_PORT_SUBTYPE_DEFAULT;
778de5
778de5
	/* port guid */
778de5
	ntapi->tt_guid_copy(
778de5
		&dctx->daemon_attr.guid,
778de5
		&ptyc_daemon_guid);
778de5
778de5
	/* port keys */
778de5
	if ((status = ntapi->tt_port_generate_keys(&dctx->daemon_attr.keys)))
778de5
		return status;
778de5
778de5
	/* port name */
39751a
	ntapi->tt_port_name_from_attr(
778de5
		&dctx->daemon_name,
778de5
		&dctx->daemon_attr);
778de5
778de5
	/* dparams */
778de5
	ntapi->tt_aligned_block_memset(
778de5
		&dparams,0,sizeof(dparams));
778de5
778de5
	port_name      = (wchar16_t *)&dctx->daemon_name;
778de5
	port_name_keys = (nt_port_name_keys *)&dctx->daemon_name.port_name_keys;
778de5
778de5
	dparams.port_keys	= &dctx->daemon_keys;
778de5
	dparams.port_name	= port_name;
778de5
	dparams.port_name_keys	= port_name_keys;
778de5
778de5
	dparams.port_msg_size	= sizeof(nt_tty_port_msg);
778de5
	dparams.flags		= NT_DSR_INIT_DEFAULT;
778de5
778de5
	dparams.daemon_once_routine	= 0;
778de5
	dparams.daemon_loop_routine	= ptyc_daemon_loop;
778de5
	dparams.daemon_loop_context	= dctx;
778de5
778de5
	dparams.pport_daemon		= &dctx->hport_daemon;
778de5
	dparams.pport_internal_client	= &dctx->hport_internal_client;
778de5
778de5
	dparams.pevent_daemon_ready		= &dctx->hevent_daemon_ready;
778de5
	dparams.pevent_internal_client_ready	= &dctx->hevent_internal_client_ready;
778de5
778de5
	dparams.stack_size_commit		= 8192;
778de5
	dparams.stack_size_reserve		= 8192;
778de5
cc2a48
	if ((status = ntapi->dsr_init(&dparams)))
cc2a48
		return status;
cc2a48
cc2a48
	return ntapi->tty_request_peer(
cc2a48
                htty,
cc2a48
		PTYC_DAEMON_TTYSIGNAL,
cc2a48
		0,&(nt_guid)TTY_PTS_GUID,
cc2a48
		&dctx->daemon_attr);
778de5
}
778de5
778de5
static int32_t ptyc_daemon_once = 0;
778de5
778de5
int32_t __stdcall ptyc_daemon_init(struct ptyc_daemon_ctx * dctx, uint64_t drvflags)
778de5
{
778de5
	int32_t			status;
778de5
	nt_timeout		timeout;
778de5
	nt_runtime_data *	rtdata;
778de5
778de5
	/* rtdata */
778de5
	if ((status = ntapi->tt_get_runtime_data(&rtdata,0)))
778de5
		return status;
778de5
778de5
	/* needed? */
778de5
	if (drvflags & PTYC_DRIVER_DAEMON_NEVER)
778de5
		return 0;
778de5
778de5
	if (!(drvflags & PTYC_DRIVER_DAEMON_ALWAYS))
778de5
		if (ntapi->tt_guid_compare(&rtdata->srv_guid,&ptyc_daemon_guid))
778de5
			return 0;
778de5
778de5
	/* once */
778de5
	switch (at_locked_cas_32(&ptyc_daemon_once,0,1)) {
778de5
		case 0:
cc2a48
			if ((status = ptyc_daemon_init_impl(dctx,rtdata->hsession))) {
778de5
				at_locked_add_32(&ptyc_daemon_once,2);
778de5
				return status;
778de5
			}
778de5
778de5
			at_locked_inc_32(&ptyc_daemon_once);
778de5
			return 0;
778de5
778de5
		case 1:
778de5
			timeout.quad = -10;
778de5
778de5
			for (; (at_locked_cas_32(&ptyc_daemon_once,0,1) == 1); )
778de5
				ntapi->zw_delay_execution(
778de5
					NT_SYNC_ALERTABLE,
778de5
					&timeout);
778de5
778de5
			return (ptyc_daemon_once == 2)
778de5
				? 0 : -1;
778de5
778de5
		case 2:
778de5
			return 0;
778de5
778de5
		case 3:
778de5
		default:
778de5
			return -1;
778de5
	}
778de5
}