forked from project-stacker/stacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal.go
120 lines (101 loc) · 2.61 KB
/
internal.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* This file is a little bit strange. The problem is that we want to do
* daemonized containers with liblxc, but we can't spawn containers in threaded
* environments (i.e. golang), with go-lxc. So instead, we embed some C into
* our program that catches execution before golang starts. This way, we can do
* a tiny C program to actually spawn the container.
*
* We do this in the "stacker" package so that if anyone uses the library, the
* re-exec will actually work. Of course, this is slightly impolite, but what
* can you do.
*/
package stacker
// #cgo LDFLAGS: -llxc
/*
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <lxc/lxccontainer.h>
static int spawn_container(char *name, char *lxcpath, char *config)
{
struct lxc_container *c;
c = lxc_container_new(name, lxcpath);
if (!c) {
fprintf(stderr, "failed to create container %s\n", name);
return -1;
}
c->clear_config(c);
if (!c->load_config(c, config)) {
fprintf(stderr, "failed to load container config at %s\n", config);
return -1;
}
c->daemonize = false;
if (!c->start(c, 1, NULL)) {
fprintf(stderr, "failed to start container %s\n", name);
return -1;
}
return c->error_num;
}
// main function for the "internal" command. Right now, arguments look like:
// stacker internal <container_name> <lxcpath> <config_path>
__attribute__((constructor)) void internal(void)
{
int ret, status;
char buf[4096];
ssize_t size;
char *cur, *name, *lxcpath, *config_path;
ret = open("/proc/self/cmdline", O_RDONLY);
if (ret < 0) {
perror("error: open");
exit(96);
}
if ((size = read(ret, buf, sizeof(buf)-1)) < 0) {
close(ret);
perror("error: read");
exit(96);
}
close(ret);
// /proc/self/cmdline is null separated, but let's be real safe
buf[size] = 0;
cur = buf;
#define ADVANCE_ARG \
do { \
while (*cur) { \
cur++; \
} \
cur++; \
} while (0)
// skip argv[0]
ADVANCE_ARG;
// is this really the internal command, if not, continue normal execution
if (strcmp(cur, "internal"))
return;
ADVANCE_ARG;
name = cur;
ADVANCE_ARG;
lxcpath = cur;
ADVANCE_ARG;
config_path = cur;
ret = isatty(STDIN_FILENO);
if (ret < 0) {
perror("isatty");
exit(96);
}
// If this is non interactive, get rid of our controlling terminal,
// since we don't want lxc's setting of ISIG to ignore user's ^Cs.
if (!ret)
setsid();
status = spawn_container(name, lxcpath, config_path);
// Try and propagate the container's exit code.
if (WIFEXITED(status)) {
exit(WEXITSTATUS(status));
} else {
kill(0, WTERMSIG(status));
exit(EXIT_FAILURE);
}
}
*/
import "C"