Blame src/daemon/toks_daemon_connect.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
5e5175
#include <toksvc/toksvc.h>
5e5175
#include "toksvc_daemon_impl.h"
5e5175
#include "toksvc_driver_impl.h"
5e5175
74d496
static int32_t toks_daemon_client_wait(void * rapunzel)
74d496
{
74d496
	struct toks_client_ctx * client;
74d496
74d496
	client = (struct toks_client_ctx *)rapunzel;
74d496
74d496
	ntapi->zw_set_event(client->hswap,0);
74d496
	ntapi->zw_close(client->hswap);
74d496
74d496
	ntapi->zw_wait_for_single_object(
74d496
		client->hprocess,
74d496
		NT_SYNC_NON_ALERTABLE,
74d496
		0);
74d496
74d496
	ntapi->zw_close(client->hprocess);
74d496
	ntapi->zw_close(client->hport);
74d496
74d496
	return ntapi->zw_terminate_thread(
74d496
		NT_CURRENT_THREAD_HANDLE,
74d496
		NT_STATUS_SUCCESS);
74d496
}
74d496
74d496
static int32_t toks_daemon_client_instance(struct toks_client_ctx * client)
74d496
{
74d496
	int32_t			status;
74d496
	nt_thread_params	params;
74d496
74d496
	if ((status = ntapi->tt_create_private_event(
74d496
			&client->hswap,
74d496
			NT_NOTIFICATION_EVENT,
74d496
			NT_EVENT_NOT_SIGNALED)))
74d496
		return status;
74d496
74d496
	ntapi->tt_aligned_block_memset(
74d496
		&params,0,sizeof(params));
74d496
74d496
	params.hprocess		  = NT_CURRENT_PROCESS_HANDLE;
74d496
	params.start		  = toks_daemon_client_wait;
74d496
	params.ext_ctx            = client;
74d496
	params.ext_ctx_size       = sizeof(*client);
74d496
	params.stack_size_commit  = 4 * 1024;
74d496
	params.stack_size_reserve = 4 * 1024;
74d496
	params.creation_flags	  = NT_CREATE_LOCAL_THREAD;
74d496
74d496
	if ((status = ntapi->tt_create_thread(&params)))
74d496
		return status;
74d496
74d496
	if ((status = ntapi->zw_wait_for_single_object(
74d496
			client->hswap,
74d496
			NT_SYNC_NON_ALERTABLE,
74d496
			0)))
74d496
		return status;
74d496
74d496
	return ntapi->zw_close(params.hthread);
74d496
}
74d496
9985a1
int32_t __stdcall toks_daemon_connect(struct toks_daemon_ctx * dctx)
5e5175
{
74d496
	int32_t                     status;
74d496
	nt_tty_port_msg *           msg;
74d496
	struct toks_client_ctx      client;
74d496
	nt_oa                       oa;
5e5175
74d496
	msg                   = &dctx->reply;
74d496
	msg->ttyinfo.exarg    = 0;
5e5175
74d496
	client.hport          = 0;
74d496
	client.hprocess       = 0;
74d496
	client.hswap          = 0;
74d496
	client.cid.process_id = msg->header.client_id.process_id;
74d496
	client.cid.thread_id  = 0;
74d496
74d496
	oa.len      = sizeof(oa);
74d496
	oa.root_dir = 0;
74d496
	oa.obj_name = 0;
74d496
	oa.obj_attr = 0;
74d496
	oa.sec_desc = 0;
74d496
	oa.sec_qos  = 0;
74d496
74d496
	status = ntapi->zw_open_process(
74d496
		&client.hprocess,
74d496
		NT_PROCESS_SYNCHRONIZE | NT_PROCESS_QUERY_INFORMATION,
74d496
		&oa,&client.cid);
74d496
74d496
	status = ntapi->zw_accept_connect_port(
74d496
		&client.hport,
74d496
		client.cid.process_id,
5e5175
		&msg->header,
5e5175
		NT_LPC_ACCEPT_CONNECTION,0,0);
5e5175
74d496
	if (status) {
74d496
		ntapi->zw_close(client.hprocess);
74d496
		return status;
74d496
	}
74d496
74d496
	ntapi->zw_complete_connect_port(client.hport);
74d496
74d496
	return toks_daemon_client_instance(&client);
5e5175
}