mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-06-25 09:33:36 +02:00
「🏗️」 wip: process fork should be nearly complete
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/24 13:46:34 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<std::string, std::string> _envp; // The envp filled with _initEnvp
|
std::map<std::string, std::string> _envp; // The envp filled with _initEnvp
|
||||||
config::Route *_conf; // The configuration for the route used
|
config::Route *_conf; // The configuration for the route used
|
||||||
http::ARequest *_request; // The requests that will be used for the cgi
|
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
|
}; // namespace webserv
|
||||||
|
@ -6,17 +6,22 @@
|
|||||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/24 13:46:34 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 <cerrno>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <help.hpp>
|
#include <help.hpp>
|
||||||
#include <requests/Cgi.hpp>
|
#include <requests/Cgi.hpp>
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
using namespace webserv;
|
using namespace webserv;
|
||||||
|
|
||||||
@ -90,13 +95,38 @@ char **Cgi::_genEnv(void) {
|
|||||||
return newEnv;
|
return newEnv;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cgi::process() {
|
void Cgi::process(void) {
|
||||||
int pipefd[2];
|
int pipefd[2];
|
||||||
int forkPid;
|
pid_t forkPid;
|
||||||
|
|
||||||
if (pipe(pipefd) <= -1) {
|
if (pipe(pipefd) == -1) {
|
||||||
// TODO: error handling
|
throw;
|
||||||
|
// TODO: error handling pipe fail
|
||||||
}
|
}
|
||||||
|
|
||||||
forkPid = fork();
|
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<char *>(_cgi_path.c_str()),
|
||||||
|
const_cast<char *>(_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);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user