Blob Blame History Raw
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntapi/nt_file.h>
#include <ntapi/nt_string.h>
#include <ntapi/nt_atomic.h>
#include <ntapi/nt_port.h>
#include <ntapi/nt_ipc.h>
#include <ntapi/nt_sem.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"

static int32_t __sem_ioctl_return(
	intptr_t *	hlock,
	int32_t		status)
{
	at_store(hlock,0);
	return status;
}


int32_t	__stdcall __ntapi_sem_ioctl(
	__in	nt_sem_info *		sem,
	__in	void *			hevent			__optional,
	__in	nt_io_apc_routine *	apc_routine		__optional,
	__in	void *			apc_context		__optional,
	__out	nt_iosb *		iosb,
	__in	uint32_t		io_control_code,
	__in	void *			input_buffer		__optional,
	__in	uint32_t		input_buffer_length,
	__out	void *			output_buffer		__optional,
	__in	uint32_t		output_buffer_length)
{
	int32_t			status;
	void *			hsection;
	void *			secaddr;
	size_t			secsize;
	nt_sem_info_msg		msg;
	intptr_t *		hlock;

	(void)output_buffer;
	(void)output_buffer_length;

	/* validate */
	if (io_control_code)
		return NT_STATUS_NOT_SUPPORTED;

	else if (!iosb)
		return NT_STATUS_INVALID_PARAMETER;

	else if (!input_buffer_length)
		return NT_STATUS_INVALID_PARAMETER;

	else if (input_buffer_length % sizeof(nt_sem_op))
		return NT_STATUS_INFO_LENGTH_MISMATCH;

	/* section */
	if (sem->section_addr) {
		hsection = sem->section;
		secaddr  = sem->section_addr;
		secsize  = sem->section_size;

	} else if ((status = __ntapi->ipc_init_section_by_port(
			sem->hport,&hsection,
			&secaddr,&secsize)))
		return status;

	if (input_buffer_length > secsize)
		return NT_STATUS_INFO_LENGTH_MISMATCH;

	/* lock */
	hlock = &(__ntapi_internals()->hlock);

	if (at_locked_cas(hlock,0,1))
		return NT_STATUS_RESOURCE_NOT_OWNED;

	/* semop array to section */
	__ntapi->tt_generic_memcpy(
		secaddr,input_buffer,
		input_buffer_length);

	/* msg */
	__ntapi->tt_aligned_block_memset(
		&msg,0,sizeof(msg));

	msg.header.msg_type		= NT_LPC_NEW_MESSAGE;
	msg.header.data_size		= sizeof(msg.data);
	msg.header.msg_size		= sizeof(msg);
	msg.data.ttyinfo.opcode		= NT_TTY_SEM_IOCTL;

	msg.data.seminfo.semkey		= sem->semkey;
	msg.data.seminfo.semid		= sem->semid;
	msg.data.seminfo.sempid		= sem->sempid;
	msg.data.seminfo.section_addr	= secaddr;
	msg.data.seminfo.section_size	= input_buffer_length;

	msg.data.seminfo.hevent[0]	= hevent;
	msg.data.seminfo.apc_routine	= apc_routine;
	msg.data.seminfo.apc_context	= apc_context;
	msg.data.seminfo.riosb		= iosb;

	if ((status = __ntapi->zw_request_wait_reply_port(sem->hport,&msg,&msg)))
		return __sem_ioctl_return(hlock,status);
	else if (msg.data.ttyinfo.status)
		return __sem_ioctl_return(hlock,msg.data.ttyinfo.status);

	iosb->status = NT_STATUS_SUCCESS;
	iosb->info   = 0;

	return __sem_ioctl_return(hlock,NT_STATUS_SUCCESS);
}