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 <toksvc/toksvc.h>
#include "toksvc_driver_impl.h"
#include "toksvc_daemon_impl.h"

static int32_t toks_client_cancel(void * hport)
{
	int32_t			status;
	struct _nt_tty_ipc_msg	msg;

	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_CANCEL;

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

	return msg.data.ttyinfo.status;
}

int32_t toks_client_acquire(struct toks_driver_ctx * dctx)
{
	int32_t			status;
	void *			hport;
	struct _nt_tty_ipc_msg	msg;
	struct _nt_port_keys *	keys;
	int64_t			millisecs;
	nt_timeout		timeout;

	hport = toks_get_driver_hservice(dctx);
	keys  = toks_get_driver_keys(dctx);

	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_ACQUIRE;

	msg.data.ipcinfo.ctrlsvc.keys.reserved = toks_get_driver_tokpid(dctx);
	msg.data.ipcinfo.hevent                = toks_get_driver_hevent(dctx);

	if (!(millisecs = toks_get_driver_timeout(dctx))) {
		msg.data.ipcinfo.hevent = 0;

	} else if (millisecs == (-1)) {
		timeout.ulow  = ~(0u);
		timeout.ihigh = ~(0x80000000);

	} else {
		timeout.quad = (-1) * 10 * 1000 * millisecs;
	}

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

	switch (msg.data.ttyinfo.status) {
		case NT_STATUS_SUCCESS:
			break;

		case NT_STATUS_PENDING:
			status = ntapi->zw_wait_for_single_object(
				msg.data.ipcinfo.hevent,
				NT_SYNC_NON_ALERTABLE,
				&timeout);

			if (status != NT_STATUS_SUCCESS) {
				switch (toks_client_cancel(hport)) {
					case NT_STATUS_NOT_FOUND:
						break;

					default:
						return status;
				}
			}

			break;

		default:
			return msg.data.ttyinfo.status;
	}

	keys->key[0] = msg.data.ipcinfo.ipckeys[0];
	keys->key[1] = msg.data.ipcinfo.ipckeys[1];
	keys->key[2] = msg.data.ipcinfo.ipckeys[2];
	keys->key[3] = msg.data.ipcinfo.ipckeys[3];
	keys->key[4] = msg.data.ipcinfo.ipckeys[4];
	keys->key[5] = msg.data.ipcinfo.ipckeys[5];

	return NT_STATUS_SUCCESS;
}