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_daemon_impl.h"
#include "toksvc_driver_impl.h"

int32_t __stdcall toks_daemon_cancel(struct toks_daemon_ctx * dctx)
{
	nt_tty_port_msg *           msg;
	nt_cid *                    cid;
	struct toks_waiter *        waiter;
	struct toks_waiter *        waitcap;
	int                         nwaiters;

	msg      = &dctx->reply;
	cid      = &msg->header.client_id;
	nwaiters = toks_get_driver_nwaiters(dctx->driver_ctx);

	if (nwaiters == 0)
		return NT_STATUS_NOT_FOUND;

	waiter  = dctx->waiter_first;
	waitcap = &waiter[nwaiters];

	if (waitcap > dctx->waiter_cap)
		waitcap = dctx->waiter_cap;

	for (; waiter<waitcap; waiter++) {
		if (waiter->msg.header.client_id.process_id == cid->process_id) {
			if (waiter->msg.header.client_id.thread_id == cid->thread_id) {
				waiter->msg.header.msg_id = 0;
				toks_set_driver_nwaiters(dctx->driver_ctx,--nwaiters);
				return NT_STATUS_CANCELLED;
			}
		}
	}

	if (nwaiters < (dctx->waiter_cap - dctx->waiter_first))
		return NT_STATUS_NOT_FOUND;

	waiter  = dctx->waiter_base;
	waitcap = &waiter[nwaiters - (dctx->waiter_cap-dctx->waiter_first)];

	for (; waiter<waitcap; waiter++) {
		if (waiter->msg.header.client_id.process_id == cid->process_id) {
			if (waiter->msg.header.client_id.thread_id == cid->thread_id) {
				waiter->msg.header.msg_id = 0;
				toks_set_driver_nwaiters(dctx->driver_ctx,--nwaiters);
				return NT_STATUS_CANCELLED;
			}
		}
	}

	return NT_STATUS_NOT_FOUND;
}