Blob Blame History Raw
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2021  SysDeer Technologies, LLC */
/*  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_set_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_sem_set(
	__in	nt_sem_info *		sem,
	__out	nt_io_status_block *	iosb,
	__in	void *			sem_info,
	__in	uint32_t		sem_info_length,
	__in	int32_t			sem_ipc_cmd)
{
	int32_t			status;
	void *			mapaddr;
	void *			hsection;
	void *			secaddr;
	size_t			secsize;
	nt_sem_info_msg		msg;
	intptr_t volatile *	hlock;

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

	else if (!sem_info)
		return NT_STATUS_INVALID_PARAMETER;

	else if (!sem_info_length)
		return NT_STATUS_INVALID_PARAMETER;

	else if (sem_ipc_cmd != NT_SEM_CMD_SETALL)
		if (sem_info_length != sizeof(nt_sem_info))
			return NT_STATUS_INFO_LENGTH_MISMATCH;

	/* init */
	hlock = 0;

	/* section */
	hsection = 0;
	secaddr  = 0;
	secsize  = 0;

	/* SETALL */
	if (sem_ipc_cmd == NT_SEM_CMD_SETALL) {
		if (sem->section_addr) {
			hsection = sem->section;
			secaddr  = sem->section_addr;
			secsize  = sem->section_size;
			mapaddr  = 0;

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

		} else {
			mapaddr = secaddr;
		}

		/* data size */
		if (secsize < sem_info_length)
			return __sem_set_return(
				mapaddr,0,
				NT_STATUS_DATA_OVERRUN);

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

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

		/* data copy */
		__ntapi->tt_generic_memcpy(
			secaddr,sem_info,
			sem_info_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_SET;

	msg.data.seminfo.semcmd		= sem_ipc_cmd;
	msg.data.seminfo.semkey		= sem->semkey;
	msg.data.seminfo.semid		= sem->semid;
	msg.data.seminfo.semnum		= sem->semnum;
	msg.data.seminfo.semval		= sem->semval;
	msg.data.seminfo.section_addr	= secaddr;
	msg.data.seminfo.section_size	= sem_info_length;

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

	/* reply */
	iosb->status = NT_STATUS_SUCCESS;
	iosb->info   = 0;

	return __sem_set_return(mapaddr,hlock,NT_STATUS_SUCCESS);
}