From 7d53470dd0f735d4f94e34289fd4568340466f29 Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 19 May 2025 11:42:24 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20process=20fork=20should=20be=20nearly=20complete?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/requests/Cgi.hpp | 5 ++++- src/requests_handling/Cgi.cpp | 42 ++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/includes/requests/Cgi.hpp b/includes/requests/Cgi.hpp index d78490b..f3274ac 100644 --- a/includes/requests/Cgi.hpp +++ b/includes/requests/Cgi.hpp @@ -6,7 +6,7 @@ /* By: gadelbes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */ -/* Updated: 2025/05/16 12:26:18 by adjoly ### ########.fr */ +/* Updated: 2025/05/19 11:07:07 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,6 +49,9 @@ class Cgi : public server::AClientResource { std::map _envp; // The envp filled with _initEnvp config::Route *_conf; // The configuration for the route used http::ARequest *_request; // The requests that will be used for the cgi + + int stdin_pipe[2]; // The pipefd for the stdin of the cgi + int stdout_pipe[2]; // The pipefd for the stdout of the cgi }; }; // namespace webserv diff --git a/src/requests_handling/Cgi.cpp b/src/requests_handling/Cgi.cpp index ca48219..0904da2 100644 --- a/src/requests_handling/Cgi.cpp +++ b/src/requests_handling/Cgi.cpp @@ -6,17 +6,22 @@ /* By: gadelbes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */ -/* Updated: 2025/05/16 12:30:10 by adjoly ### ########.fr */ +/* Updated: 2025/05/19 11:41:28 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include #include +#include #include #include #include #include +#include #include +#include +#include using namespace webserv; @@ -90,13 +95,38 @@ char **Cgi::_genEnv(void) { return newEnv; } -void Cgi::process() { - int pipefd[2]; - int forkPid; +void Cgi::process(void) { + int pipefd[2]; + pid_t forkPid; - if (pipe(pipefd) <= -1) { - // TODO: error handling + if (pipe(pipefd) == -1) { + throw; + // TODO: error handling pipe fail } forkPid = fork(); + if (forkPid < 0) { + throw; + // TODO: fork fail + } else if (forkPid == 0) { + dup2(pipefd[1], STDOUT_FILENO); + close(pipefd[1]); + close(pipefd[0]); + + char *argv[] = {const_cast(_cgi_path.c_str()), + const_cast(_script_path.c_str()), NULL}; + char **env = _genEnv(); + + if (execve(_cgi_path.c_str(), argv, env) == -1) { + std::stringstream str; + str << "how did you do that ???? : "; + str << errno; + _log->error(str.str()); + for (int i = 0; env[i] != NULL; i++) + delete env[i]; + delete env; + exit(EXIT_FAILURE); + } + } + waitpid(forkPid, NULL, 0); }