Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit d74aaa3

Browse files
committed
make exit code of exec/container consist with docker
The exit code from `docker run` gives information about why the container failed to run or why it exited. When `docker run` exits with a non-zero code, the exit codes follow the `chroot` standard, see below: **_125_** if the error is with Docker daemon **_itself_** $ docker run --foo busybox; echo $? # flag provided but not defined: --foo See 'docker run --help'. 125 **_126_** if the **_contained command_** cannot be invoked $ docker run busybox /etc; echo $? # docker: Error response from daemon: Container command '/etc' could not be invoked. 126 **_127_** if the **_contained command_** cannot be found $ docker run busybox foo; echo $? # docker: Error response from daemon: Container command 'foo' not found or does not exist. 127 **_Exit code_** of **_contained command_** otherwise $ docker run busybox /bin/sh -c 'exit 3'; echo $? # 3 Signed-off-by: Gao feng <omarapazanadi@gmail.com>
1 parent 9615c24 commit d74aaa3

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/container.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,15 @@ static int hyper_container_init(void *data)
624624
execvp(container->exec.argv[0], container->exec.argv);
625625
perror("exec container command failed");
626626

627+
/* the exit codes follow the `chroot` standard,
628+
see docker/docs/reference/run.md#exit-status */
629+
if (errno == ENOENT)
630+
_exit(127);
631+
else if (errno == EACCES)
632+
_exit(126);
633+
627634
fail:
628-
_exit(-1);
635+
_exit(125);
629636
}
630637

631638
static int hyper_setup_pty(struct hyper_container *c)

src/exec.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,10 +590,20 @@ static int hyper_do_exec_cmd(void *data)
590590
goto exit;
591591
}
592592

593-
if (execvp(exec->argv[0], exec->argv) < 0)
593+
if (execvp(exec->argv[0], exec->argv) < 0) {
594594
perror("exec failed");
595+
596+
/* the exit codes follow the `chroot` standard,
597+
see docker/docs/reference/run.md#exit-status */
598+
if (errno == ENOENT)
599+
exit(127);
600+
else if (errno == EACCES)
601+
exit(126);
602+
603+
}
604+
595605
exit:
596-
_exit(ret);
606+
_exit(125);
597607
out:
598608
hyper_send_type(arg->pipe[1], ret ? ERROR : READY);
599609
_exit(ret);

0 commit comments

Comments
 (0)