Blame src/cmds/ntux_cmd_spawn.c

5ec9b6
/***********************************************************/
5ec9b6
/*  ntux: native translation und extension                 */
47596e
/*  Copyright (C) 2016--2021  Z. Gilboa                    */
5ec9b6
/*  Released under GPLv2 and GPLv3; see COPYING.NTUX.      */
5ec9b6
/***********************************************************/
5ec9b6
5ec9b6
#include <psxabi/sys_sysapi.h>
5ec9b6
#include <psxabi/sys_errno.h>
5ec9b6
5ec9b6
#include <ntux/ntux.h>
5ec9b6
#include "ntux_driver_impl.h"
5ec9b6
#include "ntux_nolibc_impl.h"
5ec9b6
#include "ntux_errinfo_impl.h"
5ec9b6
5ec9b6
int ntux_cmd_spawn(const struct ntux_driver_ctx * dctx)
5ec9b6
{
5ec9b6
	int32_t		status;
5ec9b6
	pid_t		pid;
5ec9b6
	const char **	argv;
5ec9b6
	const char **	envp;
5ec9b6
	unsigned char * program;
5ec9b6
5ec9b6
	/* init */
5ec9b6
	ntux_driver_set_ectx(
5ec9b6
		dctx,0,
5ec9b6
		dctx->cctx->sargv[0]);
5ec9b6
5ec9b6
	argv    = (const char **)dctx->cctx->sargv;
5ec9b6
	envp    = (const char **)dctx->cctx->senvp;
5ec9b6
	program = (unsigned char *)dctx->cctx->sargv[0];
5ec9b6
5ec9b6
	/* spawn */
5ec9b6
	pid = __sys_vfork();
5ec9b6
5ec9b6
	/* failed? */
5ec9b6
	if (pid < 0)
5ec9b6
		if (ntux_errno_set(dctx,pid))
5ec9b6
			return NTUX_SYSTEM_ERROR(dctx);
5ec9b6
5ec9b6
	/* child */
5ec9b6
	if (pid == 0)
5ec9b6
		if ((status = __sys_execve(program,argv,envp)))
5ec9b6
			if (ntux_errno_set(dctx,status))
5ec9b6
				if (NTUX_SYSTEM_ERROR(dctx))
5ec9b6
					__sys_exit(0);
5ec9b6
5ec9b6
	/* parent */
74fb0c
	struct ntux_driver_ctx_impl * ictx = ntux_get_driver_ictx(dctx);
74fb0c
5ec9b6
	__sys_wait4(
74fb0c
		pid,
74fb0c
		&ictx->cctx.status,
5ec9b6
		0,0);
5ec9b6
5ec9b6
	return 0;
5ec9b6
}