diff --git a/src/daemon/toks_daemon_connect.c b/src/daemon/toks_daemon_connect.c index 05212ee..1ef8425 100644 --- a/src/daemon/toks_daemon_connect.c +++ b/src/daemon/toks_daemon_connect.c @@ -11,20 +11,102 @@ #include "toksvc_daemon_impl.h" #include "toksvc_driver_impl.h" +static int32_t toks_daemon_client_wait(void * rapunzel) +{ + struct toks_client_ctx * client; + + client = (struct toks_client_ctx *)rapunzel; + + ntapi->zw_set_event(client->hswap,0); + ntapi->zw_close(client->hswap); + + ntapi->zw_wait_for_single_object( + client->hprocess, + NT_SYNC_NON_ALERTABLE, + 0); + + ntapi->zw_close(client->hprocess); + ntapi->zw_close(client->hport); + + return ntapi->zw_terminate_thread( + NT_CURRENT_THREAD_HANDLE, + NT_STATUS_SUCCESS); +} + +static int32_t toks_daemon_client_instance(struct toks_client_ctx * client) +{ + int32_t status; + nt_thread_params params; + + if ((status = ntapi->tt_create_private_event( + &client->hswap, + NT_NOTIFICATION_EVENT, + NT_EVENT_NOT_SIGNALED))) + return status; + + ntapi->tt_aligned_block_memset( + ¶ms,0,sizeof(params)); + + params.hprocess = NT_CURRENT_PROCESS_HANDLE; + params.start = toks_daemon_client_wait; + params.ext_ctx = client; + params.ext_ctx_size = sizeof(*client); + params.stack_size_commit = 4 * 1024; + params.stack_size_reserve = 4 * 1024; + params.creation_flags = NT_CREATE_LOCAL_THREAD; + + if ((status = ntapi->tt_create_thread(¶ms))) + return status; + + if ((status = ntapi->zw_wait_for_single_object( + client->hswap, + NT_SYNC_NON_ALERTABLE, + 0))) + return status; + + return ntapi->zw_close(params.hthread); +} + int32_t __stdcall toks_daemon_connect(struct toks_daemon_ctx * dctx) { - nt_tty_port_msg * msg; - void * hport; + int32_t status; + nt_tty_port_msg * msg; + struct toks_client_ctx client; + nt_oa oa; - msg = &dctx->reply; - msg->ttyinfo.exarg = 0; - hport = 0; + msg = &dctx->reply; + msg->ttyinfo.exarg = 0; - ntapi->zw_accept_connect_port( - &hport, - msg->header.client_id.process_id, + client.hport = 0; + client.hprocess = 0; + client.hswap = 0; + client.cid.process_id = msg->header.client_id.process_id; + client.cid.thread_id = 0; + + oa.len = sizeof(oa); + oa.root_dir = 0; + oa.obj_name = 0; + oa.obj_attr = 0; + oa.sec_desc = 0; + oa.sec_qos = 0; + + status = ntapi->zw_open_process( + &client.hprocess, + NT_PROCESS_SYNCHRONIZE | NT_PROCESS_QUERY_INFORMATION, + &oa,&client.cid); + + status = ntapi->zw_accept_connect_port( + &client.hport, + client.cid.process_id, &msg->header, NT_LPC_ACCEPT_CONNECTION,0,0); - return ntapi->zw_complete_connect_port(hport); + if (status) { + ntapi->zw_close(client.hprocess); + return status; + } + + ntapi->zw_complete_connect_port(client.hport); + + return toks_daemon_client_instance(&client); } diff --git a/src/internal/toksvc_daemon_impl.h b/src/internal/toksvc_daemon_impl.h index 2e3643d..41f15ae 100644 --- a/src/internal/toksvc_daemon_impl.h +++ b/src/internal/toksvc_daemon_impl.h @@ -18,6 +18,13 @@ enum toks_daemon_opcodes { struct toks_daemon_ctx; struct toks_driver_ctx; +struct toks_client_ctx { + void * hprocess; + void * hport; + void * hswap; + nt_cid cid; +}; + typedef int32_t __stdcall toks_daemon_routine(struct toks_daemon_ctx *); struct toks_daemon_ctx { diff --git a/src/internal/toksvc_driver_impl.h b/src/internal/toksvc_driver_impl.h index 61a7bf1..921e721 100644 --- a/src/internal/toksvc_driver_impl.h +++ b/src/internal/toksvc_driver_impl.h @@ -31,16 +31,8 @@ struct toks_ticks { int32_t (*qpc)(nt_filetime *); }; -struct toks_client_ctx { - void * hprocess; - void * hthread; - nt_cid cid; - nt_pty_client_info clinfo; -}; - struct toks_driver_ctx_impl { nt_rtdata * rtdata; - struct toks_client_ctx clctx; struct toks_common_ctx cctx; struct toks_driver_ctx ctx; struct toks_ticks ticks;