Blame src/process/ntapi_tt_init_runtime_data.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
64e606
/*  Copyright (C) 2013--2021  SysDeer Technologies, LLC */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/nt_memory.h>
dd89bb
#include <ntapi/nt_process.h>
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_update_runtime_data(nt_runtime_data * rtdata)
dd89bb
{
dd89bb
	int32_t				status;
dd89bb
	nt_process_basic_information	pbi;
dd89bb
	uint32_t			ret;
1219be
	nt_oa				oa = {sizeof(oa),0,0,0,0,0};
dd89bb
dd89bb
	/* process (self) */
dd89bb
	rtdata->cid_self.process_id = pe_get_current_process_id();
dd89bb
	rtdata->cid_self.thread_id  = 0;
dd89bb
dd89bb
	if ((status = __ntapi->zw_open_process(
dea1aa
			&rtdata->hself,
dd89bb
			NT_PROCESS_ALL_ACCESS,
dd89bb
			&oa,&rtdata->cid_self)))
dd89bb
		return status;
dd89bb
dd89bb
	if (rtdata->cid_parent.process_id)
dd89bb
		return NT_STATUS_SUCCESS;
dd89bb
dd89bb
	/* process (parent) */
dd89bb
	if ((status = __ntapi->zw_query_information_process(
dea1aa
			rtdata->hself,
dd89bb
			NT_PROCESS_BASIC_INFORMATION,
dd89bb
			&pbi,sizeof(pbi),&ret)))
dd89bb
		return status;
dd89bb
dd89bb
	rtdata->cid_parent.process_id = pbi.inherited_from_unique_process_id;
dd89bb
	rtdata->cid_parent.thread_id  = 0;
dea1aa
	rtdata->hparent               = 0;
dd89bb
dd89bb
	return NT_STATUS_SUCCESS;
dd89bb
}
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_init_runtime_data(nt_runtime_data * rtdata)
dd89bb
{
dd89bb
	int32_t				status;
dd89bb
	nt_peb *			peb;
1219be
	nt_oa				oa = {sizeof(oa),0,0,0,0,0};
dd89bb
dd89bb
	/* init */
dd89bb
	__ntapi->tt_aligned_block_memset(rtdata,0,sizeof(*rtdata));
dd89bb
	peb = (nt_peb *)(pe_get_peb_address());
dd89bb
dd89bb
	/* pid (self,parent) */
dd89bb
	if ((status = __ntapi_tt_update_runtime_data(rtdata)))
dd89bb
		return status;
dd89bb
dd89bb
	/* std handles */
dd89bb
	rtdata->hstdin  = peb->process_params->hstdin;
dd89bb
	rtdata->hstdout = peb->process_params->hstdout;
dd89bb
	rtdata->hstderr = peb->process_params->hstderr;
dd89bb
dd89bb
	if (__ntapi->tt_get_file_handle_type(rtdata->hstdin,&rtdata->stdin_type)) {
dd89bb
		rtdata->hstdin = NT_INVALID_HANDLE_VALUE;
dd89bb
		rtdata->stdin_type = 0;
dd89bb
	}
dd89bb
dd89bb
	if (__ntapi->tt_get_file_handle_type(rtdata->hstdout,&rtdata->stdout_type)) {
dd89bb
		rtdata->hstdout = NT_INVALID_HANDLE_VALUE;
dd89bb
		rtdata->stdout_type = 0;
dd89bb
	}
dd89bb
dd89bb
	if (__ntapi->tt_get_file_handle_type(rtdata->hstderr,&rtdata->stderr_type)) {
dd89bb
		rtdata->hstderr = NT_INVALID_HANDLE_VALUE;
dd89bb
		rtdata->stderr_type = 0;
dd89bb
	}
dd89bb
dd89bb
	return 0;
dd89bb
}