Blame src/pty/ptyc_pty_ctx.c

538c8a
/*********************************************************/
538c8a
/*  ptycon: a pty-console bridge                         */
538c8a
/*  Copyright (C) 2016  Z. Gilboa                        */
538c8a
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.  */
538c8a
/*********************************************************/
538c8a
538c8a
#include <psxtypes/psxtypes.h>
538c8a
#include <ntapi/ntapi.h>
421963
#include <ntapi/nt_termios.h>
538c8a
#include <ptycon/ptycon.h>
538c8a
#include "ptycon_driver_impl.h"
538c8a
#include "ptycon_ioctl_impl.h"
538c8a
#include "ptycon_status_impl.h"
538c8a
538c8a
static int32_t ptyc_pty_open(nt_pty * hptm, nt_guid * pty_guid, nt_pty ** hpty)
538c8a
{
538c8a
	nt_vfd_dev_name		pty_name;
538c8a
	nt_oa			oa = {sizeof(oa),0,0,0,0,0};
538c8a
538c8a
	ntapi->vfd_dev_name_init(
538c8a
		&pty_name,pty_guid);
538c8a
538c8a
	oa.obj_name = &pty_name.name;
538c8a
	oa.root_dir = hptm;
538c8a
538c8a
	return ntapi->pty_open(
538c8a
		0,hpty,
538c8a
		NT_FILE_ALL_ACCESS,
538c8a
		&oa,0,0,0);
538c8a
}
538c8a
9b8e64
static int32_t ptyc_ptm_open(struct ptyc_common_ctx * cctx)
538c8a
{
538c8a
	nt_guid pty_guid = TTY_PTM_GUID;
9b8e64
538c8a
	return ptyc_pty_open(
538c8a
		0,&pty_guid,
9b8e64
		&cctx->hptm);
538c8a
}
538c8a
9b8e64
static int32_t ptyc_pts_open(struct ptyc_common_ctx * cctx)
538c8a
{
538c8a
	int32_t status;
538c8a
	nt_guid pty_guid = TTY_PTS_GUID;
538c8a
538c8a
	if ((status = ptyc_grant(cctx->hptm)))
538c8a
		return status;
538c8a
538c8a
	return ptyc_pty_open(
538c8a
		cctx->hptm,&pty_guid,
9b8e64
		&cctx->hpts);
538c8a
}
538c8a
421963
static int32_t ptyc_make_raw(nt_pty * hpty)
421963
{
421963
	int32_t			status;
421963
	nt_iosb			iosb;
421963
	nt_tty_sigctl_info	ctlinfo;
421963
	struct tty_termios	termios;
421963
421963
	ntapi->tt_aligned_block_memset(
421963
		&ctlinfo,0,sizeof(ctlinfo));
421963
421963
	if ((status = ntapi->pty_ioctl(
421963
			hpty,
421963
			0,0,0,
421963
			&iosb,
421963
			TTY_TCGETS,
421963
			&ctlinfo,sizeof(ctlinfo),
421963
			&ctlinfo,sizeof(ctlinfo))))
421963
		return status;
421963
421963
	ntapi->tt_generic_memcpy(
421963
		&termios,
421963
		&ctlinfo.terminfo,
421963
		sizeof(termios));
421963
421963
	termios.c_oflag &= ~TTY_OPOST;
421963
421963
	termios.c_cflag &= ~(TTY_CSIZE | TTY_PARENB);
421963
	termios.c_cflag |= TTY_CS8;
421963
421963
	termios.c_lflag &= ~(TTY_ECHO
421963
				| TTY_ECHONL
421963
				| TTY_ICANON
421963
				| TTY_ISIG
421963
				| TTY_IEXTEN);
421963
421963
	termios.c_iflag &= ~(TTY_IGNBRK
421963
				| TTY_BRKINT
421963
				| TTY_PARMRK
421963
				| TTY_ISTRIP
421963
				| TTY_INLCR
421963
				| TTY_IGNCR
421963
				| TTY_ICRNL
421963
				| TTY_IXON);
421963
421963
	ntapi->tt_aligned_block_memset(
421963
		&ctlinfo,0,sizeof(ctlinfo));
421963
421963
	ntapi->tt_generic_memcpy(
421963
		&ctlinfo.terminfo,
421963
		&termios,
421963
		sizeof(termios));
421963
421963
	return ntapi->pty_ioctl(
421963
		hpty,
421963
		0,0,0,
421963
		&iosb,
421963
		TTY_TCSETS,
421963
		&ctlinfo,sizeof(ctlinfo),
421963
		&ctlinfo,sizeof(ctlinfo));
421963
}
421963
538c8a
int ptyc_alloc_pty(struct ptyc_driver_ctx * dctx)
538c8a
{
9b8e64
	int32_t				status;
9b8e64
	struct ptyc_driver_ctx_impl *	ictx;
9b8e64
	struct ptyc_common_ctx *	cctx;
9b8e64
9b8e64
	if (!(ictx = ptyc_get_driver_ictx(dctx)))
9b8e64
		return NT_STATUS_INVALID_HANDLE;
9b8e64
9b8e64
	cctx = &ictx->cctx;
538c8a
9b8e64
	if (cctx->hpts || cctx->hptm)
538c8a
		return ptyc_set_status(
538c8a
			dctx,NT_STATUS_DEVICE_ALREADY_ATTACHED);
538c8a
9b8e64
	if ((status = ptyc_ptm_open(cctx)))
538c8a
		return ptyc_set_status(
538c8a
			dctx,status);
538c8a
421963
	if ((status = ptyc_make_raw(cctx->hptm)))
421963
		return ptyc_set_status(
421963
			dctx,status);
421963
2de078
	if (!dctx->units[0]
2de078
			&& !(cctx->drvflags & PTYC_DRIVER_DBG_OVEN)
9b8e64
			&& !(cctx->drvflags & PTYC_DRIVER_DBG_RAW))
538c8a
		return ptyc_set_status(
538c8a
			dctx,NT_STATUS_SUCCESS);
538c8a
9b8e64
	if ((status = ptyc_pts_open(cctx)))
538c8a
		return ptyc_set_status(
538c8a
			dctx,status);
538c8a
538c8a
	return ptyc_set_status(
538c8a
		dctx,NT_STATUS_SUCCESS);
538c8a
}
538c8a
538c8a
void ptyc_free_pty(struct ptyc_driver_ctx * dctx)
538c8a
{
9b8e64
	struct ptyc_driver_ctx_impl * ictx;
538c8a
9b8e64
	if (!(ictx = ptyc_get_driver_ictx(dctx)))
9b8e64
		return;
538c8a
9b8e64
	if (ictx->cctx.hpts) {
9b8e64
		ntapi->pty_close(ictx->cctx.hpts);
9b8e64
		ictx->cctx.hpts = 0;
538c8a
	}
538c8a
9b8e64
	if (ictx->cctx.hptm) {
9b8e64
		ntapi->pty_close(ictx->cctx.hptm);
9b8e64
		ictx->cctx.hptm = 0;
538c8a
	}
538c8a
}