added semaphore and shared memory to print without superposition between parent and child process

This commit is contained in:
hugogogo
2022-08-22 14:52:31 +02:00
parent 5225e3258b
commit f7b31c7db7
14 changed files with 127 additions and 65 deletions

View File

@@ -12,11 +12,11 @@ bool Webserv::_is_cgi(Client *client, std::string path)
size_t file_access = client->status;
size_t pos = 0;
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._is_cgi()\n";
print_secure(client->get_cl_fd(), "....._is_cgi()\n");
while (pos != NPOS)
{
pos = _cgi_pos(client, path, pos);
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_is_cgi()\n";
print_secure(client->get_cl_fd(), "....>_is_cgi()\n");
if (pos == NPOS)
break;
client->fill_script_path(path, pos);
@@ -44,7 +44,7 @@ size_t Webserv::_cgi_pos(Client *client, std::string &path, size_t pos)
size_t len;
std::locale loc; // for isalpha()
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._cgi_pos()\n";
print_secure(client->get_cl_fd(), "....._cgi_pos()\n");
v_ext = client->assigned_location->cgi_ext;
if (v_ext.empty())
return NPOS;
@@ -75,16 +75,16 @@ void Webserv::_cgi_open_pipes(Client *client)
#define W 1
int pipe_fd[2];
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._cgi_open_pipes()\n";
print_secure(client->get_cl_fd(), "....._cgi_open_pipes()\n");
if (::pipe(pipe_fd) == -1)
{
std::perror("err pipe");
client->status = 500;
}
client->cgi_pipe_r_from_parent = pipe_fd[R];
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") open pipe rfp [" << client->cgi_pipe_r_from_parent << "]\n";
print_secure(client->get_cl_fd(), "open pipe rfp [" + ::itos(client->cgi_pipe_r_from_parent) + "]\n");
client->cgi_pipe_w_to_child = pipe_fd[W];
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") open pipe wtc [" << client->cgi_pipe_w_to_child << "]\n";
print_secure(client->get_cl_fd(), "open pipe wtc [" + ::itos(client->cgi_pipe_w_to_child) + "]\n");
if (::pipe(pipe_fd) == -1)
{
@@ -96,9 +96,9 @@ void Webserv::_cgi_open_pipes(Client *client)
client->status = 500;
}
client->cgi_pipe_r_from_child = pipe_fd[R];
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") open pipe rfc [" << client->cgi_pipe_r_from_child << "]\n";
print_secure(client->get_cl_fd(), "open pipe rfc [" + ::itos(client->cgi_pipe_r_from_child) + "]\n");
client->cgi_pipe_w_to_parent = pipe_fd[W];
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") open pipe wtp [" << client->cgi_pipe_w_to_parent << "]\n";
print_secure(client->get_cl_fd(), "open pipe wtp [" + ::itos(client->cgi_pipe_w_to_parent) + "]\n");
// epoll add for writing body to child
_epoll_update(client->cgi_pipe_w_to_child, EPOLLOUT, EPOLL_CTL_ADD);
@@ -112,7 +112,7 @@ void Webserv::_write_body_to_cgi(Client *client)
ssize_t ret;
std::string body = client->get_rq_body();
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._write_body_to_cgi()\n";
print_secure(client->get_cl_fd(), "....._write_body_to_cgi()\n");
ret = ::write(client->cgi_pipe_w_to_child, body.c_str(), body.size());
if (ret == -1)
{
@@ -130,7 +130,7 @@ void Webserv::_exec_cgi(Client *client)
env_vector.reserve(18);
int i = 0;
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._exec_cgi()\n";
print_secure(client->get_cl_fd(), "....._exec_cgi()\n");
_set_env_vector(client, env_vector);
try {
_set_env_cstr(env_cstr, env_vector);
@@ -171,7 +171,7 @@ std::string Webserv::_dup_env(std::string var, int i)
*/
void Webserv::_set_env_vector(Client *client, std::vector<std::string> &env_vector)
{
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._set_env_vector()\n";
print_secure(client->get_cl_fd(), "....._set_env_vector()\n");
env_vector.push_back(_dup_env("AUTH_TYPE")); // authentification not supported
env_vector.push_back(_dup_env("CONTENT_LENGTH" , client->get_rq_body().size()));
env_vector.push_back(_dup_env("CONTENT_TYPE" , client->get_rq_headers("Content-Type")));
@@ -215,10 +215,10 @@ void Webserv::_exec_script(Client *client, char *env[])
char * const nll[1] = {NULL};
std::string path;
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._exec_script()\n";
print_secure(client->get_cl_fd(), "....._exec_script()\n");
// pid = 1; // DEBUG, if no fork, no problem
pid = fork();
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") fork()\n";
print_secure(client->get_cl_fd(), "fork()\n");
if (pid == -1)
{
std::perror("err fork()");
@@ -229,7 +229,7 @@ void Webserv::_exec_script(Client *client, char *env[])
{
std::signal(SIGPIPE, SIG_DFL);
std::signal(SIGINT, SIG_DFL);
family = CHILD;
g_family = CHILD;
if (dup2(client->cgi_pipe_r_from_parent, STDIN_FILENO) == -1)
{
@@ -247,7 +247,7 @@ void Webserv::_exec_script(Client *client, char *env[])
_close_all_clients_fd();
path = client->get_rq_script_path();
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") execve:[" << path << "]\n"; // DEBUG
print_secure(client->get_cl_fd(), "execve:[" + path + "]\n");
if (::execve(path.c_str(), nll, env) == -1) // replace path for debug error forcing
{
std::perror("err execve()");
@@ -260,19 +260,19 @@ void Webserv::_exec_script(Client *client, char *env[])
}
else //parent
{
usleep(1 * 1000);
//usleep(1 * 10000);
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe rfp [" << client->cgi_pipe_r_from_parent << "]\n";
print_secure(client->get_cl_fd(), "close pipe rfp [" + ::itos(client->cgi_pipe_r_from_parent) + "]\n");
if (::close(client->cgi_pipe_r_from_parent) == -1)
std::perror("err close");
client->cgi_pipe_r_from_parent = -1;
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe wtp [" << client->cgi_pipe_w_to_parent << "]\n";
print_secure(client->get_cl_fd(), "close pipe wtp [" + ::itos(client->cgi_pipe_w_to_parent) + "]\n");
if (::close(client->cgi_pipe_w_to_parent) == -1)
std::perror("err close");
client->cgi_pipe_w_to_parent = -1;
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe wtc [" << client->cgi_pipe_w_to_child << "]\n";
print_secure(client->get_cl_fd(), "close pipe wtc [" + ::itos(client->cgi_pipe_w_to_child) + "]\n");
if (::close(client->cgi_pipe_w_to_child) == -1)
std::perror("err close");
client->cgi_pipe_w_to_child = -1;
@@ -294,7 +294,7 @@ void Webserv::_check_script_output(Client *client, std::string & output)
size_t pos;
pos = client->cgi_output.find(CRLF CRLF);
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._check_script_output()\n";
print_secure(client->get_cl_fd(), "....._check_script_output()\n");
if (pos == 0 || pos == NPOS)
{
client->status = 500;;
@@ -314,7 +314,7 @@ void Webserv::_check_script_status(Client *client, std::string & output)
size_t pos;
int status_pos;
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._check_script_status()\n";
print_secure(client->get_cl_fd(), "....._check_script_status()\n");
pos = output.find("Status:");
if (pos != NPOS)
{
@@ -363,7 +363,7 @@ void Webserv::_check_fields_duplicates(Client *client, std::string & output)
std::string tmp;
size_t pos;
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._check_fields_duplicates()\n";
print_secure(client->get_cl_fd(), "....._check_fields_duplicates()\n");
// put server headers in map
tmp = client->response;
pos = tmp.find(CRLF CRLF);