summaryrefslogtreecommitdiffhomepage
path: root/src/internal/ptycon_ioctl_impl.h
blob: d87ff3a7dbd8935b0da5473291fa10aa639e016a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef PTYCON_IOCTL_IMPL_H
#define PTYCON_IOCTL_IMPL_H

#include <ntapi/ntapi.h>

static int32_t ptyc_grant(nt_pty * hptm)
{
	nt_tty_sigctl_info	ctlinfo;
	nt_iosb			iosb;

	ntapi->tt_aligned_block_memset(
		&ctlinfo,0,sizeof(ctlinfo));

	return ntapi->pty_ioctl(
		hptm,
		0,0,0,
		&iosb,TTY_TIOCSPTLCK,
		&ctlinfo,sizeof(ctlinfo),
		&ctlinfo,sizeof(ctlinfo));
}

static int32_t ptyc_set_client_info(
	nt_pty *		hpty,
	nt_pty_client_info *	clinfo)
{
	int32_t			status;
	nt_iosb			iosb;
	nt_large_integer	ticks;
	nt_large_integer	freq;
	nt_pty_client_info	ptyinfo;

	if ((status = ntapi->zw_query_performance_counter(
			&ticks,&freq)))
		return status;

	ptyinfo.any[0] = (ticks.ulow << 19) | (ticks.ulow >> 13);

	if ((status = ntapi->zw_query_performance_counter(
			&ticks,&freq)))
		return status;

	ptyinfo.any[1] = (ticks.ulow << 15) | (ticks.ulow >> 17);

	if ((status = ntapi->zw_query_performance_counter(
			&ticks,&freq)))
		return status;

	ptyinfo.any[1] = (ticks.ulow << 11) | (ticks.ulow >> 21);

	if ((status = ntapi->zw_query_performance_counter(
			&ticks,&freq)))
		return status;

	ptyinfo.any[1] = (ticks.ulow << 17) | (ticks.ulow >> 15);

	if ((status = ntapi->pty_set(
			hpty,&iosb,
			&ptyinfo,sizeof(ptyinfo),
			NT_PTY_CLIENT_INFORMATION)))
		return status;

	clinfo->any[0] = ptyinfo.any[0];
	clinfo->any[1] = ptyinfo.any[1];
	clinfo->any[2] = ptyinfo.any[2];
	clinfo->any[3] = ptyinfo.any[3];

	return NT_STATUS_SUCCESS;
}

static int32_t ptyc_pty_own(
	nt_pty *		hpty,
	nt_runtime_data *	rtdata)
{
	int32_t			status;
	nt_tty_session_info	sessioninfo;
	nt_tty_sigctl_info	ctlinfo;
	nt_pty_client_info	ptyinfo;
	nt_iosb			iosb;

	/* inherited session info */
	if ((status = ntapi->tty_client_session_query(
			rtdata->hsession,
			&sessioninfo)))
		return status;

	/* simulated fork */
	sessioninfo.pid	   = rtdata->cid_self.process_id;
	sessioninfo.pgid   = sessioninfo.pgid ? sessioninfo.pgid : -1;
	sessioninfo.sid	   = 0;
	sessioninfo.syspid = 0;

	if ((status = ntapi->tty_client_session_set(
			rtdata->hsession,
			&sessioninfo)))
		return status;

	/* setsid() */
	sessioninfo.pid	   = rtdata->cid_self.process_id;
	sessioninfo.pgid   = rtdata->cid_self.process_id;
	sessioninfo.sid	   = rtdata->cid_self.process_id;
	sessioninfo.syspid = 0;

	if ((status = ntapi->tty_client_session_set(
			rtdata->hsession,
			&sessioninfo)))
		return status;

	/* ctty */
	ntapi->tt_aligned_block_memset(
		&ctlinfo,0,sizeof(ctlinfo));

	if ((status = ntapi->pty_ioctl(
			hpty,
			0,0,0,
			&iosb,
			TTY_TIOCSCTTY,
			&ctlinfo,sizeof(ctlinfo),
			&ctlinfo,sizeof(ctlinfo))))
		return status;

	/* tcsetpgrp */
	ntapi->tt_aligned_block_memset(
		&ctlinfo,0,sizeof(ctlinfo));

	ctlinfo.ctxarg[0] = rtdata->cid_self.process_id;

	if ((status = ntapi->pty_ioctl(
			hpty,
			0,0,0,
			&iosb,
			TTY_TIOCSPGRP,
			&ctlinfo,sizeof(ctlinfo),
			&ctlinfo,sizeof(ctlinfo))))
		return status;

	/* [ptycon daemon loop]: ctty signals */
	ntapi->tt_aligned_block_memset(
		&ptyinfo,0,sizeof(ptyinfo));

	if ((status = ntapi->pty_set(
			hpty,&iosb,
			&ptyinfo,sizeof(ptyinfo),
			NT_PTY_CLIENT_INFORMATION)))
		return status;

	return NT_STATUS_SUCCESS;
}

#endif