Blame src/msq/ntapi_msq_send.c

47f21a
/********************************************************/
47f21a
/*  ntapi: Native API core library                      */
47f21a
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
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_send_return(
ef6024
	void *			mapaddr,
ef6024
	intptr_t volatile *	hlock,
ef6024
	int32_t			status)
47f21a
{
12d3bf
	if (hlock)
12d3bf
		at_store(hlock,0);
12d3bf
12d3bf
	if (mapaddr)
12d3bf
		__ntapi->zw_unmap_view_of_section(
12d3bf
			NT_CURRENT_PROCESS_HANDLE,
12d3bf
			mapaddr);
12d3bf
47f21a
	return status;
47f21a
}
47f21a
47f21a
47f21a
int32_t __stdcall __ntapi_msq_send(
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
	__in	const 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;
12d3bf
	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
	/* section */
47f21a
	if (msq->section_addr) {
47f21a
		hsection = msq->section;
47f21a
		secaddr  = msq->section_addr;
47f21a
		secsize  = msq->section_size;
12d3bf
		mapaddr  = 0;
47f21a
47f21a
	} else if ((status = __ntapi->ipc_init_section_by_port(
47f21a
			msq->hport,&hsection,
47f21a
			&secaddr,&secsize)))
47f21a
		return status;
47f21a
12d3bf
	else
12d3bf
		mapaddr = secaddr;
12d3bf
47f21a
	if (len > secsize)
12d3bf
		return __msq_send_return(
12d3bf
			mapaddr,0,
12d3bf
			NT_STATUS_INFO_LENGTH_MISMATCH);
47f21a
47f21a
	/* lock */
47f21a
	hlock = &(__ntapi_internals()->hlock);
47f21a
47f21a
	if (at_locked_cas(hlock,0,1))
12d3bf
		return __msq_send_return(
12d3bf
			mapaddr,0,
12d3bf
			NT_STATUS_RESOURCE_NOT_OWNED);
47f21a
47f21a
	/* msq data to section */
47f21a
	__ntapi->tt_generic_memcpy(
47f21a
		secaddr,buffer,len);
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_SEND;
47f21a
47f21a
	msg.data.msqinfo.msqkey		= msq->msqkey;
47f21a
	msg.data.msqinfo.msqid		= msq->msqid;
47f21a
	msg.data.msqinfo.msqspid	= msq->msqspid;
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)))
12d3bf
		return __msq_send_return(mapaddr,hlock,status);
47f21a
	else if (msg.data.ttyinfo.status)
12d3bf
		return __msq_send_return(mapaddr,hlock,msg.data.ttyinfo.status);
47f21a
47f21a
	iosb->status = msg.data.msqinfo.ntiosb.status;
47f21a
	iosb->info   = msg.data.msqinfo.ntiosb.info;
47f21a
12d3bf
	return __msq_send_return(mapaddr,hlock,NT_STATUS_SUCCESS);
47f21a
}