stdme os revamped
This commit is contained in:
parent
f29e4ad7ef
commit
2c9a3ee834
11 changed files with 285 additions and 274 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/30 16:34:34 by rparodi ### ########.fr */
|
||||
/* Updated: 2024/08/01 06:38:36 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -23,8 +23,8 @@
|
|||
# define BASE_PATH "/no_base_path_defined/"
|
||||
#endif
|
||||
|
||||
// #if defined(PRINT_BACKTRACE) || defined(BACKTRACE_DEEP) || true
|
||||
#if true // TO_REMOVE
|
||||
#if defined(PRINT_BACKTRACE) || defined(BACKTRACE_DEEP)
|
||||
// #if true // TO_REMOVE
|
||||
# ifndef BACKTRACE_DEEP
|
||||
# define BACKTRACE_DEEP 256
|
||||
# endif
|
||||
|
|
|
|||
|
|
@ -6,19 +6,51 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/04 17:59:48 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/04 18:01:42 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/08/01 06:56:14 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/os/pipe.h"
|
||||
#include "me/fs/fs.h"
|
||||
#include "me/os/os.h"
|
||||
#include "me/str/str.h"
|
||||
|
||||
static t_error _pipe_get_fd_slot(struct s_file_slot **read,
|
||||
struct s_file_slot **write)
|
||||
{
|
||||
(*read) = get_unused_fd_slot();
|
||||
if ((*read) == NULL)
|
||||
return (ERROR);
|
||||
(*read)->ty = SLOT_FD;
|
||||
(*write) = get_unused_fd_slot();
|
||||
if ((*write) == NULL)
|
||||
return ((*read)->ty = SLOT_UNUSED, ERROR);
|
||||
(*write)->ty = SLOT_FD;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error create_pipe(t_pipe *out)
|
||||
{
|
||||
int fds[2];
|
||||
int fd[2];
|
||||
struct s_file_slot *read;
|
||||
struct s_file_slot *write;
|
||||
|
||||
if (pipe(fds))
|
||||
if (_pipe_get_fd_slot(&read, &write))
|
||||
return (ERROR);
|
||||
out->read = fds[0];
|
||||
out->write = fds[1];
|
||||
if (pipe(fd))
|
||||
{
|
||||
read->ty = SLOT_UNUSED;
|
||||
write->ty = SLOT_UNUSED;
|
||||
return (ERROR);
|
||||
}
|
||||
read->slot.fd.fd = fd[0];
|
||||
write->slot.fd.fd = fd[0];
|
||||
read->slot.fd.type = FD_PIPE;
|
||||
write->slot.fd.type = FD_PIPE;
|
||||
read->slot.fd.perms = FD_READ;
|
||||
write->slot.fd.perms = FD_WRITE;
|
||||
read->slot.fd.name = str_clone("<pipe[read]>");
|
||||
write->slot.fd.name = str_clone("<pipe[write]>");
|
||||
out->read = &read->slot.fd;
|
||||
out->write = &write->slot.fd;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,28 +6,25 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/30 16:08:20 by rparodi ### ########.fr */
|
||||
/* Updated: 2024/08/01 07:14:29 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/os/process.h"
|
||||
#include "me/string/string.h"
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/os/os.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/os/pipe.h"
|
||||
#include "me/str/str.h"
|
||||
#include "me/str/str.h"
|
||||
#include "me/str/str.h"
|
||||
#include "me/string/string.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_str.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
bool find_path(const t_str *s);
|
||||
bool find_null(const t_str *s);
|
||||
bool str_start_with(t_const_str s, t_const_str prefix);
|
||||
t_error handle_redirections(t_spawn_info *info, t_process *process);
|
||||
bool find_path(const t_str *s);
|
||||
bool find_null(const t_str *s);
|
||||
bool str_start_with(t_const_str s, t_const_str prefix);
|
||||
t_error handle_redirections(t_spawn_info *info, t_process *process);
|
||||
|
||||
t_error spawn_process_exec(t_spawn_info info, t_process *process)
|
||||
{
|
||||
|
|
@ -35,15 +32,15 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process)
|
|||
|
||||
if (info.forked_free)
|
||||
info.forked_free(info.forked_free_args);
|
||||
dup2(info.stdin.vals.fd.value, 0);
|
||||
dup2(info.stdout.vals.fd.value, 1);
|
||||
dup2(info.stderr.vals.fd.value, 2);
|
||||
close(process->stdin.vals.ro.fd);
|
||||
close(process->stdout.vals.ro.fd);
|
||||
close(process->stderr.vals.ro.fd);
|
||||
close(info.stdin.vals.fd.value);
|
||||
close(info.stdout.vals.fd.value);
|
||||
close(info.stderr.vals.fd.value);
|
||||
dup2(info.stdin.fd.fd->fd, 0);
|
||||
dup2(info.stdout.fd.fd->fd, 1);
|
||||
dup2(info.stderr.fd.fd->fd, 2);
|
||||
close_fd(process->stdin);
|
||||
close_fd(process->stdout);
|
||||
close_fd(process->stderr);
|
||||
close_fd(info.stdin.fd.fd);
|
||||
close_fd(info.stdout.fd.fd);
|
||||
close_fd(info.stderr.fd.fd);
|
||||
if (!vec_str_any(&info.arguments, find_null, &res) && res)
|
||||
vec_str_push(&info.arguments, NULL);
|
||||
res = false;
|
||||
|
|
@ -54,7 +51,7 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process)
|
|||
}
|
||||
|
||||
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path_raw,
|
||||
t_string *s)
|
||||
t_string *s)
|
||||
{
|
||||
t_vec_str path;
|
||||
t_usize idx;
|
||||
|
|
@ -82,8 +79,8 @@ t_error find_binary(t_spawn_info *info, t_process *process)
|
|||
if (info->binary_path == NULL)
|
||||
return (ERROR);
|
||||
s = string_new(256);
|
||||
if (str_start_with(info->binary_path, "/") || \
|
||||
str_find_chr(info->binary_path, '/') != NULL)
|
||||
if (str_start_with(info->binary_path, "/")
|
||||
|| str_find_chr(info->binary_path, '/') != NULL)
|
||||
string_push(&s, info->binary_path);
|
||||
else
|
||||
{
|
||||
|
|
@ -101,18 +98,17 @@ t_error find_binary(t_spawn_info *info, t_process *process)
|
|||
return (string_free(s), ERROR);
|
||||
}
|
||||
|
||||
static void cleanup(t_spawn_info info, t_process *process, \
|
||||
bool cleanup_process)
|
||||
static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process)
|
||||
{
|
||||
if (cleanup_process && process->stdin.tag != INVALID)
|
||||
close(process->stdin.vals.ro.fd);
|
||||
if (cleanup_process && process->stdout.tag != INVALID)
|
||||
close(process->stdout.vals.ro.fd);
|
||||
if (cleanup_process && process->stderr.tag != INVALID)
|
||||
close(process->stderr.vals.ro.fd);
|
||||
close(info.stdin.vals.fd.value);
|
||||
close(info.stdout.vals.fd.value);
|
||||
close(info.stderr.vals.fd.value);
|
||||
if (cleanup_process && process->stdin)
|
||||
close_fd(process->stdin);
|
||||
if (cleanup_process && process->stdout)
|
||||
close_fd(process->stdout);
|
||||
if (cleanup_process && process->stderr)
|
||||
close_fd(process->stderr);
|
||||
close_fd(info.stdin.fd.fd);
|
||||
close_fd(info.stdout.fd.fd);
|
||||
close_fd(info.stderr.fd.fd);
|
||||
vec_str_free(info.arguments);
|
||||
vec_str_free(info.environement);
|
||||
mem_free(info.binary_path);
|
||||
|
|
@ -131,7 +127,7 @@ t_error spawn_process(t_spawn_info info, t_process *process)
|
|||
{
|
||||
cleanup(info, process, false);
|
||||
if (process->pid == -1)
|
||||
return (printf("pid\n"), ERROR);
|
||||
return (ERROR);
|
||||
}
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/04 22:25:44 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/10 18:05:03 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/08/01 06:37:51 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -19,16 +19,6 @@ bool find_null(const t_str *s)
|
|||
return (s == NULL);
|
||||
}
|
||||
|
||||
bool str_start_with(t_const_str s, t_const_str prefix)
|
||||
{
|
||||
while (*prefix && *s)
|
||||
{
|
||||
if (*prefix++ != *s++)
|
||||
return (false);
|
||||
}
|
||||
return (*prefix == '\0');
|
||||
}
|
||||
|
||||
bool find_path(const t_str *s)
|
||||
{
|
||||
t_str ss;
|
||||
|
|
|
|||
|
|
@ -6,31 +6,31 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/04 22:27:00 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/04 23:01:03 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/08/01 07:14:10 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/fs/fs.h"
|
||||
#include "me/os/os.h"
|
||||
#include "me/types.h"
|
||||
#include "me/os/process.h"
|
||||
#include "me/os/pipe.h"
|
||||
|
||||
void handle_redirections_second(t_spawn_info *info, t_process *process)
|
||||
void handle_redirections_inherited(t_spawn_info *info, t_process *process)
|
||||
{
|
||||
(void)(process);
|
||||
if (info->stdin.tag == R_INHERITED)
|
||||
{
|
||||
info->stdin = fd(dup(0));
|
||||
process->stdin = ro(dup(0));
|
||||
info->stdin = fd(dup_fd(get_stdin()));
|
||||
process->stdin = dup_fd(get_stdin());
|
||||
}
|
||||
if (info->stdout.tag == R_INHERITED)
|
||||
{
|
||||
info->stdout = fd(dup(1));
|
||||
process->stdout = wo(dup(1));
|
||||
info->stdout = fd(dup_fd(get_stdout()));
|
||||
process->stdout = dup_fd(get_stdout());
|
||||
}
|
||||
if (info->stderr.tag == R_INHERITED)
|
||||
{
|
||||
info->stderr = fd(dup(2));
|
||||
process->stderr = wo(dup(2));
|
||||
info->stderr = fd(dup_fd(get_stderr()));
|
||||
process->stderr = dup_fd(get_stderr());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -38,28 +38,28 @@ void handle_redirections_fds(t_spawn_info *info, t_process *process)
|
|||
{
|
||||
if (info->stdin.tag == R_FD)
|
||||
{
|
||||
info->stdin = fd(dup(info->stdin.vals.fd.value));
|
||||
process->stdin = wo(dup(info->stdin.vals.fd.value));
|
||||
info->stdin = fd(dup_fd(info->stdin.fd.fd));
|
||||
process->stdin = dup_fd(info->stdin.fd.fd);
|
||||
}
|
||||
if (info->stdout.tag == R_FD)
|
||||
{
|
||||
info->stdout = fd(dup(info->stdout.vals.fd.value));
|
||||
process->stdout = ro(dup(info->stdout.vals.fd.value));
|
||||
info->stdout = fd(dup_fd(info->stdout.fd.fd));
|
||||
process->stdout = dup_fd(info->stdout.fd.fd);
|
||||
}
|
||||
if (info->stderr.tag == R_FD)
|
||||
{
|
||||
info->stderr = fd(dup(info->stderr.vals.fd.value));
|
||||
process->stderr = ro(dup(info->stderr.vals.fd.value));
|
||||
info->stderr = fd(dup_fd(info->stderr.fd.fd));
|
||||
process->stderr = dup_fd(info->stderr.fd.fd);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void redirection_inner(t_spawn_info *info, t_process *process)
|
||||
{
|
||||
process->stderr.tag = INVALID;
|
||||
process->stdout.tag = INVALID;
|
||||
process->stdin.tag = INVALID;
|
||||
process->stderr = NULL;
|
||||
process->stdout = NULL;
|
||||
process->stdin = NULL;
|
||||
handle_redirections_fds(info, process);
|
||||
handle_redirections_second(info, process);
|
||||
handle_redirections_inherited(info, process);
|
||||
}
|
||||
|
||||
t_error handle_redirections(t_spawn_info *info, t_process *process)
|
||||
|
|
@ -71,21 +71,21 @@ t_error handle_redirections(t_spawn_info *info, t_process *process)
|
|||
{
|
||||
if (create_pipe(&pipe_fd))
|
||||
return (ERROR);
|
||||
process->stdin = wo(pipe_fd.write);
|
||||
process->stdin = pipe_fd.write;
|
||||
info->stdin = fd(pipe_fd.read);
|
||||
}
|
||||
if (info->stdout.tag == R_PIPED)
|
||||
{
|
||||
if (create_pipe(&pipe_fd))
|
||||
return (ERROR);
|
||||
process->stdout = ro(pipe_fd.read);
|
||||
process->stdout = pipe_fd.read;
|
||||
info->stdout = fd(pipe_fd.write);
|
||||
}
|
||||
if (info->stderr.tag == R_PIPED)
|
||||
{
|
||||
if (create_pipe(&pipe_fd))
|
||||
return (ERROR);
|
||||
process->stderr = ro(pipe_fd.read);
|
||||
process->stderr = pipe_fd.read;
|
||||
info->stderr = fd(pipe_fd.write);
|
||||
}
|
||||
return (NO_ERROR);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue