Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import gojail pkg into runj repo #13

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions cmd/runj-entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"syscall"

"go.sbk.wtf/runj/pkg/gojail"
"go.sbk.wtf/runj/state"

"github.com/containerd/console"
"golang.org/x/sys/unix"
)
Expand All @@ -48,7 +52,6 @@ func main() {
var errUsage = errors.New("usage: runj-entrypoint JAIL-ID FIFO-PATH PROGRAM [ARGS...]")

const (
jexecPath = "/usr/sbin/jexec"
consoleSocketEnv = "__RUNJ_CONSOLE_SOCKET"

// skipExecFifo signals that the exec fifo sync procedure should be skipped
Expand All @@ -59,9 +62,17 @@ func _main() (int, error) {
if len(os.Args) < 4 {
return 1, errUsage
}
jid := os.Args[1]
id := os.Args[1]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why pass the container ID and not the JID? id appears to only be used to load the state (line 70), and then the resulting state is only used to find the JID (line 75).

I'd prefer to avoid having runj-entrypoint read the state file if possible.

fifoPath := os.Args[2]
argv := os.Args[3:]
command := os.Args[3]
argv := os.Args[4:]

s, err := state.Load(id)
if err != nil {
return 1, err
}

jid := gojail.JailID(s.JID)

if err := setupConsole(); err != nil {
return 2, err
Expand All @@ -78,8 +89,25 @@ func _main() (int, error) {
}
}

jail, err := gojail.JailGetByID(jid)
if err != nil {
return 5, err
}
//jail attach places us inside the jail, and implicitly does a chroot and cwd to
//the jail root path
err = jail.Attach()
if err != nil {
return 6, err
}

//we need to lookup the full path of the supplied command, otherwise unix.Exec
//might fail if we are not supplied with a full path to the command
cmdpath, err := exec.LookPath(command)
if err != nil {
return 6, fmt.Errorf("Could not find start command %w", err)
}
// call unix.Exec (which is execve(2)) to replace this process with the jexec
if err := unix.Exec(jexecPath, append([]string{"jexec", jid}, argv...), unix.Environ()); err != nil {
if err := unix.Exec(cmdpath, append([]string{command}, argv...), unix.Environ()); err != nil {
return 6, fmt.Errorf("failed to exec: %w", err)
}
return 0, nil
Expand Down
18 changes: 12 additions & 6 deletions cmd/runj/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"go.sbk.wtf/runj/jail"
"go.sbk.wtf/runj/oci"
"go.sbk.wtf/runj/pkg/gojail"
"go.sbk.wtf/runj/runtimespec"
"go.sbk.wtf/runj/state"

Expand Down Expand Up @@ -129,14 +130,19 @@ the console's pseudoterminal`)
} else if *consoleSocket != "" {
return errors.New("console-socket provided but Process.Terminal is false")
}
var confPath string
confPath, err = jail.CreateConfig(id, rootPath)

jailconfig := make(map[string]interface{})
jailconfig["name"] = id
jailconfig["path"] = rootPath
jailconfig["persist"] = true

j, err := gojail.JailCreate(jailconfig)
if err != nil {
return err
}
if err := jail.CreateJail(cmd.Context(), confPath); err != nil {
return err
return fmt.Errorf("failed creating jail: %w", err)
}
s.JID = int(j.ID())
s.Save()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving should be handled by the defer on line 93. If we remove the need for runj-entrypoint to read the file, we shouldn't need to save here.


err = jail.Mount(ociConfig)
if err != nil {
return err
Expand Down
15 changes: 8 additions & 7 deletions cmd/runj/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import (
"errors"
"fmt"
"os"

"go.sbk.wtf/runj/jail"
"go.sbk.wtf/runj/oci"
"go.sbk.wtf/runj/pkg/gojail"
"go.sbk.wtf/runj/runtimespec"
"go.sbk.wtf/runj/state"

Expand All @@ -32,6 +32,10 @@ func deleteCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
disableUsage(cmd)
id := args[0]
s, err := state.Load(id)
if err != nil {
return fmt.Errorf("delete: failed to load state: %w", err)
}
running, err := jail.IsRunning(cmd.Context(), id, 0)
if err != nil {
return fmt.Errorf("delete: failed to determine if jail is running: %w", err)
Expand All @@ -43,14 +47,11 @@ func deleteCommand() *cobra.Command {
if err != nil {
return fmt.Errorf("delete: failed to find entrypoint process: %w", err)
}
confPath := jail.ConfPath(id)
if _, err := os.Stat(confPath); err != nil {
return errors.New("invalid jail id provided")
}
err = jail.DestroyJail(cmd.Context(), confPath, id)
j, err := gojail.JailGetByID(gojail.JailID(s.JID))
if err != nil {
return err
return fmt.Errorf("delete: failed to get jail: %w", err)
}
j.Destroy()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
j.Destroy()
err = j.Destroy()
if err != nil {
return err
}

var ociConfig *runtimespec.Spec
ociConfig, err = oci.LoadConfig(id)
if err != nil {
Expand Down
65 changes: 0 additions & 65 deletions jail/conf.go

This file was deleted.

20 changes: 0 additions & 20 deletions jail/conf_test.go

This file was deleted.

28 changes: 0 additions & 28 deletions jail/jail8.go

This file was deleted.

Loading