Blame src/sem/ntapi_sem_ioctl.c

9faf9a
/********************************************************/
9faf9a
/*  ntapi: Native API core library                      */
9faf9a
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
9faf9a
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
9faf9a
/********************************************************/
9faf9a
9faf9a
#include <psxtypes/psxtypes.h>
9faf9a
#include <ntapi/nt_file.h>
9faf9a
#include <ntapi/nt_string.h>
9faf9a
#include <ntapi/nt_atomic.h>
9faf9a
#include <ntapi/nt_port.h>
9faf9a
#include <ntapi/nt_ipc.h>
9faf9a
#include <ntapi/nt_sem.h>
9faf9a
#include <ntapi/ntapi.h>
9faf9a
#include "ntapi_impl.h"
9faf9a
9faf9a
static int32_t __sem_ioctl_return(
31d7ce
	void *		mapaddr,
9faf9a
	intptr_t *	hlock,
9faf9a
	int32_t		status)
9faf9a
{
31d7ce
	if (hlock)
31d7ce
		at_store(hlock,0);
31d7ce
31d7ce
	if (mapaddr)
31d7ce
		__ntapi->zw_unmap_view_of_section(
31d7ce
			NT_CURRENT_PROCESS_HANDLE,
31d7ce
			mapaddr);
31d7ce
9faf9a
	return status;
9faf9a
}
9faf9a
9faf9a
9faf9a
int32_t	__stdcall __ntapi_sem_ioctl(
9faf9a
	__in	nt_sem_info *		sem,
9faf9a
	__in	void *			hevent			__optional,
9faf9a
	__in	nt_io_apc_routine *	apc_routine		__optional,
9faf9a
	__in	void *			apc_context		__optional,
9faf9a
	__out	nt_iosb *		iosb,
9faf9a
	__in	uint32_t		io_control_code,
9faf9a
	__in	void *			input_buffer		__optional,
9faf9a
	__in	uint32_t		input_buffer_length,
9faf9a
	__out	void *			output_buffer		__optional,
9faf9a
	__in	uint32_t		output_buffer_length)
9faf9a
{
9faf9a
	int32_t			status;
31d7ce
	void *			mapaddr;
9faf9a
	void *			hsection;
9faf9a
	void *			secaddr;
9faf9a
	size_t			secsize;
9faf9a
	nt_sem_info_msg		msg;
9faf9a
	intptr_t *		hlock;
9faf9a
9faf9a
	(void)output_buffer;
9faf9a
	(void)output_buffer_length;
9faf9a
9faf9a
	/* validate */
9faf9a
	if (io_control_code)
9faf9a
		return NT_STATUS_NOT_SUPPORTED;
9faf9a
9faf9a
	else if (!iosb)
9faf9a
		return NT_STATUS_INVALID_PARAMETER;
9faf9a
9faf9a
	else if (!input_buffer_length)
9faf9a
		return NT_STATUS_INVALID_PARAMETER;
9faf9a
9faf9a
	else if (input_buffer_length % sizeof(nt_sem_op))
9faf9a
		return NT_STATUS_INFO_LENGTH_MISMATCH;
9faf9a
9faf9a
	/* section */
9faf9a
	if (sem->section_addr) {
9faf9a
		hsection = sem->section;
9faf9a
		secaddr  = sem->section_addr;
9faf9a
		secsize  = sem->section_size;
31d7ce
		mapaddr  = 0;
9faf9a
9faf9a
	} else if ((status = __ntapi->ipc_init_section_by_port(
9faf9a
			sem->hport,&hsection,
9faf9a
			&secaddr,&secsize)))
9faf9a
		return status;
9faf9a
31d7ce
	else
31d7ce
		mapaddr = secaddr;
31d7ce
9faf9a
	if (input_buffer_length > secsize)
31d7ce
		return __sem_ioctl_return(
31d7ce
			mapaddr,0,
31d7ce
			NT_STATUS_INFO_LENGTH_MISMATCH);
9faf9a
9faf9a
	/* lock */
9faf9a
	hlock = &(__ntapi_internals()->hlock);
9faf9a
9faf9a
	if (at_locked_cas(hlock,0,1))
31d7ce
		return __sem_ioctl_return(
31d7ce
			mapaddr,0,
31d7ce
			NT_STATUS_RESOURCE_NOT_OWNED);
9faf9a
9faf9a
	/* semop array to section */
9faf9a
	__ntapi->tt_generic_memcpy(
9faf9a
		secaddr,input_buffer,
9faf9a
		input_buffer_length);
9faf9a
9faf9a
	/* msg */
9faf9a
	__ntapi->tt_aligned_block_memset(
9faf9a
		&msg,0,sizeof(msg));
9faf9a
9faf9a
	msg.header.msg_type		= NT_LPC_NEW_MESSAGE;
9faf9a
	msg.header.data_size		= sizeof(msg.data);
9faf9a
	msg.header.msg_size		= sizeof(msg);
9faf9a
	msg.data.ttyinfo.opcode		= NT_TTY_SEM_IOCTL;
9faf9a
9faf9a
	msg.data.seminfo.semkey		= sem->semkey;
9faf9a
	msg.data.seminfo.semid		= sem->semid;
9faf9a
	msg.data.seminfo.sempid		= sem->sempid;
9faf9a
	msg.data.seminfo.section_addr	= secaddr;
9faf9a
	msg.data.seminfo.section_size	= input_buffer_length;
9faf9a
808b63
	msg.data.seminfo.hevent		= hevent;
9faf9a
	msg.data.seminfo.apc_routine	= apc_routine;
9faf9a
	msg.data.seminfo.apc_context	= apc_context;
9faf9a
	msg.data.seminfo.riosb		= iosb;
9faf9a
9faf9a
	if ((status = __ntapi->zw_request_wait_reply_port(sem->hport,&msg,&msg)))
31d7ce
		return __sem_ioctl_return(mapaddr,hlock,status);
9faf9a
	else if (msg.data.ttyinfo.status)
31d7ce
		return __sem_ioctl_return(mapaddr,hlock,msg.data.ttyinfo.status);
9faf9a
9faf9a
	iosb->status = NT_STATUS_SUCCESS;
9faf9a
	iosb->info   = 0;
9faf9a
31d7ce
	return __sem_ioctl_return(mapaddr,hlock,NT_STATUS_SUCCESS);
9faf9a
}