Merge pull request #15 from runcom/fix2

Various fixes
This commit is contained in:
Mrunal Patel 2016-09-17 08:02:45 -07:00 committed by GitHub
commit 6007176ec9
5 changed files with 88 additions and 41 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
ocid ocid
ocic ocic
conmon/conmon
conmon/conmon.o

View file

@ -1,6 +1,9 @@
.PHONY: all clean ocid ocic .PHONY: all clean conmon ocid ocic
all: ocid ocic all: conmon ocid ocic
conmon:
make -C $@
ocid: ocid:
go build -o ocid ./cmd/server/main.go go build -o ocid ./cmd/server/main.go

View file

@ -33,9 +33,13 @@
#define _cleanup_(x) __attribute__((cleanup(x))) #define _cleanup_(x) __attribute__((cleanup(x)))
static inline void freep(void *p) { free(*(void **)p); } static inline void freep(void *p)
{
free(*(void **)p);
}
static inline void closep(int *fd) { static inline void closep(int *fd)
{
if (*fd >= 0) if (*fd >= 0)
close(*fd); close(*fd);
*fd = -1; *fd = -1;
@ -62,6 +66,7 @@ int main(int argc, char *argv[])
int opt; int opt;
bool terminal = FALSE; bool terminal = FALSE;
const char *cid = NULL; const char *cid = NULL;
const char *runtime_path = NULL;
char cmd[CMD_SIZE]; char cmd[CMD_SIZE];
GError *err = NULL; GError *err = NULL;
_cleanup_free_ char *contents; _cleanup_free_ char *contents;
@ -80,7 +85,7 @@ int main(int argc, char *argv[])
char *sync_pipe, *endptr; char *sync_pipe, *endptr;
int len; int len;
while ((opt = getopt(argc, argv, "tc:")) != -1) { while ((opt = getopt(argc, argv, "tc:r:")) != -1) {
switch (opt) { switch (opt) {
case 't': case 't':
terminal = TRUE; terminal = TRUE;
@ -88,21 +93,30 @@ int main(int argc, char *argv[])
case 'c': case 'c':
cid = optarg; cid = optarg;
break; break;
case 'r':
runtime_path = optarg;
break;
case '?': case '?':
if (optopt == 'c') if (optopt == 'c' || optopt == 'r')
nexit("Option -%c requires an argument.", optopt); nexit("Option -%c requires an argument.",
optopt);
else if (isprint(optopt)) else if (isprint(optopt))
nexit("Unknown option `-%c'.", optopt); nexit("Unknown option `-%c'.", optopt);
else else
nexit("Unknown option character `\\x%x'.\n", optopt); nexit("Unknown option character `\\x%x'.\n",
optopt);
default: default:
nexit("Usage: %s [-c container_id] [-t]", argv[0]); nexit
("Usage: %s -r runtime_path [-c container_id] [-t]",
argv[0]);
} }
} }
if (cid == NULL) { if (cid == NULL)
nexit("Container ID not passed"); nexit("Container ID not provided");
}
if (runtime_path == NULL)
nexit("Runtime path not provided");
sync_pipe = getenv("_OCI_SYNCPIPE"); sync_pipe = getenv("_OCI_SYNCPIPE");
if (sync_pipe) { if (sync_pipe) {
@ -146,9 +160,12 @@ int main(int argc, char *argv[])
/* Create the container */ /* Create the container */
if (terminal) { if (terminal) {
snprintf(cmd, CMD_SIZE, "runc create %s --pid-file pidfile --console %s", cid, slname); snprintf(cmd, CMD_SIZE,
"%s create %s --pid-file pidfile --console %s",
runtime_path, cid, slname);
} else { } else {
snprintf(cmd, CMD_SIZE, "runc create %s --pid-file pidfile", cid); snprintf(cmd, CMD_SIZE, "%s create %s --pid-file pidfile",
runtime_path, cid);
} }
ret = system(cmd); ret = system(cmd);
if (ret != 0) { if (ret != 0) {
@ -180,9 +197,11 @@ int main(int argc, char *argv[])
pexit("tcegetattr"); pexit("tcegetattr");
/* Settings for raw mode */ /* Settings for raw mode */
t.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL | IEXTEN); t.c_lflag &=
t.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR | INPCK | ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL | IEXTEN);
ISTRIP | IXON | IXOFF | IGNPAR | PARMRK); t.c_iflag &=
~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR | INPCK | ISTRIP |
IXON | IXOFF | IGNPAR | PARMRK);
t.c_oflag &= ~OPOST; t.c_oflag &= ~OPOST;
t.c_cc[VMIN] = 1; t.c_cc[VMIN] = 1;
t.c_cc[VTIME] = 0; t.c_cc[VTIME] = 0;
@ -214,26 +233,36 @@ int main(int argc, char *argv[])
for (int i = 0; i < ready; i++) { for (int i = 0; i < ready; i++) {
if (evlist[i].events & EPOLLIN) { if (evlist[i].events & EPOLLIN) {
if (evlist[i].data.fd == STDIN_FILENO) { if (evlist[i].data.fd == STDIN_FILENO) {
num_read = read(STDIN_FILENO, buf, BUF_SIZE); num_read =
read(STDIN_FILENO, buf,
BUF_SIZE);
if (num_read <= 0) if (num_read <= 0)
goto out; goto out;
if (write(mfd, buf, num_read) != num_read) { if (write(mfd, buf, num_read) !=
nwarn("partial/failed write (masterFd)"); num_read) {
nwarn
("partial/failed write (masterFd)");
goto out; goto out;
} }
} else if (evlist[i].data.fd == mfd) { } else if (evlist[i].data.fd == mfd) {
num_read = read(mfd, buf, BUF_SIZE); num_read =
read(mfd, buf, BUF_SIZE);
if (num_read <= 0) if (num_read <= 0)
goto out; goto out;
if (write(STDOUT_FILENO, buf, num_read) != num_read) { if (write
nwarn("partial/failed write (STDOUT_FILENO)"); (STDOUT_FILENO, buf,
num_read) != num_read) {
nwarn
("partial/failed write (STDOUT_FILENO)");
goto out; goto out;
} }
} }
} else if (evlist[i].events & (EPOLLHUP | EPOLLERR)) { } else if (evlist[i].events &
printf("closing fd %d\n", evlist[i].data.fd); (EPOLLHUP | EPOLLERR)) {
printf("closing fd %d\n",
evlist[i].data.fd);
if (close(evlist[i].data.fd) < 0) if (close(evlist[i].data.fd) < 0)
pexit("close"); pexit("close");
goto out; goto out;
@ -253,9 +282,12 @@ out:
if (ret < 0) { if (ret < 0) {
pexit("Failed to allocate memory for status"); pexit("Failed to allocate memory for status");
} }
g_file_set_contents("exit", status_str, strlen(status_str), &err); g_file_set_contents("exit", status_str,
strlen(status_str), &err);
if (err) { if (err) {
fprintf(stderr, "Failed to write %s to exit file: %s\n", status_str, err->message); fprintf(stderr,
"Failed to write %s to exit file: %s\n",
status_str, err->message);
g_error_free(err); g_error_free(err);
exit(1); exit(1);
} }

View file

@ -85,6 +85,7 @@ func (r *Runtime) CreateContainer(c *Container) error {
defer parentPipe.Close() defer parentPipe.Close()
args := []string{"-c", c.name} args := []string{"-c", c.name}
args = append(args, "-r", r.path)
if c.terminal { if c.terminal {
args = append(args, "-t") args = append(args, "-t")
} }

View file

@ -61,6 +61,12 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
return nil, err return nil, err
} }
defer func() {
if err != nil {
os.RemoveAll(podSandboxDir)
}
}()
// creates a spec Generator with the default spec. // creates a spec Generator with the default spec.
g := generate.New() g := generate.New()
@ -84,8 +90,11 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
dnsServers := req.GetConfig().GetDnsOptions().GetServers() dnsServers := req.GetConfig().GetDnsOptions().GetServers()
dnsSearches := req.GetConfig().GetDnsOptions().GetSearches() dnsSearches := req.GetConfig().GetDnsOptions().GetSearches()
resolvPath := fmt.Sprintf("%s/resolv.conf", podSandboxDir) resolvPath := fmt.Sprintf("%s/resolv.conf", podSandboxDir)
if err := parseDNSOptions(dnsServers, dnsSearches, resolvPath); err != nil { err = parseDNSOptions(dnsServers, dnsSearches, resolvPath)
if err1 := removeFile(resolvPath); err1 != nil { if err != nil {
err1 := removeFile(resolvPath)
if err1 != nil {
err = err1
return nil, fmt.Errorf("%v; failed to remove %s: %v", err, resolvPath, err1) return nil, fmt.Errorf("%v; failed to remove %s: %v", err, resolvPath, err1)
} }
return nil, err return nil, err
@ -113,21 +122,21 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
// set up namespaces // set up namespaces
if req.GetConfig().GetLinux().GetNamespaceOptions().GetHostNetwork() { if req.GetConfig().GetLinux().GetNamespaceOptions().GetHostNetwork() {
err := g.RemoveLinuxNamespace("network") err = g.RemoveLinuxNamespace("network")
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
if req.GetConfig().GetLinux().GetNamespaceOptions().GetHostPid() { if req.GetConfig().GetLinux().GetNamespaceOptions().GetHostPid() {
err := g.RemoveLinuxNamespace("pid") err = g.RemoveLinuxNamespace("pid")
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
if req.GetConfig().GetLinux().GetNamespaceOptions().GetHostIpc() { if req.GetConfig().GetLinux().GetNamespaceOptions().GetHostIpc() {
err := g.RemoveLinuxNamespace("ipc") err = g.RemoveLinuxNamespace("ipc")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -144,11 +153,11 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
return nil, err return nil, err
} }
if err := s.runtime.CreateContainer(container); err != nil { if err = s.runtime.CreateContainer(container); err != nil {
return nil, err return nil, err
} }
if err := s.runtime.UpdateStatus(container); err != nil { if err = s.runtime.UpdateStatus(container); err != nil {
return nil, err return nil, err
} }
@ -158,17 +167,17 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := s.netPlugin.SetUpPod(netnsPath, podNamespace, name, containerName); err != nil { if err = s.netPlugin.SetUpPod(netnsPath, podNamespace, name, containerName); err != nil {
return nil, fmt.Errorf("failed to create network for container %s in sandbox %s: %v", containerName, name, err) return nil, fmt.Errorf("failed to create network for container %s in sandbox %s: %v", containerName, name, err)
} }
if err := s.runtime.StartContainer(container); err != nil { if err = s.runtime.StartContainer(container); err != nil {
return nil, err return nil, err
} }
s.addContainer(container) s.addContainer(container)
if err := s.runtime.UpdateStatus(container); err != nil { if err = s.runtime.UpdateStatus(container); err != nil {
return nil, err return nil, err
} }