From 5ec9b67e061e7bf839d6ae986354190d2e1e0037 Mon Sep 17 00:00:00 2001 From: midipix Date: May 23 2018 03:49:18 +0000 Subject: ntux_cmd_spawn(): initial implementation and integration. --- diff --git a/include/ntux/ntux.h b/include/ntux/ntux.h index d56e795..e5f4ac6 100644 --- a/include/ntux/ntux.h +++ b/include/ntux/ntux.h @@ -109,6 +109,7 @@ ntux_api void ntux_free_driver_ctx (struct ntux_driver_ctx *); /* cmd api */ ntux_api int ntux_cmd_stat (const struct ntux_driver_ctx *, const char *); +ntux_api int ntux_cmd_spawn (const struct ntux_driver_ctx *); /* utility api */ ntux_api int ntux_main (int, char **, char **); diff --git a/project/common.mk b/project/common.mk index bfc901d..c3dde46 100644 --- a/project/common.mk +++ b/project/common.mk @@ -14,6 +14,7 @@ INTERNAL_SRCS = \ src/internal/ntux_strerr_impl.c \ CMD_SRCS = \ + src/cmds/ntux_cmd_spawn.c \ src/cmds/ntux_cmd_stat.c \ OUTPUT_SRCS = \ diff --git a/src/cmds/ntux_cmd_spawn.c b/src/cmds/ntux_cmd_spawn.c new file mode 100644 index 0000000..cb88696 --- /dev/null +++ b/src/cmds/ntux_cmd_spawn.c @@ -0,0 +1,54 @@ +/***********************************************************/ +/* ntux: native translation und extension */ +/* Copyright (C) 2016--2018 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.NTUX. */ +/***********************************************************/ + +#include +#include +#include + +#include +#include "ntux_driver_impl.h" +#include "ntux_nolibc_impl.h" +#include "ntux_errinfo_impl.h" + +int ntux_cmd_spawn(const struct ntux_driver_ctx * dctx) +{ + int32_t status; + pid_t pid; + const char ** argv; + const char ** envp; + unsigned char * program; + + /* init */ + ntux_driver_set_ectx( + dctx,0, + dctx->cctx->sargv[0]); + + argv = (const char **)dctx->cctx->sargv; + envp = (const char **)dctx->cctx->senvp; + program = (unsigned char *)dctx->cctx->sargv[0]; + + /* spawn */ + pid = __sys_vfork(); + + /* failed? */ + if (pid < 0) + if (ntux_errno_set(dctx,pid)) + return NTUX_SYSTEM_ERROR(dctx); + + /* child */ + if (pid == 0) + if ((status = __sys_execve(program,argv,envp))) + if (ntux_errno_set(dctx,status)) + if (NTUX_SYSTEM_ERROR(dctx)) + __sys_exit(0); + + /* parent */ + __sys_wait4( + pid,&status, + 0,0); + + return 0; +} diff --git a/src/driver/ntux_amain.c b/src/driver/ntux_amain.c index 5a9dfc9..9a1d9e1 100644 --- a/src/driver/ntux_amain.c +++ b/src/driver/ntux_amain.c @@ -77,6 +77,9 @@ int ntux_main(int argc, char ** argv, char ** envp) if ((ntux_version(dctx)) < 0) return ntux_exit(dctx,NTUX_ERROR); + if (dctx->cctx->cmd == NTUX_CMD_SPAWN) + ntux_cmd_spawn(dctx); + for (unit=dctx->units; *unit; unit++) ntux_perform_unit_actions(dctx,*unit);