Blame src/daemon/ntapi_dsr_init.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
dde53a
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
6c2ba1
#include <ntapi/nt_atomic.h>
dd89bb
#include <ntapi/nt_status.h>
dd89bb
#include <ntapi/nt_thread.h>
dd89bb
#include <ntapi/nt_port.h>
dd89bb
#include <ntapi/nt_daemon.h>
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
553954
int32_t __attr_hidden__ __ntapi_tt_seh_frame(void *, void *, void *, int32_t (*)(nt_daemon_params *));
cd4191
f9e9df
static int32_t __ntapi_dsr_once(nt_daemon_params * params);
f9e9df
static int32_t __ntapi_dsr_start(nt_daemon_params * params);
dd89bb
dd89bb
int32_t __stdcall __ntapi_dsr_init(nt_daemon_params * params)
dd89bb
{
dd89bb
	int32_t			status;
dd89bb
	nt_thread_params	tparams;
dd89bb
6c2ba1
	/* report status */
6c2ba1
	at_store_32(
6c2ba1
		&params->exit_code_daemon_loop,
6c2ba1
		NT_STATUS_PENDING);
6c2ba1
6c2ba1
	at_store_32(
6c2ba1
		&params->exit_code_daemon_start,
6c2ba1
		NT_STATUS_PENDING);
6c2ba1
6c2ba1
	at_store_32(
6c2ba1
		&params->exit_code_internal_client,
6c2ba1
		NT_STATUS_PENDING);
6c2ba1
dd89bb
	/* port_keys */
dd89bb
	if (params->flags & NT_DSR_INIT_GENERATE_KEYS)
dd89bb
		if ((status = __ntapi->tt_port_generate_keys(params->port_keys)))
dd89bb
			return status;
dd89bb
dd89bb
	/* port_name_keys */
dd89bb
	if (params->flags & NT_DSR_INIT_FORMAT_KEYS)
dd89bb
		__ntapi->tt_port_format_keys(
dd89bb
			params->port_keys,
dd89bb
			params->port_name_keys);
dd89bb
ad58de
	/* 'port-created' event */
ad58de
	if (!params->hevent_daemon_port) {
ad58de
		if ((status = __ntapi->tt_create_private_event(
ad58de
				&params->hevent_daemon_port,
ad58de
				NT_NOTIFICATION_EVENT,
ad58de
				NT_EVENT_NOT_SIGNALED)))
ad58de
			return status;
ad58de
ad58de
		if (params->pevent_daemon_port)
ad58de
			at_store(
ad58de
				(intptr_t *)params->pevent_daemon_port,
ad58de
				(intptr_t)params->hevent_daemon_port);
ad58de
	}
ad58de
dd89bb
	/* 'daemon-is-ready' event */
dd89bb
	if (!params->hevent_daemon_ready) {
dd89bb
		if ((status = __ntapi->tt_create_private_event(
dd89bb
				&params->hevent_daemon_ready,
dd89bb
				NT_NOTIFICATION_EVENT,
dd89bb
				NT_EVENT_NOT_SIGNALED)))
dd89bb
			return status;
dd89bb
dd89bb
		if (params->pevent_daemon_ready)
a5dc9f
			at_store(
a5dc9f
				(intptr_t *)params->pevent_daemon_ready,
a5dc9f
				(intptr_t)params->hevent_daemon_ready);
dd89bb
	}
dd89bb
dd89bb
	/* 'internal-client-is-ready' event */
dd89bb
	if (!params->hevent_internal_client_ready) {
a5dc9f
		if ((status = __ntapi->tt_create_private_event(
dd89bb
				&params->hevent_internal_client_ready,
dd89bb
				NT_NOTIFICATION_EVENT,
dd89bb
				NT_EVENT_NOT_SIGNALED)))
dd89bb
			return status;
dd89bb
dd89bb
		if (params->pevent_internal_client_ready)
a5dc9f
			at_store(
a5dc9f
				(intptr_t *)params->pevent_internal_client_ready,
a5dc9f
				(intptr_t)params->hevent_internal_client_ready);
dd89bb
	}
dd89bb
2766fe
	/* memory barrier */
2766fe
	__ntapi->tt_aligned_block_memlock(
2766fe
		params,
2766fe
		sizeof(*params));
2766fe
2766fe
	__ntapi->tt_aligned_block_memlock(
2766fe
		params->port_name,
2766fe
		sizeof(*params->port_name));
2766fe
dd89bb
	/* daemon dedicated thread: general parameters */
dd89bb
	__ntapi->tt_aligned_block_memset(
dd89bb
		&tparams,0,sizeof(tparams));
dd89bb
dd89bb
	tparams.start = (nt_thread_start_routine *)__ntapi_dsr_start;
dd89bb
	tparams.arg   = params;
dd89bb
dd89bb
	/* daemon dedicated thread: stack parameters (optional) */
dd89bb
	tparams.stack_size_commit	= params->stack_size_commit;
dd89bb
	tparams.stack_size_reserve	= params->stack_size_reserve;
dd89bb
	tparams.stack_info		= params->stack_info;
dd89bb
dd89bb
	/* daemon dedicated thread: create */
a5dc9f
	if ((status = __ntapi->tt_create_local_thread(&tparams)))
a5dc9f
		return status;
dd89bb
ad58de
	at_store(
ad58de
		(intptr_t *)&params->hthread_daemon_loop,
ad58de
		(intptr_t)tparams.hthread);
d34088
dd89bb
	/* daemon dedicated thread: actual stack size */
dd89bb
	params->stack_size_commit	= tparams.stack_size_commit;
dd89bb
	params->stack_size_reserve	= tparams.stack_size_reserve;
dd89bb
ad58de
	/* wait for the port to be created */
ad58de
	if ((status = __ntapi->zw_wait_for_single_object(
ad58de
			params->hevent_daemon_port,
ad58de
			NT_SYNC_NON_ALERTABLE,0)))
ad58de
		return status;
ad58de
5a17e1
	/* overcome potential scheduling bug in recent (10.0) kernel */
5a17e1
	while (params->exit_code_daemon_start == NT_STATUS_PENDING)
5a17e1
		__ntapi->zw_delay_execution(
5a17e1
			NT_SYNC_NON_ALERTABLE,
5a17e1
			&(nt_timeout){{0,0}});
5a17e1
ad58de
	/* verify the port's successful creation */
2766fe
	if (at_locked_xadd_32(&params->exit_code_daemon_start,0))
ad58de
		return NT_STATUS_PORT_NOT_SET;
dd89bb
ad58de
	/* internal connection task-specific thread */
dd89bb
	__ntapi->tt_aligned_block_memset(
dd89bb
		&tparams,0,sizeof(tparams));
dd89bb
dd89bb
	tparams.start = (nt_thread_start_routine *)__ntapi_dsr_internal_client_connect;
dd89bb
	tparams.arg   = params;
dd89bb
a5dc9f
	if ((status = __ntapi->tt_create_local_thread(&tparams)))
a5dc9f
		return status;
a5dc9f
ad58de
	at_store(
ad58de
		(intptr_t *)&params->hthread_internal_client,
ad58de
		(intptr_t)tparams.hthread);
dd89bb
ad58de
	/* wait for the task-specific thread to exit */
a5dc9f
	if ((status = __ntapi->zw_wait_for_single_object(
ad58de
			params->hthread_internal_client,
ad58de
			NT_SYNC_NON_ALERTABLE,0)))
a5dc9f
		return status;
dd89bb
5a17e1
	/* overcome potential scheduling bug in recent (10.0) kernel */
5a17e1
	while (params->exit_code_internal_client == NT_STATUS_PENDING)
5a17e1
		__ntapi->zw_delay_execution(
5a17e1
			NT_SYNC_NON_ALERTABLE,
5a17e1
			&(nt_timeout){{0,0}});
5a17e1
ad58de
	/* verify a successful internal connection */
2766fe
	if (at_locked_xadd_32(&params->exit_code_internal_client,0))
ad58de
		return params->exit_code_internal_client;
ad58de
ad58de
	/* all_done */
ad58de
	__ntapi->zw_set_event(
ad58de
		params->hevent_daemon_ready,
ad58de
		0);
ad58de
dd89bb
	if (params->flags & NT_DSR_INIT_CLOSE_EVENTS) {
ad58de
		__ntapi->zw_close(params->hevent_daemon_port);
dd89bb
		__ntapi->zw_close(params->hevent_daemon_ready);
dd89bb
	}
dd89bb
a5dc9f
	return NT_STATUS_SUCCESS;;
dd89bb
}
dd89bb
dd89bb
dd89bb
/* __ntapi_dsr_start executes in the daemon's dedicated thread */
cd4191
static int32_t __ntapi_dsr_start_impl(nt_daemon_params * params)
dd89bb
{
6f3f3f
	int32_t status;
a5dc9f
	void *	ctx;
f85724
	int32_t (__stdcall *loop)(void *);
a5dc9f
d16b42
	ctx  = params->daemon_loop_context;
d16b42
	loop = params->daemon_loop_routine;
6f3f3f
6f3f3f
	if ((status = __ntapi_dsr_once(params)))
6f3f3f
		return status;
6f3f3f
6f3f3f
	if ((status = __ntapi_dsr_create_port(params)))
6f3f3f
		return status;
6f3f3f
6f3f3f
	if ((status = __ntapi_dsr_connect_internal_client(params)))
6f3f3f
		return status;
6f3f3f
d16b42
	if ((status = loop(ctx)))
6f3f3f
		return status;
6f3f3f
6f3f3f
	return NT_STATUS_SUCCESS;
6f3f3f
}
dd89bb
cd4191
static int32_t __ntapi_dsr_start_routine(nt_daemon_params * params)
6f3f3f
{
6c2ba1
	at_store_32(
6c2ba1
		&params->exit_code_daemon_loop,
6c2ba1
		NT_STATUS_MORE_PROCESSING_REQUIRED);
6c2ba1
6f3f3f
	return __ntapi->zw_terminate_thread(
6f3f3f
		NT_CURRENT_THREAD_HANDLE,
6f3f3f
		__ntapi_dsr_start_impl(params));
dd89bb
}
dd89bb
f9e9df
static int32_t __ntapi_dsr_start(nt_daemon_params * params)
cd4191
{
cd4191
	return __ntapi_tt_seh_frame(
cd4191
		params,0,0,
cd4191
		__ntapi_dsr_start_routine);
cd4191
}
6f3f3f
dd89bb
/* __ntapi_dsr_once executes in the daemon's dedicated thread */
f9e9df
static int32_t __ntapi_dsr_once(nt_daemon_params * params)
dd89bb
{
dd89bb
	int32_t status;
dd89bb
dd89bb
	if (!params->daemon_once_routine)
6f3f3f
		return NT_STATUS_SUCCESS;
dd89bb
6f3f3f
	if ((status = params->daemon_once_routine(params->daemon_loop_context)))
6c2ba1
		at_store_32(
6c2ba1
			&params->exit_code_daemon_start,
6c2ba1
			status);
6f3f3f
6f3f3f
	return status;
dd89bb
}
dd89bb
dd89bb
/* __ntapi_dsr_create_port executes in the daemon's dedicated thread */
6c2ba1
static int32_t __stdcall __ntapi_dsr_create_port_exit(
6c2ba1
	nt_daemon_params *	params,
6c2ba1
	int32_t			status)
6c2ba1
{
6c2ba1
	at_store_32(
6c2ba1
		&params->exit_code_daemon_start,
6c2ba1
		status);
6c2ba1
ad58de
	__ntapi->zw_set_event(
ad58de
		params->hevent_daemon_port,
ad58de
		0);
ad58de
6c2ba1
	return status;
6c2ba1
}
6c2ba1
dd89bb
int32_t __stdcall __ntapi_dsr_create_port(nt_daemon_params * params)
dd89bb
{
6c2ba1
	int32_t				status;
dd89bb
	nt_object_attributes		oa;
dd89bb
	nt_security_quality_of_service	sqos;
dd89bb
	nt_unicode_string		server_name;
dd89bb
dd89bb
	/* init server_name */
ad58de
	server_name.strlen = (uint16_t)__ntapi->tt_string_null_offset_short((int16_t *)params->port_name);
dd89bb
	server_name.maxlen = 0;
ad58de
	server_name.buffer = params->port_name;
dd89bb
dd89bb
	/* init security structure */
dd89bb
	sqos.length 			= sizeof(sqos);
dd89bb
	sqos.impersonation_level	= NT_SECURITY_IMPERSONATION;
dd89bb
	sqos.context_tracking_mode	= NT_SECURITY_TRACKING_DYNAMIC;
dd89bb
	sqos.effective_only		= 1;
dd89bb
dd89bb
	/* init the port's object attributes */
dd89bb
	oa.len		= sizeof(oa);
ad58de
	oa.root_dir	= 0;
dd89bb
	oa.obj_name	= &server_name;
dd89bb
	oa.obj_attr	= 0;
138984
	oa.sec_desc	= params->port_sd;
dd89bb
	oa.sec_qos	= &sqo;;
dd89bb
dd89bb
	/* create the port */
6c2ba1
	if ((status = __ntapi->zw_create_port(
6f3f3f
			&params->hport_daemon,
6f3f3f
			&oa,0,(uint32_t)params->port_msg_size,
6f3f3f
			0)))
6c2ba1
		return __ntapi_dsr_create_port_exit(
6c2ba1
			params,status);
dd89bb
dd89bb
	/* return port info */
dd89bb
	if (params->pport_daemon)
6c2ba1
		at_store(
6c2ba1
			(intptr_t *)params->pport_daemon,
6c2ba1
			(intptr_t)params->hport_daemon);
dd89bb
ad58de
	/* set status */
ad58de
	at_store_32(
ad58de
		&params->exit_code_daemon_start,
ad58de
		NT_STATUS_SUCCESS);
ad58de
ad58de
	/* signal the port-created event */
ad58de
	__ntapi->zw_set_event(
ad58de
		params->hevent_daemon_port,
ad58de
		0);
dd89bb
ad58de
	return NT_STATUS_SUCCESS;
dd89bb
}