|
|
47f21a |
/********************************************************/
|
|
|
47f21a |
/* ntapi: Native API core library */
|
|
|
64e606 |
/* Copyright (C) 2013--2021 SysDeer Technologies, LLC */
|
|
|
47f21a |
/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
|
|
|
47f21a |
/********************************************************/
|
|
|
47f21a |
|
|
|
47f21a |
#include <psxtypes/psxtypes.h>
|
|
|
47f21a |
#include <ntapi/nt_file.h>
|
|
|
47f21a |
#include <ntapi/nt_string.h>
|
|
|
47f21a |
#include <ntapi/nt_atomic.h>
|
|
|
47f21a |
#include <ntapi/nt_port.h>
|
|
|
47f21a |
#include <ntapi/nt_ipc.h>
|
|
|
47f21a |
#include <ntapi/nt_msq.h>
|
|
|
47f21a |
#include <ntapi/ntapi.h>
|
|
|
47f21a |
#include "ntapi_impl.h"
|
|
|
47f21a |
|
|
|
47f21a |
static const nt_guid g_msqpid = NT_IPC_GUID_MSQPID;
|
|
|
47f21a |
static const wchar16_t p_msqpid[6] = NT_IPC_OBJDIR_PREFIX_MSQPID;
|
|
|
47f21a |
|
|
|
47f21a |
static int32_t __msqctl_get_service_attr(
|
|
|
47f21a |
nt_rtdata * rtdata,
|
|
|
47f21a |
nt_tty_service_info * msqctl)
|
|
|
47f21a |
{
|
|
|
47f21a |
nt_iosb iosb;
|
|
|
47f21a |
|
|
|
47f21a |
/* inherited runtime data? */
|
|
|
47f21a |
if (rtdata->msqctl_keys[0]) {
|
|
|
47f21a |
msqctl->attr.ver_major = 0;
|
|
|
47f21a |
msqctl->attr.ver_minor = 0;
|
|
|
47f21a |
msqctl->attr.options = 0;
|
|
|
47f21a |
msqctl->attr.flags = 0;
|
|
|
47f21a |
|
|
|
47f21a |
msqctl->attr.type = rtdata->msqctl_type;
|
|
|
47f21a |
msqctl->attr.subtype = rtdata->msqctl_subtype;
|
|
|
47f21a |
|
|
|
47f21a |
msqctl->attr.keys.key[0] = rtdata->msqctl_keys[0];
|
|
|
47f21a |
msqctl->attr.keys.key[1] = rtdata->msqctl_keys[1];
|
|
|
47f21a |
msqctl->attr.keys.key[2] = rtdata->msqctl_keys[2];
|
|
|
47f21a |
msqctl->attr.keys.key[3] = rtdata->msqctl_keys[3];
|
|
|
47f21a |
msqctl->attr.keys.key[4] = rtdata->msqctl_keys[4];
|
|
|
47f21a |
msqctl->attr.keys.key[5] = rtdata->msqctl_keys[5];
|
|
|
47f21a |
|
|
|
47f21a |
__ntapi->tt_guid_copy(
|
|
|
47f21a |
&msqctl->attr.guid,
|
|
|
47f21a |
&rtdata->msqctl_guid);
|
|
|
47f21a |
|
|
|
47f21a |
return NT_STATUS_SUCCESS;
|
|
|
47f21a |
}
|
|
|
47f21a |
|
|
|
47f21a |
/* obtain service info */
|
|
|
47f21a |
return __ntapi->tty_query_information_service(
|
|
|
47f21a |
0,&iosb,msqctl,
|
|
|
47f21a |
&(nt_guid)NT_PORT_GUID_MSQCTL,
|
|
|
47f21a |
0,0);
|
|
|
47f21a |
}
|
|
|
47f21a |
|
|
|
47f21a |
static int32_t __msqctl_server_connect(
|
|
|
47f21a |
nt_rtdata * rtdata,
|
|
|
47f21a |
nt_tty_service_info * msqctl)
|
|
|
47f21a |
{
|
|
|
47f21a |
int32_t status;
|
|
|
47f21a |
|
|
|
47f21a |
/* already cononected? */
|
|
|
47f21a |
if (rtdata->hmsqctl)
|
|
|
47f21a |
return NT_STATUS_SUCCESS;
|
|
|
47f21a |
|
|
|
47f21a |
/* connect */
|
|
|
47f21a |
if ((status = __ntapi->ipc_connect_by_attr(
|
|
|
47f21a |
&rtdata->hmsqctl,
|
|
|
1717a8 |
&msqctl->attr,
|
|
|
1717a8 |
false)))
|
|
|
47f21a |
return status;
|
|
|
47f21a |
|
|
|
47f21a |
/* update */
|
|
|
47f21a |
rtdata->msqctl_type = msqctl->attr.type;
|
|
|
47f21a |
rtdata->msqctl_subtype = msqctl->attr.subtype;
|
|
|
47f21a |
|
|
|
47f21a |
rtdata->msqctl_keys[0] = msqctl->attr.keys.key[0];
|
|
|
47f21a |
rtdata->msqctl_keys[1] = msqctl->attr.keys.key[1];
|
|
|
47f21a |
rtdata->msqctl_keys[2] = msqctl->attr.keys.key[2];
|
|
|
47f21a |
rtdata->msqctl_keys[3] = msqctl->attr.keys.key[3];
|
|
|
47f21a |
rtdata->msqctl_keys[4] = msqctl->attr.keys.key[4];
|
|
|
47f21a |
rtdata->msqctl_keys[5] = msqctl->attr.keys.key[5];
|
|
|
47f21a |
|
|
|
47f21a |
__ntapi->tt_guid_copy(
|
|
|
47f21a |
&rtdata->msqctl_guid,
|
|
|
47f21a |
&msqctl->attr.guid);
|
|
|
47f21a |
|
|
|
47f21a |
return NT_STATUS_SUCCESS;
|
|
|
47f21a |
}
|
|
|
47f21a |
|
|
|
47f21a |
static int32_t __msqpid_symlink_set(
|
|
|
47f21a |
nt_rtdata * rtdata,
|
|
|
47f21a |
nt_tty_service_info * msqctl)
|
|
|
47f21a |
{
|
|
|
47f21a |
int32_t status;
|
|
|
47f21a |
void * hpiddir;
|
|
|
47f21a |
nt_port_name svcname;
|
|
|
47f21a |
nt_unicode_string str;
|
|
|
47f21a |
|
|
|
47f21a |
if (rtdata->hmsqpid)
|
|
|
47f21a |
return NT_STATUS_SUCCESS;
|
|
|
47f21a |
|
|
|
47f21a |
if (!rtdata->hmsqpiddir) {
|
|
|
47f21a |
if ((status = __ntapi->tt_open_ipc_object_directory(
|
|
|
47f21a |
&hpiddir,
|
|
|
9a95bf |
NT_SEC_READ_CONTROL
|
|
|
9a95bf |
| NT_DIRECTORY_QUERY
|
|
|
9a95bf |
| NT_DIRECTORY_TRAVERSE
|
|
|
9a95bf |
| NT_DIRECTORY_CREATE_OBJECT
|
|
|
9a95bf |
| NT_DIRECTORY_CREATE_SUBDIRECTORY,
|
|
|
47f21a |
p_msqpid,&g_msqpid)))
|
|
|
47f21a |
return status;
|
|
|
47f21a |
|
|
|
47f21a |
if (at_locked_cas((intptr_t *)&rtdata->hmsqpiddir,0,(intptr_t)hpiddir))
|
|
|
47f21a |
__ntapi->zw_close(hpiddir);
|
|
|
47f21a |
}
|
|
|
47f21a |
|
|
|
47f21a |
__ntapi->tt_port_name_from_attr(
|
|
|
47f21a |
&svcname,&msqctl->attr);
|
|
|
47f21a |
|
|
|
a73815 |
str.strlen = (uint16_t)(__offsetof(nt_port_name,null_termination));
|
|
|
47f21a |
str.maxlen = sizeof(nt_port_name);
|
|
|
47f21a |
str.buffer = svcname.base_named_objects;
|
|
|
47f21a |
|
|
|
47f21a |
return __ntapi->tt_create_ipc_object_directory_entry(
|
|
|
47f21a |
&rtdata->hmsqpid,
|
|
|
47f21a |
NT_SEC_STANDARD_RIGHTS_ALL,
|
|
|
47f21a |
rtdata->hmsqpiddir,
|
|
|
47f21a |
0,&str,
|
|
|
47f21a |
pe_get_current_process_id());
|
|
|
47f21a |
|
|
|
47f21a |
}
|
|
|
47f21a |
|
|
|
47f21a |
static int32_t __stdcall __msq_open(
|
|
|
47f21a |
void * hipc,
|
|
|
47f21a |
nt_msq_info * msq,
|
|
|
47f21a |
uint32_t access,
|
|
|
47f21a |
nt_object_attributes * oa,
|
|
|
47f21a |
nt_iosb * iosb,
|
|
|
47f21a |
uint32_t share,
|
|
|
47f21a |
uint32_t msqslots,
|
|
|
47f21a |
uint32_t key,
|
|
|
47f21a |
uint32_t id,
|
|
|
47f21a |
uint32_t opcode)
|
|
|
47f21a |
{
|
|
|
47f21a |
int32_t status;
|
|
|
47f21a |
nt_tty_port_msg msg;
|
|
|
47f21a |
nt_iosb siosb;
|
|
|
47f21a |
nt_tty_service_info msqctl;
|
|
|
47f21a |
nt_runtime_data * rtdata;
|
|
|
47f21a |
|
|
|
47f21a |
/* init */
|
|
|
47f21a |
rtdata = (__ntapi_internals())->rtdata;
|
|
|
47f21a |
|
|
|
47f21a |
/* msqctl service attributes */
|
|
|
47f21a |
if (!rtdata->hmsqpid)
|
|
|
47f21a |
if ((status = __msqctl_get_service_attr(rtdata,&msqctl)))
|
|
|
47f21a |
return status;
|
|
|
47f21a |
|
|
|
47f21a |
/* msqctl server */
|
|
|
47f21a |
if ((status = __msqctl_server_connect(rtdata,&msqctl)))
|
|
|
47f21a |
return status;
|
|
|
47f21a |
|
|
|
47f21a |
/* msqpid symlink */
|
|
|
47f21a |
if ((status = __msqpid_symlink_set(rtdata,&msqctl)))
|
|
|
47f21a |
return status;
|
|
|
47f21a |
|
|
|
47f21a |
/* hipc */
|
|
|
47f21a |
if (!hipc && (opcode == NT_TTY_MSQ_ALLOC))
|
|
|
47f21a |
hipc = (__ntapi_internals())->rtdata->hmsqctl;
|
|
|
47f21a |
|
|
|
47f21a |
/* obtain msq info */
|
|
|
47f21a |
__ntapi->tt_aligned_block_memset(
|
|
|
47f21a |
&msg,0,sizeof(msg));
|
|
|
47f21a |
|
|
|
47f21a |
if (!iosb)
|
|
|
47f21a |
iosb = &siosb;
|
|
|
47f21a |
|
|
|
47f21a |
msg.header.msg_type = NT_LPC_NEW_MESSAGE;
|
|
|
47f21a |
msg.header.data_size = sizeof(nt_msq_info_msg) - sizeof(msg.header);
|
|
|
47f21a |
msg.header.msg_size = sizeof(msg);
|
|
|
47f21a |
msg.ttyinfo.opcode = opcode;
|
|
|
47f21a |
|
|
|
47f21a |
msg.msqinfo.msqkey = (int32_t)key;
|
|
|
47f21a |
msg.msqinfo.msqid = (int32_t)id;
|
|
|
47f21a |
msg.msqinfo.msqslots = msqslots;
|
|
|
47f21a |
|
|
|
47f21a |
msg.msqinfo.ntattr = oa->obj_attr;
|
|
|
47f21a |
msg.msqinfo.ntaccess = access;
|
|
|
47f21a |
msg.msqinfo.ntshare = share;
|
|
|
47f21a |
|
|
|
47f21a |
if ((status = __ntapi->zw_request_wait_reply_port(hipc,&msg,&msg)))
|
|
|
47f21a |
return status;
|
|
|
47f21a |
else if (msg.ttyinfo.status)
|
|
|
47f21a |
return msg.ttyinfo.status;
|
|
|
47f21a |
|
|
|
47f21a |
iosb->info = sizeof(msg.svcinfo);
|
|
|
47f21a |
iosb->status = NT_STATUS_SUCCESS;
|
|
|
47f21a |
|
|
|
47f21a |
/* new msqueue? */
|
|
|
47f21a |
if (opcode == NT_TTY_MSQ_ALLOC)
|
|
|
47f21a |
if ((status = __ntapi->ipc_connect_by_attr(
|
|
|
1717a8 |
&hipc,&msg.svcinfo.attr,
|
|
|
1717a8 |
false)))
|
|
|
47f21a |
return status;
|
|
|
47f21a |
|
|
|
47f21a |
/* all done */
|
|
|
47f21a |
__ntapi->tt_aligned_block_memset(
|
|
|
47f21a |
(uintptr_t *)msq,
|
|
|
47f21a |
0,sizeof(*msq));
|
|
|
47f21a |
|
|
|
47f21a |
msq->msqkey = msg.svcinfo.key;
|
|
|
47f21a |
msq->msqid = msg.svcinfo.id;
|
|
|
47f21a |
|
|
|
47f21a |
msq->hport = hipc;
|
|
|
47f21a |
|
|
|
47f21a |
return NT_STATUS_SUCCESS;
|
|
|
47f21a |
}
|
|
|
47f21a |
|
|
|
47f21a |
|
|
|
e3eea2 |
int32_t __stdcall __ntapi_msq_create(
|
|
|
47f21a |
__in void * hport,
|
|
|
47f21a |
__out nt_msq_info * msq,
|
|
|
47f21a |
__in uint32_t access,
|
|
|
47f21a |
__in nt_object_attributes * oa,
|
|
|
47f21a |
__out nt_iosb * iosb,
|
|
|
47f21a |
__in uint32_t share,
|
|
|
47f21a |
__in uint32_t nslots)
|
|
|
47f21a |
{
|
|
|
47f21a |
uint32_t key;
|
|
|
47f21a |
|
|
|
47f21a |
/* validate */
|
|
|
47f21a |
if (!oa->root_dir)
|
|
|
47f21a |
return NT_STATUS_DIRECTORY_SERVICE_REQUIRED;
|
|
|
47f21a |
|
|
|
47f21a |
if (oa->obj_name && !oa->obj_name->strlen) {
|
|
|
47f21a |
key = 0;
|
|
|
47f21a |
|
|
|
47f21a |
} else if (oa->obj_name) {
|
|
|
47f21a |
if (oa->obj_name->strlen != 8 * sizeof(wchar16_t))
|
|
|
47f21a |
return NT_STATUS_OBJECT_NAME_INVALID;
|
|
|
47f21a |
|
|
|
47f21a |
if (__ntapi->tt_hex_utf16_to_uint32(oa->obj_name->buffer,&key))
|
|
|
47f21a |
return NT_STATUS_OBJECT_NAME_INVALID;
|
|
|
47f21a |
|
|
|
47f21a |
} else {
|
|
|
47f21a |
key = 0;
|
|
|
47f21a |
}
|
|
|
47f21a |
|
|
|
47f21a |
/* open msqueue */
|
|
|
47f21a |
return __msq_open(
|
|
|
47f21a |
hport,msq,access,
|
|
|
47f21a |
oa,iosb,share,nslots,
|
|
|
47f21a |
key,0,NT_TTY_MSQ_ALLOC);
|
|
|
47f21a |
}
|
|
|
47f21a |
|
|
|
47f21a |
|
|
|
47f21a |
int32_t __stdcall __ntapi_msq_open(
|
|
|
47f21a |
__in void * hport,
|
|
|
47f21a |
__out nt_msq_info * msq,
|
|
|
47f21a |
__in uint32_t access,
|
|
|
47f21a |
__in nt_object_attributes * oa,
|
|
|
47f21a |
__out nt_iosb * iosb,
|
|
|
47f21a |
__in uint32_t share,
|
|
|
47f21a |
__in uint32_t nslots)
|
|
|
47f21a |
{
|
|
|
47f21a |
int32_t status;
|
|
|
47f21a |
uint32_t key;
|
|
|
47f21a |
uint32_t id;
|
|
|
47f21a |
void * hsymlink;
|
|
|
47f21a |
nt_oa ipcoa;
|
|
|
47f21a |
void * hipc;
|
|
|
47f21a |
nt_rtdata * rtdata;
|
|
|
47f21a |
nt_tty_service_info msqctl;
|
|
|
47f21a |
|
|
|
47f21a |
/* init */
|
|
|
47f21a |
rtdata = (__ntapi_internals())->rtdata;
|
|
|
47f21a |
|
|
|
47f21a |
/* validate */
|
|
|
47f21a |
if (!oa->root_dir)
|
|
|
47f21a |
return NT_STATUS_DIRECTORY_SERVICE_REQUIRED;
|
|
|
47f21a |
|
|
|
47f21a |
if (!oa->obj_name)
|
|
|
47f21a |
return NT_STATUS_INVALID_PARAMETER;
|
|
|
47f21a |
|
|
|
47f21a |
if (oa->obj_name->strlen != 8 * sizeof(wchar16_t))
|
|
|
47f21a |
return NT_STATUS_OBJECT_NAME_INVALID;
|
|
|
47f21a |
|
|
|
47f21a |
if (__ntapi->tt_hex_utf16_to_uint32(oa->obj_name->buffer,&key))
|
|
|
47f21a |
return NT_STATUS_OBJECT_NAME_INVALID;
|
|
|
47f21a |
|
|
|
47f21a |
/* open symlink */
|
|
|
47f21a |
ipcoa.len = sizeof(ipcoa);
|
|
|
47f21a |
ipcoa.root_dir = oa->root_dir;
|
|
|
47f21a |
ipcoa.obj_name = oa->obj_name;
|
|
|
47f21a |
ipcoa.obj_attr = 0;
|
|
|
47f21a |
ipcoa.sec_desc = oa->sec_desc;
|
|
|
47f21a |
ipcoa.sec_qos = oa->sec_qos;
|
|
|
47f21a |
|
|
|
47f21a |
status = __ntapi->zw_open_symbolic_link_object(
|
|
|
47f21a |
&hsymlink,
|
|
|
9a95bf |
NT_SYMBOLIC_LINK_QUERY,
|
|
|
47f21a |
&ipcoa);
|
|
|
47f21a |
|
|
|
47f21a |
switch (status) {
|
|
|
47f21a |
case NT_STATUS_SUCCESS:
|
|
|
47f21a |
break;
|
|
|
47f21a |
|
|
|
47f21a |
case NT_STATUS_OBJECT_NAME_NOT_FOUND:
|
|
|
47f21a |
case NT_STATUS_OBJECT_PATH_NOT_FOUND:
|
|
|
47f21a |
if (oa->obj_attr & NT_OBJ_OPENIF)
|
|
|
47f21a |
return __msq_open(
|
|
|
47f21a |
hport,msq,access,
|
|
|
47f21a |
oa,iosb,share,nslots,
|
|
|
47f21a |
key,0,NT_TTY_MSQ_ALLOC);
|
|
|
47f21a |
else
|
|
|
47f21a |
return status;
|
|
|
47f21a |
|
|
|
47f21a |
default:
|
|
|
47f21a |
return status;
|
|
|
47f21a |
}
|
|
|
47f21a |
|
|
|
47f21a |
/* msqctl service attributes */
|
|
|
47f21a |
if (!rtdata->hmsqpid)
|
|
|
47f21a |
if ((status = __msqctl_get_service_attr(rtdata,&msqctl)))
|
|
|
47f21a |
return status;
|
|
|
47f21a |
|
|
|
47f21a |
/* msqctl server */
|
|
|
47f21a |
if ((status = __msqctl_server_connect(rtdata,&msqctl)))
|
|
|
47f21a |
return status;
|
|
|
47f21a |
|
|
|
47f21a |
/* msqpid symlink */
|
|
|
47f21a |
if ((status = __msqpid_symlink_set(rtdata,&msqctl)))
|
|
|
47f21a |
return status;
|
|
|
47f21a |
|
|
|
47f21a |
/* ipc connect */
|
|
|
47f21a |
status = __ntapi->ipc_connect_by_symlink(
|
|
|
1717a8 |
&hipc,hsymlink,false);
|
|
|
47f21a |
|
|
|
47f21a |
__ntapi->zw_close(
|
|
|
47f21a |
hsymlink);
|
|
|
47f21a |
|
|
|
47f21a |
if (status)
|
|
|
47f21a |
return status;
|
|
|
47f21a |
|
|
|
47f21a |
/* open by id? */
|
|
|
47f21a |
if (oa->obj_attr & NT_OBJ_OPENLINK) {
|
|
|
47f21a |
id = key;
|
|
|
47f21a |
key = 0;
|
|
|
47f21a |
} else {
|
|
|
47f21a |
id = 0;
|
|
|
47f21a |
}
|
|
|
47f21a |
|
|
|
47f21a |
return __msq_open(
|
|
|
47f21a |
hipc,msq,access,
|
|
|
47f21a |
oa,iosb,share,nslots,
|
|
|
47f21a |
key,id,NT_TTY_MSQ_OPEN);
|
|
|
47f21a |
}
|