Blame src/client/toks_client_acquire.c

02451d
/*********************************************************/
02451d
/*  toksvc: a framework-native token broker service      */
02451d
/*  Copyright (C) 2020  Z. Gilboa                        */
02451d
/*  Released under GPLv2 and GPLv3; see COPYING.TOKSVC.  */
02451d
/*********************************************************/
02451d
02451d
#include <psxtypes/psxtypes.h>
02451d
#include <ntapi/ntapi.h>
02451d
02451d
#include <toksvc/toksvc.h>
02451d
#include "toksvc_driver_impl.h"
02451d
#include "toksvc_daemon_impl.h"
02451d
b27e56
static int32_t toks_client_cancel(void * hport)
b27e56
{
b27e56
	int32_t			status;
b27e56
	struct _nt_tty_ipc_msg	msg;
b27e56
b27e56
	ntapi->tt_aligned_block_memset(
b27e56
		&msg,0,sizeof(msg));
b27e56
b27e56
	msg.header.msg_type	= NT_LPC_NEW_MESSAGE;
b27e56
	msg.header.data_size	= sizeof(msg.data);
b27e56
	msg.header.msg_size	= sizeof(msg);
b27e56
	msg.data.ttyinfo.opcode	= TOKS_DAEMON_CANCEL;
b27e56
b27e56
	if ((status = ntapi->zw_request_wait_reply_port(hport,&msg,&msg)))
b27e56
		return status;
b27e56
b27e56
	return msg.data.ttyinfo.status;
b27e56
}
b27e56
02451d
int32_t toks_client_acquire(struct toks_driver_ctx * dctx)
02451d
{
02451d
	int32_t			status;
02451d
	void *			hport;
02451d
	struct _nt_tty_ipc_msg	msg;
02451d
	struct _nt_port_keys *	keys;
b27e56
	int64_t			millisecs;
b27e56
	nt_timeout		timeout;
02451d
02451d
	hport = toks_get_driver_hservice(dctx);
02451d
	keys  = toks_get_driver_keys(dctx);
02451d
02451d
	ntapi->tt_aligned_block_memset(
02451d
		&msg,0,sizeof(msg));
02451d
02451d
	msg.header.msg_type	= NT_LPC_NEW_MESSAGE;
02451d
	msg.header.data_size	= sizeof(msg.data);
02451d
	msg.header.msg_size	= sizeof(msg);
02451d
	msg.data.ttyinfo.opcode	= TOKS_DAEMON_ACQUIRE;
02451d
e3e583
	msg.data.ipcinfo.ctrlsvc.keys.key[0] = toks_get_driver_tokpid(dctx);
658181
	msg.data.ipcinfo.hevent              = toks_get_driver_hevent(dctx);
e3e583
b27e56
	if (!(millisecs = toks_get_driver_timeout(dctx))) {
b27e56
		msg.data.ipcinfo.hevent = 0;
b27e56
b27e56
	} else if (millisecs == (-1)) {
b27e56
		timeout.ulow  = ~(0u);
b27e56
		timeout.ihigh = ~(0x80000000);
b27e56
b27e56
	} else {
b27e56
		timeout.quad = (-1) * 10 * 1000 * millisecs;
b27e56
	}
b27e56
02451d
	if ((status = ntapi->zw_request_wait_reply_port(hport,&msg,&msg)))
02451d
		return status;
02451d
658181
	switch (msg.data.ttyinfo.status) {
658181
		case NT_STATUS_SUCCESS:
658181
			break;
658181
658181
		case NT_STATUS_PENDING:
658181
			status = ntapi->zw_wait_for_single_object(
658181
				msg.data.ipcinfo.hevent,
658181
				NT_SYNC_NON_ALERTABLE,
b27e56
				&timeout);
b27e56
b27e56
			if (status != NT_STATUS_SUCCESS) {
b27e56
				switch (toks_client_cancel(hport)) {
b27e56
					case NT_STATUS_NOT_FOUND:
b27e56
						break;
b27e56
b27e56
					default:
b27e56
						return status;
b27e56
				}
b27e56
			}
b27e56
658181
			break;
658181
658181
		default:
658181
			return msg.data.ttyinfo.status;
658181
	}
02451d
02451d
	keys->key[0] = msg.data.ipcinfo.ipckeys[0];
02451d
	keys->key[1] = msg.data.ipcinfo.ipckeys[1];
02451d
	keys->key[2] = msg.data.ipcinfo.ipckeys[2];
02451d
	keys->key[3] = msg.data.ipcinfo.ipckeys[3];
02451d
	keys->key[4] = msg.data.ipcinfo.ipckeys[4];
02451d
	keys->key[5] = msg.data.ipcinfo.ipckeys[5];
02451d
b27e56
	return NT_STATUS_SUCCESS;
02451d
}