Blame src/msq/ntapi_msq_recv.c

47f21a
/********************************************************/
47f21a
/*  ntapi: Native API core library                      */
64e606
/*  Copyright (C) 2013--2021  SysDeer Technologies, LLC */
47f21a
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
47f21a
/********************************************************/
47f21a
47f21a
#include <psxtypes/psxtypes.h>
47f21a
#include <ntapi/nt_file.h>
47f21a
#include <ntapi/nt_string.h>
47f21a
#include <ntapi/nt_atomic.h>
47f21a
#include <ntapi/nt_port.h>
47f21a
#include <ntapi/nt_ipc.h>
47f21a
#include <ntapi/nt_msq.h>
47f21a
#include <ntapi/ntapi.h>
47f21a
#include "ntapi_impl.h"
47f21a
47f21a
static int32_t __msq_recv_return(
ef6024
	void *			mapaddr,
ef6024
	intptr_t volatile *	hlock,
ef6024
	int32_t			status)
47f21a
{
f2dbb3
	if (hlock)
f2dbb3
		at_store(hlock,0);
f2dbb3
f2dbb3
	if (mapaddr)
f2dbb3
		__ntapi->zw_unmap_view_of_section(
f2dbb3
			NT_CURRENT_PROCESS_HANDLE,
f2dbb3
			mapaddr);
f2dbb3
47f21a
	return status;
47f21a
}
47f21a
47f21a
47f21a
int32_t __stdcall __ntapi_msq_recv(
47f21a
	__in	nt_msq_info *		msq,
47f21a
	__in 	void *			hevent		__optional,
47f21a
	__in	nt_io_apc_routine *	apc_routine	__optional,
47f21a
	__in	void * 			apc_context	__optional,
47f21a
	__out	void *			buffer,
47f21a
	__in	size_t			len,
47f21a
	__in	intptr_t		rank,
47f21a
	__in	uint32_t		options,
47f21a
	__out	nt_io_status_block *	iosb)
47f21a
{
47f21a
	int32_t			status;
f2dbb3
	void *			mapaddr;
47f21a
	void *			hsection;
47f21a
	void *			secaddr;
47f21a
	size_t			secsize;
47f21a
	nt_msq_info_msg		msg;
ef6024
	intptr_t volatile *	hlock;
47f21a
47f21a
	/* validate */
47f21a
	if (!iosb)
47f21a
		return NT_STATUS_INVALID_PARAMETER;
47f21a
47f21a
	else if (!len)
47f21a
		return NT_STATUS_INVALID_PARAMETER;
47f21a
47f21a
	/* section */
47f21a
	if (msq->section_addr) {
47f21a
		hsection = msq->section;
47f21a
		secaddr  = msq->section_addr;
47f21a
		secsize  = msq->section_size;
f2dbb3
		mapaddr  = 0;
47f21a
47f21a
	} else if ((status = __ntapi->ipc_init_section_by_port(
47f21a
			msq->hport,&hsection,
ebbce4
			&secaddr,&secsize))) {
47f21a
		return status;
47f21a
ebbce4
	} else {
f2dbb3
		mapaddr = secaddr;
ebbce4
	}
f2dbb3
47f21a
	/* len */
47f21a
	if (len > secsize)
47f21a
		len = secsize;
47f21a
47f21a
	/* lock */
47f21a
	hlock = &(__ntapi_internals()->hlock);
47f21a
485b7b
	while (at_locked_cas(hlock,0,1))
485b7b
		__ntapi->zw_delay_execution(
485b7b
			NT_SYNC_NON_ALERTABLE,
485b7b
			&(nt_timeout){{0,0}});
47f21a
47f21a
	/* msg */
47f21a
	__ntapi->tt_aligned_block_memset(
47f21a
		&msg,0,sizeof(msg));
47f21a
47f21a
	msg.header.msg_type		= NT_LPC_NEW_MESSAGE;
47f21a
	msg.header.data_size		= sizeof(msg.data);
47f21a
	msg.header.msg_size		= sizeof(msg);
47f21a
	msg.data.ttyinfo.opcode		= NT_TTY_MSQ_RECV;
47f21a
47f21a
	msg.data.msqinfo.msqkey		= msq->msqkey;
47f21a
	msg.data.msqinfo.msqid		= msq->msqid;
47f21a
	msg.data.msqinfo.msqrpid	= msq->msqrpid;
47f21a
	msg.data.msqinfo.msqrank	= rank;
47f21a
	msg.data.msqinfo.section_addr	= secaddr;
47f21a
	msg.data.msqinfo.section_size	= len;
47f21a
	msg.data.msqinfo.ntoptions	= options;
47f21a
47f21a
	msg.data.msqinfo.hevent		= hevent;
47f21a
	msg.data.msqinfo.apc_routine	= apc_routine;
47f21a
	msg.data.msqinfo.apc_context	= apc_context;
47f21a
	msg.data.msqinfo.riosb		= iosb;
47f21a
47f21a
	if ((status = __ntapi->zw_request_wait_reply_port(msq->hport,&msg,&msg)))
f2dbb3
		return __msq_recv_return(mapaddr,hlock,status);
47f21a
	else if (msg.data.ttyinfo.status)
f2dbb3
		return __msq_recv_return(mapaddr,hlock,msg.data.ttyinfo.status);
47f21a
47f21a
	/* msq data section to buffer */
47f21a
	__ntapi->tt_generic_memcpy(
47f21a
		buffer,secaddr,
47f21a
		msg.data.msqinfo.ntiosb.info);
47f21a
47f21a
	/* all done */
47f21a
	iosb->status = msg.data.msqinfo.ntiosb.status;
47f21a
	iosb->info   = msg.data.msqinfo.ntiosb.info;
47f21a
f2dbb3
	return __msq_recv_return(mapaddr,hlock,NT_STATUS_SUCCESS);
47f21a
}