Blame src/driver/ntux_driver_ctx.c

26e14f
/***********************************************************/
26e14f
/*  ntux: native translation und extension                 */
14ffae
/*  Copyright (C) 2016--2021  SysDeer Technologies, LLC    */
26e14f
/*  Released under GPLv2 and GPLv3; see COPYING.NTUX.      */
26e14f
/***********************************************************/
26e14f
471bb0
#include <psxabi/sys_abitypes.h>
ec0005
#include <psxabi/sys_fcntl.h>
b824c9
#include <psxabi/sys_process.h>
b824c9
#include <psxabi/sys_sysapi.h>
b824c9
#include <psxxfi/xfi_framework.h>
471bb0
#include <psxscl/psxscl.h>
471bb0
471bb0
#include <ntapi/nt_tty.h>
26e14f
#include <stdint.h>
b824c9
#include <stdbool.h>
26e14f
26e14f
#include <ntux/ntux.h>
26e14f
#include "ntux_nolibc_impl.h"
26e14f
26e14f
#define ARGV_DRIVER
26e14f
26e14f
#include "ntux_version.h"
26e14f
#include "ntux_driver_impl.h"
26e14f
#include "argv/argv.h"
26e14f
26e14f
/* framework integration */
26e14f
#include <psxtypes/section/midipix.h>
26e14f
26e14f
__attr_section_decl__(".midipix")
26e14f
static const nt_tty_affiliation tty_affiliation
26e14f
	__attr_section__(".midipix")
26e14f
	= NT_TTY_AFFILIATION_DEFAULT;
26e14f
c27a50
/* ntux command names */
c27a50
static const char * const ntux_cmd_name[NTUX_CMD_CAP] = {
c27a50
	[NTUX_CMD_STAT]       = "stat",
c27a50
	[NTUX_CMD_SPAWN]      = "spawn",
c27a50
	[NTUX_CMD_STRACE]     = "strace",
80f43a
	[NTUX_CMD_CHMOD]      = "chmod",
dcb685
	[NTUX_CMD_ACEIT]      = "aceit",
8ec5ce
	[NTUX_CMD_FSPATH]     = "fspath",
b824c9
	[NTUX_CMD_BRIDGE]     = "bridge",
b824c9
};
b824c9
b824c9
/* ntux exec commands */
b824c9
static const bool const ntux_cmd_exec[NTUX_CMD_CAP] = {
b824c9
	[NTUX_CMD_STAT]       = false,
b824c9
	[NTUX_CMD_SPAWN]      = true,
b824c9
	[NTUX_CMD_STRACE]     = true,
b824c9
	[NTUX_CMD_CHMOD]      = false,
b824c9
	[NTUX_CMD_ACEIT]      = false,
b824c9
	[NTUX_CMD_FSPATH]     = false,
b824c9
	[NTUX_CMD_BRIDGE]     = true,
c27a50
};
c27a50
c27a50
/* ntux command options */
c27a50
static const struct argv_option * ntux_cmd_options[NTUX_CMD_CAP] = {
c27a50
	[NTUX_CMD_DEFAULT]    = ntux_default_options,
c27a50
	[NTUX_CMD_STAT]       = ntux_default_options,
c27a50
	[NTUX_CMD_SPAWN]      = ntux_spawn_options,
c27a50
	[NTUX_CMD_STRACE]     = ntux_strace_options,
80f43a
	[NTUX_CMD_CHMOD]      = ntux_chmod_options,
dcb685
	[NTUX_CMD_ACEIT]      = ntux_aceit_options,
8ec5ce
	[NTUX_CMD_FSPATH]     = ntux_fspath_options,
b824c9
	[NTUX_CMD_BRIDGE]     = ntux_bridge_options,
c27a50
};
c27a50
26e14f
/* package info */
26e14f
static const struct ntux_source_version ntux_src_version = {
26e14f
	NTUX_TAG_VER_MAJOR,
26e14f
	NTUX_TAG_VER_MINOR,
26e14f
	NTUX_TAG_VER_PATCH,
26e14f
	NTUX_GIT_VERSION
26e14f
};
26e14f
26e14f
struct ntux_driver_ctx_alloc {
26e14f
	struct argv_meta *		meta;
26e14f
	struct ntux_driver_ctx_impl	ctx;
26e14f
	uint64_t			guard;
26e14f
	const char *			units[];
26e14f
};
26e14f
26e14f
static uint32_t ntux_argv_flags(uint32_t flags)
26e14f
{
ecd0a7
	uint32_t ret = ARGV_CLONE_VECTOR;
26e14f
26e14f
	if (flags & NTUX_DRIVER_VERBOSITY_NONE)
26e14f
		ret |= ARGV_VERBOSITY_NONE;
26e14f
26e14f
	if (flags & NTUX_DRIVER_VERBOSITY_ERRORS)
26e14f
		ret |= ARGV_VERBOSITY_ERRORS;
26e14f
26e14f
	if (flags & NTUX_DRIVER_VERBOSITY_STATUS)
26e14f
		ret |= ARGV_VERBOSITY_STATUS;
26e14f
26e14f
	return ret;
26e14f
}
26e14f
26e14f
static int ntux_driver_usage(
ec0005
	int				fdout,
26e14f
	const char *			program,
26e14f
	const char *			arg,
2404cd
	const struct argv_option **	optv,
c27a50
	struct argv_meta *		meta,
c27a50
	enum ntux_cmd			cmd)
26e14f
{
c27a50
	char		header[512];
c27a50
	char *		cmdarg[2];
c27a50
	const char *	cmdname;
c27a50
c27a50
	if (cmd == NTUX_CMD_DEFAULT) {
c27a50
		cmdarg[0] = "";
c27a50
		cmdarg[1] = "";
c27a50
		cmdname   = "";
c27a50
	} else {
c27a50
		cmdarg[0] = " (--cmd=";
c27a50
		cmdarg[1] = ")";
c27a50
		cmdname   = ntux_cmd_name[cmd];
c27a50
	}
26e14f
26e14f
	snprintf(header,sizeof(header),
6a7c02
		"Usage: %s [options] ...\n"
7deda5
		"Usage: %s [options] [--cmd=<command>] <arg> <arg> ...\n\n"
7deda5
		"Notes: --cmd must precede all non-option arguments, as well as\n"
7deda5
		"       all arguments that are specific to the selected command;\n"
7deda5
		"       for commands that spawn other programs (e.g. spawn, strace),\n"
7deda5
		"       the first non-option argument must specify the name of the\n"
7deda5
		"       program to be executed.\n\n"
c27a50
		"Options%s%s%s:\n",
c27a50
		program,program,cmdarg[0],cmdname,cmdarg[1]);
26e14f
ec0005
	argv_usage(fdout,header,optv,arg);
26e14f
	argv_free(meta);
26e14f
26e14f
	return NTUX_USAGE;
26e14f
}
26e14f
26e14f
static struct ntux_driver_ctx_impl * ntux_driver_ctx_alloc(
26e14f
	struct argv_meta *		meta,
ec0005
	const struct ntux_fd_ctx *	fdctx,
26e14f
	const struct ntux_common_ctx *	cctx,
26e14f
	size_t				nunits)
26e14f
{
26e14f
	struct ntux_driver_ctx_alloc *	ictx;
26e14f
	size_t				size;
26e14f
	struct argv_entry *		entry;
26e14f
	const char **			units;
26e14f
	int				elements;
26e14f
26e14f
	(void)tty_affiliation;
26e14f
26e14f
	size =  sizeof(struct ntux_driver_ctx_alloc);
26e14f
	size += (nunits+1)*sizeof(const char *);
26e14f
26e14f
	if (!(ictx = calloc(1,size)))
26e14f
		return 0;
26e14f
26e14f
	elements = sizeof(ictx->ctx.erribuf) / sizeof(*ictx->ctx.erribuf);
26e14f
26e14f
	ictx->ctx.errinfp  = &ictx->ctx.erriptr[0];
26e14f
	ictx->ctx.erricap  = &ictx->ctx.erriptr[--elements];
26e14f
ec0005
	memcpy(&ictx->ctx.fdctx,fdctx,sizeof(*fdctx));
ec0005
	memcpy(&ictx->ctx.cctx,cctx,sizeof(*cctx));
26e14f
26e14f
	for (entry=meta->entries,units=ictx->units; entry->fopt || entry->arg; entry++)
26e14f
		if (!entry->fopt)
26e14f
			*units++ = entry->arg;
26e14f
26e14f
	ictx->meta = meta;
26e14f
	ictx->ctx.ctx.units = ictx->units;
26e14f
	ictx->ctx.ctx.errv  = ictx->ctx.errinfp;
26e14f
	return &ictx->ctx;
26e14f
}
26e14f
7deda5
static int ntux_cctx_update(
7deda5
	const char *			program,
7deda5
	const struct argv_option **	optv,
7deda5
	struct argv_meta *		meta,
7deda5
	uint32_t			flags,
ec0005
	const struct ntux_fd_ctx *	fdctx,
7deda5
	struct ntux_common_ctx *	cctx,
7deda5
	size_t *			nunits)
7deda5
{
7deda5
	struct argv_entry *		entry;
7deda5
7deda5
	/* get options, count units */
7deda5
	for (entry=meta->entries; entry->fopt || entry->arg; entry++) {
7deda5
		if (entry->fopt) {
7deda5
			switch (entry->tag) {
7deda5
				case TAG_HELP:
7deda5
					if (flags & NTUX_DRIVER_VERBOSITY_USAGE)
7deda5
						return ntux_driver_usage(
ec0005
							fdctx->fdout,
7deda5
							program,entry->arg,
c27a50
							optv,0,cctx->cmd);
7deda5
7deda5
				case TAG_VERSION:
7deda5
					cctx->drvflags |= NTUX_DRIVER_VERSION;
7deda5
					break;
7deda5
80f43a
				case TAG_VERBOSE:
80f43a
					cctx->drvflags |= NTUX_DRIVER_VERBOSE;
80f43a
					break;
80f43a
80f43a
				case TAG_SILENT:
80f43a
					cctx->drvflags |= NTUX_DRIVER_SILENT;
80f43a
					break;
80f43a
7deda5
				case TAG_CMD:
7deda5
					if (*nunits)
7deda5
						return ntux_driver_usage(
ec0005
							fdctx->fderr,
7deda5
							program,entry->arg,
c27a50
							optv,0,cctx->cmd);
7deda5
7deda5
					if (!strcmp(entry->arg,"stat"))
7deda5
						cctx->cmd = NTUX_CMD_STAT;
7deda5
7deda5
					else if (!strcmp(entry->arg,"spawn"))
7deda5
						cctx->cmd = NTUX_CMD_SPAWN;
7deda5
7deda5
					else if (!strcmp(entry->arg,"strace"))
7deda5
						cctx->cmd = NTUX_CMD_STRACE;
7deda5
80f43a
					else if (!strcmp(entry->arg,"chmod"))
80f43a
						cctx->cmd = NTUX_CMD_CHMOD;
80f43a
dcb685
					else if (!strcmp(entry->arg,"aceit"))
dcb685
						cctx->cmd = NTUX_CMD_ACEIT;
dcb685
8ec5ce
					else if (!strcmp(entry->arg,"fspath"))
8ec5ce
						cctx->cmd = NTUX_CMD_FSPATH;
8ec5ce
b824c9
					else if (!strcmp(entry->arg,"bridge"))
b824c9
						cctx->cmd = NTUX_CMD_BRIDGE;
b824c9
7deda5
					break;
9682ca
9682ca
				case TAG_LOADER:
9682ca
					cctx->loader = entry->arg;
9682ca
					break;
9682ca
9682ca
				case TAG_LOGFILE:
9682ca
					cctx->logfile = entry->arg;
9682ca
					break;
80f43a
d7daca
				case TAG_REFOBJ:
d7daca
					cctx->refobj = entry->arg;
80f43a
					break;
80f43a
80f43a
				case TAG_STRMODE:
80f43a
					cctx->strmode = entry->arg;
80f43a
					break;
80f43a
80f43a
				case TAG_RECURSIVE:
80f43a
					cctx->drvflags |= NTUX_DRIVER_RECURSIVE;
80f43a
					break;
80f43a
80f43a
				case TAG_CHANGES:
80f43a
					cctx->drvflags |= NTUX_DRIVER_CHANGES;
80f43a
					break;
80f43a
de8f1c
				case TAG_OWNER:
de8f1c
					cctx->owner = entry->arg;
de8f1c
					break;
de8f1c
de8f1c
				case TAG_GROUP:
de8f1c
					cctx->group = entry->arg;
de8f1c
					break;
de8f1c
80f43a
				case TAG_ROOTED:
80f43a
					cctx->drvflags &= ~(uint64_t)NTUX_DRIVER_ROOTLESS;
80f43a
					cctx->drvflags |= NTUX_DRIVER_ROOTED;
80f43a
					break;
80f43a
80f43a
				case TAG_ROOTLESS:
80f43a
					cctx->drvflags &= ~(uint64_t)NTUX_DRIVER_ROOTED;
80f43a
					cctx->drvflags |= NTUX_DRIVER_ROOTLESS;
80f43a
					break;
dcb685
dcb685
				case TAG_DUMP:
dcb685
					cctx->drvflags |= NTUX_DRIVER_DUMP;
dcb685
					break;
8ec5ce
b824c9
				case TAG_INTERP:
b824c9
					cctx->interp = entry->arg;
b824c9
					break;
b824c9
b824c9
				case TAG_OPTARG:
b824c9
					cctx->optarg = entry->arg;
b824c9
					break;
b824c9
8ec5ce
				case TAG_SYNTAX:
8ec5ce
				case TAG_RPATH:
8ec5ce
				case TAG_APATH:
8ec5ce
				case TAG_NPATH:
8ec5ce
				case TAG_DPATH:
8ec5ce
					cctx->drvflags &= ~(uint64_t)NTUX_DRIVER_RPATH;
8ec5ce
					cctx->drvflags &= ~(uint64_t)NTUX_DRIVER_APATH;
8ec5ce
					cctx->drvflags &= ~(uint64_t)NTUX_DRIVER_NPATH;
8ec5ce
					cctx->drvflags &= ~(uint64_t)NTUX_DRIVER_DPATH;
8ec5ce
8ec5ce
					if (entry->tag == TAG_SYNTAX) {
8ec5ce
						if (!strcmp(entry->arg,"relative"))
8ec5ce
							cctx->drvflags |= NTUX_DRIVER_RPATH;
8ec5ce
8ec5ce
						else if (!strcmp(entry->arg,"absolute"))
8ec5ce
							cctx->drvflags |= NTUX_DRIVER_APATH;
8ec5ce
8ec5ce
						else if (!strcmp(entry->arg,"native"))
8ec5ce
							cctx->drvflags |= NTUX_DRIVER_NPATH;
8ec5ce
8ec5ce
						else if (!strcmp(entry->arg,"driver"))
8ec5ce
							cctx->drvflags |= NTUX_DRIVER_DPATH;
8ec5ce
8ec5ce
					} else if (entry->tag == TAG_RPATH) {
8ec5ce
						cctx->drvflags |= NTUX_DRIVER_RPATH;
8ec5ce
8ec5ce
					} else if (entry->tag == TAG_APATH) {
8ec5ce
						cctx->drvflags |= NTUX_DRIVER_APATH;
8ec5ce
8ec5ce
					} else if (entry->tag == TAG_NPATH) {
8ec5ce
						cctx->drvflags |= NTUX_DRIVER_NPATH;
8ec5ce
8ec5ce
					} else if (entry->tag == TAG_DPATH) {
8ec5ce
						cctx->drvflags |= NTUX_DRIVER_DPATH;
8ec5ce
					}
8ec5ce
8ec5ce
					break;
7deda5
			}
7deda5
		} else {
7deda5
			(*nunits)++;
7deda5
		}
7deda5
	}
7deda5
7deda5
	return 0;
7deda5
}
7deda5
26e14f
static int ntux_get_driver_ctx_fail(struct argv_meta * meta)
26e14f
{
26e14f
	argv_free(meta);
26e14f
	return -1;
26e14f
}
26e14f
b824c9
static int ntux_cmd_program_is_bridge(const char * program)
b824c9
{
b824c9
	const char *	slash;
b824c9
	const char *	mark;
b824c9
b824c9
	mark = (slash = strrchr(program,'/'))
b824c9
		? ++slash : program;
b824c9
b824c9
	if (!strcmp(mark,"ntux.bridge.exe"))
b824c9
		return true;
b824c9
b824c9
	else if (!strcmp(mark,"ntux.bridge.exe.interp"))
b824c9
		return true;
b824c9
b824c9
	else if (!strcmp(mark,"ntux.bridge.exe.interp.abspath"))
b824c9
		return true;
b824c9
b824c9
	else if (!strcmp(mark,"ntux.bridge.exe.interp.cwdpath"))
b824c9
		return true;
b824c9
b824c9
	return false;
b824c9
}
b824c9
c27a50
static int ntux_cmd_from_program(const char * program)
c27a50
{
c27a50
	const char *	dot;
c27a50
	const char *	hyphen;
c27a50
	const char *	mark;
c27a50
c27a50
	dot    = strrchr(program,'.');
c27a50
	hyphen = strrchr(program,'-');
c27a50
c27a50
	if (hyphen > dot)
c27a50
		mark = ++hyphen;
c27a50
	else if (dot > hyphen)
c27a50
		mark = ++dot;
c27a50
	else
c27a50
		mark = program;
c27a50
b824c9
	if (!strcmp(mark,"stat")) {
c27a50
		return NTUX_CMD_STAT;
b824c9
b824c9
	} else if (!strcmp(mark,"spawn")) {
c27a50
		return NTUX_CMD_SPAWN;
b824c9
b824c9
	} else if (!strcmp(mark,"strace")) {
c27a50
		return NTUX_CMD_STRACE;
b824c9
b824c9
	} else if (!strcmp(mark,"chmod")) {
80f43a
		return NTUX_CMD_CHMOD;
b824c9
b824c9
	} else if (!strcmp(mark,"aceit")) {
dcb685
		return NTUX_CMD_ACEIT;
b824c9
b824c9
	} else if (!strcmp(mark,"fspath")) {
8ec5ce
		return NTUX_CMD_FSPATH;
c27a50
b824c9
	} else if (!strcmp(mark,"bridge")) {
b824c9
		return NTUX_CMD_BRIDGE;
b824c9
b824c9
	} else if (ntux_cmd_program_is_bridge(program)) {
b824c9
		return NTUX_CMD_BRIDGE;
b824c9
b824c9
	} else {
b824c9
		return NTUX_CMD_DEFAULT;
b824c9
	}
c27a50
}
c27a50
26e14f
int ntux_get_driver_ctx(
26e14f
	char **				argv,
26e14f
	char **				envp,
26e14f
	uint32_t			flags,
ec0005
	const struct ntux_fd_ctx *	fdctx,
26e14f
	struct ntux_driver_ctx **	pctx)
26e14f
{
b824c9
	int				ret;
b824c9
	int				val;
7deda5
	struct ntux_driver_ctx_impl *	ictx;
26e14f
	struct ntux_common_ctx		cctx;
2404cd
	const struct argv_option *	optv[NTUX_OPTV_ELEMENTS];
26e14f
	struct argv_meta *		meta;
26e14f
	size_t				nunits;
26e14f
	const char *			program;
7deda5
	char **				parg;
4fe92c
	char **				pargcap;
7deda5
	char **				cmdargv;
7deda5
	char *				cmdmark;
7deda5
	char **				execargv;
7deda5
	char *				execarg;
b824c9
	char *				interp;
b824c9
	char *				pathbuf;
b824c9
	size_t				pathlen;
7deda5
	struct argv_ctx			ctx = {ARGV_VERBOSITY_NONE,
7deda5
                                               ARGV_MODE_SCAN,
7deda5
                                               0,0,0,0,0,0,0};
7deda5
ec0005
	/* fdctx */
ec0005
	if (!fdctx) {
ec0005
		fdctx = &(const struct ntux_fd_ctx) {
ec0005
			.fdin  = STDIN_FILENO,
ec0005
			.fdout = STDOUT_FILENO,
ec0005
			.fderr = STDERR_FILENO,
ec0005
			.fdlog = (-1),
ec0005
			.fdcwd = AT_FDCWD,
ec0005
			.fddst = AT_FDCWD,
ec0005
		};
ec0005
	}
ec0005
ec0005
	/* cctx */
26e14f
	memset(&cctx,0,sizeof(cctx));
7deda5
7deda5
	nunits	      = 0;
7deda5
	execargv      = 0;
c27a50
	program       = argv_program_name(argv[0]);
c27a50
	cctx.cmd      = ntux_cmd_from_program(program);
c27a50
	cctx.drvflags = flags;
7deda5
b824c9
	switch (cctx.cmd) {
b824c9
		case NTUX_CMD_BRIDGE:
b824c9
			break;
b824c9
b824c9
		default:
b824c9
			/* potential .exe bridge setup? */
b824c9
			pathlen = ntux_strlen(argv[0]);
b824c9
b824c9
			if (pathlen + NTUX_EXE_SUFFIX_LEN >= NTUX_MAX_PATH)
b824c9
				break;
b824c9
b824c9
b824c9
			/* intermediate buffers */
b824c9
			if (!(pathbuf = ntux_calloc(1,NTUX_MAX_PATH)))
b824c9
				return NTUX_ERROR;
b824c9
b824c9
			if (!(interp = ntux_calloc(1,NTUX_MAX_PATH))) {
b824c9
				ntux_free(pathbuf);
b824c9
				return NTUX_ERROR;
b824c9
			}
b824c9
b824c9
			/* is argv[0] a symlink to ntux's bridge symlink? */
b824c9
			ret = __sys_readlinkat(
b824c9
				fdctx->fdcwd,
b824c9
				(const unsigned char *)argv[0],
b824c9
				pathbuf,NTUX_MAX_PATH);
b824c9
b824c9
			if ((ret > 0) && (ret < NTUX_MAX_PATH)) {
b824c9
				if (ntux_cmd_program_is_bridge(pathbuf)) {
b824c9
					ntux_strcpy(pathbuf,argv[0]);
b824c9
					ntux_strcpy(&pathbuf[pathlen],NTUX_EXE_SUFFIX);
b824c9
b824c9
					ret = __sys_readlinkat(
b824c9
						fdctx->fdcwd,
b824c9
						(const unsigned char *)pathbuf,
b824c9
						interp,NTUX_MAX_PATH);
b824c9
b824c9
					ntux_free(pathbuf);
b824c9
b824c9
					if ((ret < 0) || (ret >= NTUX_MAX_PATH)) {
b824c9
						ntux_free(interp);
b824c9
						return NTUX_ERROR;
b824c9
					}
b824c9
b824c9
					cctx.cmd    = NTUX_CMD_BRIDGE;
b824c9
					cctx.interp = interp;
b824c9
				}
b824c9
			}
b824c9
b824c9
			break;
b824c9
	}
b824c9
7deda5
	/* missing arguments? */
c27a50
	argv_optv_init(ntux_cmd_options[cctx.cmd],optv);
26e14f
26e14f
	if (!argv[1] && (flags & NTUX_DRIVER_VERBOSITY_USAGE))
ec0005
		return ntux_driver_usage(
ec0005
			fdctx->fderr,
ec0005
			program,0,
ec0005
			optv,0,cctx.cmd);
7deda5
7deda5
	/* initial argv scan: ... --cmd=xxx ... */
dc1caf
	argv_scan(argv,optv,&ctx,0);
dc1caf
4fe92c
	/* position of last base ntux argument */
4fe92c
	if (ctx.erridx && ctx.unitidx)
4fe92c
		pargcap = &argv[ctx.unitidx];
4fe92c
4fe92c
	else if (ctx.erridx)
4fe92c
		pargcap = &argv[ctx.erridx];
4fe92c
4fe92c
	else
4fe92c
		for (pargcap=argv; *pargcap; pargcap++)
4fe92c
			(void)0;
dc1caf
dc1caf
	/* scan for --cmd */
4fe92c
	for (parg=argv, cmdargv=0; (parg
7deda5
		if (!strcmp(*parg,"--cmd") && parg[1]) {
7deda5
				cmdargv = &parg[2];
7deda5
				cmdmark = parg[2];
7deda5
		} else if (!strncmp(*parg,"--cmd=",6)) {
7deda5
				cmdargv = &parg[1];
7deda5
				cmdmark = parg[1];
7deda5
		}
7deda5
	}
26e14f
4fe92c
	/* invalid ntux arguments? */
4fe92c
	if (!ctx.erridx) {
4fe92c
		(void)0;
4fe92c
4fe92c
	} else if (&argv[ctx.erridx] >= cmdargv) {
4fe92c
		(void)0;
4fe92c
4fe92c
	} else if ((cctx.cmd == NTUX_CMD_SPAWN) && (ctx.erridx > ctx.unitidx)) {
4fe92c
		(void)0;
4fe92c
4fe92c
	} else if ((cctx.cmd == NTUX_CMD_STRACE) && (ctx.erridx > ctx.unitidx)) {
4fe92c
		(void)0;
4fe92c
4fe92c
	} else {
4fe92c
		if (flags & NTUX_DRIVER_VERBOSITY_ERRORS)
4fe92c
			argv_get(
4fe92c
				argv,optv,
4fe92c
				ARGV_VERBOSITY_ERRORS,
4fe92c
				fdctx->fderr);
4fe92c
		return -1;
4fe92c
	}
4fe92c
7deda5
	/* process argv entries preceding --cmd */
7deda5
	if (cmdargv) {
7deda5
		*cmdargv = 0;
26e14f
1a5016
		if (!(meta = argv_get(
1a5016
				argv,optv,
1a5016
				ntux_argv_flags(flags),
ec0005
				fdctx->fderr)))
7deda5
			return -1;
6a7c02
ec0005
		if (ntux_cctx_update(
ec0005
				program,optv,meta,flags,
ec0005
				fdctx,&cctx,&nunits)) {
7deda5
			argv_free(meta);
7deda5
			return -1;
7deda5
		}
7deda5
89e9e6
		argv_free(meta);
89e9e6
7deda5
		*cmdargv-- = cmdmark;
7deda5
		*cmdargv   = argv[0];
7deda5
		argv       = cmdargv;
7deda5
	}
7deda5
7deda5
	/* set option vector by command */
7deda5
	if (cctx.cmd == NTUX_CMD_SPAWN)
7deda5
		argv_optv_init(ntux_spawn_options,optv);
7deda5
7deda5
	else if (cctx.cmd == NTUX_CMD_STRACE)
7deda5
		argv_optv_init(ntux_strace_options,optv);
7deda5
80f43a
	else if (cctx.cmd == NTUX_CMD_CHMOD)
80f43a
		argv_optv_init(ntux_chmod_options,optv);
80f43a
dcb685
	else if (cctx.cmd == NTUX_CMD_ACEIT)
dcb685
		argv_optv_init(ntux_aceit_options,optv);
dcb685
8ec5ce
	else if (cctx.cmd == NTUX_CMD_FSPATH)
8ec5ce
		argv_optv_init(ntux_fspath_options,optv);
8ec5ce
b824c9
	else if (cctx.cmd == NTUX_CMD_BRIDGE)
b824c9
		argv_optv_init(ntux_bridge_options,optv);
b824c9
b824c9
	/* spawn, strace, bridge */
b824c9
	if (ntux_cmd_exec[cctx.cmd]) {
7deda5
		argv_scan(argv,optv,&ctx,0);
7deda5
7deda5
		if (ctx.erridx && !ctx.unitidx) {
7deda5
			if (flags & NTUX_DRIVER_VERBOSITY_ERRORS)
7deda5
				argv_get(
7deda5
					argv,optv,
1a5016
					ntux_argv_flags(flags),
ec0005
					fdctx->fderr);
7deda5
			return -1;
7deda5
		}
7deda5
7deda5
		if (!ctx.unitidx && (flags & NTUX_DRIVER_VERBOSITY_USAGE))
ec0005
			ntux_driver_usage(
ec0005
				fdctx->fderr,
ec0005
				program,0,
ec0005
				optv,0,cctx.cmd);
7deda5
7deda5
		if (!ctx.unitidx)
7deda5
			return NTUX_ERROR;
7deda5
7deda5
		execargv  = &argv[ctx.unitidx];
7deda5
		execarg   = argv[ctx.unitidx];
7deda5
		*execargv = 0;
7deda5
	}
7deda5
7deda5
	/* obtain ntux's own arguments */
1a5016
	if (!(meta = argv_get(
1a5016
			argv,optv,
1a5016
			ntux_argv_flags(flags),
ec0005
			fdctx->fderr)))
7deda5
		return -1;
7deda5
ec0005
	if (ntux_cctx_update(
ec0005
			program,optv,meta,flags,
ec0005
			fdctx,&cctx,&nunits)) {
7deda5
		argv_free(meta);
7deda5
		return -1;
7deda5
	}
7deda5
b824c9
	/* spawn, strace, bridge: exec argv */
b824c9
	if (ntux_cmd_exec[cctx.cmd]) {
7deda5
		*execargv  = execarg;
7deda5
		cctx.sargv = execargv;
7deda5
		cctx.senvp = envp;
7deda5
		nunits     = 0;
b824c9
b824c9
		ret = __xfi_framework_get_int32_slot_value(
b824c9
				PSX_RTDATA_UDAT32_ARGV0_IS_INTERP,
b824c9
				&val;;
b824c9
b824c9
		if (ret < 0)
b824c9
			return NTUX_ERROR;
b824c9
b824c9
		if (val)
b824c9
			cctx.sargv--;
26e14f
	}
26e14f
7deda5
	/* finalize */
c59ae6
	if (nunits && !cctx.cmd)
ec0005
		return ntux_driver_usage(
ec0005
			fdctx->fderr,
ec0005
			program,0,
ec0005
			optv,meta,
ec0005
			NTUX_CMD_DEFAULT);
c59ae6
ec0005
	if (!(ictx = ntux_driver_ctx_alloc(meta,fdctx,&cctx,nunits)))
26e14f
		return ntux_get_driver_ctx_fail(meta);
26e14f
b824c9
	ictx->interp      = interp;
7deda5
	ictx->ctx.program = program;
7deda5
	ictx->ctx.cctx    = &ictx->cctx;
7deda5
	*pctx             = &ictx->ctx;
26e14f
7deda5
	return 0;
26e14f
}
26e14f
26e14f
static void ntux_free_driver_ctx_impl(struct ntux_driver_ctx_alloc * ictx)
26e14f
{
b824c9
	if (ictx->ctx.interp)
b824c9
		ntux_free(ictx->ctx.interp);
b824c9
26e14f
	argv_free(ictx->meta);
26e14f
	free(ictx);
26e14f
}
26e14f
26e14f
void ntux_free_driver_ctx(struct ntux_driver_ctx * ctx)
26e14f
{
26e14f
	struct ntux_driver_ctx_alloc *	ictx;
26e14f
	uintptr_t			addr;
26e14f
26e14f
	if (ctx) {
26e14f
		addr = (uintptr_t)ctx - offsetof(struct ntux_driver_ctx_impl,ctx);
26e14f
		addr = addr - offsetof(struct ntux_driver_ctx_alloc,ctx);
26e14f
		ictx = (struct ntux_driver_ctx_alloc *)addr;
26e14f
		ntux_free_driver_ctx_impl(ictx);
26e14f
	}
26e14f
}
26e14f
26e14f
const struct ntux_source_version * ntux_source_version(void)
26e14f
{
26e14f
	return &ntux_src_version;
26e14f
}
ec0005
ec0005
int ntux_get_driver_fdctx(
ec0005
	const struct ntux_driver_ctx *	dctx,
ec0005
	struct ntux_fd_ctx *		fdctx)
ec0005
{
ec0005
	struct ntux_driver_ctx_impl *	ictx;
ec0005
ec0005
	ictx = ntux_get_driver_ictx(dctx);
ec0005
ec0005
	fdctx->fdin  = ictx->fdctx.fdin;
ec0005
	fdctx->fdout = ictx->fdctx.fdout;
ec0005
	fdctx->fderr = ictx->fdctx.fderr;
ec0005
	fdctx->fdlog = ictx->fdctx.fdlog;
ec0005
	fdctx->fdcwd = ictx->fdctx.fdcwd;
ec0005
	fdctx->fddst = ictx->fdctx.fddst;
ec0005
ec0005
	return 0;
ec0005
}
ec0005
ec0005
int ntux_set_driver_fdctx(
ec0005
	struct ntux_driver_ctx *	dctx,
ec0005
	const struct ntux_fd_ctx *	fdctx)
ec0005
{
ec0005
	struct ntux_driver_ctx_impl *	ictx;
ec0005
ec0005
	ictx = ntux_get_driver_ictx(dctx);
ec0005
ec0005
	ictx->fdctx.fdin  = fdctx->fdin;
ec0005
	ictx->fdctx.fdout = fdctx->fdout;
ec0005
	ictx->fdctx.fderr = fdctx->fderr;
ec0005
	ictx->fdctx.fdlog = fdctx->fdlog;
ec0005
	ictx->fdctx.fdcwd = fdctx->fdcwd;
ec0005
	ictx->fdctx.fddst = fdctx->fddst;
ec0005
ec0005
	return 0;
ec0005
}