Blame src/tty/ntapi_tty_create_session.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
dd89bb
/*  Copyright (C) 2013,2014,2015  Z. Gilboa             */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
static int32_t __fastcall __tty_create_session_return(
dd89bb
	nt_create_process_params *	params,
dd89bb
	int32_t				status)
dd89bb
{
dd89bb
	if (status)
dd89bb
		__ntapi->zw_terminate_process(
dd89bb
			params->hprocess,
dd89bb
			NT_STATUS_UNEXPECTED_IO_ERROR);
dd89bb
dd89bb
	__ntapi->zw_close(params->hprocess);
dd89bb
	__ntapi->zw_close(params->hthread);
dd89bb
dd89bb
	return status;
dd89bb
}
dd89bb
dd89bb
int32_t __stdcall __ntapi_tty_create_session(
dd89bb
	__out	void **			hport,
dd89bb
	__out	nt_port_name *		port_name,
dd89bb
	__in	nt_tty_session_type	type,
dd89bb
	__in	const nt_guid *		guid		__optional,
dd89bb
	__in	wchar16_t *		image_name	__optional)
dd89bb
{
dd89bb
	nt_status			status;
dd89bb
	ntapi_internals *		__internals;
dd89bb
dd89bb
	nt_port_attr			port_attr;
dd89bb
	nt_runtime_data			ssattr;
dd89bb
	nt_runtime_data_block		rtblock;
dd89bb
	nt_create_process_params	params;
dd89bb
dd89bb
	wchar16_t __attr_aligned__(8) __tty_image_name_fallback[] =  {
dd89bb
		'\\','?','?','\\',
dd89bb
		'C',':',
dd89bb
		'\\','m','i','d','i','p','i','x',
dd89bb
		'\\','b','i','n',
dd89bb
		'\\','n','t','c','t','t','y',
dd89bb
		'.','e','x','e',
dd89bb
		0};
dd89bb
dd89bb
	/* init */
dd89bb
	__internals = __ntapi_internals();
dd89bb
dd89bb
	__ntapi->tt_aligned_block_memset(
dd89bb
		&port_attr,0,sizeof(port_attr));
dd89bb
dd89bb
	switch (type) {
dd89bb
		case NT_TTY_SESSION_PRIMARY:
dd89bb
			port_attr.type    = NT_PORT_TYPE_SUBSYSTEM;
dd89bb
			port_attr.subtype = NT_PORT_SUBTYPE_DEFAULT;
dd89bb
dd89bb
			if (!hport)
dd89bb
				hport = &__internals->hport_tty_session;
dd89bb
dd89bb
			if (!port_name)
dd89bb
				port_name = __internals->subsystem;
dd89bb
dd89bb
			if (!image_name)
dd89bb
				image_name = __tty_image_name_fallback;
dd89bb
dd89bb
			break;
dd89bb
dd89bb
		case NT_TTY_SESSION_PRIVATE:
dd89bb
			port_attr.type    = NT_PORT_TYPE_SUBSYSTEM;
dd89bb
			port_attr.subtype = NT_PORT_SUBTYPE_PRIVATE;
dd89bb
			break;
dd89bb
dd89bb
		default:
dd89bb
			return NT_STATUS_INVALID_PARAMETER;
dd89bb
	}
dd89bb
dd89bb
	/* port guid */
dd89bb
	if (guid)
dd89bb
		__ntapi->tt_guid_copy(
dd89bb
			&port_attr.guid,
dd89bb
			guid);
dd89bb
	else
dd89bb
		__ntapi->tt_port_guid_from_type(
dd89bb
			&port_attr.guid,
dd89bb
			port_attr.type,
dd89bb
			port_attr.subtype);
dd89bb
dd89bb
	/* port keys */
dd89bb
	if ((status = __ntapi->tt_port_generate_keys(&port_attr.keys)))
dd89bb
		return status;
dd89bb
dd89bb
	/* port name */
dd89bb
	__ntapi->tt_port_name_from_attributes(
dd89bb
		port_name,
dd89bb
		&port_attr);
dd89bb
dd89bb
	/* subsystem attributes */
dd89bb
	__ntapi->tt_aligned_block_memset(
dd89bb
		&ssattr,0,sizeof(ssattr));
dd89bb
dd89bb
	ssattr.srv_type		= port_attr.type;
dd89bb
	ssattr.srv_subtype	= port_attr.subtype;
dd89bb
	ssattr.srv_keys[0]	= port_attr.keys.key[0];
dd89bb
	ssattr.srv_keys[1]	= port_attr.keys.key[1];
dd89bb
	ssattr.srv_keys[2]	= port_attr.keys.key[2];
dd89bb
	ssattr.srv_keys[3]	= port_attr.keys.key[3];
dd89bb
	ssattr.srv_keys[4]	= port_attr.keys.key[4];
dd89bb
	ssattr.srv_keys[5]	= port_attr.keys.key[5];
dd89bb
dd89bb
	__ntapi->tt_guid_copy(
dd89bb
		&ssattr.srv_guid,
dd89bb
		&port_attr.guid);
dd89bb
dd89bb
	if ((status = __ntapi->tt_create_private_event(
dd89bb
			&ssattr.srv_ready,
dd89bb
			NT_SYNCHRONIZATION_EVENT,
dd89bb
			NT_EVENT_NOT_SIGNALED)))
dd89bb
		return status;
dd89bb
dd89bb
	/* create subsystem process */
dd89bb
	rtblock.addr		= &ssattr;
dd89bb
	rtblock.size		= sizeof(ssattr);
dd89bb
	rtblock.remote_addr	= 0;
dd89bb
	rtblock.remote_size	= 0;
dd89bb
	rtblock.flags		= NT_RUNTIME_DATA_DUPLICATE_SESSION_HANDLES;
dd89bb
dd89bb
	__ntapi->tt_aligned_block_memset(
dd89bb
		&params,0,sizeof(params));
dd89bb
dd89bb
	params.image_name	= image_name;
dd89bb
	params.rtblock		= &rtblock;
dd89bb
dd89bb
	if ((status = __ntapi->tt_create_native_process(&params)))
dd89bb
		return status;
dd89bb
dd89bb
	if ((status = __ntapi->zw_wait_for_single_object(
dd89bb
			ssattr.srv_ready,
dd89bb
			NT_SYNC_NON_ALERTABLE,
dd89bb
			0)))
dd89bb
		return __tty_create_session_return(&params,status);
dd89bb
dd89bb
	/* connect to subsystem */
dd89bb
	if ((status = __ntapi->tty_connect(
dd89bb
			hport,
dd89bb
			&port_name->base_named_objects[0],
dd89bb
			NT_SECURITY_IMPERSONATION)))
dd89bb
		return __tty_create_session_return(&params,status);
dd89bb
dd89bb
	/* finalize primary session */
dd89bb
	if (type == NT_TTY_SESSION_PRIMARY) {
dd89bb
		if (hport != &__internals->hport_tty_session)
dd89bb
			__internals->hport_tty_session = *hport;
dd89bb
dd89bb
		if (port_name != __internals->subsystem)
dd89bb
			__ntapi->tt_memcpy_utf16(
dd89bb
				__internals->subsystem->base_named_objects,
dd89bb
				port_name->base_named_objects,
dd89bb
				sizeof(*port_name));
dd89bb
	};
dd89bb
dd89bb
	return __tty_create_session_return(&params,NT_STATUS_SUCCESS);
dd89bb
}