Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
wims-info
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nicolas Thiery
wims-info
Commits
1a9fcf92
Commit
1a9fcf92
authored
9 years ago
by
Nicolas M. Thiéry
Browse files
Options
Downloads
Patches
Plain Diff
Debarasse de popen en gerant la redirection à la main
parent
fbe1a0b2
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test~coding~readingCppPrograms.fr/docker-compile-and-run.cpp
+66
-35
66 additions, 35 deletions
test~coding~readingCppPrograms.fr/docker-compile-and-run.cpp
with
66 additions
and
35 deletions
test~coding~readingCppPrograms.fr/docker-compile-and-run.cpp
+
66
−
35
View file @
1a9fcf92
...
...
@@ -33,18 +33,26 @@ int system(string command) {
std
::
system
(
command
.
c_str
());
}
int
exec
(
string
cmd
,
const
vector
<
string
>
args
,
const
string
in
,
const
string
out
,
const
string
err
)
{
cerr
<<
"Running (exec): "
+
cmd
;
void
report
(
string
comment
,
string
cmd
,
vector
<
string
>
args
)
{
cerr
<<
comment
;
cerr
<<
cmd
;
for
(
auto
arg
:
args
)
cerr
<<
" "
<<
arg
;
cerr
<<
endl
;
}
int
cpp_exec
(
string
cmd
,
const
vector
<
string
>
args
)
{
vector
<
const
char
*>
argv
;
argv
.
push_back
(
cmd
.
c_str
());
for
(
auto
&
s
:
args
)
argv
.
push_back
(
s
.
c_str
());
argv
.
push_back
((
char
*
)
0
);
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
int
status
;
...
...
@@ -60,26 +68,20 @@ int exec(string cmd, const vector<string> args, const string in, const string ou
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
;
if
(
out
!=
""
)
{
FILE
*
f
=
fopen
(
in
.
c_str
(),
"w"
);
dup2
(
fileno
(
f
),
STDOUT_FILENO
);
fclose
(
f
);
}
}
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
;
}
...
...
@@ -87,22 +89,49 @@ int exec(string cmd, const vector<string> args) {
return
exec
(
cmd
,
args
,
""
,
""
,
""
);
}
std
::
string
pexec
(
const
string
cmd
)
{
cerr
<<
"Running (pexec): "
+
cmd
<<
endl
;
FILE
*
pipe
=
popen
(
cmd
.
c_str
(),
"r"
);
if
(
!
pipe
)
return
"ERROR"
;
std
::
string
pexec
(
const
string
cmd
,
vector
<
string
>
args
)
{
report
(
"pexec: "
,
cmd
,
args
);
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
];
std
::
string
result
=
""
;
while
(
!
feof
(
pipe
))
{
if
(
fgets
(
buffer
,
128
,
pipe
)
!=
NULL
)
result
+=
buffer
;
int
n
;
while
((
n
=
read
(
pipefd
[
0
],
buffer
,
sizeof
(
buffer
)))
>
0
)
{
buffer
[
n
]
=
0
;
result
+=
buffer
;
}
pclose
(
pipe
);
wait
(
NULL
);
/* Wait for child */
return
result
;
}
string
docker_run
(
string
container
,
string
command
)
{
string
ID
=
pexec
(
docker
+
" run -d "
+
container
+
" "
+
command
);
string
docker_run
(
string
container
,
string
cmd
,
vector
<
string
>
args
)
{
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
ID
.
erase
(
ID
.
find_last_not_of
(
"
\n\r\t
"
)
+
1
);
return
ID
;
...
...
@@ -124,10 +153,12 @@ void docker_cp(string docker_id, string source, string target) {
}
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
)
{
//cout << pexec("/usr/bin/id", {}) << "|" << endl;
//printf("egid: %d\n", getegid());
//exec("/usr/bin/id", {});
...
...
@@ -140,8 +171,8 @@ int main(int argc, char **argv) {
string
program
=
argv
[
1
];
string
docker_id
=
docker_run
(
"crosbymichael/build-essential"
,
"sleep
1000"
);
//
cout << "docker_id: " << docker_id << endl;
string
docker_id
=
docker_run
(
"crosbymichael/build-essential"
,
"sleep
"
,
{
"
1000"
}
);
cout
<<
"docker_id: "
<<
docker_id
<<
endl
;
docker_cp
(
docker_id
,
bin_dir
+
compile_and_run
,
compile_and_run
);
docker_cp
(
docker_id
,
program
,
program
);
docker_exec
(
docker_id
,
{
"chmod"
,
"700"
,
compile_and_run
});
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment