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

Debarasse de popen en gerant la redirection à la main

parent fbe1a0b2
No related branches found
No related tags found
No related merge requests found
...@@ -33,18 +33,26 @@ int system(string command) { ...@@ -33,18 +33,26 @@ int system(string command) {
std::system(command.c_str()); std::system(command.c_str());
} }
int exec(string cmd, const vector<string> args, const string in, const string out, const string err) { void report(string comment, string cmd, vector<string> args) {
cerr << "Running (exec): "+cmd; cerr << comment;
cerr << cmd;
for (auto arg: args) for (auto arg: args)
cerr << " " << arg; cerr << " " << arg;
cerr << endl; cerr << endl;
}
int cpp_exec(string cmd, const vector<string> args) {
vector<const char*> argv; vector<const char*> argv;
argv.push_back(cmd.c_str()); argv.push_back(cmd.c_str());
for ( auto &s: args ) for ( auto &s: args )
argv.push_back(s.c_str()); argv.push_back(s.c_str());
argv.push_back((char *) 0); argv.push_back((char *) 0);
const vector<const char *> argv2(argv.begin(), argv.end()); const vector<const char *> argv2(argv.begin(), argv.end());
return execv(cmd.c_str(), (char * const*)argv.data());
}
int exec(string cmd, const vector<string> args, const string in, const string out, const string err) {
report("exec: ", cmd, args);
// Taken from man waitpid // Taken from man waitpid
int status; int status;
...@@ -60,26 +68,20 @@ int exec(string cmd, const vector<string> args, const string in, const string ou ...@@ -60,26 +68,20 @@ int exec(string cmd, const vector<string> args, const string in, const string ou
dup2(fileno(f), STDIN_FILENO); dup2(fileno(f), STDIN_FILENO);
fclose(f); fclose(f);
} }
execv(cmd.c_str(), (char * const*)argv.data()); if ( out != "" ) {
} FILE* f = fopen(in.c_str(), "w");
do { dup2(fileno(f), STDOUT_FILENO);
// TODO: this could be simplified for our purposes fclose(f);
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)); cpp_exec(cmd, args);
perror("exec");
exit(EXIT_FAILURE);
}
w = waitpid(cpid, &status, 0);
if (w == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
...@@ -87,22 +89,49 @@ int exec(string cmd, const vector<string> args) { ...@@ -87,22 +89,49 @@ int exec(string cmd, const vector<string> args) {
return exec(cmd, args, "", "", ""); return exec(cmd, args, "", "", "");
} }
std::string pexec(const string cmd) { std::string pexec(const string cmd, vector<string> args) {
cerr << "Running (pexec): "+cmd << endl; report("pexec: ", cmd, args);
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe) return "ERROR"; int pipefd[2];
pid_t cpid;
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { /* Child writes to pipe */
close(pipefd[0]); /* Close unused read end */
dup2(pipefd[1], STDOUT_FILENO);
cpp_exec(cmd, args);
perror("exec");
exit(EXIT_FAILURE);
}
close(pipefd[1]); /* Close unused write end */
char buffer[128]; char buffer[128];
std::string result = ""; std::string result = "";
while (!feof(pipe)) { int n;
if (fgets(buffer, 128, pipe) != NULL) while ((n = read(pipefd[0], buffer, sizeof(buffer))) > 0) {
result += buffer; buffer[n] = 0;
result += buffer;
} }
pclose(pipe); wait(NULL); /* Wait for child */
return result; return result;
} }
string docker_run(string container, string command) { string docker_run(string container, string cmd, vector<string> args) {
string ID = pexec(docker + " run -d "+container+" "+command); vector<string> docker_args = {"run", "-d", container, cmd};
for (auto arg: args)
docker_args.push_back(arg);
string ID = pexec(docker, docker_args);
// see http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring // see http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
ID.erase(ID.find_last_not_of(" \n\r\t")+1); ID.erase(ID.find_last_not_of(" \n\r\t")+1);
return ID; return ID;
...@@ -124,10 +153,12 @@ void docker_cp(string docker_id, string source, string target) { ...@@ -124,10 +153,12 @@ void docker_cp(string docker_id, string source, string target) {
} }
void docker_rm(string docker_id) { void docker_rm(string docker_id) {
exec(docker, {"rm", "-f", docker_id}); exec(docker, {"rm", "-f", docker_id}, "", "/dev/null", "");
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
//cout << pexec("/usr/bin/id", {}) << "|" << endl;
//printf("egid: %d\n", getegid()); //printf("egid: %d\n", getegid());
//exec("/usr/bin/id", {}); //exec("/usr/bin/id", {});
...@@ -140,8 +171,8 @@ int main(int argc, char **argv) { ...@@ -140,8 +171,8 @@ int main(int argc, char **argv) {
string program=argv[1]; string program=argv[1];
string docker_id = docker_run("crosbymichael/build-essential", "sleep 1000"); string docker_id = docker_run("crosbymichael/build-essential", "sleep", {"1000"});
// cout << "docker_id: " << docker_id << endl; cout << "docker_id: " << docker_id << endl;
docker_cp(docker_id, bin_dir+compile_and_run, compile_and_run); docker_cp(docker_id, bin_dir+compile_and_run, compile_and_run);
docker_cp(docker_id, program, program); docker_cp(docker_id, program, program);
docker_exec(docker_id, { "chmod", "700", compile_and_run }); docker_exec(docker_id, { "chmod", "700", compile_and_run });
......
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