From e6c54759a168c94d0387a79ab37160ff93cc6692 Mon Sep 17 00:00:00 2001 From: midipix Date: Jun 19 2020 15:25:27 +0000 Subject: daemon: toks_daemon_ioctl(): initial implementation and integration. --- diff --git a/include/toksvc/toksvc.h b/include/toksvc/toksvc.h index 9142061..99e1447 100644 --- a/include/toksvc/toksvc.h +++ b/include/toksvc/toksvc.h @@ -65,6 +65,36 @@ extern "C" { #define TOKS_ERROR_CHILD 0x0004 #define TOKS_ERROR_CUSTOM 0x0008 +/* ioctl */ +#define TOKS_IOCTL_DEVICE_TOKEN_SERVICE (0xdaaf) +#define TOKS_IOCTL_OPCODE_BASE (0xaaf) + +#define TOKS_IOCTL_METHOD_BUFFERED (0x0) +#define TOKS_IOCTL_METHOD_INDIRECT (0x1) +#define TOKS_IOCTL_METHOD_OUTDIRECT (0x2) +#define TOKS_IOCTL_METHOD_NEITHER (0x3) + +#define TOKS_IOCTL_ACCESS_READ (0x1) +#define TOKS_IOCTL_ACCESS_WRITE (0x2) + +#define TOKS_IOCTL_IDX(IDX) (TOKS_IOCTL_OPCODE_BASE + IDX) + +#define TOKS_IOCTL_ANY(IDX,ACC,METHOD) ((TOKS_IOCTL_DEVICE_TOKEN_SERVICE << 16) \ + | (TOKS_IOCTL_IDX(IDX) << 2) \ + | (ACC << 14) | (METHOD)) + +#define TOKS_IOCTL_GET(IDX) TOKS_IOCTL_ANY(IDX,TOKS_IOCTL_ACCESS_READ, \ + TOKS_IOCTL_METHOD_OUTDIRECT) + +#define TOKS_IOCTL_SET(IDX) TOKS_IOCTL_ANY(IDX,TOKS_IOCTL_ACCESS_WRITE, \ + TOKS_IOCTL_METHOD_INDIRECT) + +#define TOKS_IOCTL_IDX_GET_TOKEN_COUNT 0 +#define TOKS_IOCTL_IDX_SET_TOKEN_COUNT 1 + +#define TOKS_IOCTL_GET_TOKEN_COUNT TOKS_IOCTL_GET(TOKS_IOCTL_IDX_GET_TOKEN_COUNT) +#define TOKS_IOCTL_SET_TOKEN_COUNT TOKS_IOCTL_GET(TOKS_IOCTL_IDX_SET_TOKEN_COUNT) + enum toks_custom_error { TOKS_ERR_FLOW_ERROR, TOKS_ERR_FLEE_ERROR, diff --git a/project/common.mk b/project/common.mk index d96094b..004101a 100644 --- a/project/common.mk +++ b/project/common.mk @@ -11,6 +11,7 @@ INTERNAL_SRCS = \ src/daemon/toks_daemon_cidquery.c \ src/daemon/toks_daemon_connect.c \ src/daemon/toks_daemon_init.c \ + src/daemon/toks_daemon_ioctl.c \ src/daemon/toks_daemon_loop.c \ src/daemon/toks_daemon_pidopen.c \ src/daemon/toks_daemon_release.c \ diff --git a/src/daemon/toks_daemon_connect.c b/src/daemon/toks_daemon_connect.c index 984f8d4..916db97 100644 --- a/src/daemon/toks_daemon_connect.c +++ b/src/daemon/toks_daemon_connect.c @@ -76,7 +76,7 @@ int32_t __stdcall toks_daemon_connect(struct toks_daemon_ctx * dctx) nt_oa oa; msg = &dctx->reply; - msg->ttyinfo.exarg = 0; + msg->ttyinfo.opdata = 0; client.hport = 0; client.hprocess = 0; diff --git a/src/daemon/toks_daemon_ioctl.c b/src/daemon/toks_daemon_ioctl.c new file mode 100644 index 0000000..d9d7f99 --- /dev/null +++ b/src/daemon/toks_daemon_ioctl.c @@ -0,0 +1,36 @@ +/*********************************************************/ +/* 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 int32_t toks_daemon_ioctl_get_token_count(struct toks_daemon_ctx * dctx) +{ + nt_tty_port_msg * msg = &dctx->reply; + msg->ttyinfo.exarg = (void *)(intptr_t)toks_get_driver_ntokens(dctx->driver_ctx); + return NT_STATUS_SUCCESS; +} + +int32_t __stdcall toks_daemon_ioctl(struct toks_daemon_ctx * dctx) +{ + nt_tty_port_msg * msg; + uint32_t ctlcode; + + msg = &dctx->reply; + ctlcode = msg->ttyinfo.opdata; + + switch (ctlcode) { + case TOKS_IOCTL_GET_TOKEN_COUNT: + return toks_daemon_ioctl_get_token_count(dctx); + + default: + return NT_STATUS_NOT_IMPLEMENTED; + } +} diff --git a/src/daemon/toks_daemon_loop.c b/src/daemon/toks_daemon_loop.c index be54404..258806d 100644 --- a/src/daemon/toks_daemon_loop.c +++ b/src/daemon/toks_daemon_loop.c @@ -26,6 +26,7 @@ static toks_daemon_routine * toks_daemon_vtbl[TOKS_VTBL_ELEMENTS] = { TOKS_HANDLER(RELEASE, toks_daemon_release), TOKS_HANDLER(CANCEL, toks_daemon_cancel), TOKS_HANDLER(ABORT, toks_daemon_abort), + TOKS_HANDLER(IOCTL, toks_daemon_ioctl), }; static toks_daemon_routine * toks_client_vtbl[TOKS_VTBL_ELEMENTS] = { @@ -225,16 +226,20 @@ int32_t __stdcall toks_daemon_loop(void * ctx) reply->header.msg_type = NT_LPC_REPLY; + switch (opcode) { + case TOKS_DAEMON_IOCTL: + break; + + default: + reply->ttyinfo.opdata = reply->header.msg_id; + } + if ((opcode >= TOKS_DAEMON_OPCODE_BASE) && (opcode < TOKS_DAEMON_OPCODE_CAP)) { - reply->ttyinfo.exarg = (void *)request->header.client_id.process_id; opcode -= TOKS_DAEMON_OPCODE_BASE; - - if (svcvtbl[opcode]) - reply->ttyinfo.status = svcvtbl[opcode](dctx); - else - reply->ttyinfo.status = NT_STATUS_NOT_IMPLEMENTED; + reply->ttyinfo.status = svcvtbl[opcode] + ? svcvtbl[opcode](dctx) + : NT_STATUS_NOT_IMPLEMENTED; } else { - reply->ttyinfo.exarg = NT_INVALID_HANDLE_VALUE; reply->ttyinfo.status = NT_STATUS_LPC_INVALID_CONNECTION_USAGE; } @@ -245,7 +250,7 @@ int32_t __stdcall toks_daemon_loop(void * ctx) ntapi->tt_aligned_block_memset( request,0,sizeof(*request)); - if (!reply->ttyinfo.exarg) + if (!reply->ttyinfo.opdata) ntapi->zw_reply_wait_receive_port( dctx->hport_daemon, &port_id, diff --git a/src/internal/toksvc_daemon_impl.h b/src/internal/toksvc_daemon_impl.h index 7df3a99..273816d 100644 --- a/src/internal/toksvc_daemon_impl.h +++ b/src/internal/toksvc_daemon_impl.h @@ -17,6 +17,7 @@ enum toks_daemon_opcodes { TOKS_DAEMON_RELEASE, TOKS_DAEMON_CANCEL, TOKS_DAEMON_ABORT, + TOKS_DAEMON_IOCTL, TOKS_DAEMON_OPCODE_CAP }; @@ -92,5 +93,6 @@ 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 *); int32_t __stdcall toks_daemon_abort(struct toks_daemon_ctx *); +int32_t __stdcall toks_daemon_ioctl(struct toks_daemon_ctx *); #endif diff --git a/src/log/toks_log_lpc_message.c b/src/log/toks_log_lpc_message.c index 3dbfa26..d50a9f5 100644 --- a/src/log/toks_log_lpc_message.c +++ b/src/log/toks_log_lpc_message.c @@ -32,6 +32,7 @@ static const char * toks_opcode_str[] = { TOKS_OPCODE_STR(RELEASE) = "TOKS_DAEMON_RELEASE", TOKS_OPCODE_STR(CANCEL) = "TOKS_DAEMON_CANCEL", TOKS_OPCODE_STR(ABORT) = "TOKS_DAEMON_ABORT", + TOKS_OPCODE_STR(IOCTL) = "TOKS_DAEMON_IOCTL", }; static const char * toks_lpc_msg_str[] = {