Blame src/pty/ptyc_spawn.c

9cd8e5
/*********************************************************/
9cd8e5
/*  ptycon: a pty-console bridge                         */
9cd8e5
/*  Copyright (C) 2016  Z. Gilboa                        */
9cd8e5
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.  */
9cd8e5
/*********************************************************/
9cd8e5
9cd8e5
#include <psxtypes/psxtypes.h>
12bd9a
#include <pemagine/pemagine.h>
12bd9a
#include <ntapi/nt_status.h>
12bd9a
#include <ntapi/nt_object.h>
12bd9a
#include <ntapi/nt_thread.h>
12bd9a
#include <ntapi/nt_process.h>
12bd9a
#include <ntapi/nt_string.h>
9cd8e5
#include <ntapi/ntapi.h>
9cd8e5
9cd8e5
#include <ptycon/ptycon.h>
9cd8e5
#include "ptycon_driver_impl.h"
9cd8e5
9cd8e5
int __stdcall ptyc_spawn(struct ptyc_driver_ctx * dctx)
9cd8e5
{
9cd8e5
	int32_t				status;
12bd9a
	nt_spawn_process_params		sparams;
12bd9a
	nt_runtime_data			rtctx;
9cd8e5
	struct ptyc_driver_ctx_impl *	ictx;
9cd8e5
	struct ptyc_client_ctx *	clctx;
9cd8e5
	nt_rtdata *			self;
9cd8e5
9cd8e5
	/* init */
9cd8e5
	if (!(ictx = ptyc_get_driver_ictx(dctx)))
9cd8e5
		return NT_STATUS_INVALID_HANDLE;
9cd8e5
9cd8e5
	if (!dctx->cctx->eargv)
9cd8e5
		return NT_STATUS_SUCCESS;
9cd8e5
c26174
	self  = ictx->rtdata;
9cd8e5
	clctx = &ictx->clctx;
9cd8e5
12bd9a
	/* sparams */
9cd8e5
	ntapi->tt_aligned_block_memset(
12bd9a
		&sparams,0,sizeof(sparams));
9cd8e5
12bd9a
	sparams.rtctx    = &rtctx;
12bd9a
	sparams.patharg  = dctx->cctx->eargv[0];
12bd9a
	sparams.argv     = dctx->cctx->eargv;
12bd9a
	sparams.envp     = self->envp;
12bd9a
	sparams.hsession = self->hsession;
9cd8e5
12bd9a
	/* rtctx */
12bd9a
	ntapi->tt_aligned_block_memset(
12bd9a
		&rtctx,0,sizeof(rtctx));
9cd8e5
9cd8e5
	ntapi->tt_aligned_block_memcpy(
12bd9a
		(uintptr_t *)&rtctx.cid_parent,
9cd8e5
		(uintptr_t *)&self->cid_self,
9cd8e5
		sizeof(nt_cid));
9cd8e5
12bd9a
	rtctx.hroot = dctx->cctx->hroot;
12bd9a
	rtctx.hcwd  = self->hcwd;
9cd8e5
12bd9a
	rtctx.srv_keys[0] = self->srv_keys[0];
12bd9a
	rtctx.srv_keys[1] = self->srv_keys[1];
12bd9a
	rtctx.srv_keys[2] = self->srv_keys[2];
12bd9a
	rtctx.srv_keys[3] = self->srv_keys[3];
12bd9a
	rtctx.srv_keys[4] = self->srv_keys[4];
12bd9a
	rtctx.srv_keys[5] = self->srv_keys[5];
9cd8e5
12bd9a
	rtctx.hstdin      = NT_INVALID_HANDLE_VALUE;
12bd9a
	rtctx.hstdout     = NT_INVALID_HANDLE_VALUE;
12bd9a
	rtctx.hstderr     = NT_INVALID_HANDLE_VALUE;
9cd8e5
12bd9a
	rtctx.stdin_type  = NT_FILE_TYPE_PTY;
12bd9a
	rtctx.stdout_type = NT_FILE_TYPE_PTY;
12bd9a
	rtctx.stderr_type = NT_FILE_TYPE_PTY;
9cd8e5
12bd9a
	rtctx.ptyany[0]   = clctx->clinfo.any[0];
12bd9a
	rtctx.ptyany[1]   = clctx->clinfo.any[1];
12bd9a
	rtctx.ptyany[2]   = clctx->clinfo.any[2];
12bd9a
	rtctx.ptyany[3]   = clctx->clinfo.any[3];
9cd8e5
9cd8e5
	/* hoppla */
12bd9a
	if ((status = ntapi->tt_spawn_native_process(&sparams)))
12bd9a
		return status;
9cd8e5
9cd8e5
	/* clctx */
12bd9a
	clctx->hprocess		= sparams.hprocess;
12bd9a
	clctx->hthread		= sparams.hthread;
12bd9a
	clctx->cid.process_id	= sparams.cid.process_id;
12bd9a
	clctx->cid.thread_id	= sparams.cid.thread_id;
12bd9a
12bd9a
	/* child ready? */
12bd9a
	if (!(sparams.eready.signal_state))
12bd9a
		return NT_STATUS_GENERIC_COMMAND_FAILED;
12bd9a
12bd9a
	/* finalize */
12bd9a
	if (!(dctx->cctx->drvflags & (PTYC_DRIVER_DBG_RAW|PTYC_DRIVER_DBG_OVEN)))
12bd9a
		if ((status = ntapi->pty_close(ictx->cctx.hpts)))
12bd9a
			return status;
12bd9a
12bd9a
	/* all done */
12bd9a
	return NT_STATUS_SUCCESS;
9cd8e5
}