summaryrefslogtreecommitdiffhomepage
path: root/src/msq/ntapi_msq_send.c
blob: fb9d44ac44c92268d50a32bfc03c0aa5edd56cd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/********************************************************/
/*  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_msq.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"

static int32_t __msq_send_return(
	void *			mapaddr,
	intptr_t volatile *	hlock,
	int32_t			status)
{
	if (hlock)
		at_store(hlock,0);

	if (mapaddr)
		__ntapi->zw_unmap_view_of_section(
			NT_CURRENT_PROCESS_HANDLE,
			mapaddr);

	return status;
}


int32_t __stdcall __ntapi_msq_send(
	__in	nt_msq_info *		msq,
	__in 	void *			hevent		__optional,
	__in	nt_io_apc_routine *	apc_routine	__optional,
	__in	void * 			apc_context	__optional,
	__in	const void *		buffer,
	__in	size_t			len,
	__in	intptr_t		rank,
	__in	uint32_t		options,
	__out	nt_io_status_block *	iosb)
{
	int32_t			status;
	void *			mapaddr;
	void *			hsection;
	void *			secaddr;
	size_t			secsize;
	nt_msq_info_msg		msg;
	intptr_t volatile *	hlock;

	/* validate */
	if (!iosb)
		return NT_STATUS_INVALID_PARAMETER;

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

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

	} else {
		mapaddr = secaddr;
	}

	if (len > secsize)
		return __msq_send_return(
			mapaddr,0,
			NT_STATUS_INFO_LENGTH_MISMATCH);

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

	while (at_locked_cas(hlock,0,1))
		__ntapi->zw_delay_execution(
			NT_SYNC_NON_ALERTABLE,
			&(nt_timeout){{0,0}});

	/* msq data to section */
	__ntapi->tt_generic_memcpy(
		secaddr,buffer,len);

	/* 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_MSQ_SEND;

	msg.data.msqinfo.msqkey		= msq->msqkey;
	msg.data.msqinfo.msqid		= msq->msqid;
	msg.data.msqinfo.msqspid	= msq->msqspid;
	msg.data.msqinfo.msqrank	= rank;
	msg.data.msqinfo.section_addr	= secaddr;
	msg.data.msqinfo.section_size	= len;
	msg.data.msqinfo.ntoptions	= options;

	msg.data.msqinfo.hevent		= hevent;
	msg.data.msqinfo.apc_routine	= apc_routine;
	msg.data.msqinfo.apc_context	= apc_context;
	msg.data.msqinfo.riosb		= iosb;

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

	iosb->status = msg.data.msqinfo.ntiosb.status;
	iosb->info   = msg.data.msqinfo.ntiosb.info;

	return __msq_send_return(mapaddr,hlock,NT_STATUS_SUCCESS);
}