From f9f1df16c0775f51149bab681d475d3f799760a0 Mon Sep 17 00:00:00 2001 From: midipix Date: Jun 08 2020 01:00:09 +0000 Subject: utility: added toksvc_entry_point() and related definitions. --- diff --git a/include/toksvc/toksvc.h b/include/toksvc/toksvc.h index 5733b84..982446a 100644 --- a/include/toksvc/toksvc.h +++ b/include/toksvc/toksvc.h @@ -18,6 +18,12 @@ extern "C" { #endif #endif +/* daemon */ +#define TOKS_PORT_GUID_DAEMON {0x7d36f145, \ + 0xd8f0, \ + 0x440f, \ + {0x92,0xbe,0xc3,0x53,0x61,0x7b,0xbc,0x96}} + /* status codes */ #define TOKS_OK 0x00 #define TOKS_USAGE 0x01 diff --git a/project/common.mk b/project/common.mk index 7ecc434..e41c495 100644 --- a/project/common.mk +++ b/project/common.mk @@ -8,5 +8,6 @@ INTERNAL_SRCS = \ src/internal/toksvc_ntaio_impl.c \ APP_SRCS = \ + src/toksvc.c COMMON_SRCS = $(API_SRCS) $(INTERNAL_SRCS) diff --git a/project/headers.mk b/project/headers.mk index cdd0906..34ab9b7 100644 --- a/project/headers.mk +++ b/project/headers.mk @@ -12,6 +12,7 @@ INTERNAL_HEADERS = \ $(SOURCE_DIR)/src/internal/nolibc/string.h \ $(SOURCE_DIR)/src/internal/nolibc/unistd.h \ $(SOURCE_DIR)/src/internal/toksvc_dprintf_impl.h \ + $(SOURCE_DIR)/src/internal/toksvc_init_impl.h \ $(SOURCE_DIR)/src/internal/toksvc_memfn_impl.h \ $(SOURCE_DIR)/src/internal/toksvc_nolibc_impl.h \ diff --git a/src/internal/toksvc_init_impl.h b/src/internal/toksvc_init_impl.h new file mode 100644 index 0000000..32581ff --- /dev/null +++ b/src/internal/toksvc_init_impl.h @@ -0,0 +1,24 @@ +#ifndef TOKSVC_INIT_IMPL_H +#define TOKSVC_INIT_IMPL_H + +#include +#include + +extern const ntapi_vtbl * toks_ntapi; + +static inline int toks_init(void) +{ + int32_t status; + ntapi_vtbl * pvtbl; + + if ((status = ntapi_init(&pvtbl))) + return status; + + at_locked_cas( + (intptr_t *)&toks_ntapi, + 0,(intptr_t)pvtbl); + + return 0; +} + +#endif diff --git a/src/toksvc.c b/src/toksvc.c new file mode 100644 index 0000000..3637e57 --- /dev/null +++ b/src/toksvc.c @@ -0,0 +1,183 @@ +/*********************************************************/ +/* 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_init_impl.h" +#include "toksvc_driver_impl.h" + +static const nt_guid toksvc_daemon_guid = TOKS_PORT_GUID_DAEMON; + +static void toksvc_exit(int code) +{ + /* posix exit code? */ + if ((code >= 0) && (code <= 0xff)) + code <<= 8; + + ntapi->zw_terminate_process( + NT_CURRENT_PROCESS_HANDLE, + code); +} + +static int toksvc_dbg_helper(int32_t status) +{ + return status ? -1 : 0; +} + +static int toksvc_dbg_init(char ** argv) +{ + if (argv[1] && !(ntapi->tt_strcmp_multibyte(argv[1],"--wait"))) + return toksvc_dbg_helper( + ntapi->tt_wait_for_dummy_event()); + return 0; +} + +static int32_t toksvc_start(char ** argv, char ** envp) +{ + int32_t status; + nt_runtime_data * rtdata; + nt_port_attr port_attr; + nt_pty_client_info client_info; + nt_iosb iosb; + + /* rtdata */ + if ((status = ntapi->tt_get_runtime_data(&rtdata,0))) + return status; + + if (rtdata->argv) { + argv = rtdata->argv; + envp = rtdata->envp; + } + + /* early debug (optional) */ + toksvc_dbg_init(argv); + + /* daemon */ + if (!(ntapi->tt_guid_compare( + &rtdata->srv_guid, + &(nt_guid)NT_PORT_GUID_DEFAULT))) + ntapi->tt_guid_copy( + &rtdata->srv_guid, + &toksvc_daemon_guid); + + /* no tty session? */ + if (!rtdata->tty_keys[0]) + return toks_main(argv,envp); + + /* tty */ + ntapi->tt_aligned_block_memset( + &port_attr,0,sizeof(port_attr)); + + port_attr.type = NT_PORT_TYPE_SUBSYSTEM; + port_attr.subtype = NT_PORT_SUBTYPE_DEFAULT; + + port_attr.keys.key[0] = rtdata->tty_keys[0]; + port_attr.keys.key[1] = rtdata->tty_keys[1]; + port_attr.keys.key[2] = rtdata->tty_keys[2]; + port_attr.keys.key[3] = rtdata->tty_keys[3]; + port_attr.keys.key[4] = rtdata->tty_keys[4]; + port_attr.keys.key[5] = rtdata->tty_keys[5]; + + ntapi->tt_guid_copy( + &port_attr.guid, + &rtdata->tty_guid); + + if ((status = ntapi->tty_join_session( + &rtdata->hsession,0, + &port_attr, + NT_TTY_SESSION_PRIMARY))) + return status; + + /* pty */ + if ((status = ntapi->pty_inherit_runtime_ctty( + rtdata->hsession, + rtdata))) + return status; + + /* ctty identification */ + if (rtdata->hctty) { + client_info.any[0] = 0; + client_info.any[1] = 0; + client_info.any[2] = 0; + client_info.any[3] = 0; + + if ((status = ntapi->pty_set( + rtdata->hctty,&iosb, + &client_info,sizeof(client_info), + NT_PTY_CLIENT_INFORMATION))) + return status; + } + + /* ready */ + if (rtdata->hready) + if ((status = ntapi->zw_set_event(rtdata->hready,0))) + return status; + + /* main */ + return toks_main(argv,envp); +} + +static int __stdcall toksvc_daemon_entry_point(void * hswap) +{ + int32_t status; + int argc; + char ** argv; + char ** envp; + + ntapi->zw_set_event(hswap,0); + ntapi->zw_close(hswap); + + if ((status = ntapi->tt_get_argv_envp_utf8( + &argc,&argv,&envp, + 0,0,0))) + toksvc_exit(status); + + toksvc_exit(toksvc_start(argv,envp)); + + return NT_STATUS_INTERNAL_ERROR; +} + +int toksvc_entry_point(void) +{ + int32_t status; + nt_thread_params params; + void * hswap; + + if ((status = toks_init())) + return status; + + if ((status = ntapi->tt_create_private_event( + &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 = toksvc_daemon_entry_point; + params.arg = hswap; + params.stack_size_commit = 128 * 1024; + params.stack_size_reserve = 128 * 1024; + params.creation_flags = NT_CREATE_LOCAL_THREAD; + + if ((status = ntapi->tt_create_thread(¶ms))) + toksvc_exit(status); + + ntapi->zw_wait_for_single_object( + hswap,NT_SYNC_NON_ALERTABLE,0); + + ntapi->zw_close( + params.hthread); + + ntapi->zw_terminate_thread( + NT_CURRENT_THREAD_HANDLE, + NT_STATUS_SUCCESS); + + return NT_STATUS_INTERNAL_ERROR; +}