Added execv(3) and execve(3).

Removed the older libmaxsi system call.
This commit is contained in:
Jonas 'Sortie' Termansen 2012-03-02 15:00:11 +01:00
parent 93a9ee334d
commit 0ed0082070
8 changed files with 39 additions and 46 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
@ -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();

View File

@ -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);

View File

@ -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 <http://www.gnu.org/licenses/>.
@ -20,7 +20,7 @@
process.cpp
Exposes system calls for process creation and management.
******************************************************************************/
*******************************************************************************/
#include <libmaxsi/platform.h>
#include <libmaxsi/syscall.h>
@ -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();

View File

@ -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);

View File

@ -1,19 +1,15 @@
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <libmaxsi/platform.h>
#include <libmaxsi/process.h>
#include <unistd.h>
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;

View File

@ -5,10 +5,6 @@
#include <error.h>
#include <fcntl.h>
#include <unistd.h>
#include <libmaxsi/platform.h>
#include <libmaxsi/process.h>
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);
}

View File

@ -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);
}
}

View File

@ -7,10 +7,6 @@
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <libmaxsi/platform.h>
#include <libmaxsi/process.h>
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]);
}