summaryrefslogtreecommitdiffhomepage
path: root/src/tty/ntapi_tty_join_session.c
blob: 93fc34107479dcb68a1ef729afc30b2df6c0708c (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
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"

int32_t __stdcall __ntapi_tty_join_session(
	__out	void **			hport,
	__out	nt_port_name *		port_name,
	__in	nt_port_attr *		port_attr,
	__in	nt_tty_session_type	type)
{
	nt_status			status;
	ntapi_internals *		__internals;

	/* init */
	__internals = __ntapi_internals();

	if (type == NT_TTY_SESSION_PRIMARY) {
		hport = hport ? hport : &__internals->hport_tty_session;
		port_name = port_name ? port_name : __internals->subsystem;
	}

	/* port name */
	__ntapi->tt_port_name_from_attr(
		port_name,
		port_attr);

	/* connect to subsystem */
	if ((status = __ntapi->tty_connect(
			hport,
			(wchar16_t *)port_name,
			NT_SECURITY_IMPERSONATION)))
		return status;

	/* finalize primary session */
	if (type == NT_TTY_SESSION_PRIMARY) {
		if (hport != &__internals->hport_tty_session)
			__internals->hport_tty_session = *hport;

		if (__internals->rtdata)
			__internals->rtdata->hsession = *hport;

		if (port_name != __internals->subsystem)
			__ntapi->tt_memcpy_utf16(
				__internals->subsystem->base_named_objects,
				port_name->base_named_objects,
				sizeof(*port_name));
	};

	return status;
}