Blame src/service/toks_service_start.c

1a07be
/*********************************************************/
1a07be
/*  toksvc: a framework-native token broker service      */
1a07be
/*  Copyright (C) 2020  Z. Gilboa                        */
1a07be
/*  Released under GPLv2 and GPLv3; see COPYING.TOKSVC.  */
1a07be
/*********************************************************/
1a07be
1a07be
#include <psxtypes/psxtypes.h>
1a07be
#include <ntapi/ntapi.h>
1a07be
1a07be
#include <toksvc/toksvc.h>
1a07be
#include "toksvc_driver_impl.h"
1a07be
#include "toksvc_daemon_impl.h"
1a07be
1a07be
static char toks_service_image[] = "/bin/toksvc";
1a07be
1a07be
static int32_t toks_spawn_child_exit_code(void * hprocess)
1a07be
{
1a07be
	nt_process_basic_information pbi;
1a07be
1a07be
	if (ntapi->zw_query_information_process(
1a07be
			hprocess,
1a07be
			NT_PROCESS_BASIC_INFORMATION,
1a07be
			&pbi,sizeof(pbi),
1a07be
			&(uint32_t){0}))
1a07be
		return NT_STATUS_GENERIC_COMMAND_FAILED;
1a07be
1a07be
	return pbi.exit_status;
1a07be
}
1a07be
1a07be
static int toks_spawn_impl(
1a07be
	struct toks_driver_ctx *        dctx,
1a07be
	struct toks_common_ctx *        cctx,
1a07be
	int                             ntokens,
1a07be
	int32_t                         ctrlpid,
1a07be
	int32_t                         csyspid)
1a07be
{
1a07be
	int32_t				status;
1a07be
	nt_spawn_process_params		sparams;
1a07be
	nt_runtime_data			rtctx;
1a07be
	nt_rtdata *			self;
1a07be
	char *				img;
1a07be
	void *				hroot;
1a07be
	void *				hlog;
1a07be
	const char *			logfile;
1a07be
	char **				pearg;
1a07be
	char *				eargv[10];
1a07be
	char				tokbuf[32];
1a07be
	char				pidbuf[32];
1a07be
1a07be
	/* init */
1a07be
	self = toks_get_driver_rtdata(dctx);
1a07be
	img  = toks_service_image;
1a07be
1a07be
	if (!(hroot = cctx->hroot) && cctx->sysroot)
1a07be
		if ((status = toks_open_dir(&hroot,0,cctx->sysroot,false)))
1a07be
			return status;
1a07be
1a07be
	if (hroot && (img[0] == '/'))
1a07be
		img++;
1a07be
1a07be
	logfile = cctx->logfile;
1a07be
1a07be
	if (hroot && logfile && (logfile[0] == '/'))
1a07be
		logfile++;
1a07be
1a07be
	if (logfile && (status = toks_open_log_file(&hlog,hroot,logfile,false)))
1a07be
		return status;
1a07be
1a07be
	/* eargv */
1a07be
	pearg    = eargv;
1a07be
	*pearg++ = img;
1a07be
1a07be
	ntapi->sprintf(tokbuf,"%d",ntokens);
1a07be
	*pearg++ = "-t";
1a07be
	*pearg++ = tokbuf;
1a07be
1a07be
	if (ctrlpid) {
1a07be
		ntapi->sprintf(pidbuf,"%d",ctrlpid);
1a07be
		*pearg++ = "-p";
1a07be
		*pearg++ = pidbuf;
1a07be
	} else if (csyspid) {
1a07be
		ntapi->sprintf(pidbuf,"%d",csyspid);
1a07be
		*pearg++ = "-P";
1a07be
		*pearg++ = pidbuf;
1a07be
	}
1a07be
1a07be
	*pearg++ = 0;
1a07be
1a07be
	/* sparams */
1a07be
	ntapi->tt_aligned_block_memset(
1a07be
		&sparams,0,sizeof(sparams));
1a07be
1a07be
	sparams.rtctx    = &rtctx;
1a07be
	sparams.patharg  = img;
1a07be
	sparams.argv     = eargv;
1a07be
	sparams.envp     = self->envp;
1a07be
	sparams.hroot    = hroot ? hroot : self->hroot;
1a07be
1a07be
	if ((status = toks_open_file(&sparams.himage,hroot,img,true)))
1a07be
		return status;
1a07be
1a07be
	/* rtctx */
1a07be
	ntapi->tt_aligned_block_memset(
1a07be
		&rtctx,0,sizeof(rtctx));
1a07be
1a07be
	ntapi->tt_aligned_block_memcpy(
1a07be
		(uintptr_t *)&rtctx.cid_parent,
1a07be
		(uintptr_t *)&self->cid_self,
1a07be
		sizeof(nt_cid));
1a07be
1a07be
	rtctx.hlog  = hlog;
1a07be
	rtctx.hcwd  = self->hcwd;
1a07be
	rtctx.hroot = sparams.hroot;
1a07be
	rtctx.flags = self->flags | NT_RUNTIME_DATA_TTY_TOP_LEVEL;
1a07be
1a07be
	rtctx.dbg_level = 0;
1a07be
	rtctx.log_level = self->log_level;
1a07be
1a07be
	rtctx.hstdin      = NT_INVALID_HANDLE_VALUE;
1a07be
	rtctx.hstdout     = NT_INVALID_HANDLE_VALUE;
1a07be
	rtctx.hstderr     = NT_INVALID_HANDLE_VALUE;
1a07be
1a07be
	rtctx.stdin_type  = NT_FILE_TYPE_UNKNOWN;
1a07be
	rtctx.stdout_type = NT_FILE_TYPE_UNKNOWN;
1a07be
	rtctx.stderr_type = NT_FILE_TYPE_UNKNOWN;
1a07be
1a07be
	/* hoppla */
1a07be
	status = ntapi->tt_spawn_native_process(&sparams);
1a07be
1a07be
	if (sparams.himage)
1a07be
		ntapi->zw_close(sparams.himage);
1a07be
1a07be
	if (status)
1a07be
		return status;
1a07be
1a07be
	/* child ready? */
1a07be
	if (!(sparams.eready.signal_state))
1a07be
		status = toks_spawn_child_exit_code(sparams.hprocess);
1a07be
1a07be
	/* finalize */
1a07be
	ntapi->zw_close(sparams.hprocess);
1a07be
	ntapi->zw_close(sparams.hthread);
1a07be
1a07be
	/* all done */
1a07be
	return status;
1a07be
}
1a07be
1a07be
int32_t toks_service_start(
1a07be
	struct toks_common_ctx *    cctx,
1a07be
	int                         ntokens,
1a07be
	int32_t                     ctrlpid,
1a07be
	int32_t                     csyspid)
1a07be
{
1a07be
	int32_t                     status;
1a07be
	struct toks_driver_ctx *    dctx;
1a07be
	char *                      eargv[2];
1a07be
1a07be
	eargv[0] = "toksvc.driver";
1a07be
	eargv[1] = 0;
1a07be
1a07be
	status = toks_get_driver_ctx(eargv,0,TOKS_DRIVER_VERSION,&dctx);
1a07be
1a07be
	if (status == NT_STATUS_SUCCESS) {
1a07be
		status = toks_spawn_impl(dctx,cctx,ntokens,ctrlpid,csyspid);
1a07be
		toks_free_driver_ctx(dctx);
1a07be
	}
1a07be
1a07be
	return status;
1a07be
}