Skip to content
Snippets Groups Projects
Commit ac63271d authored by Nicolas M. Thiéry's avatar Nicolas M. Thiéry
Browse files

Debarasse de docker_old_exec en gerant la redirection à la main

parent fb061bee
No related branches found
No related tags found
No related merge requests found
......@@ -4,9 +4,10 @@
#include <sstream>
using namespace std;
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
/**
* TODO:
......@@ -32,9 +33,7 @@ int system(string command) {
std::system(command.c_str());
}
#include <stdio.h>
void exec(string cmd, const vector<string> args) {
int exec(string cmd, const vector<string> args, const string in, const string out, const string err) {
cerr << "Running (exec): "+cmd;
for (auto arg: args)
cerr << " " << arg;
......@@ -47,10 +46,45 @@ void exec(string cmd, const vector<string> args) {
const vector<const char *> argv2(argv.begin(), argv.end());
int pid = fork();
if (pid==0) {
// Taken from man waitpid
int status;
pid_t w;
pid_t cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { /* Code executed by child */
if ( in != "" ) {
FILE* f = fopen(in.c_str(), "r");
dup2(fileno(f), STDIN_FILENO);
fclose(f);
}
execv(cmd.c_str(), (char * const*)argv.data());
}
do {
// TODO: this could be simplified for our purposes
w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
if (w == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
cerr << "exited, status=" << WEXITSTATUS(status) << endl;
} else if (WIFSIGNALED(status)) {
cerr << "killed by signal=" << WTERMSIG(status) << endl;
} else if (WIFSTOPPED(status)) {
cerr << "stopped by signal " << WSTOPSIG(status) << endl;
} else if (WIFCONTINUED(status)) {
cerr << "continued" << endl;
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
return EXIT_SUCCESS;
}
int exec(string cmd, const vector<string> args) {
return exec(cmd, args, "", "", "");
}
std::string pexec(const string cmd) {
......@@ -74,12 +108,6 @@ string docker_run(string container, string command) {
return ID;
}
void docker_oldexec(string docker_id, string command) {
string cmd = docker+" exec -i "+ docker_id+ " "+ command;
cerr << "Running (docker_oldexec): "+cmd << endl;
system(cmd);
}
void docker_exec(string docker_id, vector<string> args) {
vector<string> docker_args = {"exec", "-i", docker_id};
for (auto arg: args)
......@@ -91,7 +119,8 @@ void docker_exec(string docker_id, vector<string> args) {
void docker_cp(string docker_id, string source, string target) {
// See http://stackoverflow.com/questions/22907231/copying-files-from-host-to-docker-container
// Better replace with 'docker cp' of docker 1.8
docker_oldexec(docker_id, " /bin/bash -c 'cat > "+target+"' < "+source);
exec(docker, {"exec", "-i", docker_id, "/bin/bash", "-c", "cat > "+target,},
source, "", "");
}
void docker_rm(string docker_id) {
......@@ -117,7 +146,6 @@ int main(int argc, char **argv) {
docker_cp(docker_id, program, program);
docker_exec(docker_id, { "chmod", "700", compile_and_run });
docker_exec(docker_id, { "./"+compile_and_run, program});
//docker_oldexec(docker_id, "./"+compile_and_run+" "+program);
docker_rm(docker_id);
cout.flush();
return 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment