From 3034d1c48cfc5267b6f2cc5ddafb5d7f5d4788fb Mon Sep 17 00:00:00 2001 From: midipix Date: Jun 09 2020 14:33:52 +0000 Subject: daemon: added toks_daemon_cancel() to allow client-side timeouts. --- diff --git a/project/common.mk b/project/common.mk index 717cf04..75aee5d 100644 --- a/project/common.mk +++ b/project/common.mk @@ -6,6 +6,7 @@ INTERNAL_SRCS = \ src/client/toks_client_release.c \ src/client/toks_client_token.c \ src/daemon/toks_daemon_acquire.c \ + src/daemon/toks_daemon_cancel.c \ src/daemon/toks_daemon_connect.c \ src/daemon/toks_daemon_init.c \ src/daemon/toks_daemon_loop.c \ diff --git a/src/daemon/toks_daemon_cancel.c b/src/daemon/toks_daemon_cancel.c new file mode 100644 index 0000000..9671d2a --- /dev/null +++ b/src/daemon/toks_daemon_cancel.c @@ -0,0 +1,60 @@ +/*********************************************************/ +/* toksvc: a framework-native token broker service */ +/* Copyright (C) 2020 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.TOKSVC. */ +/*********************************************************/ + +#include +#include + +#include +#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 (; waitermsg.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; + 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 (; waitermsg.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; + return NT_STATUS_CANCELLED; + } + } + } + + return NT_STATUS_NOT_FOUND; +} diff --git a/src/daemon/toks_daemon_loop.c b/src/daemon/toks_daemon_loop.c index 44709b0..343e41c 100644 --- a/src/daemon/toks_daemon_loop.c +++ b/src/daemon/toks_daemon_loop.c @@ -23,6 +23,7 @@ static toks_daemon_routine * toks_daemon_vtbl[TOKS_VTBL_ELEMENTS] = { TOKS_HANDLER(TTYSIGNAL, toks_daemon_signal), TOKS_HANDLER(ACQUIRE, toks_daemon_acquire), TOKS_HANDLER(RELEASE, toks_daemon_release), + TOKS_HANDLER(CANCEL, toks_daemon_cancel), }; int32_t __stdcall toks_daemon_loop(void * ctx) diff --git a/src/daemon/toks_daemon_release.c b/src/daemon/toks_daemon_release.c index 2889620..d17cabd 100644 --- a/src/daemon/toks_daemon_release.c +++ b/src/daemon/toks_daemon_release.c @@ -51,6 +51,9 @@ static int32_t toks_daemon_unqueue(struct toks_daemon_ctx * dctx) + if (!waiter->msg.header.msg_id) + return NT_STATUS_CANCELLED; + ntapi->tt_generic_memcpy( &cmsg,msg, sizeof(*msg)); diff --git a/src/internal/toksvc_daemon_impl.h b/src/internal/toksvc_daemon_impl.h index 4a21a0f..24569e4 100644 --- a/src/internal/toksvc_daemon_impl.h +++ b/src/internal/toksvc_daemon_impl.h @@ -14,6 +14,7 @@ enum toks_daemon_opcodes { TOKS_DAEMON_THREADEXIT, TOKS_DAEMON_ACQUIRE, TOKS_DAEMON_RELEASE, + TOKS_DAEMON_CANCEL, TOKS_DAEMON_OPCODE_CAP }; @@ -76,5 +77,6 @@ int32_t __stdcall toks_daemon_signal(struct toks_daemon_ctx *); int32_t __stdcall toks_daemon_acquire(struct toks_daemon_ctx *); int32_t __stdcall toks_daemon_pidopen(struct toks_daemon_ctx *); int32_t __stdcall toks_daemon_release(struct toks_daemon_ctx *); +int32_t __stdcall toks_daemon_cancel(struct toks_daemon_ctx *); #endif