Blame src/sem/ntapi_sem_query.c

9faf9a
/********************************************************/
9faf9a
/*  ntapi: Native API core library                      */
9faf9a
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
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(
9faf9a
	intptr_t *	hlock,
14cb5f
	int32_t		status)
9faf9a
{
14cb5f
	if (hlock)
9faf9a
		at_store(hlock,0);
9faf9a
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;
9faf9a
	void *			hsection;
9faf9a
	void *			secaddr;
9faf9a
	size_t			secsize;
9faf9a
	nt_sem_info_msg		msg;
9faf9a
	intptr_t *		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;
9faf9a
9faf9a
		} else if ((status = __ntapi->ipc_init_section_by_port(
9faf9a
				sem->hport,&hsection,
9faf9a
				&secaddr,&secsize)))
9faf9a
			return status;
9faf9a
9faf9a
		/* lock */
9faf9a
		hlock = &(__ntapi_internals()->hlock);
9faf9a
9faf9a
		if (at_locked_cas(hlock,0,1))
9faf9a
			return NT_STATUS_RESOURCE_NOT_OWNED;
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)))
14cb5f
		return __sem_query_return(hlock,status);
9faf9a
	else if (msg.data.ttyinfo.status)
14cb5f
		return __sem_query_return(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(
9faf9a
				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;
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
}