-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.hpp
66 lines (46 loc) · 1.2 KB
/
docker.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <sys/wait.h>
#include <sys/mount.h>
#include <fcntl.h>
#include <unistd.h>
#include <sched.h>
#include <cstring>
#include <string>
#define STACK_SIZE (512*512)
namespace docker{
typedef int proc_status;
proc_status proc_err = -1;
proc_status proc_exit = 0;
proc_status proc_wait = 1;
typedef struct docker_config{
std::string host_name;
std::string root_dir;
} docker_config;
class container{
public:
container(docker_config cfg){
this->cfg = cfg;
}
void start();
private:
typedef int proc_id;
docker_config cfg;
char child_stack[STACK_SIZE];
void start_bash() {
std::string bash = "/bin/bash";
char * c_bash = new char[bash.length()+1];
strcpy(c_bash, bash.c_str());
char* const child_args[] = { c_bash, NULL };
execv(child_args[0], child_args);
delete []c_bash;
}
};
void docker::container::start(){
auto setup = [](void *args) -> int {
auto _this = reinterpret_cast<container *>(args);
_this->start_bash();
return proc_wait;
};
proc_id child_pid = clone(setup, child_stack+STACK_SIZE, SIGCHLD, this);
waitpid(child_pid, nullptr, 0);
}
} //end of namespace docker