mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Added execv(3) and execve(3).
Removed the older libmaxsi system call.
This commit is contained in:
parent
93a9ee334d
commit
0ed0082070
8 changed files with 39 additions and 46 deletions
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/*******************************************************************************
|
||||||
|
|
||||||
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
|
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012.
|
||||||
|
|
||||||
This file is part of LibMaxsi.
|
This file is part of LibMaxsi.
|
||||||
|
|
||||||
|
@ -11,8 +11,8 @@
|
||||||
|
|
||||||
LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY
|
LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
|
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
||||||
more details.
|
details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public License
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
|
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
@ -20,7 +20,7 @@
|
||||||
process.h
|
process.h
|
||||||
Exposes system calls for process creation and management.
|
Exposes system calls for process creation and management.
|
||||||
|
|
||||||
******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
#ifndef LIBMAXSI_PROCESS_H
|
#ifndef LIBMAXSI_PROCESS_H
|
||||||
#define LIBMAXSI_PROCESS_H
|
#define LIBMAXSI_PROCESS_H
|
||||||
|
@ -29,7 +29,6 @@ namespace Maxsi
|
||||||
{
|
{
|
||||||
namespace Process
|
namespace Process
|
||||||
{
|
{
|
||||||
int Execute(const char* filepath, int argc, const char** argv);
|
|
||||||
void Abort();
|
void Abort();
|
||||||
void Exit(int code);
|
void Exit(int code);
|
||||||
pid_t Fork();
|
pid_t Fork();
|
||||||
|
|
|
@ -88,8 +88,6 @@ void encrypt(char [64], int);
|
||||||
int execl(const char*, const char*, ...);
|
int execl(const char*, const char*, ...);
|
||||||
int execle(const char*, const char*, ...);
|
int execle(const char*, const char*, ...);
|
||||||
int execlp(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 execvp(const char*, char* const []);
|
||||||
int faccessat(int, const char*, int, int);
|
int faccessat(int, const char*, int, int);
|
||||||
int fchdir(int);
|
int fchdir(int);
|
||||||
|
@ -154,6 +152,8 @@ int chdir(const char*);
|
||||||
int close(int);
|
int close(int);
|
||||||
int dup(int);
|
int dup(int);
|
||||||
void _exit(int);
|
void _exit(int);
|
||||||
|
int execv(const char*, char* const []);
|
||||||
|
int execve(const char*, char* const [], char* const []);
|
||||||
pid_t fork(void);
|
pid_t fork(void);
|
||||||
int ftruncate(int, off_t);
|
int ftruncate(int, off_t);
|
||||||
char* getcwd(char*, size_t);
|
char* getcwd(char*, size_t);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/*******************************************************************************
|
||||||
|
|
||||||
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
|
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012.
|
||||||
|
|
||||||
This file is part of LibMaxsi.
|
This file is part of LibMaxsi.
|
||||||
|
|
||||||
|
@ -11,8 +11,8 @@
|
||||||
|
|
||||||
LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY
|
LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
|
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
||||||
more details.
|
details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public License
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
|
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
@ -20,7 +20,7 @@
|
||||||
process.cpp
|
process.cpp
|
||||||
Exposes system calls for process creation and management.
|
Exposes system calls for process creation and management.
|
||||||
|
|
||||||
******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
#include <libmaxsi/platform.h>
|
#include <libmaxsi/platform.h>
|
||||||
#include <libmaxsi/syscall.h>
|
#include <libmaxsi/syscall.h>
|
||||||
|
@ -33,17 +33,12 @@ namespace Maxsi
|
||||||
namespace Process
|
namespace Process
|
||||||
{
|
{
|
||||||
DEFN_SYSCALL1_VOID(SysExit, SYSCALL_EXIT, int);
|
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, SysFork, SYSCALL_FORK);
|
||||||
DEFN_SYSCALL0(pid_t, SysGetPID, SYSCALL_GETPID);
|
DEFN_SYSCALL0(pid_t, SysGetPID, SYSCALL_GETPID);
|
||||||
DEFN_SYSCALL0(pid_t, SysGetParentPID, SYSCALL_GETPPID);
|
DEFN_SYSCALL0(pid_t, SysGetParentPID, SYSCALL_GETPPID);
|
||||||
DEFN_SYSCALL3(pid_t, SysWait, SYSCALL_WAIT, pid_t, int*, int);
|
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()
|
void Abort()
|
||||||
{
|
{
|
||||||
// TODO: Send SIGABRT instead!
|
// TODO: Send SIGABRT instead!
|
||||||
|
@ -57,6 +52,17 @@ namespace Maxsi
|
||||||
SysExit(status);
|
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))
|
DUAL_FUNCTION(void, exit, Exit, (int status))
|
||||||
{
|
{
|
||||||
dcloseall();
|
dcloseall();
|
||||||
|
|
|
@ -388,7 +388,7 @@ namespace Sortix
|
||||||
return (DevBuffer*) dev;
|
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!
|
// TODO: Validate that all the pointer-y parameters are SAFE!
|
||||||
|
|
||||||
|
@ -401,6 +401,8 @@ namespace Sortix
|
||||||
state->filename = String::Clone(filename);
|
state->filename = String::Clone(filename);
|
||||||
if ( !state->filename ) { delete state; return -1; }
|
if ( !state->filename ) { delete state; return -1; }
|
||||||
|
|
||||||
|
int argc; for ( argc = 0; argv[argc]; argc++ );
|
||||||
|
|
||||||
state->argc = argc;
|
state->argc = argc;
|
||||||
state->argv = new char*[state->argc];
|
state->argv = new char*[state->argc];
|
||||||
Maxsi::Memory::Set(state->argv, 0, sizeof(char*) * state->argc);
|
Maxsi::Memory::Set(state->argv, 0, sizeof(char*) * state->argc);
|
||||||
|
|
|
@ -1,19 +1,15 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <error.h>
|
#include <error.h>
|
||||||
#include <libmaxsi/platform.h>
|
#include <unistd.h>
|
||||||
#include <libmaxsi/process.h>
|
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
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");
|
printf("Please enter the name of one of the following programs:\n");
|
||||||
|
|
||||||
const char* programname = "ls";
|
const char* programname = "ls";
|
||||||
const char* newargv[] = { programname, "/bin" };
|
const char* newargv[] = { programname, "/bin", NULL };
|
||||||
|
execv(programname, (char* const*) newargv);
|
||||||
Maxsi::Process::Execute(programname, 2, newargv);
|
|
||||||
|
|
||||||
error(1, errno, "%s", programname);
|
error(1, errno, "%s", programname);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
@ -5,10 +5,6 @@
|
||||||
#include <error.h>
|
#include <error.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <libmaxsi/platform.h>
|
|
||||||
#include <libmaxsi/process.h>
|
|
||||||
|
|
||||||
using namespace Maxsi;
|
|
||||||
|
|
||||||
int parent(pid_t childid)
|
int parent(pid_t childid)
|
||||||
{
|
{
|
||||||
|
@ -20,10 +16,9 @@ int parent(pid_t childid)
|
||||||
int child()
|
int child()
|
||||||
{
|
{
|
||||||
const char* programname = "sh";
|
const char* programname = "sh";
|
||||||
const char* newargv[] = { programname };
|
const char* newargv[] = { programname, NULL };
|
||||||
|
|
||||||
Process::Execute(programname, 1, newargv);
|
|
||||||
|
|
||||||
|
execv(programname, (char* const*) newargv);
|
||||||
error(0, errno, "%s", programname);
|
error(0, errno, "%s", programname);
|
||||||
|
|
||||||
return 2;
|
return 2;
|
||||||
|
@ -44,3 +39,4 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
return ( childpid == 0 ) ? child() : parent(childpid);
|
return ( childpid == 0 ) ? child() : parent(childpid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,8 +56,8 @@ int main(int argc, char* argv[])
|
||||||
close(pipes[0]);
|
close(pipes[0]);
|
||||||
close(pipes[1]);
|
close(pipes[1]);
|
||||||
const char* columner = "column";
|
const char* columner = "column";
|
||||||
const char* argv[] = { columner };
|
const char* argv[] = { columner, NULL };
|
||||||
Maxsi::Process::Execute(columner, 1, argv);
|
execv(columner, (char* const*) argv);
|
||||||
error(127, errno, "%s", columner);
|
error(127, errno, "%s", columner);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,10 +7,6 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <error.h>
|
#include <error.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <libmaxsi/platform.h>
|
|
||||||
#include <libmaxsi/process.h>
|
|
||||||
|
|
||||||
using namespace Maxsi;
|
|
||||||
|
|
||||||
int status = 0;
|
int status = 0;
|
||||||
|
|
||||||
|
@ -52,8 +48,8 @@ void command()
|
||||||
if ( command[0] == '\0' ) { return; }
|
if ( command[0] == '\0' ) { return; }
|
||||||
|
|
||||||
if ( strcmp(command, "$?") == 0 ) { printf("%u\n", status); status = 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, "$$") == 0 ) { printf("%u\n", getpid()); status = 0; return; }
|
||||||
if ( strcmp(command, "$PPID") == 0 ) { printf("%u\n", Process::GetParentPID()); status = 0; return; }
|
if ( strcmp(command, "$PPID") == 0 ) { printf("%u\n", getppid()); status = 0; return; }
|
||||||
|
|
||||||
int argc = 0;
|
int argc = 0;
|
||||||
const char* argv[256];
|
const char* argv[256];
|
||||||
|
@ -121,10 +117,8 @@ void command()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace the current process with another process image.
|
argv[argc] = NULL;
|
||||||
Process::Execute(argv[0], argc, argv);
|
execv(argv[0], (char* const*) argv);
|
||||||
|
|
||||||
// This is clever. This only happens if the program didn't change.
|
|
||||||
error(127, errno, "%s", argv[0]);
|
error(127, errno, "%s", argv[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue