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;
8b933d
	struct _nt_tty_sync_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;
8b933d
	struct _nt_tty_sync_msg	msg;
02451d
	struct _nt_port_keys *	keys;
b27e56
	int64_t			millisecs;
b27e56
	nt_timeout		timeout;
731535
	nt_ebi			syncebi;
bbac38
	const char *		refstr;
bbac38
	size_t			reflen;
731535
	void *			hduo[2];
02451d
bbac38
	hport  = toks_get_driver_hservice(dctx);
bbac38
	keys   = toks_get_driver_keys(dctx);
bbac38
	refstr = toks_get_driver_refstr(dctx);
bbac38
	reflen = 0;
02451d
02451d
	ntapi->tt_aligned_block_memset(
02451d
		&msg,0,sizeof(msg));
02451d
083e6f
	msg.header.msg_type	  = NT_LPC_NEW_MESSAGE;
083e6f
	msg.header.data_size	  = sizeof(msg.data);
083e6f
	msg.header.msg_size	  = sizeof(msg);
083e6f
	msg.data.ttyinfo.opcode	  = TOKS_DAEMON_ACQUIRE;
083e6f
	msg.data.syncinfo.hevent  = toks_get_driver_hevent(dctx);
02451d
c42b09
	if (toks_get_driver_tokpid(dctx)) {
8b933d
		msg.data.syncinfo.ipcsvc.keys.reserved = toks_get_driver_tokpid(dctx);
8b933d
		msg.data.syncinfo.ipcsvc.options       = TOKS_OPT_FRAMEWORK_PID;
201fd7
	} else {
8b933d
		msg.data.syncinfo.ipcsvc.keys.reserved = toks_get_driver_tsyspid(dctx);
c42b09
	}
e3e583
bbac38
	if (refstr && (reflen = toks_strlen(refstr))) {
8b933d
		if (reflen > sizeof(msg.data.syncinfo.ipcsvc.keys.key))
8b933d
			reflen = sizeof(msg.data.syncinfo.ipcsvc.keys.key);
bbac38
bbac38
		ntapi->tt_generic_memcpy(
8b933d
			msg.data.syncinfo.ipcsvc.keys.key,
bbac38
			refstr,reflen);
bbac38
	}
bbac38
b27e56
	if (!(millisecs = toks_get_driver_timeout(dctx))) {
8b933d
		msg.data.syncinfo.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:
731535
			hduo[0] = msg.data.syncinfo.hevent;
731535
			hduo[1] = toks_get_driver_hserver(dctx);
731535
731535
			ntapi->zw_wait_for_multiple_objects(
731535
				2,hduo,
731535
				NT_WAIT_ANY,
658181
				NT_SYNC_NON_ALERTABLE,
b27e56
				&timeout);
b27e56
731535
			status = ntapi->zw_query_event(
731535
				msg.data.syncinfo.hevent,
731535
				NT_EVENT_BASIC_INFORMATION,
731535
				&syncebi,sizeof(syncebi),
731535
				&(size_t){0});
731535
731535
			if ((status == NT_STATUS_SUCCESS) && !syncebi.signal_state) {
731535
				return NT_STATUS_SYSTEM_PROCESS_TERMINATED;
731535
731535
			} else if (status == NT_STATUS_SUCCESS) {
731535
				(void)0;
731535
731535
			} else {
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
8b933d
	keys->key[0] = msg.data.syncinfo.ipckeys[0];
8b933d
	keys->key[1] = msg.data.syncinfo.ipckeys[1];
8b933d
	keys->key[2] = msg.data.syncinfo.ipckeys[2];
8b933d
	keys->key[3] = msg.data.syncinfo.ipckeys[3];
8b933d
	keys->key[4] = msg.data.syncinfo.ipckeys[4];
8b933d
	keys->key[5] = msg.data.syncinfo.ipckeys[5];
02451d
b27e56
	return NT_STATUS_SUCCESS;
02451d
}