Blame src/ipc/ntapi_ipc_create_pipe_v2.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
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/nt_file.h>
dd89bb
#include <ntapi/nt_string.h>
dd89bb
#include <ntapi/nt_tty.h>
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
int32_t __stdcall	__ntapi_ipc_create_pipe_v2(
dd89bb
	__out		void **			hpipe_read,
dd89bb
	__out		void **			hpipe_write,
842d0f
	__in		uint32_t		advisory_buffer_size)
dd89bb
{
dd89bb
	int32_t			status;
dd89bb
dd89bb
	void *			hdevpipes;
dd89bb
	void *			hwrite;
dd89bb
	void *			hread;
dd89bb
dd89bb
	nt_object_attributes	oa;
dd89bb
	nt_io_status_block	iosb;
dd89bb
	nt_sqos			sqos;
dd89bb
	nt_unicode_string	nt_name;
7a0fb6
	nt_timeout		timeout;
dd89bb
dd89bb
	const wchar16_t pipe_dir[] =  {
dd89bb
		'\\','D','e','v','i','c','e',
dd89bb
		'\\','N','a','m','e','d','P','i','p','e','\\',0
dd89bb
	};
dd89bb
dd89bb
	/* nt_name: pipe device directory */
dd89bb
	nt_name.strlen = (uint16_t)(sizeof(pipe_dir) - sizeof(wchar16_t));
dd89bb
	nt_name.maxlen = 0;
dd89bb
	nt_name.buffer = (uint16_t *)pipe_dir;
dd89bb
dd89bb
	/* init security structure */
dd89bb
	sqos.length 			= sizeof(sqos);
842d0f
	sqos.impersonation_level	= NT_SECURITY_ANONYMOUS;
842d0f
	sqos.context_tracking_mode	= NT_SECURITY_TRACKING_STATIC;
dd89bb
	sqos.effective_only		= 1;
dd89bb
dd89bb
	/* oa */
dd89bb
	oa.len		= sizeof(oa);
dd89bb
	oa.root_dir	= (void *)0;
dd89bb
	oa.obj_name	= &nt_name;
dd89bb
	oa.obj_attr	= NT_OBJ_CASE_INSENSITIVE | NT_OBJ_INHERIT;
dd89bb
	oa.sec_desc	= (nt_security_descriptor *)0;
dd89bb
	oa.sec_qos	= &sqo;;
dd89bb
7a0fb6
	if ((status = __ntapi->zw_open_file(
7a0fb6
			&hdevpipes,
7a0fb6
			NT_GENERIC_READ | NT_SEC_SYNCHRONIZE,
7a0fb6
			&oa,
7a0fb6
			&iosb,
7a0fb6
			NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE,
7a0fb6
			NT_FILE_DIRECTORY_FILE)))
dd89bb
		return status;
dd89bb
dd89bb
	timeout.ihigh = 0xffffffff;
dd89bb
	timeout.ulow  = 0x0;
dd89bb
dd89bb
	oa.root_dir = hdevpipes;
dd89bb
7a0fb6
	nt_name.strlen = 0;
7a0fb6
	nt_name.buffer = 0;
dd89bb
dd89bb
	status = __ntapi->zw_create_named_pipe_file(
dd89bb
		&hread,
dd89bb
		NT_GENERIC_READ | NT_SEC_SYNCHRONIZE | NT_FILE_WRITE_ATTRIBUTES,
dd89bb
		&oa,
dd89bb
		&iosb,
dd89bb
		NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE,
dd89bb
		NT_FILE_CREATE,
dd89bb
		NT_FILE_ASYNCHRONOUS_IO,
842d0f
		NT_FILE_PIPE_BYTE_STREAM_TYPE,
842d0f
		NT_FILE_PIPE_BYTE_STREAM_MODE,
842d0f
		NT_FILE_PIPE_QUEUE_OPERATION,
dd89bb
		1,
842d0f
		advisory_buffer_size,
842d0f
		advisory_buffer_size,
dd89bb
		&timeout);
dd89bb
7a0fb6
	__ntapi->zw_close(
7a0fb6
		hdevpipes);
7a0fb6
7a0fb6
	if (status)
dd89bb
		return status;
dd89bb
dd89bb
	/* the pipe is now our root directory */
dd89bb
	oa.root_dir = hread;
dd89bb
dd89bb
	status = __ntapi->zw_open_file(
dd89bb
		&hwrite,
dd89bb
		NT_GENERIC_WRITE | NT_SEC_SYNCHRONIZE | NT_FILE_READ_ATTRIBUTES,
dd89bb
		&oa,
dd89bb
		&iosb,
dd89bb
		NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE,
842d0f
		NT_FILE_WRITE_THROUGH | NT_FILE_SYNCHRONOUS_IO_ALERT | NT_FILE_NON_DIRECTORY_FILE);
dd89bb
7a0fb6
	if (status) {
dd89bb
		__ntapi->zw_close(hread);
dd89bb
		return status;
dd89bb
	}
dd89bb
dd89bb
	*hpipe_read  = hread;
dd89bb
	*hpipe_write = hwrite;
dd89bb
dd89bb
	return status;
dd89bb
}