From 778de5176a3683e31be94f6b21aa1266dae00934 Mon Sep 17 00:00:00 2001 From: midipix Date: Jul 21 2016 07:47:28 +0000 Subject: daemon: added daemon initialization logic. --- diff --git a/project/common.mk b/project/common.mk index bbd9cf6..838a6bb 100644 --- a/project/common.mk +++ b/project/common.mk @@ -7,6 +7,8 @@ COMMON_SRCS = \ src/console/ptyc_console_poller.c \ src/console/ptyc_console_reader.c \ src/console/ptyc_console_writer.c \ + src/daemon/ptyc_daemon_init.c \ + src/daemon/ptyc_daemon_loop.c \ src/debug/ptyc_dbg_cat.c \ src/debug/ptyc_dbg_event.c \ src/debug/ptyc_dbg_oven.c \ diff --git a/project/tree.mk b/project/tree.mk index 9b2966f..454821b 100644 --- a/project/tree.mk +++ b/project/tree.mk @@ -2,6 +2,7 @@ tree.tag: mkdir -p src mkdir -p src/bridge mkdir -p src/console + mkdir -p src/daemon mkdir -p src/debug mkdir -p src/driver mkdir -p src/internal diff --git a/src/daemon/ptyc_daemon_init.c b/src/daemon/ptyc_daemon_init.c new file mode 100644 index 0000000..c7fa5eb --- /dev/null +++ b/src/daemon/ptyc_daemon_init.c @@ -0,0 +1,121 @@ +/*********************************************************/ +/* ptycon: a pty-console bridge */ +/* Copyright (C) 2016 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.PTYCON. */ +/*********************************************************/ + +#include +#include +#include + +#include +#include "ptycon_daemon_impl.h" +#include "ptycon_driver_impl.h" + +static const nt_guid ptyc_daemon_guid = PTYC_PORT_GUID_DAEMON; + +static int32_t ptyc_daemon_init_impl(struct ptyc_daemon_ctx * dctx) +{ + int32_t status; + nt_daemon_params dparams; + wchar16_t * port_name; + nt_port_name_keys * port_name_keys; + + /* daemon attributes */ + dctx->daemon_attr.type = NT_PORT_TYPE_DAEMON; + dctx->daemon_attr.subtype = NT_PORT_SUBTYPE_DEFAULT; + + /* port guid */ + ntapi->tt_guid_copy( + &dctx->daemon_attr.guid, + &ptyc_daemon_guid); + + /* port keys */ + if ((status = ntapi->tt_port_generate_keys(&dctx->daemon_attr.keys))) + return status; + + /* port name */ + ntapi->tt_port_name_from_attributes( + &dctx->daemon_name, + &dctx->daemon_attr); + + /* dparams */ + ntapi->tt_aligned_block_memset( + &dparams,0,sizeof(dparams)); + + port_name = (wchar16_t *)&dctx->daemon_name; + port_name_keys = (nt_port_name_keys *)&dctx->daemon_name.port_name_keys; + + dparams.port_keys = &dctx->daemon_keys; + dparams.port_name = port_name; + dparams.port_name_keys = port_name_keys; + + dparams.port_msg_size = sizeof(nt_tty_port_msg); + dparams.flags = NT_DSR_INIT_DEFAULT; + + dparams.daemon_once_routine = 0; + dparams.daemon_loop_routine = ptyc_daemon_loop; + dparams.daemon_loop_context = dctx; + + dparams.pport_daemon = &dctx->hport_daemon; + dparams.pport_internal_client = &dctx->hport_internal_client; + + dparams.pevent_daemon_ready = &dctx->hevent_daemon_ready; + dparams.pevent_internal_client_ready = &dctx->hevent_internal_client_ready; + + dparams.stack_size_commit = 8192; + dparams.stack_size_reserve = 8192; + + return ntapi->dsr_init(&dparams); +} + +static int32_t ptyc_daemon_once = 0; + +int32_t __stdcall ptyc_daemon_init(struct ptyc_daemon_ctx * dctx, uint64_t drvflags) +{ + int32_t status; + nt_timeout timeout; + nt_runtime_data * rtdata; + + /* rtdata */ + if ((status = ntapi->tt_get_runtime_data(&rtdata,0))) + return status; + + /* needed? */ + if (drvflags & PTYC_DRIVER_DAEMON_NEVER) + return 0; + + if (!(drvflags & PTYC_DRIVER_DAEMON_ALWAYS)) + if (ntapi->tt_guid_compare(&rtdata->srv_guid,&ptyc_daemon_guid)) + return 0; + + /* once */ + switch (at_locked_cas_32(&ptyc_daemon_once,0,1)) { + case 0: + if ((status = ptyc_daemon_init_impl(dctx))) { + at_locked_add_32(&ptyc_daemon_once,2); + return status; + } + + at_locked_inc_32(&ptyc_daemon_once); + return 0; + + case 1: + timeout.quad = -10; + + for (; (at_locked_cas_32(&ptyc_daemon_once,0,1) == 1); ) + ntapi->zw_delay_execution( + NT_SYNC_ALERTABLE, + &timeout); + + return (ptyc_daemon_once == 2) + ? 0 : -1; + + case 2: + return 0; + + case 3: + default: + return -1; + } +} diff --git a/src/daemon/ptyc_daemon_loop.c b/src/daemon/ptyc_daemon_loop.c new file mode 100644 index 0000000..954ed8c --- /dev/null +++ b/src/daemon/ptyc_daemon_loop.c @@ -0,0 +1,18 @@ +/*********************************************************/ +/* ptycon: a pty-console bridge */ +/* Copyright (C) 2016 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.PTYCON. */ +/*********************************************************/ + +#include +#include + +#include +#include "ptycon_daemon_impl.h" +#include "ptycon_driver_impl.h" + +int32_t __stdcall ptyc_daemon_loop(void * ctx) +{ + (void)ctx; + return ntapi->tt_wait_for_dummy_event(); +}