Blame src/sem/ntapi_sem_ioctl.c

9faf9a
/********************************************************/
9faf9a
/*  ntapi: Native API core library                      */
64e606
/*  Copyright (C) 2013--2021  SysDeer Technologies, LLC */
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(
ef6024
	void *			mapaddr,
ef6024
	intptr_t volatile *	hlock,
ef6024
	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;
ef6024
	intptr_t volatile *	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,
a6e4e7
			&secaddr,&secsize))) {
9faf9a
		return status;
9faf9a
a6e4e7
	} else {
31d7ce
		mapaddr = secaddr;
a6e4e7
	}
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
373b6b
	while (at_locked_cas(hlock,0,1))
373b6b
		__ntapi->zw_delay_execution(
373b6b
			NT_SYNC_NON_ALERTABLE,
373b6b
			&(nt_timeout){{0,0}});
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
}