Blame src/sem/ntapi_sem_query.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_query_return(
ef6024
	void *			mapaddr,
ef6024
	intptr_t volatile *	hlock,
ef6024
	int32_t			status)
9faf9a
{
14cb5f
	if (hlock)
9faf9a
		at_store(hlock,0);
9faf9a
dfc080
	if (mapaddr)
dfc080
		__ntapi->zw_unmap_view_of_section(
dfc080
			NT_CURRENT_PROCESS_HANDLE,
dfc080
			mapaddr);
dfc080
9faf9a
	return status;
9faf9a
}
9faf9a
9faf9a
int32_t	__stdcall __ntapi_sem_query(
9faf9a
	__in	nt_sem_info *		sem,
9faf9a
	__out	nt_io_status_block *	iosb,
9faf9a
	__out	void *			sem_info,
9faf9a
	__in	uint32_t		sem_info_length,
9faf9a
	__in	int32_t			sem_ipc_cmd)
9faf9a
{
9faf9a
	int32_t			status;
dfc080
	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
	/* validate */
9faf9a
	if (!iosb)
9faf9a
		return NT_STATUS_INVALID_PARAMETER;
9faf9a
9faf9a
	else if (!sem_info)
9faf9a
		return NT_STATUS_INVALID_PARAMETER;
9faf9a
9faf9a
	else if (!sem_info_length)
9faf9a
		return NT_STATUS_INVALID_PARAMETER;
9faf9a
9faf9a
	else if (sem_ipc_cmd != NT_SEM_CMD_GETALL)
9faf9a
		if (sem_info_length != sizeof(nt_sem_info))
9faf9a
			return NT_STATUS_INFO_LENGTH_MISMATCH;
9faf9a
14cb5f
	/* init */
14cb5f
	hlock = 0;
14cb5f
9faf9a
	/* section */
9faf9a
	hsection = 0;
9faf9a
	secaddr  = 0;
9faf9a
	secsize  = 0;
9faf9a
9faf9a
	if (sem_ipc_cmd == NT_SEM_CMD_GETALL) {
9faf9a
		if (sem->section_addr) {
9faf9a
			hsection = sem->section;
9faf9a
			secaddr  = sem->section_addr;
9faf9a
			secsize  = sem->section_size;
dfc080
			mapaddr  = 0;
9faf9a
9faf9a
		} else if ((status = __ntapi->ipc_init_section_by_port(
9faf9a
				sem->hport,&hsection,
ca05bb
				&secaddr,&secsize))) {
9faf9a
			return status;
9faf9a
ca05bb
		} else {
dfc080
			mapaddr = secaddr;
ca05bb
		}
dfc080
9faf9a
		/* lock */
9faf9a
		hlock = &(__ntapi_internals()->hlock);
9faf9a
97b89a
		while (at_locked_cas(hlock,0,1))
97b89a
			__ntapi->zw_delay_execution(
97b89a
				NT_SYNC_NON_ALERTABLE,
97b89a
				&(nt_timeout){{0,0}});
9faf9a
	}
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_QUERY;
9faf9a
9faf9a
	msg.data.seminfo.semcmd		= sem_ipc_cmd;
9faf9a
	msg.data.seminfo.semkey		= sem->semkey;
9faf9a
	msg.data.seminfo.semid		= sem->semid;
9faf9a
	msg.data.seminfo.semnum		= sem->semnum;
9faf9a
	msg.data.seminfo.section_addr	= secaddr;
9faf9a
	msg.data.seminfo.section_size	= secsize;
9faf9a
9faf9a
	if ((status = __ntapi->zw_request_wait_reply_port(sem->hport,&msg,&msg)))
dfc080
		return __sem_query_return(mapaddr,hlock,status);
9faf9a
	else if (msg.data.ttyinfo.status)
dfc080
		return __sem_query_return(mapaddr,hlock,msg.data.ttyinfo.status);
9faf9a
9faf9a
	/* reply */
9faf9a
	if (sem_ipc_cmd == NT_SEM_CMD_GETALL) {
9faf9a
		if (msg.data.seminfo.section_size > sem_info_length)
9faf9a
			return __sem_query_return(
dfc080
				mapaddr,hlock,
14cb5f
				NT_STATUS_BUFFER_TOO_SMALL);
9faf9a
9faf9a
		__ntapi->tt_generic_memcpy(
9faf9a
			sem_info,secaddr,
9faf9a
			msg.data.seminfo.section_size);
9faf9a
9faf9a
		at_store(hlock,0);
9faf9a
9faf9a
		iosb->status = NT_STATUS_SUCCESS;
9faf9a
		iosb->info   = msg.data.seminfo.section_size;
dfc080
dfc080
		if (mapaddr)
dfc080
			__ntapi->zw_unmap_view_of_section(
dfc080
				NT_CURRENT_PROCESS_HANDLE,
dfc080
				mapaddr);
9faf9a
	} else {
9faf9a
		if (msg.header.data_size != sizeof(msg.data))
9faf9a
			return NT_STATUS_UNEXPECTED_IO_ERROR;
9faf9a
9faf9a
		__ntapi->tt_generic_memcpy(
9faf9a
			sem_info,
9faf9a
			&msg.data.seminfo,
9faf9a
			sizeof(msg.data.seminfo));
9faf9a
9faf9a
		iosb->status = NT_STATUS_SUCCESS;
9faf9a
		iosb->info   = sizeof(msg.data.seminfo);
9faf9a
	}
9faf9a
9faf9a
	return NT_STATUS_SUCCESS;
9faf9a
}