Blame src/sem/ntapi_sem_set.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_set_return(
ef6024
	void *			mapaddr,
ef6024
	intptr_t volatile *	hlock,
ef6024
	int32_t			status)
9faf9a
{
14cb5f
	if (hlock)
9faf9a
		at_store(hlock,0);
9faf9a
1d4745
	if (mapaddr)
1d4745
		__ntapi->zw_unmap_view_of_section(
1d4745
			NT_CURRENT_PROCESS_HANDLE,
1d4745
			mapaddr);
1d4745
9faf9a
	return status;
9faf9a
}
9faf9a
9faf9a
int32_t	__stdcall __ntapi_sem_set(
9faf9a
	__in	nt_sem_info *		sem,
9faf9a
	__out	nt_io_status_block *	iosb,
9faf9a
	__in	void *			sem_info,
9faf9a
	__in	uint32_t		sem_info_length,
9faf9a
	__in	int32_t			sem_ipc_cmd)
9faf9a
{
9faf9a
	int32_t			status;
1d4745
	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_SETALL)
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
	/* SETALL */
9faf9a
	if (sem_ipc_cmd == NT_SEM_CMD_SETALL) {
9faf9a
		if (sem->section_addr) {
9faf9a
			hsection = sem->section;
9faf9a
			secaddr  = sem->section_addr;
9faf9a
			secsize  = sem->section_size;
1d4745
			mapaddr  = 0;
9faf9a
9faf9a
		} else if ((status = __ntapi->ipc_init_section_by_port(
9faf9a
				sem->hport,&hsection,
3878c0
				&secaddr,&secsize))) {
9faf9a
			return status;
9faf9a
3878c0
		} else {
1d4745
			mapaddr = secaddr;
3878c0
		}
1d4745
9faf9a
		/* data size */
9faf9a
		if (secsize < sem_info_length)
1d4745
			return __sem_set_return(
1d4745
				mapaddr,0,
1d4745
				NT_STATUS_DATA_OVERRUN);
9faf9a
9faf9a
		/* lock */
9faf9a
		hlock = &(__ntapi_internals()->hlock);
9faf9a
37da58
		while (at_locked_cas(hlock,0,1))
37da58
			__ntapi->zw_delay_execution(
37da58
				NT_SYNC_NON_ALERTABLE,
37da58
				&(nt_timeout){{0,0}});
9faf9a
9faf9a
		/* data copy */
9faf9a
		__ntapi->tt_generic_memcpy(
9faf9a
			secaddr,sem_info,
9faf9a
			sem_info_length);
14cb5f
	}
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_SET;
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.semval		= sem->semval;
9faf9a
	msg.data.seminfo.section_addr	= secaddr;
9faf9a
	msg.data.seminfo.section_size	= sem_info_length;
9faf9a
9faf9a
	if ((status = __ntapi->zw_request_wait_reply_port(sem->hport,&msg,&msg)))
1d4745
		return __sem_set_return(mapaddr,hlock,status);
9faf9a
	else if (msg.data.ttyinfo.status)
1d4745
		return __sem_set_return(mapaddr,hlock,msg.data.ttyinfo.status);
9faf9a
9faf9a
	/* reply */
9faf9a
	iosb->status = NT_STATUS_SUCCESS;
9faf9a
	iosb->info   = 0;
9faf9a
1d4745
	return __sem_set_return(mapaddr,hlock,NT_STATUS_SUCCESS);
9faf9a
}