Blame src/pty/ptyc_spawn.c

9cd8e5
/*********************************************************/
9cd8e5
/*  ptycon: a pty-console bridge                         */
f25e99
/*  Copyright (C) 2016--2017  SysDeer Technologies, LLC  */
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
09a3eb
int 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;
0e8c10
	sparams.hroot    = dctx->cctx->hroot
0e8c10
				? dctx->cctx->hroot
0e8c10
				: self->hroot;
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.hcwd  = self->hcwd;
0e8c10
	rtctx.hroot = sparams.hroot;
9cd8e5
0fb614
	rtctx.tty_type    = self->tty_type;
0fb614
	rtctx.tty_subtype = self->tty_subtype;
0fb614
0fb614
	rtctx.tty_keys[0] = self->tty_keys[0];
0fb614
	rtctx.tty_keys[1] = self->tty_keys[1];
0fb614
	rtctx.tty_keys[2] = self->tty_keys[2];
0fb614
	rtctx.tty_keys[3] = self->tty_keys[3];
0fb614
	rtctx.tty_keys[4] = self->tty_keys[4];
0fb614
	rtctx.tty_keys[5] = self->tty_keys[5];
0fb614
0fb614
	ntapi->tt_guid_copy(
0fb614
		&rtctx.tty_guid,
0fb614
		&self->tty_guid);
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
2bdcda
	rtctx.ptyin [0]   = clctx->clinfo.any[0];
2bdcda
	rtctx.ptyin [1]   = clctx->clinfo.any[1];
2bdcda
	rtctx.ptyin [2]   = clctx->clinfo.any[2];
2bdcda
	rtctx.ptyin [3]   = clctx->clinfo.any[3];
2bdcda
2bdcda
	rtctx.ptyout[0]   = clctx->clinfo.any[0];
2bdcda
	rtctx.ptyout[1]   = clctx->clinfo.any[1];
2bdcda
	rtctx.ptyout[2]   = clctx->clinfo.any[2];
2bdcda
	rtctx.ptyout[3]   = clctx->clinfo.any[3];
2bdcda
2bdcda
	rtctx.ptyerr[0]   = clctx->clinfo.any[0];
2bdcda
	rtctx.ptyerr[1]   = clctx->clinfo.any[1];
2bdcda
	rtctx.ptyerr[2]   = clctx->clinfo.any[2];
2bdcda
	rtctx.ptyerr[3]   = clctx->clinfo.any[3];
2bdcda
2bdcda
	rtctx.ptyctl[0]   = clctx->clinfo.any[0];
2bdcda
	rtctx.ptyctl[1]   = clctx->clinfo.any[1];
2bdcda
	rtctx.ptyctl[2]   = clctx->clinfo.any[2];
2bdcda
	rtctx.ptyctl[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
}