From 1a07bea10f40e85fe912885233048a6ffe97f569 Mon Sep 17 00:00:00 2001 From: midipix Date: Jun 18 2020 21:07:44 +0000 Subject: core api: toks_service_start(): initial implementation. --- diff --git a/include/toksvc/toksvc.h b/include/toksvc/toksvc.h index 25f336d..b738139 100644 --- a/include/toksvc/toksvc.h +++ b/include/toksvc/toksvc.h @@ -135,6 +135,7 @@ toks_api void toks_driver_unset_timeout (struct toks_driver_ctx *); /* core api */ toks_api int toks_service_abort (struct toks_driver_ctx *); +toks_api int toks_service_start (struct toks_common_ctx *, int ntokens, int32_t ctrlpid, int32_t csyspid); /* client api */ toks_api int toks_client_connect (struct toks_driver_ctx *); diff --git a/project/common.mk b/project/common.mk index 5375b81..d96094b 100644 --- a/project/common.mk +++ b/project/common.mk @@ -26,6 +26,7 @@ INTERNAL_SRCS = \ src/internal/toksvc_open_impl.c \ src/log/toks_log_lpc_message.c \ src/service/toks_service_abort.c \ + src/service/toks_service_start.c \ src/skin/toks_skin_default.c \ APP_SRCS = \ diff --git a/project/extras.mk b/project/extras.mk index a93255d..fe3fc5a 100644 --- a/project/extras.mk +++ b/project/extras.mk @@ -15,6 +15,8 @@ CFLAGS_CONFIG += -D_MIDIPIX_FREESTANDING -D__NT$(HOST_BITS) CFLAGS_CONFIG += -UWIN32 -U_WIN32 -U__WIN32 -U__WIN32__ CFLAGS_CONFIG += -UWIN64 -U_WIN64 -U__WIN64 -U__WIN64__ +CFLAGS_COMMON += -DTOKS_SERVICE_IMAGE=$(BINDIR)/$(NICKNAME)$(OS_APP_SUFFIX) + LDFLAGS_COMMON += -nostdlib -lntapi -lpemagine -ldalist LDFLAGS_COMMON += -Wl,--entry -Wl,$(HOST_UNDERSCORE)$(PACKAGE)_entry_point diff --git a/src/service/toks_service_start.c b/src/service/toks_service_start.c new file mode 100644 index 0000000..f4523dc --- /dev/null +++ b/src/service/toks_service_start.c @@ -0,0 +1,169 @@ +/*********************************************************/ +/* 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_driver_impl.h" +#include "toksvc_daemon_impl.h" + +static char toks_service_image[] = "/bin/toksvc"; + +static int32_t toks_spawn_child_exit_code(void * hprocess) +{ + nt_process_basic_information pbi; + + if (ntapi->zw_query_information_process( + hprocess, + NT_PROCESS_BASIC_INFORMATION, + &pbi,sizeof(pbi), + &(uint32_t){0})) + return NT_STATUS_GENERIC_COMMAND_FAILED; + + return pbi.exit_status; +} + +static int toks_spawn_impl( + struct toks_driver_ctx * dctx, + struct toks_common_ctx * cctx, + int ntokens, + int32_t ctrlpid, + int32_t csyspid) +{ + int32_t status; + nt_spawn_process_params sparams; + nt_runtime_data rtctx; + nt_rtdata * self; + char * img; + void * hroot; + void * hlog; + const char * logfile; + char ** pearg; + char * eargv[10]; + char tokbuf[32]; + char pidbuf[32]; + + /* init */ + self = toks_get_driver_rtdata(dctx); + img = toks_service_image; + + if (!(hroot = cctx->hroot) && cctx->sysroot) + if ((status = toks_open_dir(&hroot,0,cctx->sysroot,false))) + return status; + + if (hroot && (img[0] == '/')) + img++; + + logfile = cctx->logfile; + + if (hroot && logfile && (logfile[0] == '/')) + logfile++; + + if (logfile && (status = toks_open_log_file(&hlog,hroot,logfile,false))) + return status; + + /* eargv */ + pearg = eargv; + *pearg++ = img; + + ntapi->sprintf(tokbuf,"%d",ntokens); + *pearg++ = "-t"; + *pearg++ = tokbuf; + + if (ctrlpid) { + ntapi->sprintf(pidbuf,"%d",ctrlpid); + *pearg++ = "-p"; + *pearg++ = pidbuf; + } else if (csyspid) { + ntapi->sprintf(pidbuf,"%d",csyspid); + *pearg++ = "-P"; + *pearg++ = pidbuf; + } + + *pearg++ = 0; + + /* sparams */ + ntapi->tt_aligned_block_memset( + &sparams,0,sizeof(sparams)); + + sparams.rtctx = &rtctx; + sparams.patharg = img; + sparams.argv = eargv; + sparams.envp = self->envp; + sparams.hroot = hroot ? hroot : self->hroot; + + if ((status = toks_open_file(&sparams.himage,hroot,img,true))) + return status; + + /* rtctx */ + ntapi->tt_aligned_block_memset( + &rtctx,0,sizeof(rtctx)); + + ntapi->tt_aligned_block_memcpy( + (uintptr_t *)&rtctx.cid_parent, + (uintptr_t *)&self->cid_self, + sizeof(nt_cid)); + + rtctx.hlog = hlog; + rtctx.hcwd = self->hcwd; + rtctx.hroot = sparams.hroot; + rtctx.flags = self->flags | NT_RUNTIME_DATA_TTY_TOP_LEVEL; + + rtctx.dbg_level = 0; + rtctx.log_level = self->log_level; + + rtctx.hstdin = NT_INVALID_HANDLE_VALUE; + rtctx.hstdout = NT_INVALID_HANDLE_VALUE; + rtctx.hstderr = NT_INVALID_HANDLE_VALUE; + + rtctx.stdin_type = NT_FILE_TYPE_UNKNOWN; + rtctx.stdout_type = NT_FILE_TYPE_UNKNOWN; + rtctx.stderr_type = NT_FILE_TYPE_UNKNOWN; + + /* hoppla */ + status = ntapi->tt_spawn_native_process(&sparams); + + if (sparams.himage) + ntapi->zw_close(sparams.himage); + + if (status) + return status; + + /* child ready? */ + if (!(sparams.eready.signal_state)) + status = toks_spawn_child_exit_code(sparams.hprocess); + + /* finalize */ + ntapi->zw_close(sparams.hprocess); + ntapi->zw_close(sparams.hthread); + + /* all done */ + return status; +} + +int32_t toks_service_start( + struct toks_common_ctx * cctx, + int ntokens, + int32_t ctrlpid, + int32_t csyspid) +{ + int32_t status; + struct toks_driver_ctx * dctx; + char * eargv[2]; + + eargv[0] = "toksvc.driver"; + eargv[1] = 0; + + status = toks_get_driver_ctx(eargv,0,TOKS_DRIVER_VERSION,&dctx); + + if (status == NT_STATUS_SUCCESS) { + status = toks_spawn_impl(dctx,cctx,ntokens,ctrlpid,csyspid); + toks_free_driver_ctx(dctx); + } + + return status; +}