Blob Blame History Raw
/*********************************************************/
/*  toksvc: a framework-native token broker service      */
/*  Copyright (C) 2020  SysDeer Technologies, LLC        */
/*  Released under GPLv2 and GPLv3; see COPYING.TOKSVC.  */
/*********************************************************/

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

#include <toksvc/toksvc.h>
#include "toksvc_driver_impl.h"
#include "toksvc_daemon_impl.h"

int32_t toks_service_ioctl(
	struct toks_driver_ctx *  dctx,
	uint32_t                  ctlcode,
	uintptr_t *               data,
	void *                    any)
{
	int32_t			status;
	void *			hport;
	const char *		refstr;
	size_t			reflen;
	struct _nt_tty_sync_msg	msg;

	hport = toks_get_driver_hservice(dctx);
	refstr = toks_get_driver_refstr(dctx);
	reflen = 0;

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

	msg.header.msg_type	= NT_LPC_NEW_MESSAGE;
	msg.header.data_size	= sizeof(msg.data);
	msg.header.msg_size	= sizeof(msg);
	msg.data.ttyinfo.opcode	= TOKS_DAEMON_IOCTL;
	msg.data.ttyinfo.opdata	= ctlcode;

	if (refstr && (reflen = toks_strlen(refstr))) {
		if (reflen > sizeof(msg.data.syncinfo.ipcsvc.keys.key))
			reflen = sizeof(msg.data.syncinfo.ipcsvc.keys.key);

		ntapi->tt_generic_memcpy(
			msg.data.syncinfo.ipcsvc.keys.key,
			refstr,reflen);
	}

	switch (ctlcode) {
		case TOKS_IOCTL_SET_TOKEN_COUNT:
		case TOKS_IOCTL_SET_LOG_LEVEL:
			msg.data.ttyinfo.exarg = (void *)(intptr_t)*data;
			break;

		default:
			(void)any;
			break;
	}

	if ((status = ntapi->zw_request_wait_reply_port(hport,&msg,&msg)))
		return status;

	else if (msg.data.ttyinfo.status)
		return msg.data.ttyinfo.status;

	switch (ctlcode) {
		case TOKS_IOCTL_GET_TOKEN_COUNT:
		case TOKS_IOCTL_GET_LOG_LEVEL:
			*data = (int)(intptr_t)msg.data.ttyinfo.exarg;
			break;

		default:
			(void)any;
			break;
	}

	return NT_STATUS_SUCCESS;
}