87 lines
2.0 KiB
C++
87 lines
2.0 KiB
C++
#include "session_manager.h"
|
|
#include "msql_acces.h"
|
|
#include "config_package.h"
|
|
#include <unistd.h>
|
|
#include <sys/wait.h>
|
|
#include <iostream>
|
|
#include <cstring>
|
|
|
|
session_manager::session_manager(int fd)
|
|
{
|
|
this->fd=fd;
|
|
this->data=new msql_acces();
|
|
}
|
|
|
|
bool session_manager::validate_pass(){
|
|
char* buffer = new char[256];
|
|
this->read_data(buffer, 256);
|
|
std::string user=buffer;
|
|
this->read_data(buffer, 256);
|
|
std::string pass=buffer;
|
|
if(this->data->get_passwd(user)==pass){
|
|
this->write_data("pass");
|
|
return true;
|
|
}else{
|
|
this->write_data("fail");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void session_manager::start_dialog(){
|
|
char* buffer = new char[5];
|
|
while(true){
|
|
this->read_data(buffer,5);
|
|
if(strcmp(buffer, "exec")==0){
|
|
this->execute();
|
|
}else if(strcmp(buffer, "info")==0){
|
|
this->send_information();
|
|
}else if(strcmp(buffer,"exit")){
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
int session_manager::execute(){
|
|
char* n_package = new char[256];
|
|
this->read_data(n_package, 256);
|
|
config_package conf = config_package(n_package);
|
|
this->args=new char*[4];
|
|
this->args[0]="emerge";
|
|
this->args[1]="--ask";
|
|
this->args[2]=n_package;
|
|
this->args[3]=nullptr;
|
|
int pid = fork();
|
|
int status=-2;
|
|
if(pid==0){
|
|
if(execvp(this->args[0],this->args)==-1){
|
|
std::cout << "error inesperado" << std::endl;
|
|
}
|
|
exit(0);
|
|
}else {
|
|
waitpid(pid, &status, WCONTINUED);
|
|
if(status>0){
|
|
this->write_data("ok");
|
|
}else{
|
|
this->write_data("bad");
|
|
}
|
|
}
|
|
delete[] (n_package);
|
|
return status;
|
|
}
|
|
|
|
void session_manager::send_information(){
|
|
std::list<std::string> lis=this->data->get_pinfo();
|
|
for(std::string info : lis){
|
|
this->write_data(info);
|
|
}
|
|
this->write_data("end:of:the:info");
|
|
}
|
|
|
|
int session_manager::read_data(char* input, int size){
|
|
return read(this->fd, input, size);
|
|
}
|
|
|
|
int session_manager::write_data(std::string output){
|
|
return write(this->fd, output.data(), output.size());
|
|
}
|