From 0ed0082070edbcd8c1feb3969602f0f40b774cee Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Fri, 2 Mar 2012 15:00:11 +0100 Subject: [PATCH] Added execv(3) and execve(3). Removed the older libmaxsi system call. --- libmaxsi/include/libmaxsi/process.h | 11 +++++------ libmaxsi/include/unistd.h | 4 ++-- libmaxsi/process.cpp | 28 +++++++++++++++++----------- sortix/process.cpp | 4 +++- utils/help.cpp | 10 +++------- utils/init.cpp | 10 +++------- utils/ls.cpp | 4 ++-- utils/mxsh.cpp | 14 ++++---------- 8 files changed, 39 insertions(+), 46 deletions(-) diff --git a/libmaxsi/include/libmaxsi/process.h b/libmaxsi/include/libmaxsi/process.h index 8810611b..9653b65b 100644 --- a/libmaxsi/include/libmaxsi/process.h +++ b/libmaxsi/include/libmaxsi/process.h @@ -1,6 +1,6 @@ -/****************************************************************************** +/******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. + COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012. This file is part of LibMaxsi. @@ -11,8 +11,8 @@ LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for - more details. + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. You should have received a copy of the GNU Lesser General Public License along with LibMaxsi. If not, see . @@ -20,7 +20,7 @@ process.h Exposes system calls for process creation and management. -******************************************************************************/ +*******************************************************************************/ #ifndef LIBMAXSI_PROCESS_H #define LIBMAXSI_PROCESS_H @@ -29,7 +29,6 @@ namespace Maxsi { namespace Process { - int Execute(const char* filepath, int argc, const char** argv); void Abort(); void Exit(int code); pid_t Fork(); diff --git a/libmaxsi/include/unistd.h b/libmaxsi/include/unistd.h index a895e7f5..2efe3dd8 100644 --- a/libmaxsi/include/unistd.h +++ b/libmaxsi/include/unistd.h @@ -88,8 +88,6 @@ void encrypt(char [64], int); int execl(const char*, const char*, ...); int execle(const char*, const char*, ...); int execlp(const char*, const char*, ...); -int execv(const char*, char* const []); -int execve(const char*, char* const [], char* const []); int execvp(const char*, char* const []); int faccessat(int, const char*, int, int); int fchdir(int); @@ -154,6 +152,8 @@ int chdir(const char*); int close(int); int dup(int); void _exit(int); +int execv(const char*, char* const []); +int execve(const char*, char* const [], char* const []); pid_t fork(void); int ftruncate(int, off_t); char* getcwd(char*, size_t); diff --git a/libmaxsi/process.cpp b/libmaxsi/process.cpp index b69f563b..18912a9d 100644 --- a/libmaxsi/process.cpp +++ b/libmaxsi/process.cpp @@ -1,6 +1,6 @@ -/****************************************************************************** +/******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. + COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012. This file is part of LibMaxsi. @@ -11,8 +11,8 @@ LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for - more details. + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. You should have received a copy of the GNU Lesser General Public License along with LibMaxsi. If not, see . @@ -20,7 +20,7 @@ process.cpp Exposes system calls for process creation and management. -******************************************************************************/ +*******************************************************************************/ #include #include @@ -33,17 +33,12 @@ namespace Maxsi namespace Process { DEFN_SYSCALL1_VOID(SysExit, SYSCALL_EXIT, int); - DEFN_SYSCALL4(int, SysExecVE, SYSCALL_EXEC, const char*, int, char* const*, char* const*); + DEFN_SYSCALL3(int, SysExecVE, SYSCALL_EXEC, const char*, char* const*, char* const*); DEFN_SYSCALL0(pid_t, SysFork, SYSCALL_FORK); DEFN_SYSCALL0(pid_t, SysGetPID, SYSCALL_GETPID); DEFN_SYSCALL0(pid_t, SysGetParentPID, SYSCALL_GETPPID); DEFN_SYSCALL3(pid_t, SysWait, SYSCALL_WAIT, pid_t, int*, int); - int Execute(const char* filepath, int argc, const char** argv) - { - return SysExecVE(filepath, argc, (char* const*) argv, NULL); - } - void Abort() { // TODO: Send SIGABRT instead! @@ -57,6 +52,17 @@ namespace Maxsi SysExit(status); } + extern "C" int execve(const char* pathname, char* const* argv, + char* const* envp) + { + return SysExecVE(pathname, argv, envp); + } + + extern "C" int execv(const char* pathname, char* const* argv) + { + return execve(pathname, argv, NULL); + } + DUAL_FUNCTION(void, exit, Exit, (int status)) { dcloseall(); diff --git a/sortix/process.cpp b/sortix/process.cpp index 979b0070..2e2d08cc 100644 --- a/sortix/process.cpp +++ b/sortix/process.cpp @@ -388,7 +388,7 @@ namespace Sortix return (DevBuffer*) dev; } - int SysExecVE(const char* filename, int argc, char* const argv[], char* const /*envp*/[]) + int SysExecVE(const char* filename, char* const argv[], char* const /*envp*/[]) { // TODO: Validate that all the pointer-y parameters are SAFE! @@ -401,6 +401,8 @@ namespace Sortix state->filename = String::Clone(filename); if ( !state->filename ) { delete state; return -1; } + int argc; for ( argc = 0; argv[argc]; argc++ ); + state->argc = argc; state->argv = new char*[state->argc]; Maxsi::Memory::Set(state->argv, 0, sizeof(char*) * state->argc); diff --git a/utils/help.cpp b/utils/help.cpp index 5668541a..925f04c7 100644 --- a/utils/help.cpp +++ b/utils/help.cpp @@ -1,19 +1,15 @@ #include #include #include -#include -#include +#include int main(int argc, char* argv[]) { - // Reset the terminal's color and the rest of it. printf("Please enter the name of one of the following programs:\n"); const char* programname = "ls"; - const char* newargv[] = { programname, "/bin" }; - - Maxsi::Process::Execute(programname, 2, newargv); - + const char* newargv[] = { programname, "/bin", NULL }; + execv(programname, (char* const*) newargv); error(1, errno, "%s", programname); return 1; diff --git a/utils/init.cpp b/utils/init.cpp index 825597ae..c9e1b12f 100644 --- a/utils/init.cpp +++ b/utils/init.cpp @@ -5,10 +5,6 @@ #include #include #include -#include -#include - -using namespace Maxsi; int parent(pid_t childid) { @@ -20,10 +16,9 @@ int parent(pid_t childid) int child() { const char* programname = "sh"; - const char* newargv[] = { programname }; - - Process::Execute(programname, 1, newargv); + const char* newargv[] = { programname, NULL }; + execv(programname, (char* const*) newargv); error(0, errno, "%s", programname); return 2; @@ -44,3 +39,4 @@ int main(int argc, char* argv[]) return ( childpid == 0 ) ? child() : parent(childpid); } + diff --git a/utils/ls.cpp b/utils/ls.cpp index 19df676c..b33f251a 100644 --- a/utils/ls.cpp +++ b/utils/ls.cpp @@ -56,8 +56,8 @@ int main(int argc, char* argv[]) close(pipes[0]); close(pipes[1]); const char* columner = "column"; - const char* argv[] = { columner }; - Maxsi::Process::Execute(columner, 1, argv); + const char* argv[] = { columner, NULL }; + execv(columner, (char* const*) argv); error(127, errno, "%s", columner); } } diff --git a/utils/mxsh.cpp b/utils/mxsh.cpp index 68e82378..5d0abc74 100644 --- a/utils/mxsh.cpp +++ b/utils/mxsh.cpp @@ -7,10 +7,6 @@ #include #include #include -#include -#include - -using namespace Maxsi; int status = 0; @@ -52,8 +48,8 @@ void command() if ( command[0] == '\0' ) { return; } if ( strcmp(command, "$?") == 0 ) { printf("%u\n", status); status = 0; return; } - if ( strcmp(command, "$$") == 0 ) { printf("%u\n", Process::GetPID()); status = 0; return; } - if ( strcmp(command, "$PPID") == 0 ) { printf("%u\n", Process::GetParentPID()); status = 0; return; } + if ( strcmp(command, "$$") == 0 ) { printf("%u\n", getpid()); status = 0; return; } + if ( strcmp(command, "$PPID") == 0 ) { printf("%u\n", getppid()); status = 0; return; } int argc = 0; const char* argv[256]; @@ -121,10 +117,8 @@ void command() } } - // Replace the current process with another process image. - Process::Execute(argv[0], argc, argv); - - // This is clever. This only happens if the program didn't change. + argv[argc] = NULL; + execv(argv[0], (char* const*) argv); error(127, errno, "%s", argv[0]); }