You've already forked runc
mirror of
https://github.com/opencontainers/runc.git
synced 2025-07-29 06:41:12 +03:00
Exec erros from the exec() syscall in the container's init should be treated as if the container ran but couldn't execute the process for the user instead of returning a libcontainer error as if it was an issue in the library. Before specifying different commands like `/etc`, `asldfkjasdlfj`, or `/alsdjfkasdlfj` would always return 1 on the command line with a libcontainer specific error message. Now they return the correct message and exit status defined for unix processes. Example: ```bash root@deathstar:/containers/redis# runc start test exec: "/asdlfkjasldkfj": file does not exist root@deathstar:/containers/redis# echo $? 127 root@deathstar:/containers/redis# runc start test exec: "asdlfkjasldkfj": executable file not found in $PATH root@deathstar:/containers/redis# echo $? 127 root@deathstar:/containers/redis# runc start test exec: "/etc": permission denied root@deathstar:/containers/redis# echo $? 126 ``` Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
package integration
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
|
|
_ "github.com/opencontainers/runc/libcontainer/nsenter"
|
|
)
|
|
|
|
// init runs the libcontainer initialization code because of the busybox style needs
|
|
// to work around the go runtime and the issues with forking
|
|
func init() {
|
|
if len(os.Args) < 2 || os.Args[1] != "init" {
|
|
return
|
|
}
|
|
runtime.GOMAXPROCS(1)
|
|
runtime.LockOSThread()
|
|
factory, err := libcontainer.New("")
|
|
if err != nil {
|
|
logrus.Fatalf("unable to initialize for container: %s", err)
|
|
}
|
|
if err := factory.StartInitialization(); err != nil {
|
|
// return proper unix error codes
|
|
if exerr, ok := err.(*exec.Error); ok {
|
|
switch exerr.Err {
|
|
case os.ErrPermission:
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(126)
|
|
case exec.ErrNotFound:
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(127)
|
|
default:
|
|
if os.IsNotExist(exerr.Err) {
|
|
fmt.Fprintf(os.Stderr, "exec: %s: %v\n", strconv.Quote(exerr.Name), os.ErrNotExist)
|
|
os.Exit(127)
|
|
}
|
|
}
|
|
}
|
|
logrus.Fatal(err)
|
|
}
|
|
panic("init: init failed to start contianer")
|
|
}
|
|
|
|
var (
|
|
factory libcontainer.Factory
|
|
systemdFactory libcontainer.Factory
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
var (
|
|
err error
|
|
ret int = 0
|
|
)
|
|
|
|
logrus.SetOutput(os.Stderr)
|
|
logrus.SetLevel(logrus.InfoLevel)
|
|
|
|
factory, err = libcontainer.New(".", libcontainer.Cgroupfs)
|
|
if err != nil {
|
|
logrus.Error(err)
|
|
os.Exit(1)
|
|
}
|
|
if systemd.UseSystemd() {
|
|
systemdFactory, err = libcontainer.New(".", libcontainer.SystemdCgroups)
|
|
if err != nil {
|
|
logrus.Error(err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
ret = m.Run()
|
|
os.Exit(ret)
|
|
}
|