stdme os revamped

This commit is contained in:
Maieul BOYER 2024-08-01 07:26:32 +02:00
parent f29e4ad7ef
commit 2c9a3ee834
No known key found for this signature in database
11 changed files with 285 additions and 274 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 15:12:18 by maiboyer #+# #+# */
/* Updated: 2024/07/11 18:56:59 by maiboyer ### ########.fr */
/* Updated: 2024/08/01 07:25:10 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -254,6 +254,9 @@ void put_string_fd(t_fd *fd, t_const_str string);
/// @note will fail silently if the fd is not open in write mode
void put_char_fd(t_fd *fd, t_u8 c);
/// @brief Duplicate a t_fd using the dup syscall
/// @note will return NULL in case of error (either fd is null or dup failed)
t_fd *dup_fd(t_fd *fd);
/* _____ _____ _____ ______ _____ _______ ____ _______ __
| __ \_ _| __ \| ____/ ____|__ __/ __ \| __ \ \ / /
| | | || | | |__) | |__ | | | | | | | | |__) \ \_/ /

137
stdme/include/me/os/os.h Normal file
View file

@ -0,0 +1,137 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* os.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 15:43:08 by maiboyer #+# #+# */
/* Updated: 2024/08/01 06:32:58 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef OS_H
# define OS_H
# include "me/fs/fs.h"
# include "me/types.h"
# include "me/vec/vec_str.h"
# include "me/vec/vec_u8.h"
/// @brief A process ID
typedef int t_pid;
/// @brief A process exit_code
typedef int t_exit_code;
/// @brief Redirection for spawning a process
typedef union u_redirection t_redirection;
/// @brief Pipe structure
typedef struct s_pipe t_pipe;
/// @brief Spawn information
typedef struct s_spawn_info t_spawn_info;
/// @brief Process information
typedef struct s_process t_process;
/// @struct Process output
/// @brief Process output information
typedef struct s_process_output t_process_output;
enum e_redirection
{
R_INHERITED = 0,
R_PIPED = 1,
R_FD = 2,
};
union u_redirection
{
enum e_redirection tag;
struct s_fd_redirection
{
enum e_redirection tag;
t_fd *fd;
} fd;
struct s_piped_redirection
{
enum e_redirection tag;
} piped;
struct s_inherited_redirection
{
enum e_redirection tag;
} inherited;
};
struct s_spawn_info
{
t_redirection stdin;
t_redirection stdout;
t_redirection stderr;
t_vec_str arguments;
t_vec_str environement;
t_str binary_path;
void (*forked_free)(void *);
void *forked_free_args;
};
struct s_process
{
t_fd *stdin;
t_fd *stdout;
t_fd *stderr;
t_pid pid;
};
struct s_process_output
{
t_pid pid;
t_vec_u8 stdout;
t_vec_u8 stderr;
t_exit_code exit_code;
};
struct s_pipe
{
t_fd *read;
t_fd *write;
};
/// @brief Spawn a new process with the given information
/// @param info the information to spawn the process
/// @param process data returned by the function
/// @return true if an error occured, false otherwise
t_error spawn_process(t_spawn_info info,
t_process *process);
/// @brief Create a pipe
/// @param[out] out the created pipe
/// @return the error
t_error create_pipe(t_pipe *out);
/// @brief Create a piped redirection
/// @param void
/// @return the redirection
static inline t_redirection piped(void)
{
return ((t_redirection){.tag = R_PIPED});
}
/// @brief Create an inherited redirection (inherit from the parent process)
/// @param void
/// @return the redirection
static inline t_redirection inherited(void)
{
return ((t_redirection){.tag = R_INHERITED});
}
/// @brief Create a file descriptor redirection
/// @param fd file descriptor to redirect
/// @return the redirection
static inline t_redirection fd(t_fd *fd)
{
return ((t_redirection){.fd = {.tag = R_FD, fd}});
}
#endif /* PROCESS_H */

View file

@ -1,30 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipe.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 17:57:29 by maiboyer #+# #+# */
/* Updated: 2024/01/04 17:59:30 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PIPE_H
# define PIPE_H
# include "me/types.h"
/// @brief Pipe structure
typedef struct s_pipe
{
int read;
int write;
} t_pipe;
/// @brief Create a pipe
/// @param[out] out the created pipe
/// @return the error
t_error create_pipe(t_pipe *out);
#endif /* PIPE_H */

View file

@ -1,173 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 15:43:08 by maiboyer #+# #+# */
/* Updated: 2024/07/11 18:59:04 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PROCESS_H
# define PROCESS_H
# include "me/types.h"
# include "me/vec/vec_str.h"
# include "me/vec/vec_u8.h"
typedef t_i32 t_pid;
typedef t_i32 t_exit_code;
enum e_redirection
{
R_INHERITED = 0,
R_PIPED = 1,
R_FD = 2,
};
union u_redirection
{
struct s_fd_redirection
{
int value;
} fd;
struct s_piped_redirection
{
} piped;
struct s_inherited_redirection
{
} inherited;
};
/// @brief Redirection for spawning a process
typedef struct s_redirection
{
enum e_redirection tag;
union u_redirection vals;
} t_redirection;
/// @brief Create a piped redirection
/// @param void
/// @return the redirection
static inline t_redirection piped(void)
{
return ((t_redirection){
.tag = R_PIPED,
});
}
/// @brief Create an inherited redirection (inherit from the parent process)
/// @param void
/// @return the redirection
static inline t_redirection inherited(void)
{
return ((t_redirection){
.tag = R_INHERITED,
});
}
/// @brief Create a file descriptor redirection
/// @param fd file descriptor to redirect
/// @return the redirection
static inline t_redirection fd(int fd)
{
return ((t_redirection){.tag = R_FD, \
.vals = (union u_redirection){\
.fd = {.value = fd}, \
}});
}
/// @brief Wrapped file descriptor tag
enum e_wrapped_fd_tag
{
READ_ONLY,
WRITE_ONLY,
READ_WRITE,
INVALID,
};
/// @brief Wrapped file descriptor
union u_wrapped_fd
{
struct s_read_only
{
int fd;
} ro;
struct s_write_only
{
int fd;
} wo;
struct s_read_write
{
int fd;
} rw;
};
/// @brief Wrapped file descriptor
typedef struct s_wrapped_fd
{
enum e_wrapped_fd_tag tag;
union u_wrapped_fd vals;
} t_wrapped_fd;
/// @brief Create a Read only wrapped file descriptor
/// @param fd file descriptor to wrap
/// @return the wrapped file descriptor
static inline t_wrapped_fd ro(int fd)
{
return ((t_wrapped_fd){.tag = READ_ONLY, \
.vals = (union u_wrapped_fd){\
.ro = {.fd = fd}, \
}});
}
/// @brief Create a Write only wrapped file descriptor
/// @param fd file descriptor to wrap
/// @return the wrapped file descriptor
static inline t_wrapped_fd wo(int fd)
{
return ((t_wrapped_fd){.tag = WRITE_ONLY,
.vals = (union u_wrapped_fd){.wo = {.fd = fd}}});
}
/// @brief Spawn information
typedef struct s_spawn_info
{
t_redirection stdin;
t_redirection stdout;
t_redirection stderr;
t_vec_str arguments;
t_vec_str environement;
t_str binary_path;
void (*forked_free)(void *);
void *forked_free_args;
} t_spawn_info;
/// @brief Process information
typedef struct s_process
{
t_wrapped_fd stdin;
t_wrapped_fd stdout;
t_wrapped_fd stderr;
t_pid pid;
} t_process;
/// @struct Process output
/// @brief Process output information
typedef struct s_process_output
{
t_pid pid;
t_vec_u8 stdout;
t_vec_u8 stderr;
t_exit_code exit_code;
} t_process_output;
/// @brief Spawn a new process with the given information
/// @param info the information to spawn the process
/// @param process data returned by the function
/// @return true if an error occured, false otherwise
t_error spawn_process(t_spawn_info info,
t_process *process);
#endif /* PROCESS_H */