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

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

#include "toksvc_daemon_impl.h"
#include "toksvc_driver_impl.h"
#include "toksvc_log_impl.h"

static int32_t toks_daemon_ioctl_get_token_count(struct toks_daemon_ctx * dctx)
{
	nt_tty_port_msg * msg = &dctx->reply;
	msg->ttyinfo.exarg = (void *)(intptr_t)toks_get_driver_ntokens(dctx->driver_ctx);
	return NT_STATUS_SUCCESS;
}

static int32_t toks_daemon_ioctl_set_token_count(struct toks_daemon_ctx * dctx)
{
	nt_tty_port_msg * msg;
	int               ntokens;

	msg     = &dctx->reply;
	ntokens = (int)(intptr_t)msg->ttyinfo.exarg;

	if (ntokens < 0)
		return NT_STATUS_INVALID_PARAMETER;

	else if (ntokens > toks_get_driver_atokens(dctx->driver_ctx))
		return NT_STATUS_BUFFER_TOO_SMALL;

	toks_set_driver_ntokens(dctx->driver_ctx,ntokens);

	dctx->ftokens = (dctx->utokens < ntokens)
		? ntokens - dctx->utokens : 0;

	return toks_daemon_release(dctx);
}

static int32_t toks_daemon_ioctl_get_service_info(struct toks_daemon_ctx * dctx)
{
	nt_tty_port_msg * msg = &dctx->reply;

	/* uuid */
	msg->svcdata.meta[0]  = (dctx->driver_ctx->cctx->uuid->data1);

	msg->svcdata.meta[1]  = (dctx->driver_ctx->cctx->uuid->data2);
	msg->svcdata.meta[1] |= (dctx->driver_ctx->cctx->uuid->data3 << 16);

	msg->svcdata.meta[2]  = (dctx->driver_ctx->cctx->uuid->data4[0]);
	msg->svcdata.meta[2] |= (dctx->driver_ctx->cctx->uuid->data4[1] << 8);
	msg->svcdata.meta[2] |= (dctx->driver_ctx->cctx->uuid->data4[2] << 16);
	msg->svcdata.meta[2] |= (dctx->driver_ctx->cctx->uuid->data4[3] << 24);

	msg->svcdata.meta[3]  = (dctx->driver_ctx->cctx->uuid->data4[4]);
	msg->svcdata.meta[3] |= (dctx->driver_ctx->cctx->uuid->data4[5] << 8);
	msg->svcdata.meta[3] |= (dctx->driver_ctx->cctx->uuid->data4[6] << 16);
	msg->svcdata.meta[3] |= (dctx->driver_ctx->cctx->uuid->data4[7] << 24);

	/* syspid, systid */
	msg->svcdata.data[0]  = pe_get_current_process_id();
	msg->svcdata.data[1]  = pe_get_current_thread_id();

	/* ctrlpid, csyspid */
	msg->svcdata.data[2]  = dctx->ctrlpid;
	msg->svcdata.data[3]  = dctx->csyspid;

	/* allocated, available */
	msg->svcdata.data[4]  = toks_get_driver_atokens(dctx->driver_ctx);
	msg->svcdata.data[5]  = toks_get_driver_ntokens(dctx->driver_ctx);

	/* used, free */
	msg->svcdata.data[6]  = dctx->utokens;
	msg->svcdata.data[7]  = dctx->ftokens;

	return NT_STATUS_SUCCESS;
}

static int32_t toks_daemon_ioctl_log_service_info(struct toks_daemon_ctx * dctx)
{
	toks_log_service_info(dctx);
	return NT_STATUS_SUCCESS;
}

int32_t __stdcall toks_daemon_ioctl(struct toks_daemon_ctx * dctx)
{
	nt_tty_port_msg * msg;
	uint32_t          ctlcode;

	msg     = &dctx->reply;
	ctlcode = msg->ttyinfo.opdata;

	switch (ctlcode) {
		case TOKS_IOCTL_GET_TOKEN_COUNT:
			return toks_daemon_ioctl_get_token_count(dctx);

		case TOKS_IOCTL_SET_TOKEN_COUNT:
			return toks_daemon_ioctl_set_token_count(dctx);

		case TOKS_IOCTL_GET_SERVICE_INFO:
			return toks_daemon_ioctl_get_service_info(dctx);

		case TOKS_IOCTL_LOG_SERVICE_INFO:
			return toks_daemon_ioctl_log_service_info(dctx);

		default:
			return NT_STATUS_NOT_IMPLEMENTED;
	}
}