From c78a06a250cff42404d36798418f9c9a852007d9 Mon Sep 17 00:00:00 2001 From: midipix Date: Jun 08 2020 01:00:10 +0000 Subject: daemon: toks_daemon_release(): initial implementation and integration. --- diff --git a/project/common.mk b/project/common.mk index 525dc87..152a1f4 100644 --- a/project/common.mk +++ b/project/common.mk @@ -7,6 +7,7 @@ INTERNAL_SRCS = \ src/daemon/toks_daemon_connect.c \ src/daemon/toks_daemon_init.c \ src/daemon/toks_daemon_loop.c \ + src/daemon/toks_daemon_release.c \ src/daemon/toks_daemon_signal.c \ src/driver/toks_amain.c \ src/driver/toks_driver_ctx.c \ diff --git a/src/daemon/toks_daemon_acquire.c b/src/daemon/toks_daemon_acquire.c index 9d06826..567eb68 100644 --- a/src/daemon/toks_daemon_acquire.c +++ b/src/daemon/toks_daemon_acquire.c @@ -11,6 +11,34 @@ #include "toksvc_daemon_impl.h" #include "toksvc_driver_impl.h" +static void toks_daemon_token_release(struct toks_token * token) +{ + void * hport; + struct _nt_tty_ipc_msg msg; + uint32_t * keys; + + hport = token->client.hdaemon; + keys = token->keys.key; + + 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_RELEASE; + + msg.data.ipcinfo.ipckeys[0] = keys[0]; + msg.data.ipcinfo.ipckeys[1] = keys[1]; + msg.data.ipcinfo.ipckeys[2] = keys[2]; + msg.data.ipcinfo.ipckeys[3] = keys[3]; + msg.data.ipcinfo.ipckeys[4] = keys[4]; + msg.data.ipcinfo.ipckeys[5] = keys[5]; + + ntapi->zw_request_wait_reply_port( + hport,&msg,&msg); +} + static int32_t toks_daemon_client_wait(void * rapunzel) { struct toks_token * token; @@ -36,6 +64,8 @@ static int32_t toks_daemon_client_wait(void * rapunzel) ntapi->zw_close(client->hport); ntapi->zw_close(client->halert); + toks_daemon_token_release(token); + return ntapi->zw_terminate_thread( NT_CURRENT_THREAD_HANDLE, NT_STATUS_SUCCESS); @@ -79,6 +109,7 @@ static int32_t toks_daemon_token_instance( token->client.hprocess = client->hprocess; token->client.hswap = client->hswap; token->client.halert = client->halert; + token->client.hdaemon = client->hdaemon; token->client.cid.process_id = client->cid.process_id; token->client.cid.thread_id = client->cid.thread_id; @@ -134,6 +165,7 @@ int32_t __stdcall toks_daemon_acquire(struct toks_daemon_ctx * dctx) client.hport = 0; client.hprocess = 0; client.hswap = 0; + client.hdaemon = dctx->hport_internal_client; client.cid.process_id = msg->header.client_id.process_id; client.cid.thread_id = 0; diff --git a/src/daemon/toks_daemon_loop.c b/src/daemon/toks_daemon_loop.c index 5abf3db..2977777 100644 --- a/src/daemon/toks_daemon_loop.c +++ b/src/daemon/toks_daemon_loop.c @@ -22,6 +22,7 @@ static toks_daemon_routine * toks_daemon_vtbl[TOKS_VTBL_ELEMENTS] = { TOKS_HANDLER(CONNECT, toks_daemon_connect), TOKS_HANDLER(TTYSIGNAL, toks_daemon_signal), TOKS_HANDLER(ACQUIRE, toks_daemon_acquire), + TOKS_HANDLER(RELEASE, toks_daemon_release), }; int32_t __stdcall toks_daemon_loop(void * ctx) diff --git a/src/daemon/toks_daemon_release.c b/src/daemon/toks_daemon_release.c new file mode 100644 index 0000000..2ed9442 --- /dev/null +++ b/src/daemon/toks_daemon_release.c @@ -0,0 +1,58 @@ +/*********************************************************/ +/* 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" + +static void toks_daemon_token_reset(struct toks_token * token) +{ + token->self = 0; + token->keys.key[0] = 0; + token->keys.key[1] = 0; + token->keys.key[2] = 0; + token->keys.key[3] = 0; + token->keys.key[4] = 0; + token->keys.key[5] = 0; +} + +int32_t __stdcall toks_daemon_release(struct toks_daemon_ctx * dctx) +{ + nt_tty_port_msg * msg; + struct toks_token * token; + struct toks_token * toptr; + struct toks_token * tocap; + uint32_t * keys; + + msg = &dctx->reply; + keys = msg->ipcinfo.ipckeys; + + toptr = toks_get_driver_tokens(dctx->driver_ctx); + tocap = &toptr[toks_get_driver_ntokens(dctx->driver_ctx)]; + + for (token=0; !token && (toptrself) + && (toptr->keys.key[0] == keys[0]) + && (toptr->keys.key[1] == keys[1]) + && (toptr->keys.key[2] == keys[2]) + && (toptr->keys.key[3] == keys[3]) + && (toptr->keys.key[4] == keys[4]) + && (toptr->keys.key[5] == keys[5])) + token = toptr; + + if (!token) + return NT_STATUS_INVALID_PARAMETER; + + toks_daemon_token_reset(token); + + ntapi->zw_alert_thread(token->client.hinstance); + ntapi->zw_close(token->client.hinstance); + + return NT_STATUS_SUCCESS; +} diff --git a/src/internal/toksvc_daemon_impl.h b/src/internal/toksvc_daemon_impl.h index 83e43d3..614dd24 100644 --- a/src/internal/toksvc_daemon_impl.h +++ b/src/internal/toksvc_daemon_impl.h @@ -13,6 +13,7 @@ enum toks_daemon_opcodes { TOKS_DAEMON_SIGCHLD, TOKS_DAEMON_THREADEXIT, TOKS_DAEMON_ACQUIRE, + TOKS_DAEMON_RELEASE, TOKS_DAEMON_OPCODE_CAP }; @@ -25,6 +26,7 @@ struct toks_client_ctx { void * hport; void * hswap; void * halert; + void * hdaemon; nt_cid cid; }; @@ -59,5 +61,6 @@ int32_t __stdcall toks_daemon_loop(void *); int32_t __stdcall toks_daemon_connect(struct toks_daemon_ctx *); 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_release(struct toks_daemon_ctx *); #endif