Blame src/daemon/ntapi_dsr_init.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
4256e2
/*  Copyright (C) 2013--2016  Z. Gilboa                 */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
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
dd89bb
static void __stdcall __ntapi_dsr_once(nt_daemon_params * params);
dd89bb
dd89bb
int32_t __stdcall __ntapi_dsr_init(nt_daemon_params * params)
dd89bb
{
dd89bb
	int32_t			status;
dd89bb
dd89bb
	nt_thread_params	tparams;
dd89bb
	nt_large_integer	timeout;
dd89bb
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
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)
dd89bb
			*(params->pevent_daemon_ready) = params->hevent_daemon_ready;
dd89bb
	}
dd89bb
dd89bb
	/* 'internal-client-is-ready' event */
dd89bb
	if (!params->hevent_internal_client_ready) {
dd89bb
		if ((status = __ntapi->tt_create_inheritable_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)
dd89bb
			*(params->pevent_internal_client_ready) = params->hevent_internal_client_ready;
dd89bb
	}
dd89bb
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 */
dd89bb
	status = __ntapi->tt_create_local_thread(&tparams);
dd89bb
	params->hthread_daemon_loop = tparams.hthread;
dd89bb
	if (status) return status;
dd89bb
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
dd89bb
dd89bb
	/* establish internal connection */
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
dd89bb
	status = __ntapi->tt_create_local_thread(&tparams);
dd89bb
	params->hthread_internal_client = tparams.hthread;
dd89bb
	if (status) return status;
dd89bb
dd89bb
	/* wait until the internal connection had been established */
dd89bb
	timeout.quad = NT_DSR_INIT_MAX_WAIT;
dd89bb
dd89bb
	status = __ntapi->zw_wait_for_single_object(
dd89bb
		params->hevent_internal_client_ready,
dd89bb
		0,
dd89bb
		&timeout);
dd89bb
dd89bb
	if (params->flags & NT_DSR_INIT_CLOSE_EVENTS) {
dd89bb
		__ntapi->zw_close(params->hevent_daemon_ready);
dd89bb
		__ntapi->zw_close(params->hevent_internal_client_ready);
dd89bb
	}
dd89bb
dd89bb
	return status;
dd89bb
}
dd89bb
dd89bb
dd89bb
/* __ntapi_dsr_start executes in the daemon's dedicated thread */
dd89bb
int32_t __stdcall __ntapi_dsr_start(nt_daemon_params * params)
dd89bb
{
dd89bb
	__ntapi_dsr_once(params);
dd89bb
	__ntapi_dsr_create_port(params);
dd89bb
	__ntapi_dsr_connect_internal_client(params);
dd89bb
	params->daemon_loop_routine(params->daemon_loop_context);
dd89bb
dd89bb
	/* (no return) */
dd89bb
	return NT_STATUS_INTERNAL_ERROR;
dd89bb
}
dd89bb
dd89bb
/* __ntapi_dsr_once executes in the daemon's dedicated thread */
dd89bb
static void __stdcall __ntapi_dsr_once(nt_daemon_params * params)
dd89bb
{
dd89bb
	int32_t status;
dd89bb
dd89bb
	if (!params->daemon_once_routine)
dd89bb
		return;
dd89bb
dd89bb
	if ((status = params->daemon_once_routine(params->daemon_loop_context))) {
dd89bb
		params->exit_code_daemon_start = status;
dd89bb
		__ntapi->zw_terminate_thread(NT_CURRENT_THREAD_HANDLE,status);
dd89bb
	}
dd89bb
}
dd89bb
dd89bb
/* __ntapi_dsr_create_port executes in the daemon's dedicated thread */
dd89bb
int32_t __stdcall __ntapi_dsr_create_port(nt_daemon_params * params)
dd89bb
{
dd89bb
	int32_t *			pstatus;
dd89bb
	nt_object_attributes		oa;
dd89bb
	nt_security_quality_of_service	sqos;
dd89bb
	nt_unicode_string		server_name;
dd89bb
dd89bb
	pstatus = &params->exit_code_daemon_start;
dd89bb
dd89bb
	/* init server_name */
dd89bb
	server_name.strlen = (uint16_t)__ntapi->tt_string_null_offset_short((const int16_t *)params->port_name);
dd89bb
	server_name.maxlen = 0;
dd89bb
	server_name.buffer = (uint16_t *)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);
dd89bb
	oa.root_dir	= (void *)0;
dd89bb
	oa.obj_name	= &server_name;
dd89bb
	oa.obj_attr	= 0;
dd89bb
	oa.sec_desc	= (nt_security_descriptor *)0;
dd89bb
	oa.sec_qos	= &sqo;;
dd89bb
dd89bb
	/* create the port */
dd89bb
	*pstatus = __ntapi->zw_create_port(
dd89bb
		&params->hport_daemon,
dd89bb
		&oa,0,(uint32_t)params->port_msg_size,
dd89bb
		0);
dd89bb
dd89bb
	if (*pstatus != NT_STATUS_SUCCESS)
dd89bb
		__ntapi->zw_terminate_thread(
dd89bb
			NT_CURRENT_THREAD_HANDLE,
dd89bb
			*pstatus);
dd89bb
dd89bb
	/* return port info */
dd89bb
	if (params->pport_daemon)
dd89bb
		*(params->pport_daemon) = params->hport_daemon;
dd89bb
dd89bb
	/* signal the daemon-is-ready event */
dd89bb
	*pstatus = __ntapi->zw_set_event(
dd89bb
		params->hevent_daemon_ready,
dd89bb
		(int32_t *)0);
dd89bb
dd89bb
	if (*pstatus != NT_STATUS_SUCCESS)
dd89bb
		__ntapi->zw_terminate_thread(
dd89bb
			NT_CURRENT_THREAD_HANDLE,
dd89bb
			*pstatus);
dd89bb
dd89bb
	return *pstatus;
dd89bb
}